Krzysztof Parzyszek | 8b26fbf | 2015-07-09 15:40:25 +0000 | [diff] [blame] | 1 | //===--- HexagonExpandCondsets.cpp ----------------------------------------===// |
| 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 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 10 | // Replace mux instructions with the corresponding legal instructions. |
| 11 | // It is meant to work post-SSA, but still on virtual registers. It was |
| 12 | // originally placed between register coalescing and machine instruction |
| 13 | // scheduler. |
| 14 | // In this place in the optimization sequence, live interval analysis had |
| 15 | // been performed, and the live intervals should be preserved. A large part |
| 16 | // of the code deals with preserving the liveness information. |
| 17 | // |
| 18 | // Liveness tracking aside, the main functionality of this pass is divided |
| 19 | // into two steps. The first step is to replace an instruction |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 20 | // vreg0 = C2_mux vreg1, vreg2, vreg3 |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 21 | // with a pair of conditional transfers |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 22 | // vreg0 = A2_tfrt vreg1, vreg2 |
| 23 | // vreg0 = A2_tfrf vreg1, vreg3 |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 24 | // It is the intention that the execution of this pass could be terminated |
| 25 | // after this step, and the code generated would be functionally correct. |
| 26 | // |
| 27 | // If the uses of the source values vreg1 and vreg2 are kills, and their |
| 28 | // definitions are predicable, then in the second step, the conditional |
| 29 | // transfers will then be rewritten as predicated instructions. E.g. |
| 30 | // vreg0 = A2_or vreg1, vreg2 |
| 31 | // vreg3 = A2_tfrt vreg99, vreg0<kill> |
| 32 | // will be rewritten as |
| 33 | // vreg3 = A2_port vreg99, vreg1, vreg2 |
| 34 | // |
| 35 | // This replacement has two variants: "up" and "down". Consider this case: |
| 36 | // vreg0 = A2_or vreg1, vreg2 |
| 37 | // ... [intervening instructions] ... |
| 38 | // vreg3 = A2_tfrt vreg99, vreg0<kill> |
| 39 | // variant "up": |
| 40 | // vreg3 = A2_port vreg99, vreg1, vreg2 |
| 41 | // ... [intervening instructions, vreg0->vreg3] ... |
| 42 | // [deleted] |
| 43 | // variant "down": |
| 44 | // [deleted] |
| 45 | // ... [intervening instructions] ... |
| 46 | // vreg3 = A2_port vreg99, vreg1, vreg2 |
| 47 | // |
| 48 | // Both, one or none of these variants may be valid, and checks are made |
| 49 | // to rule out inapplicable variants. |
| 50 | // |
| 51 | // As an additional optimization, before either of the two steps above is |
| 52 | // executed, the pass attempts to coalesce the target register with one of |
| 53 | // the source registers, e.g. given an instruction |
| 54 | // vreg3 = C2_mux vreg0, vreg1, vreg2 |
| 55 | // vreg3 will be coalesced with either vreg1 or vreg2. If this succeeds, |
| 56 | // the instruction would then be (for example) |
| 57 | // vreg3 = C2_mux vreg0, vreg3, vreg2 |
| 58 | // and, under certain circumstances, this could result in only one predicated |
| 59 | // instruction: |
| 60 | // vreg3 = A2_tfrf vreg0, vreg2 |
| 61 | // |
| 62 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 63 | // Splitting a definition of a register into two predicated transfers |
| 64 | // creates a complication in liveness tracking. Live interval computation |
| 65 | // will see both instructions as actual definitions, and will mark the |
| 66 | // first one as dead. The definition is not actually dead, and this |
| 67 | // situation will need to be fixed. For example: |
| 68 | // vreg1<def,dead> = A2_tfrt ... ; marked as dead |
| 69 | // vreg1<def> = A2_tfrf ... |
| 70 | // |
| 71 | // Since any of the individual predicated transfers may end up getting |
| 72 | // removed (in case it is an identity copy), some pre-existing def may |
| 73 | // be marked as dead after live interval recomputation: |
| 74 | // vreg1<def,dead> = ... ; marked as dead |
| 75 | // ... |
| 76 | // vreg1<def> = A2_tfrf ... ; if A2_tfrt is removed |
| 77 | // This case happens if vreg1 was used as a source in A2_tfrt, which means |
| 78 | // that is it actually live at the A2_tfrf, and so the now dead definition |
| 79 | // of vreg1 will need to be updated to non-dead at some point. |
| 80 | // |
| 81 | // This issue could be remedied by adding implicit uses to the predicated |
| 82 | // transfers, but this will create a problem with subsequent predication, |
| 83 | // since the transfers will no longer be possible to reorder. To avoid |
| 84 | // that, the initial splitting will not add any implicit uses. These |
| 85 | // implicit uses will be added later, after predication. The extra price, |
| 86 | // however, is that finding the locations where the implicit uses need |
| 87 | // to be added, and updating the live ranges will be more involved. |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 88 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 89 | #define DEBUG_TYPE "expand-condsets" |
| 90 | |
| 91 | #include "HexagonTargetMachine.h" |
| 92 | #include "llvm/ADT/SetVector.h" |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 93 | #include "llvm/CodeGen/Passes.h" |
| 94 | #include "llvm/CodeGen/LiveInterval.h" |
| 95 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 96 | #include "llvm/CodeGen/MachineDominators.h" |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 97 | #include "llvm/CodeGen/MachineFunction.h" |
| 98 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 99 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 100 | #include "llvm/Target/TargetInstrInfo.h" |
| 101 | #include "llvm/Target/TargetMachine.h" |
| 102 | #include "llvm/Target/TargetRegisterInfo.h" |
| 103 | #include "llvm/Support/CommandLine.h" |
| 104 | #include "llvm/Support/Debug.h" |
| 105 | #include "llvm/Support/raw_ostream.h" |
| 106 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 107 | #include <algorithm> |
| 108 | #include <iterator> |
| 109 | #include <set> |
| 110 | #include <utility> |
| 111 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 112 | using namespace llvm; |
| 113 | |
| 114 | static cl::opt<unsigned> OptTfrLimit("expand-condsets-tfr-limit", |
| 115 | cl::init(~0U), cl::Hidden, cl::desc("Max number of mux expansions")); |
| 116 | static cl::opt<unsigned> OptCoaLimit("expand-condsets-coa-limit", |
| 117 | cl::init(~0U), cl::Hidden, cl::desc("Max number of segment coalescings")); |
| 118 | |
| 119 | namespace llvm { |
| 120 | void initializeHexagonExpandCondsetsPass(PassRegistry&); |
| 121 | FunctionPass *createHexagonExpandCondsets(); |
| 122 | } |
| 123 | |
| 124 | namespace { |
| 125 | class HexagonExpandCondsets : public MachineFunctionPass { |
| 126 | public: |
| 127 | static char ID; |
| 128 | HexagonExpandCondsets() : |
| 129 | MachineFunctionPass(ID), HII(0), TRI(0), MRI(0), |
| 130 | LIS(0), CoaLimitActive(false), |
| 131 | TfrLimitActive(false), CoaCounter(0), TfrCounter(0) { |
| 132 | if (OptCoaLimit.getPosition()) |
| 133 | CoaLimitActive = true, CoaLimit = OptCoaLimit; |
| 134 | if (OptTfrLimit.getPosition()) |
| 135 | TfrLimitActive = true, TfrLimit = OptTfrLimit; |
| 136 | initializeHexagonExpandCondsetsPass(*PassRegistry::getPassRegistry()); |
| 137 | } |
| 138 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 139 | const char *getPassName() const override { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 140 | return "Hexagon Expand Condsets"; |
| 141 | } |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 142 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 143 | AU.addRequired<LiveIntervals>(); |
| 144 | AU.addPreserved<LiveIntervals>(); |
| 145 | AU.addPreserved<SlotIndexes>(); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 146 | AU.addRequired<MachineDominatorTree>(); |
| 147 | AU.addPreserved<MachineDominatorTree>(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 148 | MachineFunctionPass::getAnalysisUsage(AU); |
| 149 | } |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 150 | bool runOnMachineFunction(MachineFunction &MF) override; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | const HexagonInstrInfo *HII; |
| 154 | const TargetRegisterInfo *TRI; |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 155 | MachineDominatorTree *MDT; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 156 | MachineRegisterInfo *MRI; |
| 157 | LiveIntervals *LIS; |
| 158 | |
| 159 | bool CoaLimitActive, TfrLimitActive; |
| 160 | unsigned CoaLimit, TfrLimit, CoaCounter, TfrCounter; |
| 161 | |
| 162 | struct RegisterRef { |
| 163 | RegisterRef(const MachineOperand &Op) : Reg(Op.getReg()), |
| 164 | Sub(Op.getSubReg()) {} |
| 165 | RegisterRef(unsigned R = 0, unsigned S = 0) : Reg(R), Sub(S) {} |
| 166 | bool operator== (RegisterRef RR) const { |
| 167 | return Reg == RR.Reg && Sub == RR.Sub; |
| 168 | } |
| 169 | bool operator!= (RegisterRef RR) const { return !operator==(RR); } |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 170 | bool operator< (RegisterRef RR) const { |
| 171 | return Reg < RR.Reg || (Reg == RR.Reg && Sub < RR.Sub); |
| 172 | } |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 173 | unsigned Reg, Sub; |
| 174 | }; |
| 175 | |
| 176 | typedef DenseMap<unsigned,unsigned> ReferenceMap; |
| 177 | enum { Sub_Low = 0x1, Sub_High = 0x2, Sub_None = (Sub_Low | Sub_High) }; |
| 178 | enum { Exec_Then = 0x10, Exec_Else = 0x20 }; |
| 179 | unsigned getMaskForSub(unsigned Sub); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 180 | bool isCondset(const MachineInstr &MI); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 181 | LaneBitmask getLaneMask(unsigned Reg, unsigned Sub); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 182 | |
| 183 | void addRefToMap(RegisterRef RR, ReferenceMap &Map, unsigned Exec); |
| 184 | bool isRefInMap(RegisterRef, ReferenceMap &Map, unsigned Exec); |
| 185 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 186 | void updateDeadsInRange(unsigned Reg, LaneBitmask LM, LiveRange &Range); |
| 187 | void updateKillFlags(unsigned Reg); |
| 188 | void updateDeadFlags(unsigned Reg); |
| 189 | void recalculateLiveInterval(unsigned Reg); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 190 | void removeInstr(MachineInstr &MI); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 191 | void updateLiveness(std::set<unsigned> &RegSet, bool Recalc, |
| 192 | bool UpdateKills, bool UpdateDeads); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 193 | |
| 194 | unsigned getCondTfrOpcode(const MachineOperand &SO, bool Cond); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 195 | MachineInstr *genCondTfrFor(MachineOperand &SrcOp, |
| 196 | MachineBasicBlock::iterator At, unsigned DstR, |
| 197 | unsigned DstSR, const MachineOperand &PredOp, bool PredSense, |
| 198 | bool ReadUndef, bool ImpUse); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 199 | bool split(MachineInstr &MI, std::set<unsigned> &UpdRegs); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 200 | bool splitInBlock(MachineBasicBlock &B, std::set<unsigned> &UpdRegs); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 201 | |
| 202 | bool isPredicable(MachineInstr *MI); |
| 203 | MachineInstr *getReachingDefForPred(RegisterRef RD, |
| 204 | MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 205 | bool canMoveOver(MachineInstr &MI, ReferenceMap &Defs, ReferenceMap &Uses); |
| 206 | bool canMoveMemTo(MachineInstr &MI, MachineInstr &ToI, bool IsDown); |
| 207 | void predicateAt(const MachineOperand &DefOp, MachineInstr &MI, |
| 208 | MachineBasicBlock::iterator Where, |
| 209 | const MachineOperand &PredOp, bool Cond, |
| 210 | std::set<unsigned> &UpdRegs); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 211 | void renameInRange(RegisterRef RO, RegisterRef RN, unsigned PredR, |
| 212 | bool Cond, MachineBasicBlock::iterator First, |
| 213 | MachineBasicBlock::iterator Last); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 214 | bool predicate(MachineInstr &TfrI, bool Cond, std::set<unsigned> &UpdRegs); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 215 | bool predicateInBlock(MachineBasicBlock &B, |
| 216 | std::set<unsigned> &UpdRegs); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 217 | |
| 218 | bool isIntReg(RegisterRef RR, unsigned &BW); |
| 219 | bool isIntraBlocks(LiveInterval &LI); |
| 220 | bool coalesceRegisters(RegisterRef R1, RegisterRef R2); |
| 221 | bool coalesceSegments(MachineFunction &MF); |
| 222 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 223 | } |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 224 | |
| 225 | char HexagonExpandCondsets::ID = 0; |
| 226 | |
Krzysztof Parzyszek | 951fb36 | 2016-08-24 22:27:36 +0000 | [diff] [blame] | 227 | namespace llvm { |
| 228 | char &HexagonExpandCondsetsID = HexagonExpandCondsets::ID; |
| 229 | } |
| 230 | |
Krzysztof Parzyszek | 764fed9 | 2016-05-27 21:15:34 +0000 | [diff] [blame] | 231 | INITIALIZE_PASS_BEGIN(HexagonExpandCondsets, "expand-condsets", |
| 232 | "Hexagon Expand Condsets", false, false) |
| 233 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 234 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 235 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 236 | INITIALIZE_PASS_END(HexagonExpandCondsets, "expand-condsets", |
| 237 | "Hexagon Expand Condsets", false, false) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 238 | |
| 239 | unsigned HexagonExpandCondsets::getMaskForSub(unsigned Sub) { |
| 240 | switch (Sub) { |
| 241 | case Hexagon::subreg_loreg: |
| 242 | return Sub_Low; |
| 243 | case Hexagon::subreg_hireg: |
| 244 | return Sub_High; |
| 245 | case Hexagon::NoSubRegister: |
| 246 | return Sub_None; |
| 247 | } |
| 248 | llvm_unreachable("Invalid subregister"); |
| 249 | } |
| 250 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 251 | bool HexagonExpandCondsets::isCondset(const MachineInstr &MI) { |
| 252 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 253 | switch (Opc) { |
| 254 | case Hexagon::C2_mux: |
| 255 | case Hexagon::C2_muxii: |
| 256 | case Hexagon::C2_muxir: |
| 257 | case Hexagon::C2_muxri: |
Krzysztof Parzyszek | 258af19 | 2016-08-11 19:12:18 +0000 | [diff] [blame] | 258 | case Hexagon::PS_pselect: |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 259 | return true; |
| 260 | break; |
| 261 | } |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 266 | LaneBitmask HexagonExpandCondsets::getLaneMask(unsigned Reg, unsigned Sub) { |
| 267 | assert(TargetRegisterInfo::isVirtualRegister(Reg)); |
| 268 | return Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub) |
| 269 | : MRI->getMaxLaneMaskForVReg(Reg); |
| 270 | } |
| 271 | |
| 272 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 273 | void HexagonExpandCondsets::addRefToMap(RegisterRef RR, ReferenceMap &Map, |
| 274 | unsigned Exec) { |
| 275 | unsigned Mask = getMaskForSub(RR.Sub) | Exec; |
| 276 | ReferenceMap::iterator F = Map.find(RR.Reg); |
| 277 | if (F == Map.end()) |
| 278 | Map.insert(std::make_pair(RR.Reg, Mask)); |
| 279 | else |
| 280 | F->second |= Mask; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | bool HexagonExpandCondsets::isRefInMap(RegisterRef RR, ReferenceMap &Map, |
| 285 | unsigned Exec) { |
| 286 | ReferenceMap::iterator F = Map.find(RR.Reg); |
| 287 | if (F == Map.end()) |
| 288 | return false; |
| 289 | unsigned Mask = getMaskForSub(RR.Sub) | Exec; |
| 290 | if (Mask & F->second) |
| 291 | return true; |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 296 | void HexagonExpandCondsets::updateKillFlags(unsigned Reg) { |
| 297 | auto KillAt = [this,Reg] (SlotIndex K, LaneBitmask LM) -> void { |
| 298 | // Set the <kill> flag on a use of Reg whose lane mask is contained in LM. |
| 299 | MachineInstr *MI = LIS->getInstructionFromIndex(K); |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 300 | for (auto &Op : MI->operands()) { |
Krzysztof Parzyszek | 1bba896 | 2016-07-01 20:45:19 +0000 | [diff] [blame] | 301 | if (!Op.isReg() || !Op.isUse() || Op.getReg() != Reg) |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 302 | continue; |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 303 | LaneBitmask SLM = getLaneMask(Reg, Op.getSubReg()); |
| 304 | if ((SLM & LM) == SLM) { |
| 305 | // Only set the kill flag on the first encountered use of Reg in this |
| 306 | // instruction. |
| 307 | Op.setIsKill(true); |
| 308 | break; |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 309 | } |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 310 | } |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 311 | }; |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 312 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 313 | LiveInterval &LI = LIS->getInterval(Reg); |
| 314 | for (auto I = LI.begin(), E = LI.end(); I != E; ++I) { |
| 315 | if (!I->end.isRegister()) |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 316 | continue; |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 317 | // Do not mark the end of the segment as <kill>, if the next segment |
| 318 | // starts with a predicated instruction. |
| 319 | auto NextI = std::next(I); |
| 320 | if (NextI != E && NextI->start.isRegister()) { |
| 321 | MachineInstr *DefI = LIS->getInstructionFromIndex(NextI->start); |
| 322 | if (HII->isPredicated(*DefI)) |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 323 | continue; |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 324 | } |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 325 | bool WholeReg = true; |
| 326 | if (LI.hasSubRanges()) { |
| 327 | auto EndsAtI = [I] (LiveInterval::SubRange &S) -> bool { |
| 328 | LiveRange::iterator F = S.find(I->end); |
| 329 | return F != S.end() && I->end == F->end; |
| 330 | }; |
| 331 | // Check if all subranges end at I->end. If so, make sure to kill |
| 332 | // the whole register. |
| 333 | for (LiveInterval::SubRange &S : LI.subranges()) { |
| 334 | if (EndsAtI(S)) |
| 335 | KillAt(I->end, S.LaneMask); |
| 336 | else |
| 337 | WholeReg = false; |
| 338 | } |
| 339 | } |
| 340 | if (WholeReg) |
| 341 | KillAt(I->end, MRI->getMaxLaneMaskForVReg(Reg)); |
| 342 | } |
| 343 | } |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 344 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 345 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 346 | void HexagonExpandCondsets::updateDeadsInRange(unsigned Reg, LaneBitmask LM, |
| 347 | LiveRange &Range) { |
| 348 | assert(TargetRegisterInfo::isVirtualRegister(Reg)); |
| 349 | if (Range.empty()) |
| 350 | return; |
| 351 | |
| 352 | auto IsRegDef = [this,Reg,LM] (MachineOperand &Op) -> bool { |
| 353 | if (!Op.isReg() || !Op.isDef()) |
| 354 | return false; |
| 355 | unsigned DR = Op.getReg(), DSR = Op.getSubReg(); |
| 356 | if (!TargetRegisterInfo::isVirtualRegister(DR) || DR != Reg) |
| 357 | return false; |
| 358 | LaneBitmask SLM = getLaneMask(DR, DSR); |
| 359 | return (SLM & LM) != 0; |
| 360 | }; |
| 361 | |
| 362 | // The splitting step will create pairs of predicated definitions without |
| 363 | // any implicit uses (since implicit uses would interfere with predication). |
| 364 | // This can cause the reaching defs to become dead after live range |
| 365 | // recomputation, even though they are not really dead. |
| 366 | // We need to identify predicated defs that need implicit uses, and |
| 367 | // dead defs that are not really dead, and correct both problems. |
| 368 | |
| 369 | SetVector<MachineBasicBlock*> Defs; |
| 370 | auto Dominate = [this] (SetVector<MachineBasicBlock*> &Defs, |
| 371 | MachineBasicBlock *Dest) -> bool { |
| 372 | for (MachineBasicBlock *D : Defs) |
| 373 | if (D != Dest && MDT->dominates(D, Dest)) |
| 374 | return true; |
| 375 | |
| 376 | MachineBasicBlock *Entry = &Dest->getParent()->front(); |
| 377 | SetVector<MachineBasicBlock*> Work(Dest->pred_begin(), Dest->pred_end()); |
| 378 | for (unsigned i = 0; i < Work.size(); ++i) { |
| 379 | MachineBasicBlock *B = Work[i]; |
| 380 | if (Defs.count(B)) |
| 381 | continue; |
| 382 | if (B == Entry) |
| 383 | return false; |
| 384 | for (auto *P : B->predecessors()) |
| 385 | Work.insert(P); |
| 386 | } |
| 387 | return true; |
| 388 | }; |
| 389 | |
| 390 | // First, try to extend live range within individual basic blocks. This |
| 391 | // will leave us only with dead defs that do not reach any predicated |
| 392 | // defs in the same block. |
| 393 | SmallVector<SlotIndex,4> PredDefs; |
| 394 | for (auto &Seg : Range) { |
| 395 | if (!Seg.start.isRegister()) |
| 396 | continue; |
| 397 | MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 398 | Defs.insert(DefI->getParent()); |
| 399 | if (HII->isPredicated(*DefI)) |
| 400 | PredDefs.push_back(Seg.start); |
| 401 | } |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 402 | |
| 403 | SmallVector<SlotIndex,8> Undefs; |
| 404 | LiveInterval &LI = LIS->getInterval(Reg); |
| 405 | LI.computeSubRangeUndefs(Undefs, LM, *MRI, *LIS->getSlotIndexes()); |
| 406 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 407 | for (auto &SI : PredDefs) { |
| 408 | MachineBasicBlock *BB = LIS->getMBBFromIndex(SI); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 409 | auto P = Range.extendInBlock(Undefs, LIS->getMBBStartIdx(BB), SI); |
| 410 | if (P.first != nullptr || P.second) |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 411 | SI = SlotIndex(); |
| 412 | } |
| 413 | |
| 414 | // Calculate reachability for those predicated defs that were not handled |
| 415 | // by the in-block extension. |
| 416 | SmallVector<SlotIndex,4> ExtTo; |
| 417 | for (auto &SI : PredDefs) { |
| 418 | if (!SI.isValid()) |
| 419 | continue; |
| 420 | MachineBasicBlock *BB = LIS->getMBBFromIndex(SI); |
| 421 | if (BB->pred_empty()) |
| 422 | continue; |
| 423 | // If the defs from this range reach SI via all predecessors, it is live. |
| 424 | if (Dominate(Defs, BB)) |
| 425 | ExtTo.push_back(SI); |
| 426 | } |
Krzysztof Parzyszek | 07d9f53 | 2016-09-01 13:59:35 +0000 | [diff] [blame] | 427 | LIS->extendToIndices(Range, ExtTo, Undefs); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 428 | |
| 429 | // Remove <dead> flags from all defs that are not dead after live range |
| 430 | // extension, and collect all def operands. They will be used to generate |
| 431 | // the necessary implicit uses. |
| 432 | std::set<RegisterRef> DefRegs; |
| 433 | for (auto &Seg : Range) { |
| 434 | if (!Seg.start.isRegister()) |
| 435 | continue; |
| 436 | MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 437 | for (auto &Op : DefI->operands()) { |
| 438 | if (Seg.start.isDead() || !IsRegDef(Op)) |
| 439 | continue; |
| 440 | DefRegs.insert(Op); |
| 441 | Op.setIsDead(false); |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 445 | // Finally, add implicit uses to each predicated def that is reached |
Krzysztof Parzyszek | cbd559f | 2016-08-24 16:36:37 +0000 | [diff] [blame] | 446 | // by other defs. |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 447 | for (auto &Seg : Range) { |
| 448 | if (!Seg.start.isRegister() || !Range.liveAt(Seg.start.getPrevSlot())) |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 449 | continue; |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 450 | MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start); |
| 451 | if (!HII->isPredicated(*DefI)) |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 452 | continue; |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 453 | MachineFunction &MF = *DefI->getParent()->getParent(); |
| 454 | // Construct the set of all necessary implicit uses, based on the def |
| 455 | // operands in the instruction. |
| 456 | std::set<RegisterRef> ImpUses; |
| 457 | for (auto &Op : DefI->operands()) |
| 458 | if (Op.isReg() && Op.isDef() && DefRegs.count(Op)) |
| 459 | ImpUses.insert(Op); |
| 460 | for (RegisterRef R : ImpUses) |
| 461 | MachineInstrBuilder(MF, DefI).addReg(R.Reg, RegState::Implicit, R.Sub); |
Krzysztof Parzyszek | 8b61759 | 2016-06-07 19:25:28 +0000 | [diff] [blame] | 462 | } |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | |
| 466 | void HexagonExpandCondsets::updateDeadFlags(unsigned Reg) { |
| 467 | LiveInterval &LI = LIS->getInterval(Reg); |
| 468 | if (LI.hasSubRanges()) { |
| 469 | for (LiveInterval::SubRange &S : LI.subranges()) { |
| 470 | updateDeadsInRange(Reg, S.LaneMask, S); |
| 471 | LIS->shrinkToUses(S, Reg); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 472 | } |
| 473 | LI.clear(); |
| 474 | LIS->constructMainRangeFromSubranges(LI); |
| 475 | } else { |
| 476 | updateDeadsInRange(Reg, MRI->getMaxLaneMaskForVReg(Reg), LI); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | |
| 481 | void HexagonExpandCondsets::recalculateLiveInterval(unsigned Reg) { |
| 482 | LIS->removeInterval(Reg); |
| 483 | LIS->createAndComputeVirtRegInterval(Reg); |
| 484 | } |
| 485 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 486 | void HexagonExpandCondsets::removeInstr(MachineInstr &MI) { |
| 487 | LIS->RemoveMachineInstrFromMaps(MI); |
| 488 | MI.eraseFromParent(); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | void HexagonExpandCondsets::updateLiveness(std::set<unsigned> &RegSet, |
| 493 | bool Recalc, bool UpdateKills, bool UpdateDeads) { |
| 494 | UpdateKills |= UpdateDeads; |
| 495 | for (auto R : RegSet) { |
| 496 | if (Recalc) |
| 497 | recalculateLiveInterval(R); |
| 498 | if (UpdateKills) |
| 499 | MRI->clearKillFlags(R); |
| 500 | if (UpdateDeads) |
| 501 | updateDeadFlags(R); |
| 502 | // Fixing <dead> flags may extend live ranges, so reset <kill> flags |
| 503 | // after that. |
| 504 | if (UpdateKills) |
| 505 | updateKillFlags(R); |
| 506 | LIS->getInterval(R).verify(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | /// Get the opcode for a conditional transfer of the value in SO (source |
| 512 | /// operand). The condition (true/false) is given in Cond. |
| 513 | unsigned HexagonExpandCondsets::getCondTfrOpcode(const MachineOperand &SO, |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 514 | bool IfTrue) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 515 | using namespace Hexagon; |
| 516 | if (SO.isReg()) { |
| 517 | unsigned PhysR; |
| 518 | RegisterRef RS = SO; |
| 519 | if (TargetRegisterInfo::isVirtualRegister(RS.Reg)) { |
| 520 | const TargetRegisterClass *VC = MRI->getRegClass(RS.Reg); |
| 521 | assert(VC->begin() != VC->end() && "Empty register class"); |
| 522 | PhysR = *VC->begin(); |
| 523 | } else { |
| 524 | assert(TargetRegisterInfo::isPhysicalRegister(RS.Reg)); |
| 525 | PhysR = RS.Reg; |
| 526 | } |
| 527 | unsigned PhysS = (RS.Sub == 0) ? PhysR : TRI->getSubReg(PhysR, RS.Sub); |
| 528 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysS); |
| 529 | switch (RC->getSize()) { |
| 530 | case 4: |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 531 | return IfTrue ? A2_tfrt : A2_tfrf; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 532 | case 8: |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 533 | return IfTrue ? A2_tfrpt : A2_tfrpf; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 534 | } |
| 535 | llvm_unreachable("Invalid register operand"); |
| 536 | } |
| 537 | if (SO.isImm() || SO.isFPImm()) |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 538 | return IfTrue ? C2_cmoveit : C2_cmoveif; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 539 | llvm_unreachable("Unexpected source operand"); |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /// Generate a conditional transfer, copying the value SrcOp to the |
| 544 | /// destination register DstR:DstSR, and using the predicate register from |
| 545 | /// PredOp. The Cond argument specifies whether the predicate is to be |
| 546 | /// if(PredOp), or if(!PredOp). |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 547 | MachineInstr *HexagonExpandCondsets::genCondTfrFor(MachineOperand &SrcOp, |
| 548 | MachineBasicBlock::iterator At, |
| 549 | unsigned DstR, unsigned DstSR, const MachineOperand &PredOp, |
| 550 | bool PredSense, bool ReadUndef, bool ImpUse) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 551 | MachineInstr *MI = SrcOp.getParent(); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 552 | MachineBasicBlock &B = *At->getParent(); |
Benjamin Kramer | 4ca41fd | 2016-06-12 17:30:47 +0000 | [diff] [blame] | 553 | const DebugLoc &DL = MI->getDebugLoc(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 554 | |
| 555 | // Don't avoid identity copies here (i.e. if the source and the destination |
| 556 | // are the same registers). It is actually better to generate them here, |
| 557 | // since this would cause the copy to potentially be predicated in the next |
| 558 | // step. The predication will remove such a copy if it is unable to |
| 559 | /// predicate. |
| 560 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 561 | unsigned Opc = getCondTfrOpcode(SrcOp, PredSense); |
| 562 | unsigned State = RegState::Define | (ReadUndef ? RegState::Undef : 0); |
| 563 | MachineInstrBuilder MIB = BuildMI(B, At, DL, HII->get(Opc)) |
| 564 | .addReg(DstR, State, DstSR) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 565 | .addOperand(PredOp) |
| 566 | .addOperand(SrcOp); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 567 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 568 | // We don't want any kills yet. |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 569 | MIB->clearKillInfo(); |
| 570 | DEBUG(dbgs() << "created an initial copy: " << *MIB); |
| 571 | return &*MIB; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
| 575 | /// Replace a MUX instruction MI with a pair A2_tfrt/A2_tfrf. This function |
| 576 | /// performs all necessary changes to complete the replacement. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 577 | bool HexagonExpandCondsets::split(MachineInstr &MI, |
| 578 | std::set<unsigned> &UpdRegs) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 579 | if (TfrLimitActive) { |
| 580 | if (TfrCounter >= TfrLimit) |
| 581 | return false; |
| 582 | TfrCounter++; |
| 583 | } |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 584 | DEBUG(dbgs() << "\nsplitting BB#" << MI.getParent()->getNumber() << ": " |
| 585 | << MI); |
| 586 | MachineOperand &MD = MI.getOperand(0); // Definition |
| 587 | MachineOperand &MP = MI.getOperand(1); // Predicate register |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 588 | assert(MD.isDef()); |
| 589 | unsigned DR = MD.getReg(), DSR = MD.getSubReg(); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 590 | bool ReadUndef = MD.isUndef(); |
| 591 | MachineBasicBlock::iterator At = MI; |
| 592 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 593 | // First, create the two invididual conditional transfers, and add each |
| 594 | // of them to the live intervals information. Do that first and then remove |
| 595 | // the old instruction from live intervals. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 596 | MachineInstr *TfrT = |
| 597 | genCondTfrFor(MI.getOperand(2), At, DR, DSR, MP, true, ReadUndef, false); |
| 598 | MachineInstr *TfrF = |
| 599 | genCondTfrFor(MI.getOperand(3), At, DR, DSR, MP, false, ReadUndef, true); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 600 | LIS->InsertMachineInstrInMaps(*TfrT); |
| 601 | LIS->InsertMachineInstrInMaps(*TfrF); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 602 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 603 | // Will need to recalculate live intervals for all registers in MI. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 604 | for (auto &Op : MI.operands()) |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 605 | if (Op.isReg()) |
| 606 | UpdRegs.insert(Op.getReg()); |
| 607 | |
| 608 | removeInstr(MI); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 609 | return true; |
| 610 | } |
| 611 | |
| 612 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 613 | /// Split all MUX instructions in the given block into pairs of conditional |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 614 | /// transfers. |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 615 | bool HexagonExpandCondsets::splitInBlock(MachineBasicBlock &B, |
| 616 | std::set<unsigned> &UpdRegs) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 617 | bool Changed = false; |
| 618 | MachineBasicBlock::iterator I, E, NextI; |
| 619 | for (I = B.begin(), E = B.end(); I != E; I = NextI) { |
| 620 | NextI = std::next(I); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 621 | if (isCondset(*I)) |
| 622 | Changed |= split(*I, UpdRegs); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 623 | } |
| 624 | return Changed; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | bool HexagonExpandCondsets::isPredicable(MachineInstr *MI) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 629 | if (HII->isPredicated(*MI) || !HII->isPredicable(*MI)) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 630 | return false; |
| 631 | if (MI->hasUnmodeledSideEffects() || MI->mayStore()) |
| 632 | return false; |
| 633 | // Reject instructions with multiple defs (e.g. post-increment loads). |
| 634 | bool HasDef = false; |
| 635 | for (auto &Op : MI->operands()) { |
| 636 | if (!Op.isReg() || !Op.isDef()) |
| 637 | continue; |
| 638 | if (HasDef) |
| 639 | return false; |
| 640 | HasDef = true; |
| 641 | } |
| 642 | for (auto &Mo : MI->memoperands()) |
| 643 | if (Mo->isVolatile()) |
| 644 | return false; |
| 645 | return true; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | /// Find the reaching definition for a predicated use of RD. The RD is used |
| 650 | /// under the conditions given by PredR and Cond, and this function will ignore |
| 651 | /// definitions that set RD under the opposite conditions. |
| 652 | MachineInstr *HexagonExpandCondsets::getReachingDefForPred(RegisterRef RD, |
| 653 | MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond) { |
| 654 | MachineBasicBlock &B = *UseIt->getParent(); |
| 655 | MachineBasicBlock::iterator I = UseIt, S = B.begin(); |
| 656 | if (I == S) |
| 657 | return 0; |
| 658 | |
| 659 | bool PredValid = true; |
| 660 | do { |
| 661 | --I; |
| 662 | MachineInstr *MI = &*I; |
| 663 | // Check if this instruction can be ignored, i.e. if it is predicated |
| 664 | // on the complementary condition. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 665 | if (PredValid && HII->isPredicated(*MI)) { |
| 666 | if (MI->readsRegister(PredR) && (Cond != HII->isPredicatedTrue(*MI))) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 667 | continue; |
| 668 | } |
| 669 | |
| 670 | // Check the defs. If the PredR is defined, invalidate it. If RD is |
| 671 | // defined, return the instruction or 0, depending on the circumstances. |
| 672 | for (auto &Op : MI->operands()) { |
| 673 | if (!Op.isReg() || !Op.isDef()) |
| 674 | continue; |
| 675 | RegisterRef RR = Op; |
| 676 | if (RR.Reg == PredR) { |
| 677 | PredValid = false; |
| 678 | continue; |
| 679 | } |
| 680 | if (RR.Reg != RD.Reg) |
| 681 | continue; |
| 682 | // If the "Reg" part agrees, there is still the subregister to check. |
| 683 | // If we are looking for vreg1:loreg, we can skip vreg1:hireg, but |
| 684 | // not vreg1 (w/o subregisters). |
| 685 | if (RR.Sub == RD.Sub) |
| 686 | return MI; |
| 687 | if (RR.Sub == 0 || RD.Sub == 0) |
| 688 | return 0; |
| 689 | // We have different subregisters, so we can continue looking. |
| 690 | } |
| 691 | } while (I != S); |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | /// Check if the instruction MI can be safely moved over a set of instructions |
| 698 | /// whose side-effects (in terms of register defs and uses) are expressed in |
| 699 | /// the maps Defs and Uses. These maps reflect the conditional defs and uses |
| 700 | /// that depend on the same predicate register to allow moving instructions |
| 701 | /// over instructions predicated on the opposite condition. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 702 | bool HexagonExpandCondsets::canMoveOver(MachineInstr &MI, ReferenceMap &Defs, |
| 703 | ReferenceMap &Uses) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 704 | // In order to be able to safely move MI over instructions that define |
| 705 | // "Defs" and use "Uses", no def operand from MI can be defined or used |
| 706 | // and no use operand can be defined. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 707 | for (auto &Op : MI.operands()) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 708 | if (!Op.isReg()) |
| 709 | continue; |
| 710 | RegisterRef RR = Op; |
| 711 | // For physical register we would need to check register aliases, etc. |
| 712 | // and we don't want to bother with that. It would be of little value |
| 713 | // before the actual register rewriting (from virtual to physical). |
| 714 | if (!TargetRegisterInfo::isVirtualRegister(RR.Reg)) |
| 715 | return false; |
| 716 | // No redefs for any operand. |
| 717 | if (isRefInMap(RR, Defs, Exec_Then)) |
| 718 | return false; |
| 719 | // For defs, there cannot be uses. |
| 720 | if (Op.isDef() && isRefInMap(RR, Uses, Exec_Then)) |
| 721 | return false; |
| 722 | } |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | |
| 727 | /// Check if the instruction accessing memory (TheI) can be moved to the |
| 728 | /// location ToI. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 729 | bool HexagonExpandCondsets::canMoveMemTo(MachineInstr &TheI, MachineInstr &ToI, |
| 730 | bool IsDown) { |
| 731 | bool IsLoad = TheI.mayLoad(), IsStore = TheI.mayStore(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 732 | if (!IsLoad && !IsStore) |
| 733 | return true; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 734 | if (HII->areMemAccessesTriviallyDisjoint(TheI, ToI)) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 735 | return true; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 736 | if (TheI.hasUnmodeledSideEffects()) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 737 | return false; |
| 738 | |
| 739 | MachineBasicBlock::iterator StartI = IsDown ? TheI : ToI; |
| 740 | MachineBasicBlock::iterator EndI = IsDown ? ToI : TheI; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 741 | bool Ordered = TheI.hasOrderedMemoryRef(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 742 | |
| 743 | // Search for aliased memory reference in (StartI, EndI). |
| 744 | for (MachineBasicBlock::iterator I = std::next(StartI); I != EndI; ++I) { |
| 745 | MachineInstr *MI = &*I; |
| 746 | if (MI->hasUnmodeledSideEffects()) |
| 747 | return false; |
| 748 | bool L = MI->mayLoad(), S = MI->mayStore(); |
| 749 | if (!L && !S) |
| 750 | continue; |
| 751 | if (Ordered && MI->hasOrderedMemoryRef()) |
| 752 | return false; |
| 753 | |
| 754 | bool Conflict = (L && IsStore) || S; |
| 755 | if (Conflict) |
| 756 | return false; |
| 757 | } |
| 758 | return true; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | /// Generate a predicated version of MI (where the condition is given via |
| 763 | /// PredR and Cond) at the point indicated by Where. |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 764 | void HexagonExpandCondsets::predicateAt(const MachineOperand &DefOp, |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 765 | MachineInstr &MI, |
| 766 | MachineBasicBlock::iterator Where, |
| 767 | const MachineOperand &PredOp, bool Cond, |
| 768 | std::set<unsigned> &UpdRegs) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 769 | // The problem with updating live intervals is that we can move one def |
| 770 | // past another def. In particular, this can happen when moving an A2_tfrt |
| 771 | // over an A2_tfrf defining the same register. From the point of view of |
| 772 | // live intervals, these two instructions are two separate definitions, |
| 773 | // and each one starts another live segment. LiveIntervals's "handleMove" |
| 774 | // does not allow such moves, so we need to handle it ourselves. To avoid |
| 775 | // invalidating liveness data while we are using it, the move will be |
| 776 | // implemented in 4 steps: (1) add a clone of the instruction MI at the |
| 777 | // target location, (2) update liveness, (3) delete the old instruction, |
| 778 | // and (4) update liveness again. |
| 779 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 780 | MachineBasicBlock &B = *MI.getParent(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 781 | DebugLoc DL = Where->getDebugLoc(); // "Where" points to an instruction. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 782 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 783 | unsigned PredOpc = HII->getCondOpcode(Opc, !Cond); |
| 784 | MachineInstrBuilder MB = BuildMI(B, Where, DL, HII->get(PredOpc)); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 785 | unsigned Ox = 0, NP = MI.getNumOperands(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 786 | // Skip all defs from MI first. |
| 787 | while (Ox < NP) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 788 | MachineOperand &MO = MI.getOperand(Ox); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 789 | if (!MO.isReg() || !MO.isDef()) |
| 790 | break; |
| 791 | Ox++; |
| 792 | } |
| 793 | // Add the new def, then the predicate register, then the rest of the |
| 794 | // operands. |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 795 | MB.addReg(DefOp.getReg(), getRegState(DefOp), DefOp.getSubReg()); |
| 796 | MB.addReg(PredOp.getReg(), PredOp.isUndef() ? RegState::Undef : 0, |
| 797 | PredOp.getSubReg()); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 798 | while (Ox < NP) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 799 | MachineOperand &MO = MI.getOperand(Ox); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 800 | if (!MO.isReg() || !MO.isImplicit()) |
| 801 | MB.addOperand(MO); |
| 802 | Ox++; |
| 803 | } |
| 804 | |
| 805 | MachineFunction &MF = *B.getParent(); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 806 | MachineInstr::mmo_iterator I = MI.memoperands_begin(); |
| 807 | unsigned NR = std::distance(I, MI.memoperands_end()); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 808 | MachineInstr::mmo_iterator MemRefs = MF.allocateMemRefsArray(NR); |
| 809 | for (unsigned i = 0; i < NR; ++i) |
| 810 | MemRefs[i] = *I++; |
| 811 | MB.setMemRefs(MemRefs, MemRefs+NR); |
| 812 | |
| 813 | MachineInstr *NewI = MB; |
| 814 | NewI->clearKillInfo(); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 815 | LIS->InsertMachineInstrInMaps(*NewI); |
| 816 | |
| 817 | for (auto &Op : NewI->operands()) |
| 818 | if (Op.isReg()) |
| 819 | UpdRegs.insert(Op.getReg()); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | |
| 823 | /// In the range [First, Last], rename all references to the "old" register RO |
| 824 | /// to the "new" register RN, but only in instructions predicated on the given |
| 825 | /// condition. |
| 826 | void HexagonExpandCondsets::renameInRange(RegisterRef RO, RegisterRef RN, |
| 827 | unsigned PredR, bool Cond, MachineBasicBlock::iterator First, |
| 828 | MachineBasicBlock::iterator Last) { |
| 829 | MachineBasicBlock::iterator End = std::next(Last); |
| 830 | for (MachineBasicBlock::iterator I = First; I != End; ++I) { |
| 831 | MachineInstr *MI = &*I; |
| 832 | // Do not touch instructions that are not predicated, or are predicated |
| 833 | // on the opposite condition. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 834 | if (!HII->isPredicated(*MI)) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 835 | continue; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 836 | if (!MI->readsRegister(PredR) || (Cond != HII->isPredicatedTrue(*MI))) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 837 | continue; |
| 838 | |
| 839 | for (auto &Op : MI->operands()) { |
| 840 | if (!Op.isReg() || RO != RegisterRef(Op)) |
| 841 | continue; |
| 842 | Op.setReg(RN.Reg); |
| 843 | Op.setSubReg(RN.Sub); |
| 844 | // In practice, this isn't supposed to see any defs. |
| 845 | assert(!Op.isDef() && "Not expecting a def"); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | |
| 851 | /// For a given conditional copy, predicate the definition of the source of |
| 852 | /// the copy under the given condition (using the same predicate register as |
| 853 | /// the copy). |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 854 | bool HexagonExpandCondsets::predicate(MachineInstr &TfrI, bool Cond, |
| 855 | std::set<unsigned> &UpdRegs) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 856 | // TfrI - A2_tfr[tf] Instruction (not A2_tfrsi). |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 857 | unsigned Opc = TfrI.getOpcode(); |
Simon Atanasyan | 772944a | 2015-03-31 19:43:47 +0000 | [diff] [blame] | 858 | (void)Opc; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 859 | assert(Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf); |
| 860 | DEBUG(dbgs() << "\nattempt to predicate if-" << (Cond ? "true" : "false") |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 861 | << ": " << TfrI); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 862 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 863 | MachineOperand &MD = TfrI.getOperand(0); |
| 864 | MachineOperand &MP = TfrI.getOperand(1); |
| 865 | MachineOperand &MS = TfrI.getOperand(2); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 866 | // The source operand should be a <kill>. This is not strictly necessary, |
| 867 | // but it makes things a lot simpler. Otherwise, we would need to rename |
| 868 | // some registers, which would complicate the transformation considerably. |
| 869 | if (!MS.isKill()) |
| 870 | return false; |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 871 | // Avoid predicating instructions that define a subregister if subregister |
| 872 | // liveness tracking is not enabled. |
| 873 | if (MD.getSubReg() && !MRI->shouldTrackSubRegLiveness(MD.getReg())) |
Krzysztof Parzyszek | a580273 | 2016-05-31 14:27:10 +0000 | [diff] [blame] | 874 | return false; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 875 | |
| 876 | RegisterRef RT(MS); |
| 877 | unsigned PredR = MP.getReg(); |
| 878 | MachineInstr *DefI = getReachingDefForPred(RT, TfrI, PredR, Cond); |
| 879 | if (!DefI || !isPredicable(DefI)) |
| 880 | return false; |
| 881 | |
| 882 | DEBUG(dbgs() << "Source def: " << *DefI); |
| 883 | |
| 884 | // Collect the information about registers defined and used between the |
| 885 | // DefI and the TfrI. |
| 886 | // Map: reg -> bitmask of subregs |
| 887 | ReferenceMap Uses, Defs; |
| 888 | MachineBasicBlock::iterator DefIt = DefI, TfrIt = TfrI; |
| 889 | |
| 890 | // Check if the predicate register is valid between DefI and TfrI. |
| 891 | // If it is, we can then ignore instructions predicated on the negated |
| 892 | // conditions when collecting def and use information. |
| 893 | bool PredValid = true; |
| 894 | for (MachineBasicBlock::iterator I = std::next(DefIt); I != TfrIt; ++I) { |
| 895 | if (!I->modifiesRegister(PredR, 0)) |
| 896 | continue; |
| 897 | PredValid = false; |
| 898 | break; |
| 899 | } |
| 900 | |
| 901 | for (MachineBasicBlock::iterator I = std::next(DefIt); I != TfrIt; ++I) { |
| 902 | MachineInstr *MI = &*I; |
| 903 | // If this instruction is predicated on the same register, it could |
| 904 | // potentially be ignored. |
| 905 | // By default assume that the instruction executes on the same condition |
| 906 | // as TfrI (Exec_Then), and also on the opposite one (Exec_Else). |
| 907 | unsigned Exec = Exec_Then | Exec_Else; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 908 | if (PredValid && HII->isPredicated(*MI) && MI->readsRegister(PredR)) |
| 909 | Exec = (Cond == HII->isPredicatedTrue(*MI)) ? Exec_Then : Exec_Else; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 910 | |
| 911 | for (auto &Op : MI->operands()) { |
| 912 | if (!Op.isReg()) |
| 913 | continue; |
| 914 | // We don't want to deal with physical registers. The reason is that |
| 915 | // they can be aliased with other physical registers. Aliased virtual |
| 916 | // registers must share the same register number, and can only differ |
| 917 | // in the subregisters, which we are keeping track of. Physical |
| 918 | // registers ters no longer have subregisters---their super- and |
| 919 | // subregisters are other physical registers, and we are not checking |
| 920 | // that. |
| 921 | RegisterRef RR = Op; |
| 922 | if (!TargetRegisterInfo::isVirtualRegister(RR.Reg)) |
| 923 | return false; |
| 924 | |
| 925 | ReferenceMap &Map = Op.isDef() ? Defs : Uses; |
| 926 | addRefToMap(RR, Map, Exec); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | // The situation: |
| 931 | // RT = DefI |
| 932 | // ... |
| 933 | // RD = TfrI ..., RT |
| 934 | |
| 935 | // If the register-in-the-middle (RT) is used or redefined between |
| 936 | // DefI and TfrI, we may not be able proceed with this transformation. |
| 937 | // We can ignore a def that will not execute together with TfrI, and a |
| 938 | // use that will. If there is such a use (that does execute together with |
| 939 | // TfrI), we will not be able to move DefI down. If there is a use that |
| 940 | // executed if TfrI's condition is false, then RT must be available |
| 941 | // unconditionally (cannot be predicated). |
| 942 | // Essentially, we need to be able to rename RT to RD in this segment. |
| 943 | if (isRefInMap(RT, Defs, Exec_Then) || isRefInMap(RT, Uses, Exec_Else)) |
| 944 | return false; |
| 945 | RegisterRef RD = MD; |
| 946 | // If the predicate register is defined between DefI and TfrI, the only |
| 947 | // potential thing to do would be to move the DefI down to TfrI, and then |
| 948 | // predicate. The reaching def (DefI) must be movable down to the location |
| 949 | // of the TfrI. |
| 950 | // If the target register of the TfrI (RD) is not used or defined between |
| 951 | // DefI and TfrI, consider moving TfrI up to DefI. |
| 952 | bool CanUp = canMoveOver(TfrI, Defs, Uses); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 953 | bool CanDown = canMoveOver(*DefI, Defs, Uses); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 954 | // The TfrI does not access memory, but DefI could. Check if it's safe |
| 955 | // to move DefI down to TfrI. |
| 956 | if (DefI->mayLoad() || DefI->mayStore()) |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 957 | if (!canMoveMemTo(*DefI, TfrI, true)) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 958 | CanDown = false; |
| 959 | |
| 960 | DEBUG(dbgs() << "Can move up: " << (CanUp ? "yes" : "no") |
| 961 | << ", can move down: " << (CanDown ? "yes\n" : "no\n")); |
| 962 | MachineBasicBlock::iterator PastDefIt = std::next(DefIt); |
| 963 | if (CanUp) |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 964 | predicateAt(MD, *DefI, PastDefIt, MP, Cond, UpdRegs); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 965 | else if (CanDown) |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 966 | predicateAt(MD, *DefI, TfrIt, MP, Cond, UpdRegs); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 967 | else |
| 968 | return false; |
| 969 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 970 | if (RT != RD) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 971 | renameInRange(RT, RD, PredR, Cond, PastDefIt, TfrIt); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 972 | UpdRegs.insert(RT.Reg); |
| 973 | } |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 974 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 975 | removeInstr(TfrI); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 976 | removeInstr(*DefI); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 977 | return true; |
| 978 | } |
| 979 | |
| 980 | |
| 981 | /// Predicate all cases of conditional copies in the specified block. |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 982 | bool HexagonExpandCondsets::predicateInBlock(MachineBasicBlock &B, |
| 983 | std::set<unsigned> &UpdRegs) { |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 984 | bool Changed = false; |
| 985 | MachineBasicBlock::iterator I, E, NextI; |
| 986 | for (I = B.begin(), E = B.end(); I != E; I = NextI) { |
| 987 | NextI = std::next(I); |
| 988 | unsigned Opc = I->getOpcode(); |
| 989 | if (Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 990 | bool Done = predicate(*I, (Opc == Hexagon::A2_tfrt), UpdRegs); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 991 | if (!Done) { |
| 992 | // If we didn't predicate I, we may need to remove it in case it is |
| 993 | // an "identity" copy, e.g. vreg1 = A2_tfrt vreg2, vreg1. |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 994 | if (RegisterRef(I->getOperand(0)) == RegisterRef(I->getOperand(2))) { |
| 995 | for (auto &Op : I->operands()) |
| 996 | if (Op.isReg()) |
| 997 | UpdRegs.insert(Op.getReg()); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 998 | removeInstr(*I); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 999 | } |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1000 | } |
| 1001 | Changed |= Done; |
| 1002 | } |
| 1003 | } |
| 1004 | return Changed; |
| 1005 | } |
| 1006 | |
| 1007 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1008 | bool HexagonExpandCondsets::isIntReg(RegisterRef RR, unsigned &BW) { |
| 1009 | if (!TargetRegisterInfo::isVirtualRegister(RR.Reg)) |
| 1010 | return false; |
| 1011 | const TargetRegisterClass *RC = MRI->getRegClass(RR.Reg); |
| 1012 | if (RC == &Hexagon::IntRegsRegClass) { |
| 1013 | BW = 32; |
| 1014 | return true; |
| 1015 | } |
| 1016 | if (RC == &Hexagon::DoubleRegsRegClass) { |
| 1017 | BW = (RR.Sub != 0) ? 32 : 64; |
| 1018 | return true; |
| 1019 | } |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | bool HexagonExpandCondsets::isIntraBlocks(LiveInterval &LI) { |
| 1025 | for (LiveInterval::iterator I = LI.begin(), E = LI.end(); I != E; ++I) { |
| 1026 | LiveRange::Segment &LR = *I; |
| 1027 | // Range must start at a register... |
| 1028 | if (!LR.start.isRegister()) |
| 1029 | return false; |
| 1030 | // ...and end in a register or in a dead slot. |
| 1031 | if (!LR.end.isRegister() && !LR.end.isDead()) |
| 1032 | return false; |
| 1033 | } |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | bool HexagonExpandCondsets::coalesceRegisters(RegisterRef R1, RegisterRef R2) { |
| 1039 | if (CoaLimitActive) { |
| 1040 | if (CoaCounter >= CoaLimit) |
| 1041 | return false; |
| 1042 | CoaCounter++; |
| 1043 | } |
| 1044 | unsigned BW1, BW2; |
| 1045 | if (!isIntReg(R1, BW1) || !isIntReg(R2, BW2) || BW1 != BW2) |
| 1046 | return false; |
| 1047 | if (MRI->isLiveIn(R1.Reg)) |
| 1048 | return false; |
| 1049 | if (MRI->isLiveIn(R2.Reg)) |
| 1050 | return false; |
| 1051 | |
| 1052 | LiveInterval &L1 = LIS->getInterval(R1.Reg); |
| 1053 | LiveInterval &L2 = LIS->getInterval(R2.Reg); |
Krzysztof Parzyszek | 66dd679 | 2016-08-19 14:29:43 +0000 | [diff] [blame] | 1054 | if (L2.empty()) |
| 1055 | return false; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1056 | bool Overlap = L1.overlaps(L2); |
| 1057 | |
| 1058 | DEBUG(dbgs() << "compatible registers: (" |
| 1059 | << (Overlap ? "overlap" : "disjoint") << ")\n " |
| 1060 | << PrintReg(R1.Reg, TRI, R1.Sub) << " " << L1 << "\n " |
| 1061 | << PrintReg(R2.Reg, TRI, R2.Sub) << " " << L2 << "\n"); |
| 1062 | if (R1.Sub || R2.Sub) |
| 1063 | return false; |
| 1064 | if (Overlap) |
| 1065 | return false; |
| 1066 | |
| 1067 | // Coalescing could have a negative impact on scheduling, so try to limit |
| 1068 | // to some reasonable extent. Only consider coalescing segments, when one |
| 1069 | // of them does not cross basic block boundaries. |
| 1070 | if (!isIntraBlocks(L1) && !isIntraBlocks(L2)) |
| 1071 | return false; |
| 1072 | |
| 1073 | MRI->replaceRegWith(R2.Reg, R1.Reg); |
| 1074 | |
| 1075 | // Move all live segments from L2 to L1. |
| 1076 | typedef DenseMap<VNInfo*,VNInfo*> ValueInfoMap; |
| 1077 | ValueInfoMap VM; |
| 1078 | for (LiveInterval::iterator I = L2.begin(), E = L2.end(); I != E; ++I) { |
| 1079 | VNInfo *NewVN, *OldVN = I->valno; |
| 1080 | ValueInfoMap::iterator F = VM.find(OldVN); |
| 1081 | if (F == VM.end()) { |
| 1082 | NewVN = L1.getNextValue(I->valno->def, LIS->getVNInfoAllocator()); |
| 1083 | VM.insert(std::make_pair(OldVN, NewVN)); |
| 1084 | } else { |
| 1085 | NewVN = F->second; |
| 1086 | } |
| 1087 | L1.addSegment(LiveRange::Segment(I->start, I->end, NewVN)); |
| 1088 | } |
| 1089 | while (L2.begin() != L2.end()) |
| 1090 | L2.removeSegment(*L2.begin()); |
| 1091 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1092 | updateKillFlags(R1.Reg); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1093 | DEBUG(dbgs() << "coalesced: " << L1 << "\n"); |
| 1094 | L1.verify(); |
| 1095 | |
| 1096 | return true; |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | /// Attempt to coalesce one of the source registers to a MUX intruction with |
| 1101 | /// the destination register. This could lead to having only one predicated |
| 1102 | /// instruction in the end instead of two. |
| 1103 | bool HexagonExpandCondsets::coalesceSegments(MachineFunction &MF) { |
| 1104 | SmallVector<MachineInstr*,16> Condsets; |
| 1105 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 1106 | MachineBasicBlock &B = *I; |
| 1107 | for (MachineBasicBlock::iterator J = B.begin(), F = B.end(); J != F; ++J) { |
| 1108 | MachineInstr *MI = &*J; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 1109 | if (!isCondset(*MI)) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1110 | continue; |
| 1111 | MachineOperand &S1 = MI->getOperand(2), &S2 = MI->getOperand(3); |
| 1112 | if (!S1.isReg() && !S2.isReg()) |
| 1113 | continue; |
| 1114 | Condsets.push_back(MI); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | bool Changed = false; |
| 1119 | for (unsigned i = 0, n = Condsets.size(); i < n; ++i) { |
| 1120 | MachineInstr *CI = Condsets[i]; |
| 1121 | RegisterRef RD = CI->getOperand(0); |
| 1122 | RegisterRef RP = CI->getOperand(1); |
| 1123 | MachineOperand &S1 = CI->getOperand(2), &S2 = CI->getOperand(3); |
| 1124 | bool Done = false; |
| 1125 | // Consider this case: |
| 1126 | // vreg1 = instr1 ... |
| 1127 | // vreg2 = instr2 ... |
| 1128 | // vreg0 = C2_mux ..., vreg1, vreg2 |
| 1129 | // If vreg0 was coalesced with vreg1, we could end up with the following |
| 1130 | // code: |
| 1131 | // vreg0 = instr1 ... |
| 1132 | // vreg2 = instr2 ... |
| 1133 | // vreg0 = A2_tfrf ..., vreg2 |
| 1134 | // which will later become: |
| 1135 | // vreg0 = instr1 ... |
| 1136 | // vreg0 = instr2_cNotPt ... |
| 1137 | // i.e. there will be an unconditional definition (instr1) of vreg0 |
| 1138 | // followed by a conditional one. The output dependency was there before |
| 1139 | // and it unavoidable, but if instr1 is predicable, we will no longer be |
| 1140 | // able to predicate it here. |
| 1141 | // To avoid this scenario, don't coalesce the destination register with |
| 1142 | // a source register that is defined by a predicable instruction. |
| 1143 | if (S1.isReg()) { |
| 1144 | RegisterRef RS = S1; |
| 1145 | MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, true); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1146 | if (!RDef || !HII->isPredicable(*RDef)) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1147 | Done = coalesceRegisters(RD, RegisterRef(S1)); |
| 1148 | } |
| 1149 | if (!Done && S2.isReg()) { |
| 1150 | RegisterRef RS = S2; |
| 1151 | MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, false); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1152 | if (!RDef || !HII->isPredicable(*RDef)) |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1153 | Done = coalesceRegisters(RD, RegisterRef(S2)); |
| 1154 | } |
| 1155 | Changed |= Done; |
| 1156 | } |
| 1157 | return Changed; |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | bool HexagonExpandCondsets::runOnMachineFunction(MachineFunction &MF) { |
Andrew Kaylor | 5b444a2 | 2016-04-26 19:46:28 +0000 | [diff] [blame] | 1162 | if (skipFunction(*MF.getFunction())) |
| 1163 | return false; |
| 1164 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1165 | HII = static_cast<const HexagonInstrInfo*>(MF.getSubtarget().getInstrInfo()); |
| 1166 | TRI = MF.getSubtarget().getRegisterInfo(); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1167 | MDT = &getAnalysis<MachineDominatorTree>(); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1168 | LIS = &getAnalysis<LiveIntervals>(); |
| 1169 | MRI = &MF.getRegInfo(); |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1170 | |
| 1171 | DEBUG(LIS->print(dbgs() << "Before expand-condsets\n", |
| 1172 | MF.getFunction()->getParent())); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1173 | |
| 1174 | bool Changed = false; |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1175 | std::set<unsigned> SplitUpd, PredUpd; |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1176 | |
| 1177 | // Try to coalesce the target of a mux with one of its sources. |
| 1178 | // This could eliminate a register copy in some circumstances. |
| 1179 | Changed |= coalesceSegments(MF); |
| 1180 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1181 | // First, simply split all muxes into a pair of conditional transfers |
| 1182 | // and update the live intervals to reflect the new arrangement. The |
| 1183 | // goal is to update the kill flags, since predication will rely on |
| 1184 | // them. |
| 1185 | for (auto &B : MF) |
| 1186 | Changed |= splitInBlock(B, SplitUpd); |
| 1187 | updateLiveness(SplitUpd, true, true, false); |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1188 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1189 | // Traverse all blocks and collapse predicable instructions feeding |
| 1190 | // conditional transfers into predicated instructions. |
| 1191 | // Walk over all the instructions again, so we may catch pre-existing |
| 1192 | // cases that were not created in the previous step. |
| 1193 | for (auto &B : MF) |
| 1194 | Changed |= predicateInBlock(B, PredUpd); |
Krzysztof Parzyszek | 9062b75 | 2016-04-22 16:47:01 +0000 | [diff] [blame] | 1195 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1196 | updateLiveness(PredUpd, true, true, true); |
| 1197 | // Remove from SplitUpd all registers contained in PredUpd to avoid |
| 1198 | // unnecessary liveness recalculation. |
| 1199 | std::set<unsigned> Diff; |
| 1200 | std::set_difference(SplitUpd.begin(), SplitUpd.end(), |
| 1201 | PredUpd.begin(), PredUpd.end(), |
| 1202 | std::inserter(Diff, Diff.begin())); |
| 1203 | updateLiveness(Diff, false, false, true); |
| 1204 | |
Krzysztof Parzyszek | b16882d | 2016-06-08 12:31:16 +0000 | [diff] [blame] | 1205 | DEBUG({ |
| 1206 | if (Changed) |
| 1207 | LIS->print(dbgs() << "After expand-condsets\n", |
| 1208 | MF.getFunction()->getParent()); |
| 1209 | }); |
Krzysztof Parzyszek | 9062b75 | 2016-04-22 16:47:01 +0000 | [diff] [blame] | 1210 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1211 | return Changed; |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | //===----------------------------------------------------------------------===// |
| 1216 | // Public Constructor Functions |
| 1217 | //===----------------------------------------------------------------------===// |
| 1218 | |
Krzysztof Parzyszek | c05dff1 | 2015-03-31 13:35:12 +0000 | [diff] [blame] | 1219 | FunctionPass *llvm::createHexagonExpandCondsets() { |
| 1220 | return new HexagonExpandCondsets(); |
| 1221 | } |