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