1 //===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass implements an _extremely_ simple interprocedural constant
11 // propagation pass. It could certainly be improved in many different ways,
12 // like using a worklist. This pass makes arguments dead, but does not remove
13 // them. The existing dead argument elimination pass should be run after this
14 // to clean up the mess.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Support/CallSite.h"
23 #include "Support/Statistic.h"
26 Statistic<> NumArgumentsProped("ipconstprop",
27 "Number of args turned into constants");
29 /// IPCP - The interprocedural constant propagation pass
31 struct IPCP : public Pass {
34 bool processFunction(Function &F);
36 RegisterOpt<IPCP> X("ipconstprop", "Interprocedural constant propagation");
39 Pass *createIPConstantPropagationPass() { return new IPCP(); }
41 bool IPCP::run(Module &M) {
43 bool LocalChange = true;
45 // FIXME: instead of using smart algorithms, we just iterate until we stop
49 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
50 if (!I->isExternal() && I->hasInternalLinkage())
51 LocalChange |= processFunction(*I);
52 Changed |= LocalChange;
57 /// processFunction - Look at all uses of the specified function. If all uses
58 /// are direct call sites, and all pass a particular constant in for an
59 /// argument, propagate that constant in as the argument.
61 bool IPCP::processFunction(Function &F) {
62 if (F.aempty() || F.use_empty()) return false; // No arguments? Early exit.
64 std::vector<std::pair<Constant*, bool> > ArgumentConstants;
65 ArgumentConstants.resize(F.asize());
67 unsigned NumNonconstant = 0;
69 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
70 if (!isa<Instruction>(*I))
71 return false; // Used by a non-instruction, do not transform
73 CallSite CS = CallSite::get(cast<Instruction>(*I));
74 if (CS.getInstruction() == 0 ||
75 CS.getCalledFunction() != &F)
76 return false; // Not a direct call site?
78 // Check out all of the potentially constant arguments
79 CallSite::arg_iterator AI = CS.arg_begin();
80 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
81 if (*AI == &F) return false; // Passes the function into itself
83 if (!ArgumentConstants[i].second) {
84 if (isa<Constant>(*AI) || isa<GlobalValue>(*AI)) {
85 Constant *C = dyn_cast<Constant>(*AI);
86 if (!C) C = ConstantPointerRef::get(cast<GlobalValue>(*AI));
88 if (!ArgumentConstants[i].first)
89 ArgumentConstants[i].first = C;
90 else if (ArgumentConstants[i].first != C) {
91 // Became non-constant
92 ArgumentConstants[i].second = true;
94 if (NumNonconstant == ArgumentConstants.size()) return false;
97 // This is not a constant argument. Mark the argument as
99 ArgumentConstants[i].second = true;
101 if (NumNonconstant == ArgumentConstants.size()) return false;
107 // If we got to this point, there is a constant argument!
108 assert(NumNonconstant != ArgumentConstants.size());
109 Function::aiterator AI = F.abegin();
110 bool MadeChange = false;
111 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
112 // Do we have a constant argument!?
113 if (!ArgumentConstants[i].second && !AI->use_empty()) {
114 assert(ArgumentConstants[i].first && "Unknown constant value!");
115 Value *V = ArgumentConstants[i].first;
116 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
118 AI->replaceAllUsesWith(V);
119 ++NumArgumentsProped;