Convert SCCP over to use InstVisitor instead of hand crafted switch

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2286 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2002-04-18 15:13:15 +00:00
parent d59b0af98b
commit 2a632551a7

View File

@ -25,6 +25,7 @@
#include "llvm/iTerminators.h" #include "llvm/iTerminators.h"
#include "llvm/iOther.h" #include "llvm/iOther.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Support/InstVisitor.h"
#include "Support/STLExtras.h" #include "Support/STLExtras.h"
#include <algorithm> #include <algorithm>
#include <map> #include <map>
@ -84,7 +85,7 @@ public:
// This class does all of the work of Sparse Conditional Constant Propogation. // This class does all of the work of Sparse Conditional Constant Propogation.
// It's public interface consists of a constructor and a doSCCP() method. // It's public interface consists of a constructor and a doSCCP() method.
// //
class SCCP { class SCCP : public InstVisitor<SCCP> {
Function *M; // The function that we are working on Function *M; // The function that we are working on
std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
@ -109,6 +110,7 @@ public:
// The implementation of this class // The implementation of this class
// //
private: private:
friend class InstVisitor<SCCP>; // Allow callbacks from visitor
// markValueOverdefined - Make a value be marked as "constant". If the value // markValueOverdefined - Make a value be marked as "constant". If the value
// is not already a constant, add it to the instruction work list so that // is not already a constant, add it to the instruction work list so that
@ -168,11 +170,34 @@ private:
} }
// UpdateInstruction - Something changed in this instruction... Either an // visit implementations - Something changed in this instruction... Either an
// operand made a transition, or the instruction is newly executable. Change // operand made a transition, or the instruction is newly executable. Change
// the value type of I to reflect these changes if appropriate. // the value type of I to reflect these changes if appropriate.
// //
void UpdateInstruction(Instruction *I); void visitPHINode(PHINode *I);
// Terminators
void visitReturnInst(ReturnInst *I) { /*does not have an effect*/ }
void visitBranchInst(BranchInst *I);
void visitSwitchInst(SwitchInst *I);
void visitUnaryOperator(Instruction *I);
void visitCastInst(CastInst *I) { visitUnaryOperator(I); }
void visitBinaryOperator(Instruction *I);
void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
// Instructions that cannot be folded away...
void visitMemAccessInst (Instruction *I) { markOverdefined(I); }
void visitCallInst (Instruction *I) { markOverdefined(I); }
void visitInvokeInst (Instruction *I) { markOverdefined(I); }
void visitAllocationInst(Instruction *I) { markOverdefined(I); }
void visitFreeInst (Instruction *I) { markOverdefined(I); }
void visitInstruction(Instruction *I) {
// If a new instruction is added to LLVM that we don't handle...
cerr << "SCCP: Don't know how to handle: " << I;
markOverdefined(I); // Just in case
}
// OperandChangedState - This method is invoked on all of the users of an // OperandChangedState - This method is invoked on all of the users of an
// instruction that was just changed state somehow.... Based on this // instruction that was just changed state somehow.... Based on this
@ -225,10 +250,9 @@ bool SCCP::doSCCP() {
if (BB->getTerminator()->getNumSuccessors() == 1) if (BB->getTerminator()->getNumSuccessors() == 1)
markExecutable(BB->getTerminator()->getSuccessor(0)); markExecutable(BB->getTerminator()->getSuccessor(0));
// Loop over all of the instructions and notify them that they are newly // Notify all instructions in this basic block that they are newly
// executable... // executable.
for_each(BB->begin(), BB->end(), visit(BB);
bind_obj(this, &SCCP::UpdateInstruction));
} }
} }
@ -284,7 +308,7 @@ bool SCCP::doSCCP() {
} }
// UpdateInstruction - Something changed in this instruction... Either an // visit Implementations - Something changed in this instruction... Either an
// operand made a transition, or the instruction is newly executable. Change // operand made a transition, or the instruction is newly executable. Change
// the value type of I to reflect these changes if appropriate. This method // the value type of I to reflect these changes if appropriate. This method
// makes sure to do the following actions: // makes sure to do the following actions:
@ -302,17 +326,8 @@ bool SCCP::doSCCP() {
// 7. If a conditional branch has a value that is overdefined, make all // 7. If a conditional branch has a value that is overdefined, make all
// successors executable. // successors executable.
// //
void SCCP::UpdateInstruction(Instruction *I) {
InstVal &IValue = ValueState[I];
if (IValue.isOverdefined())
return; // If already overdefined, we aren't going to effect anything
switch (I->getOpcode()) { void SCCP::visitPHINode(PHINode *PN) {
//===-----------------------------------------------------------------===//
// Handle PHI nodes...
//
case Instruction::PHINode: {
PHINode *PN = cast<PHINode>(I);
unsigned NumValues = PN->getNumIncomingValues(), i; unsigned NumValues = PN->getNumIncomingValues(), i;
InstVal *OperandIV = 0; InstVal *OperandIV = 0;
@ -343,7 +358,7 @@ void SCCP::UpdateInstruction(Instruction *I) {
// Yes there is. This means the PHI node is not constant. // Yes there is. This means the PHI node is not constant.
// You must be overdefined poor PHI. // You must be overdefined poor PHI.
// //
markOverdefined(I); // The PHI node now becomes overdefined markOverdefined(PN); // The PHI node now becomes overdefined
return; // I'm done analyzing you return; // I'm done analyzing you
} }
} }
@ -357,31 +372,11 @@ void SCCP::UpdateInstruction(Instruction *I) {
// //
if (OperandIV) { if (OperandIV) {
assert(OperandIV->isConstant() && "Should only be here for constants!"); assert(OperandIV->isConstant() && "Should only be here for constants!");
markConstant(I, OperandIV->getConstant()); // Aquire operand value markConstant(PN, OperandIV->getConstant()); // Aquire operand value
}
return;
} }
}
//===-----------------------------------------------------------------===// void SCCP::visitBranchInst(BranchInst *BI) {
// Handle instructions that unconditionally provide overdefined values...
//
case Instruction::Malloc:
case Instruction::Free:
case Instruction::Alloca:
case Instruction::Load:
case Instruction::Store:
// TODO: getfield
case Instruction::Call:
case Instruction::Invoke:
markOverdefined(I); // Memory and call's are all overdefined
return;
//===-----------------------------------------------------------------===//
// Handle Terminator instructions...
//
case Instruction::Ret: return; // Function return doesn't affect anything
case Instruction::Br: { // Handle conditional branches...
BranchInst *BI = cast<BranchInst>(I);
if (BI->isUnconditional()) if (BI->isUnconditional())
return; // Unconditional branches are already handled! return; // Unconditional branches are already handled!
@ -392,17 +387,14 @@ void SCCP::UpdateInstruction(Instruction *I) {
markExecutable(BI->getSuccessor(1)); markExecutable(BI->getSuccessor(1));
} else if (BCValue.isConstant()) { } else if (BCValue.isConstant()) {
// Constant condition variables mean the branch can only go a single way. // Constant condition variables mean the branch can only go a single way.
ConstantBool *CPB = cast<ConstantBool>(BCValue.getConstant()); if (BCValue.getConstant() == ConstantBool::True)
if (CPB->getValue()) // If the branch condition is TRUE...
markExecutable(BI->getSuccessor(0)); markExecutable(BI->getSuccessor(0));
else // Else if the br cond is FALSE... else
markExecutable(BI->getSuccessor(1)); markExecutable(BI->getSuccessor(1));
} }
return; }
}
case Instruction::Switch: { void SCCP::visitSwitchInst(SwitchInst *SI) {
SwitchInst *SI = cast<SwitchInst>(I);
InstVal &SCValue = getValueState(SI->getCondition()); InstVal &SCValue = getValueState(SI->getCondition());
if (SCValue.isOverdefined()) { // Overdefined condition? All dests are exe if (SCValue.isOverdefined()) { // Overdefined condition? All dests are exe
for(unsigned i = 0; BasicBlock *Succ = SI->getSuccessor(i); ++i) for(unsigned i = 0; BasicBlock *Succ = SI->getSuccessor(i); ++i)
@ -421,17 +413,9 @@ void SCCP::UpdateInstruction(Instruction *I) {
// default branch then... // default branch then...
markExecutable(SI->getDefaultDest()); markExecutable(SI->getDefaultDest());
} }
return; }
}
default: break; // Handle math operators as groups. void SCCP::visitUnaryOperator(Instruction *I) {
} // end switch(I->getOpcode())
//===-------------------------------------------------------------------===//
// Handle Unary and cast instructions...
//
if (isa<UnaryOperator>(I) || isa<CastInst>(I)) {
Value *V = I->getOperand(0); Value *V = I->getOperand(0);
InstVal &VState = getValueState(V); InstVal &VState = getValueState(V);
if (VState.isOverdefined()) { // Inherit overdefinedness of operand if (VState.isOverdefined()) { // Inherit overdefinedness of operand
@ -448,54 +432,25 @@ void SCCP::UpdateInstruction(Instruction *I) {
markOverdefined(I); // Don't know how to fold this instruction. :( markOverdefined(I); // Don't know how to fold this instruction. :(
} }
} }
return; }
}
// Handle BinaryOperators and Shift Instructions...
//===-----------------------------------------------------------------===// void SCCP::visitBinaryOperator(Instruction *I) {
// Handle GetElementPtr instructions... InstVal &V1State = getValueState(I->getOperand(0));
// InstVal &V2State = getValueState(I->getOperand(1));
if (isa<GetElementPtrInst>(I)) {
markOverdefined(I);
return;
}
//===-----------------------------------------------------------------===//
// Handle Binary instructions...
//
if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) {
Value *V1 = I->getOperand(0);
Value *V2 = I->getOperand(1);
InstVal &V1State = getValueState(V1);
InstVal &V2State = getValueState(V2);
if (V1State.isOverdefined() || V2State.isOverdefined()) { if (V1State.isOverdefined() || V2State.isOverdefined()) {
markOverdefined(I); markOverdefined(I);
} else if (V1State.isConstant() && V2State.isConstant()) { } else if (V1State.isConstant() && V2State.isConstant()) {
Constant *Result = Constant *Result = ConstantFoldBinaryInstruction(I->getOpcode(),
ConstantFoldBinaryInstruction(I->getOpcode(),
V1State.getConstant(), V1State.getConstant(),
V2State.getConstant()); V2State.getConstant());
if (Result) { if (Result)
// This instruction constant folds! markConstant(I, Result); // This instruction constant fold!s
markConstant(I, Result); else
} else {
markOverdefined(I); // Don't know how to fold this instruction. :( markOverdefined(I); // Don't know how to fold this instruction. :(
} }
}
return;
}
// Shouldn't get here... either the switch statement or one of the group
// handlers should have kicked in...
//
cerr << "SCCP: Don't know how to handle: " << I;
markOverdefined(I); // Just in case
} }
// OperandChangedState - This method is invoked on all of the users of an // OperandChangedState - This method is invoked on all of the users of an
// instruction that was just changed state somehow.... Based on this // instruction that was just changed state somehow.... Based on this
// information, we need to update the specified user of this instruction. // information, we need to update the specified user of this instruction.
@ -505,7 +460,7 @@ void SCCP::OperandChangedState(User *U) {
Instruction *I = cast<Instruction>(U); Instruction *I = cast<Instruction>(U);
if (!BBExecutable.count(I->getParent())) return; // Inst not executable yet! if (!BBExecutable.count(I->getParent())) return; // Inst not executable yet!
UpdateInstruction(I); visit(I);
} }
namespace { namespace {