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