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