AMDGPU: Mark test as XFAIL in expensive_checks builds
[llvm-project.git] / llvm / lib / Target / Hexagon / HexagonGenMux.cpp
blobc52748a6e5f67f2a3ee1cde2cdc31cb7033190d8
1 //===- HexagonGenMux.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // During instruction selection, MUX instructions are generated for
10 // conditional assignments. Since such assignments often present an
11 // opportunity to predicate instructions, HexagonExpandCondsets
12 // expands MUXes into pairs of conditional transfers, and then proceeds
13 // with predication of the producers/consumers of the registers involved.
14 // This happens after exiting from the SSA form, but before the machine
15 // instruction scheduler. After the scheduler and after the register
16 // allocation there can be cases of pairs of conditional transfers
17 // resulting from a MUX where neither of them was further predicated. If
18 // these transfers are now placed far enough from the instruction defining
19 // the predicate register, they cannot use the .new form. In such cases it
20 // is better to collapse them back to a single MUX instruction.
22 #include "HexagonInstrInfo.h"
23 #include "HexagonRegisterInfo.h"
24 #include "HexagonSubtarget.h"
25 #include "llvm/ADT/BitVector.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/CodeGen/LiveRegUnits.h"
30 #include "llvm/CodeGen/MachineBasicBlock.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineOperand.h"
36 #include "llvm/IR/DebugLoc.h"
37 #include "llvm/MC/MCInstrDesc.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/MathExtras.h"
41 #include <algorithm>
42 #include <cassert>
43 #include <iterator>
44 #include <limits>
45 #include <utility>
47 #define DEBUG_TYPE "hexmux"
49 using namespace llvm;
51 namespace llvm {
53 FunctionPass *createHexagonGenMux();
54 void initializeHexagonGenMuxPass(PassRegistry& Registry);
56 } // end namespace llvm
58 // Initialize this to 0 to always prefer generating mux by default.
59 static cl::opt<unsigned> MinPredDist("hexagon-gen-mux-threshold", cl::Hidden,
60 cl::init(0), cl::desc("Minimum distance between predicate definition and "
61 "farther of the two predicated uses"));
63 namespace {
65 class HexagonGenMux : public MachineFunctionPass {
66 public:
67 static char ID;
69 HexagonGenMux() : MachineFunctionPass(ID) {}
71 StringRef getPassName() const override {
72 return "Hexagon generate mux instructions";
75 void getAnalysisUsage(AnalysisUsage &AU) const override {
76 MachineFunctionPass::getAnalysisUsage(AU);
79 bool runOnMachineFunction(MachineFunction &MF) override;
81 MachineFunctionProperties getRequiredProperties() const override {
82 return MachineFunctionProperties().set(
83 MachineFunctionProperties::Property::NoVRegs);
86 private:
87 const HexagonInstrInfo *HII = nullptr;
88 const HexagonRegisterInfo *HRI = nullptr;
90 struct CondsetInfo {
91 unsigned PredR = 0;
92 unsigned TrueX = std::numeric_limits<unsigned>::max();
93 unsigned FalseX = std::numeric_limits<unsigned>::max();
95 CondsetInfo() = default;
98 struct DefUseInfo {
99 BitVector Defs, Uses;
101 DefUseInfo() = default;
102 DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
105 struct MuxInfo {
106 MachineBasicBlock::iterator At;
107 unsigned DefR, PredR;
108 MachineOperand *SrcT, *SrcF;
109 MachineInstr *Def1, *Def2;
111 MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
112 MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
113 MachineInstr &D2)
114 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
115 Def2(&D2) {}
118 using InstrIndexMap = DenseMap<MachineInstr *, unsigned>;
119 using DefUseInfoMap = DenseMap<unsigned, DefUseInfo>;
120 using MuxInfoList = SmallVector<MuxInfo, 4>;
122 bool isRegPair(unsigned Reg) const {
123 return Hexagon::DoubleRegsRegClass.contains(Reg);
126 void getSubRegs(unsigned Reg, BitVector &SRs) const;
127 void expandReg(unsigned Reg, BitVector &Set) const;
128 void getDefsUses(const MachineInstr *MI, BitVector &Defs,
129 BitVector &Uses) const;
130 void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
131 DefUseInfoMap &DUM);
132 bool isCondTransfer(unsigned Opc) const;
133 unsigned getMuxOpcode(const MachineOperand &Src1,
134 const MachineOperand &Src2) const;
135 bool genMuxInBlock(MachineBasicBlock &B);
138 } // end anonymous namespace
140 char HexagonGenMux::ID = 0;
142 INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux",
143 "Hexagon generate mux instructions", false, false)
145 void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
146 for (MCPhysReg I : HRI->subregs(Reg))
147 SRs[I] = true;
150 void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
151 if (isRegPair(Reg))
152 getSubRegs(Reg, Set);
153 else
154 Set[Reg] = true;
157 void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
158 BitVector &Uses) const {
159 // First, get the implicit defs and uses for this instruction.
160 unsigned Opc = MI->getOpcode();
161 const MCInstrDesc &D = HII->get(Opc);
162 for (MCPhysReg R : D.implicit_defs())
163 expandReg(R, Defs);
164 for (MCPhysReg R : D.implicit_uses())
165 expandReg(R, Uses);
167 // Look over all operands, and collect explicit defs and uses.
168 for (const MachineOperand &MO : MI->operands()) {
169 if (!MO.isReg() || MO.isImplicit())
170 continue;
171 Register R = MO.getReg();
172 BitVector &Set = MO.isDef() ? Defs : Uses;
173 expandReg(R, Set);
177 void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
178 DefUseInfoMap &DUM) {
179 unsigned Index = 0;
180 unsigned NR = HRI->getNumRegs();
181 BitVector Defs(NR), Uses(NR);
183 for (MachineInstr &MI : B) {
184 I2X.insert(std::make_pair(&MI, Index));
185 Defs.reset();
186 Uses.reset();
187 getDefsUses(&MI, Defs, Uses);
188 DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
189 Index++;
193 bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
194 switch (Opc) {
195 case Hexagon::A2_tfrt:
196 case Hexagon::A2_tfrf:
197 case Hexagon::C2_cmoveit:
198 case Hexagon::C2_cmoveif:
199 return true;
201 return false;
204 unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
205 const MachineOperand &Src2) const {
206 bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
207 if (IsReg1)
208 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
209 if (IsReg2)
210 return Hexagon::C2_muxri;
212 // Neither is a register. The first source is extendable, but the second
213 // is not (s8).
214 if (Src2.isImm() && isInt<8>(Src2.getImm()))
215 return Hexagon::C2_muxii;
217 return 0;
220 bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
221 bool Changed = false;
222 InstrIndexMap I2X;
223 DefUseInfoMap DUM;
224 buildMaps(B, I2X, DUM);
226 using CondsetMap = DenseMap<unsigned, CondsetInfo>;
228 CondsetMap CM;
229 MuxInfoList ML;
231 for (MachineInstr &MI : llvm::make_early_inc_range(B)) {
232 unsigned Opc = MI.getOpcode();
233 if (!isCondTransfer(Opc))
234 continue;
235 Register DR = MI.getOperand(0).getReg();
236 if (isRegPair(DR))
237 continue;
238 MachineOperand &PredOp = MI.getOperand(1);
239 if (PredOp.isUndef())
240 continue;
242 Register PR = PredOp.getReg();
243 unsigned Idx = I2X.lookup(&MI);
244 CondsetMap::iterator F = CM.find(DR);
245 bool IfTrue = HII->isPredicatedTrue(Opc);
247 // If there is no record of a conditional transfer for this register,
248 // or the predicate register differs, create a new record for it.
249 if (F != CM.end() && F->second.PredR != PR) {
250 CM.erase(F);
251 F = CM.end();
253 if (F == CM.end()) {
254 auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
255 F = It.first;
256 F->second.PredR = PR;
258 CondsetInfo &CI = F->second;
259 if (IfTrue)
260 CI.TrueX = Idx;
261 else
262 CI.FalseX = Idx;
263 if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
264 CI.FalseX == std::numeric_limits<unsigned>::max())
265 continue;
267 // There is now a complete definition of DR, i.e. we have the predicate
268 // register, the definition if-true, and definition if-false.
270 // First, check if the definitions are far enough from the definition
271 // of the predicate register.
272 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
273 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
274 // Specifically, check if the predicate definition is within a prescribed
275 // distance from the farther of the two predicated instructions.
276 unsigned SearchX = (MaxX >= MinPredDist) ? MaxX-MinPredDist : 0;
277 bool NearDef = false;
278 for (unsigned X = SearchX; X < MaxX; ++X) {
279 const DefUseInfo &DU = DUM.lookup(X);
280 if (!DU.Defs[PR])
281 continue;
282 NearDef = true;
283 break;
285 if (NearDef)
286 continue;
288 // The predicate register is not defined in the last few instructions.
289 // Check if the conversion to MUX is possible (either "up", i.e. at the
290 // place of the earlier partial definition, or "down", where the later
291 // definition is located). Examine all defs and uses between these two
292 // definitions.
293 // SR1, SR2 - source registers from the first and the second definition.
294 MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
295 std::advance(It1, MinX);
296 std::advance(It2, MaxX);
297 MachineInstr &Def1 = *It1, &Def2 = *It2;
298 MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);
299 Register SR1 = Src1->isReg() ? Src1->getReg() : Register();
300 Register SR2 = Src2->isReg() ? Src2->getReg() : Register();
301 bool Failure = false, CanUp = true, CanDown = true;
302 for (unsigned X = MinX+1; X < MaxX; X++) {
303 const DefUseInfo &DU = DUM.lookup(X);
304 if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
305 Failure = true;
306 break;
308 if (CanDown && DU.Defs[SR1])
309 CanDown = false;
310 if (CanUp && DU.Defs[SR2])
311 CanUp = false;
313 if (Failure || (!CanUp && !CanDown))
314 continue;
316 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
317 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
318 // Prefer "down", since this will move the MUX farther away from the
319 // predicate definition.
320 MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
321 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
324 for (MuxInfo &MX : ML) {
325 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
326 if (!MxOpc)
327 continue;
328 // Basic correctness check: since we are deleting instructions, validate the
329 // iterators. There is a possibility that one of Def1 or Def2 is translated
330 // to "mux" and being considered for other "mux" instructions.
331 if (!MX.At->getParent() || !MX.Def1->getParent() || !MX.Def2->getParent())
332 continue;
334 MachineBasicBlock &B = *MX.At->getParent();
335 const DebugLoc &DL = B.findDebugLoc(MX.At);
336 auto NewMux = BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
337 .addReg(MX.PredR)
338 .add(*MX.SrcT)
339 .add(*MX.SrcF);
340 NewMux->clearKillInfo();
341 B.remove(MX.Def1);
342 B.remove(MX.Def2);
343 Changed = true;
346 // Fix up kill flags.
348 LiveRegUnits LPR(*HRI);
349 LPR.addLiveOuts(B);
350 for (MachineInstr &I : llvm::reverse(B)) {
351 if (I.isDebugInstr())
352 continue;
353 // This isn't 100% accurate, but it's safe.
354 // It won't detect (as a kill) a case like this
355 // r0 = add r0, 1 <-- r0 should be "killed"
356 // ... = r0
357 for (MachineOperand &Op : I.operands()) {
358 if (!Op.isReg() || !Op.isUse())
359 continue;
360 assert(Op.getSubReg() == 0 && "Should have physical registers only");
361 bool Live = !LPR.available(Op.getReg());
362 Op.setIsKill(!Live);
364 LPR.stepBackward(I);
367 return Changed;
370 bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
371 if (skipFunction(MF.getFunction()))
372 return false;
373 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
374 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
375 bool Changed = false;
376 for (auto &I : MF)
377 Changed |= genMuxInBlock(I);
378 return Changed;
381 FunctionPass *llvm::createHexagonGenMux() {
382 return new HexagonGenMux();