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