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