Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 1 | //===-- PredicateInfo.cpp - PredicateInfo Builder--------------------===// |
| 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 | //===----------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PredicateInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/PredicateInfo.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/DepthFirstIterator.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/Analysis/AssumptionCache.h" |
| 21 | #include "llvm/Analysis/CFG.h" |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 22 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/Dominators.h" |
| 25 | #include "llvm/IR/GlobalVariable.h" |
| 26 | #include "llvm/IR/IRBuilder.h" |
| 27 | #include "llvm/IR/IntrinsicInst.h" |
| 28 | #include "llvm/IR/LLVMContext.h" |
| 29 | #include "llvm/IR/Metadata.h" |
| 30 | #include "llvm/IR/Module.h" |
| 31 | #include "llvm/IR/PatternMatch.h" |
| 32 | #include "llvm/Support/Debug.h" |
Daniel Berlin | a4b5c01 | 2017-02-19 04:29:01 +0000 | [diff] [blame] | 33 | #include "llvm/Support/DebugCounter.h" |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 34 | #include "llvm/Support/FormattedStream.h" |
| 35 | #include "llvm/Transforms/Scalar.h" |
Daniel Berlin | b7df17e | 2017-06-29 17:01:14 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/OrderedInstructions.h" |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 37 | #include <algorithm> |
| 38 | #define DEBUG_TYPE "predicateinfo" |
| 39 | using namespace llvm; |
| 40 | using namespace PatternMatch; |
| 41 | using namespace llvm::PredicateInfoClasses; |
| 42 | |
| 43 | INITIALIZE_PASS_BEGIN(PredicateInfoPrinterLegacyPass, "print-predicateinfo", |
| 44 | "PredicateInfo Printer", false, false) |
| 45 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 46 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 47 | INITIALIZE_PASS_END(PredicateInfoPrinterLegacyPass, "print-predicateinfo", |
| 48 | "PredicateInfo Printer", false, false) |
| 49 | static cl::opt<bool> VerifyPredicateInfo( |
| 50 | "verify-predicateinfo", cl::init(false), cl::Hidden, |
| 51 | cl::desc("Verify PredicateInfo in legacy printer pass.")); |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 52 | namespace { |
Daniel Berlin | a4b5c01 | 2017-02-19 04:29:01 +0000 | [diff] [blame] | 53 | DEBUG_COUNTER(RenameCounter, "predicateinfo-rename", |
Simon Pilgrim | 5798180 | 2017-02-19 12:32:44 +0000 | [diff] [blame] | 54 | "Controls which variables are renamed with predicateinfo") |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 55 | // Given a predicate info that is a type of branching terminator, get the |
| 56 | // branching block. |
| 57 | const BasicBlock *getBranchBlock(const PredicateBase *PB) { |
| 58 | assert(isa<PredicateWithEdge>(PB) && |
| 59 | "Only branches and switches should have PHIOnly defs that " |
| 60 | "require branch blocks."); |
| 61 | return cast<PredicateWithEdge>(PB)->From; |
| 62 | } |
| 63 | |
| 64 | // Given a predicate info that is a type of branching terminator, get the |
| 65 | // branching terminator. |
| 66 | static Instruction *getBranchTerminator(const PredicateBase *PB) { |
| 67 | assert(isa<PredicateWithEdge>(PB) && |
| 68 | "Not a predicate info type we know how to get a terminator from."); |
| 69 | return cast<PredicateWithEdge>(PB)->From->getTerminator(); |
| 70 | } |
| 71 | |
| 72 | // Given a predicate info that is a type of branching terminator, get the |
| 73 | // edge this predicate info represents |
| 74 | const std::pair<BasicBlock *, BasicBlock *> |
| 75 | getBlockEdge(const PredicateBase *PB) { |
| 76 | assert(isa<PredicateWithEdge>(PB) && |
| 77 | "Not a predicate info type we know how to get an edge from."); |
| 78 | const auto *PEdge = cast<PredicateWithEdge>(PB); |
| 79 | return std::make_pair(PEdge->From, PEdge->To); |
| 80 | } |
| 81 | } |
Daniel Berlin | a4b5c01 | 2017-02-19 04:29:01 +0000 | [diff] [blame] | 82 | |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 83 | namespace llvm { |
| 84 | namespace PredicateInfoClasses { |
| 85 | enum LocalNum { |
| 86 | // Operations that must appear first in the block. |
| 87 | LN_First, |
| 88 | // Operations that are somewhere in the middle of the block, and are sorted on |
| 89 | // demand. |
| 90 | LN_Middle, |
| 91 | // Operations that must appear last in a block, like successor phi node uses. |
| 92 | LN_Last |
| 93 | }; |
| 94 | |
| 95 | // Associate global and local DFS info with defs and uses, so we can sort them |
| 96 | // into a global domination ordering. |
| 97 | struct ValueDFS { |
| 98 | int DFSIn = 0; |
| 99 | int DFSOut = 0; |
| 100 | unsigned int LocalNum = LN_Middle; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 101 | // Only one of Def or Use will be set. |
| 102 | Value *Def = nullptr; |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 103 | Use *U = nullptr; |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 104 | // Neither PInfo nor EdgeOnly participate in the ordering |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 105 | PredicateBase *PInfo = nullptr; |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 106 | bool EdgeOnly = false; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Daniel Berlin | b7df17e | 2017-06-29 17:01:14 +0000 | [diff] [blame] | 109 | // Perform a strict weak ordering on instructions and arguments. |
| 110 | static bool valueComesBefore(OrderedInstructions &OI, const Value *A, |
| 111 | const Value *B) { |
| 112 | auto *ArgA = dyn_cast_or_null<Argument>(A); |
| 113 | auto *ArgB = dyn_cast_or_null<Argument>(B); |
| 114 | if (ArgA && !ArgB) |
| 115 | return true; |
| 116 | if (ArgB && !ArgA) |
| 117 | return false; |
| 118 | if (ArgA && ArgB) |
| 119 | return ArgA->getArgNo() < ArgB->getArgNo(); |
| 120 | return OI.dominates(cast<Instruction>(A), cast<Instruction>(B)); |
| 121 | } |
| 122 | |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 123 | // This compares ValueDFS structures, creating OrderedBasicBlocks where |
| 124 | // necessary to compare uses/defs in the same block. Doing so allows us to walk |
| 125 | // the minimum number of instructions necessary to compute our def/use ordering. |
| 126 | struct ValueDFS_Compare { |
Daniel Berlin | b7df17e | 2017-06-29 17:01:14 +0000 | [diff] [blame] | 127 | OrderedInstructions &OI; |
| 128 | ValueDFS_Compare(OrderedInstructions &OI) : OI(OI) {} |
| 129 | |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 130 | bool operator()(const ValueDFS &A, const ValueDFS &B) const { |
| 131 | if (&A == &B) |
| 132 | return false; |
| 133 | // The only case we can't directly compare them is when they in the same |
| 134 | // block, and both have localnum == middle. In that case, we have to use |
| 135 | // comesbefore to see what the real ordering is, because they are in the |
| 136 | // same basic block. |
| 137 | |
| 138 | bool SameBlock = std::tie(A.DFSIn, A.DFSOut) == std::tie(B.DFSIn, B.DFSOut); |
| 139 | |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 140 | // We want to put the def that will get used for a given set of phi uses, |
| 141 | // before those phi uses. |
| 142 | // So we sort by edge, then by def. |
| 143 | // Note that only phi nodes uses and defs can come last. |
| 144 | if (SameBlock && A.LocalNum == LN_Last && B.LocalNum == LN_Last) |
| 145 | return comparePHIRelated(A, B); |
| 146 | |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 147 | if (!SameBlock || A.LocalNum != LN_Middle || B.LocalNum != LN_Middle) |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 148 | return std::tie(A.DFSIn, A.DFSOut, A.LocalNum, A.Def, A.U) < |
| 149 | std::tie(B.DFSIn, B.DFSOut, B.LocalNum, B.Def, B.U); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 150 | return localComesBefore(A, B); |
| 151 | } |
| 152 | |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 153 | // For a phi use, or a non-materialized def, return the edge it represents. |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 154 | const std::pair<BasicBlock *, BasicBlock *> |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 155 | getBlockEdge(const ValueDFS &VD) const { |
| 156 | if (!VD.Def && VD.U) { |
| 157 | auto *PHI = cast<PHINode>(VD.U->getUser()); |
| 158 | return std::make_pair(PHI->getIncomingBlock(*VD.U), PHI->getParent()); |
| 159 | } |
| 160 | // This is really a non-materialized def. |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 161 | return ::getBlockEdge(VD.PInfo); |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // For two phi related values, return the ordering. |
| 165 | bool comparePHIRelated(const ValueDFS &A, const ValueDFS &B) const { |
| 166 | auto &ABlockEdge = getBlockEdge(A); |
| 167 | auto &BBlockEdge = getBlockEdge(B); |
| 168 | // Now sort by block edge and then defs before uses. |
| 169 | return std::tie(ABlockEdge, A.Def, A.U) < std::tie(BBlockEdge, B.Def, B.U); |
| 170 | } |
| 171 | |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 172 | // Get the definition of an instruction that occurs in the middle of a block. |
| 173 | Value *getMiddleDef(const ValueDFS &VD) const { |
| 174 | if (VD.Def) |
| 175 | return VD.Def; |
| 176 | // It's possible for the defs and uses to be null. For branches, the local |
| 177 | // numbering will say the placed predicaeinfos should go first (IE |
| 178 | // LN_beginning), so we won't be in this function. For assumes, we will end |
| 179 | // up here, beause we need to order the def we will place relative to the |
| 180 | // assume. So for the purpose of ordering, we pretend the def is the assume |
| 181 | // because that is where we will insert the info. |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 182 | if (!VD.U) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 183 | assert(VD.PInfo && |
| 184 | "No def, no use, and no predicateinfo should not occur"); |
| 185 | assert(isa<PredicateAssume>(VD.PInfo) && |
| 186 | "Middle of block should only occur for assumes"); |
| 187 | return cast<PredicateAssume>(VD.PInfo)->AssumeInst; |
| 188 | } |
| 189 | return nullptr; |
| 190 | } |
| 191 | |
| 192 | // Return either the Def, if it's not null, or the user of the Use, if the def |
| 193 | // is null. |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 194 | const Instruction *getDefOrUser(const Value *Def, const Use *U) const { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 195 | if (Def) |
| 196 | return cast<Instruction>(Def); |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 197 | return cast<Instruction>(U->getUser()); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // This performs the necessary local basic block ordering checks to tell |
| 201 | // whether A comes before B, where both are in the same basic block. |
| 202 | bool localComesBefore(const ValueDFS &A, const ValueDFS &B) const { |
| 203 | auto *ADef = getMiddleDef(A); |
| 204 | auto *BDef = getMiddleDef(B); |
| 205 | |
| 206 | // See if we have real values or uses. If we have real values, we are |
| 207 | // guaranteed they are instructions or arguments. No matter what, we are |
| 208 | // guaranteed they are in the same block if they are instructions. |
| 209 | auto *ArgA = dyn_cast_or_null<Argument>(ADef); |
| 210 | auto *ArgB = dyn_cast_or_null<Argument>(BDef); |
| 211 | |
Daniel Berlin | b7df17e | 2017-06-29 17:01:14 +0000 | [diff] [blame] | 212 | if (ArgA || ArgB) |
| 213 | return valueComesBefore(OI, ArgA, ArgB); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 214 | |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 215 | auto *AInst = getDefOrUser(ADef, A.U); |
| 216 | auto *BInst = getDefOrUser(BDef, B.U); |
Daniel Berlin | b7df17e | 2017-06-29 17:01:14 +0000 | [diff] [blame] | 217 | return valueComesBefore(OI, AInst, BInst); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 218 | } |
| 219 | }; |
| 220 | |
| 221 | } // namespace PredicateInfoClasses |
| 222 | |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 223 | bool PredicateInfo::stackIsInScope(const ValueDFSStack &Stack, |
| 224 | const ValueDFS &VDUse) const { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 225 | if (Stack.empty()) |
| 226 | return false; |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 227 | // If it's a phi only use, make sure it's for this phi node edge, and that the |
| 228 | // use is in a phi node. If it's anything else, and the top of the stack is |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 229 | // EdgeOnly, we need to pop the stack. We deliberately sort phi uses next to |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 230 | // the defs they must go with so that we can know it's time to pop the stack |
| 231 | // when we hit the end of the phi uses for a given def. |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 232 | if (Stack.back().EdgeOnly) { |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 233 | if (!VDUse.U) |
| 234 | return false; |
| 235 | auto *PHI = dyn_cast<PHINode>(VDUse.U->getUser()); |
| 236 | if (!PHI) |
| 237 | return false; |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 238 | // Check edge |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 239 | BasicBlock *EdgePred = PHI->getIncomingBlock(*VDUse.U); |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 240 | if (EdgePred != getBranchBlock(Stack.back().PInfo)) |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 241 | return false; |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 242 | |
| 243 | // Use dominates, which knows how to handle edge dominance. |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 244 | return DT.dominates(getBlockEdge(Stack.back().PInfo), *VDUse.U); |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return (VDUse.DFSIn >= Stack.back().DFSIn && |
| 248 | VDUse.DFSOut <= Stack.back().DFSOut); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 251 | void PredicateInfo::popStackUntilDFSScope(ValueDFSStack &Stack, |
| 252 | const ValueDFS &VD) { |
| 253 | while (!Stack.empty() && !stackIsInScope(Stack, VD)) |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 254 | Stack.pop_back(); |
| 255 | } |
| 256 | |
| 257 | // Convert the uses of Op into a vector of uses, associating global and local |
| 258 | // DFS info with each one. |
| 259 | void PredicateInfo::convertUsesToDFSOrdered( |
| 260 | Value *Op, SmallVectorImpl<ValueDFS> &DFSOrderedSet) { |
| 261 | for (auto &U : Op->uses()) { |
| 262 | if (auto *I = dyn_cast<Instruction>(U.getUser())) { |
| 263 | ValueDFS VD; |
| 264 | // Put the phi node uses in the incoming block. |
| 265 | BasicBlock *IBlock; |
| 266 | if (auto *PN = dyn_cast<PHINode>(I)) { |
| 267 | IBlock = PN->getIncomingBlock(U); |
| 268 | // Make phi node users appear last in the incoming block |
| 269 | // they are from. |
| 270 | VD.LocalNum = LN_Last; |
| 271 | } else { |
| 272 | // If it's not a phi node use, it is somewhere in the middle of the |
| 273 | // block. |
| 274 | IBlock = I->getParent(); |
| 275 | VD.LocalNum = LN_Middle; |
| 276 | } |
| 277 | DomTreeNode *DomNode = DT.getNode(IBlock); |
| 278 | // It's possible our use is in an unreachable block. Skip it if so. |
| 279 | if (!DomNode) |
| 280 | continue; |
| 281 | VD.DFSIn = DomNode->getDFSNumIn(); |
| 282 | VD.DFSOut = DomNode->getDFSNumOut(); |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 283 | VD.U = &U; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 284 | DFSOrderedSet.push_back(VD); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Collect relevant operations from Comparison that we may want to insert copies |
| 290 | // for. |
| 291 | void collectCmpOps(CmpInst *Comparison, SmallVectorImpl<Value *> &CmpOperands) { |
| 292 | auto *Op0 = Comparison->getOperand(0); |
| 293 | auto *Op1 = Comparison->getOperand(1); |
| 294 | if (Op0 == Op1) |
| 295 | return; |
| 296 | CmpOperands.push_back(Comparison); |
| 297 | // Only want real values, not constants. Additionally, operands with one use |
| 298 | // are only being used in the comparison, which means they will not be useful |
| 299 | // for us to consider for predicateinfo. |
| 300 | // |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 301 | if ((isa<Instruction>(Op0) || isa<Argument>(Op0)) && !Op0->hasOneUse()) |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 302 | CmpOperands.push_back(Op0); |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 303 | if ((isa<Instruction>(Op1) || isa<Argument>(Op1)) && !Op1->hasOneUse()) |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 304 | CmpOperands.push_back(Op1); |
| 305 | } |
| 306 | |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 307 | // Add Op, PB to the list of value infos for Op, and mark Op to be renamed. |
| 308 | void PredicateInfo::addInfoFor(SmallPtrSetImpl<Value *> &OpsToRename, Value *Op, |
| 309 | PredicateBase *PB) { |
| 310 | OpsToRename.insert(Op); |
| 311 | auto &OperandInfo = getOrCreateValueInfo(Op); |
| 312 | AllInfos.push_back(PB); |
| 313 | OperandInfo.Infos.push_back(PB); |
| 314 | } |
| 315 | |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 316 | // Process an assume instruction and place relevant operations we want to rename |
| 317 | // into OpsToRename. |
| 318 | void PredicateInfo::processAssume(IntrinsicInst *II, BasicBlock *AssumeBB, |
| 319 | SmallPtrSetImpl<Value *> &OpsToRename) { |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 320 | // See if we have a comparison we support |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 321 | SmallVector<Value *, 8> CmpOperands; |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 322 | SmallVector<Value *, 2> ConditionsToProcess; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 323 | CmpInst::Predicate Pred; |
| 324 | Value *Operand = II->getOperand(0); |
| 325 | if (m_c_And(m_Cmp(Pred, m_Value(), m_Value()), |
| 326 | m_Cmp(Pred, m_Value(), m_Value())) |
| 327 | .match(II->getOperand(0))) { |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 328 | ConditionsToProcess.push_back(cast<BinaryOperator>(Operand)->getOperand(0)); |
| 329 | ConditionsToProcess.push_back(cast<BinaryOperator>(Operand)->getOperand(1)); |
| 330 | ConditionsToProcess.push_back(Operand); |
| 331 | } else if (isa<CmpInst>(Operand)) { |
| 332 | |
| 333 | ConditionsToProcess.push_back(Operand); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 334 | } |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 335 | for (auto Cond : ConditionsToProcess) { |
| 336 | if (auto *Cmp = dyn_cast<CmpInst>(Cond)) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 337 | collectCmpOps(Cmp, CmpOperands); |
| 338 | // Now add our copy infos for our operands |
| 339 | for (auto *Op : CmpOperands) { |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 340 | auto *PA = new PredicateAssume(Op, II, Cmp); |
| 341 | addInfoFor(OpsToRename, Op, PA); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 342 | } |
| 343 | CmpOperands.clear(); |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 344 | } else if (auto *BinOp = dyn_cast<BinaryOperator>(Cond)) { |
| 345 | // Otherwise, it should be an AND. |
| 346 | assert(BinOp->getOpcode() == Instruction::And && |
Simon Pilgrim | dba9011 | 2017-02-19 00:33:37 +0000 | [diff] [blame] | 347 | "Should have been an AND"); |
| 348 | auto *PA = new PredicateAssume(BinOp, II, BinOp); |
| 349 | addInfoFor(OpsToRename, BinOp, PA); |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 350 | } else { |
| 351 | llvm_unreachable("Unknown type of condition"); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Process a block terminating branch, and place relevant operations to be |
| 357 | // renamed into OpsToRename. |
| 358 | void PredicateInfo::processBranch(BranchInst *BI, BasicBlock *BranchBB, |
| 359 | SmallPtrSetImpl<Value *> &OpsToRename) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 360 | BasicBlock *FirstBB = BI->getSuccessor(0); |
| 361 | BasicBlock *SecondBB = BI->getSuccessor(1); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 362 | SmallVector<BasicBlock *, 2> SuccsToProcess; |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 363 | SuccsToProcess.push_back(FirstBB); |
| 364 | SuccsToProcess.push_back(SecondBB); |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 365 | SmallVector<Value *, 2> ConditionsToProcess; |
| 366 | |
| 367 | auto InsertHelper = [&](Value *Op, bool isAnd, bool isOr, Value *Cond) { |
| 368 | for (auto *Succ : SuccsToProcess) { |
| 369 | // Don't try to insert on a self-edge. This is mainly because we will |
| 370 | // eliminate during renaming anyway. |
| 371 | if (Succ == BranchBB) |
| 372 | continue; |
| 373 | bool TakenEdge = (Succ == FirstBB); |
| 374 | // For and, only insert on the true edge |
| 375 | // For or, only insert on the false edge |
| 376 | if ((isAnd && !TakenEdge) || (isOr && TakenEdge)) |
| 377 | continue; |
| 378 | PredicateBase *PB = |
| 379 | new PredicateBranch(Op, BranchBB, Succ, Cond, TakenEdge); |
| 380 | addInfoFor(OpsToRename, Op, PB); |
| 381 | if (!Succ->getSinglePredecessor()) |
| 382 | EdgeUsesOnly.insert({BranchBB, Succ}); |
| 383 | } |
| 384 | }; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 385 | |
| 386 | // Match combinations of conditions. |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 387 | CmpInst::Predicate Pred; |
| 388 | bool isAnd = false; |
| 389 | bool isOr = false; |
| 390 | SmallVector<Value *, 8> CmpOperands; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 391 | if (match(BI->getCondition(), m_And(m_Cmp(Pred, m_Value(), m_Value()), |
| 392 | m_Cmp(Pred, m_Value(), m_Value()))) || |
| 393 | match(BI->getCondition(), m_Or(m_Cmp(Pred, m_Value(), m_Value()), |
| 394 | m_Cmp(Pred, m_Value(), m_Value())))) { |
| 395 | auto *BinOp = cast<BinaryOperator>(BI->getCondition()); |
| 396 | if (BinOp->getOpcode() == Instruction::And) |
| 397 | isAnd = true; |
| 398 | else if (BinOp->getOpcode() == Instruction::Or) |
| 399 | isOr = true; |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 400 | ConditionsToProcess.push_back(BinOp->getOperand(0)); |
| 401 | ConditionsToProcess.push_back(BinOp->getOperand(1)); |
| 402 | ConditionsToProcess.push_back(BI->getCondition()); |
| 403 | } else if (isa<CmpInst>(BI->getCondition())) { |
| 404 | ConditionsToProcess.push_back(BI->getCondition()); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 405 | } |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 406 | for (auto Cond : ConditionsToProcess) { |
| 407 | if (auto *Cmp = dyn_cast<CmpInst>(Cond)) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 408 | collectCmpOps(Cmp, CmpOperands); |
| 409 | // Now add our copy infos for our operands |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 410 | for (auto *Op : CmpOperands) |
| 411 | InsertHelper(Op, isAnd, isOr, Cmp); |
| 412 | } else if (auto *BinOp = dyn_cast<BinaryOperator>(Cond)) { |
| 413 | // This must be an AND or an OR. |
| 414 | assert((BinOp->getOpcode() == Instruction::And || |
| 415 | BinOp->getOpcode() == Instruction::Or) && |
| 416 | "Should have been an AND or an OR"); |
| 417 | // The actual value of the binop is not subject to the same restrictions |
| 418 | // as the comparison. It's either true or false on the true/false branch. |
Simon Pilgrim | dba9011 | 2017-02-19 00:33:37 +0000 | [diff] [blame] | 419 | InsertHelper(BinOp, false, false, BinOp); |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 420 | } else { |
| 421 | llvm_unreachable("Unknown type of condition"); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 422 | } |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 423 | CmpOperands.clear(); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 426 | // Process a block terminating switch, and place relevant operations to be |
| 427 | // renamed into OpsToRename. |
| 428 | void PredicateInfo::processSwitch(SwitchInst *SI, BasicBlock *BranchBB, |
| 429 | SmallPtrSetImpl<Value *> &OpsToRename) { |
| 430 | Value *Op = SI->getCondition(); |
| 431 | if ((!isa<Instruction>(Op) && !isa<Argument>(Op)) || Op->hasOneUse()) |
| 432 | return; |
| 433 | |
| 434 | // Remember how many outgoing edges there are to every successor. |
| 435 | SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges; |
| 436 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 437 | BasicBlock *TargetBlock = SI->getSuccessor(i); |
| 438 | ++SwitchEdges[TargetBlock]; |
| 439 | } |
| 440 | |
| 441 | // Now propagate info for each case value |
| 442 | for (auto C : SI->cases()) { |
| 443 | BasicBlock *TargetBlock = C.getCaseSuccessor(); |
| 444 | if (SwitchEdges.lookup(TargetBlock) == 1) { |
| 445 | PredicateSwitch *PS = new PredicateSwitch( |
| 446 | Op, SI->getParent(), TargetBlock, C.getCaseValue(), SI); |
| 447 | addInfoFor(OpsToRename, Op, PS); |
| 448 | if (!TargetBlock->getSinglePredecessor()) |
| 449 | EdgeUsesOnly.insert({BranchBB, TargetBlock}); |
| 450 | } |
| 451 | } |
| 452 | } |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 453 | |
| 454 | // Build predicate info for our function |
| 455 | void PredicateInfo::buildPredicateInfo() { |
| 456 | DT.updateDFSNumbers(); |
| 457 | // Collect operands to rename from all conditional branch terminators, as well |
| 458 | // as assume statements. |
| 459 | SmallPtrSet<Value *, 8> OpsToRename; |
| 460 | for (auto DTN : depth_first(DT.getRootNode())) { |
| 461 | BasicBlock *BranchBB = DTN->getBlock(); |
| 462 | if (auto *BI = dyn_cast<BranchInst>(BranchBB->getTerminator())) { |
| 463 | if (!BI->isConditional()) |
| 464 | continue; |
Daniel Berlin | 6d2db9e | 2017-06-14 21:19:52 +0000 | [diff] [blame] | 465 | // Can't insert conditional information if they all go to the same place. |
| 466 | if (BI->getSuccessor(0) == BI->getSuccessor(1)) |
| 467 | continue; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 468 | processBranch(BI, BranchBB, OpsToRename); |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 469 | } else if (auto *SI = dyn_cast<SwitchInst>(BranchBB->getTerminator())) { |
| 470 | processSwitch(SI, BranchBB, OpsToRename); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | for (auto &Assume : AC.assumptions()) { |
| 474 | if (auto *II = dyn_cast_or_null<IntrinsicInst>(Assume)) |
| 475 | processAssume(II, II->getParent(), OpsToRename); |
| 476 | } |
| 477 | // Now rename all our operations. |
| 478 | renameUses(OpsToRename); |
| 479 | } |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 480 | |
| 481 | // Given the renaming stack, make all the operands currently on the stack real |
| 482 | // by inserting them into the IR. Return the last operation's value. |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 483 | Value *PredicateInfo::materializeStack(unsigned int &Counter, |
| 484 | ValueDFSStack &RenameStack, |
| 485 | Value *OrigOp) { |
| 486 | // Find the first thing we have to materialize |
| 487 | auto RevIter = RenameStack.rbegin(); |
| 488 | for (; RevIter != RenameStack.rend(); ++RevIter) |
| 489 | if (RevIter->Def) |
| 490 | break; |
| 491 | |
| 492 | size_t Start = RevIter - RenameStack.rbegin(); |
| 493 | // The maximum number of things we should be trying to materialize at once |
| 494 | // right now is 4, depending on if we had an assume, a branch, and both used |
| 495 | // and of conditions. |
| 496 | for (auto RenameIter = RenameStack.end() - Start; |
| 497 | RenameIter != RenameStack.end(); ++RenameIter) { |
| 498 | auto *Op = |
| 499 | RenameIter == RenameStack.begin() ? OrigOp : (RenameIter - 1)->Def; |
| 500 | ValueDFS &Result = *RenameIter; |
| 501 | auto *ValInfo = Result.PInfo; |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 502 | // For edge predicates, we can just place the operand in the block before |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 503 | // the terminator. For assume, we have to place it right before the assume |
| 504 | // to ensure we dominate all of our uses. Always insert right before the |
| 505 | // relevant instruction (terminator, assume), so that we insert in proper |
| 506 | // order in the case of multiple predicateinfo in the same block. |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 507 | if (isa<PredicateWithEdge>(ValInfo)) { |
| 508 | IRBuilder<> B(getBranchTerminator(ValInfo)); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 509 | Function *IF = Intrinsic::getDeclaration( |
| 510 | F.getParent(), Intrinsic::ssa_copy, Op->getType()); |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 511 | CallInst *PIC = |
| 512 | B.CreateCall(IF, Op, Op->getName() + "." + Twine(Counter++)); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 513 | PredicateMap.insert({PIC, ValInfo}); |
| 514 | Result.Def = PIC; |
| 515 | } else { |
| 516 | auto *PAssume = dyn_cast<PredicateAssume>(ValInfo); |
| 517 | assert(PAssume && |
| 518 | "Should not have gotten here without it being an assume"); |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 519 | IRBuilder<> B(PAssume->AssumeInst); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 520 | Function *IF = Intrinsic::getDeclaration( |
| 521 | F.getParent(), Intrinsic::ssa_copy, Op->getType()); |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 522 | CallInst *PIC = B.CreateCall(IF, Op); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 523 | PredicateMap.insert({PIC, ValInfo}); |
| 524 | Result.Def = PIC; |
| 525 | } |
| 526 | } |
| 527 | return RenameStack.back().Def; |
| 528 | } |
| 529 | |
| 530 | // Instead of the standard SSA renaming algorithm, which is O(Number of |
| 531 | // instructions), and walks the entire dominator tree, we walk only the defs + |
| 532 | // uses. The standard SSA renaming algorithm does not really rely on the |
| 533 | // dominator tree except to order the stack push/pops of the renaming stacks, so |
| 534 | // that defs end up getting pushed before hitting the correct uses. This does |
| 535 | // not require the dominator tree, only the *order* of the dominator tree. The |
| 536 | // complete and correct ordering of the defs and uses, in dominator tree is |
| 537 | // contained in the DFS numbering of the dominator tree. So we sort the defs and |
| 538 | // uses into the DFS ordering, and then just use the renaming stack as per |
| 539 | // normal, pushing when we hit a def (which is a predicateinfo instruction), |
| 540 | // popping when we are out of the dfs scope for that def, and replacing any uses |
| 541 | // with top of stack if it exists. In order to handle liveness without |
| 542 | // propagating liveness info, we don't actually insert the predicateinfo |
| 543 | // instruction def until we see a use that it would dominate. Once we see such |
| 544 | // a use, we materialize the predicateinfo instruction in the right place and |
| 545 | // use it. |
| 546 | // |
| 547 | // TODO: Use this algorithm to perform fast single-variable renaming in |
| 548 | // promotememtoreg and memoryssa. |
Mandeep Singh Grang | 33a1b73 | 2017-06-01 18:36:24 +0000 | [diff] [blame] | 549 | void PredicateInfo::renameUses(SmallPtrSetImpl<Value *> &OpSet) { |
| 550 | // Sort OpsToRename since we are going to iterate it. |
| 551 | SmallVector<Value *, 8> OpsToRename(OpSet.begin(), OpSet.end()); |
Daniel Berlin | b7df17e | 2017-06-29 17:01:14 +0000 | [diff] [blame] | 552 | auto Comparator = [&](const Value *A, const Value *B) { |
| 553 | return valueComesBefore(OI, A, B); |
| 554 | }; |
| 555 | std::sort(OpsToRename.begin(), OpsToRename.end(), Comparator); |
| 556 | ValueDFS_Compare Compare(OI); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 557 | // Compute liveness, and rename in O(uses) per Op. |
| 558 | for (auto *Op : OpsToRename) { |
| 559 | unsigned Counter = 0; |
| 560 | SmallVector<ValueDFS, 16> OrderedUses; |
| 561 | const auto &ValueInfo = getValueInfo(Op); |
| 562 | // Insert the possible copies into the def/use list. |
| 563 | // They will become real copies if we find a real use for them, and never |
| 564 | // created otherwise. |
| 565 | for (auto &PossibleCopy : ValueInfo.Infos) { |
| 566 | ValueDFS VD; |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 567 | // Determine where we are going to place the copy by the copy type. |
| 568 | // The predicate info for branches always come first, they will get |
| 569 | // materialized in the split block at the top of the block. |
| 570 | // The predicate info for assumes will be somewhere in the middle, |
| 571 | // it will get materialized in front of the assume. |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 572 | if (const auto *PAssume = dyn_cast<PredicateAssume>(PossibleCopy)) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 573 | VD.LocalNum = LN_Middle; |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 574 | DomTreeNode *DomNode = DT.getNode(PAssume->AssumeInst->getParent()); |
| 575 | if (!DomNode) |
| 576 | continue; |
| 577 | VD.DFSIn = DomNode->getDFSNumIn(); |
| 578 | VD.DFSOut = DomNode->getDFSNumOut(); |
| 579 | VD.PInfo = PossibleCopy; |
| 580 | OrderedUses.push_back(VD); |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 581 | } else if (isa<PredicateWithEdge>(PossibleCopy)) { |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 582 | // If we can only do phi uses, we treat it like it's in the branch |
| 583 | // block, and handle it specially. We know that it goes last, and only |
| 584 | // dominate phi uses. |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 585 | auto BlockEdge = getBlockEdge(PossibleCopy); |
| 586 | if (EdgeUsesOnly.count(BlockEdge)) { |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 587 | VD.LocalNum = LN_Last; |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 588 | auto *DomNode = DT.getNode(BlockEdge.first); |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 589 | if (DomNode) { |
| 590 | VD.DFSIn = DomNode->getDFSNumIn(); |
| 591 | VD.DFSOut = DomNode->getDFSNumOut(); |
| 592 | VD.PInfo = PossibleCopy; |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 593 | VD.EdgeOnly = true; |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 594 | OrderedUses.push_back(VD); |
| 595 | } |
| 596 | } else { |
| 597 | // Otherwise, we are in the split block (even though we perform |
| 598 | // insertion in the branch block). |
| 599 | // Insert a possible copy at the split block and before the branch. |
| 600 | VD.LocalNum = LN_First; |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 601 | auto *DomNode = DT.getNode(BlockEdge.second); |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 602 | if (DomNode) { |
| 603 | VD.DFSIn = DomNode->getDFSNumIn(); |
| 604 | VD.DFSOut = DomNode->getDFSNumOut(); |
| 605 | VD.PInfo = PossibleCopy; |
| 606 | OrderedUses.push_back(VD); |
| 607 | } |
| 608 | } |
| 609 | } |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | convertUsesToDFSOrdered(Op, OrderedUses); |
| 613 | std::sort(OrderedUses.begin(), OrderedUses.end(), Compare); |
| 614 | SmallVector<ValueDFS, 8> RenameStack; |
| 615 | // For each use, sorted into dfs order, push values and replaces uses with |
| 616 | // top of stack, which will represent the reaching def. |
| 617 | for (auto &VD : OrderedUses) { |
| 618 | // We currently do not materialize copy over copy, but we should decide if |
| 619 | // we want to. |
| 620 | bool PossibleCopy = VD.PInfo != nullptr; |
| 621 | if (RenameStack.empty()) { |
| 622 | DEBUG(dbgs() << "Rename Stack is empty\n"); |
| 623 | } else { |
| 624 | DEBUG(dbgs() << "Rename Stack Top DFS numbers are (" |
| 625 | << RenameStack.back().DFSIn << "," |
| 626 | << RenameStack.back().DFSOut << ")\n"); |
| 627 | } |
| 628 | |
| 629 | DEBUG(dbgs() << "Current DFS numbers are (" << VD.DFSIn << "," |
| 630 | << VD.DFSOut << ")\n"); |
| 631 | |
| 632 | bool ShouldPush = (VD.Def || PossibleCopy); |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 633 | bool OutOfScope = !stackIsInScope(RenameStack, VD); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 634 | if (OutOfScope || ShouldPush) { |
| 635 | // Sync to our current scope. |
Daniel Berlin | dbe8264 | 2017-02-12 22:12:20 +0000 | [diff] [blame] | 636 | popStackUntilDFSScope(RenameStack, VD); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 637 | if (ShouldPush) { |
| 638 | RenameStack.push_back(VD); |
| 639 | } |
| 640 | } |
| 641 | // If we get to this point, and the stack is empty we must have a use |
| 642 | // with no renaming needed, just skip it. |
| 643 | if (RenameStack.empty()) |
| 644 | continue; |
| 645 | // Skip values, only want to rename the uses |
| 646 | if (VD.Def || PossibleCopy) |
| 647 | continue; |
Daniel Berlin | a4b5c01 | 2017-02-19 04:29:01 +0000 | [diff] [blame] | 648 | if (!DebugCounter::shouldExecute(RenameCounter)) { |
| 649 | DEBUG(dbgs() << "Skipping execution due to debug counter\n"); |
| 650 | continue; |
| 651 | } |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 652 | ValueDFS &Result = RenameStack.back(); |
| 653 | |
| 654 | // If the possible copy dominates something, materialize our stack up to |
| 655 | // this point. This ensures every comparison that affects our operation |
| 656 | // ends up with predicateinfo. |
| 657 | if (!Result.Def) |
| 658 | Result.Def = materializeStack(Counter, RenameStack, Op); |
| 659 | |
| 660 | DEBUG(dbgs() << "Found replacement " << *Result.Def << " for " |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 661 | << *VD.U->get() << " in " << *(VD.U->getUser()) << "\n"); |
| 662 | assert(DT.dominates(cast<Instruction>(Result.Def), *VD.U) && |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 663 | "Predicateinfo def should have dominated this use"); |
Daniel Berlin | c763fd1 | 2017-02-07 22:11:43 +0000 | [diff] [blame] | 664 | VD.U->set(Result.Def); |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | PredicateInfo::ValueInfo &PredicateInfo::getOrCreateValueInfo(Value *Operand) { |
| 670 | auto OIN = ValueInfoNums.find(Operand); |
| 671 | if (OIN == ValueInfoNums.end()) { |
| 672 | // This will grow it |
| 673 | ValueInfos.resize(ValueInfos.size() + 1); |
| 674 | // This will use the new size and give us a 0 based number of the info |
| 675 | auto InsertResult = ValueInfoNums.insert({Operand, ValueInfos.size() - 1}); |
| 676 | assert(InsertResult.second && "Value info number already existed?"); |
| 677 | return ValueInfos[InsertResult.first->second]; |
| 678 | } |
| 679 | return ValueInfos[OIN->second]; |
| 680 | } |
| 681 | |
| 682 | const PredicateInfo::ValueInfo & |
| 683 | PredicateInfo::getValueInfo(Value *Operand) const { |
| 684 | auto OINI = ValueInfoNums.lookup(Operand); |
| 685 | assert(OINI != 0 && "Operand was not really in the Value Info Numbers"); |
| 686 | assert(OINI < ValueInfos.size() && |
| 687 | "Value Info Number greater than size of Value Info Table"); |
| 688 | return ValueInfos[OINI]; |
| 689 | } |
| 690 | |
| 691 | PredicateInfo::PredicateInfo(Function &F, DominatorTree &DT, |
| 692 | AssumptionCache &AC) |
Daniel Berlin | b7df17e | 2017-06-29 17:01:14 +0000 | [diff] [blame] | 693 | : F(F), DT(DT), AC(AC), OI(&DT) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 694 | // Push an empty operand info so that we can detect 0 as not finding one |
| 695 | ValueInfos.resize(1); |
| 696 | buildPredicateInfo(); |
| 697 | } |
| 698 | |
| 699 | PredicateInfo::~PredicateInfo() {} |
| 700 | |
| 701 | void PredicateInfo::verifyPredicateInfo() const {} |
| 702 | |
| 703 | char PredicateInfoPrinterLegacyPass::ID = 0; |
| 704 | |
| 705 | PredicateInfoPrinterLegacyPass::PredicateInfoPrinterLegacyPass() |
| 706 | : FunctionPass(ID) { |
| 707 | initializePredicateInfoPrinterLegacyPassPass( |
| 708 | *PassRegistry::getPassRegistry()); |
| 709 | } |
| 710 | |
| 711 | void PredicateInfoPrinterLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 712 | AU.setPreservesAll(); |
| 713 | AU.addRequiredTransitive<DominatorTreeWrapperPass>(); |
| 714 | AU.addRequired<AssumptionCacheTracker>(); |
| 715 | } |
| 716 | |
| 717 | bool PredicateInfoPrinterLegacyPass::runOnFunction(Function &F) { |
| 718 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 719 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
| 720 | auto PredInfo = make_unique<PredicateInfo>(F, DT, AC); |
| 721 | PredInfo->print(dbgs()); |
| 722 | if (VerifyPredicateInfo) |
| 723 | PredInfo->verifyPredicateInfo(); |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | PreservedAnalyses PredicateInfoPrinterPass::run(Function &F, |
| 728 | FunctionAnalysisManager &AM) { |
| 729 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 730 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
| 731 | OS << "PredicateInfo for function: " << F.getName() << "\n"; |
| 732 | make_unique<PredicateInfo>(F, DT, AC)->print(OS); |
| 733 | |
| 734 | return PreservedAnalyses::all(); |
| 735 | } |
| 736 | |
| 737 | /// \brief An assembly annotator class to print PredicateInfo information in |
| 738 | /// comments. |
| 739 | class PredicateInfoAnnotatedWriter : public AssemblyAnnotationWriter { |
| 740 | friend class PredicateInfo; |
| 741 | const PredicateInfo *PredInfo; |
| 742 | |
| 743 | public: |
| 744 | PredicateInfoAnnotatedWriter(const PredicateInfo *M) : PredInfo(M) {} |
| 745 | |
| 746 | virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, |
| 747 | formatted_raw_ostream &OS) {} |
| 748 | |
| 749 | virtual void emitInstructionAnnot(const Instruction *I, |
| 750 | formatted_raw_ostream &OS) { |
| 751 | if (const auto *PI = PredInfo->getPredicateInfoFor(I)) { |
| 752 | OS << "; Has predicate info\n"; |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 753 | if (const auto *PB = dyn_cast<PredicateBranch>(PI)) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 754 | OS << "; branch predicate info { TrueEdge: " << PB->TrueEdge |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 755 | << " Comparison:" << *PB->Condition << " Edge: ["; |
| 756 | PB->From->printAsOperand(OS); |
| 757 | OS << ","; |
| 758 | PB->To->printAsOperand(OS); |
| 759 | OS << "] }\n"; |
| 760 | } else if (const auto *PS = dyn_cast<PredicateSwitch>(PI)) { |
| 761 | OS << "; switch predicate info { CaseValue: " << *PS->CaseValue |
| 762 | << " Switch:" << *PS->Switch << " Edge: ["; |
| 763 | PS->From->printAsOperand(OS); |
| 764 | OS << ","; |
| 765 | PS->To->printAsOperand(OS); |
| 766 | OS << "] }\n"; |
| 767 | } else if (const auto *PA = dyn_cast<PredicateAssume>(PI)) { |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 768 | OS << "; assume predicate info {" |
Daniel Berlin | 588e0be | 2017-02-18 23:06:38 +0000 | [diff] [blame] | 769 | << " Comparison:" << *PA->Condition << " }\n"; |
Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 770 | } |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | }; |
| 774 | |
| 775 | void PredicateInfo::print(raw_ostream &OS) const { |
| 776 | PredicateInfoAnnotatedWriter Writer(this); |
| 777 | F.print(OS, &Writer); |
| 778 | } |
| 779 | |
| 780 | void PredicateInfo::dump() const { |
| 781 | PredicateInfoAnnotatedWriter Writer(this); |
| 782 | F.print(dbgs(), &Writer); |
| 783 | } |
| 784 | |
| 785 | PreservedAnalyses PredicateInfoVerifierPass::run(Function &F, |
| 786 | FunctionAnalysisManager &AM) { |
| 787 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 788 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
| 789 | make_unique<PredicateInfo>(F, DT, AC)->verifyPredicateInfo(); |
| 790 | |
| 791 | return PreservedAnalyses::all(); |
| 792 | } |
| 793 | } |