1 //===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// This pass lowers the pseudo control flow instructions to real 11 /// machine instructions. 12 /// 13 /// All control flow is handled using predicated instructions and 14 /// a predicate stack. Each Scalar ALU controls the operations of 64 Vector 15 /// ALUs. The Scalar ALU can update the predicate for any of the Vector ALUs 16 /// by writting to the 64-bit EXEC register (each bit corresponds to a 17 /// single vector ALU). Typically, for predicates, a vector ALU will write 18 /// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each 19 /// Vector ALU) and then the ScalarALU will AND the VCC register with the 20 /// EXEC to update the predicates. 21 /// 22 /// For example: 23 /// %vcc = V_CMP_GT_F32 %vgpr1, %vgpr2 24 /// %sgpr0 = SI_IF %vcc 25 /// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 26 /// %sgpr0 = SI_ELSE %sgpr0 27 /// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr0 28 /// SI_END_CF %sgpr0 29 /// 30 /// becomes: 31 /// 32 /// %sgpr0 = S_AND_SAVEEXEC_B64 %vcc // Save and update the exec mask 33 /// %sgpr0 = S_XOR_B64 %sgpr0, %exec // Clear live bits from saved exec mask 34 /// S_CBRANCH_EXECZ label0 // This instruction is an optional 35 /// // optimization which allows us to 36 /// // branch if all the bits of 37 /// // EXEC are zero. 38 /// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 // Do the IF block of the branch 39 /// 40 /// label0: 41 /// %sgpr0 = S_OR_SAVEEXEC_B64 %sgpr0 // Restore the exec mask for the Then block 42 /// %exec = S_XOR_B64 %sgpr0, %exec // Update the exec mask 43 /// S_BRANCH_EXECZ label1 // Use our branch optimization 44 /// // instruction again. 45 /// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr // Do the THEN block 46 /// label1: 47 /// %exec = S_OR_B64 %exec, %sgpr0 // Re-enable saved exec mask bits 48 //===----------------------------------------------------------------------===// 49 50 #include "AMDGPU.h" 51 #include "AMDGPUSubtarget.h" 52 #include "SIInstrInfo.h" 53 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 54 #include "llvm/ADT/SetVector.h" 55 #include "llvm/ADT/SmallSet.h" 56 #include "llvm/ADT/SmallVector.h" 57 #include "llvm/ADT/StringRef.h" 58 #include "llvm/CodeGen/LiveIntervals.h" 59 #include "llvm/CodeGen/MachineBasicBlock.h" 60 #include "llvm/CodeGen/MachineFunction.h" 61 #include "llvm/CodeGen/MachineFunctionPass.h" 62 #include "llvm/CodeGen/MachineInstr.h" 63 #include "llvm/CodeGen/MachineInstrBuilder.h" 64 #include "llvm/CodeGen/MachineOperand.h" 65 #include "llvm/CodeGen/MachineRegisterInfo.h" 66 #include "llvm/CodeGen/Passes.h" 67 #include "llvm/CodeGen/SlotIndexes.h" 68 #include "llvm/CodeGen/TargetRegisterInfo.h" 69 #include "llvm/MC/MCRegisterInfo.h" 70 #include "llvm/Pass.h" 71 #include <cassert> 72 #include <iterator> 73 74 using namespace llvm; 75 76 #define DEBUG_TYPE "si-lower-control-flow" 77 78 static cl::opt<bool> 79 RemoveRedundantEndcf("amdgpu-remove-redundant-endcf", 80 cl::init(true), cl::ReallyHidden); 81 82 namespace { 83 84 class SILowerControlFlow : public MachineFunctionPass { 85 private: 86 const SIRegisterInfo *TRI = nullptr; 87 const SIInstrInfo *TII = nullptr; 88 LiveIntervals *LIS = nullptr; 89 MachineRegisterInfo *MRI = nullptr; 90 SetVector<MachineInstr*> LoweredEndCf; 91 DenseSet<Register> LoweredIf; 92 93 const TargetRegisterClass *BoolRC = nullptr; 94 unsigned AndOpc; 95 unsigned OrOpc; 96 unsigned XorOpc; 97 unsigned MovTermOpc; 98 unsigned Andn2TermOpc; 99 unsigned XorTermrOpc; 100 unsigned OrSaveExecOpc; 101 unsigned Exec; 102 103 void emitIf(MachineInstr &MI); 104 void emitElse(MachineInstr &MI); 105 void emitIfBreak(MachineInstr &MI); 106 void emitLoop(MachineInstr &MI); 107 void emitEndCf(MachineInstr &MI); 108 109 void findMaskOperands(MachineInstr &MI, unsigned OpNo, 110 SmallVectorImpl<MachineOperand> &Src) const; 111 112 void combineMasks(MachineInstr &MI); 113 114 // Skip to the next instruction, ignoring debug instructions, and trivial 115 // block boundaries (blocks that have one (typically fallthrough) successor, 116 // and the successor has one predecessor. 117 MachineBasicBlock::iterator 118 skipIgnoreExecInstsTrivialSucc(MachineBasicBlock &MBB, 119 MachineBasicBlock::iterator It) const; 120 121 // Remove redundant SI_END_CF instructions. 122 void optimizeEndCf(); 123 124 public: 125 static char ID; 126 127 SILowerControlFlow() : MachineFunctionPass(ID) {} 128 129 bool runOnMachineFunction(MachineFunction &MF) override; 130 131 StringRef getPassName() const override { 132 return "SI Lower control flow pseudo instructions"; 133 } 134 135 void getAnalysisUsage(AnalysisUsage &AU) const override { 136 // Should preserve the same set that TwoAddressInstructions does. 137 AU.addPreserved<SlotIndexes>(); 138 AU.addPreserved<LiveIntervals>(); 139 AU.addPreservedID(LiveVariablesID); 140 AU.addPreservedID(MachineLoopInfoID); 141 AU.addPreservedID(MachineDominatorsID); 142 AU.setPreservesCFG(); 143 MachineFunctionPass::getAnalysisUsage(AU); 144 } 145 }; 146 147 } // end anonymous namespace 148 149 char SILowerControlFlow::ID = 0; 150 151 INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE, 152 "SI lower control flow", false, false) 153 154 static void setImpSCCDefDead(MachineInstr &MI, bool IsDead) { 155 MachineOperand &ImpDefSCC = MI.getOperand(3); 156 assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef()); 157 158 ImpDefSCC.setIsDead(IsDead); 159 } 160 161 char &llvm::SILowerControlFlowID = SILowerControlFlow::ID; 162 163 static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI, 164 const SIInstrInfo *TII) { 165 Register SaveExecReg = MI.getOperand(0).getReg(); 166 auto U = MRI->use_instr_nodbg_begin(SaveExecReg); 167 168 if (U == MRI->use_instr_nodbg_end() || 169 std::next(U) != MRI->use_instr_nodbg_end() || 170 U->getOpcode() != AMDGPU::SI_END_CF) 171 return false; 172 173 // Check for SI_KILL_*_TERMINATOR on path from if to endif. 174 // if there is any such terminator simplififcations are not safe. 175 auto SMBB = MI.getParent(); 176 auto EMBB = U->getParent(); 177 DenseSet<const MachineBasicBlock*> Visited; 178 SmallVector<MachineBasicBlock*, 4> Worklist(SMBB->succ_begin(), 179 SMBB->succ_end()); 180 181 while (!Worklist.empty()) { 182 MachineBasicBlock *MBB = Worklist.pop_back_val(); 183 184 if (MBB == EMBB || !Visited.insert(MBB).second) 185 continue; 186 for(auto &Term : MBB->terminators()) 187 if (TII->isKillTerminator(Term.getOpcode())) 188 return false; 189 190 Worklist.append(MBB->succ_begin(), MBB->succ_end()); 191 } 192 193 return true; 194 } 195 196 void SILowerControlFlow::emitIf(MachineInstr &MI) { 197 MachineBasicBlock &MBB = *MI.getParent(); 198 const DebugLoc &DL = MI.getDebugLoc(); 199 MachineBasicBlock::iterator I(&MI); 200 Register SaveExecReg = MI.getOperand(0).getReg(); 201 MachineOperand& Cond = MI.getOperand(1); 202 assert(Cond.getSubReg() == AMDGPU::NoSubRegister); 203 204 MachineOperand &ImpDefSCC = MI.getOperand(4); 205 assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef()); 206 207 // If there is only one use of save exec register and that use is SI_END_CF, 208 // we can optimize SI_IF by returning the full saved exec mask instead of 209 // just cleared bits. 210 bool SimpleIf = isSimpleIf(MI, MRI, TII); 211 212 // Add an implicit def of exec to discourage scheduling VALU after this which 213 // will interfere with trying to form s_and_saveexec_b64 later. 214 Register CopyReg = SimpleIf ? SaveExecReg 215 : MRI->createVirtualRegister(BoolRC); 216 MachineInstr *CopyExec = 217 BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), CopyReg) 218 .addReg(Exec) 219 .addReg(Exec, RegState::ImplicitDefine); 220 LoweredIf.insert(CopyReg); 221 222 Register Tmp = MRI->createVirtualRegister(BoolRC); 223 224 MachineInstr *And = 225 BuildMI(MBB, I, DL, TII->get(AndOpc), Tmp) 226 .addReg(CopyReg) 227 .add(Cond); 228 229 setImpSCCDefDead(*And, true); 230 231 MachineInstr *Xor = nullptr; 232 if (!SimpleIf) { 233 Xor = 234 BuildMI(MBB, I, DL, TII->get(XorOpc), SaveExecReg) 235 .addReg(Tmp) 236 .addReg(CopyReg); 237 setImpSCCDefDead(*Xor, ImpDefSCC.isDead()); 238 } 239 240 // Use a copy that is a terminator to get correct spill code placement it with 241 // fast regalloc. 242 MachineInstr *SetExec = 243 BuildMI(MBB, I, DL, TII->get(MovTermOpc), Exec) 244 .addReg(Tmp, RegState::Kill); 245 246 // Insert the S_CBRANCH_EXECZ instruction which will be optimized later 247 // during SIRemoveShortExecBranches. 248 MachineInstr *NewBr = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ)) 249 .add(MI.getOperand(2)); 250 251 if (!LIS) { 252 MI.eraseFromParent(); 253 return; 254 } 255 256 LIS->InsertMachineInstrInMaps(*CopyExec); 257 258 // Replace with and so we don't need to fix the live interval for condition 259 // register. 260 LIS->ReplaceMachineInstrInMaps(MI, *And); 261 262 if (!SimpleIf) 263 LIS->InsertMachineInstrInMaps(*Xor); 264 LIS->InsertMachineInstrInMaps(*SetExec); 265 LIS->InsertMachineInstrInMaps(*NewBr); 266 267 LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC); 268 MI.eraseFromParent(); 269 270 // FIXME: Is there a better way of adjusting the liveness? It shouldn't be 271 // hard to add another def here but I'm not sure how to correctly update the 272 // valno. 273 LIS->removeInterval(SaveExecReg); 274 LIS->createAndComputeVirtRegInterval(SaveExecReg); 275 LIS->createAndComputeVirtRegInterval(Tmp); 276 if (!SimpleIf) 277 LIS->createAndComputeVirtRegInterval(CopyReg); 278 } 279 280 void SILowerControlFlow::emitElse(MachineInstr &MI) { 281 MachineBasicBlock &MBB = *MI.getParent(); 282 const DebugLoc &DL = MI.getDebugLoc(); 283 284 Register DstReg = MI.getOperand(0).getReg(); 285 286 bool ExecModified = MI.getOperand(3).getImm() != 0; 287 MachineBasicBlock::iterator Start = MBB.begin(); 288 289 // We are running before TwoAddressInstructions, and si_else's operands are 290 // tied. In order to correctly tie the registers, split this into a copy of 291 // the src like it does. 292 Register CopyReg = MRI->createVirtualRegister(BoolRC); 293 MachineInstr *CopyExec = 294 BuildMI(MBB, Start, DL, TII->get(AMDGPU::COPY), CopyReg) 295 .add(MI.getOperand(1)); // Saved EXEC 296 297 // This must be inserted before phis and any spill code inserted before the 298 // else. 299 Register SaveReg = ExecModified ? 300 MRI->createVirtualRegister(BoolRC) : DstReg; 301 MachineInstr *OrSaveExec = 302 BuildMI(MBB, Start, DL, TII->get(OrSaveExecOpc), SaveReg) 303 .addReg(CopyReg); 304 305 MachineBasicBlock *DestBB = MI.getOperand(2).getMBB(); 306 307 MachineBasicBlock::iterator ElsePt(MI); 308 309 if (ExecModified) { 310 MachineInstr *And = 311 BuildMI(MBB, ElsePt, DL, TII->get(AndOpc), DstReg) 312 .addReg(Exec) 313 .addReg(SaveReg); 314 315 if (LIS) 316 LIS->InsertMachineInstrInMaps(*And); 317 } 318 319 MachineInstr *Xor = 320 BuildMI(MBB, ElsePt, DL, TII->get(XorTermrOpc), Exec) 321 .addReg(Exec) 322 .addReg(DstReg); 323 324 MachineInstr *Branch = 325 BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ)) 326 .addMBB(DestBB); 327 328 if (!LIS) { 329 MI.eraseFromParent(); 330 return; 331 } 332 333 LIS->RemoveMachineInstrFromMaps(MI); 334 MI.eraseFromParent(); 335 336 LIS->InsertMachineInstrInMaps(*CopyExec); 337 LIS->InsertMachineInstrInMaps(*OrSaveExec); 338 339 LIS->InsertMachineInstrInMaps(*Xor); 340 LIS->InsertMachineInstrInMaps(*Branch); 341 342 // src reg is tied to dst reg. 343 LIS->removeInterval(DstReg); 344 LIS->createAndComputeVirtRegInterval(DstReg); 345 LIS->createAndComputeVirtRegInterval(CopyReg); 346 if (ExecModified) 347 LIS->createAndComputeVirtRegInterval(SaveReg); 348 349 // Let this be recomputed. 350 LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC); 351 } 352 353 void SILowerControlFlow::emitIfBreak(MachineInstr &MI) { 354 MachineBasicBlock &MBB = *MI.getParent(); 355 const DebugLoc &DL = MI.getDebugLoc(); 356 auto Dst = MI.getOperand(0).getReg(); 357 358 // Skip ANDing with exec if the break condition is already masked by exec 359 // because it is a V_CMP in the same basic block. (We know the break 360 // condition operand was an i1 in IR, so if it is a VALU instruction it must 361 // be one with a carry-out.) 362 bool SkipAnding = false; 363 if (MI.getOperand(1).isReg()) { 364 if (MachineInstr *Def = MRI->getUniqueVRegDef(MI.getOperand(1).getReg())) { 365 SkipAnding = Def->getParent() == MI.getParent() 366 && SIInstrInfo::isVALU(*Def); 367 } 368 } 369 370 // AND the break condition operand with exec, then OR that into the "loop 371 // exit" mask. 372 MachineInstr *And = nullptr, *Or = nullptr; 373 if (!SkipAnding) { 374 Register AndReg = MRI->createVirtualRegister(BoolRC); 375 And = BuildMI(MBB, &MI, DL, TII->get(AndOpc), AndReg) 376 .addReg(Exec) 377 .add(MI.getOperand(1)); 378 Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst) 379 .addReg(AndReg) 380 .add(MI.getOperand(2)); 381 if (LIS) 382 LIS->createAndComputeVirtRegInterval(AndReg); 383 } else 384 Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst) 385 .add(MI.getOperand(1)) 386 .add(MI.getOperand(2)); 387 388 if (LIS) { 389 if (And) 390 LIS->InsertMachineInstrInMaps(*And); 391 LIS->ReplaceMachineInstrInMaps(MI, *Or); 392 } 393 394 MI.eraseFromParent(); 395 } 396 397 void SILowerControlFlow::emitLoop(MachineInstr &MI) { 398 MachineBasicBlock &MBB = *MI.getParent(); 399 const DebugLoc &DL = MI.getDebugLoc(); 400 401 MachineInstr *AndN2 = 402 BuildMI(MBB, &MI, DL, TII->get(Andn2TermOpc), Exec) 403 .addReg(Exec) 404 .add(MI.getOperand(0)); 405 406 MachineInstr *Branch = 407 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) 408 .add(MI.getOperand(1)); 409 410 if (LIS) { 411 LIS->ReplaceMachineInstrInMaps(MI, *AndN2); 412 LIS->InsertMachineInstrInMaps(*Branch); 413 } 414 415 MI.eraseFromParent(); 416 } 417 418 MachineBasicBlock::iterator 419 SILowerControlFlow::skipIgnoreExecInstsTrivialSucc( 420 MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const { 421 422 SmallSet<const MachineBasicBlock *, 4> Visited; 423 MachineBasicBlock *B = &MBB; 424 do { 425 if (!Visited.insert(B).second) 426 return MBB.end(); 427 428 auto E = B->end(); 429 for ( ; It != E; ++It) { 430 if (TII->mayReadEXEC(*MRI, *It)) 431 break; 432 } 433 434 if (It != E) 435 return It; 436 437 if (B->succ_size() != 1) 438 return MBB.end(); 439 440 // If there is one trivial successor, advance to the next block. 441 MachineBasicBlock *Succ = *B->succ_begin(); 442 443 It = Succ->begin(); 444 B = Succ; 445 } while (true); 446 } 447 448 void SILowerControlFlow::emitEndCf(MachineInstr &MI) { 449 MachineBasicBlock &MBB = *MI.getParent(); 450 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 451 unsigned CFMask = MI.getOperand(0).getReg(); 452 MachineInstr *Def = MRI.getUniqueVRegDef(CFMask); 453 const DebugLoc &DL = MI.getDebugLoc(); 454 455 MachineBasicBlock::iterator InsPt = 456 Def && Def->getParent() == &MBB ? std::next(MachineBasicBlock::iterator(Def)) 457 : MBB.begin(); 458 MachineInstr *NewMI = BuildMI(MBB, InsPt, DL, TII->get(OrOpc), Exec) 459 .addReg(Exec) 460 .add(MI.getOperand(0)); 461 462 LoweredEndCf.insert(NewMI); 463 464 if (LIS) 465 LIS->ReplaceMachineInstrInMaps(MI, *NewMI); 466 467 MI.eraseFromParent(); 468 469 if (LIS) 470 LIS->handleMove(*NewMI); 471 } 472 473 // Returns replace operands for a logical operation, either single result 474 // for exec or two operands if source was another equivalent operation. 475 void SILowerControlFlow::findMaskOperands(MachineInstr &MI, unsigned OpNo, 476 SmallVectorImpl<MachineOperand> &Src) const { 477 MachineOperand &Op = MI.getOperand(OpNo); 478 if (!Op.isReg() || !Register::isVirtualRegister(Op.getReg())) { 479 Src.push_back(Op); 480 return; 481 } 482 483 MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg()); 484 if (!Def || Def->getParent() != MI.getParent() || 485 !(Def->isFullCopy() || (Def->getOpcode() == MI.getOpcode()))) 486 return; 487 488 // Make sure we do not modify exec between def and use. 489 // A copy with implcitly defined exec inserted earlier is an exclusion, it 490 // does not really modify exec. 491 for (auto I = Def->getIterator(); I != MI.getIterator(); ++I) 492 if (I->modifiesRegister(AMDGPU::EXEC, TRI) && 493 !(I->isCopy() && I->getOperand(0).getReg() != Exec)) 494 return; 495 496 for (const auto &SrcOp : Def->explicit_operands()) 497 if (SrcOp.isReg() && SrcOp.isUse() && 498 (Register::isVirtualRegister(SrcOp.getReg()) || SrcOp.getReg() == Exec)) 499 Src.push_back(SrcOp); 500 } 501 502 // Search and combine pairs of equivalent instructions, like 503 // S_AND_B64 x, (S_AND_B64 x, y) => S_AND_B64 x, y 504 // S_OR_B64 x, (S_OR_B64 x, y) => S_OR_B64 x, y 505 // One of the operands is exec mask. 506 void SILowerControlFlow::combineMasks(MachineInstr &MI) { 507 assert(MI.getNumExplicitOperands() == 3); 508 SmallVector<MachineOperand, 4> Ops; 509 unsigned OpToReplace = 1; 510 findMaskOperands(MI, 1, Ops); 511 if (Ops.size() == 1) OpToReplace = 2; // First operand can be exec or its copy 512 findMaskOperands(MI, 2, Ops); 513 if (Ops.size() != 3) return; 514 515 unsigned UniqueOpndIdx; 516 if (Ops[0].isIdenticalTo(Ops[1])) UniqueOpndIdx = 2; 517 else if (Ops[0].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1; 518 else if (Ops[1].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1; 519 else return; 520 521 Register Reg = MI.getOperand(OpToReplace).getReg(); 522 MI.RemoveOperand(OpToReplace); 523 MI.addOperand(Ops[UniqueOpndIdx]); 524 if (MRI->use_empty(Reg)) 525 MRI->getUniqueVRegDef(Reg)->eraseFromParent(); 526 } 527 528 void SILowerControlFlow::optimizeEndCf() { 529 // If the only instruction immediately following this END_CF is an another 530 // END_CF in the only successor we can avoid emitting exec mask restore here. 531 if (!RemoveRedundantEndcf) 532 return; 533 534 for (MachineInstr *MI : LoweredEndCf) { 535 MachineBasicBlock &MBB = *MI->getParent(); 536 auto Next = 537 skipIgnoreExecInstsTrivialSucc(MBB, std::next(MI->getIterator())); 538 if (Next == MBB.end() || !LoweredEndCf.count(&*Next)) 539 continue; 540 // Only skip inner END_CF if outer ENDCF belongs to SI_IF. 541 // If that belongs to SI_ELSE then saved mask has an inverted value. 542 Register SavedExec 543 = TII->getNamedOperand(*Next, AMDGPU::OpName::src1)->getReg(); 544 assert(SavedExec.isVirtual() && "Expected saved exec to be src1!"); 545 546 const MachineInstr *Def = MRI->getUniqueVRegDef(SavedExec); 547 if (Def && LoweredIf.count(SavedExec)) { 548 LLVM_DEBUG(dbgs() << "Skip redundant "; MI->dump()); 549 if (LIS) 550 LIS->RemoveMachineInstrFromMaps(*MI); 551 MI->eraseFromParent(); 552 } 553 } 554 } 555 556 bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) { 557 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 558 TII = ST.getInstrInfo(); 559 TRI = &TII->getRegisterInfo(); 560 561 // This doesn't actually need LiveIntervals, but we can preserve them. 562 LIS = getAnalysisIfAvailable<LiveIntervals>(); 563 MRI = &MF.getRegInfo(); 564 BoolRC = TRI->getBoolRC(); 565 566 if (ST.isWave32()) { 567 AndOpc = AMDGPU::S_AND_B32; 568 OrOpc = AMDGPU::S_OR_B32; 569 XorOpc = AMDGPU::S_XOR_B32; 570 MovTermOpc = AMDGPU::S_MOV_B32_term; 571 Andn2TermOpc = AMDGPU::S_ANDN2_B32_term; 572 XorTermrOpc = AMDGPU::S_XOR_B32_term; 573 OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B32; 574 Exec = AMDGPU::EXEC_LO; 575 } else { 576 AndOpc = AMDGPU::S_AND_B64; 577 OrOpc = AMDGPU::S_OR_B64; 578 XorOpc = AMDGPU::S_XOR_B64; 579 MovTermOpc = AMDGPU::S_MOV_B64_term; 580 Andn2TermOpc = AMDGPU::S_ANDN2_B64_term; 581 XorTermrOpc = AMDGPU::S_XOR_B64_term; 582 OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B64; 583 Exec = AMDGPU::EXEC; 584 } 585 586 MachineFunction::iterator NextBB; 587 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); 588 BI != BE; BI = NextBB) { 589 NextBB = std::next(BI); 590 MachineBasicBlock &MBB = *BI; 591 592 MachineBasicBlock::iterator I, Next, Last; 593 594 for (I = MBB.begin(), Last = MBB.end(); I != MBB.end(); I = Next) { 595 Next = std::next(I); 596 MachineInstr &MI = *I; 597 598 switch (MI.getOpcode()) { 599 case AMDGPU::SI_IF: 600 emitIf(MI); 601 break; 602 603 case AMDGPU::SI_ELSE: 604 emitElse(MI); 605 break; 606 607 case AMDGPU::SI_IF_BREAK: 608 emitIfBreak(MI); 609 break; 610 611 case AMDGPU::SI_LOOP: 612 emitLoop(MI); 613 break; 614 615 case AMDGPU::SI_END_CF: 616 emitEndCf(MI); 617 break; 618 619 case AMDGPU::S_AND_B64: 620 case AMDGPU::S_OR_B64: 621 case AMDGPU::S_AND_B32: 622 case AMDGPU::S_OR_B32: 623 // Cleanup bit manipulations on exec mask 624 combineMasks(MI); 625 Last = I; 626 continue; 627 628 default: 629 Last = I; 630 continue; 631 } 632 633 // Replay newly inserted code to combine masks 634 Next = (Last == MBB.end()) ? MBB.begin() : Last; 635 } 636 } 637 638 optimizeEndCf(); 639 640 LoweredEndCf.clear(); 641 LoweredIf.clear(); 642 643 return true; 644 } 645