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 // We are allowed to refine the comparison to either true or false for out
321 // of range inputs. Here we refine the comparison to true, i.e. we relax
322 // the range check.
323 auto NewCR = CR->exactUnionWith(LRange.inverse());
324 // TODO: Check if we can narrow the range check to an equality test.
325 // E.g, for X in [0, 4), X - 3 u< 2 -> X == 3
326 if (!NewCR)
327 return nullptr;
329 APInt RHS;
330 // Check if we can represent NewCR as an icmp predicate.
331 if (NewCR->getEquivalentICmp(Pred, RHS)) {
332 IRBuilder<NoFolder> Builder(&Inst);
333 Value *NewICmp =
334 Builder.CreateICmp(Pred, X, ConstantInt::get(X->getType(), RHS));
335 InsertedValues.insert(NewICmp);
336 return NewICmp;
337 }
338 }
339 }
340
341 return nullptr;
342}
343
345 SmallPtrSetImpl<Value *> &InsertedValues,
346 Statistic &InstRemovedStat,
347 Statistic &InstReplacedStat) {
348 bool MadeChanges = false;
349 for (Instruction &Inst : make_early_inc_range(BB)) {
350 if (Inst.getType()->isVoidTy())
351 continue;
352 if (tryToReplaceWithConstant(&Inst)) {
354 Inst.eraseFromParent();
355
356 MadeChanges = true;
357 ++InstRemovedStat;
358 } else if (replaceSignedInst(*this, InsertedValues, Inst)) {
359 MadeChanges = true;
360 ++InstReplacedStat;
361 } else if (refineInstruction(*this, InsertedValues, Inst)) {
362 MadeChanges = true;
363 } else if (auto *V = simplifyInstruction(*this, InsertedValues, Inst)) {
364 Inst.replaceAllUsesWith(V);
365 Inst.eraseFromParent();
366 ++InstRemovedStat;
367 MadeChanges = true;
368 }
369 }
370 return MadeChanges;
371}
372
374 BasicBlock *&NewUnreachableBB) const {
375 SmallPtrSet<BasicBlock *, 8> FeasibleSuccessors;
376 bool HasNonFeasibleEdges = false;
377 for (BasicBlock *Succ : successors(BB)) {
378 if (isEdgeFeasible(BB, Succ))
379 FeasibleSuccessors.insert(Succ);
380 else
381 HasNonFeasibleEdges = true;
382 }
383
384 // All edges feasible, nothing to do.
385 if (!HasNonFeasibleEdges)
386 return false;
387
388 // SCCP can only determine non-feasible edges for br, switch and indirectbr.
389 Instruction *TI = BB->getTerminator();
391 isa<IndirectBrInst>(TI)) &&
392 "Terminator must be a br, switch or indirectbr");
393
394 if (FeasibleSuccessors.size() == 0) {
395 // Branch on undef/poison, replace with unreachable.
398 for (BasicBlock *Succ : successors(BB)) {
399 Succ->removePredecessor(BB);
400 if (SeenSuccs.insert(Succ).second)
401 Updates.push_back({DominatorTree::Delete, BB, Succ});
402 }
403 TI->eraseFromParent();
404 new UnreachableInst(BB->getContext(), BB);
405 DTU.applyUpdatesPermissive(Updates);
406 } else if (FeasibleSuccessors.size() == 1) {
407 // Replace with an unconditional branch to the only feasible successor.
408 BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
410 bool HaveSeenOnlyFeasibleSuccessor = false;
411 for (BasicBlock *Succ : successors(BB)) {
412 if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
413 // Don't remove the edge to the only feasible successor the first time
414 // we see it. We still do need to remove any multi-edges to it though.
415 HaveSeenOnlyFeasibleSuccessor = true;
416 continue;
417 }
418
419 Succ->removePredecessor(BB);
420 Updates.push_back({DominatorTree::Delete, BB, Succ});
421 }
422
423 Instruction *BI = BranchInst::Create(OnlyFeasibleSuccessor, BB);
424 BI->setDebugLoc(TI->getDebugLoc());
425 TI->eraseFromParent();
426 DTU.applyUpdatesPermissive(Updates);
427 } else if (FeasibleSuccessors.size() > 1) {
430
431 // If the default destination is unfeasible it will never be taken. Replace
432 // it with a new block with a single Unreachable instruction.
433 BasicBlock *DefaultDest = SI->getDefaultDest();
434 if (!FeasibleSuccessors.contains(DefaultDest)) {
435 if (!NewUnreachableBB) {
436 NewUnreachableBB =
437 BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
438 DefaultDest->getParent(), DefaultDest);
439 auto *UI =
440 new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
441 UI->setDebugLoc(DebugLoc::getTemporary());
442 }
443
444 DefaultDest->removePredecessor(BB);
445 SI->setDefaultDest(NewUnreachableBB);
446 Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
447 Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
448 }
449
450 for (auto CI = SI->case_begin(); CI != SI->case_end();) {
451 if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
452 ++CI;
453 continue;
454 }
455
456 BasicBlock *Succ = CI->getCaseSuccessor();
457 Succ->removePredecessor(BB);
458 Updates.push_back({DominatorTree::Delete, BB, Succ});
459 SI.removeCase(CI);
460 // Don't increment CI, as we removed a case.
461 }
462
463 DTU.applyUpdatesPermissive(Updates);
464 } else {
465 llvm_unreachable("Must have at least one feasible successor");
466 }
467 return true;
468}
469
470static void inferAttribute(Function *F, unsigned AttrIndex,
471 const ValueLatticeElement &Val) {
472 // If there is a known constant range for the value, add range attribute.
473 if (Val.isConstantRange() && !Val.getConstantRange().isSingleElement()) {
474 // Do not add range attribute if the value may include undef.
476 return;
477
478 // Take the intersection of the existing attribute and the inferred range.
479 Attribute OldAttr = F->getAttributeAtIndex(AttrIndex, Attribute::Range);
481 if (OldAttr.isValid())
482 CR = CR.intersectWith(OldAttr.getRange());
483 F->addAttributeAtIndex(
484 AttrIndex, Attribute::get(F->getContext(), Attribute::Range, CR));
485 return;
486 }
487 // Infer nonnull attribute.
488 if (Val.isNotConstant() && Val.getNotConstant()->getType()->isPointerTy() &&
489 Val.getNotConstant()->isNullValue() &&
490 !F->hasAttributeAtIndex(AttrIndex, Attribute::NonNull)) {
491 F->addAttributeAtIndex(AttrIndex,
492 Attribute::get(F->getContext(), Attribute::NonNull));
493 }
494}
495
497 for (const auto &[F, ReturnValue] : getTrackedRetVals())
498 inferAttribute(F, AttributeList::ReturnIndex, ReturnValue);
499}
500
503 if (!isBlockExecutable(&F->front()))
504 continue;
505 for (Argument &A : F->args())
506 if (!A.getType()->isStructTy())
507 inferAttribute(F, AttributeList::FirstArgIndex + A.getArgNo(),
509 }
510}
511
512/// Helper class for SCCPSolver. This implements the instruction visitor and
513/// holds all the state.
514class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
515 const DataLayout &DL;
516 std::function<const TargetLibraryInfo &(Function &)> GetTLI;
517 /// Basic blocks that are executable (but may not have been visited yet).
518 SmallPtrSet<BasicBlock *, 8> BBExecutable;
519 /// Basic blocks that are executable and have been visited at least once.
522 ValueState; // The state each value is in.
523
524 /// StructValueState - This maintains ValueState for values that have
525 /// StructType, for example for formal arguments, calls, insertelement, etc.
527
528 /// GlobalValue - If we are tracking any values for the contents of a global
529 /// variable, we keep a mapping from the constant accessor to the element of
530 /// the global, to the currently known value. If the value becomes
531 /// overdefined, it's entry is simply removed from this map.
533
534 /// TrackedRetVals - If we are tracking arguments into and the return
535 /// value out of a function, it will have an entry in this map, indicating
536 /// what the known return value for the function is.
538
539 /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
540 /// that return multiple values.
542 TrackedMultipleRetVals;
543
544 /// The set of values whose lattice has been invalidated.
545 /// Populated by resetLatticeValueFor(), cleared after resolving undefs.
546 DenseSet<Value *> Invalidated;
547
548 /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
549 /// represented here for efficient lookup.
550 SmallPtrSet<Function *, 16> MRVFunctionsTracked;
551
552 /// A list of functions whose return cannot be modified.
553 SmallPtrSet<Function *, 16> MustPreserveReturnsInFunctions;
554
555 /// TrackingIncomingArguments - This is the set of functions for whose
556 /// arguments we make optimistic assumptions about and try to prove as
557 /// constants.
558 SmallPtrSet<Function *, 16> TrackingIncomingArguments;
559
560 /// Worklist of instructions to re-visit. This only includes instructions
561 /// in blocks that have already been visited at least once.
563
564 /// Current instruction while visiting a block for the first time, used to
565 /// avoid unnecessary instruction worklist insertions. Null if an instruction
566 /// is visited outside a whole-block visitation.
567 Instruction *CurI = nullptr;
568
569 // The BasicBlock work list
571
572 /// KnownFeasibleEdges - Entries in this set are edges which have already had
573 /// PHI nodes retriggered.
574 using Edge = std::pair<BasicBlock *, BasicBlock *>;
575 DenseSet<Edge> KnownFeasibleEdges;
576
578
580
581 LLVMContext &Ctx;
582
583 BumpPtrAllocator PredicateInfoAllocator;
584
585private:
586 ConstantInt *getConstantInt(const ValueLatticeElement &IV, Type *Ty) const {
588 }
589
590 /// Push instruction \p I to the worklist.
591 void pushToWorkList(Instruction *I);
592
593 /// Push users of value \p V to the worklist.
594 void pushUsersToWorkList(Value *V);
595
596 /// Like pushUsersToWorkList(), but also prints a debug message with the
597 /// updated value.
598 void pushUsersToWorkListMsg(ValueLatticeElement &IV, Value *V);
599
600 // markConstant - Make a value be marked as "constant". If the value
601 // is not already a constant, add it to the instruction work list so that
602 // the users of the instruction are updated later.
603 bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C,
604 bool MayIncludeUndef = false);
605
606 bool markConstant(Value *V, Constant *C) {
607 assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
608 return markConstant(ValueState[V], V, C);
609 }
610
611 bool markNotConstant(ValueLatticeElement &IV, Value *V, Constant *C);
612
613 bool markNotNull(ValueLatticeElement &IV, Value *V) {
614 return markNotConstant(IV, V, Constant::getNullValue(V->getType()));
615 }
616
617 /// markConstantRange - Mark the object as constant range with \p CR. If the
618 /// object is not a constant range with the range \p CR, add it to the
619 /// instruction work list so that the users of the instruction are updated
620 /// later.
621 bool markConstantRange(ValueLatticeElement &IV, Value *V,
622 const ConstantRange &CR);
623
624 // markOverdefined - Make a value be marked as "overdefined". If the
625 // value is not already overdefined, add it to the overdefined instruction
626 // work list so that the users of the instruction are updated later.
627 bool markOverdefined(ValueLatticeElement &IV, Value *V);
628
629 /// Merge \p MergeWithV into \p IV and push \p V to the worklist, if \p IV
630 /// changes.
631 bool mergeInValue(ValueLatticeElement &IV, Value *V,
632 ValueLatticeElement MergeWithV,
634 /*MayIncludeUndef=*/false, /*CheckWiden=*/false});
635
636 bool mergeInValue(Value *V, ValueLatticeElement MergeWithV,
638 /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
639 assert(!V->getType()->isStructTy() &&
640 "non-structs should use markConstant");
641 return mergeInValue(ValueState[V], V, MergeWithV, Opts);
642 }
643
644 /// getValueState - Return the ValueLatticeElement object that corresponds to
645 /// the value. This function handles the case when the value hasn't been seen
646 /// yet by properly seeding constants etc.
647 ValueLatticeElement &getValueState(Value *V) {
648 assert(!V->getType()->isStructTy() && "Should use getStructValueState");
649
650 auto I = ValueState.try_emplace(V);
651 ValueLatticeElement &LV = I.first->second;
652
653 if (!I.second)
654 return LV; // Common case, already in the map.
655
656 if (auto *C = dyn_cast<Constant>(V))
657 LV.markConstant(C); // Constants are constant
658
659 // All others are unknown by default.
660 return LV;
661 }
662
663 /// getStructValueState - Return the ValueLatticeElement object that
664 /// corresponds to the value/field pair. This function handles the case when
665 /// the value hasn't been seen yet by properly seeding constants etc.
666 ValueLatticeElement &getStructValueState(Value *V, unsigned i) {
667 assert(V->getType()->isStructTy() && "Should use getValueState");
668 assert(i < cast<StructType>(V->getType())->getNumElements() &&
669 "Invalid element #");
670
671 auto I = StructValueState.insert(
672 std::make_pair(std::make_pair(V, i), ValueLatticeElement()));
673 ValueLatticeElement &LV = I.first->second;
674
675 if (!I.second)
676 return LV; // Common case, already in the map.
677
678 if (auto *C = dyn_cast<Constant>(V)) {
679 Constant *Elt = C->getAggregateElement(i);
680
681 if (!Elt)
682 LV.markOverdefined(); // Unknown sort of constant.
683 else
684 LV.markConstant(Elt); // Constants are constant.
685 }
686
687 // All others are underdefined by default.
688 return LV;
689 }
690
691 /// Traverse the use-def chain of \p Call, marking itself and its users as
692 /// "unknown" on the way.
693 void invalidate(CallBase *Call) {
695 ToInvalidate.push_back(Call);
696
697 while (!ToInvalidate.empty()) {
698 Instruction *Inst = ToInvalidate.pop_back_val();
699
700 if (!Invalidated.insert(Inst).second)
701 continue;
702
703 if (!BBExecutable.count(Inst->getParent()))
704 continue;
705
706 Value *V = nullptr;
707 // For return instructions we need to invalidate the tracked returns map.
708 // Anything else has its lattice in the value map.
709 if (auto *RetInst = dyn_cast<ReturnInst>(Inst)) {
710 Function *F = RetInst->getParent()->getParent();
711 if (auto It = TrackedRetVals.find(F); It != TrackedRetVals.end()) {
712 It->second = ValueLatticeElement();
713 V = F;
714 } else if (MRVFunctionsTracked.count(F)) {
715 auto *STy = cast<StructType>(F->getReturnType());
716 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I)
717 TrackedMultipleRetVals[{F, I}] = ValueLatticeElement();
718 V = F;
719 }
720 } else if (auto *STy = dyn_cast<StructType>(Inst->getType())) {
721 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
722 if (auto It = StructValueState.find({Inst, I});
723 It != StructValueState.end()) {
724 It->second = ValueLatticeElement();
725 V = Inst;
726 }
727 }
728 } else if (auto It = ValueState.find(Inst); It != ValueState.end()) {
729 It->second = ValueLatticeElement();
730 V = Inst;
731 }
732
733 if (V) {
734 LLVM_DEBUG(dbgs() << "Invalidated lattice for " << *V << "\n");
735
736 for (User *U : V->users())
737 if (auto *UI = dyn_cast<Instruction>(U))
738 ToInvalidate.push_back(UI);
739
740 auto It = AdditionalUsers.find(V);
741 if (It != AdditionalUsers.end())
742 for (User *U : It->second)
743 if (auto *UI = dyn_cast<Instruction>(U))
744 ToInvalidate.push_back(UI);
745 }
746 }
747 }
748
749 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
750 /// work list if it is not already executable.
751 bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
752
753 // getFeasibleSuccessors - Return a vector of booleans to indicate which
754 // successors are reachable from a given terminator instruction.
755 void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs);
756
757 // Add U as additional user of V.
758 void addAdditionalUser(Value *V, User *U) { AdditionalUsers[V].insert(U); }
759
760 void handlePredicate(Instruction *I, Value *CopyOf, const PredicateBase *PI);
761 void handleCallOverdefined(CallBase &CB);
762 void handleCallResult(CallBase &CB);
763 void handleCallArguments(CallBase &CB);
764 void handleExtractOfWithOverflow(ExtractValueInst &EVI,
765 const WithOverflowInst *WO, unsigned Idx);
766
767private:
768 friend class InstVisitor<SCCPInstVisitor>;
769
770 // visit implementations - Something changed in this instruction. Either an
771 // operand made a transition, or the instruction is newly executable. Change
772 // the value type of I to reflect these changes if appropriate.
773 void visitPHINode(PHINode &I);
774
775 // Terminators
776
777 void visitReturnInst(ReturnInst &I);
778 void visitTerminator(Instruction &TI);
779
780 void visitCastInst(CastInst &I);
781 void visitSelectInst(SelectInst &I);
782 void visitUnaryOperator(Instruction &I);
783 void visitFreezeInst(FreezeInst &I);
784 void visitBinaryOperator(Instruction &I);
785 void visitCmpInst(CmpInst &I);
786 void visitExtractValueInst(ExtractValueInst &EVI);
787 void visitInsertValueInst(InsertValueInst &IVI);
788
789 void visitCatchSwitchInst(CatchSwitchInst &CPI) {
790 markOverdefined(&CPI);
791 visitTerminator(CPI);
792 }
793
794 // Instructions that cannot be folded away.
795
796 void visitStoreInst(StoreInst &I);
797 void visitLoadInst(LoadInst &I);
798 void visitGetElementPtrInst(GetElementPtrInst &I);
799 void visitAllocaInst(AllocaInst &AI);
800
801 void visitInvokeInst(InvokeInst &II) {
802 visitCallBase(II);
803 visitTerminator(II);
804 }
805
806 void visitCallBrInst(CallBrInst &CBI) {
807 visitCallBase(CBI);
808 visitTerminator(CBI);
809 }
810
811 void visitCallBase(CallBase &CB);
812 void visitResumeInst(ResumeInst &I) { /*returns void*/
813 }
814 void visitUnreachableInst(UnreachableInst &I) { /*returns void*/
815 }
816 void visitFenceInst(FenceInst &I) { /*returns void*/
817 }
818
819 void visitInstruction(Instruction &I);
820
821public:
823 FnPredicateInfo.insert({&F, std::make_unique<PredicateInfo>(
824 F, DT, AC, PredicateInfoAllocator)});
825 }
826
828 auto It = FnPredicateInfo.find(&F);
829 if (It == FnPredicateInfo.end())
830 return;
831
832 for (BasicBlock &BB : F) {
833 for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
834 if (auto *BC = dyn_cast<BitCastInst>(&Inst)) {
835 if (BC->getType() == BC->getOperand(0)->getType()) {
836 if (It->second->getPredicateInfoFor(&Inst)) {
837 Value *Op = BC->getOperand(0);
838 Inst.replaceAllUsesWith(Op);
839 Inst.eraseFromParent();
840 }
841 }
842 }
843 }
844 }
845 }
846
847 void visitCallInst(CallInst &I) { visitCallBase(I); }
848
850
852 auto It = FnPredicateInfo.find(I->getParent()->getParent());
853 if (It == FnPredicateInfo.end())
854 return nullptr;
855 return It->second->getPredicateInfoFor(I);
856 }
857
859 std::function<const TargetLibraryInfo &(Function &)> GetTLI,
860 LLVMContext &Ctx)
861 : DL(DL), GetTLI(GetTLI), Ctx(Ctx) {}
862
864 // We only track the contents of scalar globals.
865 if (GV->getValueType()->isSingleValueType()) {
866 ValueLatticeElement &IV = TrackedGlobals[GV];
867 IV.markConstant(GV->getInitializer());
868 }
869 }
870
872 // Add an entry, F -> undef.
873 if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
874 MRVFunctionsTracked.insert(F);
875 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
876 TrackedMultipleRetVals.try_emplace(std::make_pair(F, i));
877 } else if (!F->getReturnType()->isVoidTy())
878 TrackedRetVals.try_emplace(F);
879 }
880
882 MustPreserveReturnsInFunctions.insert(F);
883 }
884
886 return MustPreserveReturnsInFunctions.count(F);
887 }
888
890 TrackingIncomingArguments.insert(F);
891 }
892
894 return TrackingIncomingArguments.count(F);
895 }
896
898 return TrackingIncomingArguments;
899 }
900
901 void solve();
902
904
906
908 return BBExecutable.count(BB);
909 }
910
911 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
912
913 std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const {
914 std::vector<ValueLatticeElement> StructValues;
915 auto *STy = dyn_cast<StructType>(V->getType());
916 assert(STy && "getStructLatticeValueFor() can be called only on structs");
917 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
918 auto I = StructValueState.find(std::make_pair(V, i));
919 assert(I != StructValueState.end() && "Value not in valuemap!");
920 StructValues.push_back(I->second);
921 }
922 return StructValues;
923 }
924
925 void removeLatticeValueFor(Value *V) { ValueState.erase(V); }
926
927 /// Invalidate the Lattice Value of \p Call and its users after specializing
928 /// the call. Then recompute it.
930 // Calls to void returning functions do not need invalidation.
931 Function *F = Call->getCalledFunction();
932 (void)F;
933 assert(!F->getReturnType()->isVoidTy() &&
934 (TrackedRetVals.count(F) || MRVFunctionsTracked.count(F)) &&
935 "All non void specializations should be tracked");
936 invalidate(Call);
937 handleCallResult(*Call);
938 }
939
941 assert(!V->getType()->isStructTy() &&
942 "Should use getStructLatticeValueFor");
944 ValueState.find(V);
945 assert(I != ValueState.end() &&
946 "V not found in ValueState nor Paramstate map!");
947 return I->second;
948 }
949
951 return TrackedRetVals;
952 }
953
956 return TrackedGlobals;
957 }
958
960 return MRVFunctionsTracked;
961 }
962
964 if (auto *STy = dyn_cast<StructType>(V->getType()))
965 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
966 markOverdefined(getStructValueState(V, i), V);
967 else
968 markOverdefined(ValueState[V], V);
969 }
970
972 if (A->getType()->isIntOrIntVectorTy()) {
973 if (std::optional<ConstantRange> Range = A->getRange())
975 }
976 if (A->hasNonNullAttr())
978 // Assume nothing about the incoming arguments without attributes.
980 }
981
983 if (A->getType()->isStructTy())
984 return (void)markOverdefined(A);
985 mergeInValue(A, getArgAttributeVL(A));
986 }
987
989
990 Constant *getConstant(const ValueLatticeElement &LV, Type *Ty) const;
991
993
995 const SmallVectorImpl<ArgInfo> &Args);
996
998 for (auto &BB : *F)
999 BBExecutable.erase(&BB);
1000 }
1001
1003 bool ResolvedUndefs = true;
1004 while (ResolvedUndefs) {
1005 solve();
1006 ResolvedUndefs = false;
1007 for (Function &F : M)
1008 ResolvedUndefs |= resolvedUndefsIn(F);
1009 }
1010 }
1011
1013 bool ResolvedUndefs = true;
1014 while (ResolvedUndefs) {
1015 solve();
1016 ResolvedUndefs = false;
1017 for (Function *F : WorkList)
1018 ResolvedUndefs |= resolvedUndefsIn(*F);
1019 }
1020 }
1021
1023 bool ResolvedUndefs = true;
1024 while (ResolvedUndefs) {
1025 solve();
1026 ResolvedUndefs = false;
1027 for (Value *V : Invalidated)
1028 if (auto *I = dyn_cast<Instruction>(V))
1029 ResolvedUndefs |= resolvedUndef(*I);
1030 }
1031 Invalidated.clear();
1032 }
1033};
1034
1035} // namespace llvm
1036
1038 if (!BBExecutable.insert(BB).second)
1039 return false;
1040 LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
1041 BBWorkList.push_back(BB); // Add the block to the work list!
1042 return true;
1043}
1044
1045void SCCPInstVisitor::pushToWorkList(Instruction *I) {
1046 // If we're currently visiting a block, do not push any instructions in the
1047 // same blocks that are after the current one, as they will be visited
1048 // anyway. We do have to push updates to earlier instructions (e.g. phi
1049 // nodes or loads of tracked globals).
1050 if (CurI && I->getParent() == CurI->getParent() && !I->comesBefore(CurI))
1051 return;
1052 // Only push instructions in already visited blocks. Otherwise we'll handle
1053 // it when we visit the block for the first time.
1054 if (BBVisited.contains(I->getParent()))
1055 InstWorkList.insert(I);
1056}
1057
1058void SCCPInstVisitor::pushUsersToWorkList(Value *V) {
1059 for (User *U : V->users())
1060 if (auto *UI = dyn_cast<Instruction>(U))
1061 pushToWorkList(UI);
1062
1063 auto Iter = AdditionalUsers.find(V);
1064 if (Iter != AdditionalUsers.end()) {
1065 // Copy additional users before notifying them of changes, because new
1066 // users may be added, potentially invalidating the iterator.
1068 for (User *U : Iter->second)
1069 if (auto *UI = dyn_cast<Instruction>(U))
1070 ToNotify.push_back(UI);
1071 for (Instruction *UI : ToNotify)
1072 pushToWorkList(UI);
1073 }
1074}
1075
1076void SCCPInstVisitor::pushUsersToWorkListMsg(ValueLatticeElement &IV,
1077 Value *V) {
1078 LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n');
1079 pushUsersToWorkList(V);
1080}
1081
1082bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V,
1083 Constant *C, bool MayIncludeUndef) {
1084 if (!IV.markConstant(C, MayIncludeUndef))
1085 return false;
1086 LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
1087 pushUsersToWorkList(V);
1088 return true;
1089}
1090
1091bool SCCPInstVisitor::markNotConstant(ValueLatticeElement &IV, Value *V,
1092 Constant *C) {
1093 if (!IV.markNotConstant(C))
1094 return false;
1095 LLVM_DEBUG(dbgs() << "markNotConstant: " << *C << ": " << *V << '\n');
1096 pushUsersToWorkList(V);
1097 return true;
1098}
1099
1100bool SCCPInstVisitor::markConstantRange(ValueLatticeElement &IV, Value *V,
1101 const ConstantRange &CR) {
1102 if (!IV.markConstantRange(CR))
1103 return false;
1104 LLVM_DEBUG(dbgs() << "markConstantRange: " << CR << ": " << *V << '\n');
1105 pushUsersToWorkList(V);
1106 return true;
1107}
1108
1109bool SCCPInstVisitor::markOverdefined(ValueLatticeElement &IV, Value *V) {
1110 if (!IV.markOverdefined())
1111 return false;
1112
1113 LLVM_DEBUG(dbgs() << "markOverdefined: ";
1114 if (auto *F = dyn_cast<Function>(V)) dbgs()
1115 << "Function '" << F->getName() << "'\n";
1116 else dbgs() << *V << '\n');
1117 // Only instructions go on the work list
1118 pushUsersToWorkList(V);
1119 return true;
1120}
1121
1123 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1124 const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
1125 assert(It != TrackedMultipleRetVals.end());
1126 ValueLatticeElement LV = It->second;
1127 if (!SCCPSolver::isConstant(LV))
1128 return false;
1129 }
1130 return true;
1131}
1132
1134 Type *Ty) const {
1135 if (LV.isConstant()) {
1136 Constant *C = LV.getConstant();
1137 assert(C->getType() == Ty && "Type mismatch");
1138 return C;
1139 }
1140
1141 if (LV.isConstantRange()) {
1142 const auto &CR = LV.getConstantRange();
1143 if (CR.getSingleElement())
1144 return ConstantInt::get(Ty, *CR.getSingleElement());
1145 }
1146 return nullptr;
1147}
1148
1150 Constant *Const = nullptr;
1151 if (V->getType()->isStructTy()) {
1152 std::vector<ValueLatticeElement> LVs = getStructLatticeValueFor(V);
1154 return nullptr;
1155 std::vector<Constant *> ConstVals;
1156 auto *ST = cast<StructType>(V->getType());
1157 for (unsigned I = 0, E = ST->getNumElements(); I != E; ++I) {
1158 ValueLatticeElement LV = LVs[I];
1159 ConstVals.push_back(SCCPSolver::isConstant(LV)
1160 ? getConstant(LV, ST->getElementType(I))
1161 : UndefValue::get(ST->getElementType(I)));
1162 }
1163 Const = ConstantStruct::get(ST, ConstVals);
1164 } else {
1167 return nullptr;
1168 Const = SCCPSolver::isConstant(LV) ? getConstant(LV, V->getType())
1169 : UndefValue::get(V->getType());
1170 }
1171 assert(Const && "Constant is nullptr here!");
1172 return Const;
1173}
1174
1176 const SmallVectorImpl<ArgInfo> &Args) {
1177 assert(!Args.empty() && "Specialization without arguments");
1178 assert(F->arg_size() == Args[0].Formal->getParent()->arg_size() &&
1179 "Functions should have the same number of arguments");
1180
1181 auto Iter = Args.begin();
1182 Function::arg_iterator NewArg = F->arg_begin();
1183 Function::arg_iterator OldArg = Args[0].Formal->getParent()->arg_begin();
1184 for (auto End = F->arg_end(); NewArg != End; ++NewArg, ++OldArg) {
1185
1186 LLVM_DEBUG(dbgs() << "SCCP: Marking argument "
1187 << NewArg->getNameOrAsOperand() << "\n");
1188
1189 // Mark the argument constants in the new function
1190 // or copy the lattice state over from the old function.
1191 if (Iter != Args.end() && Iter->Formal == &*OldArg) {
1192 if (auto *STy = dyn_cast<StructType>(NewArg->getType())) {
1193 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
1194 ValueLatticeElement &NewValue = StructValueState[{&*NewArg, I}];
1195 NewValue.markConstant(Iter->Actual->getAggregateElement(I));
1196 }
1197 } else {
1198 ValueState[&*NewArg].markConstant(Iter->Actual);
1199 }
1200 ++Iter;
1201 } else {
1202 if (auto *STy = dyn_cast<StructType>(NewArg->getType())) {
1203 for (unsigned I = 0, E = STy->getNumElements(); I != E; ++I) {
1204 ValueLatticeElement &NewValue = StructValueState[{&*NewArg, I}];
1205 NewValue = StructValueState[{&*OldArg, I}];
1206 }
1207 } else {
1208 ValueLatticeElement &NewValue = ValueState[&*NewArg];
1209 NewValue = ValueState[&*OldArg];
1210 }
1211 }
1212 }
1213}
1214
1215void SCCPInstVisitor::visitInstruction(Instruction &I) {
1216 // All the instructions we don't do any special handling for just
1217 // go to overdefined.
1218 LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
1219 markOverdefined(&I);
1220}
1221
1222bool SCCPInstVisitor::mergeInValue(ValueLatticeElement &IV, Value *V,
1223 ValueLatticeElement MergeWithV,
1225 if (IV.mergeIn(MergeWithV, Opts)) {
1226 pushUsersToWorkList(V);
1227 LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : "
1228 << IV << "\n");
1229 return true;
1230 }
1231 return false;
1232}
1233
1234bool SCCPInstVisitor::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
1235 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
1236 return false; // This edge is already known to be executable!
1237
1238 if (!markBlockExecutable(Dest)) {
1239 // If the destination is already executable, we just made an *edge*
1240 // feasible that wasn't before. Revisit the PHI nodes in the block
1241 // because they have potentially new operands.
1242 LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
1243 << " -> " << Dest->getName() << '\n');
1244
1245 for (PHINode &PN : Dest->phis())
1246 pushToWorkList(&PN);
1247 }
1248 return true;
1249}
1250
1251// getFeasibleSuccessors - Return a vector of booleans to indicate which
1252// successors are reachable from a given terminator instruction.
1253void SCCPInstVisitor::getFeasibleSuccessors(Instruction &TI,
1254 SmallVectorImpl<bool> &Succs) {
1255 Succs.resize(TI.getNumSuccessors());
1256 if (auto *BI = dyn_cast<BranchInst>(&TI)) {
1257 if (BI->isUnconditional()) {
1258 Succs[0] = true;
1259 return;
1260 }
1261
1262 ValueLatticeElement BCValue = getValueState(BI->getCondition());
1263 ConstantInt *CI = getConstantInt(BCValue, BI->getCondition()->getType());
1264 if (!CI) {
1265 // Overdefined condition variables, and branches on unfoldable constant
1266 // conditions, mean the branch could go either way.
1267 if (!BCValue.isUnknownOrUndef())
1268 Succs[0] = Succs[1] = true;
1269 return;
1270 }
1271
1272 // Constant condition variables mean the branch can only go a single way.
1273 Succs[CI->isZero()] = true;
1274 return;
1275 }
1276
1277 // We cannot analyze special terminators, so consider all successors
1278 // executable.
1279 if (TI.isSpecialTerminator()) {
1280 Succs.assign(TI.getNumSuccessors(), true);
1281 return;
1282 }
1283
1284 if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
1285 if (!SI->getNumCases()) {
1286 Succs[0] = true;
1287 return;
1288 }
1289 const ValueLatticeElement &SCValue = getValueState(SI->getCondition());
1290 if (ConstantInt *CI =
1291 getConstantInt(SCValue, SI->getCondition()->getType())) {
1292 Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true;
1293 return;
1294 }
1295
1296 // TODO: Switch on undef is UB. Stop passing false once the rest of LLVM
1297 // is ready.
1298 if (SCValue.isConstantRange(/*UndefAllowed=*/false)) {
1299 const ConstantRange &Range = SCValue.getConstantRange();
1300 unsigned ReachableCaseCount = 0;
1301 for (const auto &Case : SI->cases()) {
1302 const APInt &CaseValue = Case.getCaseValue()->getValue();
1303 if (Range.contains(CaseValue)) {
1304 Succs[Case.getSuccessorIndex()] = true;
1305 ++ReachableCaseCount;
1306 }
1307 }
1308
1309 Succs[SI->case_default()->getSuccessorIndex()] =
1310 Range.isSizeLargerThan(ReachableCaseCount);
1311 return;
1312 }
1313
1314 // Overdefined or unknown condition? All destinations are executable!
1315 if (!SCValue.isUnknownOrUndef())
1316 Succs.assign(TI.getNumSuccessors(), true);
1317 return;
1318 }
1319
1320 // In case of indirect branch and its address is a blockaddress, we mark
1321 // the target as executable.
1322 if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
1323 // Casts are folded by visitCastInst.
1324 ValueLatticeElement IBRValue = getValueState(IBR->getAddress());
1326 getConstant(IBRValue, IBR->getAddress()->getType()));
1327 if (!Addr) { // Overdefined or unknown condition?
1328 // All destinations are executable!
1329 if (!IBRValue.isUnknownOrUndef())
1330 Succs.assign(TI.getNumSuccessors(), true);
1331 return;
1332 }
1333
1334 BasicBlock *T = Addr->getBasicBlock();
1335 assert(Addr->getFunction() == T->getParent() &&
1336 "Block address of a different function ?");
1337 for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
1338 // This is the target.
1339 if (IBR->getDestination(i) == T) {
1340 Succs[i] = true;
1341 return;
1342 }
1343 }
1344
1345 // If we didn't find our destination in the IBR successor list, then we
1346 // have undefined behavior. Its ok to assume no successor is executable.
1347 return;
1348 }
1349
1350 LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
1351 llvm_unreachable("SCCP: Don't know how to handle this terminator!");
1352}
1353
1354// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
1355// block to the 'To' basic block is currently feasible.
1357 // Check if we've called markEdgeExecutable on the edge yet. (We could
1358 // be more aggressive and try to consider edges which haven't been marked
1359 // yet, but there isn't any need.)
1360 return KnownFeasibleEdges.count(Edge(From, To));
1361}
1362
1363// visit Implementations - Something changed in this instruction, either an
1364// operand made a transition, or the instruction is newly executable. Change
1365// the value type of I to reflect these changes if appropriate. This method
1366// makes sure to do the following actions:
1367//
1368// 1. If a phi node merges two constants in, and has conflicting value coming
1369// from different branches, or if the PHI node merges in an overdefined
1370// value, then the PHI node becomes overdefined.
1371// 2. If a phi node merges only constants in, and they all agree on value, the
1372// PHI node becomes a constant value equal to that.
1373// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
1374// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
1375// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
1376// 6. If a conditional branch has a value that is constant, make the selected
1377// destination executable
1378// 7. If a conditional branch has a value that is overdefined, make all
1379// successors executable.
1380void SCCPInstVisitor::visitPHINode(PHINode &PN) {
1381 // If this PN returns a struct, just mark the result overdefined.
1382 // TODO: We could do a lot better than this if code actually uses this.
1383 if (PN.getType()->isStructTy())
1384 return (void)markOverdefined(&PN);
1385
1386 if (getValueState(&PN).isOverdefined())
1387 return; // Quick exit
1388
1389 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
1390 // and slow us down a lot. Just mark them overdefined.
1391 if (PN.getNumIncomingValues() > 64)
1392 return (void)markOverdefined(&PN);
1393
1394 unsigned NumActiveIncoming = 0;
1395
1396 // Look at all of the executable operands of the PHI node. If any of them
1397 // are overdefined, the PHI becomes overdefined as well. If they are all
1398 // constant, and they agree with each other, the PHI becomes the identical
1399 // constant. If they are constant and don't agree, the PHI is a constant
1400 // range. If there are no executable operands, the PHI remains unknown.
1401 ValueLatticeElement PhiState = getValueState(&PN);
1402 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1403 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
1404 continue;
1405
1406 ValueLatticeElement IV = getValueState(PN.getIncomingValue(i));
1407 PhiState.mergeIn(IV);
1408 NumActiveIncoming++;
1409 if (PhiState.isOverdefined())
1410 break;
1411 }
1412
1413 // We allow up to 1 range extension per active incoming value and one
1414 // additional extension. Note that we manually adjust the number of range
1415 // extensions to match the number of active incoming values. This helps to
1416 // limit multiple extensions caused by the same incoming value, if other
1417 // incoming values are equal.
1418 mergeInValue(&PN, PhiState,
1419 ValueLatticeElement::MergeOptions().setMaxWidenSteps(
1420 NumActiveIncoming + 1));
1421 ValueLatticeElement &PhiStateRef = getValueState(&PN);
1422 PhiStateRef.setNumRangeExtensions(
1423 std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions()));
1424}
1425
1426void SCCPInstVisitor::visitReturnInst(ReturnInst &I) {
1427 if (I.getNumOperands() == 0)
1428 return; // ret void
1429
1430 Function *F = I.getParent()->getParent();
1431 Value *ResultOp = I.getOperand(0);
1432
1433 // If we are tracking the return value of this function, merge it in.
1434 if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
1435 auto TFRVI = TrackedRetVals.find(F);
1436 if (TFRVI != TrackedRetVals.end()) {
1437 mergeInValue(TFRVI->second, F, getValueState(ResultOp));
1438 return;
1439 }
1440 }
1441
1442 // Handle functions that return multiple values.
1443 if (!TrackedMultipleRetVals.empty()) {
1444 if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
1445 if (MRVFunctionsTracked.count(F))
1446 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1447 mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
1448 getStructValueState(ResultOp, i));
1449 }
1450}
1451
1452void SCCPInstVisitor::visitTerminator(Instruction &TI) {
1453 SmallVector<bool, 16> SuccFeasible;
1454 getFeasibleSuccessors(TI, SuccFeasible);
1455
1456 BasicBlock *BB = TI.getParent();
1457
1458 // Mark all feasible successors executable.
1459 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
1460 if (SuccFeasible[i])
1461 markEdgeExecutable(BB, TI.getSuccessor(i));
1462}
1463
1464void SCCPInstVisitor::visitCastInst(CastInst &I) {
1465 // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1466 // discover a concrete value later.
1467 if (ValueState[&I].isOverdefined())
1468 return;
1469
1470 if (auto *BC = dyn_cast<BitCastInst>(&I)) {
1471 if (BC->getType() == BC->getOperand(0)->getType()) {
1472 if (const PredicateBase *PI = getPredicateInfoFor(&I)) {
1473 handlePredicate(&I, I.getOperand(0), PI);
1474 return;
1475 }
1476 }
1477 }
1478
1479 ValueLatticeElement OpSt = getValueState(I.getOperand(0));
1480 if (OpSt.isUnknownOrUndef())
1481 return;
1482
1483 if (Constant *OpC = getConstant(OpSt, I.getOperand(0)->getType())) {
1484 // Fold the constant as we build.
1485 if (Constant *C =
1486 ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL))
1487 return (void)markConstant(&I, C);
1488 }
1489
1490 // Ignore bitcasts, as they may change the number of vector elements.
1491 if (I.getDestTy()->isIntOrIntVectorTy() &&
1492 I.getSrcTy()->isIntOrIntVectorTy() &&
1493 I.getOpcode() != Instruction::BitCast) {
1494 auto &LV = getValueState(&I);
1495 ConstantRange OpRange =
1496 OpSt.asConstantRange(I.getSrcTy(), /*UndefAllowed=*/false);
1497
1498 Type *DestTy = I.getDestTy();
1499 ConstantRange Res = ConstantRange::getEmpty(DestTy->getScalarSizeInBits());
1500 if (auto *Trunc = dyn_cast<TruncInst>(&I))
1501 Res = OpRange.truncate(DestTy->getScalarSizeInBits(),
1502 Trunc->getNoWrapKind());
1503 else
1504 Res = OpRange.castOp(I.getOpcode(), DestTy->getScalarSizeInBits());
1505 mergeInValue(LV, &I, ValueLatticeElement::getRange(Res));
1506 } else
1507 markOverdefined(&I);
1508}
1509
1510void SCCPInstVisitor::handleExtractOfWithOverflow(ExtractValueInst &EVI,
1511 const WithOverflowInst *WO,
1512 unsigned Idx) {
1513 Value *LHS = WO->getLHS(), *RHS = WO->getRHS();
1514 ValueLatticeElement L = getValueState(LHS);
1515 ValueLatticeElement R = getValueState(RHS);
1516 addAdditionalUser(LHS, &EVI);
1517 addAdditionalUser(RHS, &EVI);
1518 if (L.isUnknownOrUndef() || R.isUnknownOrUndef())
1519 return; // Wait to resolve.
1520
1521 Type *Ty = LHS->getType();
1522 ConstantRange LR = L.asConstantRange(Ty, /*UndefAllowed=*/false);
1523 ConstantRange RR = R.asConstantRange(Ty, /*UndefAllowed=*/false);
1524 if (Idx == 0) {
1525 ConstantRange Res = LR.binaryOp(WO->getBinaryOp(), RR);
1526 mergeInValue(&EVI, ValueLatticeElement::getRange(Res));
1527 } else {
1528 assert(Idx == 1 && "Index can only be 0 or 1");
1529 ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
1530 WO->getBinaryOp(), RR, WO->getNoWrapKind());
1531 if (NWRegion.contains(LR))
1532 return (void)markConstant(&EVI, ConstantInt::getFalse(EVI.getType()));
1533 markOverdefined(&EVI);
1534 }
1535}
1536
1537void SCCPInstVisitor::visitExtractValueInst(ExtractValueInst &EVI) {
1538 // If this returns a struct, mark all elements over defined, we don't track
1539 // structs in structs.
1540 if (EVI.getType()->isStructTy())
1541 return (void)markOverdefined(&EVI);
1542
1543 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1544 // discover a concrete value later.
1545 if (ValueState[&EVI].isOverdefined())
1546 return (void)markOverdefined(&EVI);
1547
1548 // If this is extracting from more than one level of struct, we don't know.
1549 if (EVI.getNumIndices() != 1)
1550 return (void)markOverdefined(&EVI);
1551
1552 Value *AggVal = EVI.getAggregateOperand();
1553 if (AggVal->getType()->isStructTy()) {
1554 unsigned i = *EVI.idx_begin();
1555 if (auto *WO = dyn_cast<WithOverflowInst>(AggVal))
1556 return handleExtractOfWithOverflow(EVI, WO, i);
1557 ValueLatticeElement EltVal = getStructValueState(AggVal, i);
1558 mergeInValue(getValueState(&EVI), &EVI, EltVal);
1559 } else {
1560 // Otherwise, must be extracting from an array.
1561 return (void)markOverdefined(&EVI);
1562 }
1563}
1564
1565void SCCPInstVisitor::visitInsertValueInst(InsertValueInst &IVI) {
1566 auto *STy = dyn_cast<StructType>(IVI.getType());
1567 if (!STy)
1568 return (void)markOverdefined(&IVI);
1569
1570 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1571 // discover a concrete value later.
1572 if (ValueState[&IVI].isOverdefined())
1573 return (void)markOverdefined(&IVI);
1574
1575 // If this has more than one index, we can't handle it, drive all results to
1576 // undef.
1577 if (IVI.getNumIndices() != 1)
1578 return (void)markOverdefined(&IVI);
1579
1580 Value *Aggr = IVI.getAggregateOperand();
1581 unsigned Idx = *IVI.idx_begin();
1582
1583 // Compute the result based on what we're inserting.
1584 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1585 // This passes through all values that aren't the inserted element.
1586 if (i != Idx) {
1587 ValueLatticeElement EltVal = getStructValueState(Aggr, i);
1588 mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
1589 continue;
1590 }
1591
1592 Value *Val = IVI.getInsertedValueOperand();
1593 if (Val->getType()->isStructTy())
1594 // We don't track structs in structs.
1595 markOverdefined(getStructValueState(&IVI, i), &IVI);
1596 else {
1597 ValueLatticeElement InVal = getValueState(Val);
1598 mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
1599 }
1600 }
1601}
1602
1603void SCCPInstVisitor::visitSelectInst(SelectInst &I) {
1604 // If this select returns a struct, just mark the result overdefined.
1605 // TODO: We could do a lot better than this if code actually uses this.
1606 if (I.getType()->isStructTy())
1607 return (void)markOverdefined(&I);
1608
1609 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1610 // discover a concrete value later.
1611 if (ValueState[&I].isOverdefined())
1612 return (void)markOverdefined(&I);
1613
1614 ValueLatticeElement CondValue = getValueState(I.getCondition());
1615 if (CondValue.isUnknownOrUndef())
1616 return;
1617
1618 if (ConstantInt *CondCB =
1619 getConstantInt(CondValue, I.getCondition()->getType())) {
1620 Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
1621 mergeInValue(&I, getValueState(OpVal));
1622 return;
1623 }
1624
1625 // Otherwise, the condition is overdefined or a constant we can't evaluate.
1626 // See if we can produce something better than overdefined based on the T/F
1627 // value.
1628 ValueLatticeElement TVal = getValueState(I.getTrueValue());
1629 ValueLatticeElement FVal = getValueState(I.getFalseValue());
1630
1631 ValueLatticeElement &State = ValueState[&I];
1632 bool Changed = State.mergeIn(TVal);
1633 Changed |= State.mergeIn(FVal);
1634 if (Changed)
1635 pushUsersToWorkListMsg(State, &I);
1636}
1637
1638// Handle Unary Operators.
1639void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
1640 ValueLatticeElement V0State = getValueState(I.getOperand(0));
1641
1642 ValueLatticeElement &IV = ValueState[&I];
1643 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1644 // discover a concrete value later.
1645 if (IV.isOverdefined())
1646 return (void)markOverdefined(&I);
1647
1648 // If something is unknown/undef, wait for it to resolve.
1649 if (V0State.isUnknownOrUndef())
1650 return;
1651
1652 if (SCCPSolver::isConstant(V0State))
1653 if (Constant *C = ConstantFoldUnaryOpOperand(
1654 I.getOpcode(), getConstant(V0State, I.getType()), DL))
1655 return (void)markConstant(IV, &I, C);
1656
1657 markOverdefined(&I);
1658}
1659
1660void SCCPInstVisitor::visitFreezeInst(FreezeInst &I) {
1661 // If this freeze returns a struct, just mark the result overdefined.
1662 // TODO: We could do a lot better than this.
1663 if (I.getType()->isStructTy())
1664 return (void)markOverdefined(&I);
1665
1666 ValueLatticeElement V0State = getValueState(I.getOperand(0));
1667 ValueLatticeElement &IV = ValueState[&I];
1668 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1669 // discover a concrete value later.
1670 if (IV.isOverdefined())
1671 return (void)markOverdefined(&I);
1672
1673 // If something is unknown/undef, wait for it to resolve.
1674 if (V0State.isUnknownOrUndef())
1675 return;
1676
1677 if (SCCPSolver::isConstant(V0State) &&
1678 isGuaranteedNotToBeUndefOrPoison(getConstant(V0State, I.getType())))
1679 return (void)markConstant(IV, &I, getConstant(V0State, I.getType()));
1680
1681 markOverdefined(&I);
1682}
1683
1684// Handle Binary Operators.
1685void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
1686 ValueLatticeElement V1State = getValueState(I.getOperand(0));
1687 ValueLatticeElement V2State = getValueState(I.getOperand(1));
1688
1689 ValueLatticeElement &IV = ValueState[&I];
1690 if (IV.isOverdefined())
1691 return;
1692
1693 // If something is undef, wait for it to resolve.
1694 if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef())
1695 return;
1696
1697 if (V1State.isOverdefined() && V2State.isOverdefined())
1698 return (void)markOverdefined(&I);
1699
1700 // If either of the operands is a constant, try to fold it to a constant.
1701 // TODO: Use information from notconstant better.
1702 if ((V1State.isConstant() || V2State.isConstant())) {
1703 Value *V1 = SCCPSolver::isConstant(V1State)
1704 ? getConstant(V1State, I.getOperand(0)->getType())
1705 : I.getOperand(0);
1706 Value *V2 = SCCPSolver::isConstant(V2State)
1707 ? getConstant(V2State, I.getOperand(1)->getType())
1708 : I.getOperand(1);
1709 Value *R = simplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL, &I));
1710 auto *C = dyn_cast_or_null<Constant>(R);
1711 if (C) {
1712 // Conservatively assume that the result may be based on operands that may
1713 // be undef. Note that we use mergeInValue to combine the constant with
1714 // the existing lattice value for I, as different constants might be found
1715 // after one of the operands go to overdefined, e.g. due to one operand
1716 // being a special floating value.
1717 ValueLatticeElement NewV;
1718 NewV.markConstant(C, /*MayIncludeUndef=*/true);
1719 return (void)mergeInValue(&I, NewV);
1720 }
1721 }
1722
1723 // Only use ranges for binary operators on integers.
1724 if (!I.getType()->isIntOrIntVectorTy())
1725 return markOverdefined(&I);
1726
1727 // Try to simplify to a constant range.
1728 ConstantRange A =
1729 V1State.asConstantRange(I.getType(), /*UndefAllowed=*/false);
1730 ConstantRange B =
1731 V2State.asConstantRange(I.getType(), /*UndefAllowed=*/false);
1732
1733 auto *BO = cast<BinaryOperator>(&I);
1734 ConstantRange R = ConstantRange::getEmpty(I.getType()->getScalarSizeInBits());
1735 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO))
1736 R = A.overflowingBinaryOp(BO->getOpcode(), B, OBO->getNoWrapKind());
1737 else
1738 R = A.binaryOp(BO->getOpcode(), B);
1739 mergeInValue(&I, ValueLatticeElement::getRange(R));
1740
1741 // TODO: Currently we do not exploit special values that produce something
1742 // better than overdefined with an overdefined operand for vector or floating
1743 // point types, like and <4 x i32> overdefined, zeroinitializer.
1744}
1745
1746// Handle ICmpInst instruction.
1747void SCCPInstVisitor::visitCmpInst(CmpInst &I) {
1748 // Do not cache this lookup, getValueState calls later in the function might
1749 // invalidate the reference.
1750 if (ValueState[&I].isOverdefined())
1751 return (void)markOverdefined(&I);
1752
1753 Value *Op1 = I.getOperand(0);
1754 Value *Op2 = I.getOperand(1);
1755
1756 // For parameters, use ParamState which includes constant range info if
1757 // available.
1758 auto V1State = getValueState(Op1);
1759 auto V2State = getValueState(Op2);
1760
1761 Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State, DL);
1762 if (C) {
1763 ValueLatticeElement CV;
1764 CV.markConstant(C);
1765 mergeInValue(&I, CV);
1766 return;
1767 }
1768
1769 // If operands are still unknown, wait for it to resolve.
1770 if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) &&
1771 !SCCPSolver::isConstant(ValueState[&I]))
1772 return;
1773
1774 markOverdefined(&I);
1775}
1776
1777// Handle getelementptr instructions. If all operands are constants then we
1778// can turn this into a getelementptr ConstantExpr.
1779void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
1780 if (ValueState[&I].isOverdefined())
1781 return (void)markOverdefined(&I);
1782
1783 const ValueLatticeElement &PtrState = getValueState(I.getPointerOperand());
1784 if (PtrState.isUnknownOrUndef())
1785 return;
1786
1787 // gep inbounds/nuw of non-null is non-null.
1788 if (PtrState.isNotConstant() && PtrState.getNotConstant()->isNullValue()) {
1789 if (I.hasNoUnsignedWrap() ||
1790 (I.isInBounds() &&
1791 !NullPointerIsDefined(I.getFunction(), I.getAddressSpace())))
1792 return (void)markNotNull(ValueState[&I], &I);
1793 return (void)markOverdefined(&I);
1794 }
1795
1797 Operands.reserve(I.getNumOperands());
1798
1799 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
1800 ValueLatticeElement State = getValueState(I.getOperand(i));
1801 if (State.isUnknownOrUndef())
1802 return; // Operands are not resolved yet.
1803
1804 if (Constant *C = getConstant(State, I.getOperand(i)->getType())) {
1805 Operands.push_back(C);
1806 continue;
1807 }
1808
1809 return (void)markOverdefined(&I);
1810 }
1811
1812 if (Constant *C = ConstantFoldInstOperands(&I, Operands, DL))
1813 markConstant(&I, C);
1814 else
1815 markOverdefined(&I);
1816}
1817
1818void SCCPInstVisitor::visitAllocaInst(AllocaInst &I) {
1819 if (!NullPointerIsDefined(I.getFunction(), I.getAddressSpace()))
1820 return (void)markNotNull(ValueState[&I], &I);
1821
1822 markOverdefined(&I);
1823}
1824
1825void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
1826 // If this store is of a struct, ignore it.
1827 if (SI.getOperand(0)->getType()->isStructTy())
1828 return;
1829
1830 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1831 return;
1832
1833 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1834 auto I = TrackedGlobals.find(GV);
1835 if (I == TrackedGlobals.end())
1836 return;
1837
1838 // Get the value we are storing into the global, then merge it.
1839 mergeInValue(I->second, GV, getValueState(SI.getOperand(0)),
1840 ValueLatticeElement::MergeOptions().setCheckWiden(false));
1841 if (I->second.isOverdefined())
1842 TrackedGlobals.erase(I); // No need to keep tracking this!
1843}
1844
1846 if (const auto *CB = dyn_cast<CallBase>(I)) {
1847 if (CB->getType()->isIntOrIntVectorTy())
1848 if (std::optional<ConstantRange> Range = CB->getRange())
1850 if (CB->getType()->isPointerTy() && CB->isReturnNonNull())
1853 }
1854
1855 if (I->getType()->isIntOrIntVectorTy())
1856 if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
1859 if (I->hasMetadata(LLVMContext::MD_nonnull))
1862
1864}
1865
1866// Handle load instructions. If the operand is a constant pointer to a constant
1867// global, we can replace the load with the loaded constant value!
1868void SCCPInstVisitor::visitLoadInst(LoadInst &I) {
1869 // If this load is of a struct or the load is volatile, just mark the result
1870 // as overdefined.
1871 if (I.getType()->isStructTy() || I.isVolatile())
1872 return (void)markOverdefined(&I);
1873
1874 // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1875 // discover a concrete value later.
1876 if (ValueState[&I].isOverdefined())
1877 return (void)markOverdefined(&I);
1878
1879 ValueLatticeElement PtrVal = getValueState(I.getOperand(0));
1880 if (PtrVal.isUnknownOrUndef())
1881 return; // The pointer is not resolved yet!
1882
1883 ValueLatticeElement &IV = ValueState[&I];
1884
1885 if (SCCPSolver::isConstant(PtrVal)) {
1886 Constant *Ptr = getConstant(PtrVal, I.getOperand(0)->getType());
1887
1888 // load null is undefined.
1890 if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1891 return (void)markOverdefined(IV, &I);
1892 else
1893 return;
1894 }
1895
1896 // Transform load (constant global) into the value loaded.
1897 if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1898 if (!TrackedGlobals.empty()) {
1899 // If we are tracking this global, merge in the known value for it.
1900 auto It = TrackedGlobals.find(GV);
1901 if (It != TrackedGlobals.end()) {
1902 mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1903 return;
1904 }
1905 }
1906 }
1907
1908 // Transform load from a constant into a constant if possible.
1909 if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL))
1910 return (void)markConstant(IV, &I, C);
1911 }
1912
1913 // Fall back to metadata.
1914 mergeInValue(&I, getValueFromMetadata(&I));
1915}
1916
1917void SCCPInstVisitor::visitCallBase(CallBase &CB) {
1918 handleCallResult(CB);
1919 handleCallArguments(CB);
1920}
1921
1922void SCCPInstVisitor::handleCallOverdefined(CallBase &CB) {
1924
1925 // Void return and not tracking callee, just bail.
1926 if (CB.getType()->isVoidTy())
1927 return;
1928
1929 // Always mark struct return as overdefined.
1930 if (CB.getType()->isStructTy())
1931 return (void)markOverdefined(&CB);
1932
1933 // Otherwise, if we have a single return value case, and if the function is
1934 // a declaration, maybe we can constant fold it.
1935 if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
1937 for (const Use &A : CB.args()) {
1938 if (A.get()->getType()->isStructTy())
1939 return markOverdefined(&CB); // Can't handle struct args.
1940 if (A.get()->getType()->isMetadataTy())
1941 continue; // Carried in CB, not allowed in Operands.
1942 ValueLatticeElement State = getValueState(A);
1943
1944 if (State.isUnknownOrUndef())
1945 return; // Operands are not resolved yet.
1946 if (SCCPSolver::isOverdefined(State))
1947 return (void)markOverdefined(&CB);
1948 assert(SCCPSolver::isConstant(State) && "Unknown state!");
1949 Operands.push_back(getConstant(State, A->getType()));
1950 }
1951
1952 if (SCCPSolver::isOverdefined(getValueState(&CB)))
1953 return (void)markOverdefined(&CB);
1954
1955 // If we can constant fold this, mark the result of the call as a
1956 // constant.
1957 if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F)))
1958 return (void)markConstant(&CB, C);
1959 }
1960
1961 // Fall back to metadata.
1962 mergeInValue(&CB, getValueFromMetadata(&CB));
1963}
1964
1965void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
1967 // If this is a local function that doesn't have its address taken, mark its
1968 // entry block executable and merge in the actual arguments to the call into
1969 // the formal arguments of the function.
1970 if (TrackingIncomingArguments.count(F)) {
1971 markBlockExecutable(&F->front());
1972
1973 // Propagate information from this call site into the callee.
1974 auto CAI = CB.arg_begin();
1975 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
1976 ++AI, ++CAI) {
1977 // If this argument is byval, and if the function is not readonly, there
1978 // will be an implicit copy formed of the input aggregate.
1979 if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1980 markOverdefined(&*AI);
1981 continue;
1982 }
1983
1984 if (auto *STy = dyn_cast<StructType>(AI->getType())) {
1985 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1986 ValueLatticeElement CallArg = getStructValueState(*CAI, i);
1987 mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg,
1989 }
1990 } else
1991 mergeInValue(&*AI,
1992 getValueState(*CAI).intersect(getArgAttributeVL(&*AI)),
1994 }
1995 }
1996}
1997
1998void SCCPInstVisitor::handlePredicate(Instruction *I, Value *CopyOf,
1999 const PredicateBase *PI) {
2000 ValueLatticeElement CopyOfVal = getValueState(CopyOf);
2001 const std::optional<PredicateConstraint> &Constraint = PI->getConstraint();
2002 if (!Constraint) {
2003 mergeInValue(ValueState[I], I, CopyOfVal);
2004 return;
2005 }
2006
2007 CmpInst::Predicate Pred = Constraint->Predicate;
2008 Value *OtherOp = Constraint->OtherOp;
2009
2010 // Wait until OtherOp is resolved.
2011 if (getValueState(OtherOp).isUnknown()) {
2012 addAdditionalUser(OtherOp, I);
2013 return;
2014 }
2015
2016 ValueLatticeElement CondVal = getValueState(OtherOp);
2017 ValueLatticeElement &IV = ValueState[I];
2018 if (CondVal.isConstantRange() || CopyOfVal.isConstantRange()) {
2019 auto ImposedCR =
2020 ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
2021
2022 // Get the range imposed by the condition.
2023 if (CondVal.isConstantRange())
2025 Pred, CondVal.getConstantRange());
2026
2027 // Combine range info for the original value with the new range from the
2028 // condition.
2029 auto CopyOfCR = CopyOfVal.asConstantRange(CopyOf->getType(),
2030 /*UndefAllowed=*/true);
2031 // Treat an unresolved input like a full range.
2032 if (CopyOfCR.isEmptySet())
2033 CopyOfCR = ConstantRange::getFull(CopyOfCR.getBitWidth());
2034 auto NewCR = ImposedCR.intersectWith(CopyOfCR);
2035 // If the existing information is != x, do not use the information from
2036 // a chained predicate, as the != x information is more likely to be
2037 // helpful in practice.
2038 if (!CopyOfCR.contains(NewCR) && CopyOfCR.getSingleMissingElement())
2039 NewCR = CopyOfCR;
2040
2041 // The new range is based on a branch condition. That guarantees that
2042 // neither of the compare operands can be undef in the branch targets,
2043 // unless we have conditions that are always true/false (e.g. icmp ule
2044 // i32, %a, i32_max). For the latter overdefined/empty range will be
2045 // inferred, but the branch will get folded accordingly anyways.
2046 addAdditionalUser(OtherOp, I);
2047 mergeInValue(
2048 IV, I, ValueLatticeElement::getRange(NewCR, /*MayIncludeUndef*/ false));
2049 return;
2050 } else if (Pred == CmpInst::ICMP_EQ &&
2051 (CondVal.isConstant() || CondVal.isNotConstant())) {
2052 // For non-integer values or integer constant expressions, only
2053 // propagate equal constants or not-constants.
2054 addAdditionalUser(OtherOp, I);
2055 mergeInValue(IV, I, CondVal);
2056 return;
2057 } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant()) {
2058 // Propagate inequalities.
2059 addAdditionalUser(OtherOp, I);
2060 mergeInValue(IV, I, ValueLatticeElement::getNot(CondVal.getConstant()));
2061 return;
2062 }
2063
2064 return (void)mergeInValue(IV, I, CopyOfVal);
2065}
2066
2067void SCCPInstVisitor::handleCallResult(CallBase &CB) {
2069
2070 if (auto *II = dyn_cast<IntrinsicInst>(&CB)) {
2071 if (II->getIntrinsicID() == Intrinsic::vscale) {
2072 unsigned BitWidth = CB.getType()->getScalarSizeInBits();
2073 const ConstantRange Result = getVScaleRange(II->getFunction(), BitWidth);
2074 return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
2075 }
2076
2077 if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
2078 // Compute result range for intrinsics supported by ConstantRange.
2079 // Do this even if we don't know a range for all operands, as we may
2080 // still know something about the result range, e.g. of abs(x).
2082 for (Value *Op : II->args()) {
2083 const ValueLatticeElement &State = getValueState(Op);
2084 if (State.isUnknownOrUndef())
2085 return;
2086 OpRanges.push_back(
2087 State.asConstantRange(Op->getType(), /*UndefAllowed=*/false));
2088 }
2089
2090 ConstantRange Result =
2091 ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges);
2092 return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
2093 }
2094 }
2095
2096 // The common case is that we aren't tracking the callee, either because we
2097 // are not doing interprocedural analysis or the callee is indirect, or is
2098 // external. Handle these cases first.
2099 if (!F || F->isDeclaration())
2100 return handleCallOverdefined(CB);
2101
2102 // If this is a single/zero retval case, see if we're tracking the function.
2103 if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
2104 if (!MRVFunctionsTracked.count(F))
2105 return handleCallOverdefined(CB); // Not tracking this callee.
2106
2107 // If we are tracking this callee, propagate the result of the function
2108 // into this call site.
2109 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2110 mergeInValue(getStructValueState(&CB, i), &CB,
2111 TrackedMultipleRetVals[std::make_pair(F, i)],
2113 } else {
2114 auto TFRVI = TrackedRetVals.find(F);
2115 if (TFRVI == TrackedRetVals.end())
2116 return handleCallOverdefined(CB); // Not tracking this callee.
2117
2118 // If so, propagate the return value of the callee into this call result.
2119 mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts());
2120 }
2121}
2122
2124 // Process the work lists until they are empty!
2125 while (!BBWorkList.empty() || !InstWorkList.empty()) {
2126 // Process the instruction work list.
2127 while (!InstWorkList.empty()) {
2128 Instruction *I = InstWorkList.pop_back_val();
2129 Invalidated.erase(I);
2130
2131 LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
2132
2133 visit(I);
2134 }
2135
2136 // Process the basic block work list.
2137 while (!BBWorkList.empty()) {
2138 BasicBlock *BB = BBWorkList.pop_back_val();
2139 BBVisited.insert(BB);
2140
2141 LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
2142 for (Instruction &I : *BB) {
2143 CurI = &I;
2144 visit(I);
2145 }
2146 CurI = nullptr;
2147 }
2148 }
2149}
2150
2152 // Look for instructions which produce undef values.
2153 if (I.getType()->isVoidTy())
2154 return false;
2155
2156 if (auto *STy = dyn_cast<StructType>(I.getType())) {
2157 // Only a few things that can be structs matter for undef.
2158
2159 // Tracked calls must never be marked overdefined in resolvedUndefsIn.
2160 if (auto *CB = dyn_cast<CallBase>(&I))
2161 if (Function *F = CB->getCalledFunction())
2162 if (MRVFunctionsTracked.count(F))
2163 return false;
2164
2165 // extractvalue and insertvalue don't need to be marked; they are
2166 // tracked as precisely as their operands.
2168 return false;
2169 // Send the results of everything else to overdefined. We could be
2170 // more precise than this but it isn't worth bothering.
2171 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2172 ValueLatticeElement &LV = getStructValueState(&I, i);
2173 if (LV.isUnknown()) {
2174 markOverdefined(LV, &I);
2175 return true;
2176 }
2177 }
2178 return false;
2179 }
2180
2181 ValueLatticeElement &LV = getValueState(&I);
2182 if (!LV.isUnknown())
2183 return false;
2184
2185 // There are two reasons a call can have an undef result
2186 // 1. It could be tracked.
2187 // 2. It could be constant-foldable.
2188 // Because of the way we solve return values, tracked calls must
2189 // never be marked overdefined in resolvedUndefsIn.
2190 if (auto *CB = dyn_cast<CallBase>(&I))
2191 if (Function *F = CB->getCalledFunction())
2192 if (TrackedRetVals.count(F))
2193 return false;
2194
2195 if (isa<LoadInst>(I)) {
2196 // A load here means one of two things: a load of undef from a global,
2197 // a load from an unknown pointer. Either way, having it return undef
2198 // is okay.
2199 return false;
2200 }
2201
2202 markOverdefined(&I);
2203 return true;
2204}
2205
2206/// While solving the dataflow for a function, we don't compute a result for
2207/// operations with an undef operand, to allow undef to be lowered to a
2208/// constant later. For example, constant folding of "zext i8 undef to i16"
2209/// would result in "i16 0", and if undef is later lowered to "i8 1", then the
2210/// zext result would become "i16 1" and would result into an overdefined
2211/// lattice value once merged with the previous result. Not computing the
2212/// result of the zext (treating undef the same as unknown) allows us to handle
2213/// a later undef->constant lowering more optimally.
2214///
2215/// However, if the operand remains undef when the solver returns, we do need
2216/// to assign some result to the instruction (otherwise we would treat it as
2217/// unreachable). For simplicity, we mark any instructions that are still
2218/// unknown as overdefined.
2220 bool MadeChange = false;
2221 for (BasicBlock &BB : F) {
2222 if (!BBExecutable.count(&BB))
2223 continue;
2224
2225 for (Instruction &I : BB)
2226 MadeChange |= resolvedUndef(I);
2227 }
2228
2229 LLVM_DEBUG(if (MadeChange) dbgs()
2230 << "\nResolved undefs in " << F.getName() << '\n');
2231
2232 return MadeChange;
2233}
2234
2235//===----------------------------------------------------------------------===//
2236//
2237// SCCPSolver implementations
2238//
2240 const DataLayout &DL,
2241 std::function<const TargetLibraryInfo &(Function &)> GetTLI,
2242 LLVMContext &Ctx)
2243 : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {}
2244
2245SCCPSolver::~SCCPSolver() = default;
2246
2248 AssumptionCache &AC) {
2249 Visitor->addPredicateInfo(F, DT, AC);
2250}
2251
2253 Visitor->removeSSACopies(F);
2254}
2255
2257 return Visitor->markBlockExecutable(BB);
2258}
2259
2261 return Visitor->getPredicateInfoFor(I);
2262}
2263
2265 Visitor->trackValueOfGlobalVariable(GV);
2266}
2267
2269 Visitor->addTrackedFunction(F);
2270}
2271
2273 Visitor->addToMustPreserveReturnsInFunctions(F);
2274}
2275
2277 return Visitor->mustPreserveReturn(F);
2278}
2279
2281 Visitor->addArgumentTrackedFunction(F);
2282}
2283
2285 return Visitor->isArgumentTrackedFunction(F);
2286}
2287
2290 return Visitor->getArgumentTrackedFunctions();
2291}
2292
2293void SCCPSolver::solve() { Visitor->solve(); }
2294
2296 return Visitor->resolvedUndefsIn(F);
2297}
2298
2300 Visitor->solveWhileResolvedUndefsIn(M);
2301}
2302
2303void
2305 Visitor->solveWhileResolvedUndefsIn(WorkList);
2306}
2307
2309 Visitor->solveWhileResolvedUndefs();
2310}
2311
2313 return Visitor->isBlockExecutable(BB);
2314}
2315
2317 return Visitor->isEdgeFeasible(From, To);
2318}
2319
2320std::vector<ValueLatticeElement>
2322 return Visitor->getStructLatticeValueFor(V);
2323}
2324
2326 return Visitor->removeLatticeValueFor(V);
2327}
2328
2330 Visitor->resetLatticeValueFor(Call);
2331}
2332
2334 return Visitor->getLatticeValueFor(V);
2335}
2336
2339 return Visitor->getTrackedRetVals();
2340}
2341
2344 return Visitor->getTrackedGlobals();
2345}
2346
2348 return Visitor->getMRVFunctionsTracked();
2349}
2350
2351void SCCPSolver::markOverdefined(Value *V) { Visitor->markOverdefined(V); }
2352
2354 Visitor->trackValueOfArgument(V);
2355}
2356
2358 return Visitor->isStructLatticeConstant(F, STy);
2359}
2360
2362 Type *Ty) const {
2363 return Visitor->getConstant(LV, Ty);
2364}
2365
2367 return Visitor->getConstantOrNull(V);
2368}
2369
2371 const SmallVectorImpl<ArgInfo> &Args) {
2372 Visitor->setLatticeValueForSpecializationArguments(F, Args);
2373}
2374
2376 Visitor->markFunctionUnreachable(F);
2377}
2378
2379void SCCPSolver::visit(Instruction *I) { Visitor->visit(I); }
2380
2381void 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:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ ICMP_NE
not equal
Definition InstrTypes.h:700
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:704
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:269
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:2780
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:1077
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:356
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:130
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)
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".
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
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:1705
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:649
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:759
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:1712
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:1847
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
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:851
Struct to control some aspects related to merging constant ranges.
MergeOptions & setMaxWidenSteps(unsigned Steps=1)