blob: 36ac2023c1d7cac515c859e86cf631c5adc7dc18 [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"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattnerf516c692003-10-23 16:52:27 +000026namespace {
27 Statistic<> NumArgumentsProped("ipconstprop",
28 "Number of args turned into constants");
29
30 /// IPCP - The interprocedural constant propagation pass
31 ///
32 struct IPCP : public Pass {
33 bool run(Module &M);
34 private:
35 bool processFunction(Function &F);
36 };
37 RegisterOpt<IPCP> X("ipconstprop", "Interprocedural constant propagation");
38}
39
Chris Lattnerf52e03c2003-11-21 21:54:22 +000040Pass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
Chris Lattnerf516c692003-10-23 16:52:27 +000041
42bool IPCP::run(Module &M) {
43 bool Changed = false;
Chris Lattner5e004e82003-10-27 21:09:00 +000044 bool LocalChange = true;
45
46 // FIXME: instead of using smart algorithms, we just iterate until we stop
47 // making changes.
48 while (LocalChange) {
49 LocalChange = false;
50 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
51 if (!I->isExternal() && I->hasInternalLinkage())
52 LocalChange |= processFunction(*I);
53 Changed |= LocalChange;
54 }
Chris Lattnerf516c692003-10-23 16:52:27 +000055 return Changed;
56}
57
58/// processFunction - Look at all uses of the specified function. If all uses
59/// are direct call sites, and all pass a particular constant in for an
60/// argument, propagate that constant in as the argument.
61///
62bool IPCP::processFunction(Function &F) {
63 if (F.aempty() || F.use_empty()) return false; // No arguments? Early exit.
64
65 std::vector<std::pair<Constant*, bool> > ArgumentConstants;
66 ArgumentConstants.resize(F.asize());
67
68 unsigned NumNonconstant = 0;
69
70 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
71 if (!isa<Instruction>(*I))
72 return false; // Used by a non-instruction, do not transform
73 else {
74 CallSite CS = CallSite::get(cast<Instruction>(*I));
75 if (CS.getInstruction() == 0 ||
76 CS.getCalledFunction() != &F)
77 return false; // Not a direct call site?
78
79 // Check out all of the potentially constant arguments
80 CallSite::arg_iterator AI = CS.arg_begin();
81 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
82 if (*AI == &F) return false; // Passes the function into itself
83
84 if (!ArgumentConstants[i].second) {
Reid Spencer199aeb72004-07-18 08:31:18 +000085 if (Constant *C = dyn_cast<Constant>(*AI)) {
Chris Lattnerf516c692003-10-23 16:52:27 +000086 if (!ArgumentConstants[i].first)
87 ArgumentConstants[i].first = C;
88 else if (ArgumentConstants[i].first != C) {
89 // Became non-constant
90 ArgumentConstants[i].second = true;
91 ++NumNonconstant;
92 if (NumNonconstant == ArgumentConstants.size()) return false;
93 }
94 } else {
95 // This is not a constant argument. Mark the argument as
96 // non-constant.
97 ArgumentConstants[i].second = true;
98 ++NumNonconstant;
99 if (NumNonconstant == ArgumentConstants.size()) return false;
100 }
101 }
102 }
103 }
104
105 // If we got to this point, there is a constant argument!
106 assert(NumNonconstant != ArgumentConstants.size());
107 Function::aiterator AI = F.abegin();
Chris Lattner5e004e82003-10-27 21:09:00 +0000108 bool MadeChange = false;
Chris Lattnerf516c692003-10-23 16:52:27 +0000109 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
110 // Do we have a constant argument!?
Chris Lattner5e004e82003-10-27 21:09:00 +0000111 if (!ArgumentConstants[i].second && !AI->use_empty()) {
Chris Lattnerf516c692003-10-23 16:52:27 +0000112 assert(ArgumentConstants[i].first && "Unknown constant value!");
Chris Lattner12b78db2003-10-23 18:49:23 +0000113 Value *V = ArgumentConstants[i].first;
Chris Lattner12b78db2003-10-23 18:49:23 +0000114 AI->replaceAllUsesWith(V);
Chris Lattnerf516c692003-10-23 16:52:27 +0000115 ++NumArgumentsProped;
Chris Lattner5e004e82003-10-27 21:09:00 +0000116 MadeChange = true;
Chris Lattnerf516c692003-10-23 16:52:27 +0000117 }
Chris Lattner5e004e82003-10-27 21:09:00 +0000118 return MadeChange;
Chris Lattnerf516c692003-10-23 16:52:27 +0000119}
Brian Gaeke960707c2003-11-11 22:41:34 +0000120