LLVM 24.0.0git
SCCPSolver.h
Go to the documentation of this file.
1//===- SCCPSolver.h - SCCP Utility ----------------------------- *- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// \file
10// This file implements Sparse Conditional Constant Propagation (SCCP) utility.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
15#define LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
16
17#include "llvm/ADT/MapVector.h"
19#include "llvm/ADT/Statistic.h"
23#include <vector>
24
25namespace llvm {
26class Argument;
27class BasicBlock;
28class CallInst;
29class Constant;
30class DataLayout;
31class DominatorTree;
32class Function;
33class GlobalVariable;
34class Instruction;
35class LLVMContext;
36class StructType;
38class Value;
40
41/// Helper struct shared between Function Specialization and SCCP Solver.
42struct ArgInfo {
43 Argument *Formal; // The Formal argument being analysed.
44 Constant *Actual; // A corresponding actual constant argument.
45
47
48 bool operator==(const ArgInfo &Other) const {
49 return Formal == Other.Formal && Actual == Other.Actual;
50 }
51
52 bool operator!=(const ArgInfo &Other) const { return !(*this == Other); }
53
54 friend hash_code hash_value(const ArgInfo &A) {
55 return hash_combine(hash_value(A.Formal), hash_value(A.Actual));
56 }
57};
58
59class SCCPInstVisitor;
60
61//===----------------------------------------------------------------------===//
62//
63/// SCCPSolver - This interface class is a general purpose solver for Sparse
64/// Conditional Constant Propagation (SCCP).
65///
67 std::unique_ptr<SCCPInstVisitor> Visitor;
68
69public:
72 std::function<const TargetLibraryInfo &(Function &)> GetTLI,
73 LLVMContext &Ctx);
74
76
77 LLVM_ABI const DataLayout &getDataLayout() const;
78
80 AssumptionCache &AC);
81
83
84 /// markBlockExecutable - This method can be used by clients to mark all of
85 /// the blocks that are known to be intrinsically live in the processed unit.
86 /// This returns true if the block was not considered live before.
88
90
91 /// trackValueOfGlobalVariable - Clients can use this method to
92 /// inform the SCCPSolver that it should track loads and stores to the
93 /// specified global variable if it can. This is only legal to call if
94 /// performing Interprocedural SCCP.
96
97 /// addTrackedFunction - If the SCCP solver is supposed to track calls into
98 /// and out of the specified function (which cannot have its address taken),
99 /// this method must be called.
101
102 /// Add function to the list of functions whose return cannot be modified.
104
105 /// Returns true if the return of the given function cannot be modified.
107
109
110 /// Returns true if the given function is in the solver's set of
111 /// argument-tracked functions.
113
116
117 /// Solve - Solve for constants and executable blocks.
118 LLVM_ABI void solve();
119
120 /// resolvedUndefsIn - While solving the dataflow for a function, we assume
121 /// that branches on undef values cannot reach any of their successors.
122 /// However, this is not a safe assumption. After we solve dataflow, this
123 /// method should be use to handle this. If this returns true, the solver
124 /// should be rerun.
126
128
129 LLVM_ABI void
131
133
134 LLVM_ABI bool isBlockExecutable(BasicBlock *BB) const;
135
136 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
137 // block to the 'To' basic block is currently feasible.
138 LLVM_ABI bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
139
140 LLVM_ABI std::vector<ValueLatticeElement>
142
144
145 /// Invalidate the Lattice Value of \p Call and its users after specializing
146 /// the call. Then recompute it.
148
150
151 /// getTrackedRetVals - Get the inferred return value map.
153 getTrackedRetVals() const;
154
155 /// getTrackedGlobals - Get and return the set of inferred initializers for
156 /// global variables.
158 getTrackedGlobals() const;
159
160 /// getMRVFunctionsTracked - Get the set of functions which return multiple
161 /// values tracked by the pass.
163
164 /// markOverdefined - Mark the specified value overdefined. This
165 /// works with both scalars and structs.
167
168 /// trackValueOfArgument - Mark the specified argument overdefined unless it
169 /// have range attribute. This works with both scalars and structs.
171
172 // isStructLatticeConstant - Return true if all the lattice values
173 // corresponding to elements of the structure are constants,
174 // false otherwise.
176
177 /// Helper to return a Constant if \p LV is either a constant or a constant
178 /// range with a single element.
180
181 /// Return either a Constant or nullptr for a given Value.
183
184 /// Set the Lattice Value for the arguments of a specialization \p F.
185 /// If an argument is Constant then its lattice value is marked with the
186 /// corresponding actual argument in \p Args. Otherwise, its lattice value
187 /// is inherited (copied) from the corresponding formal argument in \p Args.
189 Function *F, const SmallVectorImpl<ArgInfo> &Args);
190
191 /// Mark all of the blocks in function \p F non-executable. Clients can used
192 /// this method to erase a function from the module (e.g., if it has been
193 /// completely specialized and is no longer needed).
195
198
200 SmallPtrSetImpl<Value *> &InsertedValues,
201 Statistic &InstRemovedStat,
202 Statistic &InstReplacedStat);
203
205 BasicBlock *&NewUnreachableBB) const;
206
207 LLVM_ABI void inferReturnAttributes() const;
208 LLVM_ABI void inferArgAttributes() const;
209
211
212 // Helper to check if \p LV is either a constant or a constant
213 // range with a single element. This should cover exactly the same cases as
214 // the old ValueLatticeElement::isConstant() and is intended to be used in the
215 // transition to ValueLatticeElement.
216 LLVM_ABI static bool isConstant(const ValueLatticeElement &LV);
217
218 // Helper to check if \p LV is either overdefined or a constant range with
219 // more than a single element. This should cover exactly the same cases as the
220 // old ValueLatticeElement::isOverdefined() and is intended to be used in the
221 // transition to ValueLatticeElement.
222 LLVM_ABI static bool isOverdefined(const ValueLatticeElement &LV);
223
224 // Helper to check if \p LV is a replaceable constant. A pointer constant with
225 // potentially different provenance may not be unconditionally propagated to
226 // all uses.
228};
229} // namespace llvm
230
231#endif // LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:215
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file implements a map that provides insertion order iteration.
This file implements the PredicateInfo analysis, which creates an Extended SSA form for operations us...
This file defines the SmallPtrSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
LLVM_ABI void visitCall(CallInst &I)
LLVM_ABI ~SCCPSolver()
LLVM_ABI void resetLatticeValueFor(CallBase *Call)
Invalidate the Lattice Value of Call and its users after specializing the call.
LLVM_ABI void trackValueOfGlobalVariable(GlobalVariable *GV)
trackValueOfGlobalVariable - Clients can use this method to inform the SCCPSolver that it should trac...
LLVM_ABI bool tryToReplaceWithConstant(Value *V)
LLVM_ABI void inferArgAttributes() const
LLVM_ABI bool isStructLatticeConstant(Function *F, StructType *STy)
LLVM_ABI void addPredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC)
LLVM_ABI void solve()
Solve - Solve for constants and executable blocks.
LLVM_ABI void visit(Instruction *I)
LLVM_ABI void trackValueOfArgument(Argument *V)
trackValueOfArgument - Mark the specified argument overdefined unless it have range attribute.
LLVM_ABI const DenseMap< GlobalVariable *, ValueLatticeElement > & getTrackedGlobals() const
getTrackedGlobals - Get and return the set of inferred initializers for global variables.
LLVM_ABI void addTrackedFunction(Function *F)
addTrackedFunction - If the SCCP solver is supposed to track calls into and out of the specified func...
LLVM_ABI void solveWhileResolvedUndefsIn(Module &M)
LLVM_ABI const PredicateBase * getPredicateInfoFor(Instruction *I)
LLVM_ABI const SmallPtrSetImpl< Function * > & getArgumentTrackedFunctions() const
LLVM_ABI const SmallPtrSet< Function *, 16 > & getMRVFunctionsTracked() const
getMRVFunctionsTracked - Get the set of functions which return multiple values tracked by the pass.
LLVM_ABI bool resolvedUndefsIn(Function &F)
resolvedUndefsIn - While solving the dataflow for a function, we assume that branches on undef values...
LLVM_ABI const DataLayout & getDataLayout() const
LLVM_ABI void addArgumentTrackedFunction(Function *F)
static LLVM_ABI bool isReplaceableConstant(const ValueLatticeElement &LV)
LLVM_ABI void solveWhileResolvedUndefs()
LLVM_ABI void removeLatticeValueFor(Value *V)
LLVM_ABI std::vector< ValueLatticeElement > getStructLatticeValueFor(Value *V) const
LLVM_ABI Constant * getConstantOrNull(Value *V) const
Return either a Constant or nullptr for a given Value.
LLVM_ABI bool simplifyInstsInBlock(BasicBlock &BB, SmallPtrSetImpl< Value * > &InsertedValues, Statistic &InstRemovedStat, Statistic &InstReplacedStat)
LLVM_ABI Constant * getConstant(const ValueLatticeElement &LV, Type *Ty) const
Helper to return a Constant if LV is either a constant or a constant range with a single element.
LLVM_ABI const ValueLatticeElement & getLatticeValueFor(Value *V) const
LLVM_ABI void addToMustPreserveReturnsInFunctions(Function *F)
Add function to the list of functions whose return cannot be modified.
LLVM_ABI bool removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU, BasicBlock *&NewUnreachableBB) const
LLVM_ABI bool isBlockExecutable(BasicBlock *BB) const
LLVM_ABI void inferReturnAttributes() const
LLVM_ABI bool markBlockExecutable(BasicBlock *BB)
markBlockExecutable - This method can be used by clients to mark all of the blocks that are known to ...
LLVM_ABI void setLatticeValueForSpecializationArguments(Function *F, const SmallVectorImpl< ArgInfo > &Args)
Set the Lattice Value for the arguments of a specialization F.
static LLVM_ABI bool isConstant(const ValueLatticeElement &LV)
LLVM_ABI const MapVector< Function *, ValueLatticeElement > & getTrackedRetVals() const
getTrackedRetVals - Get the inferred return value map.
LLVM_ABI bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const
LLVM_ABI bool mustPreserveReturn(Function *F)
Returns true if the return of the given function cannot be modified.
static LLVM_ABI bool isOverdefined(const ValueLatticeElement &LV)
LLVM_ABI void markFunctionUnreachable(Function *F)
Mark all of the blocks in function F non-executable.
LLVM_ABI bool isArgumentTrackedFunction(Function *F)
Returns true if the given function is in the solver's set of argument-tracked functions.
LLVM_ABI SCCPSolver(const DataLayout &DL, std::function< const TargetLibraryInfo &(Function &)> GetTLI, LLVMContext &Ctx)
LLVM_ABI void markOverdefined(Value *V)
markOverdefined - Mark the specified value overdefined.
LLVM_ABI void removeSSACopies(Function &F)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Class to represent struct types.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This class represents lattice values for constants.
LLVM Value Representation.
Definition Value.h:75
An opaque object representing a hash code.
Definition Hashing.h:77
CallInst * Call
This is an optimization pass for GlobalISel generic memory operations.
NoopStatistic Statistic
Definition Statistic.h:162
@ Other
Any other memory.
Definition ModRef.h:68
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:305
friend hash_code hash_value(const ArgInfo &A)
Definition SCCPSolver.h:54
Argument * Formal
Definition SCCPSolver.h:43
Constant * Actual
Definition SCCPSolver.h:44
ArgInfo(Argument *F, Constant *A)
Definition SCCPSolver.h:46
bool operator!=(const ArgInfo &Other) const
Definition SCCPSolver.h:52
bool operator==(const ArgInfo &Other) const
Definition SCCPSolver.h:48