31#define DEBUG_TYPE "called-value-propagation"
52 enum CVPLatticeStateTy { Undefined, FunctionSet, Overdefined, Untracked };
59 return FunctionIndices.
at(
LHS) < FunctionIndices.
at(
RHS);
63 CVPLatticeVal() =
default;
64 CVPLatticeVal(CVPLatticeStateTy LatticeState) : LatticeState(LatticeState) {}
65 CVPLatticeVal(std::vector<Function *> &&Functions,
67 : LatticeState(FunctionSet), Functions(std::move(Functions)) {
73 const std::vector<Function *> &getFunctions()
const {
78 bool isFunctionSet()
const {
return LatticeState == FunctionSet; }
81 return LatticeState ==
RHS.LatticeState && Functions ==
RHS.Functions;
85 return LatticeState !=
RHS.LatticeState || Functions !=
RHS.Functions;
90 CVPLatticeStateTy LatticeState = Undefined;
99 std::vector<Function *> Functions;
109 const IPOOptions &Opts;
113 explicit CVPLatticeFunc(
114 const IPOOptions &Opts,
117 CVPLatticeVal(CVPLatticeVal::Overdefined),
118 CVPLatticeVal(CVPLatticeVal::Untracked)),
119 Opts(Opts), FunctionIndices(FunctionIndices) {}
122 CVPLatticeVal ComputeLatticeVal(CVPLatticeKey
Key)
override {
123 switch (
Key.getInt()) {
124 case IPOGrouping::Register:
126 return getUndefVal();
129 return getUndefVal();
131 return computeConstant(
C);
133 return getOverdefinedVal();
134 case IPOGrouping::Memory:
135 case IPOGrouping::Return:
138 return computeConstant(GV->getInitializer());
141 return getUndefVal();
143 return getOverdefinedVal();
151 CVPLatticeVal MergeValues(CVPLatticeVal
X, CVPLatticeVal
Y)
override {
152 if (
X == getOverdefinedVal() ||
Y == getOverdefinedVal())
153 return getOverdefinedVal();
154 if (
X == getUndefVal() &&
Y == getUndefVal())
155 return getUndefVal();
156 std::vector<Function *> Union;
157 std::set_union(
X.getFunctions().begin(),
X.getFunctions().end(),
158 Y.getFunctions().begin(),
Y.getFunctions().end(),
159 std::back_inserter(Union),
160 CVPLatticeVal::Compare{FunctionIndices});
161 if (Union.size() > Opts.cvp_max_functions_per_value)
162 return getOverdefinedVal();
163 return CVPLatticeVal(std::move(Union), FunctionIndices);
170 void ComputeInstructionState(
174 switch (
I.getOpcode()) {
175 case Instruction::Call:
176 case Instruction::Invoke:
178 case Instruction::Load:
180 case Instruction::Ret:
182 case Instruction::Select:
184 case Instruction::Store:
187 return visitInst(
I, ChangedValues, SS);
192 void PrintLatticeVal(CVPLatticeVal LV,
raw_ostream &OS)
override {
193 if (LV == getUndefVal())
195 else if (LV == getOverdefinedVal())
197 else if (LV == getUntrackedVal())
204 void PrintLatticeKey(CVPLatticeKey
Key,
raw_ostream &OS)
override {
205 if (
Key.getInt() == IPOGrouping::Register)
207 else if (
Key.getInt() == IPOGrouping::Memory)
209 else if (
Key.getInt() == IPOGrouping::Return)
212 OS <<
Key.getPointer()->getName();
214 OS << *
Key.getPointer();
220 return IndirectCalls.getArrayRef();
233 CVPLatticeVal computeConstant(
Constant *
C) {
235 return CVPLatticeVal(CVPLatticeVal::FunctionSet);
237 return CVPLatticeVal({
F}, FunctionIndices);
238 return getOverdefinedVal();
248 if (
F->getReturnType()->isVoidTy())
250 auto RegI = CVPLatticeKey(
I.getReturnValue(), IPOGrouping::Register);
251 auto RetF = CVPLatticeKey(
F, IPOGrouping::Return);
252 ChangedValues[RetF] =
253 MergeValues(SS.getValueState(RegI), SS.getValueState(RetF));
265 auto RegI = CVPLatticeKey(&CB, IPOGrouping::Register);
270 IndirectCalls.
insert(&CB);
278 ChangedValues[RegI] = getOverdefinedVal();
284 SS.MarkBlockExecutable(&
F->front());
285 auto RetF = CVPLatticeKey(
F, IPOGrouping::Return);
287 auto RegFormal = CVPLatticeKey(&
A, IPOGrouping::Register);
289 CVPLatticeKey(CB.
getArgOperand(
A.getArgNo()), IPOGrouping::Register);
290 ChangedValues[RegFormal] =
291 MergeValues(SS.getValueState(RegFormal), SS.getValueState(RegActual));
299 ChangedValues[RegI] =
300 MergeValues(SS.getValueState(RegI), SS.getValueState(RetF));
309 auto RegI = CVPLatticeKey(&
I, IPOGrouping::Register);
310 auto RegT = CVPLatticeKey(
I.getTrueValue(), IPOGrouping::Register);
311 auto RegF = CVPLatticeKey(
I.getFalseValue(), IPOGrouping::Register);
312 ChangedValues[RegI] =
313 MergeValues(SS.getValueState(RegT), SS.getValueState(RegF));
323 auto RegI = CVPLatticeKey(&
I, IPOGrouping::Register);
325 auto MemGV = CVPLatticeKey(GV, IPOGrouping::Memory);
326 ChangedValues[RegI] =
327 MergeValues(SS.getValueState(RegI), SS.getValueState(MemGV));
329 ChangedValues[RegI] = getOverdefinedVal();
343 auto RegI = CVPLatticeKey(
I.getValueOperand(), IPOGrouping::Register);
344 auto MemGV = CVPLatticeKey(GV, IPOGrouping::Memory);
345 ChangedValues[MemGV] =
346 MergeValues(SS.getValueState(RegI), SS.getValueState(MemGV));
358 auto RegI = CVPLatticeKey(&
I, IPOGrouping::Register);
359 ChangedValues[RegI] = getOverdefinedVal();
370 return Key.getPointer();
373 return CVPLatticeKey(V, IPOGrouping::Register);
403 for (
CallBase *
C : Lattice.getIndirectCalls()) {
404 auto RegI = CVPLatticeKey(
C->getCalledOperand(), IPOGrouping::Register);
406 if (!LV.isFunctionSet() || LV.getFunctions().empty())
409 C->setMetadata(LLVMContext::MD_callees, Callees);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static bool runCVP(Module &M)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
This file implements a set that has insertion order iteration characteristics.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
AbstractLatticeFunction - This class is implemented by the dataflow instance to specify what the latt...
This class represents an incoming formal argument to a Function.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Value * getArgOperand(unsigned i) const
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
This is an important base class in LLVM.
ValueT & at(const_arg_type_t< KeyT > Val)
Return the entry for the specified key, or abort if no such entry exists.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
An instruction for reading from memory.
LLVM_ABI MDNode * createCallees(ArrayRef< Function * > Callees)
Return metadata indicating the possible callees of indirect calls.
A Module instance is used to store all the information related to an LLVM module.
PointerIntPair - This class implements a pair of a pointer and small integer.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
Return a value (possibly void), from a function.
This class represents the LLVM 'select' instruction.
bool insert(const value_type &X)
Insert a new element into the SetVector.
A SetVector that performs no allocations if smaller than a certain size.
SparseSolver - This class is a general purpose solver for Sparse Conditional Propagation with a progr...
void MarkBlockExecutable(BasicBlock *BB)
MarkBlockExecutable - This method can be used by clients to mark all of the blocks that are known to ...
void Solve()
Solve - Solve for constants and executable blocks.
LatticeVal getExistingValueState(LatticeKey Key) const
getExistingValueState - Return the LatticeVal object corresponding to the given value from the ValueS...
An instruction for storing to memory.
bool isVoidTy() const
Return true if this is 'void'.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
This class implements an extremely fast bulk output stream that can only output to a stream.
This class provides various memory handling functions that manipulate MemoryBlock instances.
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
bool operator!=(uint64_t V1, const APInt &V2)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
const IPOOptions & getIPOOptions(const LLVMContext &C)
LLVM_ABI bool canTrackGlobalVariableInterprocedurally(GlobalVariable *GV)
Determine if the value maintained in the given global variable can be tracked interprocedurally.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI bool canTrackReturnsInterprocedurally(Function *F)
Determine if the values of the given function's returns can be tracked interprocedurally.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI bool canTrackArgumentsInterprocedurally(Function *F)
Determine if the values of the given function's arguments can be tracked interprocedurally.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
static CVPLatticeKey getLatticeKeyFromValue(Value *V)
static Value * getValueFromLatticeKey(CVPLatticeKey Key)
A template for translating between LLVM Values and LatticeKeys.
A MapVector that performs no allocations if smaller than a certain size.