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(CondBrInst *, 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 && shouldRename(RK.WasOn))
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 CondBrInst *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<CondBrInst>(BB.getTerminator())) {
494 // Can't insert conditional information if they all go to the same place.
495 if (BI->getSuccessor(0) == BI->getSuccessor(1))
496 continue;
497 processBranch(BI, &BB, OpsToRename);
498 } else if (auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
499 processSwitch(SI, &BB, OpsToRename);
500 }
501 }
502 for (auto &Assume : AC.assumptions()) {
503 if (auto *II = cast_or_null<AssumeInst>(Assume))
504 if (DT.isReachableFromEntry(II->getParent()))
505 processAssume(II, II->getParent(), OpsToRename);
506 }
507 // Now rename all our operations.
508 renameUses(OpsToRename);
509}
510
511// Given the renaming stack, make all the operands currently on the stack real
512// by inserting them into the IR. Return the last operation's value.
513Value *PredicateInfoBuilder::materializeStack(unsigned int &Counter,
514 ValueDFSStack &RenameStack,
515 Value *OrigOp) {
516 // Find the first thing we have to materialize
517 auto RevIter = RenameStack.rbegin();
518 for (; RevIter != RenameStack.rend(); ++RevIter)
519 if (RevIter->Def)
520 break;
521
522 size_t Start = RevIter - RenameStack.rbegin();
523 // The maximum number of things we should be trying to materialize at once
524 // right now is 4, depending on if we had an assume, a branch, and both used
525 // and of conditions.
526 for (auto RenameIter = RenameStack.end() - Start;
527 RenameIter != RenameStack.end(); ++RenameIter) {
528 auto *Op =
529 RenameIter == RenameStack.begin() ? OrigOp : (RenameIter - 1)->Def;
530 StackEntry &Result = *RenameIter;
531 auto *ValInfo = Result.V->PInfo;
532 ValInfo->RenamedOp = (RenameStack.end() - Start) == RenameStack.begin()
533 ? OrigOp
534 : (RenameStack.end() - Start - 1)->Def;
535 auto CreateSSACopy = [](Instruction *InsertPt, Value *Op,
536 const Twine &Name = "") {
537 // Use a no-op bitcast to represent ssa copy.
538 return new BitCastInst(Op, Op->getType(), Name, InsertPt->getIterator());
539 };
540 // For edge predicates, we can just place the operand in the block before
541 // the terminator. For assume, we have to place it right after the assume
542 // to ensure we dominate all uses except assume itself. Always insert
543 // right before the terminator or after the assume, so that we insert in
544 // proper order in the case of multiple predicateinfo in the same block.
545 if (isa<PredicateWithEdge>(ValInfo)) {
546 BitCastInst *PIC = CreateSSACopy(getBranchTerminator(ValInfo), Op,
547 Op->getName() + "." + Twine(Counter++));
548 PI.PredicateMap.insert({PIC, ValInfo});
549 Result.Def = PIC;
550 } else {
551 auto *PAssume = dyn_cast<PredicateAssume>(ValInfo);
552 assert(PAssume &&
553 "Should not have gotten here without it being an assume");
554 // Insert the predicate directly after the assume. While it also holds
555 // directly before it, assume(i1 true) is not a useful fact.
556 BitCastInst *PIC = CreateSSACopy(PAssume->AssumeInst->getNextNode(), Op);
557 PI.PredicateMap.insert({PIC, ValInfo});
558 Result.Def = PIC;
559 }
560 }
561 return RenameStack.back().Def;
562}
563
564// Instead of the standard SSA renaming algorithm, which is O(Number of
565// instructions), and walks the entire dominator tree, we walk only the defs +
566// uses. The standard SSA renaming algorithm does not really rely on the
567// dominator tree except to order the stack push/pops of the renaming stacks, so
568// that defs end up getting pushed before hitting the correct uses. This does
569// not require the dominator tree, only the *order* of the dominator tree. The
570// complete and correct ordering of the defs and uses, in dominator tree is
571// contained in the DFS numbering of the dominator tree. So we sort the defs and
572// uses into the DFS ordering, and then just use the renaming stack as per
573// normal, pushing when we hit a def (which is a predicateinfo instruction),
574// popping when we are out of the dfs scope for that def, and replacing any uses
575// with top of stack if it exists. In order to handle liveness without
576// propagating liveness info, we don't actually insert the predicateinfo
577// instruction def until we see a use that it would dominate. Once we see such
578// a use, we materialize the predicateinfo instruction in the right place and
579// use it.
580//
581// TODO: Use this algorithm to perform fast single-variable renaming in
582// promotememtoreg and memoryssa.
583void PredicateInfoBuilder::renameUses(SmallVectorImpl<Value *> &OpsToRename) {
584 ValueDFS_Compare Compare(DT);
585 // Compute liveness, and rename in O(uses) per Op.
586 for (auto *Op : OpsToRename) {
587 LLVM_DEBUG(dbgs() << "Visiting " << *Op << "\n");
588 unsigned Counter = 0;
589 SmallVector<ValueDFS, 16> OrderedUses;
590 const auto &ValueInfo = getValueInfo(Op);
591 // Insert the possible copies into the def/use list.
592 // They will become real copies if we find a real use for them, and never
593 // created otherwise.
594 for (const auto &PossibleCopy : ValueInfo.Infos) {
595 ValueDFS VD;
596 // Determine where we are going to place the copy by the copy type.
597 // The predicate info for branches always come first, they will get
598 // materialized in the split block at the top of the block.
599 // The predicate info for assumes will be somewhere in the middle,
600 // it will get materialized right after the assume.
601 if (const auto *PAssume = dyn_cast<PredicateAssume>(PossibleCopy)) {
602 VD.LocalNum = LN_Middle;
603 DomTreeNode *DomNode = DT.getNode(PAssume->AssumeInst->getParent());
604 if (!DomNode)
605 continue;
606 VD.DFSIn = DomNode->getDFSNumIn();
607 VD.DFSOut = DomNode->getDFSNumOut();
608 VD.PInfo = PossibleCopy;
609 OrderedUses.push_back(VD);
610 } else if (isa<PredicateWithEdge>(PossibleCopy)) {
611 // If we can only do phi uses, we treat it like it's in the branch
612 // block, and handle it specially. We know that it goes last, and only
613 // dominate phi uses.
614 auto BlockEdge = getBlockEdge(PossibleCopy);
615 if (!BlockEdge.second->getSinglePredecessor()) {
616 VD.LocalNum = LN_Last;
617 auto *DomNode = DT.getNode(BlockEdge.first);
618 if (DomNode) {
619 VD.DFSIn = DomNode->getDFSNumIn();
620 VD.DFSOut = DomNode->getDFSNumOut();
621 VD.PInfo = PossibleCopy;
622 OrderedUses.push_back(VD);
623 }
624 } else {
625 // Otherwise, we are in the split block (even though we perform
626 // insertion in the branch block).
627 // Insert a possible copy at the split block and before the branch.
628 VD.LocalNum = LN_First;
629 auto *DomNode = DT.getNode(BlockEdge.second);
630 if (DomNode) {
631 VD.DFSIn = DomNode->getDFSNumIn();
632 VD.DFSOut = DomNode->getDFSNumOut();
633 VD.PInfo = PossibleCopy;
634 OrderedUses.push_back(VD);
635 }
636 }
637 }
638 }
639
640 convertUsesToDFSOrdered(Op, OrderedUses);
641 // Here we require a stable sort because we do not bother to try to
642 // assign an order to the operands the uses represent. Thus, two
643 // uses in the same instruction do not have a strict sort order
644 // currently and will be considered equal. We could get rid of the
645 // stable sort by creating one if we wanted.
646 llvm::stable_sort(OrderedUses, Compare);
647 SmallVector<StackEntry, 8> RenameStack;
648 // For each use, sorted into dfs order, push values and replaces uses with
649 // top of stack, which will represent the reaching def.
650 for (const ValueDFS &VD : OrderedUses) {
651 // We currently do not materialize copy over copy, but we should decide if
652 // we want to.
653 if (RenameStack.empty()) {
654 LLVM_DEBUG(dbgs() << "Rename Stack is empty\n");
655 } else {
656 LLVM_DEBUG(dbgs() << "Rename Stack Top DFS numbers are ("
657 << RenameStack.back().V->DFSIn << ","
658 << RenameStack.back().V->DFSOut << ")\n");
659 }
660
661 LLVM_DEBUG(dbgs() << "Current DFS numbers are (" << VD.DFSIn << ","
662 << VD.DFSOut << ")\n");
663
664 // Sync to our current scope.
665 popStackUntilDFSScope(RenameStack, VD);
666
667 if (VD.PInfo) {
668 RenameStack.push_back(&VD);
669 continue;
670 }
671
672 // If we get to this point, and the stack is empty we must have a use
673 // with no renaming needed, just skip it.
674 if (RenameStack.empty())
675 continue;
676 if (!DebugCounter::shouldExecute(RenameCounter)) {
677 LLVM_DEBUG(dbgs() << "Skipping execution due to debug counter\n");
678 continue;
679 }
680 StackEntry &Result = RenameStack.back();
681
682 // If the possible copy dominates something, materialize our stack up to
683 // this point. This ensures every comparison that affects our operation
684 // ends up with predicateinfo.
685 if (!Result.Def)
686 Result.Def = materializeStack(Counter, RenameStack, Op);
687
688 LLVM_DEBUG(dbgs() << "Found replacement " << *Result.Def << " for "
689 << *VD.U->get() << " in " << *(VD.U->getUser())
690 << "\n");
691 assert(DT.dominates(cast<Instruction>(Result.Def), *VD.U) &&
692 "Predicateinfo def should have dominated this use");
693 VD.U->set(Result.Def);
694 }
695 }
696}
697
698PredicateInfoBuilder::ValueInfo &
699PredicateInfoBuilder::getOrCreateValueInfo(Value *Operand) {
700 auto Res = ValueInfoNums.try_emplace(Operand, ValueInfos.size());
701 if (Res.second) {
702 // Allocate space for new ValueInfo.
703 ValueInfos.resize(ValueInfos.size() + 1);
704 }
705 return ValueInfos[Res.first->second];
706}
707
708const PredicateInfoBuilder::ValueInfo &
709PredicateInfoBuilder::getValueInfo(Value *Operand) const {
710 auto OINI = ValueInfoNums.lookup(Operand);
711 assert(OINI != 0 && "Operand was not really in the Value Info Numbers");
712 assert(OINI < ValueInfos.size() &&
713 "Value Info Number greater than size of Value Info Table");
714 return ValueInfos[OINI];
715}
716
718 AssumptionCache &AC, BumpPtrAllocator &Allocator)
719 : F(F) {
720 PredicateInfoBuilder Builder(*this, F, DT, AC, Allocator);
721 Builder.buildPredicateInfo();
722}
723
724std::optional<PredicateConstraint> PredicateBase::getConstraint() const {
725 switch (Type) {
726 case PT_BundleAssume: {
727 assert(cast<PredicateBundleAssume>(this)->AttrKind == Attribute::NonNull &&
728 "Cannot handle anything other than NonNull");
730 cast<PointerType>(OriginalOp->getType()))}};
731 }
732
734 case PT_Branch: {
735 bool TrueEdge = true;
736 if (auto *PBranch = dyn_cast<PredicateBranch>(this))
737 TrueEdge = PBranch->TrueEdge;
738
739 if (Condition == RenamedOp) {
740 return {{CmpInst::ICMP_EQ,
741 TrueEdge ? ConstantInt::getTrue(Condition->getType())
742 : ConstantInt::getFalse(Condition->getType())}};
743 }
744
746 return {{TrueEdge ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ,
748 }
749
751 if (!Cmp) {
752 // TODO: Make this an assertion once RenamedOp is fully accurate.
753 return std::nullopt;
754 }
755
757 Value *OtherOp;
758 if (Cmp->getOperand(0) == RenamedOp) {
759 Pred = Cmp->getPredicate();
760 OtherOp = Cmp->getOperand(1);
761 } else if (Cmp->getOperand(1) == RenamedOp) {
762 Pred = Cmp->getSwappedPredicate();
763 OtherOp = Cmp->getOperand(0);
764 } else {
765 // TODO: Make this an assertion once RenamedOp is fully accurate.
766 return std::nullopt;
767 }
768
769 // Invert predicate along false edge.
770 if (!TrueEdge)
771 Pred = CmpInst::getInversePredicate(Pred);
772
773 return {{Pred, OtherOp}};
774 }
775 case PT_Switch:
776 if (Condition != RenamedOp) {
777 // TODO: Make this an assertion once RenamedOp is fully accurate.
778 return std::nullopt;
779 }
780
781 return {{CmpInst::ICMP_EQ, cast<PredicateSwitch>(this)->CaseValue}};
782 }
783 llvm_unreachable("Unknown predicate type");
784}
785
787
788// Replace bitcasts created by PredicateInfo with their operand.
791 const auto *PI = PredInfo.getPredicateInfoFor(&Inst);
792 if (!PI)
793 continue;
794
795 assert(isa<BitCastInst>(Inst) &&
796 Inst.getType() == Inst.getOperand(0)->getType());
797 Inst.replaceAllUsesWith(Inst.getOperand(0));
798 Inst.eraseFromParent();
799 }
800}
801
804 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
805 auto &AC = AM.getResult<AssumptionAnalysis>(F);
806 OS << "PredicateInfo for function: " << F.getName() << "\n";
807 BumpPtrAllocator Allocator;
808 auto PredInfo = std::make_unique<PredicateInfo>(F, DT, AC, Allocator);
809 PredInfo->print(OS);
810
811 replaceCreatedSSACopys(*PredInfo, F);
812 return PreservedAnalyses::all();
813}
814
815/// An assembly annotator class to print PredicateInfo information in
816/// comments.
818 friend class PredicateInfo;
819 const PredicateInfo *PredInfo;
820
821public:
823
825 formatted_raw_ostream &OS) override {}
826
828 formatted_raw_ostream &OS) override {
829 if (const auto *PI = PredInfo->getPredicateInfoFor(I)) {
830 if (const auto *PB = dyn_cast<PredicateBranch>(PI)) {
831 OS << "; branch predicate info { TrueEdge: " << PB->TrueEdge
832 << " Comparison:" << *PB->Condition << " Edge: [";
833 PB->From->printAsOperand(OS);
834 OS << ",";
835 PB->To->printAsOperand(OS);
836 OS << "]";
837 } else if (const auto *PS = dyn_cast<PredicateSwitch>(PI)) {
838 OS << "; switch predicate info { CaseValue: " << *PS->CaseValue
839 << " Edge: [";
840 PS->From->printAsOperand(OS);
841 OS << ",";
842 PS->To->printAsOperand(OS);
843 OS << "]";
844 } else if (const auto *PA = dyn_cast<PredicateAssume>(PI)) {
845 OS << "; assume predicate info {";
846 if (auto *PBA = dyn_cast<PredicateBundleAssume>(PA)) {
847 OS << " Attribute: " << Attribute::getNameFromAttrKind(PBA->AttrKind);
848 } else {
850 OS << " Comparison:" << *PA->Condition;
851 }
852 }
853 OS << ", RenamedOp: ";
854 PI->RenamedOp->printAsOperand(OS, false);
855 OS << " }\n";
856 }
857 }
858};
859
861 PredicateInfoAnnotatedWriter Writer(this);
862 F.print(OS, &Writer);
863}
864
866 PredicateInfoAnnotatedWriter Writer(this);
867 F.print(dbgs(), &Writer);
868}
869
872 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
873 auto &AC = AM.getResult<AssumptionAnalysis>(F);
874 BumpPtrAllocator Allocator;
875 std::make_unique<PredicateInfo>(F, DT, AC, Allocator)->verifyPredicateInfo();
876
877 return PreservedAnalyses::all();
878}
879}
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.
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
Conditional Branch instruction.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
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:278
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
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:2116
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:634
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