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;
45 bool PropagateConstantsIntoArguments(
Function &
F);
46 bool PropagateConstantReturn(
Function &
F);
52 "Interprocedural constant propagation",
false,
false)
56 bool IPCP::runOnModule(
Module &M) {
58 bool LocalChange =
true;
65 if (!
I->isDeclaration()) {
67 I->removeDeadConstantUsers();
68 if (
I->hasLocalLinkage())
69 LocalChange |= PropagateConstantsIntoArguments(*
I);
70 Changed |= PropagateConstantReturn(*
I);
72 Changed |= LocalChange;
81 bool IPCP::PropagateConstantsIntoArguments(
Function &
F) {
89 unsigned NumNonconstant = 0;
91 User *UR = U.getUser();
93 if (isa<BlockAddress>(UR))
continue;
97 if (!isa<CallInst>(UR) && !isa<InvokeInst>(UR))
101 if (!
CS.isCallee(&U))
108 for (
unsigned i = 0, e = ArgumentConstants.
size(); i != e;
112 if (ArgumentConstants[i].second)
116 if (C && ArgumentConstants[i].first ==
nullptr) {
117 ArgumentConstants[i].first = C;
118 }
else if (C && ArgumentConstants[i].first == C) {
120 }
else if (*AI == &*Arg) {
125 if (++NumNonconstant == ArgumentConstants.
size())
127 ArgumentConstants[i].second =
true;
133 assert(NumNonconstant != ArgumentConstants.
size());
134 bool MadeChange =
false;
136 for (
unsigned i = 0, e = ArgumentConstants.
size(); i != e; ++i, ++AI) {
138 if (ArgumentConstants[i].second || AI->use_empty() ||
142 Value *V = ArgumentConstants[i].first;
144 AI->replaceAllUsesWith(V);
145 ++NumArgumentsProped;
160 bool IPCP::PropagateConstantReturn(
Function &F) {
178 unsigned NumNonConstant = 0;
180 if (
ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
181 for (
unsigned i = 0, e = RetVals.
size(); i != e; ++i) {
183 Value *RV = RetVals[i];
190 V = RI->getOperand(0);
196 if (isa<UndefValue>(V))
200 if (isa<Constant>(V) || isa<Argument>(V)) {
201 if (isa<UndefValue>(RV)) {
213 RetVals[i] =
nullptr;
215 if (++NumNonConstant == RetVals.
size())
223 bool MadeChange =
false;
230 if (!Call || !
CS.isCallee(&U))
234 if (Call->use_empty())
240 Value* New = RetVals[0];
241 if (
Argument *
A = dyn_cast<Argument>(New))
244 New =
CS.getArgument(
A->getArgNo());
245 Call->replaceAllUsesWith(New);
249 for (
auto I = Call->user_begin(), E = Call->user_end();
I != E;) {
258 if (EV->hasIndices())
259 index = *EV->idx_begin();
264 Value *New = RetVals[index];
266 if (
Argument *
A = dyn_cast<Argument>(New))
269 New =
CS.getArgument(
A->getArgNo());
277 if (MadeChange) ++NumReturnValProped;
ReturnInst - Return a value (possibly void), from a function.
void initializeIPCPPass(PassRegistry &)
iplist< Instruction >::iterator eraseFromParent()
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.
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
User::op_iterator arg_iterator
arg_iterator - The type of iterator to use when looping over actual arguments at this call site...
StructType - 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 bool mayBeOverridden(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time...
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...
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, Instruction *InsertBefore=nullptr)
FindInsertedValue - Given an aggregrate and an sequence of indices, see if the scalar value indexed i...
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.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM Value Representation.
C - The default llvm calling convention, compatible with C.
unsigned getNumElements() const
Random access to the elements.
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.