Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 1 | //===-- StructurizeCFG.cpp ------------------------------------------------===// |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 9 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "structurizecfg" |
| 11 | #include "llvm/Transforms/Scalar.h" |
Christian Konig | 90b4512 | 2013-03-26 10:24:20 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/MapVector.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SCCIterator.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/RegionInfo.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/RegionIterator.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/RegionPass.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Module.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 18 | #include "llvm/IR/PatternMatch.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 22 | using namespace llvm::PatternMatch; |
Tom Stellard | f879435 | 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 | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 29 | |
| 30 | typedef SmallVector<RegionNode*, 8> RNVector; |
| 31 | typedef SmallVector<BasicBlock*, 8> BBVector; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 32 | typedef SmallVector<BranchInst*, 8> BranchVector; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 33 | typedef SmallVector<BBValuePair, 2> BBValueVector; |
| 34 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 35 | typedef SmallPtrSet<BasicBlock *, 8> BBSet; |
| 36 | |
Christian Konig | 90b4512 | 2013-03-26 10:24:20 +0000 | [diff] [blame] | 37 | typedef MapVector<PHINode *, BBValueVector> PhiMap; |
| 38 | typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap; |
| 39 | |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 40 | typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap; |
Tom Stellard | f879435 | 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 | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 44 | typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 45 | |
| 46 | // The name for newly created blocks. |
| 47 | |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 48 | static const char *const FlowBlockName = "Flow"; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 49 | |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 50 | /// @brief Find the nearest common dominator for multiple BasicBlocks |
| 51 | /// |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 52 | /// Helper class for StructurizeCFG |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 53 | /// TODO: Maybe move into common code |
| 54 | class NearestCommonDominator { |
Christian Konig | d08e3d7 | 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 | d08e3d7 | 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 | f879435 | 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 | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 125 | /// |/ |
Tom Stellard | f879435 | 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 | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 158 | class StructurizeCFG : public RegionPass { |
Tom Stellard | f879435 | 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 | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 170 | BBSet Visited; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 171 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 172 | BBPhiMap DeletedPhis; |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 173 | BB2BBVecMap AddedPhis; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 174 | |
| 175 | PredMap Predicates; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 176 | BranchVector Conditions; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 177 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 178 | BB2BBMap Loops; |
| 179 | PredMap LoopPreds; |
| 180 | BranchVector LoopConds; |
| 181 | |
| 182 | RegionNode *PrevNode; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 183 | |
| 184 | void orderNodes(); |
| 185 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 186 | void analyzeLoops(RegionNode *N); |
| 187 | |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 188 | Value *invert(Value *Condition); |
| 189 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 190 | Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 191 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 192 | void gatherPredicates(RegionNode *N); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 193 | |
| 194 | void collectInfos(); |
| 195 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 196 | void insertConditions(bool Loops); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 197 | |
Tom Stellard | 7ec0e4f | 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 | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 204 | void killTerminator(BasicBlock *BB); |
| 205 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 206 | void changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 207 | bool IncludeDominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 208 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 209 | BasicBlock *getNextFlow(BasicBlock *Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 210 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 211 | BasicBlock *needPrefix(bool NeedEmpty); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 212 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 213 | BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed); |
| 214 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 215 | void setPrevNode(BasicBlock *BB); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 216 | |
| 217 | bool dominatesPredicates(BasicBlock *BB, RegionNode *Node); |
| 218 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 219 | bool isPredictableTrue(RegionNode *Node); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 220 | |
Christian Konig | fc6a985 | 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 | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 224 | |
| 225 | void createFlow(); |
| 226 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 227 | void rebuildSSA(); |
| 228 | |
| 229 | public: |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 230 | static char ID; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 231 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 232 | StructurizeCFG() : |
| 233 | RegionPass(ID) { |
Tom Stellard | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 234 | initializeStructurizeCFGPass(*PassRegistry::getPassRegistry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Christian Konig | 01fd1f6 | 2013-03-01 09:46:11 +0000 | [diff] [blame] | 237 | using Pass::doInitialization; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame^] | 238 | bool doInitialization(Region *R, RGPassManager &RGM) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 239 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame^] | 240 | bool runOnRegion(Region *R, RGPassManager &RGM) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 241 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame^] | 242 | const char *getPassName() const override { |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 243 | return "Structurize control flow"; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame^] | 246 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 247 | AU.addRequiredID(LowerSwitchID); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 248 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 249 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 250 | RegionPass::getAnalysisUsage(AU); |
| 251 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | } // end anonymous namespace |
| 255 | |
Matt Arsenault | d46fce1 | 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 | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 260 | INITIALIZE_PASS_DEPENDENCY(LowerSwitch) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 261 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 262 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 263 | INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG", |
| 264 | false, false) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 265 | |
| 266 | /// \brief Initialize the types and constants used in the pass |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 267 | bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) { |
Tom Stellard | f879435 | 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 | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 279 | void StructurizeCFG::orderNodes() { |
Duncan P. N. Exon Smith | 8e661ef | 2014-02-04 19:19:07 +0000 | [diff] [blame] | 280 | scc_iterator<Region *> I = scc_begin(ParentRegion); |
| 281 | for (Order.clear(); !I.isAtEnd(); ++I) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 282 | std::vector<RegionNode *> &Nodes = *I; |
| 283 | Order.append(Nodes.begin(), Nodes.end()); |
| 284 | } |
| 285 | } |
| 286 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 287 | /// \brief Determine the end of the loops |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 288 | void StructurizeCFG::analyzeLoops(RegionNode *N) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 289 | if (N->isSubRegion()) { |
| 290 | // Test for exit as back edge |
| 291 | BasicBlock *Exit = N->getNodeAs<Region>()->getExit(); |
| 292 | if (Visited.count(Exit)) |
| 293 | Loops[Exit] = N->getEntry(); |
| 294 | |
| 295 | } else { |
| 296 | // Test for sucessors as back edge |
| 297 | BasicBlock *BB = N->getNodeAs<BasicBlock>(); |
| 298 | BranchInst *Term = cast<BranchInst>(BB->getTerminator()); |
| 299 | |
| 300 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 301 | BasicBlock *Succ = Term->getSuccessor(i); |
| 302 | |
| 303 | if (Visited.count(Succ)) |
| 304 | Loops[Succ] = BB; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 309 | /// \brief Invert the given condition |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 310 | Value *StructurizeCFG::invert(Value *Condition) { |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 311 | // First: Check if it's a constant |
| 312 | if (Condition == BoolTrue) |
| 313 | return BoolFalse; |
| 314 | |
| 315 | if (Condition == BoolFalse) |
| 316 | return BoolTrue; |
| 317 | |
| 318 | if (Condition == BoolUndef) |
| 319 | return BoolUndef; |
| 320 | |
| 321 | // Second: If the condition is already inverted, return the original value |
| 322 | if (match(Condition, m_Not(m_Value(Condition)))) |
| 323 | return Condition; |
| 324 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 325 | if (Instruction *Inst = dyn_cast<Instruction>(Condition)) { |
| 326 | // Third: Check all the users for an invert |
| 327 | BasicBlock *Parent = Inst->getParent(); |
| 328 | for (Value::use_iterator I = Condition->use_begin(), |
| 329 | E = Condition->use_end(); I != E; ++I) { |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 330 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 331 | Instruction *User = dyn_cast<Instruction>(*I); |
| 332 | if (!User || User->getParent() != Parent) |
| 333 | continue; |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 334 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 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()); |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 343 | if (Argument *Arg = dyn_cast<Argument>(Condition)) { |
| 344 | BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock(); |
| 345 | return BinaryOperator::CreateNot(Condition, |
| 346 | Arg->getName() + ".inv", |
| 347 | EntryBlock.getTerminator()); |
| 348 | } |
| 349 | |
| 350 | llvm_unreachable("Unhandled condition to invert"); |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 353 | /// \brief Build the condition for one edge |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 354 | Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx, |
| 355 | bool Invert) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 356 | Value *Cond = Invert ? BoolFalse : BoolTrue; |
| 357 | if (Term->isConditional()) { |
| 358 | Cond = Term->getCondition(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 359 | |
Aaron Ballman | 1997855 | 2013-06-04 01:03:03 +0000 | [diff] [blame] | 360 | if (Idx != (unsigned)Invert) |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 361 | Cond = invert(Cond); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 362 | } |
| 363 | return Cond; |
| 364 | } |
| 365 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 366 | /// \brief Analyze the predecessors of each block and build up predicates |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 367 | void StructurizeCFG::gatherPredicates(RegionNode *N) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 368 | RegionInfo *RI = ParentRegion->getRegionInfo(); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 369 | BasicBlock *BB = N->getEntry(); |
| 370 | BBPredicates &Pred = Predicates[BB]; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 371 | BBPredicates &LPred = LoopPreds[BB]; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 372 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 373 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 374 | PI != PE; ++PI) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 375 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 376 | // Ignore it if it's a branch from outside into our region entry |
| 377 | if (!ParentRegion->contains(*PI)) |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 378 | continue; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 379 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 380 | Region *R = RI->getRegionFor(*PI); |
| 381 | if (R == ParentRegion) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 382 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 383 | // It's a top level block in our region |
| 384 | BranchInst *Term = cast<BranchInst>((*PI)->getTerminator()); |
| 385 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 386 | BasicBlock *Succ = Term->getSuccessor(i); |
| 387 | if (Succ != BB) |
| 388 | continue; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 389 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 390 | if (Visited.count(*PI)) { |
| 391 | // Normal forward edge |
| 392 | if (Term->isConditional()) { |
| 393 | // Try to treat it like an ELSE block |
| 394 | BasicBlock *Other = Term->getSuccessor(!i); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 395 | if (Visited.count(Other) && !Loops.count(Other) && |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 396 | !Pred.count(Other) && !Pred.count(*PI)) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 397 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 398 | Pred[Other] = BoolFalse; |
| 399 | Pred[*PI] = BoolTrue; |
| 400 | continue; |
| 401 | } |
| 402 | } |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 403 | Pred[*PI] = buildCondition(Term, i, false); |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 404 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 405 | } else { |
| 406 | // Back edge |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 407 | LPred[*PI] = buildCondition(Term, i, true); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 408 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | } else { |
| 412 | |
| 413 | // It's an exit from a sub region |
| 414 | while(R->getParent() != ParentRegion) |
| 415 | R = R->getParent(); |
| 416 | |
| 417 | // Edge from inside a subregion to its entry, ignore it |
| 418 | if (R == N) |
| 419 | continue; |
| 420 | |
| 421 | BasicBlock *Entry = R->getEntry(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 422 | if (Visited.count(Entry)) |
| 423 | Pred[Entry] = BoolTrue; |
| 424 | else |
| 425 | LPred[Entry] = BoolFalse; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /// \brief Collect various loop and predicate infos |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 431 | void StructurizeCFG::collectInfos() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 432 | // Reset predicate |
| 433 | Predicates.clear(); |
| 434 | |
| 435 | // and loop infos |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 436 | Loops.clear(); |
| 437 | LoopPreds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 438 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 439 | // Reset the visited nodes |
| 440 | Visited.clear(); |
| 441 | |
| 442 | for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend(); |
| 443 | OI != OE; ++OI) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 444 | |
| 445 | // Analyze all the conditions leading to a node |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 446 | gatherPredicates(*OI); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 447 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 448 | // Remember that we've seen this node |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 449 | Visited.insert((*OI)->getEntry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 450 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 451 | // Find the last back edges |
| 452 | analyzeLoops(*OI); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 453 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /// \brief Insert the missing branch conditions |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 457 | void StructurizeCFG::insertConditions(bool Loops) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 458 | BranchVector &Conds = Loops ? LoopConds : Conditions; |
| 459 | Value *Default = Loops ? BoolTrue : BoolFalse; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 460 | SSAUpdater PhiInserter; |
| 461 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 462 | for (BranchVector::iterator I = Conds.begin(), |
| 463 | E = Conds.end(); I != E; ++I) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 464 | |
| 465 | BranchInst *Term = *I; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 466 | assert(Term->isConditional()); |
| 467 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 468 | BasicBlock *Parent = Term->getParent(); |
| 469 | BasicBlock *SuccTrue = Term->getSuccessor(0); |
| 470 | BasicBlock *SuccFalse = Term->getSuccessor(1); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 471 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 472 | PhiInserter.Initialize(Boolean, ""); |
| 473 | PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 474 | PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 475 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 476 | BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue]; |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 477 | |
| 478 | NearestCommonDominator Dominator(DT); |
| 479 | Dominator.addBlock(Parent, false); |
| 480 | |
| 481 | Value *ParentValue = 0; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 482 | for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end(); |
| 483 | PI != PE; ++PI) { |
| 484 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 485 | if (PI->first == Parent) { |
| 486 | ParentValue = PI->second; |
| 487 | break; |
| 488 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 489 | PhiInserter.AddAvailableValue(PI->first, PI->second); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 490 | Dominator.addBlock(PI->first); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 493 | if (ParentValue) { |
| 494 | Term->setCondition(ParentValue); |
| 495 | } else { |
| 496 | if (!Dominator.wasResultExplicitMentioned()) |
| 497 | PhiInserter.AddAvailableValue(Dominator.getResult(), Default); |
| 498 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 499 | Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent)); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 500 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 504 | /// \brief Remove all PHI values coming from "From" into "To" and remember |
| 505 | /// them in DeletedPhis |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 506 | void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 507 | PhiMap &Map = DeletedPhis[To]; |
| 508 | for (BasicBlock::iterator I = To->begin(), E = To->end(); |
| 509 | I != E && isa<PHINode>(*I);) { |
| 510 | |
| 511 | PHINode &Phi = cast<PHINode>(*I++); |
| 512 | while (Phi.getBasicBlockIndex(From) != -1) { |
| 513 | Value *Deleted = Phi.removeIncomingValue(From, false); |
| 514 | Map[&Phi].push_back(std::make_pair(From, Deleted)); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /// \brief Add a dummy PHI value as soon as we knew the new predecessor |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 520 | void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 521 | for (BasicBlock::iterator I = To->begin(), E = To->end(); |
| 522 | I != E && isa<PHINode>(*I);) { |
| 523 | |
| 524 | PHINode &Phi = cast<PHINode>(*I++); |
| 525 | Value *Undef = UndefValue::get(Phi.getType()); |
| 526 | Phi.addIncoming(Undef, From); |
| 527 | } |
| 528 | AddedPhis[To].push_back(From); |
| 529 | } |
| 530 | |
| 531 | /// \brief Add the real PHI value as soon as everything is set up |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 532 | void StructurizeCFG::setPhiValues() { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 533 | SSAUpdater Updater; |
| 534 | for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end(); |
| 535 | AI != AE; ++AI) { |
| 536 | |
| 537 | BasicBlock *To = AI->first; |
| 538 | BBVector &From = AI->second; |
| 539 | |
| 540 | if (!DeletedPhis.count(To)) |
| 541 | continue; |
| 542 | |
| 543 | PhiMap &Map = DeletedPhis[To]; |
| 544 | for (PhiMap::iterator PI = Map.begin(), PE = Map.end(); |
| 545 | PI != PE; ++PI) { |
| 546 | |
| 547 | PHINode *Phi = PI->first; |
| 548 | Value *Undef = UndefValue::get(Phi->getType()); |
| 549 | Updater.Initialize(Phi->getType(), ""); |
| 550 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
| 551 | Updater.AddAvailableValue(To, Undef); |
| 552 | |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 553 | NearestCommonDominator Dominator(DT); |
| 554 | Dominator.addBlock(To, false); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 555 | for (BBValueVector::iterator VI = PI->second.begin(), |
| 556 | VE = PI->second.end(); VI != VE; ++VI) { |
| 557 | |
| 558 | Updater.AddAvailableValue(VI->first, VI->second); |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 559 | Dominator.addBlock(VI->first); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 562 | if (!Dominator.wasResultExplicitMentioned()) |
| 563 | Updater.AddAvailableValue(Dominator.getResult(), Undef); |
| 564 | |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 565 | for (BBVector::iterator FI = From.begin(), FE = From.end(); |
| 566 | FI != FE; ++FI) { |
| 567 | |
| 568 | int Idx = Phi->getBasicBlockIndex(*FI); |
| 569 | assert(Idx != -1); |
| 570 | Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI)); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | DeletedPhis.erase(To); |
| 575 | } |
| 576 | assert(DeletedPhis.empty()); |
| 577 | } |
| 578 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 579 | /// \brief Remove phi values from all successors and then remove the terminator. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 580 | void StructurizeCFG::killTerminator(BasicBlock *BB) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 581 | TerminatorInst *Term = BB->getTerminator(); |
| 582 | if (!Term) |
| 583 | return; |
| 584 | |
| 585 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 586 | SI != SE; ++SI) { |
| 587 | |
| 588 | delPhiValues(BB, *SI); |
| 589 | } |
| 590 | |
| 591 | Term->eraseFromParent(); |
| 592 | } |
| 593 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 594 | /// \brief Let node exit(s) point to NewExit |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 595 | void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 596 | bool IncludeDominator) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 597 | if (Node->isSubRegion()) { |
| 598 | Region *SubRegion = Node->getNodeAs<Region>(); |
| 599 | BasicBlock *OldExit = SubRegion->getExit(); |
| 600 | BasicBlock *Dominator = 0; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 601 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 602 | // Find all the edges from the sub region to the exit |
| 603 | for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit); |
| 604 | I != E;) { |
| 605 | |
| 606 | BasicBlock *BB = *I++; |
| 607 | if (!SubRegion->contains(BB)) |
| 608 | continue; |
| 609 | |
| 610 | // Modify the edges to point to the new exit |
| 611 | delPhiValues(BB, OldExit); |
| 612 | BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit); |
| 613 | addPhiValues(BB, NewExit); |
| 614 | |
| 615 | // Find the new dominator (if requested) |
| 616 | if (IncludeDominator) { |
| 617 | if (!Dominator) |
| 618 | Dominator = BB; |
| 619 | else |
| 620 | Dominator = DT->findNearestCommonDominator(Dominator, BB); |
| 621 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 624 | // Change the dominator (if requested) |
| 625 | if (Dominator) |
| 626 | DT->changeImmediateDominator(NewExit, Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 627 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 628 | // Update the region info |
| 629 | SubRegion->replaceExit(NewExit); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 630 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 631 | } else { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 632 | BasicBlock *BB = Node->getNodeAs<BasicBlock>(); |
| 633 | killTerminator(BB); |
| 634 | BranchInst::Create(NewExit, BB); |
| 635 | addPhiValues(BB, NewExit); |
| 636 | if (IncludeDominator) |
| 637 | DT->changeImmediateDominator(NewExit, BB); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 638 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 641 | /// \brief Create a new flow node and update dominator tree and region info |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 642 | BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 643 | LLVMContext &Context = Func->getContext(); |
| 644 | BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() : |
| 645 | Order.back()->getEntry(); |
| 646 | BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName, |
| 647 | Func, Insert); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 648 | DT->addNewBlock(Flow, Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 649 | ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 650 | return Flow; |
| 651 | } |
| 652 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 653 | /// \brief Create a new or reuse the previous node as flow node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 654 | BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 655 | BasicBlock *Entry = PrevNode->getEntry(); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 656 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 657 | if (!PrevNode->isSubRegion()) { |
| 658 | killTerminator(Entry); |
| 659 | if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end()) |
| 660 | return Entry; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 661 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 662 | } |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 663 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 664 | // create a new flow node |
| 665 | BasicBlock *Flow = getNextFlow(Entry); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 666 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 667 | // and wire it up |
| 668 | changeExit(PrevNode, Flow, true); |
| 669 | PrevNode = ParentRegion->getBBNode(Flow); |
| 670 | return Flow; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /// \brief Returns the region exit if possible, otherwise just a new flow node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 674 | BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow, |
| 675 | bool ExitUseAllowed) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 676 | if (Order.empty() && ExitUseAllowed) { |
| 677 | BasicBlock *Exit = ParentRegion->getExit(); |
| 678 | DT->changeImmediateDominator(Exit, Flow); |
| 679 | addPhiValues(Flow, Exit); |
| 680 | return Exit; |
| 681 | } |
| 682 | return getNextFlow(Flow); |
| 683 | } |
| 684 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 685 | /// \brief Set the previous node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 686 | void StructurizeCFG::setPrevNode(BasicBlock *BB) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 687 | PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB) : 0; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /// \brief Does BB dominate all the predicates of Node ? |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 691 | bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 692 | BBPredicates &Preds = Predicates[Node->getEntry()]; |
| 693 | for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end(); |
| 694 | PI != PE; ++PI) { |
| 695 | |
| 696 | if (!DT->dominates(BB, PI->first)) |
| 697 | return false; |
| 698 | } |
| 699 | return true; |
| 700 | } |
| 701 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 702 | /// \brief Can we predict that this node will always be called? |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 703 | bool StructurizeCFG::isPredictableTrue(RegionNode *Node) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 704 | BBPredicates &Preds = Predicates[Node->getEntry()]; |
| 705 | bool Dominated = false; |
| 706 | |
| 707 | // Regionentry is always true |
| 708 | if (PrevNode == 0) |
| 709 | return true; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 710 | |
| 711 | for (BBPredicates::iterator I = Preds.begin(), E = Preds.end(); |
| 712 | I != E; ++I) { |
| 713 | |
| 714 | if (I->second != BoolTrue) |
| 715 | return false; |
| 716 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 717 | if (!Dominated && DT->dominates(I->first, PrevNode->getEntry())) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 718 | Dominated = true; |
| 719 | } |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 720 | |
| 721 | // TODO: The dominator check is too strict |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 722 | return Dominated; |
| 723 | } |
| 724 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 725 | /// Take one node from the order vector and wire it up |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 726 | void StructurizeCFG::wireFlow(bool ExitUseAllowed, |
| 727 | BasicBlock *LoopEnd) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 728 | RegionNode *Node = Order.pop_back_val(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 729 | Visited.insert(Node->getEntry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 730 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 731 | if (isPredictableTrue(Node)) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 732 | // Just a linear flow |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 733 | if (PrevNode) { |
| 734 | changeExit(PrevNode, Node->getEntry(), true); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 735 | } |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 736 | PrevNode = Node; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 737 | |
| 738 | } else { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 739 | // Insert extra prefix node (or reuse last one) |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 740 | BasicBlock *Flow = needPrefix(false); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 741 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 742 | // Insert extra postfix node (or use exit instead) |
| 743 | BasicBlock *Entry = Node->getEntry(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 744 | BasicBlock *Next = needPostfix(Flow, ExitUseAllowed); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 745 | |
| 746 | // let it point to entry and next block |
| 747 | Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow)); |
| 748 | addPhiValues(Flow, Entry); |
| 749 | DT->changeImmediateDominator(Entry, Flow); |
| 750 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 751 | PrevNode = Node; |
| 752 | while (!Order.empty() && !Visited.count(LoopEnd) && |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 753 | dominatesPredicates(Entry, Order.back())) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 754 | handleLoops(false, LoopEnd); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 757 | changeExit(PrevNode, Next, false); |
| 758 | setPrevNode(Next); |
| 759 | } |
| 760 | } |
| 761 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 762 | void StructurizeCFG::handleLoops(bool ExitUseAllowed, |
| 763 | BasicBlock *LoopEnd) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 764 | RegionNode *Node = Order.back(); |
| 765 | BasicBlock *LoopStart = Node->getEntry(); |
| 766 | |
| 767 | if (!Loops.count(LoopStart)) { |
| 768 | wireFlow(ExitUseAllowed, LoopEnd); |
| 769 | return; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 772 | if (!isPredictableTrue(Node)) |
| 773 | LoopStart = needPrefix(true); |
| 774 | |
| 775 | LoopEnd = Loops[Node->getEntry()]; |
| 776 | wireFlow(false, LoopEnd); |
| 777 | while (!Visited.count(LoopEnd)) { |
| 778 | handleLoops(false, LoopEnd); |
| 779 | } |
| 780 | |
Matt Arsenault | 6ea0aad | 2013-11-22 19:24:39 +0000 | [diff] [blame] | 781 | // If the start of the loop is the entry block, we can't branch to it so |
| 782 | // insert a new dummy entry block. |
| 783 | Function *LoopFunc = LoopStart->getParent(); |
| 784 | if (LoopStart == &LoopFunc->getEntryBlock()) { |
| 785 | LoopStart->setName("entry.orig"); |
| 786 | |
| 787 | BasicBlock *NewEntry = |
| 788 | BasicBlock::Create(LoopStart->getContext(), |
| 789 | "entry", |
| 790 | LoopFunc, |
| 791 | LoopStart); |
| 792 | BranchInst::Create(LoopStart, NewEntry); |
| 793 | } |
| 794 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 795 | // Create an extra loop end node |
| 796 | LoopEnd = needPrefix(false); |
| 797 | BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed); |
| 798 | LoopConds.push_back(BranchInst::Create(Next, LoopStart, |
| 799 | BoolUndef, LoopEnd)); |
| 800 | addPhiValues(LoopEnd, LoopStart); |
| 801 | setPrevNode(Next); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 804 | /// After this function control flow looks like it should be, but |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 805 | /// branches and PHI nodes only have undefined conditions. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 806 | void StructurizeCFG::createFlow() { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 807 | BasicBlock *Exit = ParentRegion->getExit(); |
| 808 | bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit); |
| 809 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 810 | DeletedPhis.clear(); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 811 | AddedPhis.clear(); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 812 | Conditions.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 813 | LoopConds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 814 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 815 | PrevNode = 0; |
| 816 | Visited.clear(); |
| 817 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 818 | while (!Order.empty()) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 819 | handleLoops(EntryDominatesExit, 0); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 822 | if (PrevNode) |
| 823 | changeExit(PrevNode, Exit, EntryDominatesExit); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 824 | else |
| 825 | assert(EntryDominatesExit); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 828 | /// Handle a rare case where the disintegrated nodes instructions |
| 829 | /// no longer dominate all their uses. Not sure if this is really nessasary |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 830 | void StructurizeCFG::rebuildSSA() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 831 | SSAUpdater Updater; |
Tobias Grosser | 4abf9d3 | 2014-03-03 13:00:39 +0000 | [diff] [blame] | 832 | for (const auto &BB : ParentRegion->blocks()) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 833 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); |
| 834 | II != IE; ++II) { |
| 835 | |
| 836 | bool Initialized = false; |
| 837 | for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) { |
| 838 | |
| 839 | Next = I->getNext(); |
| 840 | |
| 841 | Instruction *User = cast<Instruction>(I->getUser()); |
| 842 | if (User->getParent() == BB) { |
| 843 | continue; |
| 844 | |
| 845 | } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) { |
| 846 | if (UserPN->getIncomingBlock(*I) == BB) |
| 847 | continue; |
| 848 | } |
| 849 | |
| 850 | if (DT->dominates(II, User)) |
| 851 | continue; |
| 852 | |
| 853 | if (!Initialized) { |
| 854 | Value *Undef = UndefValue::get(II->getType()); |
| 855 | Updater.Initialize(II->getType(), ""); |
| 856 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
| 857 | Updater.AddAvailableValue(BB, II); |
| 858 | Initialized = true; |
| 859 | } |
| 860 | Updater.RewriteUseAfterInsertions(*I); |
| 861 | } |
| 862 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /// \brief Run the transformation for each region found |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 866 | bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 867 | if (R->isTopLevelRegion()) |
| 868 | return false; |
| 869 | |
| 870 | Func = R->getEntry()->getParent(); |
| 871 | ParentRegion = R; |
| 872 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 873 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 874 | |
| 875 | orderNodes(); |
| 876 | collectInfos(); |
| 877 | createFlow(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 878 | insertConditions(false); |
| 879 | insertConditions(true); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 880 | setPhiValues(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 881 | rebuildSSA(); |
| 882 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 883 | // Cleanup |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 884 | Order.clear(); |
| 885 | Visited.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 886 | DeletedPhis.clear(); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 887 | AddedPhis.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 888 | Predicates.clear(); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 889 | Conditions.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 890 | Loops.clear(); |
| 891 | LoopPreds.clear(); |
| 892 | LoopConds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 893 | |
| 894 | return true; |
| 895 | } |
| 896 | |
| 897 | /// \brief Create the pass |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 898 | Pass *llvm::createStructurizeCFGPass() { |
| 899 | return new StructurizeCFG(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 900 | } |