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