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