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