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