blob: b592138b088155550cf13ab5d0ecac2e4339e96b [file] [log] [blame]
Chris Lattnerf516c692003-10-23 16:52:27 +00001//===-- 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
25namespace {
26 Statistic<> NumArgumentsProped("ipconstprop",
27 "Number of args turned into constants");
28
29 /// IPCP - The interprocedural constant propagation pass
30 ///
31 struct IPCP : public Pass {
32 bool run(Module &M);
33 private:
34 bool processFunction(Function &F);
35 };
36 RegisterOpt<IPCP> X("ipconstprop", "Interprocedural constant propagation");
37}
38
39Pass *createIPConstantPropagationPass() { return new IPCP(); }
40
41bool IPCP::run(Module &M) {
42 bool Changed = false;
Chris Lattner5e004e82003-10-27 21:09:00 +000043 bool LocalChange = true;
44
45 // FIXME: instead of using smart algorithms, we just iterate until we stop
46 // making changes.
47 while (LocalChange) {
48 LocalChange = false;
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;
53 }
Chris Lattnerf516c692003-10-23 16:52:27 +000054 return Changed;
55}
56
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.
60///
61bool IPCP::processFunction(Function &F) {
62 if (F.aempty() || F.use_empty()) return false; // No arguments? Early exit.
63
64 std::vector<std::pair<Constant*, bool> > ArgumentConstants;
65 ArgumentConstants.resize(F.asize());
66
67 unsigned NumNonconstant = 0;
68
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
72 else {
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?
77
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
82
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));
87
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;
93 ++NumNonconstant;
94 if (NumNonconstant == ArgumentConstants.size()) return false;
95 }
96 } else {
97 // This is not a constant argument. Mark the argument as
98 // non-constant.
99 ArgumentConstants[i].second = true;
100 ++NumNonconstant;
101 if (NumNonconstant == ArgumentConstants.size()) return false;
102 }
103 }
104 }
105 }
106
107 // If we got to this point, there is a constant argument!
108 assert(NumNonconstant != ArgumentConstants.size());
109 Function::aiterator AI = F.abegin();
Chris Lattner5e004e82003-10-27 21:09:00 +0000110 bool MadeChange = false;
Chris Lattnerf516c692003-10-23 16:52:27 +0000111 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
112 // Do we have a constant argument!?
Chris Lattner5e004e82003-10-27 21:09:00 +0000113 if (!ArgumentConstants[i].second && !AI->use_empty()) {
Chris Lattnerf516c692003-10-23 16:52:27 +0000114 assert(ArgumentConstants[i].first && "Unknown constant value!");
Chris Lattner12b78db2003-10-23 18:49:23 +0000115 Value *V = ArgumentConstants[i].first;
116 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
117 V = CPR->getValue();
118 AI->replaceAllUsesWith(V);
Chris Lattnerf516c692003-10-23 16:52:27 +0000119 ++NumArgumentsProped;
Chris Lattner5e004e82003-10-27 21:09:00 +0000120 MadeChange = true;
Chris Lattnerf516c692003-10-23 16:52:27 +0000121 }
Chris Lattner5e004e82003-10-27 21:09:00 +0000122 return MadeChange;
Chris Lattnerf516c692003-10-23 16:52:27 +0000123}