2003-10-24 00:52:27 +08:00
|
|
|
//===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass implements an _extremely_ simple interprocedural constant
|
|
|
|
// propagation pass. It could certainly be improved in many different ways,
|
|
|
|
// like using a worklist. This pass makes arguments dead, but does not remove
|
|
|
|
// them. The existing dead argument elimination pass should be run after this
|
|
|
|
// to clean up the mess.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Support/CallSite.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2003-10-24 00:52:27 +08:00
|
|
|
namespace {
|
|
|
|
Statistic<> NumArgumentsProped("ipconstprop",
|
|
|
|
"Number of args turned into constants");
|
|
|
|
|
|
|
|
/// IPCP - The interprocedural constant propagation pass
|
|
|
|
///
|
|
|
|
struct IPCP : public Pass {
|
|
|
|
bool run(Module &M);
|
|
|
|
private:
|
|
|
|
bool processFunction(Function &F);
|
|
|
|
};
|
|
|
|
RegisterOpt<IPCP> X("ipconstprop", "Interprocedural constant propagation");
|
|
|
|
}
|
|
|
|
|
2003-11-22 05:54:22 +08:00
|
|
|
Pass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
|
2003-10-24 00:52:27 +08:00
|
|
|
|
|
|
|
bool IPCP::run(Module &M) {
|
|
|
|
bool Changed = false;
|
2003-10-28 05:09:00 +08:00
|
|
|
bool LocalChange = true;
|
|
|
|
|
|
|
|
// FIXME: instead of using smart algorithms, we just iterate until we stop
|
|
|
|
// making changes.
|
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
if (!I->isExternal() && I->hasInternalLinkage())
|
|
|
|
LocalChange |= processFunction(*I);
|
|
|
|
Changed |= LocalChange;
|
|
|
|
}
|
2003-10-24 00:52:27 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// processFunction - Look at all uses of the specified function. If all uses
|
|
|
|
/// are direct call sites, and all pass a particular constant in for an
|
|
|
|
/// argument, propagate that constant in as the argument.
|
|
|
|
///
|
|
|
|
bool IPCP::processFunction(Function &F) {
|
|
|
|
if (F.aempty() || F.use_empty()) return false; // No arguments? Early exit.
|
|
|
|
|
|
|
|
std::vector<std::pair<Constant*, bool> > ArgumentConstants;
|
|
|
|
ArgumentConstants.resize(F.asize());
|
|
|
|
|
|
|
|
unsigned NumNonconstant = 0;
|
|
|
|
|
|
|
|
for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
|
|
|
|
if (!isa<Instruction>(*I))
|
|
|
|
return false; // Used by a non-instruction, do not transform
|
|
|
|
else {
|
|
|
|
CallSite CS = CallSite::get(cast<Instruction>(*I));
|
|
|
|
if (CS.getInstruction() == 0 ||
|
|
|
|
CS.getCalledFunction() != &F)
|
|
|
|
return false; // Not a direct call site?
|
|
|
|
|
|
|
|
// Check out all of the potentially constant arguments
|
|
|
|
CallSite::arg_iterator AI = CS.arg_begin();
|
|
|
|
for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
|
|
|
|
if (*AI == &F) return false; // Passes the function into itself
|
|
|
|
|
|
|
|
if (!ArgumentConstants[i].second) {
|
2004-07-18 16:31:18 +08:00
|
|
|
if (Constant *C = dyn_cast<Constant>(*AI)) {
|
2003-10-24 00:52:27 +08:00
|
|
|
if (!ArgumentConstants[i].first)
|
|
|
|
ArgumentConstants[i].first = C;
|
|
|
|
else if (ArgumentConstants[i].first != C) {
|
|
|
|
// Became non-constant
|
|
|
|
ArgumentConstants[i].second = true;
|
|
|
|
++NumNonconstant;
|
|
|
|
if (NumNonconstant == ArgumentConstants.size()) return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is not a constant argument. Mark the argument as
|
|
|
|
// non-constant.
|
|
|
|
ArgumentConstants[i].second = true;
|
|
|
|
++NumNonconstant;
|
|
|
|
if (NumNonconstant == ArgumentConstants.size()) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got to this point, there is a constant argument!
|
|
|
|
assert(NumNonconstant != ArgumentConstants.size());
|
|
|
|
Function::aiterator AI = F.abegin();
|
2003-10-28 05:09:00 +08:00
|
|
|
bool MadeChange = false;
|
2003-10-24 00:52:27 +08:00
|
|
|
for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
|
|
|
|
// Do we have a constant argument!?
|
2003-10-28 05:09:00 +08:00
|
|
|
if (!ArgumentConstants[i].second && !AI->use_empty()) {
|
2003-10-24 00:52:27 +08:00
|
|
|
assert(ArgumentConstants[i].first && "Unknown constant value!");
|
2003-10-24 02:49:23 +08:00
|
|
|
Value *V = ArgumentConstants[i].first;
|
|
|
|
AI->replaceAllUsesWith(V);
|
2003-10-24 00:52:27 +08:00
|
|
|
++NumArgumentsProped;
|
2003-10-28 05:09:00 +08:00
|
|
|
MadeChange = true;
|
2003-10-24 00:52:27 +08:00
|
|
|
}
|
2003-10-28 05:09:00 +08:00
|
|
|
return MadeChange;
|
2003-10-24 00:52:27 +08:00
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|