29 #define DEBUG_TYPE "called-value-propagation" 40 cl::desc(
"The maximum number of functions to track per lattice value"));
61 enum CVPLatticeStateTy { Undefined, FunctionSet, Overdefined, Untracked };
71 CVPLatticeVal() : LatticeState(Undefined) {}
72 CVPLatticeVal(CVPLatticeStateTy LatticeState) : LatticeState(LatticeState) {}
73 CVPLatticeVal(std::vector<Function *> &&Functions)
74 : LatticeState(FunctionSet), Functions(std::move(Functions)) {
75 assert(std::is_sorted(this->Functions.begin(), this->Functions.end(),
81 const std::vector<Function *> &getFunctions()
const {
86 bool isFunctionSet()
const {
return LatticeState == FunctionSet; }
88 bool operator==(
const CVPLatticeVal &RHS)
const {
89 return LatticeState == RHS.LatticeState && Functions == RHS.Functions;
92 bool operator!=(
const CVPLatticeVal &RHS)
const {
93 return LatticeState != RHS.LatticeState || Functions != RHS.Functions;
98 CVPLatticeStateTy LatticeState;
107 std::vector<Function *> Functions;
120 CVPLatticeVal(CVPLatticeVal::Overdefined),
121 CVPLatticeVal(CVPLatticeVal::Untracked)) {}
124 CVPLatticeVal ComputeLatticeVal(CVPLatticeKey
Key)
override {
125 switch (Key.getInt()) {
127 if (isa<Instruction>(Key.getPointer())) {
128 return getUndefVal();
129 }
else if (
auto *A = dyn_cast<Argument>(Key.getPointer())) {
131 return getUndefVal();
132 }
else if (
auto *
C = dyn_cast<Constant>(Key.getPointer())) {
133 return computeConstant(
C);
135 return getOverdefinedVal();
136 case IPOGrouping::Memory:
138 if (
auto *GV = dyn_cast<GlobalVariable>(Key.getPointer())) {
140 return computeConstant(GV->getInitializer());
141 }
else if (
auto *
F = cast<Function>(Key.getPointer()))
143 return getUndefVal();
145 return getOverdefinedVal();
153 CVPLatticeVal MergeValues(CVPLatticeVal
X, CVPLatticeVal
Y)
override {
154 if (X == getOverdefinedVal() || Y == getOverdefinedVal())
155 return getOverdefinedVal();
156 if (X == getUndefVal() && Y == getUndefVal())
157 return getUndefVal();
158 std::vector<Function *> Union;
160 Y.getFunctions().begin(), Y.getFunctions().end(),
163 return getOverdefinedVal();
164 return CVPLatticeVal(std::move(Union));
171 void ComputeInstructionState(
176 return visitCallSite(cast<CallInst>(&I), ChangedValues, SS);
177 case Instruction::Invoke:
178 return visitCallSite(cast<InvokeInst>(&I), ChangedValues, SS);
180 return visitLoad(*cast<LoadInst>(&I), ChangedValues, SS);
182 return visitReturn(*cast<ReturnInst>(&I), ChangedValues, SS);
184 return visitSelect(*cast<SelectInst>(&I), ChangedValues, SS);
186 return visitStore(*cast<StoreInst>(&I), ChangedValues, SS);
188 return visitInst(I, ChangedValues, SS);
193 void PrintLatticeVal(CVPLatticeVal LV,
raw_ostream &OS)
override {
194 if (LV == getUndefVal())
196 else if (LV == getOverdefinedVal())
198 else if (LV == getUntrackedVal())
205 void PrintLatticeKey(CVPLatticeKey Key,
raw_ostream &OS)
override {
208 else if (Key.getInt() == IPOGrouping::Memory)
212 if (isa<Function>(Key.getPointer()))
213 OS << Key.getPointer()->getName();
215 OS << *Key.getPointer();
232 CVPLatticeVal computeConstant(
Constant *
C) {
233 if (isa<ConstantPointerNull>(C))
234 return CVPLatticeVal(CVPLatticeVal::FunctionSet);
236 return CVPLatticeVal({
F});
237 return getOverdefinedVal();
250 ChangedValues[RetF] =
276 ChangedValues[RegI] = getOverdefinedVal();
288 ChangedValues[RegFormal] =
297 ChangedValues[RegI] =
309 ChangedValues[RegI] =
321 auto MemGV = CVPLatticeKey(GV, IPOGrouping::Memory);
322 ChangedValues[RegI] =
325 ChangedValues[RegI] = getOverdefinedVal();
339 auto MemGV = CVPLatticeKey(GV, IPOGrouping::Memory);
340 ChangedValues[MemGV] =
353 ChangedValues[RegI] = getOverdefinedVal();
364 return Key.getPointer();
374 CVPLatticeFunc Lattice;
389 bool Changed =
false;
395 if (!LV.isFunctionSet() || LV.getFunctions().empty())
397 MDNode *Callees = MDB.createCallees(LV.getFunctions());
412 class CalledValuePropagationLegacyPass :
public ModulePass {
420 CalledValuePropagationLegacyPass() :
ModulePass(ID) {
425 bool runOnModule(
Module &M)
override {
435 "Called Value Propagation",
false,
false)
438 return new CalledValuePropagationLegacyPass();
Return a value (possibly void), from a function.
Value * getValueOperand()
void initializeCalledValuePropagationLegacyPassPass(PassRegistry &)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents an incoming formal argument to a Function.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
bool canTrackArgumentsInterprocedurally(Function *F)
Determine if the values of the given function's arguments can be tracked interprocedurally.
This class provides various memory handling functions that manipulate MemoryBlock instances...
const Value * getTrueValue() const
An instruction for reading from memory.
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it's an indirect...
IPOGrouping
To enable interprocedural analysis, we assign LLVM values to the following groups.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
This class represents the LLVM 'select' instruction.
ValTy * getCalledValue() const
Return the pointer to function that is being called.
static CVPLatticeKey getLatticeKeyFromValue(Value *V)
InstrTy * getInstruction() const
void Solve()
Solve - Solve for constants and executable blocks.
Type * getType() const
All values are typed, get the type of this value.
ModulePass * createCalledValuePropagationPass()
createCalledValuePropagationPass - Attach metadata to indirct call sites indicating the set of functi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
An instruction for storing to memory.
bool canTrackReturnsInterprocedurally(Function *F)
Determine if the values of the given function's returns can be tracked interprocedurally.
bool isVoidTy() const
Return true if this is 'void'.
initializer< Ty > init(const Ty &Val)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Type * getReturnType() const
Returns the type of the ret val.
A set of analyses that are preserved following a run of a transformation pass.
PointerIntPair - This class implements a pair of a pointer and small integer.
static bool runCVP(Module &M)
This is an important base class in LLVM.
ValTy * getArgument(unsigned ArgNo) const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
Value * getPointerOperand()
static Value * getValueFromLatticeKey(CVPLatticeKey Key)
SparseSolver - This class is a general purpose solver for Sparse Conditional Propagation with a progr...
const Constant * stripPointerCasts() const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
AbstractLatticeFunction - This class is implemented by the dataflow instance to specify what the latt...
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
LatticeVal getExistingValueState(LatticeKey Key) const
getExistingValueState - Return the LatticeVal object corresponding to the given value from the ValueS...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
static cl::opt< unsigned > MaxFunctionsPerValue("cvp-max-functions-per-value", cl::Hidden, cl::init(4), cl::desc("The maximum number of functions to track per lattice value"))
The maximum number of functions to track per lattice value.
bool canTrackGlobalVariableInterprocedurally(GlobalVariable *GV)
Determine if the value maintained in the given global variable can be tracked interprocedurally.
Promote Memory to Register
LatticeVal getValueState(LatticeKey Key)
getValueState - Return the LatticeVal object corresponding to the given value from the ValueState map...
void setPreservesAll()
Set by analyses that do not transform their input at all.
const Value * getFalseValue() const
bool operator!=(uint64_t V1, const APInt &V2)
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const BasicBlock & front() const
void MarkBlockExecutable(BasicBlock *BB)
MarkBlockExecutable - This method can be used by clients to mark all of the blocks that are known to ...
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream...
A container for analyses that lazily runs them and caches their results.
bool operator==(uint64_t V1, const APInt &V2)
INITIALIZE_PASS(CalledValuePropagationLegacyPass, "called-value-propagation", "Called Value Propagation", false, false) ModulePass *llvm
Value * getPointerOperand()
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
iterator_range< arg_iterator > args()
const BasicBlock * getParent() const
A template for translating between LLVM Values and LatticeKeys.