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