Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / Transforms / IPO / IPConstantPropagation.cpp
1 //===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17
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"
24
25 namespace llvm {
26
27 namespace {
28   Statistic<> NumArgumentsProped("ipconstprop",
29                                  "Number of args turned into constants");
30
31   /// IPCP - The interprocedural constant propagation pass
32   ///
33   struct IPCP : public Pass {
34     bool run(Module &M);
35   private:
36     bool processFunction(Function &F);
37   };
38   RegisterOpt<IPCP> X("ipconstprop", "Interprocedural constant propagation");
39 }
40
41 Pass *createIPConstantPropagationPass() { return new IPCP(); }
42
43 bool IPCP::run(Module &M) {
44   bool Changed = false;
45   bool LocalChange = true;
46
47   // FIXME: instead of using smart algorithms, we just iterate until we stop
48   // making changes.
49   while (LocalChange) {
50     LocalChange = false;
51     for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
52       if (!I->isExternal() && I->hasInternalLinkage())
53         LocalChange |= processFunction(*I);
54     Changed |= LocalChange;
55   }
56   return Changed;
57 }
58
59 /// processFunction - Look at all uses of the specified function.  If all uses
60 /// are direct call sites, and all pass a particular constant in for an
61 /// argument, propagate that constant in as the argument.
62 ///
63 bool IPCP::processFunction(Function &F) {
64   if (F.aempty() || F.use_empty()) return false;  // No arguments?  Early exit.
65
66   std::vector<std::pair<Constant*, bool> > ArgumentConstants;
67   ArgumentConstants.resize(F.asize());
68
69   unsigned NumNonconstant = 0;
70
71   for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
72     if (!isa<Instruction>(*I))
73       return false;  // Used by a non-instruction, do not transform
74     else {
75       CallSite CS = CallSite::get(cast<Instruction>(*I));
76       if (CS.getInstruction() == 0 || 
77           CS.getCalledFunction() != &F)
78         return false;  // Not a direct call site?
79       
80       // Check out all of the potentially constant arguments
81       CallSite::arg_iterator AI = CS.arg_begin();
82       for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
83         if (*AI == &F) return false;  // Passes the function into itself
84
85         if (!ArgumentConstants[i].second) {
86           if (isa<Constant>(*AI) || isa<GlobalValue>(*AI)) {
87             Constant *C = dyn_cast<Constant>(*AI);
88             if (!C) C = ConstantPointerRef::get(cast<GlobalValue>(*AI));
89             
90             if (!ArgumentConstants[i].first)
91               ArgumentConstants[i].first = C;
92             else if (ArgumentConstants[i].first != C) {
93               // Became non-constant
94               ArgumentConstants[i].second = true;
95               ++NumNonconstant;
96               if (NumNonconstant == ArgumentConstants.size()) return false;
97             }
98           } else {
99             // This is not a constant argument.  Mark the argument as
100             // non-constant.
101             ArgumentConstants[i].second = true;
102             ++NumNonconstant;
103             if (NumNonconstant == ArgumentConstants.size()) return false;
104           }
105         }
106       }
107     }
108
109   // If we got to this point, there is a constant argument!
110   assert(NumNonconstant != ArgumentConstants.size());
111   Function::aiterator AI = F.abegin();
112   bool MadeChange = false;
113   for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
114     // Do we have a constant argument!?
115     if (!ArgumentConstants[i].second && !AI->use_empty()) {
116       assert(ArgumentConstants[i].first && "Unknown constant value!");
117       Value *V = ArgumentConstants[i].first;
118       if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
119         V = CPR->getValue();
120       AI->replaceAllUsesWith(V);
121       ++NumArgumentsProped;
122       MadeChange = true;
123     }
124   return MadeChange;
125 }
126
127 } // End llvm namespace