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