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