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 { |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 83 | AU.addRequired<LiveVariables>(); |
| 84 | AU.addPreserved<LiveVariables>(); |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 85 | |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 86 | AU.addRequired<MachinePostDominatorTree>(); |
| 87 | AU.addPreserved<MachinePostDominatorTree>(); |
| 88 | AU.setPreservesCFG(); |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 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) |
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 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 111 | static bool hasOnlyScalarBr(const MachineBasicBlock *MBB, |
| 112 | const SIInstrInfo *TII) { |
| 113 | for (MachineBasicBlock::const_iterator I = MBB->getFirstTerminator(), |
| 114 | E = MBB->end(); I != E; ++I) { |
| 115 | if (!TII->isSOPP(*I)) |
| 116 | return false; |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 121 | bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) { |
| 122 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 123 | const SIInstrInfo *TII = |
| 124 | static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 125 | const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>( |
| 126 | MF.getSubtarget().getRegisterInfo()); |
Matt Arsenault | 602a16d | 2015-08-26 19:12:03 +0000 | [diff] [blame] | 127 | bool MadeChange = false; |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 128 | |
| 129 | MachinePostDominatorTree *PDT = &getAnalysis<MachinePostDominatorTree>(); |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 130 | SmallVector<unsigned, 16> SGPRLiveRanges; |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 131 | |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 132 | LiveVariables *LV = &getAnalysis<LiveVariables>(); |
Duncan P. N. Exon Smith | a73371a | 2015-10-13 20:07:10 +0000 | [diff] [blame] | 133 | MachineBasicBlock *Entry = &MF.front(); |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 134 | |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame] | 135 | // Use a depth first order so that in SSA, we encounter all defs before |
| 136 | // uses. Once the defs of the block have been found, attempt to insert |
| 137 | // SGPR_USE instructions in successor blocks if required. |
| 138 | for (MachineBasicBlock *MBB : depth_first(Entry)) { |
| 139 | for (const MachineInstr &MI : *MBB) { |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 140 | for (const MachineOperand &MO : MI.defs()) { |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 141 | // We should never see a live out def of a physical register, so we also |
| 142 | // do not need to worry about implicit_defs(). |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 143 | unsigned Def = MO.getReg(); |
| 144 | if (TargetRegisterInfo::isVirtualRegister(Def)) { |
Matt Arsenault | 588732b | 2015-08-15 02:58:49 +0000 | [diff] [blame] | 145 | if (TRI->isSGPRClass(MRI.getRegClass(Def))) { |
| 146 | // Only consider defs that are live outs. We don't care about def / |
| 147 | // use within the same block. |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 148 | |
| 149 | // LiveVariables does not consider registers that are only used in a |
| 150 | // phi in a sucessor block as live out, unlike LiveIntervals. |
| 151 | // |
| 152 | // This is OK because SIFixSGPRCopies replaced any SGPR phis with |
| 153 | // VGPRs. |
| 154 | if (LV->isLiveOut(Def, *MBB)) |
| 155 | SGPRLiveRanges.push_back(Def); |
Matt Arsenault | 588732b | 2015-08-15 02:58:49 +0000 | [diff] [blame] | 156 | } |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 160 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 161 | if (MBB->succ_size() < 2 || hasOnlyScalarBr(MBB, TII)) |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 162 | continue; |
| 163 | |
Matt Arsenault | 4275c29 | 2015-08-15 00:12:30 +0000 | [diff] [blame] | 164 | // We have structured control flow, so the number of successors should be |
| 165 | // two. |
Matt Arsenault | 3301010 | 2015-08-22 00:43:38 +0000 | [diff] [blame] | 166 | assert(MBB->succ_size() == 2); |
| 167 | MachineBasicBlock *SuccA = *MBB->succ_begin(); |
| 168 | MachineBasicBlock *SuccB = *(++MBB->succ_begin()); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 169 | MachineBasicBlock *NCD = PDT->findNearestCommonDominator(SuccA, SuccB); |
| 170 | |
| 171 | if (!NCD) |
| 172 | continue; |
| 173 | |
| 174 | MachineBasicBlock::iterator NCDTerm = NCD->getFirstTerminator(); |
| 175 | |
| 176 | if (NCDTerm != NCD->end() && NCDTerm->getOpcode() == AMDGPU::SI_ELSE) { |
| 177 | assert(NCD->succ_size() == 2); |
| 178 | // We want to make sure we insert the Use after the ENDIF, not after |
| 179 | // the ELSE. |
| 180 | NCD = PDT->findNearestCommonDominator(*NCD->succ_begin(), |
| 181 | *(++NCD->succ_begin())); |
| 182 | } |
Matt Arsenault | b752332 | 2015-08-15 00:12:32 +0000 | [diff] [blame] | 183 | |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 184 | for (unsigned Reg : SGPRLiveRanges) { |
Matt Arsenault | 4275c29 | 2015-08-15 00:12:30 +0000 | [diff] [blame] | 185 | // FIXME: We could be smarter here. If the register is Live-In to one |
| 186 | // block, but the other doesn't have any SGPR defs, then there won't be a |
| 187 | // conflict. Also, if the branch condition is uniform then there will be |
| 188 | // no conflict. |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 189 | bool LiveInToA = LV->isLiveIn(Reg, *SuccA); |
| 190 | bool LiveInToB = LV->isLiveIn(Reg, *SuccB); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 191 | |
Matt Arsenault | aba29d6 | 2015-08-22 00:19:25 +0000 | [diff] [blame] | 192 | if (!LiveInToA && !LiveInToB) { |
| 193 | DEBUG(dbgs() << PrintReg(Reg, TRI, 0) |
| 194 | << " is live into neither successor\n"); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 195 | continue; |
Matt Arsenault | aba29d6 | 2015-08-22 00:19:25 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (LiveInToA && LiveInToB) { |
| 199 | DEBUG(dbgs() << PrintReg(Reg, TRI, 0) |
| 200 | << " is live into both successors\n"); |
| 201 | continue; |
| 202 | } |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 203 | |
| 204 | // This interval is live in to one successor, but not the other, so |
| 205 | // 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] | 206 | DEBUG(dbgs() << "Possible SGPR conflict detected for " |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 207 | << PrintReg(Reg, TRI, 0) |
| 208 | << " BB#" << SuccA->getNumber() |
| 209 | << ", BB#" << SuccB->getNumber() |
Matt Arsenault | aba29d6 | 2015-08-22 00:19:25 +0000 | [diff] [blame] | 210 | << " with NCD = BB#" << NCD->getNumber() << '\n'); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 211 | |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 212 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 213 | "Not expecting to extend live range of physreg"); |
| 214 | |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 215 | // FIXME: Need to figure out how to update LiveRange here so this pass |
| 216 | // will be able to preserve LiveInterval analysis. |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 217 | MachineInstr *NCDSGPRUse = |
| 218 | BuildMI(*NCD, NCD->getFirstNonPHI(), DebugLoc(), |
| 219 | TII->get(AMDGPU::SGPR_USE)) |
| 220 | .addReg(Reg, RegState::Implicit); |
| 221 | |
Matt Arsenault | 602a16d | 2015-08-26 19:12:03 +0000 | [diff] [blame] | 222 | MadeChange = true; |
Matt Arsenault | b87fc22 | 2015-10-01 22:10:03 +0000 | [diff] [blame] | 223 | LV->HandleVirtRegUse(Reg, NCD, NCDSGPRUse); |
Matt Arsenault | 0259a7a | 2015-08-15 00:12:37 +0000 | [diff] [blame] | 224 | |
Matt Arsenault | 670ba46 | 2015-08-15 00:12:35 +0000 | [diff] [blame] | 225 | DEBUG(NCDSGPRUse->dump()); |
Tom Stellard | 60024a0 | 2014-09-24 01:33:24 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Matt Arsenault | 602a16d | 2015-08-26 19:12:03 +0000 | [diff] [blame] | 229 | return MadeChange; |
Tom Stellard | b2de94e | 2014-07-02 20:53:48 +0000 | [diff] [blame] | 230 | } |