Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 1 | //===----- R600Packetizer.cpp - VLIW packetizer ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// This pass implements instructions packetization for R600. It unsets isLast |
| 12 | /// bit of instructions inside a bundle and substitutes src register with |
| 13 | /// PreviousVector when applicable. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "packets" |
| 18 | #include "llvm/Support/Debug.h" |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 19 | #include "AMDGPU.h" |
| 20 | #include "R600InstrInfo.h" |
Benjamin Kramer | 5c35290 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/DFAPacketizer.h" |
| 22 | #include "llvm/CodeGen/MachineDominators.h" |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 24 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 25 | #include "llvm/CodeGen/Passes.h" |
| 26 | #include "llvm/CodeGen/ScheduleDAG.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 28 | |
Benjamin Kramer | 5c35290 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 32 | |
| 33 | class R600Packetizer : public MachineFunctionPass { |
| 34 | |
| 35 | public: |
| 36 | static char ID; |
| 37 | R600Packetizer(const TargetMachine &TM) : MachineFunctionPass(ID) {} |
| 38 | |
| 39 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 | AU.setPreservesCFG(); |
| 41 | AU.addRequired<MachineDominatorTree>(); |
| 42 | AU.addPreserved<MachineDominatorTree>(); |
| 43 | AU.addRequired<MachineLoopInfo>(); |
| 44 | AU.addPreserved<MachineLoopInfo>(); |
| 45 | MachineFunctionPass::getAnalysisUsage(AU); |
| 46 | } |
| 47 | |
| 48 | const char *getPassName() const { |
| 49 | return "R600 Packetizer"; |
| 50 | } |
| 51 | |
| 52 | bool runOnMachineFunction(MachineFunction &Fn); |
| 53 | }; |
| 54 | char R600Packetizer::ID = 0; |
| 55 | |
| 56 | class R600PacketizerList : public VLIWPacketizerList { |
| 57 | |
| 58 | private: |
| 59 | const R600InstrInfo *TII; |
| 60 | const R600RegisterInfo &TRI; |
| 61 | |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 62 | unsigned getSlot(const MachineInstr *MI) const { |
| 63 | return TRI.getHWRegChan(MI->getOperand(0).getReg()); |
| 64 | } |
| 65 | |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 66 | /// \returns register to PV chan mapping for bundle/single instructions that |
| 67 | /// immediatly precedes I. |
| 68 | DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I) |
| 69 | const { |
| 70 | DenseMap<unsigned, unsigned> Result; |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 71 | I--; |
| 72 | if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle()) |
| 73 | return Result; |
| 74 | MachineBasicBlock::instr_iterator BI = I.getInstrIterator(); |
| 75 | if (I->isBundle()) |
| 76 | BI++; |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 77 | do { |
| 78 | if (TII->isPredicated(BI)) |
| 79 | continue; |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 80 | int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write); |
Vincent Lejeune | 0c92287 | 2013-06-03 15:56:12 +0000 | [diff] [blame] | 81 | if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0) |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 82 | continue; |
Tom Stellard | cedcfee | 2013-06-28 15:46:59 +0000 | [diff] [blame] | 83 | int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst); |
| 84 | if (DstIdx == -1) { |
| 85 | continue; |
| 86 | } |
| 87 | unsigned Dst = BI->getOperand(DstIdx).getReg(); |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 88 | if (TII->isTransOnly(BI)) { |
| 89 | Result[Dst] = AMDGPU::PS; |
| 90 | continue; |
| 91 | } |
Vincent Lejeune | 4ed9917 | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 92 | if (BI->getOpcode() == AMDGPU::DOT4_r600 || |
| 93 | BI->getOpcode() == AMDGPU::DOT4_eg) { |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 94 | Result[Dst] = AMDGPU::PV_X; |
| 95 | continue; |
| 96 | } |
Tom Stellard | e3d4cbc | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 97 | if (Dst == AMDGPU::OQAP) { |
| 98 | continue; |
| 99 | } |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 100 | unsigned PVReg = 0; |
| 101 | switch (TRI.getHWRegChan(Dst)) { |
| 102 | case 0: |
| 103 | PVReg = AMDGPU::PV_X; |
| 104 | break; |
| 105 | case 1: |
| 106 | PVReg = AMDGPU::PV_Y; |
| 107 | break; |
| 108 | case 2: |
| 109 | PVReg = AMDGPU::PV_Z; |
| 110 | break; |
| 111 | case 3: |
| 112 | PVReg = AMDGPU::PV_W; |
| 113 | break; |
| 114 | default: |
| 115 | llvm_unreachable("Invalid Chan"); |
| 116 | } |
| 117 | Result[Dst] = PVReg; |
| 118 | } while ((++BI)->isBundledWithPred()); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 119 | return Result; |
| 120 | } |
| 121 | |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 122 | void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs) |
| 123 | const { |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 124 | unsigned Ops[] = { |
| 125 | AMDGPU::OpName::src0, |
| 126 | AMDGPU::OpName::src1, |
| 127 | AMDGPU::OpName::src2 |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 128 | }; |
| 129 | for (unsigned i = 0; i < 3; i++) { |
| 130 | int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]); |
| 131 | if (OperandIdx < 0) |
| 132 | continue; |
| 133 | unsigned Src = MI->getOperand(OperandIdx).getReg(); |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 134 | const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src); |
| 135 | if (It != PVs.end()) |
| 136 | MI->getOperand(OperandIdx).setReg(It->second); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | public: |
| 140 | // Ctor. |
| 141 | R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI, |
| 142 | MachineDominatorTree &MDT) |
| 143 | : VLIWPacketizerList(MF, MLI, MDT, true), |
| 144 | TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())), |
| 145 | TRI(TII->getRegisterInfo()) { } |
| 146 | |
| 147 | // initPacketizerState - initialize some internal flags. |
| 148 | void initPacketizerState() { } |
| 149 | |
| 150 | // ignorePseudoInstruction - Ignore bundling of pseudo instructions. |
| 151 | bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | // isSoloInstruction - return true if instruction MI can not be packetized |
| 156 | // with any other instruction, which means that MI itself is a packet. |
| 157 | bool isSoloInstruction(MachineInstr *MI) { |
| 158 | if (TII->isVector(*MI)) |
| 159 | return true; |
| 160 | if (!TII->isALUInstr(MI->getOpcode())) |
| 161 | return true; |
Tom Stellard | cedcfee | 2013-06-28 15:46:59 +0000 | [diff] [blame] | 162 | if (MI->getOpcode() == AMDGPU::GROUP_BARRIER) |
| 163 | return true; |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ |
| 168 | // together. |
| 169 | bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) { |
| 170 | MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr(); |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 171 | if (getSlot(MII) <= getSlot(MIJ) && !TII->isTransOnly(MII)) |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 172 | return false; |
| 173 | // Does MII and MIJ share the same pred_sel ? |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 174 | int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel), |
| 175 | OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 176 | unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0, |
| 177 | PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0; |
| 178 | if (PredI != PredJ) |
| 179 | return false; |
| 180 | if (SUJ->isSucc(SUI)) { |
| 181 | for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) { |
| 182 | const SDep &Dep = SUJ->Succs[i]; |
| 183 | if (Dep.getSUnit() != SUI) |
| 184 | continue; |
| 185 | if (Dep.getKind() == SDep::Anti) |
| 186 | continue; |
| 187 | if (Dep.getKind() == SDep::Output) |
| 188 | if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg()) |
| 189 | continue; |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | // isLegalToPruneDependencies - Is it legal to prune dependece between SUI |
| 197 | // and SUJ. |
| 198 | bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;} |
| 199 | |
| 200 | void setIsLastBit(MachineInstr *MI, unsigned Bit) const { |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 201 | unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 202 | MI->getOperand(LastOp).setImm(Bit); |
| 203 | } |
| 204 | |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 205 | bool isBundlableWithCurrentPMI(MachineInstr *MI, |
| 206 | const DenseMap<unsigned, unsigned> &PV, |
| 207 | std::vector<R600InstrInfo::BankSwizzle> &BS, |
| 208 | bool &isTransSlot) { |
| 209 | isTransSlot = TII->isTransOnly(MI); |
| 210 | |
| 211 | // Are the Constants limitations met ? |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 212 | CurrentPacketMIs.push_back(MI); |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 213 | if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) { |
| 214 | DEBUG( |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 215 | dbgs() << "Couldn't pack :\n"; |
| 216 | MI->dump(); |
| 217 | dbgs() << "with the following packets :\n"; |
| 218 | for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) { |
| 219 | CurrentPacketMIs[i]->dump(); |
| 220 | dbgs() << "\n"; |
| 221 | } |
| 222 | dbgs() << "because of Consts read limitations\n"; |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 223 | ); |
| 224 | CurrentPacketMIs.pop_back(); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | // Is there a BankSwizzle set that meet Read Port limitations ? |
| 229 | if (!TII->fitsReadPortLimitations(CurrentPacketMIs, |
| 230 | PV, BS, isTransSlot)) { |
| 231 | DEBUG( |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 232 | dbgs() << "Couldn't pack :\n"; |
| 233 | MI->dump(); |
| 234 | dbgs() << "with the following packets :\n"; |
| 235 | for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) { |
| 236 | CurrentPacketMIs[i]->dump(); |
| 237 | dbgs() << "\n"; |
| 238 | } |
| 239 | dbgs() << "because of Read port limitations\n"; |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 240 | ); |
| 241 | CurrentPacketMIs.pop_back(); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | CurrentPacketMIs.pop_back(); |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | MachineBasicBlock::iterator addToPacket(MachineInstr *MI) { |
| 250 | MachineBasicBlock::iterator FirstInBundle = |
| 251 | CurrentPacketMIs.empty() ? MI : CurrentPacketMIs.front(); |
| 252 | const DenseMap<unsigned, unsigned> &PV = |
| 253 | getPreviousVector(FirstInBundle); |
| 254 | std::vector<R600InstrInfo::BankSwizzle> BS; |
| 255 | bool isTransSlot; |
| 256 | |
| 257 | if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) { |
Vincent Lejeune | 25c209e | 2013-05-17 16:50:02 +0000 | [diff] [blame] | 258 | for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) { |
| 259 | MachineInstr *MI = CurrentPacketMIs[i]; |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 260 | unsigned Op = TII->getOperandIdx(MI->getOpcode(), |
| 261 | AMDGPU::OpName::bank_swizzle); |
| 262 | MI->getOperand(Op).setImm(BS[i]); |
Vincent Lejeune | 25c209e | 2013-05-17 16:50:02 +0000 | [diff] [blame] | 263 | } |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 264 | unsigned Op = TII->getOperandIdx(MI->getOpcode(), |
| 265 | AMDGPU::OpName::bank_swizzle); |
| 266 | MI->getOperand(Op).setImm(BS.back()); |
| 267 | if (!CurrentPacketMIs.empty()) |
| 268 | setIsLastBit(CurrentPacketMIs.back(), 0); |
| 269 | substitutePV(MI, PV); |
| 270 | MachineBasicBlock::iterator It = VLIWPacketizerList::addToPacket(MI); |
| 271 | if (isTransSlot) { |
| 272 | endPacket(llvm::next(It)->getParent(), llvm::next(It)); |
| 273 | } |
| 274 | return It; |
Vincent Lejeune | 25c209e | 2013-05-17 16:50:02 +0000 | [diff] [blame] | 275 | } |
Vincent Lejeune | 8f9fbd6 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 276 | endPacket(MI->getParent(), MI); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 277 | return VLIWPacketizerList::addToPacket(MI); |
| 278 | } |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) { |
| 282 | const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo(); |
| 283 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 284 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
| 285 | |
| 286 | // Instantiate the packetizer. |
| 287 | R600PacketizerList Packetizer(Fn, MLI, MDT); |
| 288 | |
| 289 | // DFA state table should not be empty. |
| 290 | assert(Packetizer.getResourceTracker() && "Empty DFA table!"); |
| 291 | |
| 292 | // |
| 293 | // Loop over all basic blocks and remove KILL pseudo-instructions |
| 294 | // These instructions confuse the dependence analysis. Consider: |
| 295 | // D0 = ... (Insn 0) |
| 296 | // R0 = KILL R0, D0 (Insn 1) |
| 297 | // R0 = ... (Insn 2) |
| 298 | // Here, Insn 1 will result in the dependence graph not emitting an output |
| 299 | // dependence between Insn 0 and Insn 2. This can lead to incorrect |
| 300 | // packetization |
| 301 | // |
| 302 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 303 | MBB != MBBe; ++MBB) { |
| 304 | MachineBasicBlock::iterator End = MBB->end(); |
| 305 | MachineBasicBlock::iterator MI = MBB->begin(); |
| 306 | while (MI != End) { |
Vincent Lejeune | f2cfef8 | 2013-07-09 15:03:33 +0000 | [diff] [blame^] | 307 | if (MI->isKill() || |
| 308 | (MI->getOpcode() == AMDGPU::CF_ALU && !MI->getOperand(8).getImm())) { |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 309 | MachineBasicBlock::iterator DeleteMI = MI; |
| 310 | ++MI; |
| 311 | MBB->erase(DeleteMI); |
| 312 | End = MBB->end(); |
| 313 | continue; |
| 314 | } |
| 315 | ++MI; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Loop over all of the basic blocks. |
| 320 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 321 | MBB != MBBe; ++MBB) { |
| 322 | // Find scheduling regions and schedule / packetize each region. |
| 323 | unsigned RemainingCount = MBB->size(); |
| 324 | for(MachineBasicBlock::iterator RegionEnd = MBB->end(); |
| 325 | RegionEnd != MBB->begin();) { |
| 326 | // The next region starts above the previous region. Look backward in the |
| 327 | // instruction stream until we find the nearest boundary. |
| 328 | MachineBasicBlock::iterator I = RegionEnd; |
| 329 | for(;I != MBB->begin(); --I, --RemainingCount) { |
| 330 | if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn)) |
| 331 | break; |
| 332 | } |
| 333 | I = MBB->begin(); |
| 334 | |
| 335 | // Skip empty scheduling regions. |
| 336 | if (I == RegionEnd) { |
| 337 | RegionEnd = llvm::prior(RegionEnd); |
| 338 | --RemainingCount; |
| 339 | continue; |
| 340 | } |
| 341 | // Skip regions with one instruction. |
| 342 | if (I == llvm::prior(RegionEnd)) { |
| 343 | RegionEnd = llvm::prior(RegionEnd); |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | Packetizer.PacketizeMIs(MBB, I, RegionEnd); |
| 348 | RegionEnd = I; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return true; |
| 353 | |
| 354 | } |
| 355 | |
Benjamin Kramer | 5c35290 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 356 | } // end anonymous namespace |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 357 | |
| 358 | llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) { |
| 359 | return new R600Packetizer(tm); |
| 360 | } |