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