Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 1 | //===-- SIFixSGPRLiveRanges.cpp - Fix SGPR live ranges ----------------------===// |
| 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 | // |
Matt Arsenault | 4275c29 | 2015-08-15 00:12:30 +0000 | [diff] [blame] | 10 | /// \file SALU instructions ignore the execution mask, so we need to modify the |
| 11 | /// live ranges of the registers they define in some cases. |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 12 | /// |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 13 | /// The main case we need to handle is when a def is used in one side of a |
| 14 | /// branch and not another. For example: |
| 15 | /// |
| 16 | /// %def |
| 17 | /// IF |
| 18 | /// ... |
| 19 | /// ... |
| 20 | /// ELSE |
| 21 | /// %use |
| 22 | /// ... |
| 23 | /// ENDIF |
| 24 | /// |
| 25 | /// Here we need the register allocator to avoid assigning any of the defs |
| 26 | /// inside of the IF to the same register as %def. In traditional live |
| 27 | /// interval analysis %def is not live inside the IF branch, however, since |
| 28 | /// SALU instructions inside of IF will be executed even if the branch is not |
| 29 | /// taken, there is the chance that one of the instructions will overwrite the |
| 30 | /// value of %def, so the use in ELSE will see the wrong value. |
| 31 | /// |
| 32 | /// The strategy we use for solving this is to add an extra use after the ENDIF: |
| 33 | /// |
| 34 | /// %def |
| 35 | /// IF |
| 36 | /// ... |
| 37 | /// ... |
| 38 | /// ELSE |
| 39 | /// %use |
| 40 | /// ... |
| 41 | /// ENDIF |
| 42 | /// %use |
| 43 | /// |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 44 | /// Adding this use will make the def live throughout the IF branch, which is |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 45 | /// what we want. |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 46 | |
| 47 | #include "AMDGPU.h" |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 48 | #include "SIInstrInfo.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 49 | #include "SIRegisterInfo.h" |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame^] | 50 | #include "llvm/ADT/DepthFirstIterator.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/LiveVariables.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 53 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 55 | #include "llvm/CodeGen/MachinePostDominators.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 56 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 57 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 59 | #include "llvm/Target/TargetMachine.h" |
| 60 | |
| 61 | using namespace llvm; |
| 62 | |
| 63 | #define DEBUG_TYPE "si-fix-sgpr-live-ranges" |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | class SIFixSGPRLiveRanges : public MachineFunctionPass { |
| 68 | public: |
| 69 | static char ID; |
| 70 | |
| 71 | public: |
| 72 | SIFixSGPRLiveRanges() : MachineFunctionPass(ID) { |
| 73 | initializeSIFixSGPRLiveRangesPass(*PassRegistry::getPassRegistry()); |
| 74 | } |
| 75 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 76 | bool runOnMachineFunction(MachineFunction &MF) override; |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 77 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 78 | const char *getPassName() const override { |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 79 | return "SI Fix SGPR live ranges"; |
| 80 | } |
| 81 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 82 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 83 | AU.addRequired<LiveIntervals>(); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 84 | AU.addRequired<MachinePostDominatorTree>(); |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 85 | AU.setPreservesCFG(); |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 86 | |
| 87 | //AU.addPreserved<SlotIndexes>(); // XXX - This might be OK |
| 88 | AU.addPreserved<LiveIntervals>(); |
| 89 | |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 90 | MachineFunctionPass::getAnalysisUsage(AU); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | } // End anonymous namespace. |
| 95 | |
| 96 | INITIALIZE_PASS_BEGIN(SIFixSGPRLiveRanges, DEBUG_TYPE, |
| 97 | "SI Fix SGPR Live Ranges", false, false) |
| 98 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 99 | INITIALIZE_PASS_DEPENDENCY(LiveVariables) |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 100 | INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree) |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 101 | INITIALIZE_PASS_END(SIFixSGPRLiveRanges, DEBUG_TYPE, |
| 102 | "SI Fix SGPR Live Ranges", false, false) |
| 103 | |
| 104 | char SIFixSGPRLiveRanges::ID = 0; |
| 105 | |
| 106 | char &llvm::SIFixSGPRLiveRangesID = SIFixSGPRLiveRanges::ID; |
| 107 | |
| 108 | FunctionPass *llvm::createSIFixSGPRLiveRangesPass() { |
| 109 | return new SIFixSGPRLiveRanges(); |
| 110 | } |
| 111 | |
| 112 | bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) { |
| 113 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 114 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
| 115 | const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>( |
| 116 | MF.getSubtarget().getRegisterInfo()); |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 117 | |
| 118 | MachinePostDominatorTree *PDT = &getAnalysis<MachinePostDominatorTree>(); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 119 | std::vector<std::pair<unsigned, LiveRange *>> SGPRLiveRanges; |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 120 | |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 121 | LiveIntervals *LIS = &getAnalysis<LiveIntervals>(); |
| 122 | LiveVariables *LV = getAnalysisIfAvailable<LiveVariables>(); |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame^] | 123 | MachineBasicBlock *Entry = MF.begin(); |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 124 | |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame^] | 125 | // Use a depth first order so that in SSA, we encounter all defs before |
| 126 | // uses. Once the defs of the block have been found, attempt to insert |
| 127 | // SGPR_USE instructions in successor blocks if required. |
| 128 | for (MachineBasicBlock *MBB : depth_first(Entry)) { |
| 129 | for (const MachineInstr &MI : *MBB) { |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 130 | for (const MachineOperand &MO : MI.defs()) { |
| 131 | if (MO.isImplicit()) |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 132 | continue; |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 133 | unsigned Def = MO.getReg(); |
| 134 | if (TargetRegisterInfo::isVirtualRegister(Def)) { |
Matt Arsenault | 588732b | 2015-08-15 02:58:49 +0000 | [diff] [blame] | 135 | if (TRI->isSGPRClass(MRI.getRegClass(Def))) { |
| 136 | // Only consider defs that are live outs. We don't care about def / |
| 137 | // use within the same block. |
| 138 | LiveRange &LR = LIS->getInterval(Def); |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame^] | 139 | if (LIS->isLiveOutOfMBB(LR, MBB)) |
Matt Arsenault | 588732b | 2015-08-15 02:58:49 +0000 | [diff] [blame] | 140 | SGPRLiveRanges.push_back(std::make_pair(Def, &LR)); |
| 141 | } |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 142 | } else if (TRI->isSGPRClass(TRI->getPhysRegClass(Def))) { |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame^] | 143 | SGPRLiveRanges.push_back(std::make_pair(Def, &LIS->getRegUnit(Def))); |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 147 | |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame^] | 148 | if (MBB->succ_size() < 2) |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 149 | continue; |
| 150 | |
Matt Arsenault | 4275c29 | 2015-08-15 00:12:30 +0000 | [diff] [blame] | 151 | // We have structured control flow, so the number of successors should be |
| 152 | // two. |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame^] | 153 | assert(MBB->succ_size() == 2); |
| 154 | MachineBasicBlock *SuccA = *MBB->succ_begin(); |
| 155 | MachineBasicBlock *SuccB = *(++MBB->succ_begin()); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 156 | MachineBasicBlock *NCD = PDT->findNearestCommonDominator(SuccA, SuccB); |
| 157 | |
| 158 | if (!NCD) |
| 159 | continue; |
| 160 | |
| 161 | MachineBasicBlock::iterator NCDTerm = NCD->getFirstTerminator(); |
| 162 | |
| 163 | if (NCDTerm != NCD->end() && NCDTerm->getOpcode() == AMDGPU::SI_ELSE) { |
| 164 | assert(NCD->succ_size() == 2); |
| 165 | // We want to make sure we insert the Use after the ENDIF, not after |
| 166 | // the ELSE. |
| 167 | NCD = PDT->findNearestCommonDominator(*NCD->succ_begin(), |
| 168 | *(++NCD->succ_begin())); |
| 169 | } |
Matt Arsenault | b752332 | 2015-08-15 00:12:32 +0000 | [diff] [blame] | 170 | |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 171 | for (std::pair<unsigned, LiveRange*> RegLR : SGPRLiveRanges) { |
| 172 | unsigned Reg = RegLR.first; |
| 173 | LiveRange *LR = RegLR.second; |
| 174 | |
Matt Arsenault | 4275c29 | 2015-08-15 00:12:30 +0000 | [diff] [blame] | 175 | // FIXME: We could be smarter here. If the register is Live-In to one |
| 176 | // block, but the other doesn't have any SGPR defs, then there won't be a |
| 177 | // conflict. Also, if the branch condition is uniform then there will be |
| 178 | // no conflict. |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 179 | bool LiveInToA = LIS->isLiveInToMBB(*LR, SuccA); |
| 180 | bool LiveInToB = LIS->isLiveInToMBB(*LR, SuccB); |
| 181 | |
Matt Arsenault | aba29d6 | 2015-08-22 00:19:25 +0000 | [diff] [blame] | 182 | if (!LiveInToA && !LiveInToB) { |
| 183 | DEBUG(dbgs() << PrintReg(Reg, TRI, 0) |
| 184 | << " is live into neither successor\n"); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 185 | continue; |
Matt Arsenault | aba29d6 | 2015-08-22 00:19:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | if (LiveInToA && LiveInToB) { |
| 189 | DEBUG(dbgs() << PrintReg(Reg, TRI, 0) |
| 190 | << " is live into both successors\n"); |
| 191 | continue; |
| 192 | } |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 193 | |
| 194 | // This interval is live in to one successor, but not the other, so |
| 195 | // we need to update its range so it is live in to both. |
Matt Arsenault | aba29d6 | 2015-08-22 00:19:25 +0000 | [diff] [blame] | 196 | DEBUG(dbgs() << "Possible SGPR conflict detected for " |
| 197 | << PrintReg(Reg, TRI, 0) << " in " << *LR |
| 198 | << " BB#" << SuccA->getNumber() << ", BB#" |
| 199 | << SuccB->getNumber() |
| 200 | << " with NCD = BB#" << NCD->getNumber() << '\n'); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 201 | |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 202 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 203 | "Not expecting to extend live range of physreg"); |
| 204 | |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 205 | // FIXME: Need to figure out how to update LiveRange here so this pass |
| 206 | // will be able to preserve LiveInterval analysis. |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 207 | MachineInstr *NCDSGPRUse = |
| 208 | BuildMI(*NCD, NCD->getFirstNonPHI(), DebugLoc(), |
| 209 | TII->get(AMDGPU::SGPR_USE)) |
| 210 | .addReg(Reg, RegState::Implicit); |
| 211 | |
| 212 | SlotIndex SI = LIS->InsertMachineInstrInMaps(NCDSGPRUse); |
| 213 | LIS->extendToIndices(*LR, SI.getRegSlot()); |
| 214 | |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 215 | if (LV) { |
| 216 | // TODO: This won't work post-SSA |
| 217 | LV->HandleVirtRegUse(Reg, NCD, NCDSGPRUse); |
| 218 | } |
| 219 | |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 220 | DEBUG(NCDSGPRUse->dump()); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |