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