Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1 | //===- HexagonGenMux.cpp --------------------------------------------------===// |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 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. |
| 22 | |
| 23 | #define DEBUG_TYPE "hexmux" |
| 24 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 25 | #include "HexagonInstrInfo.h" |
| 26 | #include "HexagonRegisterInfo.h" |
| 27 | #include "HexagonSubtarget.h" |
| 28 | #include "llvm/ADT/BitVector.h" |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
| 31 | #include "llvm/ADT/StringRef.h" |
Krzysztof Parzyszek | 1a0da8d | 2017-06-22 20:43:02 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/LivePhysRegs.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 34 | #include "llvm/CodeGen/MachineFunction.h" |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineInstr.h" |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 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" |
Krzysztof Parzyszek | 5f7ba9a | 2018-03-23 18:43:09 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MathExtras.h" |
| 45 | #include <algorithm> |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 46 | #include <cassert> |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 47 | #include <iterator> |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 48 | #include <limits> |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 49 | #include <utility> |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace llvm; |
| 52 | |
| 53 | namespace llvm { |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 54 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 55 | FunctionPass *createHexagonGenMux(); |
| 56 | void initializeHexagonGenMuxPass(PassRegistry& Registry); |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 57 | |
| 58 | } // end namespace llvm |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 59 | |
Krzysztof Parzyszek | 5f7ba9a | 2018-03-23 18:43:09 +0000 | [diff] [blame] | 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")); |
| 64 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 65 | namespace { |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 66 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 67 | class HexagonGenMux : public MachineFunctionPass { |
| 68 | public: |
| 69 | static char ID; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 70 | |
Krzysztof Parzyszek | de2ac17 | 2017-06-13 16:07:36 +0000 | [diff] [blame] | 71 | HexagonGenMux() : MachineFunctionPass(ID) {} |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 72 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 73 | StringRef getPassName() const override { |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 74 | return "Hexagon generate mux instructions"; |
| 75 | } |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 76 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 77 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 78 | MachineFunctionPass::getAnalysisUsage(AU); |
| 79 | } |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 80 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 81 | bool runOnMachineFunction(MachineFunction &MF) override; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 82 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 83 | MachineFunctionProperties getRequiredProperties() const override { |
| 84 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 85 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 86 | } |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
Krzysztof Parzyszek | de2ac17 | 2017-06-13 16:07:36 +0000 | [diff] [blame] | 89 | const HexagonInstrInfo *HII = nullptr; |
| 90 | const HexagonRegisterInfo *HRI = nullptr; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 91 | |
| 92 | struct CondsetInfo { |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 93 | unsigned PredR = 0; |
| 94 | unsigned TrueX = std::numeric_limits<unsigned>::max(); |
| 95 | unsigned FalseX = std::numeric_limits<unsigned>::max(); |
| 96 | |
| 97 | CondsetInfo() = default; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 98 | }; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 99 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 100 | struct DefUseInfo { |
| 101 | BitVector Defs, Uses; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 102 | |
| 103 | DefUseInfo() = default; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 104 | DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {} |
| 105 | }; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 106 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 107 | struct MuxInfo { |
| 108 | MachineBasicBlock::iterator At; |
| 109 | unsigned DefR, PredR; |
| 110 | MachineOperand *SrcT, *SrcF; |
| 111 | MachineInstr *Def1, *Def2; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 112 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 113 | MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR, |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 114 | MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1, |
| 115 | MachineInstr &D2) |
| 116 | : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1), |
| 117 | Def2(&D2) {} |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 118 | }; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 119 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 120 | using InstrIndexMap = DenseMap<MachineInstr *, unsigned>; |
| 121 | using DefUseInfoMap = DenseMap<unsigned, DefUseInfo>; |
| 122 | using MuxInfoList = SmallVector<MuxInfo, 4>; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 123 | |
| 124 | bool isRegPair(unsigned Reg) const { |
| 125 | return Hexagon::DoubleRegsRegClass.contains(Reg); |
| 126 | } |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 127 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 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, |
| 133 | DefUseInfoMap &DUM); |
| 134 | bool isCondTransfer(unsigned Opc) const; |
| 135 | unsigned getMuxOpcode(const MachineOperand &Src1, |
| 136 | const MachineOperand &Src2) const; |
| 137 | bool genMuxInBlock(MachineBasicBlock &B); |
| 138 | }; |
| 139 | |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 140 | } // end anonymous namespace |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 141 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 142 | char HexagonGenMux::ID = 0; |
| 143 | |
Krzysztof Parzyszek | de2ac17 | 2017-06-13 16:07:36 +0000 | [diff] [blame] | 144 | INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux", |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 145 | "Hexagon generate mux instructions", false, false) |
| 146 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 147 | void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const { |
| 148 | for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I) |
| 149 | SRs[*I] = true; |
| 150 | } |
| 151 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 152 | void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const { |
| 153 | if (isRegPair(Reg)) |
| 154 | getSubRegs(Reg, Set); |
| 155 | else |
| 156 | Set[Reg] = true; |
| 157 | } |
| 158 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 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); |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 164 | if (const MCPhysReg *R = D.ImplicitDefs) |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 165 | while (*R) |
| 166 | expandReg(*R++, Defs); |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 167 | if (const MCPhysReg *R = D.ImplicitUses) |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 168 | while (*R) |
| 169 | expandReg(*R++, Uses); |
| 170 | |
| 171 | // Look over all operands, and collect explicit defs and uses. |
Matthias Braun | fc37155 | 2016-10-24 21:36:43 +0000 | [diff] [blame] | 172 | for (const MachineOperand &MO : MI->operands()) { |
| 173 | if (!MO.isReg() || MO.isImplicit()) |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 174 | continue; |
Matthias Braun | fc37155 | 2016-10-24 21:36:43 +0000 | [diff] [blame] | 175 | unsigned R = MO.getReg(); |
| 176 | BitVector &Set = MO.isDef() ? Defs : Uses; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 177 | expandReg(R, Set); |
| 178 | } |
| 179 | } |
| 180 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 181 | void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X, |
| 182 | DefUseInfoMap &DUM) { |
| 183 | unsigned Index = 0; |
| 184 | unsigned NR = HRI->getNumRegs(); |
| 185 | BitVector Defs(NR), Uses(NR); |
| 186 | |
| 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)); |
| 190 | Defs.reset(); |
| 191 | Uses.reset(); |
| 192 | getDefsUses(MI, Defs, Uses); |
| 193 | DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses))); |
| 194 | Index++; |
| 195 | } |
| 196 | } |
| 197 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 198 | bool HexagonGenMux::isCondTransfer(unsigned Opc) const { |
| 199 | switch (Opc) { |
| 200 | case Hexagon::A2_tfrt: |
| 201 | case Hexagon::A2_tfrf: |
| 202 | case Hexagon::C2_cmoveit: |
| 203 | case Hexagon::C2_cmoveif: |
| 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 209 | unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1, |
| 210 | const MachineOperand &Src2) const { |
| 211 | bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg(); |
| 212 | if (IsReg1) |
| 213 | return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir; |
| 214 | if (IsReg2) |
| 215 | return Hexagon::C2_muxri; |
| 216 | |
| 217 | // Neither is a register. The first source is extendable, but the second |
| 218 | // is not (s8). |
| 219 | if (Src2.isImm() && isInt<8>(Src2.getImm())) |
| 220 | return Hexagon::C2_muxii; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 225 | bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) { |
| 226 | bool Changed = false; |
| 227 | InstrIndexMap I2X; |
| 228 | DefUseInfoMap DUM; |
| 229 | buildMaps(B, I2X, DUM); |
| 230 | |
Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 231 | using CondsetMap = DenseMap<unsigned, CondsetInfo>; |
| 232 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 233 | CondsetMap CM; |
| 234 | MuxInfoList ML; |
| 235 | |
| 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)) |
| 242 | continue; |
| 243 | unsigned DR = MI->getOperand(0).getReg(); |
| 244 | if (isRegPair(DR)) |
| 245 | continue; |
Krzysztof Parzyszek | 8a7fb0f | 2017-06-08 20:56:36 +0000 | [diff] [blame] | 246 | MachineOperand &PredOp = MI->getOperand(1); |
| 247 | if (PredOp.isUndef()) |
| 248 | continue; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 249 | |
Krzysztof Parzyszek | 8a7fb0f | 2017-06-08 20:56:36 +0000 | [diff] [blame] | 250 | unsigned PR = PredOp.getReg(); |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 251 | unsigned Idx = I2X.lookup(MI); |
| 252 | CondsetMap::iterator F = CM.find(DR); |
| 253 | bool IfTrue = HII->isPredicatedTrue(Opc); |
| 254 | |
| 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) { |
| 258 | CM.erase(F); |
| 259 | F = CM.end(); |
| 260 | } |
| 261 | if (F == CM.end()) { |
| 262 | auto It = CM.insert(std::make_pair(DR, CondsetInfo())); |
| 263 | F = It.first; |
| 264 | F->second.PredR = PR; |
| 265 | } |
| 266 | CondsetInfo &CI = F->second; |
| 267 | if (IfTrue) |
| 268 | CI.TrueX = Idx; |
| 269 | else |
| 270 | CI.FalseX = Idx; |
Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 271 | if (CI.TrueX == std::numeric_limits<unsigned>::max() || |
| 272 | CI.FalseX == std::numeric_limits<unsigned>::max()) |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 273 | continue; |
| 274 | |
| 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. |
| 277 | |
Krzysztof Parzyszek | 5f7ba9a | 2018-03-23 18:43:09 +0000 | [diff] [blame] | 278 | // First, check if the definitions are far enough from the definition |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 279 | // of the predicate register. |
| 280 | unsigned MinX = std::min(CI.TrueX, CI.FalseX); |
| 281 | unsigned MaxX = std::max(CI.TrueX, CI.FalseX); |
Krzysztof Parzyszek | 5f7ba9a | 2018-03-23 18:43:09 +0000 | [diff] [blame] | 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; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 285 | bool NearDef = false; |
| 286 | for (unsigned X = SearchX; X < MaxX; ++X) { |
| 287 | const DefUseInfo &DU = DUM.lookup(X); |
| 288 | if (!DU.Defs[PR]) |
| 289 | continue; |
| 290 | NearDef = true; |
| 291 | break; |
| 292 | } |
| 293 | if (NearDef) |
| 294 | continue; |
| 295 | |
| 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 |
| 300 | // definitions. |
| 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); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 305 | MachineInstr &Def1 = *It1, &Def2 = *It2; |
| 306 | MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2); |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 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]) { |
| 313 | Failure = true; |
| 314 | break; |
| 315 | } |
| 316 | if (CanDown && DU.Defs[SR1]) |
| 317 | CanDown = false; |
| 318 | if (CanUp && DU.Defs[SR2]) |
| 319 | CanUp = false; |
| 320 | } |
| 321 | if (Failure || (!CanUp && !CanDown)) |
| 322 | continue; |
| 323 | |
| 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)); |
| 330 | } |
| 331 | |
Krzysztof Parzyszek | 1a0da8d | 2017-06-22 20:43:02 +0000 | [diff] [blame] | 332 | for (MuxInfo &MX : ML) { |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 333 | unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF); |
| 334 | if (!MxOpc) |
| 335 | continue; |
Krzysztof Parzyszek | 1a0da8d | 2017-06-22 20:43:02 +0000 | [diff] [blame] | 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) |
| 339 | .addReg(MX.PredR) |
| 340 | .add(*MX.SrcT) |
| 341 | .add(*MX.SrcF); |
| 342 | NewMux->clearKillInfo(); |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 343 | B.erase(MX.Def1); |
| 344 | B.erase(MX.Def2); |
| 345 | Changed = true; |
| 346 | } |
| 347 | |
Krzysztof Parzyszek | 1a0da8d | 2017-06-22 20:43:02 +0000 | [diff] [blame] | 348 | // Fix up kill flags. |
| 349 | |
| 350 | LivePhysRegs LPR(*HRI); |
| 351 | LPR.addLiveOuts(B); |
| 352 | auto IsLive = [&LPR,this] (unsigned Reg) -> bool { |
| 353 | for (MCSubRegIterator S(Reg, HRI, true); S.isValid(); ++S) |
| 354 | if (LPR.contains(*S)) |
| 355 | return true; |
| 356 | return false; |
| 357 | }; |
| 358 | for (auto I = B.rbegin(), E = B.rend(); I != E; ++I) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 359 | if (I->isDebugInstr()) |
Krzysztof Parzyszek | 1a0da8d | 2017-06-22 20:43:02 +0000 | [diff] [blame] | 360 | continue; |
| 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" |
| 364 | // ... = r0 |
| 365 | for (MachineOperand &Op : I->operands()) { |
| 366 | if (!Op.isReg() || !Op.isUse()) |
| 367 | continue; |
| 368 | assert(Op.getSubReg() == 0 && "Should have physical registers only"); |
| 369 | bool Live = IsLive(Op.getReg()); |
| 370 | Op.setIsKill(!Live); |
| 371 | } |
| 372 | LPR.stepBackward(*I); |
| 373 | } |
| 374 | |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 375 | return Changed; |
| 376 | } |
| 377 | |
| 378 | bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 379 | if (skipFunction(MF.getFunction())) |
Andrew Kaylor | 5b444a2 | 2016-04-26 19:46:28 +0000 | [diff] [blame] | 380 | return false; |
Krzysztof Parzyszek | 9217220 | 2015-07-20 21:23:25 +0000 | [diff] [blame] | 381 | HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo(); |
| 382 | HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
| 383 | bool Changed = false; |
| 384 | for (auto &I : MF) |
| 385 | Changed |= genMuxInBlock(I); |
| 386 | return Changed; |
| 387 | } |
| 388 | |
| 389 | FunctionPass *llvm::createHexagonGenMux() { |
| 390 | return new HexagonGenMux(); |
| 391 | } |