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