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