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 "llvm/CodeGen/Passes.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "HexagonTargetMachine.h"
34 FunctionPass *createHexagonGenMux();
35 void initializeHexagonGenMuxPass(PassRegistry& Registry);
39 class HexagonGenMux : public MachineFunctionPass {
42 HexagonGenMux() : MachineFunctionPass(ID), HII(0), HRI(0) {
43 initializeHexagonGenMuxPass(*PassRegistry::getPassRegistry());
45 const char *getPassName() const override {
46 return "Hexagon generate mux instructions";
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 MachineFunctionPass::getAnalysisUsage(AU);
51 bool runOnMachineFunction(MachineFunction &MF) override;
54 const HexagonInstrInfo *HII;
55 const HexagonRegisterInfo *HRI;
59 unsigned TrueX, FalseX;
60 CondsetInfo() : PredR(0), TrueX(UINT_MAX), FalseX(UINT_MAX) {}
64 DefUseInfo() : Defs(), Uses() {}
65 DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
68 MachineBasicBlock::iterator At;
70 MachineOperand *SrcT, *SrcF;
71 MachineInstr *Def1, *Def2;
72 MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
73 MachineOperand *TOp, MachineOperand *FOp,
74 MachineInstr *D1, MachineInstr *D2)
75 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(D1),
78 typedef DenseMap<MachineInstr*,unsigned> InstrIndexMap;
79 typedef DenseMap<unsigned,DefUseInfo> DefUseInfoMap;
80 typedef SmallVector<MuxInfo,4> MuxInfoList;
82 bool isRegPair(unsigned Reg) const {
83 return Hexagon::DoubleRegsRegClass.contains(Reg);
85 void getSubRegs(unsigned Reg, BitVector &SRs) const;
86 void expandReg(unsigned Reg, BitVector &Set) const;
87 void getDefsUses(const MachineInstr *MI, BitVector &Defs,
88 BitVector &Uses) const;
89 void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
91 bool isCondTransfer(unsigned Opc) const;
92 unsigned getMuxOpcode(const MachineOperand &Src1,
93 const MachineOperand &Src2) const;
94 bool genMuxInBlock(MachineBasicBlock &B);
97 char HexagonGenMux::ID = 0;
100 INITIALIZE_PASS(HexagonGenMux, "hexagon-mux",
101 "Hexagon generate mux instructions", false, false)
104 void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
105 for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I)
110 void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
112 getSubRegs(Reg, Set);
118 void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
119 BitVector &Uses) const {
120 // First, get the implicit defs and uses for this instruction.
121 unsigned Opc = MI->getOpcode();
122 const MCInstrDesc &D = HII->get(Opc);
123 if (const uint16_t *R = D.ImplicitDefs)
125 expandReg(*R++, Defs);
126 if (const uint16_t *R = D.ImplicitUses)
128 expandReg(*R++, Uses);
130 // Look over all operands, and collect explicit defs and uses.
131 for (ConstMIOperands Mo(MI); Mo.isValid(); ++Mo) {
132 if (!Mo->isReg() || Mo->isImplicit())
134 unsigned R = Mo->getReg();
135 BitVector &Set = Mo->isDef() ? Defs : Uses;
141 void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
142 DefUseInfoMap &DUM) {
144 unsigned NR = HRI->getNumRegs();
145 BitVector Defs(NR), Uses(NR);
147 for (MachineBasicBlock::iterator I = B.begin(), E = B.end(); I != E; ++I) {
148 MachineInstr *MI = &*I;
149 I2X.insert(std::make_pair(MI, Index));
152 getDefsUses(MI, Defs, Uses);
153 DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
159 bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
161 case Hexagon::A2_tfrt:
162 case Hexagon::A2_tfrf:
163 case Hexagon::C2_cmoveit:
164 case Hexagon::C2_cmoveif:
171 unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
172 const MachineOperand &Src2) const {
173 bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
175 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
177 return Hexagon::C2_muxri;
179 // Neither is a register. The first source is extendable, but the second
181 if (Src2.isImm() && isInt<8>(Src2.getImm()))
182 return Hexagon::C2_muxii;
188 bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
189 bool Changed = false;
192 buildMaps(B, I2X, DUM);
194 typedef DenseMap<unsigned,CondsetInfo> CondsetMap;
198 MachineBasicBlock::iterator NextI, End = B.end();
199 for (MachineBasicBlock::iterator I = B.begin(); I != End; I = NextI) {
200 MachineInstr *MI = &*I;
201 NextI = std::next(I);
202 unsigned Opc = MI->getOpcode();
203 if (!isCondTransfer(Opc))
205 unsigned DR = MI->getOperand(0).getReg();
209 unsigned PR = MI->getOperand(1).getReg();
210 unsigned Idx = I2X.lookup(MI);
211 CondsetMap::iterator F = CM.find(DR);
212 bool IfTrue = HII->isPredicatedTrue(Opc);
214 // If there is no record of a conditional transfer for this register,
215 // or the predicate register differs, create a new record for it.
216 if (F != CM.end() && F->second.PredR != PR) {
221 auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
223 F->second.PredR = PR;
225 CondsetInfo &CI = F->second;
230 if (CI.TrueX == UINT_MAX || CI.FalseX == UINT_MAX)
233 // There is now a complete definition of DR, i.e. we have the predicate
234 // register, the definition if-true, and definition if-false.
236 // First, check if both definitions are far enough from the definition
237 // of the predicate register.
238 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
239 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
240 unsigned SearchX = (MaxX > 4) ? MaxX-4 : 0;
241 bool NearDef = false;
242 for (unsigned X = SearchX; X < MaxX; ++X) {
243 const DefUseInfo &DU = DUM.lookup(X);
252 // The predicate register is not defined in the last few instructions.
253 // Check if the conversion to MUX is possible (either "up", i.e. at the
254 // place of the earlier partial definition, or "down", where the later
255 // definition is located). Examine all defs and uses between these two
257 // SR1, SR2 - source registers from the first and the second definition.
258 MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
259 std::advance(It1, MinX);
260 std::advance(It2, MaxX);
261 MachineInstr *Def1 = It1, *Def2 = It2;
262 MachineOperand *Src1 = &Def1->getOperand(2), *Src2 = &Def2->getOperand(2);
263 unsigned SR1 = Src1->isReg() ? Src1->getReg() : 0;
264 unsigned SR2 = Src2->isReg() ? Src2->getReg() : 0;
265 bool Failure = false, CanUp = true, CanDown = true;
266 for (unsigned X = MinX+1; X < MaxX; X++) {
267 const DefUseInfo &DU = DUM.lookup(X);
268 if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
272 if (CanDown && DU.Defs[SR1])
274 if (CanUp && DU.Defs[SR2])
277 if (Failure || (!CanUp && !CanDown))
280 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
281 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
282 // Prefer "down", since this will move the MUX farther away from the
283 // predicate definition.
284 MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
285 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
288 for (unsigned I = 0, N = ML.size(); I < N; ++I) {
290 MachineBasicBlock &B = *MX.At->getParent();
291 DebugLoc DL = MX.At->getDebugLoc();
292 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
295 BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
297 .addOperand(*MX.SrcT)
298 .addOperand(*MX.SrcF);
307 bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
308 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
309 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
310 bool Changed = false;
312 Changed |= genMuxInBlock(I);
316 FunctionPass *llvm::createHexagonGenMux() {
317 return new HexagonGenMux();