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