llvm.org GIT mirror llvm / d358e6f
Check in initial version of ipcp git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9423 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 15 years ago
1 changed file(s) with 110 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements an _extremely_ simple interprocedural constant
10 // propagation pass. It could certainly be improved in many different ways,
11 // like using a worklist. This pass makes arguments dead, but does not remove
12 // them. The existing dead argument elimination pass should be run after this
13 // to clean up the mess.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Support/CallSite.h"
22 #include "Support/Statistic.h"
23
24 namespace {
25 Statistic<> NumArgumentsProped("ipconstprop",
26 "Number of args turned into constants");
27
28 /// IPCP - The interprocedural constant propagation pass
29 ///
30 struct IPCP : public Pass {
31 bool run(Module &M);
32 private:
33 bool processFunction(Function &F);
34 };
35 RegisterOpt X("ipconstprop", "Interprocedural constant propagation");
36 }
37
38 Pass *createIPConstantPropagationPass() { return new IPCP(); }
39
40 bool IPCP::run(Module &M) {
41 bool Changed = false;
42 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
43 if (!I->isExternal() && I->hasInternalLinkage())
44 Changed |= processFunction(*I);
45 return Changed;
46 }
47
48 /// processFunction - Look at all uses of the specified function. If all uses
49 /// are direct call sites, and all pass a particular constant in for an
50 /// argument, propagate that constant in as the argument.
51 ///
52 bool IPCP::processFunction(Function &F) {
53 if (F.aempty() || F.use_empty()) return false; // No arguments? Early exit.
54
55 std::vector > ArgumentConstants;
56 ArgumentConstants.resize(F.asize());
57
58 unsigned NumNonconstant = 0;
59
60 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
61 if (!isa(*I))
62 return false; // Used by a non-instruction, do not transform
63 else {
64 CallSite CS = CallSite::get(cast(*I));
65 if (CS.getInstruction() == 0 ||
66 CS.getCalledFunction() != &F)
67 return false; // Not a direct call site?
68
69 // Check out all of the potentially constant arguments
70 CallSite::arg_iterator AI = CS.arg_begin();
71 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
72 if (*AI == &F) return false; // Passes the function into itself
73
74 if (!ArgumentConstants[i].second) {
75 if (isa(*AI) || isa(*AI)) {
76 Constant *C = dyn_cast(*AI);
77 if (!C) C = ConstantPointerRef::get(cast(*AI));
78
79 if (!ArgumentConstants[i].first)
80 ArgumentConstants[i].first = C;
81 else if (ArgumentConstants[i].first != C) {
82 // Became non-constant
83 ArgumentConstants[i].second = true;
84 ++NumNonconstant;
85 if (NumNonconstant == ArgumentConstants.size()) return false;
86 }
87 } else {
88 // This is not a constant argument. Mark the argument as
89 // non-constant.
90 ArgumentConstants[i].second = true;
91 ++NumNonconstant;
92 if (NumNonconstant == ArgumentConstants.size()) return false;
93 }
94 }
95 }
96 }
97
98 // If we got to this point, there is a constant argument!
99 assert(NumNonconstant != ArgumentConstants.size());
100 Function::aiterator AI = F.abegin();
101 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
102 // Do we have a constant argument!?
103 if (!ArgumentConstants[i].second) {
104 assert(ArgumentConstants[i].first && "Unknown constant value!");
105 AI->replaceAllUsesWith(ArgumentConstants[i].first);
106 ++NumArgumentsProped;
107 }
108 return true;
109 }