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; |
| 80 | if (TII->isTransOnly(BI)) |
| 81 | continue; |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 82 | int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write); |
Vincent Lejeune | 0c92287 | 2013-06-03 15:56:12 +0000 | [diff] [blame] | 83 | if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0) |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 84 | continue; |
Tom Stellard | cedcfee | 2013-06-28 15:46:59 +0000 | [diff] [blame] | 85 | int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst); |
| 86 | if (DstIdx == -1) { |
| 87 | continue; |
| 88 | } |
| 89 | unsigned Dst = BI->getOperand(DstIdx).getReg(); |
Vincent Lejeune | 4ed9917 | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 90 | if (BI->getOpcode() == AMDGPU::DOT4_r600 || |
| 91 | BI->getOpcode() == AMDGPU::DOT4_eg) { |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 92 | Result[Dst] = AMDGPU::PV_X; |
| 93 | continue; |
| 94 | } |
Tom Stellard | e3d4cbc | 2013-06-28 15:47:08 +0000 | [diff] [blame^] | 95 | if (Dst == AMDGPU::OQAP) { |
| 96 | continue; |
| 97 | } |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 98 | unsigned PVReg = 0; |
| 99 | switch (TRI.getHWRegChan(Dst)) { |
| 100 | case 0: |
| 101 | PVReg = AMDGPU::PV_X; |
| 102 | break; |
| 103 | case 1: |
| 104 | PVReg = AMDGPU::PV_Y; |
| 105 | break; |
| 106 | case 2: |
| 107 | PVReg = AMDGPU::PV_Z; |
| 108 | break; |
| 109 | case 3: |
| 110 | PVReg = AMDGPU::PV_W; |
| 111 | break; |
| 112 | default: |
| 113 | llvm_unreachable("Invalid Chan"); |
| 114 | } |
| 115 | Result[Dst] = PVReg; |
| 116 | } while ((++BI)->isBundledWithPred()); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 117 | return Result; |
| 118 | } |
| 119 | |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 120 | void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs) |
| 121 | const { |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 122 | unsigned Ops[] = { |
| 123 | AMDGPU::OpName::src0, |
| 124 | AMDGPU::OpName::src1, |
| 125 | AMDGPU::OpName::src2 |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 126 | }; |
| 127 | for (unsigned i = 0; i < 3; i++) { |
| 128 | int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]); |
| 129 | if (OperandIdx < 0) |
| 130 | continue; |
| 131 | unsigned Src = MI->getOperand(OperandIdx).getReg(); |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 132 | const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src); |
| 133 | if (It != PVs.end()) |
| 134 | MI->getOperand(OperandIdx).setReg(It->second); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | public: |
| 138 | // Ctor. |
| 139 | R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI, |
| 140 | MachineDominatorTree &MDT) |
| 141 | : VLIWPacketizerList(MF, MLI, MDT, true), |
| 142 | TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())), |
| 143 | TRI(TII->getRegisterInfo()) { } |
| 144 | |
| 145 | // initPacketizerState - initialize some internal flags. |
| 146 | void initPacketizerState() { } |
| 147 | |
| 148 | // ignorePseudoInstruction - Ignore bundling of pseudo instructions. |
| 149 | bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // isSoloInstruction - return true if instruction MI can not be packetized |
| 154 | // with any other instruction, which means that MI itself is a packet. |
| 155 | bool isSoloInstruction(MachineInstr *MI) { |
| 156 | if (TII->isVector(*MI)) |
| 157 | return true; |
| 158 | if (!TII->isALUInstr(MI->getOpcode())) |
| 159 | return true; |
| 160 | if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::TRANS_ONLY) |
| 161 | return true; |
| 162 | if (TII->isTransOnly(MI)) |
| 163 | return true; |
Tom Stellard | cedcfee | 2013-06-28 15:46:59 +0000 | [diff] [blame] | 164 | if (MI->getOpcode() == AMDGPU::GROUP_BARRIER) |
| 165 | return true; |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
| 169 | // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ |
| 170 | // together. |
| 171 | bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) { |
| 172 | MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr(); |
| 173 | if (getSlot(MII) <= getSlot(MIJ)) |
| 174 | return false; |
| 175 | // Does MII and MIJ share the same pred_sel ? |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 176 | int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel), |
| 177 | OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 178 | unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0, |
| 179 | PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0; |
| 180 | if (PredI != PredJ) |
| 181 | return false; |
| 182 | if (SUJ->isSucc(SUI)) { |
| 183 | for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) { |
| 184 | const SDep &Dep = SUJ->Succs[i]; |
| 185 | if (Dep.getSUnit() != SUI) |
| 186 | continue; |
| 187 | if (Dep.getKind() == SDep::Anti) |
| 188 | continue; |
| 189 | if (Dep.getKind() == SDep::Output) |
| 190 | if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg()) |
| 191 | continue; |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | // isLegalToPruneDependencies - Is it legal to prune dependece between SUI |
| 199 | // and SUJ. |
| 200 | bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;} |
| 201 | |
| 202 | void setIsLastBit(MachineInstr *MI, unsigned Bit) const { |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 203 | unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 204 | MI->getOperand(LastOp).setImm(Bit); |
| 205 | } |
| 206 | |
| 207 | MachineBasicBlock::iterator addToPacket(MachineInstr *MI) { |
| 208 | CurrentPacketMIs.push_back(MI); |
| 209 | bool FitsConstLimits = TII->canBundle(CurrentPacketMIs); |
| 210 | DEBUG( |
| 211 | if (!FitsConstLimits) { |
| 212 | dbgs() << "Couldn't pack :\n"; |
| 213 | MI->dump(); |
| 214 | dbgs() << "with the following packets :\n"; |
| 215 | for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) { |
| 216 | CurrentPacketMIs[i]->dump(); |
| 217 | dbgs() << "\n"; |
| 218 | } |
| 219 | dbgs() << "because of Consts read limitations\n"; |
| 220 | }); |
Vincent Lejeune | 152ebee | 2013-05-02 21:52:55 +0000 | [diff] [blame] | 221 | const DenseMap<unsigned, unsigned> &PV = |
| 222 | getPreviousVector(CurrentPacketMIs.front()); |
Vincent Lejeune | 25c209e | 2013-05-17 16:50:02 +0000 | [diff] [blame] | 223 | std::vector<R600InstrInfo::BankSwizzle> BS; |
| 224 | bool FitsReadPortLimits = |
| 225 | TII->fitsReadPortLimitations(CurrentPacketMIs, PV, BS); |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 226 | DEBUG( |
| 227 | if (!FitsReadPortLimits) { |
| 228 | dbgs() << "Couldn't pack :\n"; |
| 229 | MI->dump(); |
| 230 | dbgs() << "with the following packets :\n"; |
| 231 | for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) { |
| 232 | CurrentPacketMIs[i]->dump(); |
| 233 | dbgs() << "\n"; |
| 234 | } |
| 235 | dbgs() << "because of Read port limitations\n"; |
| 236 | }); |
| 237 | bool isBundlable = FitsConstLimits && FitsReadPortLimits; |
Vincent Lejeune | 25c209e | 2013-05-17 16:50:02 +0000 | [diff] [blame] | 238 | if (isBundlable) { |
| 239 | for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) { |
| 240 | MachineInstr *MI = CurrentPacketMIs[i]; |
| 241 | unsigned Op = TII->getOperandIdx(MI->getOpcode(), |
Tom Stellard | 5e48a0e | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 242 | AMDGPU::OpName::bank_swizzle); |
Vincent Lejeune | 25c209e | 2013-05-17 16:50:02 +0000 | [diff] [blame] | 243 | MI->getOperand(Op).setImm(BS[i]); |
| 244 | } |
| 245 | } |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 246 | CurrentPacketMIs.pop_back(); |
| 247 | if (!isBundlable) { |
| 248 | endPacket(MI->getParent(), MI); |
| 249 | substitutePV(MI, getPreviousVector(MI)); |
| 250 | return VLIWPacketizerList::addToPacket(MI); |
| 251 | } |
| 252 | if (!CurrentPacketMIs.empty()) |
| 253 | setIsLastBit(CurrentPacketMIs.back(), 0); |
| 254 | substitutePV(MI, PV); |
| 255 | return VLIWPacketizerList::addToPacket(MI); |
| 256 | } |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) { |
| 260 | const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo(); |
| 261 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 262 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
| 263 | |
| 264 | // Instantiate the packetizer. |
| 265 | R600PacketizerList Packetizer(Fn, MLI, MDT); |
| 266 | |
| 267 | // DFA state table should not be empty. |
| 268 | assert(Packetizer.getResourceTracker() && "Empty DFA table!"); |
| 269 | |
| 270 | // |
| 271 | // Loop over all basic blocks and remove KILL pseudo-instructions |
| 272 | // These instructions confuse the dependence analysis. Consider: |
| 273 | // D0 = ... (Insn 0) |
| 274 | // R0 = KILL R0, D0 (Insn 1) |
| 275 | // R0 = ... (Insn 2) |
| 276 | // Here, Insn 1 will result in the dependence graph not emitting an output |
| 277 | // dependence between Insn 0 and Insn 2. This can lead to incorrect |
| 278 | // packetization |
| 279 | // |
| 280 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 281 | MBB != MBBe; ++MBB) { |
| 282 | MachineBasicBlock::iterator End = MBB->end(); |
| 283 | MachineBasicBlock::iterator MI = MBB->begin(); |
| 284 | while (MI != End) { |
| 285 | if (MI->isKill()) { |
| 286 | MachineBasicBlock::iterator DeleteMI = MI; |
| 287 | ++MI; |
| 288 | MBB->erase(DeleteMI); |
| 289 | End = MBB->end(); |
| 290 | continue; |
| 291 | } |
| 292 | ++MI; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Loop over all of the basic blocks. |
| 297 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 298 | MBB != MBBe; ++MBB) { |
| 299 | // Find scheduling regions and schedule / packetize each region. |
| 300 | unsigned RemainingCount = MBB->size(); |
| 301 | for(MachineBasicBlock::iterator RegionEnd = MBB->end(); |
| 302 | RegionEnd != MBB->begin();) { |
| 303 | // The next region starts above the previous region. Look backward in the |
| 304 | // instruction stream until we find the nearest boundary. |
| 305 | MachineBasicBlock::iterator I = RegionEnd; |
| 306 | for(;I != MBB->begin(); --I, --RemainingCount) { |
| 307 | if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn)) |
| 308 | break; |
| 309 | } |
| 310 | I = MBB->begin(); |
| 311 | |
| 312 | // Skip empty scheduling regions. |
| 313 | if (I == RegionEnd) { |
| 314 | RegionEnd = llvm::prior(RegionEnd); |
| 315 | --RemainingCount; |
| 316 | continue; |
| 317 | } |
| 318 | // Skip regions with one instruction. |
| 319 | if (I == llvm::prior(RegionEnd)) { |
| 320 | RegionEnd = llvm::prior(RegionEnd); |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | Packetizer.PacketizeMIs(MBB, I, RegionEnd); |
| 325 | RegionEnd = I; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | |
| 331 | } |
| 332 | |
Benjamin Kramer | 5c35290 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 333 | } // end anonymous namespace |
Vincent Lejeune | 25f259c | 2013-04-30 00:14:27 +0000 | [diff] [blame] | 334 | |
| 335 | llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) { |
| 336 | return new R600Packetizer(tm); |
| 337 | } |