29 #define DEBUG_TYPE "ipconstprop"
31 STATISTIC(NumArgumentsProped,
"Number of args turned into constants");
32 STATISTIC(NumReturnValProped,
"Number of return values turned into constants");
43 bool runOnModule(
Module &M)
override;
59 unsigned NumNonconstant = 0;
61 User *UR = U.getUser();
63 if (isa<BlockAddress>(UR))
continue;
67 if (!isa<CallInst>(UR) && !isa<InvokeInst>(UR))
78 for (
unsigned i = 0, e = ArgumentConstants.
size();
i != e;
82 if (ArgumentConstants[
i].second)
86 if (C && ArgumentConstants[
i].first ==
nullptr) {
87 ArgumentConstants[
i].first =
C;
88 }
else if (C && ArgumentConstants[
i].first == C) {
90 }
else if (*AI == &*Arg) {
95 if (++NumNonconstant == ArgumentConstants.
size())
97 ArgumentConstants[
i].second =
true;
103 assert(NumNonconstant != ArgumentConstants.
size());
104 bool MadeChange =
false;
106 for (
unsigned i = 0, e = ArgumentConstants.
size();
i != e; ++
i, ++AI) {
108 if (ArgumentConstants[
i].second || AI->use_empty() ||
112 Value *V = ArgumentConstants[
i].first;
114 AI->replaceAllUsesWith(V);
115 ++NumArgumentsProped;
149 unsigned NumNonConstant = 0;
151 if (
ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) {
152 for (
unsigned i = 0, e = RetVals.
size();
i != e; ++
i) {
161 V = RI->getOperand(0);
167 if (isa<UndefValue>(V))
171 if (isa<Constant>(V) || isa<Argument>(V)) {
172 if (isa<UndefValue>(RV)) {
184 RetVals[
i] =
nullptr;
186 if (++NumNonConstant == RetVals.
size())
194 bool MadeChange =
false;
195 for (
Use &U : F.uses()) {
201 if (!Call || !CS.isCallee(&U))
205 if (Call->use_empty())
211 Value* New = RetVals[0];
212 if (
Argument *
A = dyn_cast<Argument>(New))
215 New = CS.getArgument(
A->getArgNo());
216 Call->replaceAllUsesWith(New);
220 for (
auto I = Call->user_begin(),
E = Call->user_end();
I !=
E;) {
229 if (EV->hasIndices())
230 index = *EV->idx_begin();
235 Value *New = RetVals[index];
237 if (
Argument *
A = dyn_cast<Argument>(New))
240 New = CS.getArgument(
A->getArgNo());
248 if (MadeChange) ++NumReturnValProped;
254 "Interprocedural constant propagation",
false,
false)
258 bool IPCP::runOnModule(
Module &M) {
262 bool Changed =
false;
263 bool LocalChange =
true;
267 while (LocalChange) {
270 if (!
F.isDeclaration()) {
272 F.removeDeadConstantUsers();
273 if (
F.hasLocalLinkage())
277 Changed |= LocalChange;
Return a value (possibly void), from a function.
void initializeIPCPPass(PassRegistry &)
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
ModulePass * createIPConstantPropagationPass()
createIPConstantPropagationPass - This pass propagates constants from call sites into the bodies of f...
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM Argument representation.
static bool PropagateConstantsIntoArguments(Function &F)
PropagateConstantsIntoArguments - Look at all uses of the specified function.
STATISTIC(NumFunctions,"Total number of functions")
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
INITIALIZE_PASS(IPCP,"ipconstprop","Interprocedural constant propagation", false, false) ModulePass *llvm
A Module instance is used to store all the information related to an LLVM module. ...
Type * getReturnType() const
Returns the type of the ret val.
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
LLVM Basic Block Representation.
Type * getElementType(unsigned N) const
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isDefinitionExact() const
Return true if the currently visible definition of this global (if any) is exactly the definition we ...
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
User::op_iterator arg_iterator
arg_iterator - The type of iterator to use when looping over actual arguments at this call site...
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, Instruction *InsertBefore=nullptr)
Given an aggregrate and an sequence of indices, see if the scalar value indexed is already around as ...
Iterator for intrusive lists based on ilist_node.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
static bool PropagateConstantReturn(Function &F)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
unsigned getNumElements() const
Random access to the elements.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
bool isCallee(Value::const_user_iterator UI) const
isCallee - Determine whether the passed iterator points to the callee operand's Use.
bool isVoidTy() const
Return true if this is 'void'.