1 //===-- SIAnnotateControlFlow.cpp - ------------------===// 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 /// Annotates the control flow with hardware specific intrinsics. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AMDGPU.h" 16 #include "llvm/ADT/DepthFirstIterator.h" 17 #include "llvm/Analysis/DivergenceAnalysis.h" 18 #include "llvm/Analysis/LoopInfo.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/Dominators.h" 21 #include "llvm/IR/Instructions.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/Pass.h" 24 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 25 #include "llvm/Transforms/Utils/SSAUpdater.h" 26 27 using namespace llvm; 28 29 #define DEBUG_TYPE "si-annotate-control-flow" 30 31 namespace { 32 33 // Complex types used in this pass 34 typedef std::pair<BasicBlock *, Value *> StackEntry; 35 typedef SmallVector<StackEntry, 16> StackVector; 36 37 // Intrinsic names the control flow is annotated with 38 static const char *const IfIntrinsic = "llvm.amdgcn.if"; 39 static const char *const ElseIntrinsic = "llvm.amdgcn.else"; 40 static const char *const BreakIntrinsic = "llvm.amdgcn.break"; 41 static const char *const IfBreakIntrinsic = "llvm.amdgcn.if.break"; 42 static const char *const ElseBreakIntrinsic = "llvm.amdgcn.else.break"; 43 static const char *const LoopIntrinsic = "llvm.amdgcn.loop"; 44 static const char *const EndCfIntrinsic = "llvm.amdgcn.end.cf"; 45 46 class SIAnnotateControlFlow : public FunctionPass { 47 DivergenceAnalysis *DA; 48 49 Type *Boolean; 50 Type *Void; 51 Type *Int64; 52 Type *ReturnStruct; 53 54 ConstantInt *BoolTrue; 55 ConstantInt *BoolFalse; 56 UndefValue *BoolUndef; 57 Constant *Int64Zero; 58 59 Constant *If; 60 Constant *Else; 61 Constant *Break; 62 Constant *IfBreak; 63 Constant *ElseBreak; 64 Constant *Loop; 65 Constant *EndCf; 66 67 DominatorTree *DT; 68 StackVector Stack; 69 70 LoopInfo *LI; 71 72 bool isUniform(BranchInst *T); 73 74 bool isTopOfStack(BasicBlock *BB); 75 76 Value *popSaved(); 77 78 void push(BasicBlock *BB, Value *Saved); 79 80 bool isElse(PHINode *Phi); 81 82 void eraseIfUnused(PHINode *Phi); 83 84 void openIf(BranchInst *Term); 85 86 void insertElse(BranchInst *Term); 87 88 Value *handleLoopCondition(Value *Cond, PHINode *Broken, 89 llvm::Loop *L, BranchInst *Term); 90 91 void handleLoop(BranchInst *Term); 92 93 void closeControlFlow(BasicBlock *BB); 94 95 public: 96 static char ID; 97 98 SIAnnotateControlFlow(): 99 FunctionPass(ID) { } 100 101 bool doInitialization(Module &M) override; 102 103 bool runOnFunction(Function &F) override; 104 105 const char *getPassName() const override { 106 return "SI annotate control flow"; 107 } 108 109 void getAnalysisUsage(AnalysisUsage &AU) const override { 110 AU.addRequired<LoopInfoWrapperPass>(); 111 AU.addRequired<DominatorTreeWrapperPass>(); 112 AU.addRequired<DivergenceAnalysis>(); 113 AU.addPreserved<DominatorTreeWrapperPass>(); 114 FunctionPass::getAnalysisUsage(AU); 115 } 116 117 }; 118 119 } // end anonymous namespace 120 121 INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE, 122 "Annotate SI Control Flow", false, false) 123 INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis) 124 INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE, 125 "Annotate SI Control Flow", false, false) 126 127 char SIAnnotateControlFlow::ID = 0; 128 129 /// \brief Initialize all the types and constants used in the pass 130 bool SIAnnotateControlFlow::doInitialization(Module &M) { 131 LLVMContext &Context = M.getContext(); 132 133 Void = Type::getVoidTy(Context); 134 Boolean = Type::getInt1Ty(Context); 135 Int64 = Type::getInt64Ty(Context); 136 ReturnStruct = StructType::get(Boolean, Int64, (Type *)nullptr); 137 138 BoolTrue = ConstantInt::getTrue(Context); 139 BoolFalse = ConstantInt::getFalse(Context); 140 BoolUndef = UndefValue::get(Boolean); 141 Int64Zero = ConstantInt::get(Int64, 0); 142 143 If = M.getOrInsertFunction( 144 IfIntrinsic, ReturnStruct, Boolean, (Type *)nullptr); 145 146 Else = M.getOrInsertFunction( 147 ElseIntrinsic, ReturnStruct, Int64, (Type *)nullptr); 148 149 Break = M.getOrInsertFunction( 150 BreakIntrinsic, Int64, Int64, (Type *)nullptr); 151 cast<Function>(Break)->setDoesNotAccessMemory(); 152 153 IfBreak = M.getOrInsertFunction( 154 IfBreakIntrinsic, Int64, Boolean, Int64, (Type *)nullptr); 155 cast<Function>(IfBreak)->setDoesNotAccessMemory();; 156 157 ElseBreak = M.getOrInsertFunction( 158 ElseBreakIntrinsic, Int64, Int64, Int64, (Type *)nullptr); 159 cast<Function>(ElseBreak)->setDoesNotAccessMemory(); 160 161 Loop = M.getOrInsertFunction( 162 LoopIntrinsic, Boolean, Int64, (Type *)nullptr); 163 164 EndCf = M.getOrInsertFunction( 165 EndCfIntrinsic, Void, Int64, (Type *)nullptr); 166 167 return false; 168 } 169 170 /// \brief Is the branch condition uniform or did the StructurizeCFG pass 171 /// consider it as such? 172 bool SIAnnotateControlFlow::isUniform(BranchInst *T) { 173 return DA->isUniform(T->getCondition()) || 174 T->getMetadata("structurizecfg.uniform") != nullptr; 175 } 176 177 /// \brief Is BB the last block saved on the stack ? 178 bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) { 179 return !Stack.empty() && Stack.back().first == BB; 180 } 181 182 /// \brief Pop the last saved value from the control flow stack 183 Value *SIAnnotateControlFlow::popSaved() { 184 return Stack.pop_back_val().second; 185 } 186 187 /// \brief Push a BB and saved value to the control flow stack 188 void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) { 189 Stack.push_back(std::make_pair(BB, Saved)); 190 } 191 192 /// \brief Can the condition represented by this PHI node treated like 193 /// an "Else" block? 194 bool SIAnnotateControlFlow::isElse(PHINode *Phi) { 195 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock(); 196 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { 197 if (Phi->getIncomingBlock(i) == IDom) { 198 199 if (Phi->getIncomingValue(i) != BoolTrue) 200 return false; 201 202 } else { 203 if (Phi->getIncomingValue(i) != BoolFalse) 204 return false; 205 206 } 207 } 208 return true; 209 } 210 211 // \brief Erase "Phi" if it is not used any more 212 void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) { 213 if (!Phi->hasNUsesOrMore(1)) 214 Phi->eraseFromParent(); 215 } 216 217 /// \brief Open a new "If" block 218 void SIAnnotateControlFlow::openIf(BranchInst *Term) { 219 if (isUniform(Term)) { 220 return; 221 } 222 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term); 223 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term)); 224 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term)); 225 } 226 227 /// \brief Close the last "If" block and open a new "Else" block 228 void SIAnnotateControlFlow::insertElse(BranchInst *Term) { 229 if (isUniform(Term)) { 230 return; 231 } 232 Value *Ret = CallInst::Create(Else, popSaved(), "", Term); 233 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term)); 234 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term)); 235 } 236 237 /// \brief Recursively handle the condition leading to a loop 238 Value *SIAnnotateControlFlow::handleLoopCondition(Value *Cond, PHINode *Broken, 239 llvm::Loop *L, BranchInst *Term) { 240 241 // Only search through PHI nodes which are inside the loop. If we try this 242 // with PHI nodes that are outside of the loop, we end up inserting new PHI 243 // nodes outside of the loop which depend on values defined inside the loop. 244 // This will break the module with 245 // 'Instruction does not dominate all users!' errors. 246 PHINode *Phi = nullptr; 247 if ((Phi = dyn_cast<PHINode>(Cond)) && L->contains(Phi)) { 248 249 BasicBlock *Parent = Phi->getParent(); 250 PHINode *NewPhi = PHINode::Create(Int64, 0, "", &Parent->front()); 251 Value *Ret = NewPhi; 252 253 // Handle all non-constant incoming values first 254 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { 255 Value *Incoming = Phi->getIncomingValue(i); 256 BasicBlock *From = Phi->getIncomingBlock(i); 257 if (isa<ConstantInt>(Incoming)) { 258 NewPhi->addIncoming(Broken, From); 259 continue; 260 } 261 262 Phi->setIncomingValue(i, BoolFalse); 263 Value *PhiArg = handleLoopCondition(Incoming, Broken, L, Term); 264 NewPhi->addIncoming(PhiArg, From); 265 } 266 267 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock(); 268 269 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { 270 271 Value *Incoming = Phi->getIncomingValue(i); 272 if (Incoming != BoolTrue) 273 continue; 274 275 BasicBlock *From = Phi->getIncomingBlock(i); 276 if (From == IDom) { 277 // We're in the following situation: 278 // IDom/From 279 // | \ 280 // | If-block 281 // | / 282 // Parent 283 // where we want to break out of the loop if the If-block is not taken. 284 // Due to the depth-first traversal, there should be an end.cf 285 // intrinsic in Parent, and we insert an else.break before it. 286 // 287 // Note that the end.cf need not be the first non-phi instruction 288 // of parent, particularly when we're dealing with a multi-level 289 // break, but it should occur within a group of intrinsic calls 290 // at the beginning of the block. 291 CallInst *OldEnd = dyn_cast<CallInst>(Parent->getFirstInsertionPt()); 292 while (OldEnd && OldEnd->getCalledFunction() != EndCf) 293 OldEnd = dyn_cast<CallInst>(OldEnd->getNextNode()); 294 if (OldEnd && OldEnd->getCalledFunction() == EndCf) { 295 Value *Args[] = { OldEnd->getArgOperand(0), NewPhi }; 296 Ret = CallInst::Create(ElseBreak, Args, "", OldEnd); 297 continue; 298 } 299 } 300 TerminatorInst *Insert = From->getTerminator(); 301 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert); 302 NewPhi->setIncomingValue(i, PhiArg); 303 } 304 eraseIfUnused(Phi); 305 return Ret; 306 307 } else if (Instruction *Inst = dyn_cast<Instruction>(Cond)) { 308 BasicBlock *Parent = Inst->getParent(); 309 Instruction *Insert; 310 if (L->contains(Inst)) { 311 Insert = Parent->getTerminator(); 312 } else { 313 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime(); 314 } 315 Value *Args[] = { Cond, Broken }; 316 return CallInst::Create(IfBreak, Args, "", Insert); 317 318 // Insert IfBreak before TERM for constant COND. 319 } else if (isa<ConstantInt>(Cond)) { 320 Value *Args[] = { Cond, Broken }; 321 return CallInst::Create(IfBreak, Args, "", Term); 322 323 } else { 324 llvm_unreachable("Unhandled loop condition!"); 325 } 326 return nullptr; 327 } 328 329 /// \brief Handle a back edge (loop) 330 void SIAnnotateControlFlow::handleLoop(BranchInst *Term) { 331 if (isUniform(Term)) { 332 return; 333 } 334 335 BasicBlock *BB = Term->getParent(); 336 llvm::Loop *L = LI->getLoopFor(BB); 337 if (!L) 338 return; 339 BasicBlock *Target = Term->getSuccessor(1); 340 PHINode *Broken = PHINode::Create(Int64, 0, "", &Target->front()); 341 342 Value *Cond = Term->getCondition(); 343 Term->setCondition(BoolTrue); 344 Value *Arg = handleLoopCondition(Cond, Broken, L, Term); 345 346 for (pred_iterator PI = pred_begin(Target), PE = pred_end(Target); 347 PI != PE; ++PI) { 348 349 Broken->addIncoming(*PI == BB ? Arg : Int64Zero, *PI); 350 } 351 352 Term->setCondition(CallInst::Create(Loop, Arg, "", Term)); 353 push(Term->getSuccessor(0), Arg); 354 }/// \brief Close the last opened control flow 355 void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) { 356 llvm::Loop *L = LI->getLoopFor(BB); 357 358 assert(Stack.back().first == BB); 359 360 if (L && L->getHeader() == BB) { 361 // We can't insert an EndCF call into a loop header, because it will 362 // get executed on every iteration of the loop, when it should be 363 // executed only once before the loop. 364 SmallVector <BasicBlock*, 8> Latches; 365 L->getLoopLatches(Latches); 366 367 std::vector<BasicBlock*> Preds; 368 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { 369 if (!is_contained(Latches, *PI)) 370 Preds.push_back(*PI); 371 } 372 BB = llvm::SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, false); 373 } 374 375 Value *Exec = popSaved(); 376 if (!isa<UndefValue>(Exec)) 377 CallInst::Create(EndCf, Exec, "", &*BB->getFirstInsertionPt()); 378 } 379 380 /// \brief Annotate the control flow with intrinsics so the backend can 381 /// recognize if/then/else and loops. 382 bool SIAnnotateControlFlow::runOnFunction(Function &F) { 383 384 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 385 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 386 DA = &getAnalysis<DivergenceAnalysis>(); 387 388 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()), 389 E = df_end(&F.getEntryBlock()); I != E; ++I) { 390 391 BranchInst *Term = dyn_cast<BranchInst>((*I)->getTerminator()); 392 393 if (!Term || Term->isUnconditional()) { 394 if (isTopOfStack(*I)) 395 closeControlFlow(*I); 396 397 continue; 398 } 399 400 if (I.nodeVisited(Term->getSuccessor(1))) { 401 if (isTopOfStack(*I)) 402 closeControlFlow(*I); 403 404 handleLoop(Term); 405 continue; 406 } 407 408 if (isTopOfStack(*I)) { 409 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition()); 410 if (Phi && Phi->getParent() == *I && isElse(Phi)) { 411 insertElse(Term); 412 eraseIfUnused(Phi); 413 continue; 414 } 415 closeControlFlow(*I); 416 } 417 openIf(Term); 418 } 419 420 assert(Stack.empty()); 421 return true; 422 } 423 424 /// \brief Create the annotation pass 425 FunctionPass *llvm::createSIAnnotateControlFlowPass() { 426 return new SIAnnotateControlFlow(); 427 } 428