10b57cec5SDimitry Andric //===- SIAnnotateControlFlow.cpp ------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// Annotates the control flow with hardware specific intrinsics.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "AMDGPU.h"
15e8d8bef9SDimitry Andric #include "GCNSubtarget.h"
160b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
1706c3fb27SDimitry Andric #include "llvm/Analysis/UniformityAnalysis.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
190b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
200b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
210b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
225f757f3fSDimitry Andric #include "llvm/IR/IRBuilder.h"
23e8d8bef9SDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h"
24480093f4SDimitry Andric #include "llvm/InitializePasses.h"
25e8d8bef9SDimitry Andric #include "llvm/Target/TargetMachine.h"
260b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
270b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric using namespace llvm;
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric #define DEBUG_TYPE "si-annotate-control-flow"
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric namespace {
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric // Complex types used in this pass
360b57cec5SDimitry Andric using StackEntry = std::pair<BasicBlock *, Value *>;
370b57cec5SDimitry Andric using StackVector = SmallVector<StackEntry, 16>;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric class SIAnnotateControlFlow : public FunctionPass {
4006c3fb27SDimitry Andric UniformityInfo *UA;
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric Type *Boolean;
430b57cec5SDimitry Andric Type *Void;
440b57cec5SDimitry Andric Type *IntMask;
450b57cec5SDimitry Andric Type *ReturnStruct;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric ConstantInt *BoolTrue;
480b57cec5SDimitry Andric ConstantInt *BoolFalse;
490b57cec5SDimitry Andric UndefValue *BoolUndef;
500b57cec5SDimitry Andric Constant *IntMaskZero;
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric Function *If;
530b57cec5SDimitry Andric Function *Else;
540b57cec5SDimitry Andric Function *IfBreak;
550b57cec5SDimitry Andric Function *Loop;
560b57cec5SDimitry Andric Function *EndCf;
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric DominatorTree *DT;
590b57cec5SDimitry Andric StackVector Stack;
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric LoopInfo *LI;
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric void initialize(Module &M, const GCNSubtarget &ST);
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric bool isUniform(BranchInst *T);
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric bool isTopOfStack(BasicBlock *BB);
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric Value *popSaved();
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric void push(BasicBlock *BB, Value *Saved);
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric bool isElse(PHINode *Phi);
740b57cec5SDimitry Andric
75fe6060f1SDimitry Andric bool hasKill(const BasicBlock *BB);
76fe6060f1SDimitry Andric
7781ad6265SDimitry Andric bool eraseIfUnused(PHINode *Phi);
780b57cec5SDimitry Andric
7981ad6265SDimitry Andric bool openIf(BranchInst *Term);
800b57cec5SDimitry Andric
8181ad6265SDimitry Andric bool insertElse(BranchInst *Term);
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric Value *
840b57cec5SDimitry Andric handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
850b57cec5SDimitry Andric BranchInst *Term);
860b57cec5SDimitry Andric
8781ad6265SDimitry Andric bool handleLoop(BranchInst *Term);
880b57cec5SDimitry Andric
8981ad6265SDimitry Andric bool closeControlFlow(BasicBlock *BB);
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric public:
920b57cec5SDimitry Andric static char ID;
930b57cec5SDimitry Andric
SIAnnotateControlFlow()940b57cec5SDimitry Andric SIAnnotateControlFlow() : FunctionPass(ID) {}
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric bool runOnFunction(Function &F) override;
970b57cec5SDimitry Andric
getPassName() const980b57cec5SDimitry Andric StringRef getPassName() const override { return "SI annotate control flow"; }
990b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const1000b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
1010b57cec5SDimitry Andric AU.addRequired<LoopInfoWrapperPass>();
1020b57cec5SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>();
10306c3fb27SDimitry Andric AU.addRequired<UniformityInfoWrapperPass>();
104fe6060f1SDimitry Andric AU.addPreserved<LoopInfoWrapperPass>();
1050b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>();
1060b57cec5SDimitry Andric AU.addRequired<TargetPassConfig>();
1070b57cec5SDimitry Andric FunctionPass::getAnalysisUsage(AU);
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric };
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric } // end anonymous namespace
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
1140b57cec5SDimitry Andric "Annotate SI Control Flow", false, false)
1150b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
11606c3fb27SDimitry Andric INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass)
1170b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
1180b57cec5SDimitry Andric INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
1190b57cec5SDimitry Andric "Annotate SI Control Flow", false, false)
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric char SIAnnotateControlFlow::ID = 0;
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric /// Initialize all the types and constants used in the pass
initialize(Module & M,const GCNSubtarget & ST)1240b57cec5SDimitry Andric void SIAnnotateControlFlow::initialize(Module &M, const GCNSubtarget &ST) {
1250b57cec5SDimitry Andric LLVMContext &Context = M.getContext();
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric Void = Type::getVoidTy(Context);
1280b57cec5SDimitry Andric Boolean = Type::getInt1Ty(Context);
1290b57cec5SDimitry Andric IntMask = ST.isWave32() ? Type::getInt32Ty(Context)
1300b57cec5SDimitry Andric : Type::getInt64Ty(Context);
1310b57cec5SDimitry Andric ReturnStruct = StructType::get(Boolean, IntMask);
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric BoolTrue = ConstantInt::getTrue(Context);
1340b57cec5SDimitry Andric BoolFalse = ConstantInt::getFalse(Context);
135bdd1243dSDimitry Andric BoolUndef = PoisonValue::get(Boolean);
1360b57cec5SDimitry Andric IntMaskZero = ConstantInt::get(IntMask, 0);
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if, { IntMask });
1390b57cec5SDimitry Andric Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else,
1400b57cec5SDimitry Andric { IntMask, IntMask });
1410b57cec5SDimitry Andric IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break,
1425ffd83dbSDimitry Andric { IntMask });
1430b57cec5SDimitry Andric Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop, { IntMask });
1440b57cec5SDimitry Andric EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf, { IntMask });
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric /// Is the branch condition uniform or did the StructurizeCFG pass
1480b57cec5SDimitry Andric /// consider it as such?
isUniform(BranchInst * T)1490b57cec5SDimitry Andric bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
150*0fca6ea1SDimitry Andric return UA->isUniform(T) || T->hasMetadata("structurizecfg.uniform");
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric /// Is BB the last block saved on the stack ?
isTopOfStack(BasicBlock * BB)1540b57cec5SDimitry Andric bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
1550b57cec5SDimitry Andric return !Stack.empty() && Stack.back().first == BB;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric /// Pop the last saved value from the control flow stack
popSaved()1590b57cec5SDimitry Andric Value *SIAnnotateControlFlow::popSaved() {
1600b57cec5SDimitry Andric return Stack.pop_back_val().second;
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric /// Push a BB and saved value to the control flow stack
push(BasicBlock * BB,Value * Saved)1640b57cec5SDimitry Andric void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
165bdd1243dSDimitry Andric Stack.push_back(std::pair(BB, Saved));
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric /// Can the condition represented by this PHI node treated like
1690b57cec5SDimitry Andric /// an "Else" block?
isElse(PHINode * Phi)1700b57cec5SDimitry Andric bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
1710b57cec5SDimitry Andric BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
1720b57cec5SDimitry Andric for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
1730b57cec5SDimitry Andric if (Phi->getIncomingBlock(i) == IDom) {
1740b57cec5SDimitry Andric
1750b57cec5SDimitry Andric if (Phi->getIncomingValue(i) != BoolTrue)
1760b57cec5SDimitry Andric return false;
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric } else {
1790b57cec5SDimitry Andric if (Phi->getIncomingValue(i) != BoolFalse)
1800b57cec5SDimitry Andric return false;
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric return true;
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
hasKill(const BasicBlock * BB)187fe6060f1SDimitry Andric bool SIAnnotateControlFlow::hasKill(const BasicBlock *BB) {
188fe6060f1SDimitry Andric for (const Instruction &I : *BB) {
189fe6060f1SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(&I))
190fe6060f1SDimitry Andric if (CI->getIntrinsicID() == Intrinsic::amdgcn_kill)
191fe6060f1SDimitry Andric return true;
192fe6060f1SDimitry Andric }
193fe6060f1SDimitry Andric return false;
194fe6060f1SDimitry Andric }
195fe6060f1SDimitry Andric
19681ad6265SDimitry Andric // Erase "Phi" if it is not used any more. Return true if any change was made.
eraseIfUnused(PHINode * Phi)19781ad6265SDimitry Andric bool SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
19881ad6265SDimitry Andric bool Changed = RecursivelyDeleteDeadPHINode(Phi);
19981ad6265SDimitry Andric if (Changed)
2000b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Erased unused condition phi\n");
20181ad6265SDimitry Andric return Changed;
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric /// Open a new "If" block
openIf(BranchInst * Term)20581ad6265SDimitry Andric bool SIAnnotateControlFlow::openIf(BranchInst *Term) {
2060b57cec5SDimitry Andric if (isUniform(Term))
20781ad6265SDimitry Andric return false;
2080b57cec5SDimitry Andric
2095f757f3fSDimitry Andric IRBuilder<> IRB(Term);
2105f757f3fSDimitry Andric Value *IfCall = IRB.CreateCall(If, {Term->getCondition()});
2115f757f3fSDimitry Andric Value *Cond = IRB.CreateExtractValue(IfCall, {0});
2125f757f3fSDimitry Andric Value *Mask = IRB.CreateExtractValue(IfCall, {1});
2135f757f3fSDimitry Andric Term->setCondition(Cond);
2145f757f3fSDimitry Andric push(Term->getSuccessor(1), Mask);
21581ad6265SDimitry Andric return true;
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric /// Close the last "If" block and open a new "Else" block
insertElse(BranchInst * Term)21981ad6265SDimitry Andric bool SIAnnotateControlFlow::insertElse(BranchInst *Term) {
2200b57cec5SDimitry Andric if (isUniform(Term)) {
22181ad6265SDimitry Andric return false;
2220b57cec5SDimitry Andric }
2235f757f3fSDimitry Andric
2245f757f3fSDimitry Andric IRBuilder<> IRB(Term);
2255f757f3fSDimitry Andric Value *ElseCall = IRB.CreateCall(Else, {popSaved()});
2265f757f3fSDimitry Andric Value *Cond = IRB.CreateExtractValue(ElseCall, {0});
2275f757f3fSDimitry Andric Value *Mask = IRB.CreateExtractValue(ElseCall, {1});
2285f757f3fSDimitry Andric Term->setCondition(Cond);
2295f757f3fSDimitry Andric push(Term->getSuccessor(1), Mask);
23081ad6265SDimitry Andric return true;
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andric /// Recursively handle the condition leading to a loop
handleLoopCondition(Value * Cond,PHINode * Broken,llvm::Loop * L,BranchInst * Term)2340b57cec5SDimitry Andric Value *SIAnnotateControlFlow::handleLoopCondition(
2350b57cec5SDimitry Andric Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) {
2365f757f3fSDimitry Andric
2375f757f3fSDimitry Andric auto CreateBreak = [this, Cond, Broken](Instruction *I) -> CallInst * {
2385f757f3fSDimitry Andric return IRBuilder<>(I).CreateCall(IfBreak, {Cond, Broken});
2395f757f3fSDimitry Andric };
2405f757f3fSDimitry Andric
2410b57cec5SDimitry Andric if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
2420b57cec5SDimitry Andric BasicBlock *Parent = Inst->getParent();
2430b57cec5SDimitry Andric Instruction *Insert;
2440b57cec5SDimitry Andric if (L->contains(Inst)) {
2450b57cec5SDimitry Andric Insert = Parent->getTerminator();
2460b57cec5SDimitry Andric } else {
2470b57cec5SDimitry Andric Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric
2505f757f3fSDimitry Andric return CreateBreak(Insert);
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric // Insert IfBreak in the loop header TERM for constant COND other than true.
2540b57cec5SDimitry Andric if (isa<Constant>(Cond)) {
2550b57cec5SDimitry Andric Instruction *Insert = Cond == BoolTrue ?
2560b57cec5SDimitry Andric Term : L->getHeader()->getTerminator();
2570b57cec5SDimitry Andric
2585f757f3fSDimitry Andric return CreateBreak(Insert);
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric
26104eeddc0SDimitry Andric if (isa<Argument>(Cond)) {
26204eeddc0SDimitry Andric Instruction *Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
2635f757f3fSDimitry Andric return CreateBreak(Insert);
26404eeddc0SDimitry Andric }
26504eeddc0SDimitry Andric
2660b57cec5SDimitry Andric llvm_unreachable("Unhandled loop condition!");
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric /// Handle a back edge (loop)
handleLoop(BranchInst * Term)27081ad6265SDimitry Andric bool SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
2710b57cec5SDimitry Andric if (isUniform(Term))
27281ad6265SDimitry Andric return false;
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric BasicBlock *BB = Term->getParent();
2750b57cec5SDimitry Andric llvm::Loop *L = LI->getLoopFor(BB);
2760b57cec5SDimitry Andric if (!L)
27781ad6265SDimitry Andric return false;
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric BasicBlock *Target = Term->getSuccessor(1);
2805f757f3fSDimitry Andric PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken");
2815f757f3fSDimitry Andric Broken->insertBefore(Target->begin());
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric Value *Cond = Term->getCondition();
2840b57cec5SDimitry Andric Term->setCondition(BoolTrue);
2850b57cec5SDimitry Andric Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
2860b57cec5SDimitry Andric
2870b57cec5SDimitry Andric for (BasicBlock *Pred : predecessors(Target)) {
2880b57cec5SDimitry Andric Value *PHIValue = IntMaskZero;
2890b57cec5SDimitry Andric if (Pred == BB) // Remember the value of the previous iteration.
2900b57cec5SDimitry Andric PHIValue = Arg;
2910b57cec5SDimitry Andric // If the backedge from Pred to Target could be executed before the exit
2920b57cec5SDimitry Andric // of the loop at BB, it should not reset or change "Broken", which keeps
2930b57cec5SDimitry Andric // track of the number of threads exited the loop at BB.
2940b57cec5SDimitry Andric else if (L->contains(Pred) && DT->dominates(Pred, BB))
2950b57cec5SDimitry Andric PHIValue = Broken;
2960b57cec5SDimitry Andric Broken->addIncoming(PHIValue, Pred);
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric
2995f757f3fSDimitry Andric CallInst *LoopCall = IRBuilder<>(Term).CreateCall(Loop, {Arg});
3005f757f3fSDimitry Andric Term->setCondition(LoopCall);
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric push(Term->getSuccessor(0), Arg);
30381ad6265SDimitry Andric
30481ad6265SDimitry Andric return true;
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andric /// Close the last opened control flow
closeControlFlow(BasicBlock * BB)30881ad6265SDimitry Andric bool SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
3090b57cec5SDimitry Andric llvm::Loop *L = LI->getLoopFor(BB);
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric assert(Stack.back().first == BB);
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric if (L && L->getHeader() == BB) {
3140b57cec5SDimitry Andric // We can't insert an EndCF call into a loop header, because it will
3150b57cec5SDimitry Andric // get executed on every iteration of the loop, when it should be
3160b57cec5SDimitry Andric // executed only once before the loop.
3170b57cec5SDimitry Andric SmallVector <BasicBlock *, 8> Latches;
3180b57cec5SDimitry Andric L->getLoopLatches(Latches);
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric SmallVector<BasicBlock *, 2> Preds;
3210b57cec5SDimitry Andric for (BasicBlock *Pred : predecessors(BB)) {
3220b57cec5SDimitry Andric if (!is_contained(Latches, Pred))
3230b57cec5SDimitry Andric Preds.push_back(Pred);
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr,
3270b57cec5SDimitry Andric false);
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric
3300b57cec5SDimitry Andric Value *Exec = popSaved();
3317a6dacacSDimitry Andric BasicBlock::iterator FirstInsertionPt = BB->getFirstInsertionPt();
332e8d8bef9SDimitry Andric if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt)) {
333e8d8bef9SDimitry Andric Instruction *ExecDef = cast<Instruction>(Exec);
334e8d8bef9SDimitry Andric BasicBlock *DefBB = ExecDef->getParent();
335e8d8bef9SDimitry Andric if (!DT->dominates(DefBB, BB)) {
336e8d8bef9SDimitry Andric // Split edge to make Def dominate Use
3377a6dacacSDimitry Andric FirstInsertionPt = SplitEdge(DefBB, BB, DT, LI)->getFirstInsertionPt();
338e8d8bef9SDimitry Andric }
339*0fca6ea1SDimitry Andric IRBuilder<> IRB(FirstInsertionPt->getParent(), FirstInsertionPt);
340*0fca6ea1SDimitry Andric // TODO: StructurizeCFG 'Flow' blocks have debug locations from the
341*0fca6ea1SDimitry Andric // condition, for now just avoid copying these DebugLocs so that stepping
342*0fca6ea1SDimitry Andric // out of the then/else block in a debugger doesn't step to the condition.
343*0fca6ea1SDimitry Andric IRB.SetCurrentDebugLocation(DebugLoc());
344*0fca6ea1SDimitry Andric IRB.CreateCall(EndCf, {Exec});
3450b57cec5SDimitry Andric }
34681ad6265SDimitry Andric
34781ad6265SDimitry Andric return true;
348e8d8bef9SDimitry Andric }
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andric /// Annotate the control flow with intrinsics so the backend can
3510b57cec5SDimitry Andric /// recognize if/then/else and loops.
runOnFunction(Function & F)3520b57cec5SDimitry Andric bool SIAnnotateControlFlow::runOnFunction(Function &F) {
3530b57cec5SDimitry Andric DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
3540b57cec5SDimitry Andric LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
35506c3fb27SDimitry Andric UA = &getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
3560b57cec5SDimitry Andric TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
3570b57cec5SDimitry Andric const TargetMachine &TM = TPC.getTM<TargetMachine>();
3580b57cec5SDimitry Andric
35981ad6265SDimitry Andric bool Changed = false;
3600b57cec5SDimitry Andric initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F));
3610b57cec5SDimitry Andric for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
3620b57cec5SDimitry Andric E = df_end(&F.getEntryBlock()); I != E; ++I) {
3630b57cec5SDimitry Andric BasicBlock *BB = *I;
3640b57cec5SDimitry Andric BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric if (!Term || Term->isUnconditional()) {
3670b57cec5SDimitry Andric if (isTopOfStack(BB))
36881ad6265SDimitry Andric Changed |= closeControlFlow(BB);
3690b57cec5SDimitry Andric
3700b57cec5SDimitry Andric continue;
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric if (I.nodeVisited(Term->getSuccessor(1))) {
3740b57cec5SDimitry Andric if (isTopOfStack(BB))
37581ad6265SDimitry Andric Changed |= closeControlFlow(BB);
3760b57cec5SDimitry Andric
377e8d8bef9SDimitry Andric if (DT->dominates(Term->getSuccessor(1), BB))
37881ad6265SDimitry Andric Changed |= handleLoop(Term);
3790b57cec5SDimitry Andric continue;
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric if (isTopOfStack(BB)) {
3830b57cec5SDimitry Andric PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
384fe6060f1SDimitry Andric if (Phi && Phi->getParent() == BB && isElse(Phi) && !hasKill(BB)) {
38581ad6265SDimitry Andric Changed |= insertElse(Term);
38681ad6265SDimitry Andric Changed |= eraseIfUnused(Phi);
3870b57cec5SDimitry Andric continue;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric
39081ad6265SDimitry Andric Changed |= closeControlFlow(BB);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric
39381ad6265SDimitry Andric Changed |= openIf(Term);
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric if (!Stack.empty()) {
3970b57cec5SDimitry Andric // CFG was probably not structured.
3980b57cec5SDimitry Andric report_fatal_error("failed to annotate CFG");
3990b57cec5SDimitry Andric }
4000b57cec5SDimitry Andric
40181ad6265SDimitry Andric return Changed;
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric /// Create the annotation pass
createSIAnnotateControlFlowPass()4050b57cec5SDimitry Andric FunctionPass *llvm::createSIAnnotateControlFlowPass() {
4060b57cec5SDimitry Andric return new SIAnnotateControlFlow();
4070b57cec5SDimitry Andric }
408