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