LLVM 22.0.0git
SCCPSolver.cpp
Go to the documentation of this file.
1//===- SCCPSolver.cpp - SCCP Utility --------------------------- *- C++ -*-===//
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// \file
10// This file implements the Sparse Conditional Constant Propagation (SCCP)
11// utility.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/SetVector.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/InstVisitor.h"
26#include "llvm/IR/NoFolder.h"
29#include "llvm/Support/Debug.h"
33#include <cassert>
34#include <utility>
35#include <vector>
36
37using namespace llvm;
38using namespace PatternMatch;
39
40#define DEBUG_TYPE "sccp"
41
42// The maximum number of range extensions allowed for operations requiring
43// widening.
44static const unsigned MaxNumRangeExtensions = 10;
45
46/// Returns MergeOptions with MaxWidenSteps set to MaxNumRangeExtensions.
51
52namespace llvm {
53
55 return LV.isConstant() ||
57}
58
62
64 Constant *Const = getConstantOrNull(V);
65 if (!Const)
66 return false;
67 // Replacing `musttail` instructions with constant breaks `musttail` invariant
68 // unless the call itself can be removed.
69 // Calls with "clang.arc.attachedcall" implicitly use the return value and
70 // those uses cannot be updated with a constant.
72 if (CB && ((CB->isMustTailCall() && !wouldInstructionBeTriviallyDead(CB)) ||
75
76 // Don't zap returns of the callee
77 if (F)
79
80 LLVM_DEBUG(dbgs() << " Can\'t treat the result of call " << *CB
81 << " as a constant\n");
82 return false;
83 }
84
85 LLVM_DEBUG(dbgs() << " Constant: " << *Const << " = " << *V << '\n');
86
87 // Replaces all of the uses of a variable with uses of the constant.
88 V->replaceAllUsesWith(Const);
89 return true;
90}
91
92/// Helper for getting ranges from \p Solver. Instructions inserted during
93/// simplification are unavailable in the solver, so we return a full range for
94/// them.
96 const SmallPtrSetImpl<Value *> &InsertedValues) {
97 if (auto *Const = dyn_cast<Constant>(Op))
98 return Const->toConstantRange();
99 if (InsertedValues.contains(Op)) {
100 unsigned Bitwidth = Op->getType()->getScalarSizeInBits();
101 return ConstantRange::getFull(Bitwidth);
102 }
103 return Solver.getLatticeValueFor(Op).asConstantRange(Op->getType(),
104 /*UndefAllowed=*/false);
105}
106
107/// Try to use \p Inst's value range from \p Solver to infer the NUW flag.
108static bool refineInstruction(SCCPSolver &Solver,
109 const SmallPtrSetImpl<Value *> &InsertedValues,
110 Instruction &Inst) {
111 bool Changed = false;
112 auto GetRange = [&Solver, &InsertedValues](Value *Op) {
113 return getRange(Op, Solver, InsertedValues);
114 };
115
117 if (Inst.hasNoSignedWrap() && Inst.hasNoUnsignedWrap())
118 return false;
119
120 auto RangeA = GetRange(Inst.getOperand(0));
121 auto RangeB = GetRange(Inst.getOperand(1));
122 if (!Inst.hasNoUnsignedWrap()) {
124 Instruction::BinaryOps(Inst.getOpcode()), RangeB,
126 if (NUWRange.contains(RangeA)) {
128 Changed = true;
129 }
130 }
131 if (!Inst.hasNoSignedWrap()) {
133 Instruction::BinaryOps(Inst.getOpcode()), RangeB,
135 if (NSWRange.contains(RangeA)) {
136 Inst.setHasNoSignedWrap();
137 Changed = true;
138 }
139 }
140 } else if (isa<PossiblyNonNegInst>(Inst) && !Inst.hasNonNeg()) {
141 auto Range = GetRange(Inst.getOperand(0));
142 if (Range.isAllNonNegative()) {
143 Inst.setNonNeg();
144 Changed = true;
145 }
146 } else if (TruncInst *TI = dyn_cast<TruncInst>(&Inst)) {
147 if (TI->hasNoSignedWrap() && TI->hasNoUnsignedWrap())
148 return false;
149
150 auto Range = GetRange(Inst.getOperand(0));
151 uint64_t DestWidth = TI->getDestTy()->getScalarSizeInBits();
152 if (!TI->hasNoUnsignedWrap()) {
153 if (Range.getActiveBits() <= DestWidth) {
154 TI->setHasNoUnsignedWrap(true);
155 Changed = true;
156 }
157 }
158 if (!TI->hasNoSignedWrap()) {
159 if (Range.getMinSignedBits() <= DestWidth) {
160 TI->setHasNoSignedWrap(true);
161 Changed = true;
162 }
163 }
164 } else if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst)) {
165 if (GEP->hasNoUnsignedWrap() || !GEP->hasNoUnsignedSignedWrap())
166 return false;
167
168 if (all_of(GEP->indices(),
169 [&](Value *V) { return GetRange(V).isAllNonNegative(); })) {
170 GEP->setNoWrapFlags(GEP->getNoWrapFlags() |
172 Changed = true;
173 }
174 }
175
176 return Changed;
177}
178
179/// Try to replace signed instructions with their unsigned equivalent.
180static bool replaceSignedInst(SCCPSolver &Solver,
181 SmallPtrSetImpl<Value *> &InsertedValues,
182 Instruction &Inst) {
183 // Determine if a signed value is known to be >= 0.
184 auto isNonNegative = [&Solver, &InsertedValues](Value *V) {
185 return getRange(V, Solver, InsertedValues).isAllNonNegative();
186 };
187
188 Instruction *NewInst = nullptr;
189 switch (Inst.getOpcode()) {
190 case Instruction::SIToFP:
191 case Instruction::SExt: {
192 // If the source value is not negative, this is a zext/uitofp.
193 Value *Op0 = Inst.getOperand(0);
194 if (!isNonNegative(Op0))
195 return false;
196 NewInst = CastInst::Create(Inst.getOpcode() == Instruction::SExt
197 ? Instruction::ZExt
198 : Instruction::UIToFP,
199 Op0, Inst.getType(), "", Inst.getIterator());
200 NewInst->setNonNeg();
201 break;
202 }
203 case Instruction::AShr: {
204 // If the shifted value is not negative, this is a logical shift right.
205 Value *Op0 = Inst.getOperand(0);
206 if (!isNonNegative(Op0))
207 return false;
208 NewInst = BinaryOperator::CreateLShr(Op0, Inst.getOperand(1), "", Inst.getIterator());
209 NewInst->setIsExact(Inst.isExact());
210 break;
211 }
212 case Instruction::SDiv:
213 case Instruction::SRem: {
214 // If both operands are not negative, this is the same as udiv/urem.
215 Value *Op0 = Inst.getOperand(0), *Op1 = Inst.getOperand(1);
216 if (!isNonNegative(Op0) || !isNonNegative(Op1))
217 return false;
218 auto NewOpcode = Inst.getOpcode() == Instruction::SDiv ? Instruction::UDiv
219 : Instruction::URem;
220 NewInst = BinaryOperator::Create(NewOpcode, Op0, Op1, "", Inst.getIterator());
221 if (Inst.getOpcode() == Instruction::SDiv)
222 NewInst->setIsExact(Inst.isExact());
223 break;
224 }
225 default:
226 return false;
227 }
228
229 // Wire up the new instruction and update state.
230 assert(NewInst && "Expected replacement instruction");
231 NewInst->takeName(&Inst);
232 InsertedValues.insert(NewInst);
233 Inst.replaceAllUsesWith(NewInst);
234 NewInst->setDebugLoc(Inst.getDebugLoc());
235 Solver.removeLatticeValueFor(&Inst);
236 Inst.eraseFromParent();
237 return true;
238}
239
240/// Try to use \p Inst's value range from \p Solver to simplify it.
242 SmallPtrSetImpl<Value *> &InsertedValues,
243 Instruction &Inst) {
244 auto GetRange = [&Solver, &InsertedValues](Value *Op) {
245 return getRange(Op, Solver, InsertedValues);
246 };
247
248 Value *X;
249 const APInt *RHSC;
250 // Remove masking operations.
251 if (match(&Inst, m_And(m_Value(X), m_LowBitMask(RHSC)))) {
252 ConstantRange LRange = GetRange(X);
253 if (LRange.getUnsignedMax().ule(*RHSC))
254 return X;
255 }
256
257 // Check if we can simplify [us]cmp(X, Y) to X - Y.
258 if (auto *Cmp = dyn_cast<CmpIntrinsic>(&Inst)) {
259 Value *LHS = Cmp->getOperand(0);
260 Value *RHS = Cmp->getOperand(1);
261 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
262 // Bail out on 1-bit comparisons.
263 if (BitWidth == 1)
264 return nullptr;
265 ConstantRange LRange = GetRange(LHS);
266 if (LRange.isSizeLargerThan(3))
267 return nullptr;
268 ConstantRange RRange = GetRange(RHS);
269 if (RRange.isSizeLargerThan(3))
270 return nullptr;
271 ConstantRange RHSLower = RRange.sub(APInt(BitWidth, 1));
272 ConstantRange RHSUpper = RRange.add(APInt(BitWidth, 1));
274 Cmp->isSigned() ? CmpInst::ICMP_SLE : CmpInst::ICMP_ULE;
275 if (!RHSLower.icmp(Pred, LRange) || !LRange.icmp(Pred, RHSUpper))
276 return nullptr;
277
278 IRBuilder<NoFolder> Builder(&Inst);
279 Value *Sub = Builder.CreateSub(LHS, RHS, Inst.getName(), /*HasNUW=*/false,
280 /*HasNSW=*/Cmp->isSigned());
281 InsertedValues.insert(Sub);
282 if (Sub->getType() != Inst.getType()) {
283 Sub = Builder.CreateSExtOrTrunc(Sub, Inst.getType());
284 InsertedValues.insert(Sub);
285 }
286 return Sub;
287 }
288
289 // Relax range checks.
290 if (auto *ICmp = dyn_cast<ICmpInst>(&Inst)) {
291 Value *X;
292 auto MatchTwoInstructionExactRangeCheck =
293 [&]() -> std::optional<ConstantRange> {
294 const APInt *RHSC;
295 if (!match(ICmp->getOperand(1), m_APInt(RHSC)))
296 return std::nullopt;
297
298 Value *LHS = ICmp->getOperand(0);
299 ICmpInst::Predicate Pred = ICmp->getPredicate();
300 const APInt *Offset;
302 return ConstantRange::makeExactICmpRegion(Pred, *RHSC).sub(*Offset);
303 // Match icmp eq/ne X & NegPow2, C
304 if (ICmp->isEquality()) {
305 const APInt *Mask;
306 if (match(LHS, m_OneUse(m_And(m_Value(X), m_NegatedPower2(Mask)))) &&
307 RHSC->countr_zero() >= Mask->countr_zero()) {
308 ConstantRange CR(*RHSC, *RHSC - *Mask);
309 return Pred == ICmpInst::ICMP_EQ ? CR : CR.inverse();
310 }
311 }
312 return std::nullopt;
313 };
314
315 if (auto CR = MatchTwoInstructionExactRangeCheck()) {
316 ConstantRange LRange = GetRange(X);
317 // Early exit if we know nothing about X.
318 if (LRange.isFullSet())
319 return nullptr;
320 auto ConvertCRToICmp =
321 [&](const std::optional<ConstantRange> &NewCR) -> Value * {
323 APInt RHS;
324 // Check if we can represent NewCR as an icmp predicate.
325 if (NewCR && NewCR->getEquivalentICmp(Pred, RHS)) {
326 IRBuilder<NoFolder> Builder(&Inst);
327 Value *NewICmp =
328 Builder.CreateICmp(Pred, X, ConstantInt::get(X->getType(), RHS));
329 InsertedValues.insert(NewICmp);
330 return NewICmp;
331 }
332 return nullptr;
333 };
334 // We are allowed to refine the comparison to either true or false for out
335 // of range inputs.
336 // Here we refine the comparison to false, and check if we can narrow the
337 // range check to a simpler test.
338 if (auto *V = ConvertCRToICmp(CR->exactIntersectWith(LRange)))
339 return V;
340 // Here we refine the comparison to true, i.e. we relax the range check.
341 if (auto *V = ConvertCRToICmp(CR->exactUnionWith(LRange.inverse())))
342 return V;
343 }
344 }
345
346 return nullptr;
347}
348
350 SmallPtrSetImpl<Value *> &InsertedValues,
351 Statistic &InstRemovedStat,
352 Statistic &InstReplacedStat) {
353 bool MadeChanges = false;
354 for (Instruction &Inst : make_early_inc_range(BB)) {
355 if (Inst.getType()->isVoidTy())
356 continue;
357 if (tryToReplaceWithConstant(&Inst)) {
359 Inst.eraseFromParent();
360
361 MadeChanges = true;
362 ++InstRemovedStat;
363 } else if (replaceSignedInst(*this, InsertedValues, Inst)) {
364 MadeChanges = true;
365 ++InstReplacedStat;
366 } else if (refineInstruction(*this, InsertedValues, Inst)) {
367 MadeChanges = true;
368 } else if (auto *V = simplifyInstruction(*this, InsertedValues, Inst)) {
369 Inst.replaceAllUsesWith(V);
370 Inst.eraseFromParent();
371 ++InstRemovedStat;
372 MadeChanges = true;
373 }
374 }
375 return MadeChanges;
376}
377
379 BasicBlock *&NewUnreachableBB) const {
380 SmallPtrSet<BasicBlock *, 8> FeasibleSuccessors;
381 bool HasNonFeasibleEdges = false;
382 for (BasicBlock *Succ : successors(BB)) {
383 if (isEdgeFeasible(BB, Succ))
384 FeasibleSuccessors.insert(Succ);
385 else
386 HasNonFeasibleEdges = true;
387 }
388
389 // All edges feasible, nothing to do.
390 if (!HasNonFeasibleEdges)
391 return false;
392
393 // SCCP can only determine non-feasible edges for br, switch and indirectbr.
394 Instruction *TI = BB->getTerminator();
396 isa<IndirectBrInst>(TI)) &&
397 "Terminator must be a br, switch or indirectbr");
398
399 if (FeasibleSuccessors.size() == 0) {
400 // Branch on undef/poison, replace with unreachable.
403 for (BasicBlock *Succ : successors(BB)) {
404 Succ->removePredecessor(BB);
405 if (SeenSuccs.insert(Succ).second)
406 Updates.push_back({DominatorTree::Delete, BB, Succ});
407 }
408 TI->eraseFromParent();
409 new UnreachableInst(BB->getContext(), BB);
410 DTU.applyUpdatesPermissive(Updates);
411 } else if (FeasibleSuccessors.size() == 1) {
412 // Replace with an unconditional branch to the only feasible successor.
413 BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
415 bool HaveSeenOnlyFeasibleSuccessor = false;
416 for (BasicBlock *Succ : successors(BB)) {
417 if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
418 // Don't remove the edge to the only feasible successor the first time
419 // we see it. We still do need to remove any multi-edges to it though.
420 HaveSeenOnlyFeasibleSuccessor = true;
421 continue;
422 }
423
424 Succ->removePredecessor(BB);
425 Updates.push_back({DominatorTree::Delete, BB, Succ});
426 }
427
428 Instruction *BI = BranchInst::Create(OnlyFeasibleSuccessor, BB);
429 BI->setDebugLoc(TI->getDebugLoc());
430 TI->eraseFromParent();
431 DTU.applyUpdatesPermissive(Updates);
432 } else if (FeasibleSuccessors.size() > 1) {
435
436 // If the default destination is unfeasible it will never be taken. Replace
437 // it with a new block with a single Unreachable instruction.
438 BasicBlock *DefaultDest = SI->getDefaultDest();
439 if (!FeasibleSuccessors.contains(DefaultDest)) {
440 if (!NewUnreachableBB) {
441 NewUnreachableBB =
442 BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
443 DefaultDest->getParent(), DefaultDest);
444 auto *UI =
445 new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
446 UI->setDebugLoc(DebugLoc::getTemporary());
447 }
448
449 DefaultDest->removePredecessor(BB);
450 SI->setDefaultDest(NewUnreachableBB);
451 Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
452 Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
453 }
454
455 for (auto CI = SI->case_begin(); CI != SI->case_end();) {
456 if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
457 ++CI;
458 continue;
459 }
460
461 BasicBlock *Succ = CI->getCaseSuccessor();
462 Succ->removePredecessor(BB);
463 Updates.push_back({DominatorTree::Delete, BB, Succ});
464 SI.removeCase(CI);
465 // Don't increment CI, as we removed a case.
466 }
467
468 DTU.applyUpdatesPermissive(Updates);
469 } else {
470 llvm_unreachable("Must have at least one feasible successor");
471 }
472 return true;
473}
474
475static void inferAttribute(Function *F, unsigned AttrIndex,
476 const ValueLatticeElement &Val) {
477 // If there is a known constant range for the value, add range attribute.
478 if (Val.isConstantRange() && !Val.getConstantRange().isSingleElement()) {
479 // Do not add range attribute if the value may include undef.
481 return;
482
483 // Take the intersection of the existing attribute and the inferred range.
484 Attribute OldAttr = F->getAttributeAtIndex(AttrIndex, Attribute::Range);
486 if (OldAttr.isValid())
487 CR = CR.intersectWith(OldAttr.getRange());
488 F->addAttributeAtIndex(
489 AttrIndex, Attribute::get(F->getContext(), Attribute::Range, CR));
490 return;
491 }
492 // Infer nonnull attribute.
493 if (Val.isNotConstant() && Val.getNotConstant()->getType()->isPointerTy() &&
494 Val.getNotConstant()->isNullValue() &&
495 !F->hasAttributeAtIndex(AttrIndex, Attribute::NonNull)) {
496 F->addAttributeAtIndex(AttrIndex,
497 Attribute::get(F->getContext(), Attribute::NonNull));
498 }
499}
500
502 for (const auto &[F, ReturnValue] : getTrackedRetVals())
503 inferAttribute(F, AttributeList::ReturnIndex, ReturnValue);
504}
505
508 if (!isBlockExecutable(&F->front()))
509 continue;
510 for (Argument &A : F->args())
511 if (!A.getType()->isStructTy())
512 inferAttribute(F, AttributeList::FirstArgIndex + A.getArgNo(),
514 }
515}
516
517/// Helper class for SCCPSolver. This implements the instruction visitor and
518/// holds all the state.
519class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
520 const DataLayout &DL;
521 std::function<const TargetLibraryInfo &(Function &)> GetTLI;
522 /// Basic blocks that are executable (but may not have been visited yet).
523 SmallPtrSet<BasicBlock *, 8> BBExecutable;
524 /// Basic blocks that are executable and have been visited at least once.
527 ValueState; // The state each value is in.
528
529 /// StructValueState - This maintains ValueState for values that have
530 /// StructType, for example for formal arguments, calls, insertelement, etc.
532
533 /// GlobalValue - If we are tracking any values for the contents of a global
534 /// variable, we keep a mapping from the constant accessor to the element of
535 /// the global, to the currently known value. If the value becomes
536 /// overdefined, it's entry is simply removed from this map.
538
539 /// TrackedRetVals - If we are tracking arguments into and the return
540 /// value out of a function, it will have an entry in this map, indicating
541 /// what the known return value for the function is.
543
544 /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
545 /// that return multiple values.
547 TrackedMultipleRetVals;
548
549 /// The set of values whose lattice has been invalidated.
550 /// Populated by resetLatticeValueFor(), cleared after resolving undefs.
551 DenseSet<Value *> Invalidated;
552
553 /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
554 /// represented here for efficient lookup.
555 SmallPtrSet<Function *, 16> MRVFunctionsTracked;
556
557 /// A list of functions whose return cannot be modified.
558 SmallPtrSet<Function *, 16> MustPreserveReturnsInFunctions;
559
560 /// TrackingIncomingArguments - This is the set of functions for whose
561 /// arguments we make optimistic assumptions about and try to prove as
562 /// constants.
563 SmallPtrSet<Function *, 16> TrackingIncomingArguments;
564
565 /// Worklist of instructions to re-visit. This only includes instructions
566 /// in blocks that have already been visited at least once.
568
569 /// Current instruction while visiting a block for the first time, used to
570 /// avoid unnecessary instruction worklist insertions. Null if an instruction
571 /// is visited outside a whole-block visitation.
572 Instruction *CurI = nullptr;
573
574 // The BasicBlock work list
576
577 /// KnownFeasibleEdges - Entries in this set are edges which have already had
578 /// PHI nodes retriggered.
579 using Edge = std::pair<BasicBlock *, BasicBlock *>;
580 DenseSet<Edge> KnownFeasibleEdges;
581
583
585
586 LLVMContext &Ctx;
587
588 BumpPtrAllocator PredicateInfoAllocator;
589
590private:
591 ConstantInt *getConstantInt(const ValueLatticeElement &IV, Type *Ty) const {
593 }
594
595 /// Push instruction \p I to the worklist.
596 void pushToWorkList(Instruction *I);
597
598 /// Push users of value \p V to the worklist.
599 void pushUsersToWorkList(Value *V);
600
601 /// Like pushUsersToWorkList(), but also prints a debug message with the
602 /// updated value.
603 void pushUsersToWorkListMsg(ValueLatticeElement &IV, Value *V);
604
605 // markConstant - Make a value be marked as "constant". If the value
606 // is not already a constant, add it to the instruction work list so that
607 // the users of the instruction are updated later.
608 bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C,
609 bool MayIncludeUndef = false);
610
611 bool markConstant(Value *V, Constant *C) {
612 assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
613 return markConstant(ValueState[V], V, C);
614 }
615
616 bool markNotConstant(ValueLatticeElement &IV, Value *V, Constant *C);
617
618 bool markNotNull(ValueLatticeElement &IV, Value *V) {
619 return markNotConstant(IV, V, Constant::getNullValue(V->getType()));
620 }
621
622 /// markConstantRange - Mark the object as constant range with \p CR. If the
623 /// object is not a constant range with the range \p CR, add it to the
624 /// instruction work list so that the users of the instruction are updated
625 /// later.
626 bool markConstantRange(ValueLatticeElement &IV, Value *V,
627 const ConstantRange &CR);
628
629 // markOverdefined - Make a value be marked as "overdefined". If the
630 // value is not already overdefined, add it to the overdefined instruction
631 // work list so that the users of the instruction are updated later.
632 bool markOverdefined(ValueLatticeElement &IV, Value *V);
633
634 /// Merge \p MergeWithV into \p IV and push \p V to the worklist, if \p IV
635 /// changes.
636 bool mergeInValue(ValueLatticeElement &IV, Value *V,
637 ValueLatticeElement MergeWithV,
639 /*MayIncludeUndef=*/false, /*CheckWiden=*/false});
640
641 bool mergeInValue(Value *V, ValueLatticeElement MergeWithV,
643 /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
644 assert(!V->getType()->isStructTy() &&
645 "non-structs should use markConstant");
646 return mergeInValue(ValueState[V], V, MergeWithV, Opts);
647 }
648
649 /// getValueState - Return the ValueLatticeElement object that corresponds to
650 /// the value. This function handles the case when the value hasn't been seen
651 /// yet by properly seeding constants etc.
652 ValueLatticeElement &getValueState(Value *V) {
653 assert(!V->getType()->isStructTy() && "Should use getStructValueState");
654
655 auto I = ValueState.try_emplace(V);
656 ValueLatticeElement &LV = I.first->second;
657
658 if (!I.second)
659 return LV; // Common case, already in the map.
660
661 if (auto *C = dyn_cast<Constant>(V))
662 LV.markConstant(C); // Constants are constant
663
664 // All others are unknown by default.
665 return LV;
666 }
667
668 /// getStructValueState - Return the ValueLatticeElement object that
669 /// corresponds to the value/field pair. This function handles the case when
670 /// the value hasn't been seen yet by properly seeding constants etc.
671 ValueLatticeElement &getStructValueState(Value *V, unsigned i) {
672 assert(V->getType()->isStructTy() && "Should use getValueState");
673 assert(i < cast<StructType>(V->getType())->getNumElements() &&
674 "Invalid element #");
675
676 auto I = StructValueState.insert(
677 std::make_pair(std::make_pair(V, i), ValueLatticeElement()));
678 ValueLatticeElement &LV = I.first->second;
679
680 if (!I.second)
681 return LV; // Common case, already in the map.
682
683 if (auto *C = dyn_cast<Constant>(V)) {
684 Constant *Elt = C->getAggregateElement(i);
685
686 if (!Elt)
687 LV.markOverdefined(); // Unknown sort of constant.
688 else
689 LV.markConstant(Elt); // Constants are constant.
690 }
691
692 // All others are underdefined by default.
693 return LV;
694 }
695
696 /// Traverse the use-def chain of \p Call, marking itself and its users as
697 /// "unknown" on the way.
698 void invalidate(CallBase *Call) {
700 ToInvalidate.push_back(Call);
701
702 while (!ToInvalidate.empty()) {
703 Instruction *Inst = ToInvalidate.pop_back_val();
704
705 if (!Invalidated.insert(Inst).second)
706 continue;
707
708 if (!BBExecutable.count(Inst->getParent()))
709 continue;
710
711 Value *V = nullptr;
712 // For return instructions we need to invalidate the tracked returns map.
713 // Anything else has its lattice in the value map.
714 if (auto *RetInst = dyn_cast<ReturnInst>(Inst)) {
715 Function *F = RetInst->getParent()->getParent();
716 if (auto It = TrackedRetVals.find(F); It != TrackedRetVals.end()) {
717 It->second = ValueLatticeElement();
718 V = F;
719 } else if (MRVFunctionsTracked.count(F)) {
720 auto *STy = cast<StructType>(F->getReturnType());
721 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I)
722 TrackedMultipleRetVals[{F, I}] = ValueLatticeElement();
723 V = F;
724 }
725 } else if (auto *STy = dyn_cast<StructType>(Inst->getType())) {
726 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
727 if (auto It = StructValueState.find({Inst, I});
728 It != StructValueState.end()) {
729 It->second = ValueLatticeElement();
730 V = Inst;
731 }
732 }
733 } else if (auto It = ValueState.find(Inst); It != ValueState.end()) {
734 It->second = ValueLatticeElement();
735 V = Inst;
736 }
737
738 if (V) {
739 LLVM_DEBUG(dbgs() << "Invalidated lattice for " << *V << "\n");
740
741 for (User *U : V->users())
742 if (auto *UI = dyn_cast<Instruction>(U))
743 ToInvalidate.push_back(UI);
744
745 auto It = AdditionalUsers.find(V);
746 if (It != AdditionalUsers.end())
747 for (User *U : It->second)
748 if (auto *UI = dyn_cast<Instruction>(U))
749 ToInvalidate.push_back(UI);
750 }
751 }
752 }
753
754 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
755 /// work list if it is not already executable.
756 bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
757
758 // getFeasibleSuccessors - Return a vector of booleans to indicate which
759 // successors are reachable from a given terminator instruction.
760 void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs);
761
762 // Add U as additional user of V.
763 void addAdditionalUser(Value *V, User *U) { AdditionalUsers[V].insert(U); }
764
765 void handlePredicate(Instruction *I, Value *CopyOf, const PredicateBase *PI);
766 void handleCallOverdefined(CallBase &CB);
767 void handleCallResult(CallBase &CB);
768 void handleCallArguments(CallBase &CB);
769 void handleExtractOfWithOverflow(ExtractValueInst &EVI,
770 const WithOverflowInst *WO, unsigned Idx);
771
772private:
773 friend class InstVisitor<SCCPInstVisitor>;
774
775 // visit implementations - Something changed in this instruction. Either an
776 // operand made a transition, or the instruction is newly executable. Change
777 // the value type of I to reflect these changes if appropriate.
778 void visitPHINode(PHINode &I);
779
780 // Terminators
781
782 void visitReturnInst(ReturnInst &I);
783 void visitTerminator(Instruction &TI);
784
785 void visitCastInst(CastInst &I);
786 void visitSelectInst(SelectInst &I);
787 void visitUnaryOperator(Instruction &I);
788 void visitFreezeInst(FreezeInst &I);
789 void visitBinaryOperator(Instruction &I);
790 void visitCmpInst(CmpInst &I);
791 void visitExtractValueInst(ExtractValueInst &EVI);
792 void visitInsertValueInst(InsertValueInst &IVI);
793
794 void visitCatchSwitchInst(CatchSwitchInst &CPI) {
795 markOverdefined(&CPI);
796 visitTerminator(CPI);
797 }
798
799 // Instructions that cannot be folded away.
800
801 void visitStoreInst(StoreInst &I);
802 void visitLoadInst(LoadInst &I);
803 void visitGetElementPtrInst(GetElementPtrInst &I);
804 void visitAllocaInst(AllocaInst &AI);
805
806 void visitInvokeInst(InvokeInst &II) {
807 visitCallBase(II);
808 visitTerminator(II);
809 }
810
811 void visitCallBrInst(CallBrInst &CBI) {
812 visitCallBase(CBI);
813 visitTerminator(CBI);
814 }
815
816 void visitCallBase(CallBase &CB);
817 void visitResumeInst(ResumeInst &I) { /*returns void*/
818 }
819 void visitUnreachableInst(UnreachableInst &I) { /*returns void*/
820 }
821 void visitFenceInst(FenceInst &I) { /*returns void*/
822 }
823
824 void visitInstruction(Instruction &I);
825
826public:
828 FnPredicateInfo.insert({&F, std::make_unique<PredicateInfo>(
829 F, DT, AC, PredicateInfoAllocator)});
830 }
831
833 auto It = FnPredicateInfo.find(&F);
834 if (It == FnPredicateInfo.end())
835 return;
836
837 for (BasicBlock &BB : F) {
838 for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
839 if (auto *BC = dyn_cast<BitCastInst>(&Inst)) {
840 if (BC->getType() == BC->getOperand(0)->getType()) {
841 if (It->second->getPredicateInfoFor(&Inst)) {
842 Value *Op = BC->getOperand(0);
843 Inst.replaceAllUsesWith(Op);
844 Inst.eraseFromParent();
845 }
846 }
847 }
848 }
849 }
850 }
851
852 void visitCallInst(CallInst &I) { visitCallBase(I); }
853
855
857 auto It = FnPredicateInfo.find(I->getParent()->getParent());
858 if (It == FnPredicateInfo.end())
859 return nullptr;
860 return It->second->getPredicateInfoFor(I);
861 }
862
864 std::function<const TargetLibraryInfo &(Function &)> GetTLI,
865 LLVMContext &Ctx)
866 : DL(DL), GetTLI(GetTLI), Ctx(Ctx) {}
867
869 // We only track the contents of scalar globals.
870 if (GV->getValueType()->isSingleValueType()) {
871 ValueLatticeElement &IV = TrackedGlobals[GV];
872 IV.markConstant(GV->getInitializer());
873 }
874 }
875
877 // Add an entry, F -> undef.
878 if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
879 MRVFunctionsTracked.insert(F);
880 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
881 TrackedMultipleRetVals.try_emplace(std::make_pair(F, i));
882 } else if (!F->getReturnType()->isVoidTy())
883 TrackedRetVals.try_emplace(F);
884 }
885
887 MustPreserveReturnsInFunctions.insert(F);
888 }
889
891 return MustPreserveReturnsInFunctions.count(F);
892 }
893
895 TrackingIncomingArguments.insert(F);
896 }
897
899 return TrackingIncomingArguments.count(F);
900 }
901
903 return TrackingIncomingArguments;
904 }
905
906 void solve();
907
909
911
913 return BBExecutable.count(BB);
914 }
915
916 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
917
918 std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const {
919 std::vector<ValueLatticeElement> StructValues;
920 auto *STy = dyn_cast<StructType>(V->getType());
921 assert(STy && "getStructLatticeValueFor() can be called only on structs");
922 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
923 auto I = StructValueState.find(std::make_pair(V, i));
924 assert(I != StructValueState.end() && "Value not in valuemap!");
925 StructValues.push_back(I->second);
926 }
927 return StructValues;
928 }
929
930 void removeLatticeValueFor(Value *V) { ValueState.erase(V); }
931
932 /// Invalidate the Lattice Value of \p Call and its users after specializing
933 /// the call. Then recompute it.
935 // Calls to void returning functions do not need invalidation.
936 Function *F = Call->getCalledFunction();
937 (void)F;
938 assert(!F->getReturnType()->isVoidTy() &&
939 (TrackedRetVals.count(F) || MRVFunctionsTracked.count(F)) &&
940 "All non void specializations should be tracked");
941 invalidate(Call);
942 handleCallResult(*Call);
943 }
944
946 assert(!V->getType()->isStructTy() &&
947 "Should use getStructLatticeValueFor");
949 ValueState.find(V);
950 assert(I != ValueState.end() &&
951 "V not found in ValueState nor Paramstate map!");
952 return I->second;
953 }
954
956 return TrackedRetVals;
957 }
958
961 return TrackedGlobals;
962 }
963
965 return MRVFunctionsTracked;
966 }
967
969 if (auto *STy = dyn_cast<StructType>(V->getType()))
970 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
971 markOverdefined(getStructValueState(V, i), V);
972 else
973 markOverdefined(ValueState[V], V);
974 }
975
977 if (A->getType()->isIntOrIntVectorTy()) {
978 if (std::optional<ConstantRange> Range = A->getRange())
980 }
981 if (A->hasNonNullAttr())
983 // Assume nothing about the incoming arguments without attributes.
985 }
986
988 if (A->getType()->isStructTy())
989 return (void)markOverdefined(A);
990 mergeInValue(A, getArgAttributeVL(A));
991 }
992
994
995 Constant *getConstant(const ValueLatticeElement &LV, Type *Ty) const;
996
998
1000 const SmallVectorImpl<ArgInfo> &Args);
1001
1003 for (auto &BB : *F)
1004 BBExecutable.erase(&BB);
1005 }
1006
1008 bool ResolvedUndefs = true;
1009 while (ResolvedUndefs) {
1010 solve();
1011 ResolvedUndefs = false;
1012 for (Function &F : M)
1013 ResolvedUndefs |= resolvedUndefsIn(F);
1014 }
1015 }
1016
1018 bool ResolvedUndefs = true;
1019 while (ResolvedUndefs) {
1020 solve();
1021 ResolvedUndefs = false;
1022 for (Function *F : WorkList)
1023 ResolvedUndefs |= resolvedUndefsIn(*F);
1024 }
1025 }
1026
1028 bool ResolvedUndefs = true;
1029 while (ResolvedUndefs) {
1030 solve();
1031 ResolvedUndefs = false;
1032 for (Value *V : Invalidated)
1033 if (auto *I = dyn_cast<Instruction>(V))
1034 ResolvedUndefs |= resolvedUndef(*I);
1035 }
1036 Invalidated.clear();
1037 }
1038};
1039
1040} // namespace llvm
1041
1043 if (!BBExecutable.insert(BB).second)
1044 return false;
1045 LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
1046 BBWorkList.push_back(BB); // Add the block to the work list!
1047 return true;
1048}
1049
1050void SCCPInstVisitor::pushToWorkList(Instruction *I) {
1051 // If we're currently visiting a block, do not push any instructions in the
1052 // same blocks that are after the current one, as they will be visited
1053 // anyway. We do have to push updates to earlier instructions (e.g. phi
1054 // nodes or loads of tracked globals).
1055 if (CurI && I->getParent() == CurI->getParent() && !I->comesBefore(CurI))
1056 return;
1057 // Only push instructions in already visited blocks. Otherwise we'll handle
1058 // it when we visit the block for the first time.
1059 if (BBVisited.contains(I->getParent()))
1060 InstWorkList.insert(I);
1061}
1062
1063void SCCPInstVisitor::pushUsersToWorkList(Value *V) {
1064 for (User *U : V->users())
1065 if (auto *UI = dyn_cast<Instruction>(U))
1066 pushToWorkList(UI);
1067
1068 auto Iter = AdditionalUsers.find(V);
1069 if (Iter != AdditionalUsers.end()) {
1070 // Copy additional users before notifying them of changes, because new
1071 // users may be added, potentially invalidating the iterator.
1073 for (User *U : Iter->second)
1074 if (auto *UI = dyn_cast<Instruction>(U))
1075 ToNotify.push_back(UI);
1076 for (Instruction *UI : ToNotify)
1077 pushToWorkList(UI);
1078 }
1079}
1080
1081void SCCPInstVisitor::pushUsersToWorkListMsg(ValueLatticeElement &IV,
1082 Value *V) {
1083 LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n');
1084 pushUsersToWorkList(V);
1085}
1086
1087bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V,
1088 Constant *C, bool MayIncludeUndef) {
1089 if (!IV.markConstant(C, MayIncludeUndef))
1090 return false;
1091 LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
1092 pushUsersToWorkList(V);
1093 return true;
1094}
1095
1096bool SCCPInstVisitor::markNotConstant(ValueLatticeElement &IV, Value *V,
1097 Constant *C) {
1098 if (!IV.markNotConstant(C))
1099 return false;
1100 LLVM_DEBUG(dbgs() << "markNotConstant: " << *C << ": " << *V << '\n');
1101 pushUsersToWorkList(V);
1102 return true;
1103}
1104
1105bool SCCPInstVisitor::markConstantRange(ValueLatticeElement &IV, Value *V,
1106 const ConstantRange &CR) {
1107 if (!IV.markConstantRange(CR))
1108 return false;
1109 LLVM_DEBUG(dbgs() << "markConstantRange: " << CR << ": " << *V << '\n');
1110 pushUsersToWorkList(V);
1111 return true;
1112}
1113
1114bool SCCPInstVisitor::markOverdefined(ValueLatticeElement &IV, Value *V) {
1115 if (!IV.markOverdefined())
1116 return false;
1117
1118 LLVM_DEBUG(dbgs() << "markOverdefined: ";
1119 if (auto *F = dyn_cast<Function>(V)) dbgs()
1120 << "Function '" << F->getName() << "'\n";
1121 else dbgs() << *V << '\n');
1122 // Only instructions go on the work list
1123 pushUsersToWorkList(V);
1124 return true;
1125}
1126
1128 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1129 const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
1130 assert(It != TrackedMultipleRetVals.end());
1131 ValueLatticeElement LV = It->second;
1132 if (!SCCPSolver::isConstant(LV))
1133 return false;
1134 }
1135 return true;
1136}
1137
1139 Type *Ty) const {
1140 if (LV.isConstant()) {
1141 Constant *C = LV.getConstant();
1142 assert(C->getType() == Ty && "Type mismatch");
1143 return C;
1144 }
1145
1146 if (LV.isConstantRange()) {
1147 const auto &CR = LV.getConstantRange();
1148 if (CR.getSingleElement())
1149 return ConstantInt::get(Ty, *CR.getSingleElement());
1150 }
1151 return nullptr;
1152}
1153
1155 Constant *Const = nullptr;
1156 if (V->getType()->isStructTy()) {
1157 std::vector<ValueLatticeElement> LVs = getStructLatticeValueFor(V);
1159 return nullptr;
1160 std::vector<Constant *> ConstVals;
1161 auto *ST = cast<StructType>(V->getType());
1162 for (unsigned I = 0, E = ST->getNumElements(); I != E; ++I) {
1163 ValueLatticeElement LV = LVs[I];
1164 ConstVals.push_back(SCCPSolver::isConstant(LV)
1165 ? getConstant(LV, ST->getElementType(I))
1166 : UndefValue::get(ST->getElementType(I)));
1167 }
1168 Const = ConstantStruct::get(ST, ConstVals);
1169 } else {
1172 return nullptr;
1173 Const = SCCPSolver::isConstant(LV) ? getConstant(LV, V->getType())
1174 : UndefValue::get(V->getType());
1175 }
1176 assert(Const && "Constant is nullptr here!");
1177 return Const;
1178}
1179
1181 const SmallVectorImpl<ArgInfo> &Args) {
1182 assert(!Args.empty() && "Specialization without arguments");
1183 assert(F->arg_size() == Args[0].Formal->getParent()->arg_size() &&
1184 "Functions should have the same number of arguments");
1185
1186 auto Iter = Args.begin();
1187 Function::arg_iterator NewArg = F->arg_begin();
1188 Function::arg_iterator OldArg = Args[0].Formal->getParent()->arg_begin();
1189 for (auto End = F->arg_end(); NewArg != End; ++NewArg, ++OldArg) {
1190
1191 LLVM_DEBUG(dbgs() << "SCCP: Marking argument "
1192 << NewArg->getNameOrAsOperand() << "\n");
1193
1194 // Mark the argument constants in the new function
1195 // or copy the lattice state over from the old function.
1196 if (Iter != Args.end() && Iter->Formal == &*OldArg) {
1197 if (auto *STy = dyn_cast<StructType>(NewArg->getType())) {
1198 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
1199 ValueLatticeElement &NewValue = StructValueState[{&*NewArg, I}];
1200 NewValue.markConstant(Iter->Actual->getAggregateElement(I));
1201 }
1202 } else {
1203 ValueState[&*NewArg].markConstant(Iter->Actual);
1204 }
1205 ++Iter;
1206 } else {
1207 if (auto *STy = dyn_cast<StructType>(NewArg->getType())) {
1208 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
1209 ValueLatticeElement &NewValue = StructValueState[{&*NewArg, I}];
1210 NewValue = StructValueState[{&*OldArg, I}];
1211 }
1212 } else {
1213 ValueLatticeElement &NewValue = ValueState[&*NewArg];
1214 NewValue = ValueState[&*OldArg];
1215 }
1216 }
1217 }
1218}
1219
1220void SCCPInstVisitor::visitInstruction(Instruction &I) {
1221 // All the instructions we don't do any special handling for just
1222 // go to overdefined.
1223 LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
1224 markOverdefined(&I);
1225}
1226
1227bool SCCPInstVisitor::mergeInValue(ValueLatticeElement &IV, Value *V,
1228 ValueLatticeElement MergeWithV,
1230 if (IV.mergeIn(MergeWithV, Opts)) {
1231 pushUsersToWorkList(V);
1232 LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : "
1233 << IV << "\n");
1234 return true;
1235 }
1236 return false;
1237}
1238
1239bool SCCPInstVisitor::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
1240 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
1241 return false; // This edge is already known to be executable!
1242
1243 if (!markBlockExecutable(Dest)) {
1244 // If the destination is already executable, we just made an *edge*
1245 // feasible that wasn't before. Revisit the PHI nodes in the block
1246 // because they have potentially new operands.
1247 LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
1248 << " -> " << Dest->getName() << '\n');
1249
1250 for (PHINode &PN : Dest->phis())
1251 pushToWorkList(&PN);
1252 }
1253 return true;
1254}
1255
1256// getFeasibleSuccessors - Return a vector of booleans to indicate which
1257// successors are reachable from a given terminator instruction.
1258void SCCPInstVisitor::getFeasibleSuccessors(Instruction &TI,
1259 SmallVectorImpl<bool> &Succs) {
1260 Succs.resize(TI.getNumSuccessors());
1261 if (auto *BI = dyn_cast<BranchInst>(&TI)) {
1262 if (BI->isUnconditional()) {
1263 Succs[0] = true;
1264 return;
1265 }
1266
1267 ValueLatticeElement BCValue = getValueState(BI->getCondition());
1268 ConstantInt *CI = getConstantInt(BCValue, BI->getCondition()->getType());
1269 if (!CI) {
1270 // Overdefined condition variables, and branches on unfoldable constant
1271 // conditions, mean the branch could go either way.
1272 if (!BCValue.isUnknownOrUndef())
1273 Succs[0] = Succs[1] = true;
1274 return;
1275 }
1276
1277 // Constant condition variables mean the branch can only go a single way.
1278 Succs[CI->isZero()] = true;
1279 return;
1280 }
1281
1282 // We cannot analyze special terminators, so consider all successors
1283 // executable.
1284 if (TI.isSpecialTerminator()) {
1285 Succs.assign(TI.getNumSuccessors(), true);
1286 return;
1287 }
1288
1289 if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
1290 if (!SI->getNumCases()) {
1291 Succs[0] = true;
1292 return;
1293 }
1294 const ValueLatticeElement &SCValue = getValueState(SI->getCondition());
1295 if (ConstantInt *CI =
1296 getConstantInt(SCValue, SI->getCondition()->getType())) {
1297 Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true;
1298 return;
1299 }
1300
1301 // TODO: Switch on undef is UB. Stop passing false once the rest of LLVM
1302 // is ready.
1303 if (SCValue.isConstantRange(/*UndefAllowed=*/false)) {
1304 const ConstantRange &Range = SCValue.getConstantRange();
1305 unsigned ReachableCaseCount = 0;
1306 for (const auto &Case : SI->cases()) {
1307 const APInt &CaseValue = Case.getCaseValue()->getValue();
1308 if (Range.contains(CaseValue)) {
1309 Succs[Case.getSuccessorIndex()] = true;
1310 ++ReachableCaseCount;
1311 }
1312 }
1313
1314 Succs[SI->case_default()->getSuccessorIndex()] =
1315 Range.isSizeLargerThan(ReachableCaseCount);
1316 return;
1317 }
1318
1319 // Overdefined or unknown condition? All destinations are executable!
1320 if (!SCValue.isUnknownOrUndef())
1321 Succs.assign(TI.getNumSuccessors(), true);
1322 return;
1323 }
1324
1325 // In case of indirect branch and its address is a blockaddress, we mark
1326 // the target as executable.
1327 if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
1328 // Casts are folded by visitCastInst.
1329 ValueLatticeElement IBRValue = getValueState(IBR->getAddress());
1331 getConstant(IBRValue, IBR->getAddress()->getType()));
1332 if (!Addr) { // Overdefined or unknown condition?
1333 // All destinations are executable!
1334 if (!IBRValue.isUnknownOrUndef())
1335 Succs.assign(TI.getNumSuccessors(), true);
1336 return;
1337 }
1338
1339 BasicBlock *T = Addr->getBasicBlock();
1340 assert(Addr->getFunction() == T->getParent() &&
1341 "Block address of a different function ?");
1342 for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
1343 // This is the target.
1344 if (IBR->getDestination(i) == T) {
1345 Succs[i] = true;
1346 return;
1347 }
1348 }
1349
1350 // If we didn't find our destination in the IBR successor list, then we
1351 // have undefined behavior. Its ok to assume no successor is executable.
1352 return;
1353 }
1354
1355 LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
1356 llvm_unreachable("SCCP: Don't know how to handle this terminator!");
1357}
1358
1359// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
1360// block to the 'To' basic block is currently feasible.
1362 // Check if we've called markEdgeExecutable on the edge yet. (We could
1363 // be more aggressive and try to consider edges which haven't been marked
1364 // yet, but there isn't any need.)
1365 return KnownFeasibleEdges.count(Edge(From, To));
1366}
1367
1368// visit Implementations - Something changed in this instruction, either an
1369// operand made a transition, or the instruction is newly executable. Change
1370// the value type of I to reflect these changes if appropriate. This method
1371// makes sure to do the following actions:
1372//
1373// 1. If a phi node merges two constants in, and has conflicting value coming
1374// from different branches, or if the PHI node merges in an overdefined
1375// value, then the PHI node becomes overdefined.
1376// 2. If a phi node merges only constants in, and they all agree on value, the
1377// PHI node becomes a constant value equal to that.
1378// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
1379// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
1380// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
1381// 6. If a conditional branch has a value that is constant, make the selected
1382// destination executable
1383// 7. If a conditional branch has a value that is overdefined, make all
1384// successors executable.
1385void SCCPInstVisitor::visitPHINode(PHINode &PN) {
1386 // If this PN returns a struct, just mark the result overdefined.
1387 // TODO: We could do a lot better than this if code actually uses this.
1388 if (PN.getType()->isStructTy())
1389 return (void)markOverdefined(&PN);
1390
1391 if (getValueState(&PN).isOverdefined())
1392 return; // Quick exit
1393
1394 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
1395 // and slow us down a lot. Just mark them overdefined.
1396 if (PN.getNumIncomingValues() > 64)
1397 return (void)markOverdefined(&PN);
1398
1399 unsigned NumActiveIncoming = 0;
1400
1401 // Look at all of the executable operands of the PHI node. If any of them
1402 // are overdefined, the PHI becomes overdefined as well. If they are all
1403 // constant, and they agree with each other, the PHI becomes the identical
1404 // constant. If they are constant and don't agree, the PHI is a constant
1405 // range. If there are no executable operands, the PHI remains unknown.
1406 ValueLatticeElement PhiState = getValueState(&PN);
1407 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1408 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
1409 continue;
1410
1411 ValueLatticeElement IV = getValueState(PN.getIncomingValue(i));
1412 PhiState.mergeIn(IV);
1413 NumActiveIncoming++;
1414 if (PhiState.isOverdefined())
1415 break;
1416 }
1417
1418 // We allow up to 1 range extension per active incoming value and one
1419 // additional extension. Note that we manually adjust the number of range
1420 // extensions to match the number of active incoming values. This helps to
1421 // limit multiple extensions caused by the same incoming value, if other
1422 // incoming values are equal.
1423 mergeInValue(&PN, PhiState,
1424 ValueLatticeElement::MergeOptions().setMaxWidenSteps(
1425 NumActiveIncoming + 1));
1426 ValueLatticeElement &PhiStateRef = getValueState(&PN);
1427 PhiStateRef.setNumRangeExtensions(
1428 std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions()));
1429}
1430
1431void SCCPInstVisitor::visitReturnInst(ReturnInst &I) {
1432 if (I.getNumOperands() == 0)
1433 return; // ret void
1434
1435 Function *F = I.getParent()->getParent();
1436 Value *ResultOp = I.getOperand(0);
1437
1438 // If we are tracking the return value of this function, merge it in.
1439 if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
1440 auto TFRVI = TrackedRetVals.find(F);
1441 if (TFRVI != TrackedRetVals.end()) {
1442 mergeInValue(TFRVI->second, F, getValueState(ResultOp));
1443 return;
1444 }
1445 }
1446
1447 // Handle functions that return multiple values.
1448 if (!TrackedMultipleRetVals.empty()) {
1449 if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
1450 if (MRVFunctionsTracked.count(F))
1451 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1452 mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
1453 getStructValueState(ResultOp, i));
1454 }
1455}
1456
1457void SCCPInstVisitor::visitTerminator(Instruction &TI) {
1458 SmallVector<bool, 16> SuccFeasible;
1459 getFeasibleSuccessors(TI, SuccFeasible);
1460
1461 BasicBlock *BB = TI.getParent();
1462
1463 // Mark all feasible successors executable.
1464 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
1465 if (SuccFeasible[i])
1466 markEdgeExecutable(BB, TI.getSuccessor(i));
1467}
1468
1469void SCCPInstVisitor::visitCastInst(CastInst &I) {
1470 // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1471 // discover a concrete value later.
1472 if (ValueState[&I].isOverdefined())
1473 return;
1474
1475 if (auto *BC = dyn_cast<BitCastInst>(&I)) {
1476 if (BC->getType() == BC->getOperand(0)->getType()) {
1477 if (const PredicateBase *PI = getPredicateInfoFor(&I)) {
1478 handlePredicate(&I, I.getOperand(0), PI);
1479 return;
1480 }
1481 }
1482 }
1483
1484 ValueLatticeElement OpSt = getValueState(I.getOperand(0));
1485 if (OpSt.isUnknownOrUndef())
1486 return;
1487
1488 if (Constant *OpC = getConstant(OpSt, I.getOperand(0)->getType())) {
1489 // Fold the constant as we build.
1490 if (Constant *C =
1491 ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL))
1492 return (void)markConstant(&I, C);
1493 }
1494
1495 // Ignore bitcasts, as they may change the number of vector elements.
1496 if (I.getDestTy()->isIntOrIntVectorTy() &&
1497 I.getSrcTy()->isIntOrIntVectorTy() &&
1498 I.getOpcode() != Instruction::BitCast) {
1499 auto &LV = getValueState(&I);
1500 ConstantRange OpRange =
1501 OpSt.asConstantRange(I.getSrcTy(), /*UndefAllowed=*/false);
1502
1503 Type *DestTy = I.getDestTy();
1504 ConstantRange Res = ConstantRange::getEmpty(DestTy->getScalarSizeInBits());
1505 if (auto *Trunc = dyn_cast<TruncInst>(&I))
1506 Res = OpRange.truncate(DestTy->getScalarSizeInBits(),
1507 Trunc->getNoWrapKind());
1508 else
1509 Res = OpRange.castOp(I.getOpcode(), DestTy->getScalarSizeInBits());
1510 mergeInValue(LV, &I, ValueLatticeElement::getRange(Res));
1511 } else
1512 markOverdefined(&I);
1513}
1514
1515void SCCPInstVisitor::handleExtractOfWithOverflow(ExtractValueInst &EVI,
1516 const WithOverflowInst *WO,
1517 unsigned Idx) {
1518 Value *LHS = WO->getLHS(), *RHS = WO->getRHS();
1519 ValueLatticeElement L = getValueState(LHS);
1520 ValueLatticeElement R = getValueState(RHS);
1521 addAdditionalUser(LHS, &EVI);
1522 addAdditionalUser(RHS, &EVI);
1523 if (L.isUnknownOrUndef() || R.isUnknownOrUndef())
1524 return; // Wait to resolve.
1525
1526 Type *Ty = LHS->getType();
1527 ConstantRange LR = L.asConstantRange(Ty, /*UndefAllowed=*/false);
1528 ConstantRange RR = R.asConstantRange(Ty, /*UndefAllowed=*/false);
1529 if (Idx == 0) {
1530 ConstantRange Res = LR.binaryOp(WO->getBinaryOp(), RR);
1531 mergeInValue(&EVI, ValueLatticeElement::getRange(Res));
1532 } else {
1533 assert(Idx == 1 && "Index can only be 0 or 1");
1534 ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
1535 WO->getBinaryOp(), RR, WO->getNoWrapKind());
1536 if (NWRegion.contains(LR))
1537 return (void)markConstant(&EVI, ConstantInt::getFalse(EVI.getType()));
1538 markOverdefined(&EVI);
1539 }
1540}
1541
1542void SCCPInstVisitor::visitExtractValueInst(ExtractValueInst &EVI) {
1543 // If this returns a struct, mark all elements over defined, we don't track
1544 // structs in structs.
1545 if (EVI.getType()->isStructTy())
1546 return (void)markOverdefined(&EVI);
1547
1548 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1549 // discover a concrete value later.
1550 if (ValueState[&EVI].isOverdefined())
1551 return (void)markOverdefined(&EVI);
1552
1553 // If this is extracting from more than one level of struct, we don't know.
1554 if (EVI.getNumIndices() != 1)
1555 return (void)markOverdefined(&EVI);
1556
1557 Value *AggVal = EVI.getAggregateOperand();
1558 if (AggVal->getType()->isStructTy()) {
1559 unsigned i = *EVI.idx_begin();
1560 if (auto *WO = dyn_cast<WithOverflowInst>(AggVal))
1561 return handleExtractOfWithOverflow(EVI, WO, i);
1562 ValueLatticeElement EltVal = getStructValueState(AggVal, i);
1563 mergeInValue(getValueState(&EVI), &EVI, EltVal);
1564 } else {
1565 // Otherwise, must be extracting from an array.
1566 return (void)markOverdefined(&EVI);
1567 }
1568}
1569
1570void SCCPInstVisitor::visitInsertValueInst(InsertValueInst &IVI) {
1571 auto *STy = dyn_cast<StructType>(IVI.getType());
1572 if (!STy)
1573 return (void)markOverdefined(&IVI);
1574
1575 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1576 // discover a concrete value later.
1577 if (ValueState[&IVI].isOverdefined())
1578 return (void)markOverdefined(&IVI);
1579
1580 // If this has more than one index, we can't handle it, drive all results to
1581 // undef.
1582 if (IVI.getNumIndices() != 1)
1583 return (void)markOverdefined(&IVI);
1584
1585 Value *Aggr = IVI.getAggregateOperand();
1586 unsigned Idx = *IVI.idx_begin();
1587
1588 // Compute the result based on what we're inserting.
1589 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1590 // This passes through all values that aren't the inserted element.
1591 if (i != Idx) {
1592 ValueLatticeElement EltVal = getStructValueState(Aggr, i);
1593 mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
1594 continue;
1595 }
1596
1597 Value *Val = IVI.getInsertedValueOperand();
1598 if (Val->getType()->isStructTy())
1599 // We don't track structs in structs.
1600 markOverdefined(getStructValueState(&IVI, i), &IVI);
1601 else {
1602 ValueLatticeElement InVal = getValueState(Val);
1603 mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
1604 }
1605 }
1606}
1607
1608void SCCPInstVisitor::visitSelectInst(SelectInst &I) {
1609 // If this select returns a struct, just mark the result overdefined.
1610 // TODO: We could do a lot better than this if code actually uses this.
1611 if (I.getType()->isStructTy())
1612 return (void)markOverdefined(&I);
1613
1614 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1615 // discover a concrete value later.
1616 if (ValueState[&I].isOverdefined())
1617 return (void)markOverdefined(&I);
1618
1619 ValueLatticeElement CondValue = getValueState(I.getCondition());
1620 if (CondValue.isUnknownOrUndef())
1621 return;
1622
1623 if (ConstantInt *CondCB =
1624 getConstantInt(CondValue, I.getCondition()->getType())) {
1625 Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
1626 mergeInValue(&I, getValueState(OpVal));
1627 return;
1628 }
1629
1630 // Otherwise, the condition is overdefined or a constant we can't evaluate.
1631 // See if we can produce something better than overdefined based on the T/F
1632 // value.
1633 ValueLatticeElement TVal = getValueState(I.getTrueValue());
1634 ValueLatticeElement FVal = getValueState(I.getFalseValue());
1635
1636 ValueLatticeElement &State = ValueState[&I];
1637 bool Changed = State.mergeIn(TVal);
1638 Changed |= State.mergeIn(FVal);
1639 if (Changed)
1640 pushUsersToWorkListMsg(State, &I);
1641}
1642
1643// Handle Unary Operators.
1644void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
1645 ValueLatticeElement V0State = getValueState(I.getOperand(0));
1646
1647 ValueLatticeElement &IV = ValueState[&I];
1648 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1649 // discover a concrete value later.
1650 if (IV.isOverdefined())
1651 return (void)markOverdefined(&I);
1652
1653 // If something is unknown/undef, wait for it to resolve.
1654 if (V0State.isUnknownOrUndef())
1655 return;
1656
1657 if (SCCPSolver::isConstant(V0State))
1658 if (Constant *C = ConstantFoldUnaryOpOperand(
1659 I.getOpcode(), getConstant(V0State, I.getType()), DL))
1660 return (void)markConstant(IV, &I, C);
1661
1662 markOverdefined(&I);
1663}
1664
1665void SCCPInstVisitor::visitFreezeInst(FreezeInst &I) {
1666 // If this freeze returns a struct, just mark the result overdefined.
1667 // TODO: We could do a lot better than this.
1668 if (I.getType()->isStructTy())
1669 return (void)markOverdefined(&I);
1670
1671 ValueLatticeElement V0State = getValueState(I.getOperand(0));
1672 ValueLatticeElement &IV = ValueState[&I];
1673 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1674 // discover a concrete value later.
1675 if (IV.isOverdefined())
1676 return (void)markOverdefined(&I);
1677
1678 // If something is unknown/undef, wait for it to resolve.
1679 if (V0State.isUnknownOrUndef())
1680 return;
1681
1682 if (SCCPSolver::isConstant(V0State) &&
1683 isGuaranteedNotToBeUndefOrPoison(getConstant(V0State, I.getType())))
1684 return (void)markConstant(IV, &I, getConstant(V0State, I.getType()));
1685
1686 markOverdefined(&I);
1687}
1688
1689// Handle Binary Operators.
1690void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
1691 ValueLatticeElement V1State = getValueState(I.getOperand(0));
1692 ValueLatticeElement V2State = getValueState(I.getOperand(1));
1693
1694 ValueLatticeElement &IV = ValueState[&I];
1695 if (IV.isOverdefined())
1696 return;
1697
1698 // If something is undef, wait for it to resolve.
1699 if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef())
1700 return;
1701
1702 if (V1State.isOverdefined() && V2State.isOverdefined())
1703 return (void)markOverdefined(&I);
1704
1705 // If either of the operands is a constant, try to fold it to a constant.
1706 // TODO: Use information from notconstant better.
1707 if ((V1State.isConstant() || V2State.isConstant())) {
1708 Value *V1 = SCCPSolver::isConstant(V1State)
1709 ? getConstant(V1State, I.getOperand(0)->getType())
1710 : I.getOperand(0);
1711 Value *V2 = SCCPSolver::isConstant(V2State)
1712 ? getConstant(V2State, I.getOperand(1)->getType())
1713 : I.getOperand(1);
1714 Value *R = simplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL, &I));
1715 auto *C = dyn_cast_or_null<Constant>(R);
1716 if (C) {
1717 // Conservatively assume that the result may be based on operands that may
1718 // be undef. Note that we use mergeInValue to combine the constant with
1719 // the existing lattice value for I, as different constants might be found
1720 // after one of the operands go to overdefined, e.g. due to one operand
1721 // being a special floating value.
1722 ValueLatticeElement NewV;
1723 NewV.markConstant(C, /*MayIncludeUndef=*/true);
1724 return (void)mergeInValue(&I, NewV);
1725 }
1726 }
1727
1728 // Only use ranges for binary operators on integers.
1729 if (!I.getType()->isIntOrIntVectorTy())
1730 return markOverdefined(&I);
1731
1732 // Try to simplify to a constant range.
1733 ConstantRange A =
1734 V1State.asConstantRange(I.getType(), /*UndefAllowed=*/false);
1735 ConstantRange B =
1736 V2State.asConstantRange(I.getType(), /*UndefAllowed=*/false);
1737
1738 auto *BO = cast<BinaryOperator>(&I);
1739 ConstantRange R = ConstantRange::getEmpty(I.getType()->getScalarSizeInBits());
1740 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO))
1741 R = A.overflowingBinaryOp(BO->getOpcode(), B, OBO->getNoWrapKind());
1742 else
1743 R = A.binaryOp(BO->getOpcode(), B);
1744 mergeInValue(&I, ValueLatticeElement::getRange(R));
1745
1746 // TODO: Currently we do not exploit special values that produce something
1747 // better than overdefined with an overdefined operand for vector or floating
1748 // point types, like and <4 x i32> overdefined, zeroinitializer.
1749}
1750
1751// Handle ICmpInst instruction.
1752void SCCPInstVisitor::visitCmpInst(CmpInst &I) {
1753 // Do not cache this lookup, getValueState calls later in the function might
1754 // invalidate the reference.
1755 if (ValueState[&I].isOverdefined())
1756 return (void)markOverdefined(&I);
1757
1758 Value *Op1 = I.getOperand(0);
1759 Value *Op2 = I.getOperand(1);
1760
1761 // For parameters, use ParamState which includes constant range info if
1762 // available.
1763 auto V1State = getValueState(Op1);
1764 auto V2State = getValueState(Op2);
1765
1766 Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State, DL);
1767 if (C) {
1768 ValueLatticeElement CV;
1769 CV.markConstant(C);
1770 mergeInValue(&I, CV);
1771 return;
1772 }
1773
1774 // If operands are still unknown, wait for it to resolve.
1775 if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) &&
1776 !SCCPSolver::isConstant(ValueState[&I]))
1777 return;
1778
1779 markOverdefined(&I);
1780}
1781
1782// Handle getelementptr instructions. If all operands are constants then we
1783// can turn this into a getelementptr ConstantExpr.
1784void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
1785 if (ValueState[&I].isOverdefined())
1786 return (void)markOverdefined(&I);
1787
1788 const ValueLatticeElement &PtrState = getValueState(I.getPointerOperand());
1789 if (PtrState.isUnknownOrUndef())
1790 return;
1791
1792 // gep inbounds/nuw of non-null is non-null.
1793 if (PtrState.isNotConstant() && PtrState.getNotConstant()->isNullValue()) {
1794 if (I.hasNoUnsignedWrap() ||
1795 (I.isInBounds() &&
1796 !NullPointerIsDefined(I.getFunction(), I.getAddressSpace())))
1797 return (void)markNotNull(ValueState[&I], &I);
1798 return (void)markOverdefined(&I);
1799 }
1800
1802 Operands.reserve(I.getNumOperands());
1803
1804 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
1805 ValueLatticeElement State = getValueState(I.getOperand(i));
1806 if (State.isUnknownOrUndef())
1807 return; // Operands are not resolved yet.
1808
1809 if (Constant *C = getConstant(State, I.getOperand(i)->getType())) {
1810 Operands.push_back(C);
1811 continue;
1812 }
1813
1814 return (void)markOverdefined(&I);
1815 }
1816
1817 if (Constant *C = ConstantFoldInstOperands(&I, Operands, DL))
1818 markConstant(&I, C);
1819 else
1820 markOverdefined(&I);
1821}
1822
1823void SCCPInstVisitor::visitAllocaInst(AllocaInst &I) {
1824 if (!NullPointerIsDefined(I.getFunction(), I.getAddressSpace()))
1825 return (void)markNotNull(ValueState[&I], &I);
1826
1827 markOverdefined(&I);
1828}
1829
1830void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
1831 // If this store is of a struct, ignore it.
1832 if (SI.getOperand(0)->getType()->isStructTy())
1833 return;
1834
1835 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1836 return;
1837
1838 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1839 auto I = TrackedGlobals.find(GV);
1840 if (I == TrackedGlobals.end())
1841 return;
1842
1843 // Get the value we are storing into the global, then merge it.
1844 mergeInValue(I->second, GV, getValueState(SI.getOperand(0)),
1845 ValueLatticeElement::MergeOptions().setCheckWiden(false));
1846 if (I->second.isOverdefined())
1847 TrackedGlobals.erase(I); // No need to keep tracking this!
1848}
1849
1851 if (const auto *CB = dyn_cast<CallBase>(I)) {
1852 if (CB->getType()->isIntOrIntVectorTy())
1853 if (std::optional<ConstantRange> Range = CB->getRange())
1855 if (CB->getType()->isPointerTy() && CB->isReturnNonNull())
1858 }
1859
1860 if (I->getType()->isIntOrIntVectorTy())
1861 if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
1864 if (I->hasMetadata(LLVMContext::MD_nonnull))
1867
1869}
1870
1871// Handle load instructions. If the operand is a constant pointer to a constant
1872// global, we can replace the load with the loaded constant value!
1873void SCCPInstVisitor::visitLoadInst(LoadInst &I) {
1874 // If this load is of a struct or the load is volatile, just mark the result
1875 // as overdefined.
1876 if (I.getType()->isStructTy() || I.isVolatile())
1877 return (void)markOverdefined(&I);
1878
1879 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1880 // discover a concrete value later.
1881 if (ValueState[&I].isOverdefined())
1882 return (void)markOverdefined(&I);
1883
1884 ValueLatticeElement PtrVal = getValueState(I.getOperand(0));
1885 if (PtrVal.isUnknownOrUndef())
1886 return; // The pointer is not resolved yet!
1887
1888 ValueLatticeElement &IV = ValueState[&I];
1889
1890 if (SCCPSolver::isConstant(PtrVal)) {
1891 Constant *Ptr = getConstant(PtrVal, I.getOperand(0)->getType());
1892
1893 // load null is undefined.
1895 if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1896 return (void)markOverdefined(IV, &I);
1897 else
1898 return;
1899 }
1900
1901 // Transform load (constant global) into the value loaded.
1902 if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1903 if (!TrackedGlobals.empty()) {
1904 // If we are tracking this global, merge in the known value for it.
1905 auto It = TrackedGlobals.find(GV);
1906 if (It != TrackedGlobals.end()) {
1907 mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1908 return;
1909 }
1910 }
1911 }
1912
1913 // Transform load from a constant into a constant if possible.
1914 if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL))
1915 return (void)markConstant(IV, &I, C);
1916 }
1917
1918 // Fall back to metadata.
1919 mergeInValue(&I, getValueFromMetadata(&I));
1920}
1921
1922void SCCPInstVisitor::visitCallBase(CallBase &CB) {
1923 handleCallResult(CB);
1924 handleCallArguments(CB);
1925}
1926
1927void SCCPInstVisitor::handleCallOverdefined(CallBase &CB) {
1929
1930 // Void return and not tracking callee, just bail.
1931 if (CB.getType()->isVoidTy())
1932 return;
1933
1934 // Always mark struct return as overdefined.
1935 if (CB.getType()->isStructTy())
1936 return (void)markOverdefined(&CB);
1937
1938 // Otherwise, if we have a single return value case, and if the function is
1939 // a declaration, maybe we can constant fold it.
1940 if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
1942 for (const Use &A : CB.args()) {
1943 if (A.get()->getType()->isStructTy())
1944 return markOverdefined(&CB); // Can't handle struct args.
1945 if (A.get()->getType()->isMetadataTy())
1946 continue; // Carried in CB, not allowed in Operands.
1947 ValueLatticeElement State = getValueState(A);
1948
1949 if (State.isUnknownOrUndef())
1950 return; // Operands are not resolved yet.
1951 if (SCCPSolver::isOverdefined(State))
1952 return (void)markOverdefined(&CB);
1953 assert(SCCPSolver::isConstant(State) && "Unknown state!");
1954 Operands.push_back(getConstant(State, A->getType()));
1955 }
1956
1957 if (SCCPSolver::isOverdefined(getValueState(&CB)))
1958 return (void)markOverdefined(&CB);
1959
1960 // If we can constant fold this, mark the result of the call as a
1961 // constant.
1962 if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F)))
1963 return (void)markConstant(&CB, C);
1964 }
1965
1966 // Fall back to metadata.
1967 mergeInValue(&CB, getValueFromMetadata(&CB));
1968}
1969
1970void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
1972 // If this is a local function that doesn't have its address taken, mark its
1973 // entry block executable and merge in the actual arguments to the call into
1974 // the formal arguments of the function.
1975 if (TrackingIncomingArguments.count(F)) {
1976 markBlockExecutable(&F->front());
1977
1978 // Propagate information from this call site into the callee.
1979 auto CAI = CB.arg_begin();
1980 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
1981 ++AI, ++CAI) {
1982 // If this argument is byval, and if the function is not readonly, there
1983 // will be an implicit copy formed of the input aggregate.
1984 if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1985 markOverdefined(&*AI);
1986 continue;
1987 }
1988
1989 if (auto *STy = dyn_cast<StructType>(AI->getType())) {
1990 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1991 ValueLatticeElement CallArg = getStructValueState(*CAI, i);
1992 mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg,
1994 }
1995 } else
1996 mergeInValue(&*AI,
1997 getValueState(*CAI).intersect(getArgAttributeVL(&*AI)),
1999 }
2000 }
2001}
2002
2003void SCCPInstVisitor::handlePredicate(Instruction *I, Value *CopyOf,
2004 const PredicateBase *PI) {
2005 ValueLatticeElement CopyOfVal = getValueState(CopyOf);
2006 const std::optional<PredicateConstraint> &Constraint = PI->getConstraint();
2007 if (!Constraint) {
2008 mergeInValue(ValueState[I], I, CopyOfVal);
2009 return;
2010 }
2011
2012 CmpInst::Predicate Pred = Constraint->Predicate;
2013 Value *OtherOp = Constraint->OtherOp;
2014
2015 // Wait until OtherOp is resolved.
2016 if (getValueState(OtherOp).isUnknown()) {
2017 addAdditionalUser(OtherOp, I);
2018 return;
2019 }
2020
2021 ValueLatticeElement CondVal = getValueState(OtherOp);
2022 ValueLatticeElement &IV = ValueState[I];
2023 if (CondVal.isConstantRange() || CopyOfVal.isConstantRange()) {
2024 auto ImposedCR =
2025 ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
2026
2027 // Get the range imposed by the condition.
2028 if (CondVal.isConstantRange())
2030 Pred, CondVal.getConstantRange());
2031
2032 // Combine range info for the original value with the new range from the
2033 // condition.
2034 auto CopyOfCR = CopyOfVal.asConstantRange(CopyOf->getType(),
2035 /*UndefAllowed=*/true);
2036 // Treat an unresolved input like a full range.
2037 if (CopyOfCR.isEmptySet())
2038 CopyOfCR = ConstantRange::getFull(CopyOfCR.getBitWidth());
2039 auto NewCR = ImposedCR.intersectWith(CopyOfCR);
2040 // If the existing information is != x, do not use the information from
2041 // a chained predicate, as the != x information is more likely to be
2042 // helpful in practice.
2043 if (!CopyOfCR.contains(NewCR) && CopyOfCR.getSingleMissingElement())
2044 NewCR = CopyOfCR;
2045
2046 // The new range is based on a branch condition. That guarantees that
2047 // neither of the compare operands can be undef in the branch targets,
2048 // unless we have conditions that are always true/false (e.g. icmp ule
2049 // i32, %a, i32_max). For the latter overdefined/empty range will be
2050 // inferred, but the branch will get folded accordingly anyways.
2051 addAdditionalUser(OtherOp, I);
2052 mergeInValue(
2053 IV, I, ValueLatticeElement::getRange(NewCR, /*MayIncludeUndef*/ false));
2054 return;
2055 } else if (Pred == CmpInst::ICMP_EQ &&
2056 (CondVal.isConstant() || CondVal.isNotConstant())) {
2057 // For non-integer values or integer constant expressions, only
2058 // propagate equal constants or not-constants.
2059 addAdditionalUser(OtherOp, I);
2060 mergeInValue(IV, I, CondVal);
2061 return;
2062 } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant()) {
2063 // Propagate inequalities.
2064 addAdditionalUser(OtherOp, I);
2065 mergeInValue(IV, I, ValueLatticeElement::getNot(CondVal.getConstant()));
2066 return;
2067 }
2068
2069 return (void)mergeInValue(IV, I, CopyOfVal);
2070}
2071
2072void SCCPInstVisitor::handleCallResult(CallBase &CB) {
2074
2075 if (auto *II = dyn_cast<IntrinsicInst>(&CB)) {
2076 if (II->getIntrinsicID() == Intrinsic::vscale) {
2077 unsigned BitWidth = CB.getType()->getScalarSizeInBits();
2078 const ConstantRange Result = getVScaleRange(II->getFunction(), BitWidth);
2079 return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
2080 }
2081
2082 if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
2083 // Compute result range for intrinsics supported by ConstantRange.
2084 // Do this even if we don't know a range for all operands, as we may
2085 // still know something about the result range, e.g. of abs(x).
2087 for (Value *Op : II->args()) {
2088 const ValueLatticeElement &State = getValueState(Op);
2089 if (State.isUnknownOrUndef())
2090 return;
2091 OpRanges.push_back(
2092 State.asConstantRange(Op->getType(), /*UndefAllowed=*/false));
2093 }
2094
2095 ConstantRange Result =
2096 ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges);
2097 return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
2098 }
2099 }
2100
2101 // The common case is that we aren't tracking the callee, either because we
2102 // are not doing interprocedural analysis or the callee is indirect, or is
2103 // external. Handle these cases first.
2104 if (!F || F->isDeclaration())
2105 return handleCallOverdefined(CB);
2106
2107 // If this is a single/zero retval case, see if we're tracking the function.
2108 if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
2109 if (!MRVFunctionsTracked.count(F))
2110 return handleCallOverdefined(CB); // Not tracking this callee.
2111
2112 // If we are tracking this callee, propagate the result of the function
2113 // into this call site.
2114 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2115 mergeInValue(getStructValueState(&CB, i), &CB,
2116 TrackedMultipleRetVals[std::make_pair(F, i)],
2118 } else {
2119 auto TFRVI = TrackedRetVals.find(F);
2120 if (TFRVI == TrackedRetVals.end())
2121 return handleCallOverdefined(CB); // Not tracking this callee.
2122
2123 // If so, propagate the return value of the callee into this call result.
2124 mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts());
2125 }
2126}
2127
2129 // Process the work lists until they are empty!
2130 while (!BBWorkList.empty() || !InstWorkList.empty()) {
2131 // Process the instruction work list.
2132 while (!InstWorkList.empty()) {
2133 Instruction *I = InstWorkList.pop_back_val();
2134 Invalidated.erase(I);
2135
2136 LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
2137
2138 visit(I);
2139 }
2140
2141 // Process the basic block work list.
2142 while (!BBWorkList.empty()) {
2143 BasicBlock *BB = BBWorkList.pop_back_val();
2144 BBVisited.insert(BB);
2145
2146 LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
2147 for (Instruction &I : *BB) {
2148 CurI = &I;
2149 visit(I);
2150 }
2151 CurI = nullptr;
2152 }
2153 }
2154}
2155
2157 // Look for instructions which produce undef values.
2158 if (I.getType()->isVoidTy())
2159 return false;
2160
2161 if (auto *STy = dyn_cast<StructType>(I.getType())) {
2162 // Only a few things that can be structs matter for undef.
2163
2164 // Tracked calls must never be marked overdefined in resolvedUndefsIn.
2165 if (auto *CB = dyn_cast<CallBase>(&I))
2166 if (Function *F = CB->getCalledFunction())
2167 if (MRVFunctionsTracked.count(F))
2168 return false;
2169
2170 // extractvalue and insertvalue don't need to be marked; they are
2171 // tracked as precisely as their operands.
2173 return false;
2174 // Send the results of everything else to overdefined. We could be
2175 // more precise than this but it isn't worth bothering.
2176 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2177 ValueLatticeElement &LV = getStructValueState(&I, i);
2178 if (LV.isUnknown()) {
2179 markOverdefined(LV, &I);
2180 return true;
2181 }
2182 }
2183 return false;
2184 }
2185
2186 ValueLatticeElement &LV = getValueState(&I);
2187 if (!LV.isUnknown())
2188 return false;
2189
2190 // There are two reasons a call can have an undef result
2191 // 1. It could be tracked.
2192 // 2. It could be constant-foldable.
2193 // Because of the way we solve return values, tracked calls must
2194 // never be marked overdefined in resolvedUndefsIn.
2195 if (auto *CB = dyn_cast<CallBase>(&I))
2196 if (Function *F = CB->getCalledFunction())
2197 if (TrackedRetVals.count(F))
2198 return false;
2199
2200 if (isa<LoadInst>(I)) {
2201 // A load here means one of two things: a load of undef from a global,
2202 // a load from an unknown pointer. Either way, having it return undef
2203 // is okay.
2204 return false;
2205 }
2206
2207 markOverdefined(&I);
2208 return true;
2209}
2210
2211/// While solving the dataflow for a function, we don't compute a result for
2212/// operations with an undef operand, to allow undef to be lowered to a
2213/// constant later. For example, constant folding of "zext i8 undef to i16"
2214/// would result in "i16 0", and if undef is later lowered to "i8 1", then the
2215/// zext result would become "i16 1" and would result into an overdefined
2216/// lattice value once merged with the previous result. Not computing the
2217/// result of the zext (treating undef the same as unknown) allows us to handle
2218/// a later undef->constant lowering more optimally.
2219///
2220/// However, if the operand remains undef when the solver returns, we do need
2221/// to assign some result to the instruction (otherwise we would treat it as
2222/// unreachable). For simplicity, we mark any instructions that are still
2223/// unknown as overdefined.
2225 bool MadeChange = false;
2226 for (BasicBlock &BB : F) {
2227 if (!BBExecutable.count(&BB))
2228 continue;
2229
2230 for (Instruction &I : BB)
2231 MadeChange |= resolvedUndef(I);
2232 }
2233
2234 LLVM_DEBUG(if (MadeChange) dbgs()
2235 << "\nResolved undefs in " << F.getName() << '\n');
2236
2237 return MadeChange;
2238}
2239
2240//===----------------------------------------------------------------------===//
2241//
2242// SCCPSolver implementations
2243//
2245 const DataLayout &DL,
2246 std::function<const TargetLibraryInfo &(Function &)> GetTLI,
2247 LLVMContext &Ctx)
2248 : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {}
2249
2250SCCPSolver::~SCCPSolver() = default;
2251
2253 AssumptionCache &AC) {
2254 Visitor->addPredicateInfo(F, DT, AC);
2255}
2256
2258 Visitor->removeSSACopies(F);
2259}
2260
2262 return Visitor->markBlockExecutable(BB);
2263}
2264
2266 return Visitor->getPredicateInfoFor(I);
2267}
2268
2270 Visitor->trackValueOfGlobalVariable(GV);
2271}
2272
2274 Visitor->addTrackedFunction(F);
2275}
2276
2278 Visitor->addToMustPreserveReturnsInFunctions(F);
2279}
2280
2282 return Visitor->mustPreserveReturn(F);
2283}
2284
2286 Visitor->addArgumentTrackedFunction(F);
2287}
2288
2290 return Visitor->isArgumentTrackedFunction(F);
2291}
2292
2295 return Visitor->getArgumentTrackedFunctions();
2296}
2297
2298void SCCPSolver::solve() { Visitor->solve(); }
2299
2301 return Visitor->resolvedUndefsIn(F);
2302}
2303
2305 Visitor->solveWhileResolvedUndefsIn(M);
2306}
2307
2308void
2310 Visitor->solveWhileResolvedUndefsIn(WorkList);
2311}
2312
2314 Visitor->solveWhileResolvedUndefs();
2315}
2316
2318 return Visitor->isBlockExecutable(BB);
2319}
2320
2322 return Visitor->isEdgeFeasible(From, To);
2323}
2324
2325std::vector<ValueLatticeElement>
2327 return Visitor->getStructLatticeValueFor(V);
2328}
2329
2331 return Visitor->removeLatticeValueFor(V);
2332}
2333
2335 Visitor->resetLatticeValueFor(Call);
2336}
2337
2339 return Visitor->getLatticeValueFor(V);
2340}
2341
2344 return Visitor->getTrackedRetVals();
2345}
2346
2349 return Visitor->getTrackedGlobals();
2350}
2351
2353 return Visitor->getMRVFunctionsTracked();
2354}
2355
2356void SCCPSolver::markOverdefined(Value *V) { Visitor->markOverdefined(V); }
2357
2359 Visitor->trackValueOfArgument(V);
2360}
2361
2363 return Visitor->isStructLatticeConstant(F, STy);
2364}
2365
2367 Type *Ty) const {
2368 return Visitor->getConstant(LV, Ty);
2369}
2370
2372 return Visitor->getConstantOrNull(V);
2373}
2374
2376 const SmallVectorImpl<ArgInfo> &Args) {
2377 Visitor->setLatticeValueForSpecializationArguments(F, Args);
2378}
2379
2381 Visitor->markFunctionUnreachable(F);
2382}
2383
2384void SCCPSolver::visit(Instruction *I) { Visitor->visit(I); }
2385
2386void SCCPSolver::visitCall(CallInst &I) { Visitor->visitCall(I); }
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Hexagon Common GEP
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
mir Rename Register Operands
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts()
Returns MergeOptions with MaxWidenSteps set to MaxNumRangeExtensions.
static const unsigned MaxNumRangeExtensions
static ValueLatticeElement getValueFromMetadata(const Instruction *I)
std::pair< BasicBlock *, BasicBlock * > Edge
This file implements a set that has insertion order iteration characteristics.
static ConstantInt * getConstantInt(Value *V, const DataLayout &DL)
Extract ConstantInt from value, looking through IntToPtr and PointerNullValue.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1639
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1150
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
A cache of @llvm.assume calls within a function.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI const ConstantRange & getRange() const
Returns the value of the range attribute.
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:223
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:528
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
LLVM_ABI unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Function * getFunction() const
Definition Constants.h:935
BasicBlock * getBasicBlock() const
Definition Constants.h:934
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
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_SLE
signed less or equal
Definition InstrTypes.h:706
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
This is the shared class of boolean and integer constants.
Definition Constants.h:87
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
This class represents a range of values.
LLVM_ABI ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
LLVM_ABI ConstantRange castOp(Instruction::CastOps CastOp, uint32_t BitWidth) const
Return a new range representing the possible values resulting from an application of the specified ca...
LLVM_ABI bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
static LLVM_ABI ConstantRange intrinsic(Intrinsic::ID IntrinsicID, ArrayRef< ConstantRange > Ops)
Compute range of intrinsic result for the given operand ranges.
LLVM_ABI bool isSizeLargerThan(uint64_t MaxSize) const
Compare set size of this range with Value.
static LLVM_ABI bool isIntrinsicSupported(Intrinsic::ID IntrinsicID)
Returns true if ConstantRange calculations are supported for intrinsic with IntrinsicID.
bool isSingleElement() const
Return true if this set contains exactly one member.
LLVM_ABI ConstantRange truncate(uint32_t BitWidth, unsigned NoWrapKind=0) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI ConstantRange inverse() const
Return a new range that is the logical not of the current set.
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
static LLVM_ABI ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind)
Produce the largest range containing all X such that "X BinOp Y" is guaranteed not to wrap (overflow)...
LLVM_ABI ConstantRange binaryOp(Instruction::BinaryOps BinOp, const ConstantRange &Other) const
Return a new range representing the possible values resulting from an application of the specified bi...
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static DebugLoc getTemporary()
Definition DebugLoc.h:161
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
This instruction extracts a struct member or array element value from an aggregate value.
unsigned getNumIndices() const
idx_iterator idx_begin() const
This class represents a freeze function that returns random concrete value if an operand is either a ...
Argument * arg_iterator
Definition Function.h:72
static GEPNoWrapFlags noUnsignedWrap()
void applyUpdatesPermissive(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Module * getParent()
Get the module that this global value is contained inside of...
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2783
This instruction inserts a struct field of array element value into an aggregate value.
Value * getInsertedValueOperand()
unsigned getNumIndices() const
idx_iterator idx_begin() const
Base class for instruction visitors.
Definition InstVisitor.h:78
void visit(Iterator Start, Iterator End)
Definition InstVisitor.h:87
LLVM_ABI void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
LLVM_ABI void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
LLVM_ABI void setNonNeg(bool b=true)
Set or clear the nneg flag on this instruction, which must be a zext instruction.
LLVM_ABI bool hasNonNeg() const LLVM_READONLY
Determine whether the the nneg flag is set.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
bool isSpecialTerminator() const
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Metadata node.
Definition Metadata.h:1078
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
LLVM_ABI std::optional< PredicateConstraint > getConstraint() const
Fetch condition in the form of PredicateConstraint, if possible.
Return a value (possibly void), from a function.
Helper class for SCCPSolver.
const MapVector< Function *, ValueLatticeElement > & getTrackedRetVals() const
const PredicateBase * getPredicateInfoFor(Instruction *I)
std::vector< ValueLatticeElement > getStructLatticeValueFor(Value *V) const
bool resolvedUndef(Instruction &I)
void markFunctionUnreachable(Function *F)
bool markBlockExecutable(BasicBlock *BB)
bool resolvedUndefsIn(Function &F)
While solving the dataflow for a function, we don't compute a result for operations with an undef ope...
Constant * getConstant(const ValueLatticeElement &LV, Type *Ty) const
SCCPInstVisitor(const DataLayout &DL, std::function< const TargetLibraryInfo &(Function &)> GetTLI, LLVMContext &Ctx)
const DenseMap< GlobalVariable *, ValueLatticeElement > & getTrackedGlobals() const
const ValueLatticeElement & getLatticeValueFor(Value *V) const
void removeLatticeValueFor(Value *V)
void trackValueOfArgument(Argument *A)
void visitCallInst(CallInst &I)
void markOverdefined(Value *V)
bool isArgumentTrackedFunction(Function *F)
void addTrackedFunction(Function *F)
void solveWhileResolvedUndefsIn(Module &M)
void trackValueOfGlobalVariable(GlobalVariable *GV)
Constant * getConstantOrNull(Value *V) const
void removeSSACopies(Function &F)
const SmallPtrSet< Function *, 16 > & getMRVFunctionsTracked() const
const SmallPtrSetImpl< Function * > & getArgumentTrackedFunctions() const
void resetLatticeValueFor(CallBase *Call)
Invalidate the Lattice Value of Call and its users after specializing the call.
ValueLatticeElement getArgAttributeVL(Argument *A)
void addPredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC)
void addToMustPreserveReturnsInFunctions(Function *F)
void addArgumentTrackedFunction(Function *F)
bool isStructLatticeConstant(Function *F, StructType *STy)
void solveWhileResolvedUndefsIn(SmallVectorImpl< Function * > &WorkList)
bool isBlockExecutable(BasicBlock *BB) const
bool mustPreserveReturn(Function *F)
void setLatticeValueForSpecializationArguments(Function *F, const SmallVectorImpl< ArgInfo > &Args)
bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const
SCCPSolver - This interface class is a general purpose solver for Sparse Conditional Constant Propaga...
Definition SCCPSolver.h:66
LLVM_ABI void visitCall(CallInst &I)
LLVM_ABI ~SCCPSolver()
LLVM_ABI void resetLatticeValueFor(CallBase *Call)
Invalidate the Lattice Value of Call and its users after specializing the call.
LLVM_ABI void trackValueOfGlobalVariable(GlobalVariable *GV)
trackValueOfGlobalVariable - Clients can use this method to inform the SCCPSolver that it should trac...
LLVM_ABI bool tryToReplaceWithConstant(Value *V)
LLVM_ABI void inferArgAttributes() const
LLVM_ABI bool isStructLatticeConstant(Function *F, StructType *STy)
LLVM_ABI void addPredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC)
LLVM_ABI void solve()
Solve - Solve for constants and executable blocks.
LLVM_ABI void visit(Instruction *I)
LLVM_ABI void trackValueOfArgument(Argument *V)
trackValueOfArgument - Mark the specified argument overdefined unless it have range attribute.
LLVM_ABI const DenseMap< GlobalVariable *, ValueLatticeElement > & getTrackedGlobals() const
getTrackedGlobals - Get and return the set of inferred initializers for global variables.
LLVM_ABI void addTrackedFunction(Function *F)
addTrackedFunction - If the SCCP solver is supposed to track calls into and out of the specified func...
LLVM_ABI void solveWhileResolvedUndefsIn(Module &M)
LLVM_ABI const PredicateBase * getPredicateInfoFor(Instruction *I)
LLVM_ABI const SmallPtrSetImpl< Function * > & getArgumentTrackedFunctions() const
LLVM_ABI const SmallPtrSet< Function *, 16 > & getMRVFunctionsTracked() const
getMRVFunctionsTracked - Get the set of functions which return multiple values tracked by the pass.
LLVM_ABI bool resolvedUndefsIn(Function &F)
resolvedUndefsIn - While solving the dataflow for a function, we assume that branches on undef values...
LLVM_ABI void addArgumentTrackedFunction(Function *F)
LLVM_ABI void solveWhileResolvedUndefs()
LLVM_ABI void removeLatticeValueFor(Value *V)
LLVM_ABI std::vector< ValueLatticeElement > getStructLatticeValueFor(Value *V) const
LLVM_ABI Constant * getConstantOrNull(Value *V) const
Return either a Constant or nullptr for a given Value.
LLVM_ABI bool simplifyInstsInBlock(BasicBlock &BB, SmallPtrSetImpl< Value * > &InsertedValues, Statistic &InstRemovedStat, Statistic &InstReplacedStat)
LLVM_ABI Constant * getConstant(const ValueLatticeElement &LV, Type *Ty) const
Helper to return a Constant if LV is either a constant or a constant range with a single element.
LLVM_ABI const ValueLatticeElement & getLatticeValueFor(Value *V) const
LLVM_ABI void addToMustPreserveReturnsInFunctions(Function *F)
Add function to the list of functions whose return cannot be modified.
LLVM_ABI bool removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU, BasicBlock *&NewUnreachableBB) const
LLVM_ABI bool isBlockExecutable(BasicBlock *BB) const
LLVM_ABI void inferReturnAttributes() const
LLVM_ABI bool markBlockExecutable(BasicBlock *BB)
markBlockExecutable - This method can be used by clients to mark all of the blocks that are known to ...
LLVM_ABI void setLatticeValueForSpecializationArguments(Function *F, const SmallVectorImpl< ArgInfo > &Args)
Set the Lattice Value for the arguments of a specialization F.
static LLVM_ABI bool isConstant(const ValueLatticeElement &LV)
LLVM_ABI const MapVector< Function *, ValueLatticeElement > & getTrackedRetVals() const
getTrackedRetVals - Get the inferred return value map.
LLVM_ABI bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const
LLVM_ABI bool mustPreserveReturn(Function *F)
Returns true if the return of the given function cannot be modified.
static LLVM_ABI bool isOverdefined(const ValueLatticeElement &LV)
LLVM_ABI void markFunctionUnreachable(Function *F)
Mark all of the blocks in function F non-executable.
LLVM_ABI bool isArgumentTrackedFunction(Function *F)
Returns true if the given function is in the solver's set of argument-tracked functions.
LLVM_ABI SCCPSolver(const DataLayout &DL, std::function< const TargetLibraryInfo &(Function &)> GetTLI, LLVMContext &Ctx)
LLVM_ABI void markOverdefined(Value *V)
markOverdefined - Mark the specified value overdefined.
LLVM_ABI void removeSSACopies(Function &F)
This class represents the LLVM 'select' instruction.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
iterator begin() const
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:338
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
A wrapper class to simplify modification of SwitchInst cases along with their prof branch_weights met...
Provides information about what library functions are available for the current target.
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isSingleValueType() const
Return true if the type is a valid type for a register in codegen.
Definition Type.h:296
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:261
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
This function has undefined behavior.
Value * getOperand(unsigned i) const
Definition User.h:232
This class represents lattice values for constants.
static ValueLatticeElement getRange(ConstantRange CR, bool MayIncludeUndef=false)
LLVM_ABI Constant * getCompare(CmpInst::Predicate Pred, Type *Ty, const ValueLatticeElement &Other, const DataLayout &DL) const
true, false or undef constants, or nullptr if the comparison cannot be evaluated.
bool isConstantRangeIncludingUndef() const
static ValueLatticeElement getNot(Constant *C)
ConstantRange asConstantRange(unsigned BW, bool UndefAllowed=false) const
void setNumRangeExtensions(unsigned N)
const ConstantRange & getConstantRange(bool UndefAllowed=true) const
Returns the constant range for this value.
bool isConstantRange(bool UndefAllowed=true) const
Returns true if this value is a constant range.
unsigned getNumRangeExtensions() const
Constant * getNotConstant() const
Constant * getConstant() const
bool mergeIn(const ValueLatticeElement &RHS, MergeOptions Opts=MergeOptions())
Updates this object to approximate both this object and RHS.
bool markConstant(Constant *V, bool MayIncludeUndef=false)
static ValueLatticeElement getOverdefined()
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI std::string getNameOrAsOperand() const
Definition Value.cpp:457
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
Represents an op.with.overflow intrinsic.
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
CallInst * Call
Changed
#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
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
bool match(Val *V, const Pattern &P)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1727
static bool replaceSignedInst(SCCPSolver &Solver, SmallPtrSetImpl< Value * > &InsertedValues, Instruction &Inst)
Try to replace signed instructions with their unsigned equivalent.
LLVM_ABI bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
auto successors(const MachineBasicBlock *BB)
static ConstantRange getRange(Value *Op, SCCPSolver &Solver, const SmallPtrSetImpl< Value * > &InsertedValues)
Helper for getting ranges from Solver.
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
LLVM_ABI Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:754
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1734
NoopStatistic Statistic
Definition Statistic.h:162
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI bool wouldInstructionBeTriviallyDead(const Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction would have no side effects if it was not used.
Definition Local.cpp:421
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
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
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1869
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
static bool refineInstruction(SCCPSolver &Solver, const SmallPtrSetImpl< Value * > &InsertedValues, Instruction &Inst)
Try to use Inst's value range from Solver to infer the NUW flag.
static void inferAttribute(Function *F, unsigned AttrIndex, const ValueLatticeElement &Val)
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
Struct to control some aspects related to merging constant ranges.
MergeOptions & setMaxWidenSteps(unsigned Steps=1)