Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 1 | //===-- StructurizeCFG.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 | //===----------------------------------------------------------------------===// |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 9 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Scalar.h" |
Christian Konig | 90b4512 | 2013-03-26 10:24:20 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/MapVector.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SCCIterator.h" |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame^] | 13 | #include "llvm/ADT/PostOrderIterator.h" |
Tom Stellard | 1f0dded | 2014-12-03 04:28:32 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/LoopInfo.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/RegionInfo.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/RegionIterator.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/RegionPass.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Module.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 19 | #include "llvm/IR/PatternMatch.h" |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 24 | using namespace llvm::PatternMatch; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 25 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "structurizecfg" |
| 27 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | // Definition of the complex types used in this pass. |
| 31 | |
| 32 | typedef std::pair<BasicBlock *, Value *> BBValuePair; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 33 | |
| 34 | typedef SmallVector<RegionNode*, 8> RNVector; |
| 35 | typedef SmallVector<BasicBlock*, 8> BBVector; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 36 | typedef SmallVector<BranchInst*, 8> BranchVector; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 37 | typedef SmallVector<BBValuePair, 2> BBValueVector; |
| 38 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 39 | typedef SmallPtrSet<BasicBlock *, 8> BBSet; |
| 40 | |
Christian Konig | 90b4512 | 2013-03-26 10:24:20 +0000 | [diff] [blame] | 41 | typedef MapVector<PHINode *, BBValueVector> PhiMap; |
| 42 | typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap; |
| 43 | |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 44 | typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 45 | typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap; |
| 46 | typedef DenseMap<BasicBlock *, Value *> BBPredicates; |
| 47 | typedef DenseMap<BasicBlock *, BBPredicates> PredMap; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 48 | typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 49 | |
| 50 | // The name for newly created blocks. |
| 51 | |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 52 | static const char *const FlowBlockName = "Flow"; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 53 | |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 54 | /// @brief Find the nearest common dominator for multiple BasicBlocks |
| 55 | /// |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 56 | /// Helper class for StructurizeCFG |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 57 | /// TODO: Maybe move into common code |
| 58 | class NearestCommonDominator { |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 59 | DominatorTree *DT; |
| 60 | |
| 61 | DTN2UnsignedMap IndexMap; |
| 62 | |
| 63 | BasicBlock *Result; |
| 64 | unsigned ResultIndex; |
| 65 | bool ExplicitMentioned; |
| 66 | |
| 67 | public: |
| 68 | /// \brief Start a new query |
| 69 | NearestCommonDominator(DominatorTree *DomTree) { |
| 70 | DT = DomTree; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 71 | Result = nullptr; |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /// \brief Add BB to the resulting dominator |
| 75 | void addBlock(BasicBlock *BB, bool Remember = true) { |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 76 | DomTreeNode *Node = DT->getNode(BB); |
| 77 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 78 | if (!Result) { |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 79 | unsigned Numbering = 0; |
| 80 | for (;Node;Node = Node->getIDom()) |
| 81 | IndexMap[Node] = ++Numbering; |
| 82 | Result = BB; |
| 83 | ResultIndex = 1; |
| 84 | ExplicitMentioned = Remember; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | for (;Node;Node = Node->getIDom()) |
| 89 | if (IndexMap.count(Node)) |
| 90 | break; |
| 91 | else |
| 92 | IndexMap[Node] = 0; |
| 93 | |
| 94 | assert(Node && "Dominator tree invalid!"); |
| 95 | |
| 96 | unsigned Numbering = IndexMap[Node]; |
| 97 | if (Numbering > ResultIndex) { |
| 98 | Result = Node->getBlock(); |
| 99 | ResultIndex = Numbering; |
| 100 | ExplicitMentioned = Remember && (Result == BB); |
| 101 | } else if (Numbering == ResultIndex) { |
| 102 | ExplicitMentioned |= Remember; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// \brief Is "Result" one of the BBs added with "Remember" = True? |
| 107 | bool wasResultExplicitMentioned() { |
| 108 | return ExplicitMentioned; |
| 109 | } |
| 110 | |
| 111 | /// \brief Get the query result |
| 112 | BasicBlock *getResult() { |
| 113 | return Result; |
| 114 | } |
| 115 | }; |
| 116 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 117 | /// @brief Transforms the control flow graph on one single entry/exit region |
| 118 | /// at a time. |
| 119 | /// |
| 120 | /// After the transform all "If"/"Then"/"Else" style control flow looks like |
| 121 | /// this: |
| 122 | /// |
| 123 | /// \verbatim |
| 124 | /// 1 |
| 125 | /// || |
| 126 | /// | | |
| 127 | /// 2 | |
| 128 | /// | / |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 129 | /// |/ |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 130 | /// 3 |
| 131 | /// || Where: |
| 132 | /// | | 1 = "If" block, calculates the condition |
| 133 | /// 4 | 2 = "Then" subregion, runs if the condition is true |
| 134 | /// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow |
| 135 | /// |/ 4 = "Else" optional subregion, runs if the condition is false |
| 136 | /// 5 5 = "End" block, also rejoins the control flow |
| 137 | /// \endverbatim |
| 138 | /// |
| 139 | /// Control flow is expressed as a branch where the true exit goes into the |
| 140 | /// "Then"/"Else" region, while the false exit skips the region |
| 141 | /// The condition for the optional "Else" region is expressed as a PHI node. |
| 142 | /// The incomming values of the PHI node are true for the "If" edge and false |
| 143 | /// for the "Then" edge. |
| 144 | /// |
| 145 | /// Additionally to that even complicated loops look like this: |
| 146 | /// |
| 147 | /// \verbatim |
| 148 | /// 1 |
| 149 | /// || |
| 150 | /// | | |
| 151 | /// 2 ^ Where: |
| 152 | /// | / 1 = "Entry" block |
| 153 | /// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block |
| 154 | /// 3 3 = "Flow" block, with back edge to entry block |
| 155 | /// | |
| 156 | /// \endverbatim |
| 157 | /// |
| 158 | /// The back edge of the "Flow" block is always on the false side of the branch |
| 159 | /// while the true side continues the general flow. So the loop condition |
| 160 | /// consist of a network of PHI nodes where the true incoming values expresses |
| 161 | /// breaks and the false values expresses continue states. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 162 | class StructurizeCFG : public RegionPass { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 163 | Type *Boolean; |
| 164 | ConstantInt *BoolTrue; |
| 165 | ConstantInt *BoolFalse; |
| 166 | UndefValue *BoolUndef; |
| 167 | |
| 168 | Function *Func; |
| 169 | Region *ParentRegion; |
| 170 | |
| 171 | DominatorTree *DT; |
Tom Stellard | 1f0dded | 2014-12-03 04:28:32 +0000 | [diff] [blame] | 172 | LoopInfo *LI; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 173 | |
| 174 | RNVector Order; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 175 | BBSet Visited; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 176 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 177 | BBPhiMap DeletedPhis; |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 178 | BB2BBVecMap AddedPhis; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 179 | |
| 180 | PredMap Predicates; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 181 | BranchVector Conditions; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 182 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 183 | BB2BBMap Loops; |
| 184 | PredMap LoopPreds; |
| 185 | BranchVector LoopConds; |
| 186 | |
| 187 | RegionNode *PrevNode; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 188 | |
| 189 | void orderNodes(); |
| 190 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 191 | void analyzeLoops(RegionNode *N); |
| 192 | |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 193 | Value *invert(Value *Condition); |
| 194 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 195 | Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 196 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 197 | void gatherPredicates(RegionNode *N); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 198 | |
| 199 | void collectInfos(); |
| 200 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 201 | void insertConditions(bool Loops); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 202 | |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 203 | void delPhiValues(BasicBlock *From, BasicBlock *To); |
| 204 | |
| 205 | void addPhiValues(BasicBlock *From, BasicBlock *To); |
| 206 | |
| 207 | void setPhiValues(); |
| 208 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 209 | void killTerminator(BasicBlock *BB); |
| 210 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 211 | void changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 212 | bool IncludeDominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 213 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 214 | BasicBlock *getNextFlow(BasicBlock *Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 215 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 216 | BasicBlock *needPrefix(bool NeedEmpty); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 217 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 218 | BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed); |
| 219 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 220 | void setPrevNode(BasicBlock *BB); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 221 | |
| 222 | bool dominatesPredicates(BasicBlock *BB, RegionNode *Node); |
| 223 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 224 | bool isPredictableTrue(RegionNode *Node); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 225 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 226 | void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd); |
| 227 | |
| 228 | void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 229 | |
| 230 | void createFlow(); |
| 231 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 232 | void rebuildSSA(); |
| 233 | |
| 234 | public: |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 235 | static char ID; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 236 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 237 | StructurizeCFG() : |
| 238 | RegionPass(ID) { |
Tom Stellard | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 239 | initializeStructurizeCFGPass(*PassRegistry::getPassRegistry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Christian Konig | 01fd1f6 | 2013-03-01 09:46:11 +0000 | [diff] [blame] | 242 | using Pass::doInitialization; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 243 | bool doInitialization(Region *R, RGPassManager &RGM) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 244 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 245 | bool runOnRegion(Region *R, RGPassManager &RGM) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 246 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 247 | const char *getPassName() const override { |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 248 | return "Structurize control flow"; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 251 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 252 | AU.addRequiredID(LowerSwitchID); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 253 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 254 | AU.addRequired<LoopInfoWrapperPass>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 255 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 256 | RegionPass::getAnalysisUsage(AU); |
| 257 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | } // end anonymous namespace |
| 261 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 262 | char StructurizeCFG::ID = 0; |
| 263 | |
| 264 | INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG", |
| 265 | false, false) |
Tom Stellard | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 266 | INITIALIZE_PASS_DEPENDENCY(LowerSwitch) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 267 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 268 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass) |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 269 | INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG", |
| 270 | false, false) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 271 | |
| 272 | /// \brief Initialize the types and constants used in the pass |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 273 | bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 274 | LLVMContext &Context = R->getEntry()->getContext(); |
| 275 | |
| 276 | Boolean = Type::getInt1Ty(Context); |
| 277 | BoolTrue = ConstantInt::getTrue(Context); |
| 278 | BoolFalse = ConstantInt::getFalse(Context); |
| 279 | BoolUndef = UndefValue::get(Boolean); |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | /// \brief Build up the general order of nodes |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 285 | void StructurizeCFG::orderNodes() { |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame^] | 286 | RNVector TempOrder; |
| 287 | ReversePostOrderTraversal<Region*> RPOT(ParentRegion); |
| 288 | TempOrder.append(RPOT.begin(), RPOT.end()); |
| 289 | |
| 290 | std::map<Loop*, unsigned> LoopBlocks; |
| 291 | |
| 292 | |
| 293 | // The reverse post-order traversal of the list gives us an ordering close |
| 294 | // to what we want. The only problem with it is that sometimes backedges |
| 295 | // for outer loops will be visited before backedges for inner loops. |
| 296 | for (RegionNode *RN : TempOrder) { |
| 297 | BasicBlock *BB = RN->getEntry(); |
| 298 | Loop *Loop = LI->getLoopFor(BB); |
| 299 | if (!LoopBlocks.count(Loop)) { |
| 300 | LoopBlocks[Loop] = 1; |
| 301 | continue; |
| 302 | } |
| 303 | LoopBlocks[Loop]++; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 304 | } |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame^] | 305 | |
| 306 | unsigned CurrentLoopDepth = 0; |
| 307 | Loop *CurrentLoop = nullptr; |
| 308 | BBSet TempVisited; |
| 309 | for (RNVector::iterator I = TempOrder.begin(), E = TempOrder.end(); I != E; ++I) { |
| 310 | BasicBlock *BB = (*I)->getEntry(); |
| 311 | unsigned LoopDepth = LI->getLoopDepth(BB); |
| 312 | |
| 313 | if (std::find(Order.begin(), Order.end(), *I) != Order.end()) |
| 314 | continue; |
| 315 | |
| 316 | if (LoopDepth < CurrentLoopDepth) { |
| 317 | // Make sure we have visited all blocks in this loop before moving back to |
| 318 | // the outer loop. |
| 319 | |
| 320 | RNVector::iterator LoopI = I; |
| 321 | while(LoopBlocks[CurrentLoop]) { |
| 322 | LoopI++; |
| 323 | BasicBlock *LoopBB = (*LoopI)->getEntry(); |
| 324 | if (LI->getLoopFor(LoopBB) == CurrentLoop) { |
| 325 | LoopBlocks[CurrentLoop]--; |
| 326 | Order.push_back(*LoopI); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | CurrentLoop = LI->getLoopFor(BB); |
| 332 | if (CurrentLoop) { |
| 333 | LoopBlocks[CurrentLoop]--; |
| 334 | } |
| 335 | |
| 336 | CurrentLoopDepth = LoopDepth; |
| 337 | Order.push_back(*I); |
| 338 | } |
| 339 | |
| 340 | // This pass originally used a post-order traversal and then operated on |
| 341 | // the list in reverse. Now that we are using a reverse post-order traversal |
| 342 | // rather than re-working the whole pass to operate on the list in order, |
| 343 | // we just reverse the list and continue to operate on it in reverse. |
| 344 | std::reverse(Order.begin(), Order.end()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 347 | /// \brief Determine the end of the loops |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 348 | void StructurizeCFG::analyzeLoops(RegionNode *N) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 349 | if (N->isSubRegion()) { |
| 350 | // Test for exit as back edge |
| 351 | BasicBlock *Exit = N->getNodeAs<Region>()->getExit(); |
| 352 | if (Visited.count(Exit)) |
| 353 | Loops[Exit] = N->getEntry(); |
| 354 | |
| 355 | } else { |
| 356 | // Test for sucessors as back edge |
| 357 | BasicBlock *BB = N->getNodeAs<BasicBlock>(); |
| 358 | BranchInst *Term = cast<BranchInst>(BB->getTerminator()); |
| 359 | |
| 360 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 361 | BasicBlock *Succ = Term->getSuccessor(i); |
| 362 | |
Tom Stellard | 1f0dded | 2014-12-03 04:28:32 +0000 | [diff] [blame] | 363 | if (Visited.count(Succ) && LI->isLoopHeader(Succ) ) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 364 | Loops[Succ] = BB; |
Tom Stellard | 1f0dded | 2014-12-03 04:28:32 +0000 | [diff] [blame] | 365 | } |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 370 | /// \brief Invert the given condition |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 371 | Value *StructurizeCFG::invert(Value *Condition) { |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 372 | // First: Check if it's a constant |
| 373 | if (Condition == BoolTrue) |
| 374 | return BoolFalse; |
| 375 | |
| 376 | if (Condition == BoolFalse) |
| 377 | return BoolTrue; |
| 378 | |
| 379 | if (Condition == BoolUndef) |
| 380 | return BoolUndef; |
| 381 | |
| 382 | // Second: If the condition is already inverted, return the original value |
| 383 | if (match(Condition, m_Not(m_Value(Condition)))) |
| 384 | return Condition; |
| 385 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 386 | if (Instruction *Inst = dyn_cast<Instruction>(Condition)) { |
| 387 | // Third: Check all the users for an invert |
| 388 | BasicBlock *Parent = Inst->getParent(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 389 | for (User *U : Condition->users()) |
| 390 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 391 | if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition)))) |
| 392 | return I; |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 393 | |
| 394 | // Last option: Create a new instruction |
| 395 | return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator()); |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 398 | if (Argument *Arg = dyn_cast<Argument>(Condition)) { |
| 399 | BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock(); |
| 400 | return BinaryOperator::CreateNot(Condition, |
| 401 | Arg->getName() + ".inv", |
| 402 | EntryBlock.getTerminator()); |
| 403 | } |
| 404 | |
| 405 | llvm_unreachable("Unhandled condition to invert"); |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 408 | /// \brief Build the condition for one edge |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 409 | Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx, |
| 410 | bool Invert) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 411 | Value *Cond = Invert ? BoolFalse : BoolTrue; |
| 412 | if (Term->isConditional()) { |
| 413 | Cond = Term->getCondition(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 414 | |
Aaron Ballman | 1997855 | 2013-06-04 01:03:03 +0000 | [diff] [blame] | 415 | if (Idx != (unsigned)Invert) |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 416 | Cond = invert(Cond); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 417 | } |
| 418 | return Cond; |
| 419 | } |
| 420 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 421 | /// \brief Analyze the predecessors of each block and build up predicates |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 422 | void StructurizeCFG::gatherPredicates(RegionNode *N) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 423 | RegionInfo *RI = ParentRegion->getRegionInfo(); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 424 | BasicBlock *BB = N->getEntry(); |
| 425 | BBPredicates &Pred = Predicates[BB]; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 426 | BBPredicates &LPred = LoopPreds[BB]; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 427 | |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 428 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 429 | PI != PE; ++PI) { |
| 430 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 431 | // Ignore it if it's a branch from outside into our region entry |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 432 | if (!ParentRegion->contains(*PI)) |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 433 | continue; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 434 | |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 435 | Region *R = RI->getRegionFor(*PI); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 436 | if (R == ParentRegion) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 437 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 438 | // It's a top level block in our region |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 439 | BranchInst *Term = cast<BranchInst>((*PI)->getTerminator()); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 440 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 441 | BasicBlock *Succ = Term->getSuccessor(i); |
| 442 | if (Succ != BB) |
| 443 | continue; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 444 | |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 445 | if (Visited.count(*PI)) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 446 | // Normal forward edge |
| 447 | if (Term->isConditional()) { |
| 448 | // Try to treat it like an ELSE block |
| 449 | BasicBlock *Other = Term->getSuccessor(!i); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 450 | if (Visited.count(Other) && !Loops.count(Other) && |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 451 | !Pred.count(Other) && !Pred.count(*PI)) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 452 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 453 | Pred[Other] = BoolFalse; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 454 | Pred[*PI] = BoolTrue; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 455 | continue; |
| 456 | } |
| 457 | } |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 458 | Pred[*PI] = buildCondition(Term, i, false); |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 459 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 460 | } else { |
| 461 | // Back edge |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 462 | LPred[*PI] = buildCondition(Term, i, true); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 463 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | } else { |
| 467 | |
| 468 | // It's an exit from a sub region |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 469 | while (R->getParent() != ParentRegion) |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 470 | R = R->getParent(); |
| 471 | |
| 472 | // Edge from inside a subregion to its entry, ignore it |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 473 | if (*R == *N) |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 474 | continue; |
| 475 | |
| 476 | BasicBlock *Entry = R->getEntry(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 477 | if (Visited.count(Entry)) |
| 478 | Pred[Entry] = BoolTrue; |
| 479 | else |
| 480 | LPred[Entry] = BoolFalse; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /// \brief Collect various loop and predicate infos |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 486 | void StructurizeCFG::collectInfos() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 487 | // Reset predicate |
| 488 | Predicates.clear(); |
| 489 | |
| 490 | // and loop infos |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 491 | Loops.clear(); |
| 492 | LoopPreds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 493 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 494 | // Reset the visited nodes |
| 495 | Visited.clear(); |
| 496 | |
| 497 | for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend(); |
| 498 | OI != OE; ++OI) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 499 | |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame^] | 500 | DEBUG(dbgs() << "Visiting: " << |
| 501 | ((*OI)->isSubRegion() ? "SubRegion with entry: " : "") << |
| 502 | (*OI)->getEntry()->getName() << " Loop Depth: " << LI->getLoopDepth((*OI)->getEntry()) << "\n"); |
| 503 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 504 | // Analyze all the conditions leading to a node |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 505 | gatherPredicates(*OI); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 506 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 507 | // Remember that we've seen this node |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 508 | Visited.insert((*OI)->getEntry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 509 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 510 | // Find the last back edges |
| 511 | analyzeLoops(*OI); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 512 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /// \brief Insert the missing branch conditions |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 516 | void StructurizeCFG::insertConditions(bool Loops) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 517 | BranchVector &Conds = Loops ? LoopConds : Conditions; |
| 518 | Value *Default = Loops ? BoolTrue : BoolFalse; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 519 | SSAUpdater PhiInserter; |
| 520 | |
Matt Arsenault | 04b67ce | 2014-05-19 17:52:48 +0000 | [diff] [blame] | 521 | for (BranchInst *Term : Conds) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 522 | assert(Term->isConditional()); |
| 523 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 524 | BasicBlock *Parent = Term->getParent(); |
| 525 | BasicBlock *SuccTrue = Term->getSuccessor(0); |
| 526 | BasicBlock *SuccFalse = Term->getSuccessor(1); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 527 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 528 | PhiInserter.Initialize(Boolean, ""); |
| 529 | PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 530 | PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 531 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 532 | BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue]; |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 533 | |
| 534 | NearestCommonDominator Dominator(DT); |
| 535 | Dominator.addBlock(Parent, false); |
| 536 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 537 | Value *ParentValue = nullptr; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 538 | for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end(); |
| 539 | PI != PE; ++PI) { |
| 540 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 541 | if (PI->first == Parent) { |
| 542 | ParentValue = PI->second; |
| 543 | break; |
| 544 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 545 | PhiInserter.AddAvailableValue(PI->first, PI->second); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 546 | Dominator.addBlock(PI->first); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 549 | if (ParentValue) { |
| 550 | Term->setCondition(ParentValue); |
| 551 | } else { |
| 552 | if (!Dominator.wasResultExplicitMentioned()) |
| 553 | PhiInserter.AddAvailableValue(Dominator.getResult(), Default); |
| 554 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 555 | Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent)); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 556 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 560 | /// \brief Remove all PHI values coming from "From" into "To" and remember |
| 561 | /// them in DeletedPhis |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 562 | void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 563 | PhiMap &Map = DeletedPhis[To]; |
| 564 | for (BasicBlock::iterator I = To->begin(), E = To->end(); |
| 565 | I != E && isa<PHINode>(*I);) { |
| 566 | |
| 567 | PHINode &Phi = cast<PHINode>(*I++); |
| 568 | while (Phi.getBasicBlockIndex(From) != -1) { |
| 569 | Value *Deleted = Phi.removeIncomingValue(From, false); |
| 570 | Map[&Phi].push_back(std::make_pair(From, Deleted)); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /// \brief Add a dummy PHI value as soon as we knew the new predecessor |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 576 | void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 577 | for (BasicBlock::iterator I = To->begin(), E = To->end(); |
| 578 | I != E && isa<PHINode>(*I);) { |
| 579 | |
| 580 | PHINode &Phi = cast<PHINode>(*I++); |
| 581 | Value *Undef = UndefValue::get(Phi.getType()); |
| 582 | Phi.addIncoming(Undef, From); |
| 583 | } |
| 584 | AddedPhis[To].push_back(From); |
| 585 | } |
| 586 | |
| 587 | /// \brief Add the real PHI value as soon as everything is set up |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 588 | void StructurizeCFG::setPhiValues() { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 589 | SSAUpdater Updater; |
| 590 | for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end(); |
| 591 | AI != AE; ++AI) { |
| 592 | |
| 593 | BasicBlock *To = AI->first; |
| 594 | BBVector &From = AI->second; |
| 595 | |
| 596 | if (!DeletedPhis.count(To)) |
| 597 | continue; |
| 598 | |
| 599 | PhiMap &Map = DeletedPhis[To]; |
| 600 | for (PhiMap::iterator PI = Map.begin(), PE = Map.end(); |
| 601 | PI != PE; ++PI) { |
| 602 | |
| 603 | PHINode *Phi = PI->first; |
| 604 | Value *Undef = UndefValue::get(Phi->getType()); |
| 605 | Updater.Initialize(Phi->getType(), ""); |
| 606 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
| 607 | Updater.AddAvailableValue(To, Undef); |
| 608 | |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 609 | NearestCommonDominator Dominator(DT); |
| 610 | Dominator.addBlock(To, false); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 611 | for (BBValueVector::iterator VI = PI->second.begin(), |
| 612 | VE = PI->second.end(); VI != VE; ++VI) { |
| 613 | |
| 614 | Updater.AddAvailableValue(VI->first, VI->second); |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 615 | Dominator.addBlock(VI->first); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 618 | if (!Dominator.wasResultExplicitMentioned()) |
| 619 | Updater.AddAvailableValue(Dominator.getResult(), Undef); |
| 620 | |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 621 | for (BBVector::iterator FI = From.begin(), FE = From.end(); |
| 622 | FI != FE; ++FI) { |
| 623 | |
| 624 | int Idx = Phi->getBasicBlockIndex(*FI); |
| 625 | assert(Idx != -1); |
| 626 | Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI)); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | DeletedPhis.erase(To); |
| 631 | } |
| 632 | assert(DeletedPhis.empty()); |
| 633 | } |
| 634 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 635 | /// \brief Remove phi values from all successors and then remove the terminator. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 636 | void StructurizeCFG::killTerminator(BasicBlock *BB) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 637 | TerminatorInst *Term = BB->getTerminator(); |
| 638 | if (!Term) |
| 639 | return; |
| 640 | |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 641 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 642 | SI != SE; ++SI) { |
| 643 | |
| 644 | delPhiValues(BB, *SI); |
| 645 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 646 | |
| 647 | Term->eraseFromParent(); |
| 648 | } |
| 649 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 650 | /// \brief Let node exit(s) point to NewExit |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 651 | void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 652 | bool IncludeDominator) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 653 | if (Node->isSubRegion()) { |
| 654 | Region *SubRegion = Node->getNodeAs<Region>(); |
| 655 | BasicBlock *OldExit = SubRegion->getExit(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 656 | BasicBlock *Dominator = nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 657 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 658 | // Find all the edges from the sub region to the exit |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 659 | for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit); |
| 660 | I != E;) { |
| 661 | |
| 662 | BasicBlock *BB = *I++; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 663 | if (!SubRegion->contains(BB)) |
| 664 | continue; |
| 665 | |
| 666 | // Modify the edges to point to the new exit |
| 667 | delPhiValues(BB, OldExit); |
| 668 | BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit); |
| 669 | addPhiValues(BB, NewExit); |
| 670 | |
| 671 | // Find the new dominator (if requested) |
| 672 | if (IncludeDominator) { |
| 673 | if (!Dominator) |
| 674 | Dominator = BB; |
| 675 | else |
| 676 | Dominator = DT->findNearestCommonDominator(Dominator, BB); |
| 677 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 680 | // Change the dominator (if requested) |
| 681 | if (Dominator) |
| 682 | DT->changeImmediateDominator(NewExit, Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 683 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 684 | // Update the region info |
| 685 | SubRegion->replaceExit(NewExit); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 686 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 687 | } else { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 688 | BasicBlock *BB = Node->getNodeAs<BasicBlock>(); |
| 689 | killTerminator(BB); |
| 690 | BranchInst::Create(NewExit, BB); |
| 691 | addPhiValues(BB, NewExit); |
| 692 | if (IncludeDominator) |
| 693 | DT->changeImmediateDominator(NewExit, BB); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 694 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 697 | /// \brief Create a new flow node and update dominator tree and region info |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 698 | BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 699 | LLVMContext &Context = Func->getContext(); |
| 700 | BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() : |
| 701 | Order.back()->getEntry(); |
| 702 | BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName, |
| 703 | Func, Insert); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 704 | DT->addNewBlock(Flow, Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 705 | ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 706 | return Flow; |
| 707 | } |
| 708 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 709 | /// \brief Create a new or reuse the previous node as flow node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 710 | BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 711 | BasicBlock *Entry = PrevNode->getEntry(); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 712 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 713 | if (!PrevNode->isSubRegion()) { |
| 714 | killTerminator(Entry); |
| 715 | if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end()) |
| 716 | return Entry; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 717 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 718 | } |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 719 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 720 | // create a new flow node |
| 721 | BasicBlock *Flow = getNextFlow(Entry); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 722 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 723 | // and wire it up |
| 724 | changeExit(PrevNode, Flow, true); |
| 725 | PrevNode = ParentRegion->getBBNode(Flow); |
| 726 | return Flow; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /// \brief Returns the region exit if possible, otherwise just a new flow node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 730 | BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow, |
| 731 | bool ExitUseAllowed) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 732 | if (Order.empty() && ExitUseAllowed) { |
| 733 | BasicBlock *Exit = ParentRegion->getExit(); |
| 734 | DT->changeImmediateDominator(Exit, Flow); |
| 735 | addPhiValues(Flow, Exit); |
| 736 | return Exit; |
| 737 | } |
| 738 | return getNextFlow(Flow); |
| 739 | } |
| 740 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 741 | /// \brief Set the previous node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 742 | void StructurizeCFG::setPrevNode(BasicBlock *BB) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 743 | PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB) |
| 744 | : nullptr; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /// \brief Does BB dominate all the predicates of Node ? |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 748 | bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 749 | BBPredicates &Preds = Predicates[Node->getEntry()]; |
| 750 | for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end(); |
| 751 | PI != PE; ++PI) { |
| 752 | |
| 753 | if (!DT->dominates(BB, PI->first)) |
| 754 | return false; |
| 755 | } |
| 756 | return true; |
| 757 | } |
| 758 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 759 | /// \brief Can we predict that this node will always be called? |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 760 | bool StructurizeCFG::isPredictableTrue(RegionNode *Node) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 761 | BBPredicates &Preds = Predicates[Node->getEntry()]; |
| 762 | bool Dominated = false; |
| 763 | |
| 764 | // Regionentry is always true |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 765 | if (!PrevNode) |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 766 | return true; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 767 | |
| 768 | for (BBPredicates::iterator I = Preds.begin(), E = Preds.end(); |
| 769 | I != E; ++I) { |
| 770 | |
| 771 | if (I->second != BoolTrue) |
| 772 | return false; |
| 773 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 774 | if (!Dominated && DT->dominates(I->first, PrevNode->getEntry())) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 775 | Dominated = true; |
| 776 | } |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 777 | |
| 778 | // TODO: The dominator check is too strict |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 779 | return Dominated; |
| 780 | } |
| 781 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 782 | /// Take one node from the order vector and wire it up |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 783 | void StructurizeCFG::wireFlow(bool ExitUseAllowed, |
| 784 | BasicBlock *LoopEnd) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 785 | RegionNode *Node = Order.pop_back_val(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 786 | Visited.insert(Node->getEntry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 787 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 788 | if (isPredictableTrue(Node)) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 789 | // Just a linear flow |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 790 | if (PrevNode) { |
| 791 | changeExit(PrevNode, Node->getEntry(), true); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 792 | } |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 793 | PrevNode = Node; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 794 | |
| 795 | } else { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 796 | // Insert extra prefix node (or reuse last one) |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 797 | BasicBlock *Flow = needPrefix(false); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 798 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 799 | // Insert extra postfix node (or use exit instead) |
| 800 | BasicBlock *Entry = Node->getEntry(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 801 | BasicBlock *Next = needPostfix(Flow, ExitUseAllowed); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 802 | |
| 803 | // let it point to entry and next block |
| 804 | Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow)); |
| 805 | addPhiValues(Flow, Entry); |
| 806 | DT->changeImmediateDominator(Entry, Flow); |
| 807 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 808 | PrevNode = Node; |
| 809 | while (!Order.empty() && !Visited.count(LoopEnd) && |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 810 | dominatesPredicates(Entry, Order.back())) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 811 | handleLoops(false, LoopEnd); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 814 | changeExit(PrevNode, Next, false); |
| 815 | setPrevNode(Next); |
| 816 | } |
| 817 | } |
| 818 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 819 | void StructurizeCFG::handleLoops(bool ExitUseAllowed, |
| 820 | BasicBlock *LoopEnd) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 821 | RegionNode *Node = Order.back(); |
| 822 | BasicBlock *LoopStart = Node->getEntry(); |
| 823 | |
| 824 | if (!Loops.count(LoopStart)) { |
| 825 | wireFlow(ExitUseAllowed, LoopEnd); |
| 826 | return; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 829 | if (!isPredictableTrue(Node)) |
| 830 | LoopStart = needPrefix(true); |
| 831 | |
| 832 | LoopEnd = Loops[Node->getEntry()]; |
| 833 | wireFlow(false, LoopEnd); |
| 834 | while (!Visited.count(LoopEnd)) { |
| 835 | handleLoops(false, LoopEnd); |
| 836 | } |
| 837 | |
Matt Arsenault | 6ea0aad | 2013-11-22 19:24:39 +0000 | [diff] [blame] | 838 | // If the start of the loop is the entry block, we can't branch to it so |
| 839 | // insert a new dummy entry block. |
| 840 | Function *LoopFunc = LoopStart->getParent(); |
| 841 | if (LoopStart == &LoopFunc->getEntryBlock()) { |
| 842 | LoopStart->setName("entry.orig"); |
| 843 | |
| 844 | BasicBlock *NewEntry = |
| 845 | BasicBlock::Create(LoopStart->getContext(), |
| 846 | "entry", |
| 847 | LoopFunc, |
| 848 | LoopStart); |
| 849 | BranchInst::Create(LoopStart, NewEntry); |
| 850 | } |
| 851 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 852 | // Create an extra loop end node |
| 853 | LoopEnd = needPrefix(false); |
| 854 | BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed); |
| 855 | LoopConds.push_back(BranchInst::Create(Next, LoopStart, |
| 856 | BoolUndef, LoopEnd)); |
| 857 | addPhiValues(LoopEnd, LoopStart); |
| 858 | setPrevNode(Next); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 861 | /// After this function control flow looks like it should be, but |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 862 | /// branches and PHI nodes only have undefined conditions. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 863 | void StructurizeCFG::createFlow() { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 864 | BasicBlock *Exit = ParentRegion->getExit(); |
| 865 | bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit); |
| 866 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 867 | DeletedPhis.clear(); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 868 | AddedPhis.clear(); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 869 | Conditions.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 870 | LoopConds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 871 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 872 | PrevNode = nullptr; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 873 | Visited.clear(); |
| 874 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 875 | while (!Order.empty()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 876 | handleLoops(EntryDominatesExit, nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 879 | if (PrevNode) |
| 880 | changeExit(PrevNode, Exit, EntryDominatesExit); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 881 | else |
| 882 | assert(EntryDominatesExit); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 885 | /// Handle a rare case where the disintegrated nodes instructions |
| 886 | /// no longer dominate all their uses. Not sure if this is really nessasary |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 887 | void StructurizeCFG::rebuildSSA() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 888 | SSAUpdater Updater; |
Tobias Grosser | 4abf9d3 | 2014-03-03 13:00:39 +0000 | [diff] [blame] | 889 | for (const auto &BB : ParentRegion->blocks()) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 890 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); |
| 891 | II != IE; ++II) { |
| 892 | |
| 893 | bool Initialized = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 894 | for (auto I = II->use_begin(), E = II->use_end(); I != E;) { |
| 895 | Use &U = *I++; |
| 896 | Instruction *User = cast<Instruction>(U.getUser()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 897 | if (User->getParent() == BB) { |
| 898 | continue; |
| 899 | |
| 900 | } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 901 | if (UserPN->getIncomingBlock(U) == BB) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 902 | continue; |
| 903 | } |
| 904 | |
| 905 | if (DT->dominates(II, User)) |
| 906 | continue; |
| 907 | |
| 908 | if (!Initialized) { |
| 909 | Value *Undef = UndefValue::get(II->getType()); |
| 910 | Updater.Initialize(II->getType(), ""); |
| 911 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
| 912 | Updater.AddAvailableValue(BB, II); |
| 913 | Initialized = true; |
| 914 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 915 | Updater.RewriteUseAfterInsertions(U); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /// \brief Run the transformation for each region found |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 921 | bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 922 | if (R->isTopLevelRegion()) |
| 923 | return false; |
| 924 | |
| 925 | Func = R->getEntry()->getParent(); |
| 926 | ParentRegion = R; |
| 927 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 928 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 929 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 930 | |
| 931 | orderNodes(); |
| 932 | collectInfos(); |
| 933 | createFlow(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 934 | insertConditions(false); |
| 935 | insertConditions(true); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 936 | setPhiValues(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 937 | rebuildSSA(); |
| 938 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 939 | // Cleanup |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 940 | Order.clear(); |
| 941 | Visited.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 942 | DeletedPhis.clear(); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 943 | AddedPhis.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 944 | Predicates.clear(); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 945 | Conditions.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 946 | Loops.clear(); |
| 947 | LoopPreds.clear(); |
| 948 | LoopConds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 949 | |
| 950 | return true; |
| 951 | } |
| 952 | |
| 953 | /// \brief Create the pass |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 954 | Pass *llvm::createStructurizeCFGPass() { |
| 955 | return new StructurizeCFG(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 956 | } |