LLVM 23.0.0git
PredicateInfo.cpp
Go to the documentation of this file.
1//===-- PredicateInfo.cpp - PredicateInfo Builder--------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------===//
8//
9// This file implements the PredicateInfo class.
10//
11//===----------------------------------------------------------------===//
12
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/STLExtras.h"
20#include "llvm/IR/Dominators.h"
21#include "llvm/IR/IRBuilder.h"
26#include "llvm/Support/Debug.h"
29#define DEBUG_TYPE "predicateinfo"
30using namespace llvm;
31using namespace PatternMatch;
32
34 "verify-predicateinfo", cl::init(false), cl::Hidden,
35 cl::desc("Verify PredicateInfo in legacy printer pass."));
36DEBUG_COUNTER(RenameCounter, "predicateinfo-rename",
37 "Controls which variables are renamed with predicateinfo");
38
39// Maximum number of conditions considered for renaming for each branch/assume.
40// This limits renaming of deep and/or chains.
41static const unsigned MaxCondsPerBranch = 8;
42
43namespace {
44// Given a predicate info that is a type of branching terminator, get the
45// branching block.
46const BasicBlock *getBranchBlock(const PredicateBase *PB) {
48 "Only branches and switches should have PHIOnly defs that "
49 "require branch blocks.");
50 return cast<PredicateWithEdge>(PB)->From;
51}
52
53// Given a predicate info that is a type of branching terminator, get the
54// branching terminator.
55static Instruction *getBranchTerminator(const PredicateBase *PB) {
57 "Not a predicate info type we know how to get a terminator from.");
58 return cast<PredicateWithEdge>(PB)->From->getTerminator();
59}
60
61// Given a predicate info that is a type of branching terminator, get the
62// edge this predicate info represents
63std::pair<BasicBlock *, BasicBlock *> getBlockEdge(const PredicateBase *PB) {
65 "Not a predicate info type we know how to get an edge from.");
66 const auto *PEdge = cast<PredicateWithEdge>(PB);
67 return std::make_pair(PEdge->From, PEdge->To);
68}
69}
70
71namespace llvm {
73 // Operations that must appear first in the block.
75 // Operations that are somewhere in the middle of the block, and are sorted on
76 // demand.
78 // Operations that must appear last in a block, like successor phi node uses.
80};
81
82// Associate global and local DFS info with defs (PInfo set) and uses (U set),
83// so we can sort them into a global domination ordering.
84struct ValueDFS {
85 int DFSIn = 0;
86 int DFSOut = 0;
87 unsigned int LocalNum = LN_Middle;
88 // Only one of U or PInfo will be set.
89 Use *U = nullptr;
90 PredicateBase *PInfo = nullptr;
91};
92
93// This compares ValueDFS structures. Doing so allows us to walk the minimum
94// number of instructions necessary to compute our def/use ordering.
98
99 bool operator()(const ValueDFS &A, const ValueDFS &B) const {
100 if (&A == &B)
101 return false;
102
103 // Order by block first.
104 if (A.DFSIn != B.DFSIn)
105 return A.DFSIn < B.DFSIn;
106 assert(A.DFSOut == B.DFSOut &&
107 "Equal DFS-in numbers imply equal out numbers");
108
109 // Then order by first/middle/last.
110 if (A.LocalNum != B.LocalNum)
111 return A.LocalNum < B.LocalNum;
112
113 // We want to put the def that will get used for a given set of phi uses,
114 // before those phi uses.
115 // So we sort by edge, then by def.
116 // Note that only phi nodes uses and defs can come last.
117 if (A.LocalNum == LN_Last)
118 return comparePHIRelated(A, B);
119
120 // Use block-local ordering for instructions in the middle.
121 if (A.LocalNum == LN_Middle)
122 return localComesBefore(A, B);
123
124 // The order of PredicateInfo definitions at the start of the block does not
125 // matter.
126 assert(A.LocalNum == LN_First);
127 assert(A.PInfo && B.PInfo && "Must be predicate info def");
128 return false;
129 }
130
131 // For a phi use, or a non-materialized def, return the edge it represents.
132 std::pair<BasicBlock *, BasicBlock *> getBlockEdge(const ValueDFS &VD) const {
133 if (VD.U) {
134 auto *PHI = cast<PHINode>(VD.U->getUser());
135 return std::make_pair(PHI->getIncomingBlock(*VD.U), PHI->getParent());
136 }
137 // This is really a non-materialized def.
138 return ::getBlockEdge(VD.PInfo);
139 }
140
141 // For two phi related values, return the ordering.
142 bool comparePHIRelated(const ValueDFS &A, const ValueDFS &B) const {
143 BasicBlock *ASrc, *ADest, *BSrc, *BDest;
144 std::tie(ASrc, ADest) = getBlockEdge(A);
145 std::tie(BSrc, BDest) = getBlockEdge(B);
146
147#ifndef NDEBUG
148 // This function should only be used for values in the same BB, check that.
149 DomTreeNode *DomASrc = DT.getNode(ASrc);
150 DomTreeNode *DomBSrc = DT.getNode(BSrc);
151 assert(DomASrc->getDFSNumIn() == (unsigned)A.DFSIn &&
152 "DFS numbers for A should match the ones of the source block");
153 assert(DomBSrc->getDFSNumIn() == (unsigned)B.DFSIn &&
154 "DFS numbers for B should match the ones of the source block");
155 assert(A.DFSIn == B.DFSIn && "Values must be in the same block");
156#endif
157 (void)ASrc;
158 (void)BSrc;
159
160 // Use DFS numbers to compare destination blocks, to guarantee a
161 // deterministic order.
162 DomTreeNode *DomADest = DT.getNode(ADest);
163 DomTreeNode *DomBDest = DT.getNode(BDest);
164 unsigned AIn = DomADest->getDFSNumIn();
165 unsigned BIn = DomBDest->getDFSNumIn();
166 bool isAUse = A.U;
167 bool isBUse = B.U;
168 assert((!A.PInfo || !A.U) && (!B.PInfo || !B.U) &&
169 "Def and U cannot be set at the same time");
170 // Now sort by edge destination and then defs before uses.
171 return std::tie(AIn, isAUse) < std::tie(BIn, isBUse);
172 }
173
174 const Instruction *getDefOrUser(const ValueDFS &VD) const {
175 if (VD.U)
176 return cast<Instruction>(VD.U->getUser());
177
178 // For the purpose of ordering, we pretend the def is right after the
179 // assume, because that is where we will insert the info.
180 assert(VD.PInfo && "No use, and no predicateinfo should not occur");
182 "Middle of block should only occur for assumes");
183 return cast<PredicateAssume>(VD.PInfo)->AssumeInst->getNextNode();
184 }
185
186 // This performs the necessary local basic block ordering checks to tell
187 // whether A comes before B, where both are in the same basic block.
188 bool localComesBefore(const ValueDFS &A, const ValueDFS &B) const {
189 const Instruction *AInst = getDefOrUser(A);
190 const Instruction *BInst = getDefOrUser(B);
191 return AInst->comesBefore(BInst);
192 }
193};
194
196 // Used to store information about each value we might rename.
197 struct ValueInfo {
199 };
200
201 PredicateInfo &PI;
202 Function &F;
203 DominatorTree &DT;
204 AssumptionCache &AC;
205
206 // This stores info about each operand or comparison result we make copies
207 // of. The real ValueInfos start at index 1, index 0 is unused so that we
208 // can more easily detect invalid indexing.
210
211 // This gives the index into the ValueInfos array for a given Value. Because
212 // 0 is not a valid Value Info index, you can use DenseMap::lookup and tell
213 // whether it returned a valid result.
215
216 BumpPtrAllocator &Allocator;
217
218 ValueInfo &getOrCreateValueInfo(Value *);
219 const ValueInfo &getValueInfo(Value *) const;
220
221 void processAssume(AssumeInst *, BasicBlock *,
222 SmallVectorImpl<Value *> &OpsToRename);
223 void processBranch(BranchInst *, BasicBlock *,
224 SmallVectorImpl<Value *> &OpsToRename);
225 void processSwitch(SwitchInst *, BasicBlock *,
226 SmallVectorImpl<Value *> &OpsToRename);
227 void renameUses(SmallVectorImpl<Value *> &OpsToRename);
228 void addInfoFor(SmallVectorImpl<Value *> &OpsToRename, Value *Op,
230
231 struct StackEntry {
232 const ValueDFS *V;
233 Value *Def = nullptr;
234
235 StackEntry(const ValueDFS *V) : V(V) {}
236 };
237
238 using ValueDFSStack = SmallVectorImpl<StackEntry>;
239 void convertUsesToDFSOrdered(Value *, SmallVectorImpl<ValueDFS> &);
240 Value *materializeStack(unsigned int &, ValueDFSStack &, Value *);
241 bool stackIsInScope(const ValueDFSStack &, const ValueDFS &) const;
242 void popStackUntilDFSScope(ValueDFSStack &, const ValueDFS &);
243
244public:
246 AssumptionCache &AC, BumpPtrAllocator &Allocator)
247 : PI(PI), F(F), DT(DT), AC(AC), Allocator(Allocator) {
248 // Push an empty operand info so that we can detect 0 as not finding one
249 ValueInfos.resize(1);
250 }
251
252 void buildPredicateInfo();
253};
254
255bool PredicateInfoBuilder::stackIsInScope(const ValueDFSStack &Stack,
256 const ValueDFS &VDUse) const {
257 assert(!Stack.empty() && "Should not be called with empty stack");
258 // If it's a phi only use, make sure it's for this phi node edge, and that the
259 // use is in a phi node. If it's anything else, and the top of the stack is
260 // a LN_Last def, we need to pop the stack. We deliberately sort phi uses
261 // next to the defs they must go with so that we can know it's time to pop
262 // the stack when we hit the end of the phi uses for a given def.
263 const ValueDFS &Top = *Stack.back().V;
264 assert(Top.PInfo && "RenameStack should only contain predicate infos (defs)");
265 if (Top.LocalNum == LN_Last) {
266 if (!VDUse.U) {
267 assert(VDUse.PInfo && "A non-use VDUse should have a predicate info");
268 // We should reserve adjacent LN_Last defs for the same phi use.
269 return VDUse.LocalNum == LN_Last &&
270 // If the two phi defs have the same edge, they must be designated
271 // for the same succ BB.
272 getBlockEdge(Top.PInfo) == getBlockEdge(VDUse.PInfo);
273 }
274 auto *PHI = dyn_cast<PHINode>(VDUse.U->getUser());
275 if (!PHI)
276 return false;
277 // Check edge
278 BasicBlock *EdgePred = PHI->getIncomingBlock(*VDUse.U);
279 if (EdgePred != getBranchBlock(Top.PInfo))
280 return false;
281
282 // Use dominates, which knows how to handle edge dominance.
283 return DT.dominates(getBlockEdge(Top.PInfo), *VDUse.U);
284 }
285
286 return VDUse.DFSIn >= Top.DFSIn && VDUse.DFSOut <= Top.DFSOut;
287}
288
289void PredicateInfoBuilder::popStackUntilDFSScope(ValueDFSStack &Stack,
290 const ValueDFS &VD) {
291 while (!Stack.empty() && !stackIsInScope(Stack, VD))
292 Stack.pop_back();
293}
294
295// Convert the uses of Op into a vector of uses, associating global and local
296// DFS info with each one.
297void PredicateInfoBuilder::convertUsesToDFSOrdered(
298 Value *Op, SmallVectorImpl<ValueDFS> &DFSOrderedSet) {
299 for (auto &U : Op->uses()) {
300 if (auto *I = dyn_cast<Instruction>(U.getUser())) {
301 // Lifetime intrinsics must work directly on alloca, do not replace them
302 // with a predicated copy.
303 if (I->isLifetimeStartOrEnd())
304 continue;
305
306 ValueDFS VD;
307 // Put the phi node uses in the incoming block.
308 BasicBlock *IBlock;
309 if (auto *PN = dyn_cast<PHINode>(I)) {
310 IBlock = PN->getIncomingBlock(U);
311 // Make phi node users appear last in the incoming block
312 // they are from.
313 VD.LocalNum = LN_Last;
314 } else {
315 // If it's not a phi node use, it is somewhere in the middle of the
316 // block.
317 IBlock = I->getParent();
318 VD.LocalNum = LN_Middle;
319 }
320 DomTreeNode *DomNode = DT.getNode(IBlock);
321 // It's possible our use is in an unreachable block. Skip it if so.
322 if (!DomNode)
323 continue;
324 VD.DFSIn = DomNode->getDFSNumIn();
325 VD.DFSOut = DomNode->getDFSNumOut();
326 VD.U = &U;
327 DFSOrderedSet.push_back(VD);
328 }
329 }
330}
331
333 // Only want real values, not constants. Additionally, operands with one use
334 // are only being used in the comparison, which means they will not be useful
335 // for us to consider for predicateinfo.
336 return (isa<Instruction>(V) || isa<Argument>(V)) && !V->hasOneUse();
337}
338
339// Collect relevant operations from Comparison that we may want to insert copies
340// for.
341void collectCmpOps(CmpInst *Comparison, SmallVectorImpl<Value *> &CmpOperands) {
342 auto *Op0 = Comparison->getOperand(0);
343 auto *Op1 = Comparison->getOperand(1);
344 if (Op0 == Op1)
345 return;
346
347 CmpOperands.push_back(Op0);
348 CmpOperands.push_back(Op1);
349}
350
351// Add Op, PB to the list of value infos for Op, and mark Op to be renamed.
352void PredicateInfoBuilder::addInfoFor(SmallVectorImpl<Value *> &OpsToRename,
354 auto &OperandInfo = getOrCreateValueInfo(Op);
355 if (OperandInfo.Infos.empty())
356 OpsToRename.push_back(Op);
357 OperandInfo.Infos.push_back(PB);
358}
359
360// Process an assume instruction and place relevant operations we want to rename
361// into OpsToRename.
362void PredicateInfoBuilder::processAssume(
363 AssumeInst *II, BasicBlock *AssumeBB,
364 SmallVectorImpl<Value *> &OpsToRename) {
365 if (II->hasOperandBundles()) {
366 for (auto BOI : II->bundle_op_infos()) {
367 if (RetainedKnowledge RK = getKnowledgeFromBundle(*II, BOI)) {
368 if (RK.AttrKind == Attribute::NonNull)
369 addInfoFor(OpsToRename, RK.WasOn,
370 new (Allocator) PredicateBundleAssume(RK.WasOn, II,
371 Attribute::NonNull));
372 }
373 }
374 return;
375 }
376
377 SmallVector<Value *, 4> Worklist;
378 SmallPtrSet<Value *, 4> Visited;
379 Worklist.push_back(II->getOperand(0));
380 while (!Worklist.empty()) {
381 Value *Cond = Worklist.pop_back_val();
382 if (!Visited.insert(Cond).second)
383 continue;
384 if (Visited.size() > MaxCondsPerBranch)
385 break;
386
387 Value *Op0, *Op1;
388 if (match(Cond, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
389 Worklist.push_back(Op1);
390 Worklist.push_back(Op0);
391 }
392
393 SmallVector<Value *, 4> Values;
394 Values.push_back(Cond);
395 if (auto *Cmp = dyn_cast<CmpInst>(Cond))
396 collectCmpOps(Cmp, Values);
397 else if (match(Cond, m_NUWTrunc(m_Value(Op0))))
398 Values.push_back(Op0);
399
400 for (Value *V : Values) {
401 if (shouldRename(V)) {
402 auto *PA = new (Allocator) PredicateConditionAssume(V, II, Cond);
403 addInfoFor(OpsToRename, V, PA);
404 }
405 }
406 }
407}
408
409// Process a block terminating branch, and place relevant operations to be
410// renamed into OpsToRename.
411void PredicateInfoBuilder::processBranch(
412 BranchInst *BI, BasicBlock *BranchBB,
413 SmallVectorImpl<Value *> &OpsToRename) {
414 BasicBlock *FirstBB = BI->getSuccessor(0);
415 BasicBlock *SecondBB = BI->getSuccessor(1);
416
417 for (BasicBlock *Succ : {FirstBB, SecondBB}) {
418 bool TakenEdge = Succ == FirstBB;
419 // Don't try to insert on a self-edge. This is mainly because we will
420 // eliminate during renaming anyway.
421 if (Succ == BranchBB)
422 continue;
423
424 SmallVector<Value *, 4> Worklist;
425 SmallPtrSet<Value *, 4> Visited;
426 Worklist.push_back(BI->getCondition());
427 while (!Worklist.empty()) {
428 Value *Cond = Worklist.pop_back_val();
429 if (!Visited.insert(Cond).second)
430 continue;
431 if (Visited.size() > MaxCondsPerBranch)
432 break;
433
434 Value *Op0, *Op1;
435 if (TakenEdge ? match(Cond, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))
436 : match(Cond, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) {
437 Worklist.push_back(Op1);
438 Worklist.push_back(Op0);
439 }
440
441 SmallVector<Value *, 4> Values;
442 Values.push_back(Cond);
443 if (auto *Cmp = dyn_cast<CmpInst>(Cond))
444 collectCmpOps(Cmp, Values);
445 else if (match(Cond, m_NUWTrunc(m_Value(Op0))))
446 Values.push_back(Op0);
447
448 for (Value *V : Values) {
449 if (shouldRename(V)) {
450 PredicateBase *PB = new (Allocator)
451 PredicateBranch(V, BranchBB, Succ, Cond, TakenEdge);
452 addInfoFor(OpsToRename, V, PB);
453 }
454 }
455 }
456 }
457}
458// Process a block terminating switch, and place relevant operations to be
459// renamed into OpsToRename.
460void PredicateInfoBuilder::processSwitch(
461 SwitchInst *SI, BasicBlock *BranchBB,
462 SmallVectorImpl<Value *> &OpsToRename) {
463 Value *Op = SI->getCondition();
464 if ((!isa<Instruction>(Op) && !isa<Argument>(Op)) || Op->hasOneUse())
465 return;
466
467 // Remember how many outgoing edges there are to every successor.
468 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
469 for (BasicBlock *TargetBlock : successors(BranchBB))
470 ++SwitchEdges[TargetBlock];
471
472 // Now propagate info for each case value
473 for (auto C : SI->cases()) {
474 BasicBlock *TargetBlock = C.getCaseSuccessor();
475 if (SwitchEdges.lookup(TargetBlock) == 1) {
476 PredicateSwitch *PS = new (Allocator) PredicateSwitch(
477 Op, SI->getParent(), TargetBlock, C.getCaseValue(), SI);
478 addInfoFor(OpsToRename, Op, PS);
479 }
480 }
481}
482
483// Build predicate info for our function
485 DT.updateDFSNumbers();
486 // Collect operands to rename from all conditional branch terminators, as well
487 // as assume statements.
488 SmallVector<Value *, 8> OpsToRename;
489 for (BasicBlock &BB : F) {
490 if (!DT.isReachableFromEntry(&BB))
491 continue;
492
493 if (auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
494 if (!BI->isConditional())
495 continue;
496 // Can't insert conditional information if they all go to the same place.
497 if (BI->getSuccessor(0) == BI->getSuccessor(1))
498 continue;
499 processBranch(BI, &BB, OpsToRename);
500 } else if (auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
501 processSwitch(SI, &BB, OpsToRename);
502 }
503 }
504 for (auto &Assume : AC.assumptions()) {
505 if (auto *II = cast_or_null<AssumeInst>(Assume))
506 if (DT.isReachableFromEntry(II->getParent()))
507 processAssume(II, II->getParent(), OpsToRename);
508 }
509 // Now rename all our operations.
510 renameUses(OpsToRename);
511}
512
513// Given the renaming stack, make all the operands currently on the stack real
514// by inserting them into the IR. Return the last operation's value.
515Value *PredicateInfoBuilder::materializeStack(unsigned int &Counter,
516 ValueDFSStack &RenameStack,
517 Value *OrigOp) {
518 // Find the first thing we have to materialize
519 auto RevIter = RenameStack.rbegin();
520 for (; RevIter != RenameStack.rend(); ++RevIter)
521 if (RevIter->Def)
522 break;
523
524 size_t Start = RevIter - RenameStack.rbegin();
525 // The maximum number of things we should be trying to materialize at once
526 // right now is 4, depending on if we had an assume, a branch, and both used
527 // and of conditions.
528 for (auto RenameIter = RenameStack.end() - Start;
529 RenameIter != RenameStack.end(); ++RenameIter) {
530 auto *Op =
531 RenameIter == RenameStack.begin() ? OrigOp : (RenameIter - 1)->Def;
532 StackEntry &Result = *RenameIter;
533 auto *ValInfo = Result.V->PInfo;
534 ValInfo->RenamedOp = (RenameStack.end() - Start) == RenameStack.begin()
535 ? OrigOp
536 : (RenameStack.end() - Start - 1)->Def;
537 auto CreateSSACopy = [](Instruction *InsertPt, Value *Op,
538 const Twine &Name = "") {
539 // Use a no-op bitcast to represent ssa copy.
540 return new BitCastInst(Op, Op->getType(), Name, InsertPt->getIterator());
541 };
542 // For edge predicates, we can just place the operand in the block before
543 // the terminator. For assume, we have to place it right after the assume
544 // to ensure we dominate all uses except assume itself. Always insert
545 // right before the terminator or after the assume, so that we insert in
546 // proper order in the case of multiple predicateinfo in the same block.
547 if (isa<PredicateWithEdge>(ValInfo)) {
548 BitCastInst *PIC = CreateSSACopy(getBranchTerminator(ValInfo), Op,
549 Op->getName() + "." + Twine(Counter++));
550 PI.PredicateMap.insert({PIC, ValInfo});
551 Result.Def = PIC;
552 } else {
553 auto *PAssume = dyn_cast<PredicateAssume>(ValInfo);
554 assert(PAssume &&
555 "Should not have gotten here without it being an assume");
556 // Insert the predicate directly after the assume. While it also holds
557 // directly before it, assume(i1 true) is not a useful fact.
558 BitCastInst *PIC = CreateSSACopy(PAssume->AssumeInst->getNextNode(), Op);
559 PI.PredicateMap.insert({PIC, ValInfo});
560 Result.Def = PIC;
561 }
562 }
563 return RenameStack.back().Def;
564}
565
566// Instead of the standard SSA renaming algorithm, which is O(Number of
567// instructions), and walks the entire dominator tree, we walk only the defs +
568// uses. The standard SSA renaming algorithm does not really rely on the
569// dominator tree except to order the stack push/pops of the renaming stacks, so
570// that defs end up getting pushed before hitting the correct uses. This does
571// not require the dominator tree, only the *order* of the dominator tree. The
572// complete and correct ordering of the defs and uses, in dominator tree is
573// contained in the DFS numbering of the dominator tree. So we sort the defs and
574// uses into the DFS ordering, and then just use the renaming stack as per
575// normal, pushing when we hit a def (which is a predicateinfo instruction),
576// popping when we are out of the dfs scope for that def, and replacing any uses
577// with top of stack if it exists. In order to handle liveness without
578// propagating liveness info, we don't actually insert the predicateinfo
579// instruction def until we see a use that it would dominate. Once we see such
580// a use, we materialize the predicateinfo instruction in the right place and
581// use it.
582//
583// TODO: Use this algorithm to perform fast single-variable renaming in
584// promotememtoreg and memoryssa.
585void PredicateInfoBuilder::renameUses(SmallVectorImpl<Value *> &OpsToRename) {
586 ValueDFS_Compare Compare(DT);
587 // Compute liveness, and rename in O(uses) per Op.
588 for (auto *Op : OpsToRename) {
589 LLVM_DEBUG(dbgs() << "Visiting " << *Op << "\n");
590 unsigned Counter = 0;
591 SmallVector<ValueDFS, 16> OrderedUses;
592 const auto &ValueInfo = getValueInfo(Op);
593 // Insert the possible copies into the def/use list.
594 // They will become real copies if we find a real use for them, and never
595 // created otherwise.
596 for (const auto &PossibleCopy : ValueInfo.Infos) {
597 ValueDFS VD;
598 // Determine where we are going to place the copy by the copy type.
599 // The predicate info for branches always come first, they will get
600 // materialized in the split block at the top of the block.
601 // The predicate info for assumes will be somewhere in the middle,
602 // it will get materialized right after the assume.
603 if (const auto *PAssume = dyn_cast<PredicateAssume>(PossibleCopy)) {
604 VD.LocalNum = LN_Middle;
605 DomTreeNode *DomNode = DT.getNode(PAssume->AssumeInst->getParent());
606 if (!DomNode)
607 continue;
608 VD.DFSIn = DomNode->getDFSNumIn();
609 VD.DFSOut = DomNode->getDFSNumOut();
610 VD.PInfo = PossibleCopy;
611 OrderedUses.push_back(VD);
612 } else if (isa<PredicateWithEdge>(PossibleCopy)) {
613 // If we can only do phi uses, we treat it like it's in the branch
614 // block, and handle it specially. We know that it goes last, and only
615 // dominate phi uses.
616 auto BlockEdge = getBlockEdge(PossibleCopy);
617 if (!BlockEdge.second->getSinglePredecessor()) {
618 VD.LocalNum = LN_Last;
619 auto *DomNode = DT.getNode(BlockEdge.first);
620 if (DomNode) {
621 VD.DFSIn = DomNode->getDFSNumIn();
622 VD.DFSOut = DomNode->getDFSNumOut();
623 VD.PInfo = PossibleCopy;
624 OrderedUses.push_back(VD);
625 }
626 } else {
627 // Otherwise, we are in the split block (even though we perform
628 // insertion in the branch block).
629 // Insert a possible copy at the split block and before the branch.
630 VD.LocalNum = LN_First;
631 auto *DomNode = DT.getNode(BlockEdge.second);
632 if (DomNode) {
633 VD.DFSIn = DomNode->getDFSNumIn();
634 VD.DFSOut = DomNode->getDFSNumOut();
635 VD.PInfo = PossibleCopy;
636 OrderedUses.push_back(VD);
637 }
638 }
639 }
640 }
641
642 convertUsesToDFSOrdered(Op, OrderedUses);
643 // Here we require a stable sort because we do not bother to try to
644 // assign an order to the operands the uses represent. Thus, two
645 // uses in the same instruction do not have a strict sort order
646 // currently and will be considered equal. We could get rid of the
647 // stable sort by creating one if we wanted.
648 llvm::stable_sort(OrderedUses, Compare);
649 SmallVector<StackEntry, 8> RenameStack;
650 // For each use, sorted into dfs order, push values and replaces uses with
651 // top of stack, which will represent the reaching def.
652 for (const ValueDFS &VD : OrderedUses) {
653 // We currently do not materialize copy over copy, but we should decide if
654 // we want to.
655 if (RenameStack.empty()) {
656 LLVM_DEBUG(dbgs() << "Rename Stack is empty\n");
657 } else {
658 LLVM_DEBUG(dbgs() << "Rename Stack Top DFS numbers are ("
659 << RenameStack.back().V->DFSIn << ","
660 << RenameStack.back().V->DFSOut << ")\n");
661 }
662
663 LLVM_DEBUG(dbgs() << "Current DFS numbers are (" << VD.DFSIn << ","
664 << VD.DFSOut << ")\n");
665
666 // Sync to our current scope.
667 popStackUntilDFSScope(RenameStack, VD);
668
669 if (VD.PInfo) {
670 RenameStack.push_back(&VD);
671 continue;
672 }
673
674 // If we get to this point, and the stack is empty we must have a use
675 // with no renaming needed, just skip it.
676 if (RenameStack.empty())
677 continue;
678 if (!DebugCounter::shouldExecute(RenameCounter)) {
679 LLVM_DEBUG(dbgs() << "Skipping execution due to debug counter\n");
680 continue;
681 }
682 StackEntry &Result = RenameStack.back();
683
684 // If the possible copy dominates something, materialize our stack up to
685 // this point. This ensures every comparison that affects our operation
686 // ends up with predicateinfo.
687 if (!Result.Def)
688 Result.Def = materializeStack(Counter, RenameStack, Op);
689
690 LLVM_DEBUG(dbgs() << "Found replacement " << *Result.Def << " for "
691 << *VD.U->get() << " in " << *(VD.U->getUser())
692 << "\n");
693 assert(DT.dominates(cast<Instruction>(Result.Def), *VD.U) &&
694 "Predicateinfo def should have dominated this use");
695 VD.U->set(Result.Def);
696 }
697 }
698}
699
700PredicateInfoBuilder::ValueInfo &
701PredicateInfoBuilder::getOrCreateValueInfo(Value *Operand) {
702 auto Res = ValueInfoNums.try_emplace(Operand, ValueInfos.size());
703 if (Res.second) {
704 // Allocate space for new ValueInfo.
705 ValueInfos.resize(ValueInfos.size() + 1);
706 }
707 return ValueInfos[Res.first->second];
708}
709
710const PredicateInfoBuilder::ValueInfo &
711PredicateInfoBuilder::getValueInfo(Value *Operand) const {
712 auto OINI = ValueInfoNums.lookup(Operand);
713 assert(OINI != 0 && "Operand was not really in the Value Info Numbers");
714 assert(OINI < ValueInfos.size() &&
715 "Value Info Number greater than size of Value Info Table");
716 return ValueInfos[OINI];
717}
718
720 AssumptionCache &AC, BumpPtrAllocator &Allocator)
721 : F(F) {
722 PredicateInfoBuilder Builder(*this, F, DT, AC, Allocator);
723 Builder.buildPredicateInfo();
724}
725
726std::optional<PredicateConstraint> PredicateBase::getConstraint() const {
727 switch (Type) {
728 case PT_BundleAssume: {
729 assert(cast<PredicateBundleAssume>(this)->AttrKind == Attribute::NonNull &&
730 "Cannot handle anything other than NonNull");
732 cast<PointerType>(OriginalOp->getType()))}};
733 }
734
736 case PT_Branch: {
737 bool TrueEdge = true;
738 if (auto *PBranch = dyn_cast<PredicateBranch>(this))
739 TrueEdge = PBranch->TrueEdge;
740
741 if (Condition == RenamedOp) {
742 return {{CmpInst::ICMP_EQ,
743 TrueEdge ? ConstantInt::getTrue(Condition->getType())
744 : ConstantInt::getFalse(Condition->getType())}};
745 }
746
748 return {{TrueEdge ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ,
750 }
751
753 if (!Cmp) {
754 // TODO: Make this an assertion once RenamedOp is fully accurate.
755 return std::nullopt;
756 }
757
759 Value *OtherOp;
760 if (Cmp->getOperand(0) == RenamedOp) {
761 Pred = Cmp->getPredicate();
762 OtherOp = Cmp->getOperand(1);
763 } else if (Cmp->getOperand(1) == RenamedOp) {
764 Pred = Cmp->getSwappedPredicate();
765 OtherOp = Cmp->getOperand(0);
766 } else {
767 // TODO: Make this an assertion once RenamedOp is fully accurate.
768 return std::nullopt;
769 }
770
771 // Invert predicate along false edge.
772 if (!TrueEdge)
773 Pred = CmpInst::getInversePredicate(Pred);
774
775 return {{Pred, OtherOp}};
776 }
777 case PT_Switch:
778 if (Condition != RenamedOp) {
779 // TODO: Make this an assertion once RenamedOp is fully accurate.
780 return std::nullopt;
781 }
782
783 return {{CmpInst::ICMP_EQ, cast<PredicateSwitch>(this)->CaseValue}};
784 }
785 llvm_unreachable("Unknown predicate type");
786}
787
789
790// Replace bitcasts created by PredicateInfo with their operand.
793 const auto *PI = PredInfo.getPredicateInfoFor(&Inst);
794 if (!PI)
795 continue;
796
797 assert(isa<BitCastInst>(Inst) &&
798 Inst.getType() == Inst.getOperand(0)->getType());
799 Inst.replaceAllUsesWith(Inst.getOperand(0));
800 Inst.eraseFromParent();
801 }
802}
803
806 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
807 auto &AC = AM.getResult<AssumptionAnalysis>(F);
808 OS << "PredicateInfo for function: " << F.getName() << "\n";
809 BumpPtrAllocator Allocator;
810 auto PredInfo = std::make_unique<PredicateInfo>(F, DT, AC, Allocator);
811 PredInfo->print(OS);
812
813 replaceCreatedSSACopys(*PredInfo, F);
814 return PreservedAnalyses::all();
815}
816
817/// An assembly annotator class to print PredicateInfo information in
818/// comments.
820 friend class PredicateInfo;
821 const PredicateInfo *PredInfo;
822
823public:
825
827 formatted_raw_ostream &OS) override {}
828
830 formatted_raw_ostream &OS) override {
831 if (const auto *PI = PredInfo->getPredicateInfoFor(I)) {
832 if (const auto *PB = dyn_cast<PredicateBranch>(PI)) {
833 OS << "; branch predicate info { TrueEdge: " << PB->TrueEdge
834 << " Comparison:" << *PB->Condition << " Edge: [";
835 PB->From->printAsOperand(OS);
836 OS << ",";
837 PB->To->printAsOperand(OS);
838 OS << "]";
839 } else if (const auto *PS = dyn_cast<PredicateSwitch>(PI)) {
840 OS << "; switch predicate info { CaseValue: " << *PS->CaseValue
841 << " Edge: [";
842 PS->From->printAsOperand(OS);
843 OS << ",";
844 PS->To->printAsOperand(OS);
845 OS << "]";
846 } else if (const auto *PA = dyn_cast<PredicateAssume>(PI)) {
847 OS << "; assume predicate info {";
848 if (auto *PBA = dyn_cast<PredicateBundleAssume>(PA)) {
849 OS << " Attribute: " << Attribute::getNameFromAttrKind(PBA->AttrKind);
850 } else {
852 OS << " Comparison:" << *PA->Condition;
853 }
854 }
855 OS << ", RenamedOp: ";
856 PI->RenamedOp->printAsOperand(OS, false);
857 OS << " }\n";
858 }
859 }
860};
861
863 PredicateInfoAnnotatedWriter Writer(this);
864 F.print(OS, &Writer);
865}
866
868 PredicateInfoAnnotatedWriter Writer(this);
869 F.print(dbgs(), &Writer);
870}
871
874 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
875 auto &AC = AM.getResult<AssumptionAnalysis>(F);
876 BumpPtrAllocator Allocator;
877 std::make_unique<PredicateInfo>(F, DT, AC, Allocator)->verifyPredicateInfo();
878
879 return PreservedAnalyses::all();
880}
881}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
Expand Atomic instructions
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file defines the DenseMap class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
PassInstrumentationCallbacks PIC
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
static cl::opt< bool > VerifyPredicateInfo("verify-predicateinfo", cl::init(false), cl::Hidden, cl::desc("Verify PredicateInfo in legacy printer pass."))
static const unsigned MaxCondsPerBranch
This file implements the PredicateInfo analysis, which creates an Extended SSA form for operations us...
const SmallVectorImpl< MachineOperand > & Cond
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This represents the llvm.assume intrinsic.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
static LLVM_ABI StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
This class represents a no-op cast from one type to another.
Conditional or Unconditional Branch instruction.
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
Value * getCondition() const
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_NE
not equal
Definition InstrTypes.h:698
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
static bool shouldExecute(CounterInfo &Counter)
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
unsigned getDFSNumIn() const
getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes in the dominator tree.
unsigned getDFSNumOut() const
Analysis pass which computes a DominatorTree.
Definition Dominators.h:283
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
PredicateType Type
LLVM_ABI std::optional< PredicateConstraint > getConstraint() const
Fetch condition in the form of PredicateConstraint, if possible.
PredicateInfoAnnotatedWriter(const PredicateInfo *M)
void emitInstructionAnnot(const Instruction *I, formatted_raw_ostream &OS) override
emitInstructionAnnot - This may be implemented to emit a string right before an instruction is emitte...
void emitBasicBlockStartAnnot(const BasicBlock *BB, formatted_raw_ostream &OS) override
emitBasicBlockStartAnnot - This may be implemented to emit a string right after the basic block label...
PredicateInfoBuilder(PredicateInfo &PI, Function &F, DominatorTree &DT, AssumptionCache &AC, BumpPtrAllocator &Allocator)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Encapsulates PredicateInfo, including all data associated with memory accesses.
friend class PredicateInfoAnnotatedWriter
LLVM_ABI void verifyPredicateInfo() const
friend class PredicateInfoBuilder
LLVM_ABI void print(raw_ostream &) const
LLVM_ABI PredicateInfo(Function &, DominatorTree &, AssumptionCache &, BumpPtrAllocator &)
LLVM_ABI void dump() const
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
size_type size() const
Definition SmallPtrSet.h:99
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Multiway switch.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
void stable_sort(R &&Range)
Definition STLExtras.h:2106
void collectCmpOps(CmpInst *Comparison, SmallVectorImpl< Value * > &CmpOperands)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
static void replaceCreatedSSACopys(PredicateInfo &PredInfo, Function &F)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
bool shouldRename(Value *V)
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:94
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ PT_ConditionAssume
@ PT_Switch
@ PT_BundleAssume
@ PT_Branch
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
std::pair< BasicBlock *, BasicBlock * > getBlockEdge(const ValueDFS &VD) const
bool operator()(const ValueDFS &A, const ValueDFS &B) const
const Instruction * getDefOrUser(const ValueDFS &VD) const
bool comparePHIRelated(const ValueDFS &A, const ValueDFS &B) const
ValueDFS_Compare(DominatorTree &DT)
bool localComesBefore(const ValueDFS &A, const ValueDFS &B) const
PredicateBase * PInfo
unsigned int LocalNum