1327952Sdim//===- SIAnnotateControlFlow.cpp ------------------------------------------===// 2284677Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6284677Sdim// 7284677Sdim//===----------------------------------------------------------------------===// 8284677Sdim// 9284677Sdim/// \file 10284677Sdim/// Annotates the control flow with hardware specific intrinsics. 11284677Sdim// 12284677Sdim//===----------------------------------------------------------------------===// 13284677Sdim 14284677Sdim#include "AMDGPU.h" 15353358Sdim#include "AMDGPUSubtarget.h" 16284677Sdim#include "llvm/ADT/DepthFirstIterator.h" 17327952Sdim#include "llvm/ADT/STLExtras.h" 18327952Sdim#include "llvm/ADT/SmallVector.h" 19344779Sdim#include "llvm/Analysis/LegacyDivergenceAnalysis.h" 20284677Sdim#include "llvm/Analysis/LoopInfo.h" 21353358Sdim#include "llvm/CodeGen/TargetPassConfig.h" 22327952Sdim#include "llvm/IR/BasicBlock.h" 23327952Sdim#include "llvm/IR/CFG.h" 24327952Sdim#include "llvm/IR/Constant.h" 25284677Sdim#include "llvm/IR/Constants.h" 26327952Sdim#include "llvm/IR/DerivedTypes.h" 27284677Sdim#include "llvm/IR/Dominators.h" 28327952Sdim#include "llvm/IR/Function.h" 29327952Sdim#include "llvm/IR/Instruction.h" 30284677Sdim#include "llvm/IR/Instructions.h" 31327952Sdim#include "llvm/IR/Intrinsics.h" 32284677Sdim#include "llvm/IR/Module.h" 33327952Sdim#include "llvm/IR/Type.h" 34327952Sdim#include "llvm/IR/ValueHandle.h" 35360784Sdim#include "llvm/InitializePasses.h" 36284677Sdim#include "llvm/Pass.h" 37327952Sdim#include "llvm/Support/Casting.h" 38327952Sdim#include "llvm/Support/Debug.h" 39327952Sdim#include "llvm/Support/ErrorHandling.h" 40327952Sdim#include "llvm/Support/raw_ostream.h" 41284677Sdim#include "llvm/Transforms/Utils/BasicBlockUtils.h" 42353358Sdim#include "llvm/Transforms/Utils/Local.h" 43327952Sdim#include <cassert> 44327952Sdim#include <utility> 45284677Sdim 46284677Sdimusing namespace llvm; 47284677Sdim 48284677Sdim#define DEBUG_TYPE "si-annotate-control-flow" 49284677Sdim 50284677Sdimnamespace { 51284677Sdim 52284677Sdim// Complex types used in this pass 53327952Sdimusing StackEntry = std::pair<BasicBlock *, Value *>; 54327952Sdimusing StackVector = SmallVector<StackEntry, 16>; 55284677Sdim 56284677Sdimclass SIAnnotateControlFlow : public FunctionPass { 57344779Sdim LegacyDivergenceAnalysis *DA; 58284677Sdim 59284677Sdim Type *Boolean; 60284677Sdim Type *Void; 61353358Sdim Type *IntMask; 62284677Sdim Type *ReturnStruct; 63284677Sdim 64284677Sdim ConstantInt *BoolTrue; 65284677Sdim ConstantInt *BoolFalse; 66284677Sdim UndefValue *BoolUndef; 67353358Sdim Constant *IntMaskZero; 68284677Sdim 69321369Sdim Function *If; 70321369Sdim Function *Else; 71321369Sdim Function *IfBreak; 72321369Sdim Function *Loop; 73321369Sdim Function *EndCf; 74284677Sdim 75284677Sdim DominatorTree *DT; 76284677Sdim StackVector Stack; 77284677Sdim 78284677Sdim LoopInfo *LI; 79284677Sdim 80353358Sdim void initialize(Module &M, const GCNSubtarget &ST); 81353358Sdim 82309124Sdim bool isUniform(BranchInst *T); 83309124Sdim 84284677Sdim bool isTopOfStack(BasicBlock *BB); 85284677Sdim 86284677Sdim Value *popSaved(); 87284677Sdim 88284677Sdim void push(BasicBlock *BB, Value *Saved); 89284677Sdim 90284677Sdim bool isElse(PHINode *Phi); 91284677Sdim 92284677Sdim void eraseIfUnused(PHINode *Phi); 93284677Sdim 94284677Sdim void openIf(BranchInst *Term); 95284677Sdim 96284677Sdim void insertElse(BranchInst *Term); 97284677Sdim 98321369Sdim Value * 99321369Sdim handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L, 100344779Sdim BranchInst *Term); 101284677Sdim 102284677Sdim void handleLoop(BranchInst *Term); 103284677Sdim 104284677Sdim void closeControlFlow(BasicBlock *BB); 105284677Sdim 106284677Sdimpublic: 107309124Sdim static char ID; 108309124Sdim 109327952Sdim SIAnnotateControlFlow() : FunctionPass(ID) {} 110284677Sdim 111284677Sdim bool runOnFunction(Function &F) override; 112284677Sdim 113314564Sdim StringRef getPassName() const override { return "SI annotate control flow"; } 114284677Sdim 115284677Sdim void getAnalysisUsage(AnalysisUsage &AU) const override { 116284677Sdim AU.addRequired<LoopInfoWrapperPass>(); 117284677Sdim AU.addRequired<DominatorTreeWrapperPass>(); 118344779Sdim AU.addRequired<LegacyDivergenceAnalysis>(); 119284677Sdim AU.addPreserved<DominatorTreeWrapperPass>(); 120353358Sdim AU.addRequired<TargetPassConfig>(); 121284677Sdim FunctionPass::getAnalysisUsage(AU); 122284677Sdim } 123284677Sdim}; 124284677Sdim 125284677Sdim} // end anonymous namespace 126284677Sdim 127309124SdimINITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE, 128309124Sdim "Annotate SI Control Flow", false, false) 129321369SdimINITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 130344779SdimINITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis) 131353358SdimINITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 132309124SdimINITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE, 133309124Sdim "Annotate SI Control Flow", false, false) 134309124Sdim 135284677Sdimchar SIAnnotateControlFlow::ID = 0; 136284677Sdim 137341825Sdim/// Initialize all the types and constants used in the pass 138353358Sdimvoid SIAnnotateControlFlow::initialize(Module &M, const GCNSubtarget &ST) { 139284677Sdim LLVMContext &Context = M.getContext(); 140284677Sdim 141284677Sdim Void = Type::getVoidTy(Context); 142284677Sdim Boolean = Type::getInt1Ty(Context); 143353358Sdim IntMask = ST.isWave32() ? Type::getInt32Ty(Context) 144353358Sdim : Type::getInt64Ty(Context); 145353358Sdim ReturnStruct = StructType::get(Boolean, IntMask); 146284677Sdim 147284677Sdim BoolTrue = ConstantInt::getTrue(Context); 148284677Sdim BoolFalse = ConstantInt::getFalse(Context); 149284677Sdim BoolUndef = UndefValue::get(Boolean); 150353358Sdim IntMaskZero = ConstantInt::get(IntMask, 0); 151284677Sdim 152353358Sdim If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if, { IntMask }); 153353358Sdim Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else, 154353358Sdim { IntMask, IntMask }); 155353358Sdim IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break, 156353358Sdim { IntMask, IntMask }); 157353358Sdim Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop, { IntMask }); 158353358Sdim EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf, { IntMask }); 159284677Sdim} 160284677Sdim 161341825Sdim/// Is the branch condition uniform or did the StructurizeCFG pass 162309124Sdim/// consider it as such? 163309124Sdimbool SIAnnotateControlFlow::isUniform(BranchInst *T) { 164344779Sdim return DA->isUniform(T) || 165309124Sdim T->getMetadata("structurizecfg.uniform") != nullptr; 166309124Sdim} 167309124Sdim 168341825Sdim/// Is BB the last block saved on the stack ? 169284677Sdimbool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) { 170284677Sdim return !Stack.empty() && Stack.back().first == BB; 171284677Sdim} 172284677Sdim 173341825Sdim/// Pop the last saved value from the control flow stack 174284677SdimValue *SIAnnotateControlFlow::popSaved() { 175284677Sdim return Stack.pop_back_val().second; 176284677Sdim} 177284677Sdim 178341825Sdim/// Push a BB and saved value to the control flow stack 179284677Sdimvoid SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) { 180284677Sdim Stack.push_back(std::make_pair(BB, Saved)); 181284677Sdim} 182284677Sdim 183341825Sdim/// Can the condition represented by this PHI node treated like 184284677Sdim/// an "Else" block? 185284677Sdimbool SIAnnotateControlFlow::isElse(PHINode *Phi) { 186284677Sdim BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock(); 187284677Sdim for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { 188284677Sdim if (Phi->getIncomingBlock(i) == IDom) { 189284677Sdim 190284677Sdim if (Phi->getIncomingValue(i) != BoolTrue) 191284677Sdim return false; 192284677Sdim 193284677Sdim } else { 194284677Sdim if (Phi->getIncomingValue(i) != BoolFalse) 195284677Sdim return false; 196284677Sdim 197284677Sdim } 198284677Sdim } 199284677Sdim return true; 200284677Sdim} 201284677Sdim 202341825Sdim// Erase "Phi" if it is not used any more 203284677Sdimvoid SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) { 204327952Sdim if (RecursivelyDeleteDeadPHINode(Phi)) { 205341825Sdim LLVM_DEBUG(dbgs() << "Erased unused condition phi\n"); 206321369Sdim } 207284677Sdim} 208284677Sdim 209341825Sdim/// Open a new "If" block 210284677Sdimvoid SIAnnotateControlFlow::openIf(BranchInst *Term) { 211321369Sdim if (isUniform(Term)) 212309124Sdim return; 213321369Sdim 214284677Sdim Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term); 215284677Sdim Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term)); 216284677Sdim push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term)); 217284677Sdim} 218284677Sdim 219341825Sdim/// Close the last "If" block and open a new "Else" block 220284677Sdimvoid SIAnnotateControlFlow::insertElse(BranchInst *Term) { 221309124Sdim if (isUniform(Term)) { 222309124Sdim return; 223309124Sdim } 224284677Sdim Value *Ret = CallInst::Create(Else, popSaved(), "", Term); 225284677Sdim Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term)); 226284677Sdim push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term)); 227284677Sdim} 228284677Sdim 229341825Sdim/// Recursively handle the condition leading to a loop 230321369SdimValue *SIAnnotateControlFlow::handleLoopCondition( 231344779Sdim Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) { 232321369Sdim if (Instruction *Inst = dyn_cast<Instruction>(Cond)) { 233284677Sdim BasicBlock *Parent = Inst->getParent(); 234284677Sdim Instruction *Insert; 235284677Sdim if (L->contains(Inst)) { 236284677Sdim Insert = Parent->getTerminator(); 237284677Sdim } else { 238284677Sdim Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime(); 239284677Sdim } 240321369Sdim 241284677Sdim Value *Args[] = { Cond, Broken }; 242284677Sdim return CallInst::Create(IfBreak, Args, "", Insert); 243321369Sdim } 244284677Sdim 245321369Sdim // Insert IfBreak in the loop header TERM for constant COND other than true. 246321369Sdim if (isa<Constant>(Cond)) { 247321369Sdim Instruction *Insert = Cond == BoolTrue ? 248321369Sdim Term : L->getHeader()->getTerminator(); 249321369Sdim 250309124Sdim Value *Args[] = { Cond, Broken }; 251321369Sdim return CallInst::Create(IfBreak, Args, "", Insert); 252321369Sdim } 253309124Sdim 254321369Sdim llvm_unreachable("Unhandled loop condition!"); 255284677Sdim} 256284677Sdim 257341825Sdim/// Handle a back edge (loop) 258284677Sdimvoid SIAnnotateControlFlow::handleLoop(BranchInst *Term) { 259321369Sdim if (isUniform(Term)) 260309124Sdim return; 261309124Sdim 262284677Sdim BasicBlock *BB = Term->getParent(); 263284677Sdim llvm::Loop *L = LI->getLoopFor(BB); 264314564Sdim if (!L) 265314564Sdim return; 266321369Sdim 267284677Sdim BasicBlock *Target = Term->getSuccessor(1); 268353358Sdim PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken", &Target->front()); 269284677Sdim 270284677Sdim Value *Cond = Term->getCondition(); 271284677Sdim Term->setCondition(BoolTrue); 272344779Sdim Value *Arg = handleLoopCondition(Cond, Broken, L, Term); 273284677Sdim 274353358Sdim for (BasicBlock *Pred : predecessors(Target)) { 275353358Sdim Value *PHIValue = IntMaskZero; 276353358Sdim if (Pred == BB) // Remember the value of the previous iteration. 277353358Sdim PHIValue = Arg; 278353358Sdim // If the backedge from Pred to Target could be executed before the exit 279353358Sdim // of the loop at BB, it should not reset or change "Broken", which keeps 280353358Sdim // track of the number of threads exited the loop at BB. 281353358Sdim else if (L->contains(Pred) && DT->dominates(Pred, BB)) 282353358Sdim PHIValue = Broken; 283353358Sdim Broken->addIncoming(PHIValue, Pred); 284353358Sdim } 285284677Sdim 286321369Sdim Term->setCondition(CallInst::Create(Loop, Arg, "", Term)); 287321369Sdim 288284677Sdim push(Term->getSuccessor(0), Arg); 289321369Sdim} 290321369Sdim 291341825Sdim/// Close the last opened control flow 292284677Sdimvoid SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) { 293284677Sdim llvm::Loop *L = LI->getLoopFor(BB); 294284677Sdim 295309124Sdim assert(Stack.back().first == BB); 296309124Sdim 297284677Sdim if (L && L->getHeader() == BB) { 298284677Sdim // We can't insert an EndCF call into a loop header, because it will 299284677Sdim // get executed on every iteration of the loop, when it should be 300284677Sdim // executed only once before the loop. 301321369Sdim SmallVector <BasicBlock *, 8> Latches; 302284677Sdim L->getLoopLatches(Latches); 303284677Sdim 304321369Sdim SmallVector<BasicBlock *, 2> Preds; 305321369Sdim for (BasicBlock *Pred : predecessors(BB)) { 306321369Sdim if (!is_contained(Latches, Pred)) 307321369Sdim Preds.push_back(Pred); 308284677Sdim } 309321369Sdim 310344779Sdim BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr, 311344779Sdim false); 312284677Sdim } 313284677Sdim 314309124Sdim Value *Exec = popSaved(); 315321369Sdim Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt(); 316321369Sdim if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt)) 317321369Sdim CallInst::Create(EndCf, Exec, "", FirstInsertionPt); 318284677Sdim} 319284677Sdim 320341825Sdim/// Annotate the control flow with intrinsics so the backend can 321284677Sdim/// recognize if/then/else and loops. 322284677Sdimbool SIAnnotateControlFlow::runOnFunction(Function &F) { 323284677Sdim DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 324284677Sdim LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 325344779Sdim DA = &getAnalysis<LegacyDivergenceAnalysis>(); 326353358Sdim TargetPassConfig &TPC = getAnalysis<TargetPassConfig>(); 327353358Sdim const TargetMachine &TM = TPC.getTM<TargetMachine>(); 328284677Sdim 329353358Sdim initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F)); 330353358Sdim 331284677Sdim for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()), 332284677Sdim E = df_end(&F.getEntryBlock()); I != E; ++I) { 333321369Sdim BasicBlock *BB = *I; 334321369Sdim BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator()); 335284677Sdim 336284677Sdim if (!Term || Term->isUnconditional()) { 337321369Sdim if (isTopOfStack(BB)) 338321369Sdim closeControlFlow(BB); 339309124Sdim 340284677Sdim continue; 341284677Sdim } 342284677Sdim 343284677Sdim if (I.nodeVisited(Term->getSuccessor(1))) { 344321369Sdim if (isTopOfStack(BB)) 345321369Sdim closeControlFlow(BB); 346309124Sdim 347284677Sdim handleLoop(Term); 348284677Sdim continue; 349284677Sdim } 350284677Sdim 351321369Sdim if (isTopOfStack(BB)) { 352284677Sdim PHINode *Phi = dyn_cast<PHINode>(Term->getCondition()); 353321369Sdim if (Phi && Phi->getParent() == BB && isElse(Phi)) { 354284677Sdim insertElse(Term); 355284677Sdim eraseIfUnused(Phi); 356284677Sdim continue; 357284677Sdim } 358321369Sdim 359321369Sdim closeControlFlow(BB); 360284677Sdim } 361321369Sdim 362284677Sdim openIf(Term); 363284677Sdim } 364284677Sdim 365341825Sdim if (!Stack.empty()) { 366341825Sdim // CFG was probably not structured. 367341825Sdim report_fatal_error("failed to annotate CFG"); 368341825Sdim } 369341825Sdim 370284677Sdim return true; 371284677Sdim} 372284677Sdim 373341825Sdim/// Create the annotation pass 374284677SdimFunctionPass *llvm::createSIAnnotateControlFlowPass() { 375284677Sdim return new SIAnnotateControlFlow(); 376284677Sdim} 377