18 #define DEBUG_TYPE "ipconstprop"
30 STATISTIC(NumArgumentsProped,
"Number of args turned into constants");
31 STATISTIC(NumReturnValProped,
"Number of return values turned into constants");
42 bool runOnModule(
Module &M);
44 bool PropagateConstantsIntoArguments(
Function &
F);
45 bool PropagateConstantReturn(
Function &
F);
51 "Interprocedural constant propagation",
false,
false)
55 bool IPCP::runOnModule(
Module &M) {
57 bool LocalChange =
true;
64 if (!
I->isDeclaration()) {
66 I->removeDeadConstantUsers();
67 if (
I->hasLocalLinkage())
68 LocalChange |= PropagateConstantsIntoArguments(*
I);
69 Changed |= PropagateConstantReturn(*
I);
71 Changed |= LocalChange;
80 bool IPCP::PropagateConstantsIntoArguments(
Function &
F) {
88 unsigned NumNonconstant = 0;
92 if (isa<BlockAddress>(U))
continue;
96 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
100 if (!CS.isCallee(UI))
107 for (
unsigned i = 0, e = ArgumentConstants.
size(); i != e;
111 if (ArgumentConstants[i].second)
115 if (C && ArgumentConstants[i].first == 0) {
116 ArgumentConstants[i].first =
C;
117 }
else if (C && ArgumentConstants[i].first == C) {
119 }
else if (*AI == &*Arg) {
124 if (++NumNonconstant == ArgumentConstants.
size())
126 ArgumentConstants[i].second =
true;
132 assert(NumNonconstant != ArgumentConstants.
size());
133 bool MadeChange =
false;
135 for (
unsigned i = 0, e = ArgumentConstants.
size(); i != e; ++i, ++AI) {
137 if (ArgumentConstants[i].second || AI->use_empty() ||
141 Value *V = ArgumentConstants[i].first;
143 AI->replaceAllUsesWith(V);
144 ++NumArgumentsProped;
159 bool IPCP::PropagateConstantReturn(
Function &F) {
177 unsigned NumNonConstant = 0;
179 if (
ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
180 for (
unsigned i = 0, e = RetVals.
size(); i != e; ++i) {
182 Value *RV = RetVals[i];
189 V = RI->getOperand(0);
195 if (isa<UndefValue>(V))
199 if (isa<Constant>(V) || isa<Argument>(V)) {
200 if (isa<UndefValue>(RV)) {
214 if (++NumNonConstant == RetVals.
size())
222 bool MadeChange =
false;
229 if (!Call || !CS.isCallee(UI))
239 Value* New = RetVals[0];
240 if (
Argument *
A = dyn_cast<Argument>(New))
243 New = CS.getArgument(
A->getArgNo());
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;
void initializeIPCPPass(PassRegistry &)
void push_back(const T &Elt)
ModulePass * createIPConstantPropagationPass()
static PassRegistry * getPassRegistry()
LLVM Argument representation.
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
The main container class for the LLVM Intermediate Representation.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Type * getReturnType() const
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, Instruction *InsertBefore=0)
ID
LLVM Calling Convention Representation.
void replaceAllUsesWith(Value *V)
User::op_iterator arg_iterator
STATISTIC(NumArgumentsProped,"Number of args turned into constants")
static bool mayBeOverridden(LinkageTypes Linkage)
Type * getElementType(unsigned N) const
LLVM Constant Representation.
static UndefValue * get(Type *T)
LLVM Value Representation.
unsigned getNumElements() const
Random access to the elements.
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.