2004-05-23 21:19:55 +00:00
|
|
|
//===-- GCSE.cpp - SSA-based Global Common Subexpression Elimination ------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-28 00:47:11 +00:00
|
|
|
//
|
|
|
|
// This pass is designed to be a very quick global transformation that
|
|
|
|
// eliminates global common subexpressions from a function. It does this by
|
2002-08-30 22:53:30 +00:00
|
|
|
// using an existing value numbering implementation to identify the common
|
|
|
|
// subexpressions, eliminating them when possible.
|
2002-04-28 00:47:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
#define DEBUG_TYPE "gcse"
|
2002-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-04-10 21:11:11 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2007-01-30 23:46:24 +00:00
|
|
|
#include "llvm/Function.h"
|
2002-08-30 22:53:30 +00:00
|
|
|
#include "llvm/Type.h"
|
2007-01-30 23:46:24 +00:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2002-04-28 00:47:11 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2002-08-30 20:22:29 +00:00
|
|
|
#include "llvm/Analysis/ValueNumbering.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2002-04-28 00:47:11 +00:00
|
|
|
#include <algorithm>
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
STATISTIC(NumInstRemoved, "Number of instructions removed");
|
|
|
|
STATISTIC(NumLoadRemoved, "Number of loads removed");
|
|
|
|
STATISTIC(NumCallRemoved, "Number of calls removed");
|
|
|
|
STATISTIC(NumNonInsts , "Number of instructions removed due "
|
|
|
|
"to non-instruction values");
|
|
|
|
STATISTIC(NumArgsRepl , "Number of function arguments replaced "
|
|
|
|
"with constant values");
|
2002-04-28 00:47:11 +00:00
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
struct VISIBILITY_HIDDEN GCSE : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
GCSE() : FunctionPass((intptr_t)&ID) {}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-04-28 00:47:11 +00:00
|
|
|
|
|
|
|
private:
|
2004-04-10 21:11:11 +00:00
|
|
|
void ReplaceInstructionWith(Instruction *I, Value *V);
|
2002-05-14 05:02:40 +00:00
|
|
|
|
2002-04-28 00:47:11 +00:00
|
|
|
// This transformation requires dominator and immediate dominator info
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-10-21 20:00:28 +00:00
|
|
|
AU.setPreservesCFG();
|
2004-04-10 21:11:11 +00:00
|
|
|
AU.addRequired<DominatorTree>();
|
2002-08-30 20:22:29 +00:00
|
|
|
AU.addRequired<ValueNumbering>();
|
2002-04-28 00:47:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char GCSE::ID = 0;
|
|
|
|
static RegisterPass<GCSE>
|
|
|
|
X("gcse", "Global Common Subexpression Elimination");
|
|
|
|
|
2002-04-28 00:47:11 +00:00
|
|
|
// createGCSEPass - The public interface to this file...
|
2004-01-09 06:02:20 +00:00
|
|
|
FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
|
2002-04-28 00:47:11 +00:00
|
|
|
|
|
|
|
// GCSE::runOnFunction - This is the main transformation entry point for a
|
|
|
|
// function.
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool GCSE::runOnFunction(Function &F) {
|
2002-04-28 00:47:11 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2002-08-22 18:24:48 +00:00
|
|
|
// Get pointers to the analysis results that we will be using...
|
2004-04-10 21:11:11 +00:00
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
2007-06-07 18:40:55 +00:00
|
|
|
ValueNumbering &VN = getAnalysis<ValueNumbering>();
|
2002-04-28 00:47:11 +00:00
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
std::vector<Value*> EqualValues;
|
2002-04-28 00:47:11 +00:00
|
|
|
|
2004-05-23 21:19:55 +00:00
|
|
|
// Check for value numbers of arguments. If the value numbering
|
|
|
|
// implementation can prove that an incoming argument is a constant or global
|
|
|
|
// value address, substitute it, making the argument dead.
|
2007-04-14 23:32:02 +00:00
|
|
|
for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
|
2004-05-23 21:19:55 +00:00
|
|
|
if (!AI->use_empty()) {
|
|
|
|
VN.getEqualNumberNodes(AI, EqualValues);
|
|
|
|
if (!EqualValues.empty()) {
|
|
|
|
for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
|
2004-07-18 00:32:14 +00:00
|
|
|
if (isa<Constant>(EqualValues[i])) {
|
2004-05-23 21:19:55 +00:00
|
|
|
AI->replaceAllUsesWith(EqualValues[i]);
|
|
|
|
++NumArgsRepl;
|
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
EqualValues.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
// Traverse the CFG of the function in dominator order, so that we see each
|
|
|
|
// instruction after we see its operands.
|
2007-06-04 00:32:22 +00:00
|
|
|
for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
|
2004-04-10 21:11:11 +00:00
|
|
|
E = df_end(DT.getRootNode()); DI != E; ++DI) {
|
|
|
|
BasicBlock *BB = DI->getBlock();
|
2002-04-28 00:47:11 +00:00
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
// Remember which instructions we've seen in this basic block as we scan.
|
|
|
|
std::set<Instruction*> BlockInsts;
|
2002-08-30 20:22:29 +00:00
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
|
|
|
|
Instruction *Inst = I++;
|
2002-05-14 05:02:40 +00:00
|
|
|
|
2004-12-12 18:23:20 +00:00
|
|
|
if (Constant *C = ConstantFoldInstruction(Inst)) {
|
|
|
|
ReplaceInstructionWith(Inst, C);
|
|
|
|
} else if (Inst->getType() != Type::VoidTy) {
|
|
|
|
// If this instruction computes a value, try to fold together common
|
|
|
|
// instructions that compute it.
|
|
|
|
//
|
2004-04-10 21:11:11 +00:00
|
|
|
VN.getEqualNumberNodes(Inst, EqualValues);
|
|
|
|
|
|
|
|
// If this instruction computes a value that is already computed
|
|
|
|
// elsewhere, try to recycle the old value.
|
|
|
|
if (!EqualValues.empty()) {
|
|
|
|
if (Inst == &*BB->begin())
|
|
|
|
I = BB->end();
|
|
|
|
else {
|
|
|
|
I = Inst; --I;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
// First check to see if we were able to value number this instruction
|
|
|
|
// to a non-instruction value. If so, prefer that value over other
|
|
|
|
// instructions which may compute the same thing.
|
|
|
|
for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
|
|
|
|
if (!isa<Instruction>(EqualValues[i])) {
|
|
|
|
++NumNonInsts; // Keep track of # of insts repl with values
|
|
|
|
|
|
|
|
// Change all users of Inst to use the replacement and remove it
|
|
|
|
// from the program.
|
|
|
|
ReplaceInstructionWith(Inst, EqualValues[i]);
|
|
|
|
Inst = 0;
|
|
|
|
EqualValues.clear(); // don't enter the next loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there were no non-instruction values that this instruction
|
|
|
|
// produces, find a dominating instruction that produces the same
|
|
|
|
// value. If we find one, use it's value instead of ours.
|
|
|
|
for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) {
|
|
|
|
Instruction *OtherI = cast<Instruction>(EqualValues[i]);
|
|
|
|
bool Dominates = false;
|
|
|
|
if (OtherI->getParent() == BB)
|
|
|
|
Dominates = BlockInsts.count(OtherI);
|
|
|
|
else
|
2007-06-07 18:45:06 +00:00
|
|
|
Dominates = DT.dominates(OtherI->getParent(), BB);
|
2004-04-10 21:11:11 +00:00
|
|
|
|
|
|
|
if (Dominates) {
|
|
|
|
// Okay, we found an instruction with the same value as this one
|
|
|
|
// and that dominates this one. Replace this instruction with the
|
|
|
|
// specified one.
|
|
|
|
ReplaceInstructionWith(Inst, OtherI);
|
|
|
|
Inst = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EqualValues.clear();
|
|
|
|
|
|
|
|
if (Inst) {
|
|
|
|
I = Inst; ++I; // Deleted no instructions
|
|
|
|
} else if (I == BB->end()) { // Deleted first instruction
|
|
|
|
I = BB->begin();
|
|
|
|
} else { // Deleted inst in middle of block.
|
|
|
|
++I;
|
|
|
|
}
|
2002-08-30 20:22:29 +00:00
|
|
|
}
|
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
if (Inst)
|
|
|
|
BlockInsts.insert(Inst);
|
2002-08-30 20:22:29 +00:00
|
|
|
}
|
2004-04-10 21:11:11 +00:00
|
|
|
}
|
2002-08-30 20:22:29 +00:00
|
|
|
}
|
2004-04-10 21:11:11 +00:00
|
|
|
|
|
|
|
// When the worklist is empty, return whether or not we changed anything...
|
2002-08-30 20:22:29 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2002-04-28 00:47:11 +00:00
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
|
|
|
|
if (isa<LoadInst>(I))
|
|
|
|
++NumLoadRemoved; // Keep track of loads eliminated
|
|
|
|
if (isa<CallInst>(I))
|
|
|
|
++NumCallRemoved; // Keep track of calls eliminated
|
|
|
|
++NumInstRemoved; // Keep track of number of insts eliminated
|
2004-02-05 22:33:19 +00:00
|
|
|
|
2004-04-10 22:33:34 +00:00
|
|
|
// Update value numbering
|
2004-05-23 21:19:55 +00:00
|
|
|
getAnalysis<ValueNumbering>().deleteValue(I);
|
2004-04-10 22:33:34 +00:00
|
|
|
|
2004-12-12 18:23:20 +00:00
|
|
|
I->replaceAllUsesWith(V);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-04-12 05:15:13 +00:00
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
|
|
|
|
// Removing an invoke instruction requires adding a branch to the normal
|
|
|
|
// destination and removing PHI node entries in the exception destination.
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(II->getNormalDest(), II);
|
2004-04-12 05:15:13 +00:00
|
|
|
II->getUnwindDest()->removePredecessor(II->getParent());
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-04-10 21:11:11 +00:00
|
|
|
// Erase the instruction from the program.
|
|
|
|
I->getParent()->getInstList().erase(I);
|
2002-04-28 00:47:11 +00:00
|
|
|
}
|