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