1 //===- HexagonGenMux.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // During instruction selection, MUX instructions are generated for
11 // conditional assignments. Since such assignments often present an
12 // opportunity to predicate instructions, HexagonExpandCondsets
13 // expands MUXes into pairs of conditional transfers, and then proceeds
14 // with predication of the producers/consumers of the registers involved.
15 // This happens after exiting from the SSA form, but before the machine
16 // instruction scheduler. After the scheduler and after the register
17 // allocation there can be cases of pairs of conditional transfers
18 // resulting from a MUX where neither of them was further predicated. If
19 // these transfers are now placed far enough from the instruction defining
20 // the predicate register, they cannot use the .new form. In such cases it
21 // is better to collapse them back to a single MUX instruction.
23 #define DEBUG_TYPE "hexmux"
25 #include "HexagonInstrInfo.h"
26 #include "HexagonRegisterInfo.h"
27 #include "HexagonSubtarget.h"
28 #include "llvm/ADT/BitVector.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/CodeGen/LivePhysRegs.h"
33 #include "llvm/CodeGen/MachineBasicBlock.h"
34 #include "llvm/CodeGen/MachineFunction.h"
35 #include "llvm/CodeGen/MachineFunctionPass.h"
36 #include "llvm/CodeGen/MachineInstr.h"
37 #include "llvm/CodeGen/MachineInstrBuilder.h"
38 #include "llvm/CodeGen/MachineOperand.h"
39 #include "llvm/IR/DebugLoc.h"
40 #include "llvm/MC/MCInstrDesc.h"
41 #include "llvm/MC/MCRegisterInfo.h"
42 #include "llvm/Pass.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/MathExtras.h"
55 FunctionPass *createHexagonGenMux();
56 void initializeHexagonGenMuxPass(PassRegistry& Registry);
58 } // end namespace llvm
60 // Initialize this to 0 to always prefer generating mux by default.
61 static cl::opt<unsigned> MinPredDist("hexagon-gen-mux-threshold", cl::Hidden,
62 cl::init(0), cl::desc("Minimum distance between predicate definition and "
63 "farther of the two predicated uses"));
67 class HexagonGenMux : public MachineFunctionPass {
71 HexagonGenMux() : MachineFunctionPass(ID) {}
73 StringRef getPassName() const override {
74 return "Hexagon generate mux instructions";
77 void getAnalysisUsage(AnalysisUsage &AU) const override {
78 MachineFunctionPass::getAnalysisUsage(AU);
81 bool runOnMachineFunction(MachineFunction &MF) override;
83 MachineFunctionProperties getRequiredProperties() const override {
84 return MachineFunctionProperties().set(
85 MachineFunctionProperties::Property::NoVRegs);
89 const HexagonInstrInfo *HII = nullptr;
90 const HexagonRegisterInfo *HRI = nullptr;
94 unsigned TrueX = std::numeric_limits<unsigned>::max();
95 unsigned FalseX = std::numeric_limits<unsigned>::max();
97 CondsetInfo() = default;
101 BitVector Defs, Uses;
103 DefUseInfo() = default;
104 DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
108 MachineBasicBlock::iterator At;
109 unsigned DefR, PredR;
110 MachineOperand *SrcT, *SrcF;
111 MachineInstr *Def1, *Def2;
113 MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
114 MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
116 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
120 using InstrIndexMap = DenseMap<MachineInstr *, unsigned>;
121 using DefUseInfoMap = DenseMap<unsigned, DefUseInfo>;
122 using MuxInfoList = SmallVector<MuxInfo, 4>;
124 bool isRegPair(unsigned Reg) const {
125 return Hexagon::DoubleRegsRegClass.contains(Reg);
128 void getSubRegs(unsigned Reg, BitVector &SRs) const;
129 void expandReg(unsigned Reg, BitVector &Set) const;
130 void getDefsUses(const MachineInstr *MI, BitVector &Defs,
131 BitVector &Uses) const;
132 void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
134 bool isCondTransfer(unsigned Opc) const;
135 unsigned getMuxOpcode(const MachineOperand &Src1,
136 const MachineOperand &Src2) const;
137 bool genMuxInBlock(MachineBasicBlock &B);
140 } // end anonymous namespace
142 char HexagonGenMux::ID = 0;
144 INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux",
145 "Hexagon generate mux instructions", false, false)
147 void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
148 for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I)
152 void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
154 getSubRegs(Reg, Set);
159 void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
160 BitVector &Uses) const {
161 // First, get the implicit defs and uses for this instruction.
162 unsigned Opc = MI->getOpcode();
163 const MCInstrDesc &D = HII->get(Opc);
164 if (const MCPhysReg *R = D.ImplicitDefs)
166 expandReg(*R++, Defs);
167 if (const MCPhysReg *R = D.ImplicitUses)
169 expandReg(*R++, Uses);
171 // Look over all operands, and collect explicit defs and uses.
172 for (const MachineOperand &MO : MI->operands()) {
173 if (!MO.isReg() || MO.isImplicit())
175 unsigned R = MO.getReg();
176 BitVector &Set = MO.isDef() ? Defs : Uses;
181 void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
182 DefUseInfoMap &DUM) {
184 unsigned NR = HRI->getNumRegs();
185 BitVector Defs(NR), Uses(NR);
187 for (MachineBasicBlock::iterator I = B.begin(), E = B.end(); I != E; ++I) {
188 MachineInstr *MI = &*I;
189 I2X.insert(std::make_pair(MI, Index));
192 getDefsUses(MI, Defs, Uses);
193 DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
198 bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
200 case Hexagon::A2_tfrt:
201 case Hexagon::A2_tfrf:
202 case Hexagon::C2_cmoveit:
203 case Hexagon::C2_cmoveif:
209 unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
210 const MachineOperand &Src2) const {
211 bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
213 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
215 return Hexagon::C2_muxri;
217 // Neither is a register. The first source is extendable, but the second
219 if (Src2.isImm() && isInt<8>(Src2.getImm()))
220 return Hexagon::C2_muxii;
225 bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
226 bool Changed = false;
229 buildMaps(B, I2X, DUM);
231 using CondsetMap = DenseMap<unsigned, CondsetInfo>;
236 MachineBasicBlock::iterator NextI, End = B.end();
237 for (MachineBasicBlock::iterator I = B.begin(); I != End; I = NextI) {
238 MachineInstr *MI = &*I;
239 NextI = std::next(I);
240 unsigned Opc = MI->getOpcode();
241 if (!isCondTransfer(Opc))
243 unsigned DR = MI->getOperand(0).getReg();
246 MachineOperand &PredOp = MI->getOperand(1);
247 if (PredOp.isUndef())
250 unsigned PR = PredOp.getReg();
251 unsigned Idx = I2X.lookup(MI);
252 CondsetMap::iterator F = CM.find(DR);
253 bool IfTrue = HII->isPredicatedTrue(Opc);
255 // If there is no record of a conditional transfer for this register,
256 // or the predicate register differs, create a new record for it.
257 if (F != CM.end() && F->second.PredR != PR) {
262 auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
264 F->second.PredR = PR;
266 CondsetInfo &CI = F->second;
271 if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
272 CI.FalseX == std::numeric_limits<unsigned>::max())
275 // There is now a complete definition of DR, i.e. we have the predicate
276 // register, the definition if-true, and definition if-false.
278 // First, check if the definitions are far enough from the definition
279 // of the predicate register.
280 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
281 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
282 // Specifically, check if the predicate definition is within a prescribed
283 // distance from the farther of the two predicated instructions.
284 unsigned SearchX = (MaxX >= MinPredDist) ? MaxX-MinPredDist : 0;
285 bool NearDef = false;
286 for (unsigned X = SearchX; X < MaxX; ++X) {
287 const DefUseInfo &DU = DUM.lookup(X);
296 // The predicate register is not defined in the last few instructions.
297 // Check if the conversion to MUX is possible (either "up", i.e. at the
298 // place of the earlier partial definition, or "down", where the later
299 // definition is located). Examine all defs and uses between these two
301 // SR1, SR2 - source registers from the first and the second definition.
302 MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
303 std::advance(It1, MinX);
304 std::advance(It2, MaxX);
305 MachineInstr &Def1 = *It1, &Def2 = *It2;
306 MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);
307 unsigned SR1 = Src1->isReg() ? Src1->getReg() : 0;
308 unsigned SR2 = Src2->isReg() ? Src2->getReg() : 0;
309 bool Failure = false, CanUp = true, CanDown = true;
310 for (unsigned X = MinX+1; X < MaxX; X++) {
311 const DefUseInfo &DU = DUM.lookup(X);
312 if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
316 if (CanDown && DU.Defs[SR1])
318 if (CanUp && DU.Defs[SR2])
321 if (Failure || (!CanUp && !CanDown))
324 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
325 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
326 // Prefer "down", since this will move the MUX farther away from the
327 // predicate definition.
328 MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
329 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
332 for (MuxInfo &MX : ML) {
333 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
336 MachineBasicBlock &B = *MX.At->getParent();
337 const DebugLoc &DL = B.findDebugLoc(MX.At);
338 auto NewMux = BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
342 NewMux->clearKillInfo();
348 // Fix up kill flags.
350 LivePhysRegs LPR(*HRI);
352 auto IsLive = [&LPR,this] (unsigned Reg) -> bool {
353 for (MCSubRegIterator S(Reg, HRI, true); S.isValid(); ++S)
354 if (LPR.contains(*S))
358 for (auto I = B.rbegin(), E = B.rend(); I != E; ++I) {
359 if (I->isDebugInstr())
361 // This isn't 100% accurate, but it's safe.
362 // It won't detect (as a kill) a case like this
363 // r0 = add r0, 1 <-- r0 should be "killed"
365 for (MachineOperand &Op : I->operands()) {
366 if (!Op.isReg() || !Op.isUse())
368 assert(Op.getSubReg() == 0 && "Should have physical registers only");
369 bool Live = IsLive(Op.getReg());
372 LPR.stepBackward(*I);
378 bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
379 if (skipFunction(MF.getFunction()))
381 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
382 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
383 bool Changed = false;
385 Changed |= genMuxInBlock(I);
389 FunctionPass *llvm::createHexagonGenMux() {
390 return new HexagonGenMux();