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