xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Utils/SCCPSolver.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1fe6060f1SDimitry Andric //===- SCCPSolver.cpp - SCCP Utility --------------------------- *- C++ -*-===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric //
9fe6060f1SDimitry Andric // \file
10fe6060f1SDimitry Andric // This file implements the Sparse Conditional Constant Propagation (SCCP)
11fe6060f1SDimitry Andric // utility.
12fe6060f1SDimitry Andric //
13fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
14fe6060f1SDimitry Andric 
15fe6060f1SDimitry Andric #include "llvm/Transforms/Utils/SCCPSolver.h"
16fe6060f1SDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
17fe6060f1SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
18fe6060f1SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
19fe6060f1SDimitry Andric #include "llvm/InitializePasses.h"
20fe6060f1SDimitry Andric #include "llvm/Pass.h"
21fe6060f1SDimitry Andric #include "llvm/Support/Casting.h"
22fe6060f1SDimitry Andric #include "llvm/Support/Debug.h"
23fe6060f1SDimitry Andric #include "llvm/Support/ErrorHandling.h"
24fe6060f1SDimitry Andric #include "llvm/Support/raw_ostream.h"
25fe6060f1SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
26fe6060f1SDimitry Andric #include <cassert>
27fe6060f1SDimitry Andric #include <utility>
28fe6060f1SDimitry Andric #include <vector>
29fe6060f1SDimitry Andric 
30fe6060f1SDimitry Andric using namespace llvm;
31fe6060f1SDimitry Andric 
32fe6060f1SDimitry Andric #define DEBUG_TYPE "sccp"
33fe6060f1SDimitry Andric 
34fe6060f1SDimitry Andric // The maximum number of range extensions allowed for operations requiring
35fe6060f1SDimitry Andric // widening.
36fe6060f1SDimitry Andric static const unsigned MaxNumRangeExtensions = 10;
37fe6060f1SDimitry Andric 
38fe6060f1SDimitry Andric /// Returns MergeOptions with MaxWidenSteps set to MaxNumRangeExtensions.
39fe6060f1SDimitry Andric static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts() {
40fe6060f1SDimitry Andric   return ValueLatticeElement::MergeOptions().setMaxWidenSteps(
41fe6060f1SDimitry Andric       MaxNumRangeExtensions);
42fe6060f1SDimitry Andric }
43fe6060f1SDimitry Andric 
44fe6060f1SDimitry Andric namespace {
45fe6060f1SDimitry Andric 
46fe6060f1SDimitry Andric // Helper to check if \p LV is either a constant or a constant
47fe6060f1SDimitry Andric // range with a single element. This should cover exactly the same cases as the
48fe6060f1SDimitry Andric // old ValueLatticeElement::isConstant() and is intended to be used in the
49fe6060f1SDimitry Andric // transition to ValueLatticeElement.
50fe6060f1SDimitry Andric bool isConstant(const ValueLatticeElement &LV) {
51fe6060f1SDimitry Andric   return LV.isConstant() ||
52fe6060f1SDimitry Andric          (LV.isConstantRange() && LV.getConstantRange().isSingleElement());
53fe6060f1SDimitry Andric }
54fe6060f1SDimitry Andric 
55fe6060f1SDimitry Andric // Helper to check if \p LV is either overdefined or a constant range with more
56fe6060f1SDimitry Andric // than a single element. This should cover exactly the same cases as the old
57fe6060f1SDimitry Andric // ValueLatticeElement::isOverdefined() and is intended to be used in the
58fe6060f1SDimitry Andric // transition to ValueLatticeElement.
59fe6060f1SDimitry Andric bool isOverdefined(const ValueLatticeElement &LV) {
60fe6060f1SDimitry Andric   return !LV.isUnknownOrUndef() && !isConstant(LV);
61fe6060f1SDimitry Andric }
62fe6060f1SDimitry Andric 
63fe6060f1SDimitry Andric } // namespace
64fe6060f1SDimitry Andric 
65fe6060f1SDimitry Andric namespace llvm {
66fe6060f1SDimitry Andric 
67fe6060f1SDimitry Andric /// Helper class for SCCPSolver. This implements the instruction visitor and
68fe6060f1SDimitry Andric /// holds all the state.
69fe6060f1SDimitry Andric class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
70fe6060f1SDimitry Andric   const DataLayout &DL;
71fe6060f1SDimitry Andric   std::function<const TargetLibraryInfo &(Function &)> GetTLI;
72fe6060f1SDimitry Andric   SmallPtrSet<BasicBlock *, 8> BBExecutable; // The BBs that are executable.
73fe6060f1SDimitry Andric   DenseMap<Value *, ValueLatticeElement>
74fe6060f1SDimitry Andric       ValueState; // The state each value is in.
75fe6060f1SDimitry Andric 
76fe6060f1SDimitry Andric   /// StructValueState - This maintains ValueState for values that have
77fe6060f1SDimitry Andric   /// StructType, for example for formal arguments, calls, insertelement, etc.
78fe6060f1SDimitry Andric   DenseMap<std::pair<Value *, unsigned>, ValueLatticeElement> StructValueState;
79fe6060f1SDimitry Andric 
80fe6060f1SDimitry Andric   /// GlobalValue - If we are tracking any values for the contents of a global
81fe6060f1SDimitry Andric   /// variable, we keep a mapping from the constant accessor to the element of
82fe6060f1SDimitry Andric   /// the global, to the currently known value.  If the value becomes
83fe6060f1SDimitry Andric   /// overdefined, it's entry is simply removed from this map.
84fe6060f1SDimitry Andric   DenseMap<GlobalVariable *, ValueLatticeElement> TrackedGlobals;
85fe6060f1SDimitry Andric 
86fe6060f1SDimitry Andric   /// TrackedRetVals - If we are tracking arguments into and the return
87fe6060f1SDimitry Andric   /// value out of a function, it will have an entry in this map, indicating
88fe6060f1SDimitry Andric   /// what the known return value for the function is.
89fe6060f1SDimitry Andric   MapVector<Function *, ValueLatticeElement> TrackedRetVals;
90fe6060f1SDimitry Andric 
91fe6060f1SDimitry Andric   /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
92fe6060f1SDimitry Andric   /// that return multiple values.
93fe6060f1SDimitry Andric   MapVector<std::pair<Function *, unsigned>, ValueLatticeElement>
94fe6060f1SDimitry Andric       TrackedMultipleRetVals;
95fe6060f1SDimitry Andric 
96fe6060f1SDimitry Andric   /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
97fe6060f1SDimitry Andric   /// represented here for efficient lookup.
98fe6060f1SDimitry Andric   SmallPtrSet<Function *, 16> MRVFunctionsTracked;
99fe6060f1SDimitry Andric 
100fe6060f1SDimitry Andric   /// A list of functions whose return cannot be modified.
101fe6060f1SDimitry Andric   SmallPtrSet<Function *, 16> MustPreserveReturnsInFunctions;
102fe6060f1SDimitry Andric 
103fe6060f1SDimitry Andric   /// TrackingIncomingArguments - This is the set of functions for whose
104fe6060f1SDimitry Andric   /// arguments we make optimistic assumptions about and try to prove as
105fe6060f1SDimitry Andric   /// constants.
106fe6060f1SDimitry Andric   SmallPtrSet<Function *, 16> TrackingIncomingArguments;
107fe6060f1SDimitry Andric 
108fe6060f1SDimitry Andric   /// The reason for two worklists is that overdefined is the lowest state
109fe6060f1SDimitry Andric   /// on the lattice, and moving things to overdefined as fast as possible
110fe6060f1SDimitry Andric   /// makes SCCP converge much faster.
111fe6060f1SDimitry Andric   ///
112fe6060f1SDimitry Andric   /// By having a separate worklist, we accomplish this because everything
113fe6060f1SDimitry Andric   /// possibly overdefined will become overdefined at the soonest possible
114fe6060f1SDimitry Andric   /// point.
115fe6060f1SDimitry Andric   SmallVector<Value *, 64> OverdefinedInstWorkList;
116fe6060f1SDimitry Andric   SmallVector<Value *, 64> InstWorkList;
117fe6060f1SDimitry Andric 
118fe6060f1SDimitry Andric   // The BasicBlock work list
119fe6060f1SDimitry Andric   SmallVector<BasicBlock *, 64> BBWorkList;
120fe6060f1SDimitry Andric 
121fe6060f1SDimitry Andric   /// KnownFeasibleEdges - Entries in this set are edges which have already had
122fe6060f1SDimitry Andric   /// PHI nodes retriggered.
123fe6060f1SDimitry Andric   using Edge = std::pair<BasicBlock *, BasicBlock *>;
124fe6060f1SDimitry Andric   DenseSet<Edge> KnownFeasibleEdges;
125fe6060f1SDimitry Andric 
126fe6060f1SDimitry Andric   DenseMap<Function *, AnalysisResultsForFn> AnalysisResults;
127fe6060f1SDimitry Andric   DenseMap<Value *, SmallPtrSet<User *, 2>> AdditionalUsers;
128fe6060f1SDimitry Andric 
129fe6060f1SDimitry Andric   LLVMContext &Ctx;
130fe6060f1SDimitry Andric 
131fe6060f1SDimitry Andric private:
132fe6060f1SDimitry Andric   ConstantInt *getConstantInt(const ValueLatticeElement &IV) const {
133fe6060f1SDimitry Andric     return dyn_cast_or_null<ConstantInt>(getConstant(IV));
134fe6060f1SDimitry Andric   }
135fe6060f1SDimitry Andric 
136fe6060f1SDimitry Andric   // pushToWorkList - Helper for markConstant/markOverdefined
137fe6060f1SDimitry Andric   void pushToWorkList(ValueLatticeElement &IV, Value *V);
138fe6060f1SDimitry Andric 
139fe6060f1SDimitry Andric   // Helper to push \p V to the worklist, after updating it to \p IV. Also
140fe6060f1SDimitry Andric   // prints a debug message with the updated value.
141fe6060f1SDimitry Andric   void pushToWorkListMsg(ValueLatticeElement &IV, Value *V);
142fe6060f1SDimitry Andric 
143fe6060f1SDimitry Andric   // markConstant - Make a value be marked as "constant".  If the value
144fe6060f1SDimitry Andric   // is not already a constant, add it to the instruction work list so that
145fe6060f1SDimitry Andric   // the users of the instruction are updated later.
146fe6060f1SDimitry Andric   bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C,
147fe6060f1SDimitry Andric                     bool MayIncludeUndef = false);
148fe6060f1SDimitry Andric 
149fe6060f1SDimitry Andric   bool markConstant(Value *V, Constant *C) {
150fe6060f1SDimitry Andric     assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
151fe6060f1SDimitry Andric     return markConstant(ValueState[V], V, C);
152fe6060f1SDimitry Andric   }
153fe6060f1SDimitry Andric 
154fe6060f1SDimitry Andric   // markOverdefined - Make a value be marked as "overdefined". If the
155fe6060f1SDimitry Andric   // value is not already overdefined, add it to the overdefined instruction
156fe6060f1SDimitry Andric   // work list so that the users of the instruction are updated later.
157fe6060f1SDimitry Andric   bool markOverdefined(ValueLatticeElement &IV, Value *V);
158fe6060f1SDimitry Andric 
159fe6060f1SDimitry Andric   /// Merge \p MergeWithV into \p IV and push \p V to the worklist, if \p IV
160fe6060f1SDimitry Andric   /// changes.
161fe6060f1SDimitry Andric   bool mergeInValue(ValueLatticeElement &IV, Value *V,
162fe6060f1SDimitry Andric                     ValueLatticeElement MergeWithV,
163fe6060f1SDimitry Andric                     ValueLatticeElement::MergeOptions Opts = {
164fe6060f1SDimitry Andric                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false});
165fe6060f1SDimitry Andric 
166fe6060f1SDimitry Andric   bool mergeInValue(Value *V, ValueLatticeElement MergeWithV,
167fe6060f1SDimitry Andric                     ValueLatticeElement::MergeOptions Opts = {
168fe6060f1SDimitry Andric                         /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
169fe6060f1SDimitry Andric     assert(!V->getType()->isStructTy() &&
170fe6060f1SDimitry Andric            "non-structs should use markConstant");
171fe6060f1SDimitry Andric     return mergeInValue(ValueState[V], V, MergeWithV, Opts);
172fe6060f1SDimitry Andric   }
173fe6060f1SDimitry Andric 
174fe6060f1SDimitry Andric   /// getValueState - Return the ValueLatticeElement object that corresponds to
175fe6060f1SDimitry Andric   /// the value.  This function handles the case when the value hasn't been seen
176fe6060f1SDimitry Andric   /// yet by properly seeding constants etc.
177fe6060f1SDimitry Andric   ValueLatticeElement &getValueState(Value *V) {
178fe6060f1SDimitry Andric     assert(!V->getType()->isStructTy() && "Should use getStructValueState");
179fe6060f1SDimitry Andric 
180fe6060f1SDimitry Andric     auto I = ValueState.insert(std::make_pair(V, ValueLatticeElement()));
181fe6060f1SDimitry Andric     ValueLatticeElement &LV = I.first->second;
182fe6060f1SDimitry Andric 
183fe6060f1SDimitry Andric     if (!I.second)
184fe6060f1SDimitry Andric       return LV; // Common case, already in the map.
185fe6060f1SDimitry Andric 
186fe6060f1SDimitry Andric     if (auto *C = dyn_cast<Constant>(V))
187fe6060f1SDimitry Andric       LV.markConstant(C); // Constants are constant
188fe6060f1SDimitry Andric 
189fe6060f1SDimitry Andric     // All others are unknown by default.
190fe6060f1SDimitry Andric     return LV;
191fe6060f1SDimitry Andric   }
192fe6060f1SDimitry Andric 
193fe6060f1SDimitry Andric   /// getStructValueState - Return the ValueLatticeElement object that
194fe6060f1SDimitry Andric   /// corresponds to the value/field pair.  This function handles the case when
195fe6060f1SDimitry Andric   /// the value hasn't been seen yet by properly seeding constants etc.
196fe6060f1SDimitry Andric   ValueLatticeElement &getStructValueState(Value *V, unsigned i) {
197fe6060f1SDimitry Andric     assert(V->getType()->isStructTy() && "Should use getValueState");
198fe6060f1SDimitry Andric     assert(i < cast<StructType>(V->getType())->getNumElements() &&
199fe6060f1SDimitry Andric            "Invalid element #");
200fe6060f1SDimitry Andric 
201fe6060f1SDimitry Andric     auto I = StructValueState.insert(
202fe6060f1SDimitry Andric         std::make_pair(std::make_pair(V, i), ValueLatticeElement()));
203fe6060f1SDimitry Andric     ValueLatticeElement &LV = I.first->second;
204fe6060f1SDimitry Andric 
205fe6060f1SDimitry Andric     if (!I.second)
206fe6060f1SDimitry Andric       return LV; // Common case, already in the map.
207fe6060f1SDimitry Andric 
208fe6060f1SDimitry Andric     if (auto *C = dyn_cast<Constant>(V)) {
209fe6060f1SDimitry Andric       Constant *Elt = C->getAggregateElement(i);
210fe6060f1SDimitry Andric 
211fe6060f1SDimitry Andric       if (!Elt)
212fe6060f1SDimitry Andric         LV.markOverdefined(); // Unknown sort of constant.
213fe6060f1SDimitry Andric       else if (isa<UndefValue>(Elt))
214fe6060f1SDimitry Andric         ; // Undef values remain unknown.
215fe6060f1SDimitry Andric       else
216fe6060f1SDimitry Andric         LV.markConstant(Elt); // Constants are constant.
217fe6060f1SDimitry Andric     }
218fe6060f1SDimitry Andric 
219fe6060f1SDimitry Andric     // All others are underdefined by default.
220fe6060f1SDimitry Andric     return LV;
221fe6060f1SDimitry Andric   }
222fe6060f1SDimitry Andric 
223fe6060f1SDimitry Andric   /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
224fe6060f1SDimitry Andric   /// work list if it is not already executable.
225fe6060f1SDimitry Andric   bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
226fe6060f1SDimitry Andric 
227fe6060f1SDimitry Andric   // getFeasibleSuccessors - Return a vector of booleans to indicate which
228fe6060f1SDimitry Andric   // successors are reachable from a given terminator instruction.
229fe6060f1SDimitry Andric   void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs);
230fe6060f1SDimitry Andric 
231fe6060f1SDimitry Andric   // OperandChangedState - This method is invoked on all of the users of an
232fe6060f1SDimitry Andric   // instruction that was just changed state somehow.  Based on this
233fe6060f1SDimitry Andric   // information, we need to update the specified user of this instruction.
234fe6060f1SDimitry Andric   void operandChangedState(Instruction *I) {
235fe6060f1SDimitry Andric     if (BBExecutable.count(I->getParent())) // Inst is executable?
236fe6060f1SDimitry Andric       visit(*I);
237fe6060f1SDimitry Andric   }
238fe6060f1SDimitry Andric 
239fe6060f1SDimitry Andric   // Add U as additional user of V.
240fe6060f1SDimitry Andric   void addAdditionalUser(Value *V, User *U) {
241fe6060f1SDimitry Andric     auto Iter = AdditionalUsers.insert({V, {}});
242fe6060f1SDimitry Andric     Iter.first->second.insert(U);
243fe6060f1SDimitry Andric   }
244fe6060f1SDimitry Andric 
245fe6060f1SDimitry Andric   // Mark I's users as changed, including AdditionalUsers.
246fe6060f1SDimitry Andric   void markUsersAsChanged(Value *I) {
247fe6060f1SDimitry Andric     // Functions include their arguments in the use-list. Changed function
248fe6060f1SDimitry Andric     // values mean that the result of the function changed. We only need to
249fe6060f1SDimitry Andric     // update the call sites with the new function result and do not have to
250fe6060f1SDimitry Andric     // propagate the call arguments.
251fe6060f1SDimitry Andric     if (isa<Function>(I)) {
252fe6060f1SDimitry Andric       for (User *U : I->users()) {
253fe6060f1SDimitry Andric         if (auto *CB = dyn_cast<CallBase>(U))
254fe6060f1SDimitry Andric           handleCallResult(*CB);
255fe6060f1SDimitry Andric       }
256fe6060f1SDimitry Andric     } else {
257fe6060f1SDimitry Andric       for (User *U : I->users())
258fe6060f1SDimitry Andric         if (auto *UI = dyn_cast<Instruction>(U))
259fe6060f1SDimitry Andric           operandChangedState(UI);
260fe6060f1SDimitry Andric     }
261fe6060f1SDimitry Andric 
262fe6060f1SDimitry Andric     auto Iter = AdditionalUsers.find(I);
263fe6060f1SDimitry Andric     if (Iter != AdditionalUsers.end()) {
264fe6060f1SDimitry Andric       // Copy additional users before notifying them of changes, because new
265fe6060f1SDimitry Andric       // users may be added, potentially invalidating the iterator.
266fe6060f1SDimitry Andric       SmallVector<Instruction *, 2> ToNotify;
267fe6060f1SDimitry Andric       for (User *U : Iter->second)
268fe6060f1SDimitry Andric         if (auto *UI = dyn_cast<Instruction>(U))
269fe6060f1SDimitry Andric           ToNotify.push_back(UI);
270fe6060f1SDimitry Andric       for (Instruction *UI : ToNotify)
271fe6060f1SDimitry Andric         operandChangedState(UI);
272fe6060f1SDimitry Andric     }
273fe6060f1SDimitry Andric   }
274fe6060f1SDimitry Andric   void handleCallOverdefined(CallBase &CB);
275fe6060f1SDimitry Andric   void handleCallResult(CallBase &CB);
276fe6060f1SDimitry Andric   void handleCallArguments(CallBase &CB);
277fe6060f1SDimitry Andric 
278fe6060f1SDimitry Andric private:
279fe6060f1SDimitry Andric   friend class InstVisitor<SCCPInstVisitor>;
280fe6060f1SDimitry Andric 
281fe6060f1SDimitry Andric   // visit implementations - Something changed in this instruction.  Either an
282fe6060f1SDimitry Andric   // operand made a transition, or the instruction is newly executable.  Change
283fe6060f1SDimitry Andric   // the value type of I to reflect these changes if appropriate.
284fe6060f1SDimitry Andric   void visitPHINode(PHINode &I);
285fe6060f1SDimitry Andric 
286fe6060f1SDimitry Andric   // Terminators
287fe6060f1SDimitry Andric 
288fe6060f1SDimitry Andric   void visitReturnInst(ReturnInst &I);
289fe6060f1SDimitry Andric   void visitTerminator(Instruction &TI);
290fe6060f1SDimitry Andric 
291fe6060f1SDimitry Andric   void visitCastInst(CastInst &I);
292fe6060f1SDimitry Andric   void visitSelectInst(SelectInst &I);
293fe6060f1SDimitry Andric   void visitUnaryOperator(Instruction &I);
294fe6060f1SDimitry Andric   void visitBinaryOperator(Instruction &I);
295fe6060f1SDimitry Andric   void visitCmpInst(CmpInst &I);
296fe6060f1SDimitry Andric   void visitExtractValueInst(ExtractValueInst &EVI);
297fe6060f1SDimitry Andric   void visitInsertValueInst(InsertValueInst &IVI);
298fe6060f1SDimitry Andric 
299fe6060f1SDimitry Andric   void visitCatchSwitchInst(CatchSwitchInst &CPI) {
300fe6060f1SDimitry Andric     markOverdefined(&CPI);
301fe6060f1SDimitry Andric     visitTerminator(CPI);
302fe6060f1SDimitry Andric   }
303fe6060f1SDimitry Andric 
304fe6060f1SDimitry Andric   // Instructions that cannot be folded away.
305fe6060f1SDimitry Andric 
306fe6060f1SDimitry Andric   void visitStoreInst(StoreInst &I);
307fe6060f1SDimitry Andric   void visitLoadInst(LoadInst &I);
308fe6060f1SDimitry Andric   void visitGetElementPtrInst(GetElementPtrInst &I);
309fe6060f1SDimitry Andric 
310fe6060f1SDimitry Andric   void visitInvokeInst(InvokeInst &II) {
311fe6060f1SDimitry Andric     visitCallBase(II);
312fe6060f1SDimitry Andric     visitTerminator(II);
313fe6060f1SDimitry Andric   }
314fe6060f1SDimitry Andric 
315fe6060f1SDimitry Andric   void visitCallBrInst(CallBrInst &CBI) {
316fe6060f1SDimitry Andric     visitCallBase(CBI);
317fe6060f1SDimitry Andric     visitTerminator(CBI);
318fe6060f1SDimitry Andric   }
319fe6060f1SDimitry Andric 
320fe6060f1SDimitry Andric   void visitCallBase(CallBase &CB);
321fe6060f1SDimitry Andric   void visitResumeInst(ResumeInst &I) { /*returns void*/
322fe6060f1SDimitry Andric   }
323fe6060f1SDimitry Andric   void visitUnreachableInst(UnreachableInst &I) { /*returns void*/
324fe6060f1SDimitry Andric   }
325fe6060f1SDimitry Andric   void visitFenceInst(FenceInst &I) { /*returns void*/
326fe6060f1SDimitry Andric   }
327fe6060f1SDimitry Andric 
328fe6060f1SDimitry Andric   void visitInstruction(Instruction &I);
329fe6060f1SDimitry Andric 
330fe6060f1SDimitry Andric public:
331fe6060f1SDimitry Andric   void addAnalysis(Function &F, AnalysisResultsForFn A) {
332fe6060f1SDimitry Andric     AnalysisResults.insert({&F, std::move(A)});
333fe6060f1SDimitry Andric   }
334fe6060f1SDimitry Andric 
335fe6060f1SDimitry Andric   void visitCallInst(CallInst &I) { visitCallBase(I); }
336fe6060f1SDimitry Andric 
337fe6060f1SDimitry Andric   bool markBlockExecutable(BasicBlock *BB);
338fe6060f1SDimitry Andric 
339fe6060f1SDimitry Andric   const PredicateBase *getPredicateInfoFor(Instruction *I) {
340fe6060f1SDimitry Andric     auto A = AnalysisResults.find(I->getParent()->getParent());
341fe6060f1SDimitry Andric     if (A == AnalysisResults.end())
342fe6060f1SDimitry Andric       return nullptr;
343fe6060f1SDimitry Andric     return A->second.PredInfo->getPredicateInfoFor(I);
344fe6060f1SDimitry Andric   }
345fe6060f1SDimitry Andric 
346fe6060f1SDimitry Andric   DomTreeUpdater getDTU(Function &F) {
347fe6060f1SDimitry Andric     auto A = AnalysisResults.find(&F);
348fe6060f1SDimitry Andric     assert(A != AnalysisResults.end() && "Need analysis results for function.");
349fe6060f1SDimitry Andric     return {A->second.DT, A->second.PDT, DomTreeUpdater::UpdateStrategy::Lazy};
350fe6060f1SDimitry Andric   }
351fe6060f1SDimitry Andric 
352fe6060f1SDimitry Andric   SCCPInstVisitor(const DataLayout &DL,
353fe6060f1SDimitry Andric                   std::function<const TargetLibraryInfo &(Function &)> GetTLI,
354fe6060f1SDimitry Andric                   LLVMContext &Ctx)
355fe6060f1SDimitry Andric       : DL(DL), GetTLI(GetTLI), Ctx(Ctx) {}
356fe6060f1SDimitry Andric 
357fe6060f1SDimitry Andric   void trackValueOfGlobalVariable(GlobalVariable *GV) {
358fe6060f1SDimitry Andric     // We only track the contents of scalar globals.
359fe6060f1SDimitry Andric     if (GV->getValueType()->isSingleValueType()) {
360fe6060f1SDimitry Andric       ValueLatticeElement &IV = TrackedGlobals[GV];
361fe6060f1SDimitry Andric       if (!isa<UndefValue>(GV->getInitializer()))
362fe6060f1SDimitry Andric         IV.markConstant(GV->getInitializer());
363fe6060f1SDimitry Andric     }
364fe6060f1SDimitry Andric   }
365fe6060f1SDimitry Andric 
366fe6060f1SDimitry Andric   void addTrackedFunction(Function *F) {
367fe6060f1SDimitry Andric     // Add an entry, F -> undef.
368fe6060f1SDimitry Andric     if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
369fe6060f1SDimitry Andric       MRVFunctionsTracked.insert(F);
370fe6060f1SDimitry Andric       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
371fe6060f1SDimitry Andric         TrackedMultipleRetVals.insert(
372fe6060f1SDimitry Andric             std::make_pair(std::make_pair(F, i), ValueLatticeElement()));
373fe6060f1SDimitry Andric     } else if (!F->getReturnType()->isVoidTy())
374fe6060f1SDimitry Andric       TrackedRetVals.insert(std::make_pair(F, ValueLatticeElement()));
375fe6060f1SDimitry Andric   }
376fe6060f1SDimitry Andric 
377fe6060f1SDimitry Andric   void addToMustPreserveReturnsInFunctions(Function *F) {
378fe6060f1SDimitry Andric     MustPreserveReturnsInFunctions.insert(F);
379fe6060f1SDimitry Andric   }
380fe6060f1SDimitry Andric 
381fe6060f1SDimitry Andric   bool mustPreserveReturn(Function *F) {
382fe6060f1SDimitry Andric     return MustPreserveReturnsInFunctions.count(F);
383fe6060f1SDimitry Andric   }
384fe6060f1SDimitry Andric 
385fe6060f1SDimitry Andric   void addArgumentTrackedFunction(Function *F) {
386fe6060f1SDimitry Andric     TrackingIncomingArguments.insert(F);
387fe6060f1SDimitry Andric   }
388fe6060f1SDimitry Andric 
389fe6060f1SDimitry Andric   bool isArgumentTrackedFunction(Function *F) {
390fe6060f1SDimitry Andric     return TrackingIncomingArguments.count(F);
391fe6060f1SDimitry Andric   }
392fe6060f1SDimitry Andric 
393fe6060f1SDimitry Andric   void solve();
394fe6060f1SDimitry Andric 
395fe6060f1SDimitry Andric   bool resolvedUndefsIn(Function &F);
396fe6060f1SDimitry Andric 
397fe6060f1SDimitry Andric   bool isBlockExecutable(BasicBlock *BB) const {
398fe6060f1SDimitry Andric     return BBExecutable.count(BB);
399fe6060f1SDimitry Andric   }
400fe6060f1SDimitry Andric 
401fe6060f1SDimitry Andric   bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
402fe6060f1SDimitry Andric 
403fe6060f1SDimitry Andric   std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const {
404fe6060f1SDimitry Andric     std::vector<ValueLatticeElement> StructValues;
405fe6060f1SDimitry Andric     auto *STy = dyn_cast<StructType>(V->getType());
406fe6060f1SDimitry Andric     assert(STy && "getStructLatticeValueFor() can be called only on structs");
407fe6060f1SDimitry Andric     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
408fe6060f1SDimitry Andric       auto I = StructValueState.find(std::make_pair(V, i));
409fe6060f1SDimitry Andric       assert(I != StructValueState.end() && "Value not in valuemap!");
410fe6060f1SDimitry Andric       StructValues.push_back(I->second);
411fe6060f1SDimitry Andric     }
412fe6060f1SDimitry Andric     return StructValues;
413fe6060f1SDimitry Andric   }
414fe6060f1SDimitry Andric 
415fe6060f1SDimitry Andric   void removeLatticeValueFor(Value *V) { ValueState.erase(V); }
416fe6060f1SDimitry Andric 
417fe6060f1SDimitry Andric   const ValueLatticeElement &getLatticeValueFor(Value *V) const {
418fe6060f1SDimitry Andric     assert(!V->getType()->isStructTy() &&
419fe6060f1SDimitry Andric            "Should use getStructLatticeValueFor");
420fe6060f1SDimitry Andric     DenseMap<Value *, ValueLatticeElement>::const_iterator I =
421fe6060f1SDimitry Andric         ValueState.find(V);
422fe6060f1SDimitry Andric     assert(I != ValueState.end() &&
423fe6060f1SDimitry Andric            "V not found in ValueState nor Paramstate map!");
424fe6060f1SDimitry Andric     return I->second;
425fe6060f1SDimitry Andric   }
426fe6060f1SDimitry Andric 
427fe6060f1SDimitry Andric   const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals() {
428fe6060f1SDimitry Andric     return TrackedRetVals;
429fe6060f1SDimitry Andric   }
430fe6060f1SDimitry Andric 
431fe6060f1SDimitry Andric   const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals() {
432fe6060f1SDimitry Andric     return TrackedGlobals;
433fe6060f1SDimitry Andric   }
434fe6060f1SDimitry Andric 
435fe6060f1SDimitry Andric   const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() {
436fe6060f1SDimitry Andric     return MRVFunctionsTracked;
437fe6060f1SDimitry Andric   }
438fe6060f1SDimitry Andric 
439fe6060f1SDimitry Andric   void markOverdefined(Value *V) {
440fe6060f1SDimitry Andric     if (auto *STy = dyn_cast<StructType>(V->getType()))
441fe6060f1SDimitry Andric       for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
442fe6060f1SDimitry Andric         markOverdefined(getStructValueState(V, i), V);
443fe6060f1SDimitry Andric     else
444fe6060f1SDimitry Andric       markOverdefined(ValueState[V], V);
445fe6060f1SDimitry Andric   }
446fe6060f1SDimitry Andric 
447fe6060f1SDimitry Andric   bool isStructLatticeConstant(Function *F, StructType *STy);
448fe6060f1SDimitry Andric 
449fe6060f1SDimitry Andric   Constant *getConstant(const ValueLatticeElement &LV) const;
450fe6060f1SDimitry Andric 
451fe6060f1SDimitry Andric   SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions() {
452fe6060f1SDimitry Andric     return TrackingIncomingArguments;
453fe6060f1SDimitry Andric   }
454fe6060f1SDimitry Andric 
455fe6060f1SDimitry Andric   void markArgInFuncSpecialization(Function *F, Argument *A, Constant *C);
456fe6060f1SDimitry Andric 
457fe6060f1SDimitry Andric   void markFunctionUnreachable(Function *F) {
458fe6060f1SDimitry Andric     for (auto &BB : *F)
459fe6060f1SDimitry Andric       BBExecutable.erase(&BB);
460fe6060f1SDimitry Andric   }
461fe6060f1SDimitry Andric };
462fe6060f1SDimitry Andric 
463fe6060f1SDimitry Andric } // namespace llvm
464fe6060f1SDimitry Andric 
465fe6060f1SDimitry Andric bool SCCPInstVisitor::markBlockExecutable(BasicBlock *BB) {
466fe6060f1SDimitry Andric   if (!BBExecutable.insert(BB).second)
467fe6060f1SDimitry Andric     return false;
468fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
469fe6060f1SDimitry Andric   BBWorkList.push_back(BB); // Add the block to the work list!
470fe6060f1SDimitry Andric   return true;
471fe6060f1SDimitry Andric }
472fe6060f1SDimitry Andric 
473fe6060f1SDimitry Andric void SCCPInstVisitor::pushToWorkList(ValueLatticeElement &IV, Value *V) {
474fe6060f1SDimitry Andric   if (IV.isOverdefined())
475fe6060f1SDimitry Andric     return OverdefinedInstWorkList.push_back(V);
476fe6060f1SDimitry Andric   InstWorkList.push_back(V);
477fe6060f1SDimitry Andric }
478fe6060f1SDimitry Andric 
479fe6060f1SDimitry Andric void SCCPInstVisitor::pushToWorkListMsg(ValueLatticeElement &IV, Value *V) {
480fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n');
481fe6060f1SDimitry Andric   pushToWorkList(IV, V);
482fe6060f1SDimitry Andric }
483fe6060f1SDimitry Andric 
484fe6060f1SDimitry Andric bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V,
485fe6060f1SDimitry Andric                                    Constant *C, bool MayIncludeUndef) {
486fe6060f1SDimitry Andric   if (!IV.markConstant(C, MayIncludeUndef))
487fe6060f1SDimitry Andric     return false;
488fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
489fe6060f1SDimitry Andric   pushToWorkList(IV, V);
490fe6060f1SDimitry Andric   return true;
491fe6060f1SDimitry Andric }
492fe6060f1SDimitry Andric 
493fe6060f1SDimitry Andric bool SCCPInstVisitor::markOverdefined(ValueLatticeElement &IV, Value *V) {
494fe6060f1SDimitry Andric   if (!IV.markOverdefined())
495fe6060f1SDimitry Andric     return false;
496fe6060f1SDimitry Andric 
497fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "markOverdefined: ";
498fe6060f1SDimitry Andric              if (auto *F = dyn_cast<Function>(V)) dbgs()
499fe6060f1SDimitry Andric              << "Function '" << F->getName() << "'\n";
500fe6060f1SDimitry Andric              else dbgs() << *V << '\n');
501fe6060f1SDimitry Andric   // Only instructions go on the work list
502fe6060f1SDimitry Andric   pushToWorkList(IV, V);
503fe6060f1SDimitry Andric   return true;
504fe6060f1SDimitry Andric }
505fe6060f1SDimitry Andric 
506fe6060f1SDimitry Andric bool SCCPInstVisitor::isStructLatticeConstant(Function *F, StructType *STy) {
507fe6060f1SDimitry Andric   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
508fe6060f1SDimitry Andric     const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
509fe6060f1SDimitry Andric     assert(It != TrackedMultipleRetVals.end());
510fe6060f1SDimitry Andric     ValueLatticeElement LV = It->second;
511fe6060f1SDimitry Andric     if (!isConstant(LV))
512fe6060f1SDimitry Andric       return false;
513fe6060f1SDimitry Andric   }
514fe6060f1SDimitry Andric   return true;
515fe6060f1SDimitry Andric }
516fe6060f1SDimitry Andric 
517fe6060f1SDimitry Andric Constant *SCCPInstVisitor::getConstant(const ValueLatticeElement &LV) const {
518fe6060f1SDimitry Andric   if (LV.isConstant())
519fe6060f1SDimitry Andric     return LV.getConstant();
520fe6060f1SDimitry Andric 
521fe6060f1SDimitry Andric   if (LV.isConstantRange()) {
522fe6060f1SDimitry Andric     const auto &CR = LV.getConstantRange();
523fe6060f1SDimitry Andric     if (CR.getSingleElement())
524fe6060f1SDimitry Andric       return ConstantInt::get(Ctx, *CR.getSingleElement());
525fe6060f1SDimitry Andric   }
526fe6060f1SDimitry Andric   return nullptr;
527fe6060f1SDimitry Andric }
528fe6060f1SDimitry Andric 
529fe6060f1SDimitry Andric void SCCPInstVisitor::markArgInFuncSpecialization(Function *F, Argument *A,
530fe6060f1SDimitry Andric                                                   Constant *C) {
531fe6060f1SDimitry Andric   assert(F->arg_size() == A->getParent()->arg_size() &&
532fe6060f1SDimitry Andric          "Functions should have the same number of arguments");
533fe6060f1SDimitry Andric 
534fe6060f1SDimitry Andric   // Mark the argument constant in the new function.
535fe6060f1SDimitry Andric   markConstant(A, C);
536fe6060f1SDimitry Andric 
537fe6060f1SDimitry Andric   // For the remaining arguments in the new function, copy the lattice state
538fe6060f1SDimitry Andric   // over from the old function.
539fe6060f1SDimitry Andric   for (auto I = F->arg_begin(), J = A->getParent()->arg_begin(),
540fe6060f1SDimitry Andric             E = F->arg_end();
541fe6060f1SDimitry Andric        I != E; ++I, ++J)
542fe6060f1SDimitry Andric     if (J != A && ValueState.count(I)) {
543*349cc55cSDimitry Andric       // Note: This previously looked like this:
544*349cc55cSDimitry Andric       // ValueState[J] = ValueState[I];
545*349cc55cSDimitry Andric       // This is incorrect because the DenseMap class may resize the underlying
546*349cc55cSDimitry Andric       // memory when inserting `J`, which will invalidate the reference to `I`.
547*349cc55cSDimitry Andric       // Instead, we make sure `J` exists, then set it to `I` afterwards.
548*349cc55cSDimitry Andric       auto &NewValue = ValueState[J];
549*349cc55cSDimitry Andric       NewValue = ValueState[I];
550*349cc55cSDimitry Andric       pushToWorkList(NewValue, J);
551fe6060f1SDimitry Andric     }
552fe6060f1SDimitry Andric }
553fe6060f1SDimitry Andric 
554fe6060f1SDimitry Andric void SCCPInstVisitor::visitInstruction(Instruction &I) {
555fe6060f1SDimitry Andric   // All the instructions we don't do any special handling for just
556fe6060f1SDimitry Andric   // go to overdefined.
557fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
558fe6060f1SDimitry Andric   markOverdefined(&I);
559fe6060f1SDimitry Andric }
560fe6060f1SDimitry Andric 
561fe6060f1SDimitry Andric bool SCCPInstVisitor::mergeInValue(ValueLatticeElement &IV, Value *V,
562fe6060f1SDimitry Andric                                    ValueLatticeElement MergeWithV,
563fe6060f1SDimitry Andric                                    ValueLatticeElement::MergeOptions Opts) {
564fe6060f1SDimitry Andric   if (IV.mergeIn(MergeWithV, Opts)) {
565fe6060f1SDimitry Andric     pushToWorkList(IV, V);
566fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : "
567fe6060f1SDimitry Andric                       << IV << "\n");
568fe6060f1SDimitry Andric     return true;
569fe6060f1SDimitry Andric   }
570fe6060f1SDimitry Andric   return false;
571fe6060f1SDimitry Andric }
572fe6060f1SDimitry Andric 
573fe6060f1SDimitry Andric bool SCCPInstVisitor::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
574fe6060f1SDimitry Andric   if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
575fe6060f1SDimitry Andric     return false; // This edge is already known to be executable!
576fe6060f1SDimitry Andric 
577fe6060f1SDimitry Andric   if (!markBlockExecutable(Dest)) {
578fe6060f1SDimitry Andric     // If the destination is already executable, we just made an *edge*
579fe6060f1SDimitry Andric     // feasible that wasn't before.  Revisit the PHI nodes in the block
580fe6060f1SDimitry Andric     // because they have potentially new operands.
581fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
582fe6060f1SDimitry Andric                       << " -> " << Dest->getName() << '\n');
583fe6060f1SDimitry Andric 
584fe6060f1SDimitry Andric     for (PHINode &PN : Dest->phis())
585fe6060f1SDimitry Andric       visitPHINode(PN);
586fe6060f1SDimitry Andric   }
587fe6060f1SDimitry Andric   return true;
588fe6060f1SDimitry Andric }
589fe6060f1SDimitry Andric 
590fe6060f1SDimitry Andric // getFeasibleSuccessors - Return a vector of booleans to indicate which
591fe6060f1SDimitry Andric // successors are reachable from a given terminator instruction.
592fe6060f1SDimitry Andric void SCCPInstVisitor::getFeasibleSuccessors(Instruction &TI,
593fe6060f1SDimitry Andric                                             SmallVectorImpl<bool> &Succs) {
594fe6060f1SDimitry Andric   Succs.resize(TI.getNumSuccessors());
595fe6060f1SDimitry Andric   if (auto *BI = dyn_cast<BranchInst>(&TI)) {
596fe6060f1SDimitry Andric     if (BI->isUnconditional()) {
597fe6060f1SDimitry Andric       Succs[0] = true;
598fe6060f1SDimitry Andric       return;
599fe6060f1SDimitry Andric     }
600fe6060f1SDimitry Andric 
601fe6060f1SDimitry Andric     ValueLatticeElement BCValue = getValueState(BI->getCondition());
602fe6060f1SDimitry Andric     ConstantInt *CI = getConstantInt(BCValue);
603fe6060f1SDimitry Andric     if (!CI) {
604fe6060f1SDimitry Andric       // Overdefined condition variables, and branches on unfoldable constant
605fe6060f1SDimitry Andric       // conditions, mean the branch could go either way.
606fe6060f1SDimitry Andric       if (!BCValue.isUnknownOrUndef())
607fe6060f1SDimitry Andric         Succs[0] = Succs[1] = true;
608fe6060f1SDimitry Andric       return;
609fe6060f1SDimitry Andric     }
610fe6060f1SDimitry Andric 
611fe6060f1SDimitry Andric     // Constant condition variables mean the branch can only go a single way.
612fe6060f1SDimitry Andric     Succs[CI->isZero()] = true;
613fe6060f1SDimitry Andric     return;
614fe6060f1SDimitry Andric   }
615fe6060f1SDimitry Andric 
616fe6060f1SDimitry Andric   // Unwinding instructions successors are always executable.
617fe6060f1SDimitry Andric   if (TI.isExceptionalTerminator()) {
618fe6060f1SDimitry Andric     Succs.assign(TI.getNumSuccessors(), true);
619fe6060f1SDimitry Andric     return;
620fe6060f1SDimitry Andric   }
621fe6060f1SDimitry Andric 
622fe6060f1SDimitry Andric   if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
623fe6060f1SDimitry Andric     if (!SI->getNumCases()) {
624fe6060f1SDimitry Andric       Succs[0] = true;
625fe6060f1SDimitry Andric       return;
626fe6060f1SDimitry Andric     }
627fe6060f1SDimitry Andric     const ValueLatticeElement &SCValue = getValueState(SI->getCondition());
628fe6060f1SDimitry Andric     if (ConstantInt *CI = getConstantInt(SCValue)) {
629fe6060f1SDimitry Andric       Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true;
630fe6060f1SDimitry Andric       return;
631fe6060f1SDimitry Andric     }
632fe6060f1SDimitry Andric 
633fe6060f1SDimitry Andric     // TODO: Switch on undef is UB. Stop passing false once the rest of LLVM
634fe6060f1SDimitry Andric     // is ready.
635fe6060f1SDimitry Andric     if (SCValue.isConstantRange(/*UndefAllowed=*/false)) {
636fe6060f1SDimitry Andric       const ConstantRange &Range = SCValue.getConstantRange();
637fe6060f1SDimitry Andric       for (const auto &Case : SI->cases()) {
638fe6060f1SDimitry Andric         const APInt &CaseValue = Case.getCaseValue()->getValue();
639fe6060f1SDimitry Andric         if (Range.contains(CaseValue))
640fe6060f1SDimitry Andric           Succs[Case.getSuccessorIndex()] = true;
641fe6060f1SDimitry Andric       }
642fe6060f1SDimitry Andric 
643fe6060f1SDimitry Andric       // TODO: Determine whether default case is reachable.
644fe6060f1SDimitry Andric       Succs[SI->case_default()->getSuccessorIndex()] = true;
645fe6060f1SDimitry Andric       return;
646fe6060f1SDimitry Andric     }
647fe6060f1SDimitry Andric 
648fe6060f1SDimitry Andric     // Overdefined or unknown condition? All destinations are executable!
649fe6060f1SDimitry Andric     if (!SCValue.isUnknownOrUndef())
650fe6060f1SDimitry Andric       Succs.assign(TI.getNumSuccessors(), true);
651fe6060f1SDimitry Andric     return;
652fe6060f1SDimitry Andric   }
653fe6060f1SDimitry Andric 
654fe6060f1SDimitry Andric   // In case of indirect branch and its address is a blockaddress, we mark
655fe6060f1SDimitry Andric   // the target as executable.
656fe6060f1SDimitry Andric   if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
657fe6060f1SDimitry Andric     // Casts are folded by visitCastInst.
658fe6060f1SDimitry Andric     ValueLatticeElement IBRValue = getValueState(IBR->getAddress());
659fe6060f1SDimitry Andric     BlockAddress *Addr = dyn_cast_or_null<BlockAddress>(getConstant(IBRValue));
660fe6060f1SDimitry Andric     if (!Addr) { // Overdefined or unknown condition?
661fe6060f1SDimitry Andric       // All destinations are executable!
662fe6060f1SDimitry Andric       if (!IBRValue.isUnknownOrUndef())
663fe6060f1SDimitry Andric         Succs.assign(TI.getNumSuccessors(), true);
664fe6060f1SDimitry Andric       return;
665fe6060f1SDimitry Andric     }
666fe6060f1SDimitry Andric 
667fe6060f1SDimitry Andric     BasicBlock *T = Addr->getBasicBlock();
668fe6060f1SDimitry Andric     assert(Addr->getFunction() == T->getParent() &&
669fe6060f1SDimitry Andric            "Block address of a different function ?");
670fe6060f1SDimitry Andric     for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
671fe6060f1SDimitry Andric       // This is the target.
672fe6060f1SDimitry Andric       if (IBR->getDestination(i) == T) {
673fe6060f1SDimitry Andric         Succs[i] = true;
674fe6060f1SDimitry Andric         return;
675fe6060f1SDimitry Andric       }
676fe6060f1SDimitry Andric     }
677fe6060f1SDimitry Andric 
678fe6060f1SDimitry Andric     // If we didn't find our destination in the IBR successor list, then we
679fe6060f1SDimitry Andric     // have undefined behavior. Its ok to assume no successor is executable.
680fe6060f1SDimitry Andric     return;
681fe6060f1SDimitry Andric   }
682fe6060f1SDimitry Andric 
683fe6060f1SDimitry Andric   // In case of callbr, we pessimistically assume that all successors are
684fe6060f1SDimitry Andric   // feasible.
685fe6060f1SDimitry Andric   if (isa<CallBrInst>(&TI)) {
686fe6060f1SDimitry Andric     Succs.assign(TI.getNumSuccessors(), true);
687fe6060f1SDimitry Andric     return;
688fe6060f1SDimitry Andric   }
689fe6060f1SDimitry Andric 
690fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
691fe6060f1SDimitry Andric   llvm_unreachable("SCCP: Don't know how to handle this terminator!");
692fe6060f1SDimitry Andric }
693fe6060f1SDimitry Andric 
694fe6060f1SDimitry Andric // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
695fe6060f1SDimitry Andric // block to the 'To' basic block is currently feasible.
696fe6060f1SDimitry Andric bool SCCPInstVisitor::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
697fe6060f1SDimitry Andric   // Check if we've called markEdgeExecutable on the edge yet. (We could
698fe6060f1SDimitry Andric   // be more aggressive and try to consider edges which haven't been marked
699fe6060f1SDimitry Andric   // yet, but there isn't any need.)
700fe6060f1SDimitry Andric   return KnownFeasibleEdges.count(Edge(From, To));
701fe6060f1SDimitry Andric }
702fe6060f1SDimitry Andric 
703fe6060f1SDimitry Andric // visit Implementations - Something changed in this instruction, either an
704fe6060f1SDimitry Andric // operand made a transition, or the instruction is newly executable.  Change
705fe6060f1SDimitry Andric // the value type of I to reflect these changes if appropriate.  This method
706fe6060f1SDimitry Andric // makes sure to do the following actions:
707fe6060f1SDimitry Andric //
708fe6060f1SDimitry Andric // 1. If a phi node merges two constants in, and has conflicting value coming
709fe6060f1SDimitry Andric //    from different branches, or if the PHI node merges in an overdefined
710fe6060f1SDimitry Andric //    value, then the PHI node becomes overdefined.
711fe6060f1SDimitry Andric // 2. If a phi node merges only constants in, and they all agree on value, the
712fe6060f1SDimitry Andric //    PHI node becomes a constant value equal to that.
713fe6060f1SDimitry Andric // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
714fe6060f1SDimitry Andric // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
715fe6060f1SDimitry Andric // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
716fe6060f1SDimitry Andric // 6. If a conditional branch has a value that is constant, make the selected
717fe6060f1SDimitry Andric //    destination executable
718fe6060f1SDimitry Andric // 7. If a conditional branch has a value that is overdefined, make all
719fe6060f1SDimitry Andric //    successors executable.
720fe6060f1SDimitry Andric void SCCPInstVisitor::visitPHINode(PHINode &PN) {
721fe6060f1SDimitry Andric   // If this PN returns a struct, just mark the result overdefined.
722fe6060f1SDimitry Andric   // TODO: We could do a lot better than this if code actually uses this.
723fe6060f1SDimitry Andric   if (PN.getType()->isStructTy())
724fe6060f1SDimitry Andric     return (void)markOverdefined(&PN);
725fe6060f1SDimitry Andric 
726fe6060f1SDimitry Andric   if (getValueState(&PN).isOverdefined())
727fe6060f1SDimitry Andric     return; // Quick exit
728fe6060f1SDimitry Andric 
729fe6060f1SDimitry Andric   // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
730fe6060f1SDimitry Andric   // and slow us down a lot.  Just mark them overdefined.
731fe6060f1SDimitry Andric   if (PN.getNumIncomingValues() > 64)
732fe6060f1SDimitry Andric     return (void)markOverdefined(&PN);
733fe6060f1SDimitry Andric 
734fe6060f1SDimitry Andric   unsigned NumActiveIncoming = 0;
735fe6060f1SDimitry Andric 
736fe6060f1SDimitry Andric   // Look at all of the executable operands of the PHI node.  If any of them
737fe6060f1SDimitry Andric   // are overdefined, the PHI becomes overdefined as well.  If they are all
738fe6060f1SDimitry Andric   // constant, and they agree with each other, the PHI becomes the identical
739fe6060f1SDimitry Andric   // constant.  If they are constant and don't agree, the PHI is a constant
740fe6060f1SDimitry Andric   // range. If there are no executable operands, the PHI remains unknown.
741fe6060f1SDimitry Andric   ValueLatticeElement PhiState = getValueState(&PN);
742fe6060f1SDimitry Andric   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
743fe6060f1SDimitry Andric     if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
744fe6060f1SDimitry Andric       continue;
745fe6060f1SDimitry Andric 
746fe6060f1SDimitry Andric     ValueLatticeElement IV = getValueState(PN.getIncomingValue(i));
747fe6060f1SDimitry Andric     PhiState.mergeIn(IV);
748fe6060f1SDimitry Andric     NumActiveIncoming++;
749fe6060f1SDimitry Andric     if (PhiState.isOverdefined())
750fe6060f1SDimitry Andric       break;
751fe6060f1SDimitry Andric   }
752fe6060f1SDimitry Andric 
753fe6060f1SDimitry Andric   // We allow up to 1 range extension per active incoming value and one
754fe6060f1SDimitry Andric   // additional extension. Note that we manually adjust the number of range
755fe6060f1SDimitry Andric   // extensions to match the number of active incoming values. This helps to
756fe6060f1SDimitry Andric   // limit multiple extensions caused by the same incoming value, if other
757fe6060f1SDimitry Andric   // incoming values are equal.
758fe6060f1SDimitry Andric   mergeInValue(&PN, PhiState,
759fe6060f1SDimitry Andric                ValueLatticeElement::MergeOptions().setMaxWidenSteps(
760fe6060f1SDimitry Andric                    NumActiveIncoming + 1));
761fe6060f1SDimitry Andric   ValueLatticeElement &PhiStateRef = getValueState(&PN);
762fe6060f1SDimitry Andric   PhiStateRef.setNumRangeExtensions(
763fe6060f1SDimitry Andric       std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions()));
764fe6060f1SDimitry Andric }
765fe6060f1SDimitry Andric 
766fe6060f1SDimitry Andric void SCCPInstVisitor::visitReturnInst(ReturnInst &I) {
767fe6060f1SDimitry Andric   if (I.getNumOperands() == 0)
768fe6060f1SDimitry Andric     return; // ret void
769fe6060f1SDimitry Andric 
770fe6060f1SDimitry Andric   Function *F = I.getParent()->getParent();
771fe6060f1SDimitry Andric   Value *ResultOp = I.getOperand(0);
772fe6060f1SDimitry Andric 
773fe6060f1SDimitry Andric   // If we are tracking the return value of this function, merge it in.
774fe6060f1SDimitry Andric   if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
775fe6060f1SDimitry Andric     auto TFRVI = TrackedRetVals.find(F);
776fe6060f1SDimitry Andric     if (TFRVI != TrackedRetVals.end()) {
777fe6060f1SDimitry Andric       mergeInValue(TFRVI->second, F, getValueState(ResultOp));
778fe6060f1SDimitry Andric       return;
779fe6060f1SDimitry Andric     }
780fe6060f1SDimitry Andric   }
781fe6060f1SDimitry Andric 
782fe6060f1SDimitry Andric   // Handle functions that return multiple values.
783fe6060f1SDimitry Andric   if (!TrackedMultipleRetVals.empty()) {
784fe6060f1SDimitry Andric     if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
785fe6060f1SDimitry Andric       if (MRVFunctionsTracked.count(F))
786fe6060f1SDimitry Andric         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
787fe6060f1SDimitry Andric           mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
788fe6060f1SDimitry Andric                        getStructValueState(ResultOp, i));
789fe6060f1SDimitry Andric   }
790fe6060f1SDimitry Andric }
791fe6060f1SDimitry Andric 
792fe6060f1SDimitry Andric void SCCPInstVisitor::visitTerminator(Instruction &TI) {
793fe6060f1SDimitry Andric   SmallVector<bool, 16> SuccFeasible;
794fe6060f1SDimitry Andric   getFeasibleSuccessors(TI, SuccFeasible);
795fe6060f1SDimitry Andric 
796fe6060f1SDimitry Andric   BasicBlock *BB = TI.getParent();
797fe6060f1SDimitry Andric 
798fe6060f1SDimitry Andric   // Mark all feasible successors executable.
799fe6060f1SDimitry Andric   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
800fe6060f1SDimitry Andric     if (SuccFeasible[i])
801fe6060f1SDimitry Andric       markEdgeExecutable(BB, TI.getSuccessor(i));
802fe6060f1SDimitry Andric }
803fe6060f1SDimitry Andric 
804fe6060f1SDimitry Andric void SCCPInstVisitor::visitCastInst(CastInst &I) {
805fe6060f1SDimitry Andric   // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
806fe6060f1SDimitry Andric   // discover a concrete value later.
807fe6060f1SDimitry Andric   if (ValueState[&I].isOverdefined())
808fe6060f1SDimitry Andric     return;
809fe6060f1SDimitry Andric 
810fe6060f1SDimitry Andric   ValueLatticeElement OpSt = getValueState(I.getOperand(0));
811*349cc55cSDimitry Andric   if (OpSt.isUnknownOrUndef())
812*349cc55cSDimitry Andric     return;
813*349cc55cSDimitry Andric 
814fe6060f1SDimitry Andric   if (Constant *OpC = getConstant(OpSt)) {
815fe6060f1SDimitry Andric     // Fold the constant as we build.
816fe6060f1SDimitry Andric     Constant *C = ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL);
817fe6060f1SDimitry Andric     if (isa<UndefValue>(C))
818fe6060f1SDimitry Andric       return;
819fe6060f1SDimitry Andric     // Propagate constant value
820fe6060f1SDimitry Andric     markConstant(&I, C);
821*349cc55cSDimitry Andric   } else if (I.getDestTy()->isIntegerTy()) {
822fe6060f1SDimitry Andric     auto &LV = getValueState(&I);
823*349cc55cSDimitry Andric     ConstantRange OpRange =
824*349cc55cSDimitry Andric         OpSt.isConstantRange()
825*349cc55cSDimitry Andric             ? OpSt.getConstantRange()
826*349cc55cSDimitry Andric             : ConstantRange::getFull(
827*349cc55cSDimitry Andric                   I.getOperand(0)->getType()->getScalarSizeInBits());
828*349cc55cSDimitry Andric 
829fe6060f1SDimitry Andric     Type *DestTy = I.getDestTy();
830fe6060f1SDimitry Andric     // Vectors where all elements have the same known constant range are treated
831fe6060f1SDimitry Andric     // as a single constant range in the lattice. When bitcasting such vectors,
832fe6060f1SDimitry Andric     // there is a mis-match between the width of the lattice value (single
833fe6060f1SDimitry Andric     // constant range) and the original operands (vector). Go to overdefined in
834fe6060f1SDimitry Andric     // that case.
835fe6060f1SDimitry Andric     if (I.getOpcode() == Instruction::BitCast &&
836fe6060f1SDimitry Andric         I.getOperand(0)->getType()->isVectorTy() &&
837fe6060f1SDimitry Andric         OpRange.getBitWidth() < DL.getTypeSizeInBits(DestTy))
838fe6060f1SDimitry Andric       return (void)markOverdefined(&I);
839fe6060f1SDimitry Andric 
840fe6060f1SDimitry Andric     ConstantRange Res =
841fe6060f1SDimitry Andric         OpRange.castOp(I.getOpcode(), DL.getTypeSizeInBits(DestTy));
842fe6060f1SDimitry Andric     mergeInValue(LV, &I, ValueLatticeElement::getRange(Res));
843*349cc55cSDimitry Andric   } else
844fe6060f1SDimitry Andric     markOverdefined(&I);
845fe6060f1SDimitry Andric }
846fe6060f1SDimitry Andric 
847fe6060f1SDimitry Andric void SCCPInstVisitor::visitExtractValueInst(ExtractValueInst &EVI) {
848fe6060f1SDimitry Andric   // If this returns a struct, mark all elements over defined, we don't track
849fe6060f1SDimitry Andric   // structs in structs.
850fe6060f1SDimitry Andric   if (EVI.getType()->isStructTy())
851fe6060f1SDimitry Andric     return (void)markOverdefined(&EVI);
852fe6060f1SDimitry Andric 
853fe6060f1SDimitry Andric   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
854fe6060f1SDimitry Andric   // discover a concrete value later.
855fe6060f1SDimitry Andric   if (ValueState[&EVI].isOverdefined())
856fe6060f1SDimitry Andric     return (void)markOverdefined(&EVI);
857fe6060f1SDimitry Andric 
858fe6060f1SDimitry Andric   // If this is extracting from more than one level of struct, we don't know.
859fe6060f1SDimitry Andric   if (EVI.getNumIndices() != 1)
860fe6060f1SDimitry Andric     return (void)markOverdefined(&EVI);
861fe6060f1SDimitry Andric 
862fe6060f1SDimitry Andric   Value *AggVal = EVI.getAggregateOperand();
863fe6060f1SDimitry Andric   if (AggVal->getType()->isStructTy()) {
864fe6060f1SDimitry Andric     unsigned i = *EVI.idx_begin();
865fe6060f1SDimitry Andric     ValueLatticeElement EltVal = getStructValueState(AggVal, i);
866fe6060f1SDimitry Andric     mergeInValue(getValueState(&EVI), &EVI, EltVal);
867fe6060f1SDimitry Andric   } else {
868fe6060f1SDimitry Andric     // Otherwise, must be extracting from an array.
869fe6060f1SDimitry Andric     return (void)markOverdefined(&EVI);
870fe6060f1SDimitry Andric   }
871fe6060f1SDimitry Andric }
872fe6060f1SDimitry Andric 
873fe6060f1SDimitry Andric void SCCPInstVisitor::visitInsertValueInst(InsertValueInst &IVI) {
874fe6060f1SDimitry Andric   auto *STy = dyn_cast<StructType>(IVI.getType());
875fe6060f1SDimitry Andric   if (!STy)
876fe6060f1SDimitry Andric     return (void)markOverdefined(&IVI);
877fe6060f1SDimitry Andric 
878fe6060f1SDimitry Andric   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
879fe6060f1SDimitry Andric   // discover a concrete value later.
880fe6060f1SDimitry Andric   if (isOverdefined(ValueState[&IVI]))
881fe6060f1SDimitry Andric     return (void)markOverdefined(&IVI);
882fe6060f1SDimitry Andric 
883fe6060f1SDimitry Andric   // If this has more than one index, we can't handle it, drive all results to
884fe6060f1SDimitry Andric   // undef.
885fe6060f1SDimitry Andric   if (IVI.getNumIndices() != 1)
886fe6060f1SDimitry Andric     return (void)markOverdefined(&IVI);
887fe6060f1SDimitry Andric 
888fe6060f1SDimitry Andric   Value *Aggr = IVI.getAggregateOperand();
889fe6060f1SDimitry Andric   unsigned Idx = *IVI.idx_begin();
890fe6060f1SDimitry Andric 
891fe6060f1SDimitry Andric   // Compute the result based on what we're inserting.
892fe6060f1SDimitry Andric   for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
893fe6060f1SDimitry Andric     // This passes through all values that aren't the inserted element.
894fe6060f1SDimitry Andric     if (i != Idx) {
895fe6060f1SDimitry Andric       ValueLatticeElement EltVal = getStructValueState(Aggr, i);
896fe6060f1SDimitry Andric       mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
897fe6060f1SDimitry Andric       continue;
898fe6060f1SDimitry Andric     }
899fe6060f1SDimitry Andric 
900fe6060f1SDimitry Andric     Value *Val = IVI.getInsertedValueOperand();
901fe6060f1SDimitry Andric     if (Val->getType()->isStructTy())
902fe6060f1SDimitry Andric       // We don't track structs in structs.
903fe6060f1SDimitry Andric       markOverdefined(getStructValueState(&IVI, i), &IVI);
904fe6060f1SDimitry Andric     else {
905fe6060f1SDimitry Andric       ValueLatticeElement InVal = getValueState(Val);
906fe6060f1SDimitry Andric       mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
907fe6060f1SDimitry Andric     }
908fe6060f1SDimitry Andric   }
909fe6060f1SDimitry Andric }
910fe6060f1SDimitry Andric 
911fe6060f1SDimitry Andric void SCCPInstVisitor::visitSelectInst(SelectInst &I) {
912fe6060f1SDimitry Andric   // If this select returns a struct, just mark the result overdefined.
913fe6060f1SDimitry Andric   // TODO: We could do a lot better than this if code actually uses this.
914fe6060f1SDimitry Andric   if (I.getType()->isStructTy())
915fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
916fe6060f1SDimitry Andric 
917fe6060f1SDimitry Andric   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
918fe6060f1SDimitry Andric   // discover a concrete value later.
919fe6060f1SDimitry Andric   if (ValueState[&I].isOverdefined())
920fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
921fe6060f1SDimitry Andric 
922fe6060f1SDimitry Andric   ValueLatticeElement CondValue = getValueState(I.getCondition());
923fe6060f1SDimitry Andric   if (CondValue.isUnknownOrUndef())
924fe6060f1SDimitry Andric     return;
925fe6060f1SDimitry Andric 
926fe6060f1SDimitry Andric   if (ConstantInt *CondCB = getConstantInt(CondValue)) {
927fe6060f1SDimitry Andric     Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
928fe6060f1SDimitry Andric     mergeInValue(&I, getValueState(OpVal));
929fe6060f1SDimitry Andric     return;
930fe6060f1SDimitry Andric   }
931fe6060f1SDimitry Andric 
932fe6060f1SDimitry Andric   // Otherwise, the condition is overdefined or a constant we can't evaluate.
933fe6060f1SDimitry Andric   // See if we can produce something better than overdefined based on the T/F
934fe6060f1SDimitry Andric   // value.
935fe6060f1SDimitry Andric   ValueLatticeElement TVal = getValueState(I.getTrueValue());
936fe6060f1SDimitry Andric   ValueLatticeElement FVal = getValueState(I.getFalseValue());
937fe6060f1SDimitry Andric 
938fe6060f1SDimitry Andric   bool Changed = ValueState[&I].mergeIn(TVal);
939fe6060f1SDimitry Andric   Changed |= ValueState[&I].mergeIn(FVal);
940fe6060f1SDimitry Andric   if (Changed)
941fe6060f1SDimitry Andric     pushToWorkListMsg(ValueState[&I], &I);
942fe6060f1SDimitry Andric }
943fe6060f1SDimitry Andric 
944fe6060f1SDimitry Andric // Handle Unary Operators.
945fe6060f1SDimitry Andric void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
946fe6060f1SDimitry Andric   ValueLatticeElement V0State = getValueState(I.getOperand(0));
947fe6060f1SDimitry Andric 
948fe6060f1SDimitry Andric   ValueLatticeElement &IV = ValueState[&I];
949fe6060f1SDimitry Andric   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
950fe6060f1SDimitry Andric   // discover a concrete value later.
951fe6060f1SDimitry Andric   if (isOverdefined(IV))
952fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
953fe6060f1SDimitry Andric 
954fe6060f1SDimitry Andric   if (isConstant(V0State)) {
955fe6060f1SDimitry Andric     Constant *C = ConstantExpr::get(I.getOpcode(), getConstant(V0State));
956fe6060f1SDimitry Andric 
957fe6060f1SDimitry Andric     // op Y -> undef.
958fe6060f1SDimitry Andric     if (isa<UndefValue>(C))
959fe6060f1SDimitry Andric       return;
960fe6060f1SDimitry Andric     return (void)markConstant(IV, &I, C);
961fe6060f1SDimitry Andric   }
962fe6060f1SDimitry Andric 
963fe6060f1SDimitry Andric   // If something is undef, wait for it to resolve.
964fe6060f1SDimitry Andric   if (!isOverdefined(V0State))
965fe6060f1SDimitry Andric     return;
966fe6060f1SDimitry Andric 
967fe6060f1SDimitry Andric   markOverdefined(&I);
968fe6060f1SDimitry Andric }
969fe6060f1SDimitry Andric 
970fe6060f1SDimitry Andric // Handle Binary Operators.
971fe6060f1SDimitry Andric void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
972fe6060f1SDimitry Andric   ValueLatticeElement V1State = getValueState(I.getOperand(0));
973fe6060f1SDimitry Andric   ValueLatticeElement V2State = getValueState(I.getOperand(1));
974fe6060f1SDimitry Andric 
975fe6060f1SDimitry Andric   ValueLatticeElement &IV = ValueState[&I];
976fe6060f1SDimitry Andric   if (IV.isOverdefined())
977fe6060f1SDimitry Andric     return;
978fe6060f1SDimitry Andric 
979fe6060f1SDimitry Andric   // If something is undef, wait for it to resolve.
980fe6060f1SDimitry Andric   if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef())
981fe6060f1SDimitry Andric     return;
982fe6060f1SDimitry Andric 
983fe6060f1SDimitry Andric   if (V1State.isOverdefined() && V2State.isOverdefined())
984fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
985fe6060f1SDimitry Andric 
986fe6060f1SDimitry Andric   // If either of the operands is a constant, try to fold it to a constant.
987fe6060f1SDimitry Andric   // TODO: Use information from notconstant better.
988fe6060f1SDimitry Andric   if ((V1State.isConstant() || V2State.isConstant())) {
989fe6060f1SDimitry Andric     Value *V1 = isConstant(V1State) ? getConstant(V1State) : I.getOperand(0);
990fe6060f1SDimitry Andric     Value *V2 = isConstant(V2State) ? getConstant(V2State) : I.getOperand(1);
991fe6060f1SDimitry Andric     Value *R = SimplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL));
992fe6060f1SDimitry Andric     auto *C = dyn_cast_or_null<Constant>(R);
993fe6060f1SDimitry Andric     if (C) {
994fe6060f1SDimitry Andric       // X op Y -> undef.
995fe6060f1SDimitry Andric       if (isa<UndefValue>(C))
996fe6060f1SDimitry Andric         return;
997fe6060f1SDimitry Andric       // Conservatively assume that the result may be based on operands that may
998fe6060f1SDimitry Andric       // be undef. Note that we use mergeInValue to combine the constant with
999fe6060f1SDimitry Andric       // the existing lattice value for I, as different constants might be found
1000fe6060f1SDimitry Andric       // after one of the operands go to overdefined, e.g. due to one operand
1001fe6060f1SDimitry Andric       // being a special floating value.
1002fe6060f1SDimitry Andric       ValueLatticeElement NewV;
1003fe6060f1SDimitry Andric       NewV.markConstant(C, /*MayIncludeUndef=*/true);
1004fe6060f1SDimitry Andric       return (void)mergeInValue(&I, NewV);
1005fe6060f1SDimitry Andric     }
1006fe6060f1SDimitry Andric   }
1007fe6060f1SDimitry Andric 
1008fe6060f1SDimitry Andric   // Only use ranges for binary operators on integers.
1009fe6060f1SDimitry Andric   if (!I.getType()->isIntegerTy())
1010fe6060f1SDimitry Andric     return markOverdefined(&I);
1011fe6060f1SDimitry Andric 
1012fe6060f1SDimitry Andric   // Try to simplify to a constant range.
1013fe6060f1SDimitry Andric   ConstantRange A = ConstantRange::getFull(I.getType()->getScalarSizeInBits());
1014fe6060f1SDimitry Andric   ConstantRange B = ConstantRange::getFull(I.getType()->getScalarSizeInBits());
1015fe6060f1SDimitry Andric   if (V1State.isConstantRange())
1016fe6060f1SDimitry Andric     A = V1State.getConstantRange();
1017fe6060f1SDimitry Andric   if (V2State.isConstantRange())
1018fe6060f1SDimitry Andric     B = V2State.getConstantRange();
1019fe6060f1SDimitry Andric 
1020fe6060f1SDimitry Andric   ConstantRange R = A.binaryOp(cast<BinaryOperator>(&I)->getOpcode(), B);
1021fe6060f1SDimitry Andric   mergeInValue(&I, ValueLatticeElement::getRange(R));
1022fe6060f1SDimitry Andric 
1023fe6060f1SDimitry Andric   // TODO: Currently we do not exploit special values that produce something
1024fe6060f1SDimitry Andric   // better than overdefined with an overdefined operand for vector or floating
1025fe6060f1SDimitry Andric   // point types, like and <4 x i32> overdefined, zeroinitializer.
1026fe6060f1SDimitry Andric }
1027fe6060f1SDimitry Andric 
1028fe6060f1SDimitry Andric // Handle ICmpInst instruction.
1029fe6060f1SDimitry Andric void SCCPInstVisitor::visitCmpInst(CmpInst &I) {
1030fe6060f1SDimitry Andric   // Do not cache this lookup, getValueState calls later in the function might
1031fe6060f1SDimitry Andric   // invalidate the reference.
1032fe6060f1SDimitry Andric   if (isOverdefined(ValueState[&I]))
1033fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
1034fe6060f1SDimitry Andric 
1035fe6060f1SDimitry Andric   Value *Op1 = I.getOperand(0);
1036fe6060f1SDimitry Andric   Value *Op2 = I.getOperand(1);
1037fe6060f1SDimitry Andric 
1038fe6060f1SDimitry Andric   // For parameters, use ParamState which includes constant range info if
1039fe6060f1SDimitry Andric   // available.
1040fe6060f1SDimitry Andric   auto V1State = getValueState(Op1);
1041fe6060f1SDimitry Andric   auto V2State = getValueState(Op2);
1042fe6060f1SDimitry Andric 
1043fe6060f1SDimitry Andric   Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State);
1044fe6060f1SDimitry Andric   if (C) {
1045fe6060f1SDimitry Andric     if (isa<UndefValue>(C))
1046fe6060f1SDimitry Andric       return;
1047fe6060f1SDimitry Andric     ValueLatticeElement CV;
1048fe6060f1SDimitry Andric     CV.markConstant(C);
1049fe6060f1SDimitry Andric     mergeInValue(&I, CV);
1050fe6060f1SDimitry Andric     return;
1051fe6060f1SDimitry Andric   }
1052fe6060f1SDimitry Andric 
1053fe6060f1SDimitry Andric   // If operands are still unknown, wait for it to resolve.
1054fe6060f1SDimitry Andric   if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) &&
1055fe6060f1SDimitry Andric       !isConstant(ValueState[&I]))
1056fe6060f1SDimitry Andric     return;
1057fe6060f1SDimitry Andric 
1058fe6060f1SDimitry Andric   markOverdefined(&I);
1059fe6060f1SDimitry Andric }
1060fe6060f1SDimitry Andric 
1061fe6060f1SDimitry Andric // Handle getelementptr instructions.  If all operands are constants then we
1062fe6060f1SDimitry Andric // can turn this into a getelementptr ConstantExpr.
1063fe6060f1SDimitry Andric void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
1064fe6060f1SDimitry Andric   if (isOverdefined(ValueState[&I]))
1065fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
1066fe6060f1SDimitry Andric 
1067fe6060f1SDimitry Andric   SmallVector<Constant *, 8> Operands;
1068fe6060f1SDimitry Andric   Operands.reserve(I.getNumOperands());
1069fe6060f1SDimitry Andric 
1070fe6060f1SDimitry Andric   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
1071fe6060f1SDimitry Andric     ValueLatticeElement State = getValueState(I.getOperand(i));
1072fe6060f1SDimitry Andric     if (State.isUnknownOrUndef())
1073fe6060f1SDimitry Andric       return; // Operands are not resolved yet.
1074fe6060f1SDimitry Andric 
1075fe6060f1SDimitry Andric     if (isOverdefined(State))
1076fe6060f1SDimitry Andric       return (void)markOverdefined(&I);
1077fe6060f1SDimitry Andric 
1078fe6060f1SDimitry Andric     if (Constant *C = getConstant(State)) {
1079fe6060f1SDimitry Andric       Operands.push_back(C);
1080fe6060f1SDimitry Andric       continue;
1081fe6060f1SDimitry Andric     }
1082fe6060f1SDimitry Andric 
1083fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
1084fe6060f1SDimitry Andric   }
1085fe6060f1SDimitry Andric 
1086fe6060f1SDimitry Andric   Constant *Ptr = Operands[0];
1087fe6060f1SDimitry Andric   auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end());
1088fe6060f1SDimitry Andric   Constant *C =
1089fe6060f1SDimitry Andric       ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr, Indices);
1090fe6060f1SDimitry Andric   if (isa<UndefValue>(C))
1091fe6060f1SDimitry Andric     return;
1092fe6060f1SDimitry Andric   markConstant(&I, C);
1093fe6060f1SDimitry Andric }
1094fe6060f1SDimitry Andric 
1095fe6060f1SDimitry Andric void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
1096fe6060f1SDimitry Andric   // If this store is of a struct, ignore it.
1097fe6060f1SDimitry Andric   if (SI.getOperand(0)->getType()->isStructTy())
1098fe6060f1SDimitry Andric     return;
1099fe6060f1SDimitry Andric 
1100fe6060f1SDimitry Andric   if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1101fe6060f1SDimitry Andric     return;
1102fe6060f1SDimitry Andric 
1103fe6060f1SDimitry Andric   GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1104fe6060f1SDimitry Andric   auto I = TrackedGlobals.find(GV);
1105fe6060f1SDimitry Andric   if (I == TrackedGlobals.end())
1106fe6060f1SDimitry Andric     return;
1107fe6060f1SDimitry Andric 
1108fe6060f1SDimitry Andric   // Get the value we are storing into the global, then merge it.
1109fe6060f1SDimitry Andric   mergeInValue(I->second, GV, getValueState(SI.getOperand(0)),
1110fe6060f1SDimitry Andric                ValueLatticeElement::MergeOptions().setCheckWiden(false));
1111fe6060f1SDimitry Andric   if (I->second.isOverdefined())
1112fe6060f1SDimitry Andric     TrackedGlobals.erase(I); // No need to keep tracking this!
1113fe6060f1SDimitry Andric }
1114fe6060f1SDimitry Andric 
1115fe6060f1SDimitry Andric static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
1116fe6060f1SDimitry Andric   if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
1117fe6060f1SDimitry Andric     if (I->getType()->isIntegerTy())
1118fe6060f1SDimitry Andric       return ValueLatticeElement::getRange(
1119fe6060f1SDimitry Andric           getConstantRangeFromMetadata(*Ranges));
1120fe6060f1SDimitry Andric   if (I->hasMetadata(LLVMContext::MD_nonnull))
1121fe6060f1SDimitry Andric     return ValueLatticeElement::getNot(
1122fe6060f1SDimitry Andric         ConstantPointerNull::get(cast<PointerType>(I->getType())));
1123fe6060f1SDimitry Andric   return ValueLatticeElement::getOverdefined();
1124fe6060f1SDimitry Andric }
1125fe6060f1SDimitry Andric 
1126fe6060f1SDimitry Andric // Handle load instructions.  If the operand is a constant pointer to a constant
1127fe6060f1SDimitry Andric // global, we can replace the load with the loaded constant value!
1128fe6060f1SDimitry Andric void SCCPInstVisitor::visitLoadInst(LoadInst &I) {
1129fe6060f1SDimitry Andric   // If this load is of a struct or the load is volatile, just mark the result
1130fe6060f1SDimitry Andric   // as overdefined.
1131fe6060f1SDimitry Andric   if (I.getType()->isStructTy() || I.isVolatile())
1132fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
1133fe6060f1SDimitry Andric 
1134fe6060f1SDimitry Andric   // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1135fe6060f1SDimitry Andric   // discover a concrete value later.
1136fe6060f1SDimitry Andric   if (ValueState[&I].isOverdefined())
1137fe6060f1SDimitry Andric     return (void)markOverdefined(&I);
1138fe6060f1SDimitry Andric 
1139fe6060f1SDimitry Andric   ValueLatticeElement PtrVal = getValueState(I.getOperand(0));
1140fe6060f1SDimitry Andric   if (PtrVal.isUnknownOrUndef())
1141fe6060f1SDimitry Andric     return; // The pointer is not resolved yet!
1142fe6060f1SDimitry Andric 
1143fe6060f1SDimitry Andric   ValueLatticeElement &IV = ValueState[&I];
1144fe6060f1SDimitry Andric 
1145fe6060f1SDimitry Andric   if (isConstant(PtrVal)) {
1146fe6060f1SDimitry Andric     Constant *Ptr = getConstant(PtrVal);
1147fe6060f1SDimitry Andric 
1148fe6060f1SDimitry Andric     // load null is undefined.
1149fe6060f1SDimitry Andric     if (isa<ConstantPointerNull>(Ptr)) {
1150fe6060f1SDimitry Andric       if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1151fe6060f1SDimitry Andric         return (void)markOverdefined(IV, &I);
1152fe6060f1SDimitry Andric       else
1153fe6060f1SDimitry Andric         return;
1154fe6060f1SDimitry Andric     }
1155fe6060f1SDimitry Andric 
1156fe6060f1SDimitry Andric     // Transform load (constant global) into the value loaded.
1157fe6060f1SDimitry Andric     if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1158fe6060f1SDimitry Andric       if (!TrackedGlobals.empty()) {
1159fe6060f1SDimitry Andric         // If we are tracking this global, merge in the known value for it.
1160fe6060f1SDimitry Andric         auto It = TrackedGlobals.find(GV);
1161fe6060f1SDimitry Andric         if (It != TrackedGlobals.end()) {
1162fe6060f1SDimitry Andric           mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1163fe6060f1SDimitry Andric           return;
1164fe6060f1SDimitry Andric         }
1165fe6060f1SDimitry Andric       }
1166fe6060f1SDimitry Andric     }
1167fe6060f1SDimitry Andric 
1168fe6060f1SDimitry Andric     // Transform load from a constant into a constant if possible.
1169fe6060f1SDimitry Andric     if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) {
1170fe6060f1SDimitry Andric       if (isa<UndefValue>(C))
1171fe6060f1SDimitry Andric         return;
1172fe6060f1SDimitry Andric       return (void)markConstant(IV, &I, C);
1173fe6060f1SDimitry Andric     }
1174fe6060f1SDimitry Andric   }
1175fe6060f1SDimitry Andric 
1176fe6060f1SDimitry Andric   // Fall back to metadata.
1177fe6060f1SDimitry Andric   mergeInValue(&I, getValueFromMetadata(&I));
1178fe6060f1SDimitry Andric }
1179fe6060f1SDimitry Andric 
1180fe6060f1SDimitry Andric void SCCPInstVisitor::visitCallBase(CallBase &CB) {
1181fe6060f1SDimitry Andric   handleCallResult(CB);
1182fe6060f1SDimitry Andric   handleCallArguments(CB);
1183fe6060f1SDimitry Andric }
1184fe6060f1SDimitry Andric 
1185fe6060f1SDimitry Andric void SCCPInstVisitor::handleCallOverdefined(CallBase &CB) {
1186fe6060f1SDimitry Andric   Function *F = CB.getCalledFunction();
1187fe6060f1SDimitry Andric 
1188fe6060f1SDimitry Andric   // Void return and not tracking callee, just bail.
1189fe6060f1SDimitry Andric   if (CB.getType()->isVoidTy())
1190fe6060f1SDimitry Andric     return;
1191fe6060f1SDimitry Andric 
1192fe6060f1SDimitry Andric   // Always mark struct return as overdefined.
1193fe6060f1SDimitry Andric   if (CB.getType()->isStructTy())
1194fe6060f1SDimitry Andric     return (void)markOverdefined(&CB);
1195fe6060f1SDimitry Andric 
1196fe6060f1SDimitry Andric   // Otherwise, if we have a single return value case, and if the function is
1197fe6060f1SDimitry Andric   // a declaration, maybe we can constant fold it.
1198fe6060f1SDimitry Andric   if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
1199fe6060f1SDimitry Andric     SmallVector<Constant *, 8> Operands;
1200*349cc55cSDimitry Andric     for (const Use &A : CB.args()) {
1201*349cc55cSDimitry Andric       if (A.get()->getType()->isStructTy())
1202fe6060f1SDimitry Andric         return markOverdefined(&CB); // Can't handle struct args.
1203*349cc55cSDimitry Andric       ValueLatticeElement State = getValueState(A);
1204fe6060f1SDimitry Andric 
1205fe6060f1SDimitry Andric       if (State.isUnknownOrUndef())
1206fe6060f1SDimitry Andric         return; // Operands are not resolved yet.
1207fe6060f1SDimitry Andric       if (isOverdefined(State))
1208fe6060f1SDimitry Andric         return (void)markOverdefined(&CB);
1209fe6060f1SDimitry Andric       assert(isConstant(State) && "Unknown state!");
1210fe6060f1SDimitry Andric       Operands.push_back(getConstant(State));
1211fe6060f1SDimitry Andric     }
1212fe6060f1SDimitry Andric 
1213fe6060f1SDimitry Andric     if (isOverdefined(getValueState(&CB)))
1214fe6060f1SDimitry Andric       return (void)markOverdefined(&CB);
1215fe6060f1SDimitry Andric 
1216fe6060f1SDimitry Andric     // If we can constant fold this, mark the result of the call as a
1217fe6060f1SDimitry Andric     // constant.
1218fe6060f1SDimitry Andric     if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F))) {
1219fe6060f1SDimitry Andric       // call -> undef.
1220fe6060f1SDimitry Andric       if (isa<UndefValue>(C))
1221fe6060f1SDimitry Andric         return;
1222fe6060f1SDimitry Andric       return (void)markConstant(&CB, C);
1223fe6060f1SDimitry Andric     }
1224fe6060f1SDimitry Andric   }
1225fe6060f1SDimitry Andric 
1226fe6060f1SDimitry Andric   // Fall back to metadata.
1227fe6060f1SDimitry Andric   mergeInValue(&CB, getValueFromMetadata(&CB));
1228fe6060f1SDimitry Andric }
1229fe6060f1SDimitry Andric 
1230fe6060f1SDimitry Andric void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
1231fe6060f1SDimitry Andric   Function *F = CB.getCalledFunction();
1232fe6060f1SDimitry Andric   // If this is a local function that doesn't have its address taken, mark its
1233fe6060f1SDimitry Andric   // entry block executable and merge in the actual arguments to the call into
1234fe6060f1SDimitry Andric   // the formal arguments of the function.
1235fe6060f1SDimitry Andric   if (!TrackingIncomingArguments.empty() &&
1236fe6060f1SDimitry Andric       TrackingIncomingArguments.count(F)) {
1237fe6060f1SDimitry Andric     markBlockExecutable(&F->front());
1238fe6060f1SDimitry Andric 
1239fe6060f1SDimitry Andric     // Propagate information from this call site into the callee.
1240fe6060f1SDimitry Andric     auto CAI = CB.arg_begin();
1241fe6060f1SDimitry Andric     for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
1242fe6060f1SDimitry Andric          ++AI, ++CAI) {
1243fe6060f1SDimitry Andric       // If this argument is byval, and if the function is not readonly, there
1244fe6060f1SDimitry Andric       // will be an implicit copy formed of the input aggregate.
1245fe6060f1SDimitry Andric       if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1246fe6060f1SDimitry Andric         markOverdefined(&*AI);
1247fe6060f1SDimitry Andric         continue;
1248fe6060f1SDimitry Andric       }
1249fe6060f1SDimitry Andric 
1250fe6060f1SDimitry Andric       if (auto *STy = dyn_cast<StructType>(AI->getType())) {
1251fe6060f1SDimitry Andric         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1252fe6060f1SDimitry Andric           ValueLatticeElement CallArg = getStructValueState(*CAI, i);
1253fe6060f1SDimitry Andric           mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg,
1254fe6060f1SDimitry Andric                        getMaxWidenStepsOpts());
1255fe6060f1SDimitry Andric         }
1256fe6060f1SDimitry Andric       } else
1257fe6060f1SDimitry Andric         mergeInValue(&*AI, getValueState(*CAI), getMaxWidenStepsOpts());
1258fe6060f1SDimitry Andric     }
1259fe6060f1SDimitry Andric   }
1260fe6060f1SDimitry Andric }
1261fe6060f1SDimitry Andric 
1262fe6060f1SDimitry Andric void SCCPInstVisitor::handleCallResult(CallBase &CB) {
1263fe6060f1SDimitry Andric   Function *F = CB.getCalledFunction();
1264fe6060f1SDimitry Andric 
1265fe6060f1SDimitry Andric   if (auto *II = dyn_cast<IntrinsicInst>(&CB)) {
1266fe6060f1SDimitry Andric     if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
1267fe6060f1SDimitry Andric       if (ValueState[&CB].isOverdefined())
1268fe6060f1SDimitry Andric         return;
1269fe6060f1SDimitry Andric 
1270fe6060f1SDimitry Andric       Value *CopyOf = CB.getOperand(0);
1271fe6060f1SDimitry Andric       ValueLatticeElement CopyOfVal = getValueState(CopyOf);
1272fe6060f1SDimitry Andric       const auto *PI = getPredicateInfoFor(&CB);
1273fe6060f1SDimitry Andric       assert(PI && "Missing predicate info for ssa.copy");
1274fe6060f1SDimitry Andric 
1275fe6060f1SDimitry Andric       const Optional<PredicateConstraint> &Constraint = PI->getConstraint();
1276fe6060f1SDimitry Andric       if (!Constraint) {
1277fe6060f1SDimitry Andric         mergeInValue(ValueState[&CB], &CB, CopyOfVal);
1278fe6060f1SDimitry Andric         return;
1279fe6060f1SDimitry Andric       }
1280fe6060f1SDimitry Andric 
1281fe6060f1SDimitry Andric       CmpInst::Predicate Pred = Constraint->Predicate;
1282fe6060f1SDimitry Andric       Value *OtherOp = Constraint->OtherOp;
1283fe6060f1SDimitry Andric 
1284fe6060f1SDimitry Andric       // Wait until OtherOp is resolved.
1285fe6060f1SDimitry Andric       if (getValueState(OtherOp).isUnknown()) {
1286fe6060f1SDimitry Andric         addAdditionalUser(OtherOp, &CB);
1287fe6060f1SDimitry Andric         return;
1288fe6060f1SDimitry Andric       }
1289fe6060f1SDimitry Andric 
1290fe6060f1SDimitry Andric       // TODO: Actually filp MayIncludeUndef for the created range to false,
1291fe6060f1SDimitry Andric       // once most places in the optimizer respect the branches on
1292fe6060f1SDimitry Andric       // undef/poison are UB rule. The reason why the new range cannot be
1293fe6060f1SDimitry Andric       // undef is as follows below:
1294fe6060f1SDimitry Andric       // The new range is based on a branch condition. That guarantees that
1295fe6060f1SDimitry Andric       // neither of the compare operands can be undef in the branch targets,
1296fe6060f1SDimitry Andric       // unless we have conditions that are always true/false (e.g. icmp ule
1297fe6060f1SDimitry Andric       // i32, %a, i32_max). For the latter overdefined/empty range will be
1298fe6060f1SDimitry Andric       // inferred, but the branch will get folded accordingly anyways.
1299fe6060f1SDimitry Andric       bool MayIncludeUndef = !isa<PredicateAssume>(PI);
1300fe6060f1SDimitry Andric 
1301fe6060f1SDimitry Andric       ValueLatticeElement CondVal = getValueState(OtherOp);
1302fe6060f1SDimitry Andric       ValueLatticeElement &IV = ValueState[&CB];
1303fe6060f1SDimitry Andric       if (CondVal.isConstantRange() || CopyOfVal.isConstantRange()) {
1304fe6060f1SDimitry Andric         auto ImposedCR =
1305fe6060f1SDimitry Andric             ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
1306fe6060f1SDimitry Andric 
1307fe6060f1SDimitry Andric         // Get the range imposed by the condition.
1308fe6060f1SDimitry Andric         if (CondVal.isConstantRange())
1309fe6060f1SDimitry Andric           ImposedCR = ConstantRange::makeAllowedICmpRegion(
1310fe6060f1SDimitry Andric               Pred, CondVal.getConstantRange());
1311fe6060f1SDimitry Andric 
1312fe6060f1SDimitry Andric         // Combine range info for the original value with the new range from the
1313fe6060f1SDimitry Andric         // condition.
1314fe6060f1SDimitry Andric         auto CopyOfCR = CopyOfVal.isConstantRange()
1315fe6060f1SDimitry Andric                             ? CopyOfVal.getConstantRange()
1316fe6060f1SDimitry Andric                             : ConstantRange::getFull(
1317fe6060f1SDimitry Andric                                   DL.getTypeSizeInBits(CopyOf->getType()));
1318fe6060f1SDimitry Andric         auto NewCR = ImposedCR.intersectWith(CopyOfCR);
1319fe6060f1SDimitry Andric         // If the existing information is != x, do not use the information from
1320fe6060f1SDimitry Andric         // a chained predicate, as the != x information is more likely to be
1321fe6060f1SDimitry Andric         // helpful in practice.
1322fe6060f1SDimitry Andric         if (!CopyOfCR.contains(NewCR) && CopyOfCR.getSingleMissingElement())
1323fe6060f1SDimitry Andric           NewCR = CopyOfCR;
1324fe6060f1SDimitry Andric 
1325fe6060f1SDimitry Andric         addAdditionalUser(OtherOp, &CB);
1326fe6060f1SDimitry Andric         mergeInValue(IV, &CB,
1327fe6060f1SDimitry Andric                      ValueLatticeElement::getRange(NewCR, MayIncludeUndef));
1328fe6060f1SDimitry Andric         return;
1329fe6060f1SDimitry Andric       } else if (Pred == CmpInst::ICMP_EQ && CondVal.isConstant()) {
1330fe6060f1SDimitry Andric         // For non-integer values or integer constant expressions, only
1331fe6060f1SDimitry Andric         // propagate equal constants.
1332fe6060f1SDimitry Andric         addAdditionalUser(OtherOp, &CB);
1333fe6060f1SDimitry Andric         mergeInValue(IV, &CB, CondVal);
1334fe6060f1SDimitry Andric         return;
1335fe6060f1SDimitry Andric       } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant() &&
1336fe6060f1SDimitry Andric                  !MayIncludeUndef) {
1337fe6060f1SDimitry Andric         // Propagate inequalities.
1338fe6060f1SDimitry Andric         addAdditionalUser(OtherOp, &CB);
1339fe6060f1SDimitry Andric         mergeInValue(IV, &CB,
1340fe6060f1SDimitry Andric                      ValueLatticeElement::getNot(CondVal.getConstant()));
1341fe6060f1SDimitry Andric         return;
1342fe6060f1SDimitry Andric       }
1343fe6060f1SDimitry Andric 
1344fe6060f1SDimitry Andric       return (void)mergeInValue(IV, &CB, CopyOfVal);
1345fe6060f1SDimitry Andric     }
1346fe6060f1SDimitry Andric 
1347fe6060f1SDimitry Andric     if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
1348fe6060f1SDimitry Andric       // Compute result range for intrinsics supported by ConstantRange.
1349fe6060f1SDimitry Andric       // Do this even if we don't know a range for all operands, as we may
1350fe6060f1SDimitry Andric       // still know something about the result range, e.g. of abs(x).
1351fe6060f1SDimitry Andric       SmallVector<ConstantRange, 2> OpRanges;
1352fe6060f1SDimitry Andric       for (Value *Op : II->args()) {
1353fe6060f1SDimitry Andric         const ValueLatticeElement &State = getValueState(Op);
1354fe6060f1SDimitry Andric         if (State.isConstantRange())
1355fe6060f1SDimitry Andric           OpRanges.push_back(State.getConstantRange());
1356fe6060f1SDimitry Andric         else
1357fe6060f1SDimitry Andric           OpRanges.push_back(
1358fe6060f1SDimitry Andric               ConstantRange::getFull(Op->getType()->getScalarSizeInBits()));
1359fe6060f1SDimitry Andric       }
1360fe6060f1SDimitry Andric 
1361fe6060f1SDimitry Andric       ConstantRange Result =
1362fe6060f1SDimitry Andric           ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges);
1363fe6060f1SDimitry Andric       return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
1364fe6060f1SDimitry Andric     }
1365fe6060f1SDimitry Andric   }
1366fe6060f1SDimitry Andric 
1367fe6060f1SDimitry Andric   // The common case is that we aren't tracking the callee, either because we
1368fe6060f1SDimitry Andric   // are not doing interprocedural analysis or the callee is indirect, or is
1369fe6060f1SDimitry Andric   // external.  Handle these cases first.
1370fe6060f1SDimitry Andric   if (!F || F->isDeclaration())
1371fe6060f1SDimitry Andric     return handleCallOverdefined(CB);
1372fe6060f1SDimitry Andric 
1373fe6060f1SDimitry Andric   // If this is a single/zero retval case, see if we're tracking the function.
1374fe6060f1SDimitry Andric   if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
1375fe6060f1SDimitry Andric     if (!MRVFunctionsTracked.count(F))
1376fe6060f1SDimitry Andric       return handleCallOverdefined(CB); // Not tracking this callee.
1377fe6060f1SDimitry Andric 
1378fe6060f1SDimitry Andric     // If we are tracking this callee, propagate the result of the function
1379fe6060f1SDimitry Andric     // into this call site.
1380fe6060f1SDimitry Andric     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1381fe6060f1SDimitry Andric       mergeInValue(getStructValueState(&CB, i), &CB,
1382fe6060f1SDimitry Andric                    TrackedMultipleRetVals[std::make_pair(F, i)],
1383fe6060f1SDimitry Andric                    getMaxWidenStepsOpts());
1384fe6060f1SDimitry Andric   } else {
1385fe6060f1SDimitry Andric     auto TFRVI = TrackedRetVals.find(F);
1386fe6060f1SDimitry Andric     if (TFRVI == TrackedRetVals.end())
1387fe6060f1SDimitry Andric       return handleCallOverdefined(CB); // Not tracking this callee.
1388fe6060f1SDimitry Andric 
1389fe6060f1SDimitry Andric     // If so, propagate the return value of the callee into this call result.
1390fe6060f1SDimitry Andric     mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts());
1391fe6060f1SDimitry Andric   }
1392fe6060f1SDimitry Andric }
1393fe6060f1SDimitry Andric 
1394fe6060f1SDimitry Andric void SCCPInstVisitor::solve() {
1395fe6060f1SDimitry Andric   // Process the work lists until they are empty!
1396fe6060f1SDimitry Andric   while (!BBWorkList.empty() || !InstWorkList.empty() ||
1397fe6060f1SDimitry Andric          !OverdefinedInstWorkList.empty()) {
1398fe6060f1SDimitry Andric     // Process the overdefined instruction's work list first, which drives other
1399fe6060f1SDimitry Andric     // things to overdefined more quickly.
1400fe6060f1SDimitry Andric     while (!OverdefinedInstWorkList.empty()) {
1401fe6060f1SDimitry Andric       Value *I = OverdefinedInstWorkList.pop_back_val();
1402fe6060f1SDimitry Andric 
1403fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
1404fe6060f1SDimitry Andric 
1405fe6060f1SDimitry Andric       // "I" got into the work list because it either made the transition from
1406fe6060f1SDimitry Andric       // bottom to constant, or to overdefined.
1407fe6060f1SDimitry Andric       //
1408fe6060f1SDimitry Andric       // Anything on this worklist that is overdefined need not be visited
1409fe6060f1SDimitry Andric       // since all of its users will have already been marked as overdefined
1410fe6060f1SDimitry Andric       // Update all of the users of this instruction's value.
1411fe6060f1SDimitry Andric       //
1412fe6060f1SDimitry Andric       markUsersAsChanged(I);
1413fe6060f1SDimitry Andric     }
1414fe6060f1SDimitry Andric 
1415fe6060f1SDimitry Andric     // Process the instruction work list.
1416fe6060f1SDimitry Andric     while (!InstWorkList.empty()) {
1417fe6060f1SDimitry Andric       Value *I = InstWorkList.pop_back_val();
1418fe6060f1SDimitry Andric 
1419fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
1420fe6060f1SDimitry Andric 
1421fe6060f1SDimitry Andric       // "I" got into the work list because it made the transition from undef to
1422fe6060f1SDimitry Andric       // constant.
1423fe6060f1SDimitry Andric       //
1424fe6060f1SDimitry Andric       // Anything on this worklist that is overdefined need not be visited
1425fe6060f1SDimitry Andric       // since all of its users will have already been marked as overdefined.
1426fe6060f1SDimitry Andric       // Update all of the users of this instruction's value.
1427fe6060f1SDimitry Andric       //
1428fe6060f1SDimitry Andric       if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
1429fe6060f1SDimitry Andric         markUsersAsChanged(I);
1430fe6060f1SDimitry Andric     }
1431fe6060f1SDimitry Andric 
1432fe6060f1SDimitry Andric     // Process the basic block work list.
1433fe6060f1SDimitry Andric     while (!BBWorkList.empty()) {
1434fe6060f1SDimitry Andric       BasicBlock *BB = BBWorkList.pop_back_val();
1435fe6060f1SDimitry Andric 
1436fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
1437fe6060f1SDimitry Andric 
1438fe6060f1SDimitry Andric       // Notify all instructions in this basic block that they are newly
1439fe6060f1SDimitry Andric       // executable.
1440fe6060f1SDimitry Andric       visit(BB);
1441fe6060f1SDimitry Andric     }
1442fe6060f1SDimitry Andric   }
1443fe6060f1SDimitry Andric }
1444fe6060f1SDimitry Andric 
1445fe6060f1SDimitry Andric /// resolvedUndefsIn - While solving the dataflow for a function, we assume
1446fe6060f1SDimitry Andric /// that branches on undef values cannot reach any of their successors.
1447fe6060f1SDimitry Andric /// However, this is not a safe assumption.  After we solve dataflow, this
1448fe6060f1SDimitry Andric /// method should be use to handle this.  If this returns true, the solver
1449fe6060f1SDimitry Andric /// should be rerun.
1450fe6060f1SDimitry Andric ///
1451fe6060f1SDimitry Andric /// This method handles this by finding an unresolved branch and marking it one
1452fe6060f1SDimitry Andric /// of the edges from the block as being feasible, even though the condition
1453fe6060f1SDimitry Andric /// doesn't say it would otherwise be.  This allows SCCP to find the rest of the
1454fe6060f1SDimitry Andric /// CFG and only slightly pessimizes the analysis results (by marking one,
1455fe6060f1SDimitry Andric /// potentially infeasible, edge feasible).  This cannot usefully modify the
1456fe6060f1SDimitry Andric /// constraints on the condition of the branch, as that would impact other users
1457fe6060f1SDimitry Andric /// of the value.
1458fe6060f1SDimitry Andric ///
1459fe6060f1SDimitry Andric /// This scan also checks for values that use undefs. It conservatively marks
1460fe6060f1SDimitry Andric /// them as overdefined.
1461fe6060f1SDimitry Andric bool SCCPInstVisitor::resolvedUndefsIn(Function &F) {
1462fe6060f1SDimitry Andric   bool MadeChange = false;
1463fe6060f1SDimitry Andric   for (BasicBlock &BB : F) {
1464fe6060f1SDimitry Andric     if (!BBExecutable.count(&BB))
1465fe6060f1SDimitry Andric       continue;
1466fe6060f1SDimitry Andric 
1467fe6060f1SDimitry Andric     for (Instruction &I : BB) {
1468fe6060f1SDimitry Andric       // Look for instructions which produce undef values.
1469fe6060f1SDimitry Andric       if (I.getType()->isVoidTy())
1470fe6060f1SDimitry Andric         continue;
1471fe6060f1SDimitry Andric 
1472fe6060f1SDimitry Andric       if (auto *STy = dyn_cast<StructType>(I.getType())) {
1473fe6060f1SDimitry Andric         // Only a few things that can be structs matter for undef.
1474fe6060f1SDimitry Andric 
1475fe6060f1SDimitry Andric         // Tracked calls must never be marked overdefined in resolvedUndefsIn.
1476fe6060f1SDimitry Andric         if (auto *CB = dyn_cast<CallBase>(&I))
1477fe6060f1SDimitry Andric           if (Function *F = CB->getCalledFunction())
1478fe6060f1SDimitry Andric             if (MRVFunctionsTracked.count(F))
1479fe6060f1SDimitry Andric               continue;
1480fe6060f1SDimitry Andric 
1481fe6060f1SDimitry Andric         // extractvalue and insertvalue don't need to be marked; they are
1482fe6060f1SDimitry Andric         // tracked as precisely as their operands.
1483fe6060f1SDimitry Andric         if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
1484fe6060f1SDimitry Andric           continue;
1485fe6060f1SDimitry Andric         // Send the results of everything else to overdefined.  We could be
1486fe6060f1SDimitry Andric         // more precise than this but it isn't worth bothering.
1487fe6060f1SDimitry Andric         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1488fe6060f1SDimitry Andric           ValueLatticeElement &LV = getStructValueState(&I, i);
1489fe6060f1SDimitry Andric           if (LV.isUnknownOrUndef()) {
1490fe6060f1SDimitry Andric             markOverdefined(LV, &I);
1491fe6060f1SDimitry Andric             MadeChange = true;
1492fe6060f1SDimitry Andric           }
1493fe6060f1SDimitry Andric         }
1494fe6060f1SDimitry Andric         continue;
1495fe6060f1SDimitry Andric       }
1496fe6060f1SDimitry Andric 
1497fe6060f1SDimitry Andric       ValueLatticeElement &LV = getValueState(&I);
1498fe6060f1SDimitry Andric       if (!LV.isUnknownOrUndef())
1499fe6060f1SDimitry Andric         continue;
1500fe6060f1SDimitry Andric 
1501fe6060f1SDimitry Andric       // There are two reasons a call can have an undef result
1502fe6060f1SDimitry Andric       // 1. It could be tracked.
1503fe6060f1SDimitry Andric       // 2. It could be constant-foldable.
1504fe6060f1SDimitry Andric       // Because of the way we solve return values, tracked calls must
1505fe6060f1SDimitry Andric       // never be marked overdefined in resolvedUndefsIn.
1506fe6060f1SDimitry Andric       if (auto *CB = dyn_cast<CallBase>(&I))
1507fe6060f1SDimitry Andric         if (Function *F = CB->getCalledFunction())
1508fe6060f1SDimitry Andric           if (TrackedRetVals.count(F))
1509fe6060f1SDimitry Andric             continue;
1510fe6060f1SDimitry Andric 
1511fe6060f1SDimitry Andric       if (isa<LoadInst>(I)) {
1512fe6060f1SDimitry Andric         // A load here means one of two things: a load of undef from a global,
1513fe6060f1SDimitry Andric         // a load from an unknown pointer.  Either way, having it return undef
1514fe6060f1SDimitry Andric         // is okay.
1515fe6060f1SDimitry Andric         continue;
1516fe6060f1SDimitry Andric       }
1517fe6060f1SDimitry Andric 
1518fe6060f1SDimitry Andric       markOverdefined(&I);
1519fe6060f1SDimitry Andric       MadeChange = true;
1520fe6060f1SDimitry Andric     }
1521fe6060f1SDimitry Andric 
1522fe6060f1SDimitry Andric     // Check to see if we have a branch or switch on an undefined value.  If so
1523fe6060f1SDimitry Andric     // we force the branch to go one way or the other to make the successor
1524fe6060f1SDimitry Andric     // values live.  It doesn't really matter which way we force it.
1525fe6060f1SDimitry Andric     Instruction *TI = BB.getTerminator();
1526fe6060f1SDimitry Andric     if (auto *BI = dyn_cast<BranchInst>(TI)) {
1527fe6060f1SDimitry Andric       if (!BI->isConditional())
1528fe6060f1SDimitry Andric         continue;
1529fe6060f1SDimitry Andric       if (!getValueState(BI->getCondition()).isUnknownOrUndef())
1530fe6060f1SDimitry Andric         continue;
1531fe6060f1SDimitry Andric 
1532fe6060f1SDimitry Andric       // If the input to SCCP is actually branch on undef, fix the undef to
1533fe6060f1SDimitry Andric       // false.
1534fe6060f1SDimitry Andric       if (isa<UndefValue>(BI->getCondition())) {
1535fe6060f1SDimitry Andric         BI->setCondition(ConstantInt::getFalse(BI->getContext()));
1536fe6060f1SDimitry Andric         markEdgeExecutable(&BB, TI->getSuccessor(1));
1537fe6060f1SDimitry Andric         MadeChange = true;
1538fe6060f1SDimitry Andric         continue;
1539fe6060f1SDimitry Andric       }
1540fe6060f1SDimitry Andric 
1541fe6060f1SDimitry Andric       // Otherwise, it is a branch on a symbolic value which is currently
1542fe6060f1SDimitry Andric       // considered to be undef.  Make sure some edge is executable, so a
1543fe6060f1SDimitry Andric       // branch on "undef" always flows somewhere.
1544fe6060f1SDimitry Andric       // FIXME: Distinguish between dead code and an LLVM "undef" value.
1545fe6060f1SDimitry Andric       BasicBlock *DefaultSuccessor = TI->getSuccessor(1);
1546fe6060f1SDimitry Andric       if (markEdgeExecutable(&BB, DefaultSuccessor))
1547fe6060f1SDimitry Andric         MadeChange = true;
1548fe6060f1SDimitry Andric 
1549fe6060f1SDimitry Andric       continue;
1550fe6060f1SDimitry Andric     }
1551fe6060f1SDimitry Andric 
1552fe6060f1SDimitry Andric     if (auto *IBR = dyn_cast<IndirectBrInst>(TI)) {
1553fe6060f1SDimitry Andric       // Indirect branch with no successor ?. Its ok to assume it branches
1554fe6060f1SDimitry Andric       // to no target.
1555fe6060f1SDimitry Andric       if (IBR->getNumSuccessors() < 1)
1556fe6060f1SDimitry Andric         continue;
1557fe6060f1SDimitry Andric 
1558fe6060f1SDimitry Andric       if (!getValueState(IBR->getAddress()).isUnknownOrUndef())
1559fe6060f1SDimitry Andric         continue;
1560fe6060f1SDimitry Andric 
1561fe6060f1SDimitry Andric       // If the input to SCCP is actually branch on undef, fix the undef to
1562fe6060f1SDimitry Andric       // the first successor of the indirect branch.
1563fe6060f1SDimitry Andric       if (isa<UndefValue>(IBR->getAddress())) {
1564fe6060f1SDimitry Andric         IBR->setAddress(BlockAddress::get(IBR->getSuccessor(0)));
1565fe6060f1SDimitry Andric         markEdgeExecutable(&BB, IBR->getSuccessor(0));
1566fe6060f1SDimitry Andric         MadeChange = true;
1567fe6060f1SDimitry Andric         continue;
1568fe6060f1SDimitry Andric       }
1569fe6060f1SDimitry Andric 
1570fe6060f1SDimitry Andric       // Otherwise, it is a branch on a symbolic value which is currently
1571fe6060f1SDimitry Andric       // considered to be undef.  Make sure some edge is executable, so a
1572fe6060f1SDimitry Andric       // branch on "undef" always flows somewhere.
1573fe6060f1SDimitry Andric       // FIXME: IndirectBr on "undef" doesn't actually need to go anywhere:
1574fe6060f1SDimitry Andric       // we can assume the branch has undefined behavior instead.
1575fe6060f1SDimitry Andric       BasicBlock *DefaultSuccessor = IBR->getSuccessor(0);
1576fe6060f1SDimitry Andric       if (markEdgeExecutable(&BB, DefaultSuccessor))
1577fe6060f1SDimitry Andric         MadeChange = true;
1578fe6060f1SDimitry Andric 
1579fe6060f1SDimitry Andric       continue;
1580fe6060f1SDimitry Andric     }
1581fe6060f1SDimitry Andric 
1582fe6060f1SDimitry Andric     if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1583fe6060f1SDimitry Andric       if (!SI->getNumCases() ||
1584fe6060f1SDimitry Andric           !getValueState(SI->getCondition()).isUnknownOrUndef())
1585fe6060f1SDimitry Andric         continue;
1586fe6060f1SDimitry Andric 
1587fe6060f1SDimitry Andric       // If the input to SCCP is actually switch on undef, fix the undef to
1588fe6060f1SDimitry Andric       // the first constant.
1589fe6060f1SDimitry Andric       if (isa<UndefValue>(SI->getCondition())) {
1590fe6060f1SDimitry Andric         SI->setCondition(SI->case_begin()->getCaseValue());
1591fe6060f1SDimitry Andric         markEdgeExecutable(&BB, SI->case_begin()->getCaseSuccessor());
1592fe6060f1SDimitry Andric         MadeChange = true;
1593fe6060f1SDimitry Andric         continue;
1594fe6060f1SDimitry Andric       }
1595fe6060f1SDimitry Andric 
1596fe6060f1SDimitry Andric       // Otherwise, it is a branch on a symbolic value which is currently
1597fe6060f1SDimitry Andric       // considered to be undef.  Make sure some edge is executable, so a
1598fe6060f1SDimitry Andric       // branch on "undef" always flows somewhere.
1599fe6060f1SDimitry Andric       // FIXME: Distinguish between dead code and an LLVM "undef" value.
1600fe6060f1SDimitry Andric       BasicBlock *DefaultSuccessor = SI->case_begin()->getCaseSuccessor();
1601fe6060f1SDimitry Andric       if (markEdgeExecutable(&BB, DefaultSuccessor))
1602fe6060f1SDimitry Andric         MadeChange = true;
1603fe6060f1SDimitry Andric 
1604fe6060f1SDimitry Andric       continue;
1605fe6060f1SDimitry Andric     }
1606fe6060f1SDimitry Andric   }
1607fe6060f1SDimitry Andric 
1608fe6060f1SDimitry Andric   return MadeChange;
1609fe6060f1SDimitry Andric }
1610fe6060f1SDimitry Andric 
1611fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
1612fe6060f1SDimitry Andric //
1613fe6060f1SDimitry Andric // SCCPSolver implementations
1614fe6060f1SDimitry Andric //
1615fe6060f1SDimitry Andric SCCPSolver::SCCPSolver(
1616fe6060f1SDimitry Andric     const DataLayout &DL,
1617fe6060f1SDimitry Andric     std::function<const TargetLibraryInfo &(Function &)> GetTLI,
1618fe6060f1SDimitry Andric     LLVMContext &Ctx)
1619fe6060f1SDimitry Andric     : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {}
1620fe6060f1SDimitry Andric 
1621fe6060f1SDimitry Andric SCCPSolver::~SCCPSolver() {}
1622fe6060f1SDimitry Andric 
1623fe6060f1SDimitry Andric void SCCPSolver::addAnalysis(Function &F, AnalysisResultsForFn A) {
1624fe6060f1SDimitry Andric   return Visitor->addAnalysis(F, std::move(A));
1625fe6060f1SDimitry Andric }
1626fe6060f1SDimitry Andric 
1627fe6060f1SDimitry Andric bool SCCPSolver::markBlockExecutable(BasicBlock *BB) {
1628fe6060f1SDimitry Andric   return Visitor->markBlockExecutable(BB);
1629fe6060f1SDimitry Andric }
1630fe6060f1SDimitry Andric 
1631fe6060f1SDimitry Andric const PredicateBase *SCCPSolver::getPredicateInfoFor(Instruction *I) {
1632fe6060f1SDimitry Andric   return Visitor->getPredicateInfoFor(I);
1633fe6060f1SDimitry Andric }
1634fe6060f1SDimitry Andric 
1635fe6060f1SDimitry Andric DomTreeUpdater SCCPSolver::getDTU(Function &F) { return Visitor->getDTU(F); }
1636fe6060f1SDimitry Andric 
1637fe6060f1SDimitry Andric void SCCPSolver::trackValueOfGlobalVariable(GlobalVariable *GV) {
1638fe6060f1SDimitry Andric   Visitor->trackValueOfGlobalVariable(GV);
1639fe6060f1SDimitry Andric }
1640fe6060f1SDimitry Andric 
1641fe6060f1SDimitry Andric void SCCPSolver::addTrackedFunction(Function *F) {
1642fe6060f1SDimitry Andric   Visitor->addTrackedFunction(F);
1643fe6060f1SDimitry Andric }
1644fe6060f1SDimitry Andric 
1645fe6060f1SDimitry Andric void SCCPSolver::addToMustPreserveReturnsInFunctions(Function *F) {
1646fe6060f1SDimitry Andric   Visitor->addToMustPreserveReturnsInFunctions(F);
1647fe6060f1SDimitry Andric }
1648fe6060f1SDimitry Andric 
1649fe6060f1SDimitry Andric bool SCCPSolver::mustPreserveReturn(Function *F) {
1650fe6060f1SDimitry Andric   return Visitor->mustPreserveReturn(F);
1651fe6060f1SDimitry Andric }
1652fe6060f1SDimitry Andric 
1653fe6060f1SDimitry Andric void SCCPSolver::addArgumentTrackedFunction(Function *F) {
1654fe6060f1SDimitry Andric   Visitor->addArgumentTrackedFunction(F);
1655fe6060f1SDimitry Andric }
1656fe6060f1SDimitry Andric 
1657fe6060f1SDimitry Andric bool SCCPSolver::isArgumentTrackedFunction(Function *F) {
1658fe6060f1SDimitry Andric   return Visitor->isArgumentTrackedFunction(F);
1659fe6060f1SDimitry Andric }
1660fe6060f1SDimitry Andric 
1661fe6060f1SDimitry Andric void SCCPSolver::solve() { Visitor->solve(); }
1662fe6060f1SDimitry Andric 
1663fe6060f1SDimitry Andric bool SCCPSolver::resolvedUndefsIn(Function &F) {
1664fe6060f1SDimitry Andric   return Visitor->resolvedUndefsIn(F);
1665fe6060f1SDimitry Andric }
1666fe6060f1SDimitry Andric 
1667fe6060f1SDimitry Andric bool SCCPSolver::isBlockExecutable(BasicBlock *BB) const {
1668fe6060f1SDimitry Andric   return Visitor->isBlockExecutable(BB);
1669fe6060f1SDimitry Andric }
1670fe6060f1SDimitry Andric 
1671fe6060f1SDimitry Andric bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
1672fe6060f1SDimitry Andric   return Visitor->isEdgeFeasible(From, To);
1673fe6060f1SDimitry Andric }
1674fe6060f1SDimitry Andric 
1675fe6060f1SDimitry Andric std::vector<ValueLatticeElement>
1676fe6060f1SDimitry Andric SCCPSolver::getStructLatticeValueFor(Value *V) const {
1677fe6060f1SDimitry Andric   return Visitor->getStructLatticeValueFor(V);
1678fe6060f1SDimitry Andric }
1679fe6060f1SDimitry Andric 
1680fe6060f1SDimitry Andric void SCCPSolver::removeLatticeValueFor(Value *V) {
1681fe6060f1SDimitry Andric   return Visitor->removeLatticeValueFor(V);
1682fe6060f1SDimitry Andric }
1683fe6060f1SDimitry Andric 
1684fe6060f1SDimitry Andric const ValueLatticeElement &SCCPSolver::getLatticeValueFor(Value *V) const {
1685fe6060f1SDimitry Andric   return Visitor->getLatticeValueFor(V);
1686fe6060f1SDimitry Andric }
1687fe6060f1SDimitry Andric 
1688fe6060f1SDimitry Andric const MapVector<Function *, ValueLatticeElement> &
1689fe6060f1SDimitry Andric SCCPSolver::getTrackedRetVals() {
1690fe6060f1SDimitry Andric   return Visitor->getTrackedRetVals();
1691fe6060f1SDimitry Andric }
1692fe6060f1SDimitry Andric 
1693fe6060f1SDimitry Andric const DenseMap<GlobalVariable *, ValueLatticeElement> &
1694fe6060f1SDimitry Andric SCCPSolver::getTrackedGlobals() {
1695fe6060f1SDimitry Andric   return Visitor->getTrackedGlobals();
1696fe6060f1SDimitry Andric }
1697fe6060f1SDimitry Andric 
1698fe6060f1SDimitry Andric const SmallPtrSet<Function *, 16> SCCPSolver::getMRVFunctionsTracked() {
1699fe6060f1SDimitry Andric   return Visitor->getMRVFunctionsTracked();
1700fe6060f1SDimitry Andric }
1701fe6060f1SDimitry Andric 
1702fe6060f1SDimitry Andric void SCCPSolver::markOverdefined(Value *V) { Visitor->markOverdefined(V); }
1703fe6060f1SDimitry Andric 
1704fe6060f1SDimitry Andric bool SCCPSolver::isStructLatticeConstant(Function *F, StructType *STy) {
1705fe6060f1SDimitry Andric   return Visitor->isStructLatticeConstant(F, STy);
1706fe6060f1SDimitry Andric }
1707fe6060f1SDimitry Andric 
1708fe6060f1SDimitry Andric Constant *SCCPSolver::getConstant(const ValueLatticeElement &LV) const {
1709fe6060f1SDimitry Andric   return Visitor->getConstant(LV);
1710fe6060f1SDimitry Andric }
1711fe6060f1SDimitry Andric 
1712fe6060f1SDimitry Andric SmallPtrSetImpl<Function *> &SCCPSolver::getArgumentTrackedFunctions() {
1713fe6060f1SDimitry Andric   return Visitor->getArgumentTrackedFunctions();
1714fe6060f1SDimitry Andric }
1715fe6060f1SDimitry Andric 
1716fe6060f1SDimitry Andric void SCCPSolver::markArgInFuncSpecialization(Function *F, Argument *A,
1717fe6060f1SDimitry Andric                                              Constant *C) {
1718fe6060f1SDimitry Andric   Visitor->markArgInFuncSpecialization(F, A, C);
1719fe6060f1SDimitry Andric }
1720fe6060f1SDimitry Andric 
1721fe6060f1SDimitry Andric void SCCPSolver::markFunctionUnreachable(Function *F) {
1722fe6060f1SDimitry Andric   Visitor->markFunctionUnreachable(F);
1723fe6060f1SDimitry Andric }
1724fe6060f1SDimitry Andric 
1725fe6060f1SDimitry Andric void SCCPSolver::visit(Instruction *I) { Visitor->visit(I); }
1726fe6060f1SDimitry Andric 
1727fe6060f1SDimitry Andric void SCCPSolver::visitCall(CallInst &I) { Visitor->visitCall(I); }
1728