2002-04-28 08:47:11 +08:00
|
|
|
//===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===//
|
|
|
|
//
|
|
|
|
// 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-31 06:53:30 +08:00
|
|
|
// using an existing value numbering implementation to identify the common
|
|
|
|
// subexpressions, eliminating them when possible.
|
2002-04-28 08:47:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-05-08 04:03:00 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-04-28 08:47:11 +08:00
|
|
|
#include "llvm/iMemory.h"
|
2002-08-31 06:53:30 +08:00
|
|
|
#include "llvm/Type.h"
|
2002-04-28 08:47:11 +08:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2002-08-31 04:22:29 +08:00
|
|
|
#include "llvm/Analysis/ValueNumbering.h"
|
2002-04-28 08:47:11 +08:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2002-10-02 06:38:41 +08:00
|
|
|
#include "Support/Statistic.h"
|
2002-04-28 08:47:11 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace {
|
2002-10-02 06:38:41 +08:00
|
|
|
Statistic<> NumInstRemoved("gcse", "Number of instructions removed");
|
|
|
|
Statistic<> NumLoadRemoved("gcse", "Number of loads removed");
|
|
|
|
Statistic<> NumNonInsts ("gcse", "Number of instructions removed due "
|
2002-08-31 04:22:29 +08:00
|
|
|
"to non-instruction values");
|
|
|
|
|
|
|
|
class GCSE : public FunctionPass {
|
2002-08-31 06:53:30 +08:00
|
|
|
std::set<Instruction*> WorkList;
|
2002-05-14 13:02:40 +08:00
|
|
|
DominatorSet *DomSetInfo;
|
2002-08-31 04:22:29 +08:00
|
|
|
ValueNumbering *VN;
|
2002-04-28 08:47:11 +08:00
|
|
|
public:
|
2002-06-26 00:13:24 +08:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
private:
|
2002-08-31 04:22:29 +08:00
|
|
|
bool EliminateRedundancies(Instruction *I,std::vector<Value*> &EqualValues);
|
|
|
|
Instruction *EliminateCSE(Instruction *I, Instruction *Other);
|
2002-04-28 08:47:11 +08:00
|
|
|
void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
|
2002-05-14 13:02:40 +08:00
|
|
|
|
2002-04-28 08:47:11 +08:00
|
|
|
// This transformation requires dominator and immediate dominator info
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-10-22 04:00:28 +08:00
|
|
|
AU.setPreservesCFG();
|
2002-08-09 03:01:30 +08:00
|
|
|
AU.addRequired<DominatorSet>();
|
|
|
|
AU.addRequired<ImmediateDominators>();
|
2002-08-31 04:22:29 +08:00
|
|
|
AU.addRequired<ValueNumbering>();
|
2002-04-28 08:47:11 +08:00
|
|
|
}
|
|
|
|
};
|
2002-07-24 02:06:35 +08:00
|
|
|
|
2002-07-27 05:12:46 +08:00
|
|
|
RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
|
2002-04-28 08:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// createGCSEPass - The public interface to this file...
|
|
|
|
Pass *createGCSEPass() { return new GCSE(); }
|
|
|
|
|
|
|
|
|
|
|
|
// GCSE::runOnFunction - This is the main transformation entry point for a
|
|
|
|
// function.
|
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
bool GCSE::runOnFunction(Function &F) {
|
2002-04-28 08:47:11 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
2002-08-23 02:24:48 +08:00
|
|
|
// Get pointers to the analysis results that we will be using...
|
2002-04-28 08:47:11 +08:00
|
|
|
DomSetInfo = &getAnalysis<DominatorSet>();
|
2002-08-31 04:22:29 +08:00
|
|
|
VN = &getAnalysis<ValueNumbering>();
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
// Step #1: Add all instructions in the function to the worklist for
|
|
|
|
// processing. All of the instructions are considered to be our
|
|
|
|
// subexpressions to eliminate if possible.
|
|
|
|
//
|
|
|
|
WorkList.insert(inst_begin(F), inst_end(F));
|
|
|
|
|
|
|
|
// Step #2: WorkList processing. Iterate through all of the instructions,
|
|
|
|
// checking to see if there are any additionally defined subexpressions in the
|
|
|
|
// program. If so, eliminate them!
|
|
|
|
//
|
|
|
|
while (!WorkList.empty()) {
|
2002-06-26 00:13:24 +08:00
|
|
|
Instruction &I = **WorkList.begin(); // Get an instruction from the worklist
|
2002-04-28 08:47:11 +08:00
|
|
|
WorkList.erase(WorkList.begin());
|
|
|
|
|
2002-08-31 04:22:29 +08:00
|
|
|
// If this instruction computes a value, try to fold together common
|
|
|
|
// instructions that compute it.
|
2002-04-28 08:47:11 +08:00
|
|
|
//
|
2002-08-31 04:22:29 +08:00
|
|
|
if (I.getType() != Type::VoidTy) {
|
|
|
|
std::vector<Value*> EqualValues;
|
|
|
|
VN->getEqualNumberNodes(&I, EqualValues);
|
|
|
|
|
|
|
|
if (!EqualValues.empty())
|
|
|
|
Changed |= EliminateRedundancies(&I, EqualValues);
|
|
|
|
}
|
2002-04-28 08:47:11 +08:00
|
|
|
}
|
2002-05-14 13:02:40 +08:00
|
|
|
|
2002-04-28 08:47:11 +08:00
|
|
|
// When the worklist is empty, return whether or not we changed anything...
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2002-08-31 04:22:29 +08:00
|
|
|
bool GCSE::EliminateRedundancies(Instruction *I,
|
|
|
|
std::vector<Value*> &EqualValues) {
|
|
|
|
// If the EqualValues set contains any non-instruction values, then we know
|
|
|
|
// that all of the instructions can be replaced with the non-instruction value
|
|
|
|
// because it is guaranteed to dominate all of the instructions in the
|
|
|
|
// function. We only have to do hard work if all we have are instructions.
|
|
|
|
//
|
|
|
|
for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
|
|
|
|
if (!isa<Instruction>(EqualValues[i])) {
|
|
|
|
// Found a non-instruction. Replace all instructions with the
|
|
|
|
// non-instruction.
|
|
|
|
//
|
|
|
|
Value *Replacement = EqualValues[i];
|
|
|
|
|
|
|
|
// Make sure we get I as well...
|
|
|
|
EqualValues[i] = I;
|
|
|
|
|
|
|
|
// Replace all instructions with the Replacement value.
|
|
|
|
for (i = 0; i != e; ++i)
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(EqualValues[i])) {
|
|
|
|
// Change all users of I to use Replacement.
|
|
|
|
I->replaceAllUsesWith(Replacement);
|
|
|
|
|
|
|
|
if (isa<LoadInst>(I))
|
|
|
|
++NumLoadRemoved; // Keep track of loads eliminated
|
|
|
|
++NumInstRemoved; // Keep track of number of instructions eliminated
|
|
|
|
++NumNonInsts; // Keep track of number of insts repl with values
|
|
|
|
|
|
|
|
// Erase the instruction from the program.
|
|
|
|
I->getParent()->getInstList().erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove duplicate entries from EqualValues...
|
|
|
|
std::sort(EqualValues.begin(), EqualValues.end());
|
|
|
|
EqualValues.erase(std::unique(EqualValues.begin(), EqualValues.end()),
|
|
|
|
EqualValues.end());
|
|
|
|
|
|
|
|
// From this point on, EqualValues is logically a vector of instructions.
|
|
|
|
//
|
|
|
|
bool Changed = false;
|
|
|
|
EqualValues.push_back(I); // Make sure I is included...
|
|
|
|
while (EqualValues.size() > 1) {
|
|
|
|
// FIXME, this could be done better than simple iteration!
|
|
|
|
Instruction *Test = cast<Instruction>(EqualValues.back());
|
|
|
|
EqualValues.pop_back();
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
|
|
|
|
if (Instruction *Ret = EliminateCSE(Test,
|
|
|
|
cast<Instruction>(EqualValues[i]))) {
|
|
|
|
if (Ret == Test) // Eliminated EqualValues[i]
|
|
|
|
EqualValues[i] = Test; // Make sure that we reprocess I at some point
|
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
// ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all
|
|
|
|
// uses of the instruction use First now instead.
|
|
|
|
//
|
|
|
|
void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
|
2002-06-26 00:13:24 +08:00
|
|
|
Instruction &Second = *SI;
|
2002-04-30 00:20:25 +08:00
|
|
|
|
|
|
|
//cerr << "DEL " << (void*)Second << Second;
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
// Add the first instruction back to the worklist
|
|
|
|
WorkList.insert(First);
|
|
|
|
|
|
|
|
// Add all uses of the second instruction to the worklist
|
2002-06-26 00:13:24 +08:00
|
|
|
for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end();
|
2002-04-28 08:47:11 +08:00
|
|
|
UI != UE; ++UI)
|
|
|
|
WorkList.insert(cast<Instruction>(*UI));
|
|
|
|
|
|
|
|
// Make all users of 'Second' now use 'First'
|
2002-06-26 00:13:24 +08:00
|
|
|
Second.replaceAllUsesWith(First);
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
// Erase the second instruction from the program
|
2002-06-26 00:13:24 +08:00
|
|
|
Second.getParent()->getInstList().erase(SI);
|
2002-04-28 08:47:11 +08:00
|
|
|
}
|
|
|
|
|
2002-08-31 04:22:29 +08:00
|
|
|
// EliminateCSE - The two instruction I & Other have been found to be common
|
|
|
|
// subexpressions. This function is responsible for eliminating one of them,
|
|
|
|
// and for fixing the worklist to be correct. The instruction that is preserved
|
|
|
|
// is returned from the function if the other is eliminated, otherwise null is
|
|
|
|
// returned.
|
2002-04-28 08:47:11 +08:00
|
|
|
//
|
2002-08-31 04:22:29 +08:00
|
|
|
Instruction *GCSE::EliminateCSE(Instruction *I, Instruction *Other) {
|
2002-05-14 13:02:40 +08:00
|
|
|
assert(I != Other);
|
2002-04-30 00:20:25 +08:00
|
|
|
|
2002-05-14 13:02:40 +08:00
|
|
|
WorkList.erase(I);
|
2002-04-30 00:20:25 +08:00
|
|
|
WorkList.erase(Other); // Other may not actually be on the worklist anymore...
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
// Handle the easy case, where both instructions are in the same basic block
|
|
|
|
BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
|
2002-08-31 04:22:29 +08:00
|
|
|
Instruction *Ret = 0;
|
|
|
|
|
2002-04-28 08:47:11 +08:00
|
|
|
if (BB1 == BB2) {
|
|
|
|
// Eliminate the second occuring instruction. Add all uses of the second
|
|
|
|
// instruction to the worklist.
|
|
|
|
//
|
|
|
|
// Scan the basic block looking for the "first" instruction
|
|
|
|
BasicBlock::iterator BI = BB1->begin();
|
2002-06-26 00:13:24 +08:00
|
|
|
while (&*BI != I && &*BI != Other) {
|
2002-04-28 08:47:11 +08:00
|
|
|
++BI;
|
|
|
|
assert(BI != BB1->end() && "Instructions not found in parent BB!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of which instructions occurred first & second
|
2002-06-26 00:13:24 +08:00
|
|
|
Instruction *First = BI;
|
2002-04-28 08:47:11 +08:00
|
|
|
Instruction *Second = I != First ? I : Other; // Get iterator to second inst
|
2002-06-26 00:13:24 +08:00
|
|
|
BI = Second;
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
// Destroy Second, using First instead.
|
2002-08-31 04:22:29 +08:00
|
|
|
ReplaceInstWithInst(First, BI);
|
|
|
|
Ret = First;
|
2002-04-28 08:47:11 +08:00
|
|
|
|
|
|
|
// Otherwise, the two instructions are in different basic blocks. If one
|
|
|
|
// dominates the other instruction, we can simply use it
|
|
|
|
//
|
|
|
|
} else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other?
|
2002-06-26 00:13:24 +08:00
|
|
|
ReplaceInstWithInst(I, Other);
|
2002-08-31 04:22:29 +08:00
|
|
|
Ret = I;
|
2002-04-28 08:47:11 +08:00
|
|
|
} else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I?
|
2002-06-26 00:13:24 +08:00
|
|
|
ReplaceInstWithInst(Other, I);
|
2002-08-31 04:22:29 +08:00
|
|
|
Ret = Other;
|
2002-04-28 08:47:11 +08:00
|
|
|
} else {
|
2002-08-03 02:06:01 +08:00
|
|
|
// This code is disabled because it has several problems:
|
|
|
|
// One, the actual assumption is wrong, as shown by this code:
|
|
|
|
// int "test"(int %X, int %Y) {
|
|
|
|
// %Z = add int %X, %Y
|
|
|
|
// ret int %Z
|
|
|
|
// Unreachable:
|
|
|
|
// %Q = add int %X, %Y
|
|
|
|
// ret int %Q
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Here there are no shared dominators. Additionally, this had the habit of
|
|
|
|
// moving computations where they were not always computed. For example, in
|
2003-02-01 12:50:59 +08:00
|
|
|
// a case like this:
|
2002-08-03 02:06:01 +08:00
|
|
|
// if (c) {
|
|
|
|
// if (d) ...
|
|
|
|
// else ... X+Y ...
|
|
|
|
// } else {
|
|
|
|
// ... X+Y ...
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// In thiscase, the expression would be hoisted to outside the 'if' stmt,
|
|
|
|
// causing the expression to be evaluated, even for the if (d) path, which
|
|
|
|
// could cause problems, if, for example, it caused a divide by zero. In
|
|
|
|
// general the problem this case is trying to solve is better addressed with
|
|
|
|
// PRE than GCSE.
|
|
|
|
//
|
2002-08-31 04:22:29 +08:00
|
|
|
return 0;
|
2002-04-28 08:47:11 +08:00
|
|
|
}
|
|
|
|
|
2002-08-31 04:22:29 +08:00
|
|
|
if (isa<LoadInst>(Ret))
|
|
|
|
++NumLoadRemoved; // Keep track of loads eliminated
|
|
|
|
++NumInstRemoved; // Keep track of number of instructions eliminated
|
2002-05-14 13:02:40 +08:00
|
|
|
|
2002-08-31 04:22:29 +08:00
|
|
|
// Add all users of Ret to the worklist...
|
|
|
|
for (Value::use_iterator I = Ret->use_begin(), E = Ret->use_end(); I != E;++I)
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(*I))
|
|
|
|
WorkList.insert(Inst);
|
2002-05-14 13:02:40 +08:00
|
|
|
|
2002-08-31 04:22:29 +08:00
|
|
|
return Ret;
|
2002-04-28 08:47:11 +08:00
|
|
|
}
|