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" |
| 50 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame^] | 51 | #include "llvm/CodeGen/LiveVariables.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 53 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 54 | #include "llvm/CodeGen/MachinePostDominators.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 56 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 57 | #include "llvm/Support/raw_ostream.h" |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 58 | #include "llvm/Target/TargetMachine.h" |
| 59 | |
| 60 | using namespace llvm; |
| 61 | |
| 62 | #define DEBUG_TYPE "si-fix-sgpr-live-ranges" |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | class SIFixSGPRLiveRanges : public MachineFunctionPass { |
| 67 | public: |
| 68 | static char ID; |
| 69 | |
| 70 | public: |
| 71 | SIFixSGPRLiveRanges() : MachineFunctionPass(ID) { |
| 72 | initializeSIFixSGPRLiveRangesPass(*PassRegistry::getPassRegistry()); |
| 73 | } |
| 74 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 75 | bool runOnMachineFunction(MachineFunction &MF) override; |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 76 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 77 | const char *getPassName() const override { |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 78 | return "SI Fix SGPR live ranges"; |
| 79 | } |
| 80 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 81 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 82 | AU.addRequired<LiveIntervals>(); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 83 | AU.addRequired<MachinePostDominatorTree>(); |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 84 | AU.setPreservesCFG(); |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 85 | |
| 86 | //AU.addPreserved<SlotIndexes>(); // XXX - This might be OK |
| 87 | AU.addPreserved<LiveIntervals>(); |
| 88 | |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 89 | MachineFunctionPass::getAnalysisUsage(AU); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | } // End anonymous namespace. |
| 94 | |
| 95 | INITIALIZE_PASS_BEGIN(SIFixSGPRLiveRanges, DEBUG_TYPE, |
| 96 | "SI Fix SGPR Live Ranges", false, false) |
| 97 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame^] | 98 | INITIALIZE_PASS_DEPENDENCY(LiveVariables) |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 99 | INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree) |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 100 | INITIALIZE_PASS_END(SIFixSGPRLiveRanges, DEBUG_TYPE, |
| 101 | "SI Fix SGPR Live Ranges", false, false) |
| 102 | |
| 103 | char SIFixSGPRLiveRanges::ID = 0; |
| 104 | |
| 105 | char &llvm::SIFixSGPRLiveRangesID = SIFixSGPRLiveRanges::ID; |
| 106 | |
| 107 | FunctionPass *llvm::createSIFixSGPRLiveRangesPass() { |
| 108 | return new SIFixSGPRLiveRanges(); |
| 109 | } |
| 110 | |
| 111 | bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) { |
| 112 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 113 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
| 114 | const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>( |
| 115 | MF.getSubtarget().getRegisterInfo()); |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame^] | 116 | |
| 117 | MachinePostDominatorTree *PDT = &getAnalysis<MachinePostDominatorTree>(); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 118 | std::vector<std::pair<unsigned, LiveRange *>> SGPRLiveRanges; |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 119 | |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame^] | 120 | LiveIntervals *LIS = &getAnalysis<LiveIntervals>(); |
| 121 | LiveVariables *LV = getAnalysisIfAvailable<LiveVariables>(); |
| 122 | |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 123 | // First pass, collect all live intervals for SGPRs |
| 124 | for (const MachineBasicBlock &MBB : MF) { |
| 125 | for (const MachineInstr &MI : MBB) { |
| 126 | for (const MachineOperand &MO : MI.defs()) { |
| 127 | if (MO.isImplicit()) |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 128 | continue; |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 129 | unsigned Def = MO.getReg(); |
| 130 | if (TargetRegisterInfo::isVirtualRegister(Def)) { |
| 131 | if (TRI->isSGPRClass(MRI.getRegClass(Def))) |
| 132 | SGPRLiveRanges.push_back( |
| 133 | std::make_pair(Def, &LIS->getInterval(Def))); |
| 134 | } else if (TRI->isSGPRClass(TRI->getPhysRegClass(Def))) { |
| 135 | SGPRLiveRanges.push_back( |
| 136 | std::make_pair(Def, &LIS->getRegUnit(Def))); |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 142 | // Second pass fix the intervals |
| 143 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 144 | BI != BE; ++BI) { |
| 145 | MachineBasicBlock &MBB = *BI; |
| 146 | if (MBB.succ_size() < 2) |
| 147 | continue; |
| 148 | |
Matt Arsenault | 4275c29 | 2015-08-15 00:12:30 +0000 | [diff] [blame] | 149 | // We have structured control flow, so the number of successors should be |
| 150 | // two. |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 151 | assert(MBB.succ_size() == 2); |
| 152 | MachineBasicBlock *SuccA = *MBB.succ_begin(); |
| 153 | MachineBasicBlock *SuccB = *(++MBB.succ_begin()); |
| 154 | MachineBasicBlock *NCD = PDT->findNearestCommonDominator(SuccA, SuccB); |
| 155 | |
| 156 | if (!NCD) |
| 157 | continue; |
| 158 | |
| 159 | MachineBasicBlock::iterator NCDTerm = NCD->getFirstTerminator(); |
| 160 | |
| 161 | if (NCDTerm != NCD->end() && NCDTerm->getOpcode() == AMDGPU::SI_ELSE) { |
| 162 | assert(NCD->succ_size() == 2); |
| 163 | // We want to make sure we insert the Use after the ENDIF, not after |
| 164 | // the ELSE. |
| 165 | NCD = PDT->findNearestCommonDominator(*NCD->succ_begin(), |
| 166 | *(++NCD->succ_begin())); |
| 167 | } |
Matt Arsenault | b752332 | 2015-08-15 00:12:32 +0000 | [diff] [blame] | 168 | |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 169 | for (std::pair<unsigned, LiveRange*> RegLR : SGPRLiveRanges) { |
| 170 | unsigned Reg = RegLR.first; |
| 171 | LiveRange *LR = RegLR.second; |
| 172 | |
Matt Arsenault | 4275c29 | 2015-08-15 00:12:30 +0000 | [diff] [blame] | 173 | // FIXME: We could be smarter here. If the register is Live-In to one |
| 174 | // block, but the other doesn't have any SGPR defs, then there won't be a |
| 175 | // conflict. Also, if the branch condition is uniform then there will be |
| 176 | // no conflict. |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 177 | bool LiveInToA = LIS->isLiveInToMBB(*LR, SuccA); |
| 178 | bool LiveInToB = LIS->isLiveInToMBB(*LR, SuccB); |
| 179 | |
| 180 | if ((!LiveInToA && !LiveInToB) || |
| 181 | (LiveInToA && LiveInToB)) |
| 182 | continue; |
| 183 | |
| 184 | // This interval is live in to one successor, but not the other, so |
| 185 | // we need to update its range so it is live in to both. |
| 186 | DEBUG(dbgs() << "Possible SGPR conflict detected " << " in " << *LR << |
| 187 | " BB#" << SuccA->getNumber() << ", BB#" << |
| 188 | SuccB->getNumber() << |
| 189 | " with NCD = " << NCD->getNumber() << '\n'); |
| 190 | |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame^] | 191 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 192 | "Not expecting to extend live range of physreg"); |
| 193 | |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 194 | // FIXME: Need to figure out how to update LiveRange here so this pass |
| 195 | // will be able to preserve LiveInterval analysis. |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 196 | MachineInstr *NCDSGPRUse = |
| 197 | BuildMI(*NCD, NCD->getFirstNonPHI(), DebugLoc(), |
| 198 | TII->get(AMDGPU::SGPR_USE)) |
| 199 | .addReg(Reg, RegState::Implicit); |
| 200 | |
| 201 | SlotIndex SI = LIS->InsertMachineInstrInMaps(NCDSGPRUse); |
| 202 | LIS->extendToIndices(*LR, SI.getRegSlot()); |
| 203 | |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame^] | 204 | if (LV) { |
| 205 | // TODO: This won't work post-SSA |
| 206 | LV->HandleVirtRegUse(Reg, NCD, NCDSGPRUse); |
| 207 | } |
| 208 | |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 209 | DEBUG(NCDSGPRUse->dump()); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 213 | return false; |
| 214 | } |