rename InstValue to SimpleValue, add some comments.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122725 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2011-01-03 02:20:48 +00:00
parent f0da7299fa
commit f19745947d

View File

@ -29,10 +29,14 @@ using namespace llvm;
STATISTIC(NumSimplify, "Number of insts simplified or DCE'd"); STATISTIC(NumSimplify, "Number of insts simplified or DCE'd");
STATISTIC(NumCSE, "Number of insts CSE'd"); STATISTIC(NumCSE, "Number of insts CSE'd");
//===----------------------------------------------------------------------===//
// SimpleValue
//===----------------------------------------------------------------------===//
namespace { namespace {
/// InstValue - Instances of this struct represent available values in the /// SimpleValue - Instances of this struct represent available values in the
/// scoped hash table. /// scoped hash table.
struct InstValue { struct SimpleValue {
Instruction *Inst; Instruction *Inst;
bool isSentinel() const { bool isSentinel() const {
@ -48,8 +52,8 @@ namespace {
isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst); isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst);
} }
static InstValue get(Instruction *I) { static SimpleValue get(Instruction *I) {
InstValue X; X.Inst = I; SimpleValue X; X.Inst = I;
assert((X.isSentinel() || canHandle(I)) && "Inst can't be handled!"); assert((X.isSentinel() || canHandle(I)) && "Inst can't be handled!");
return X; return X;
} }
@ -57,20 +61,20 @@ namespace {
} }
namespace llvm { namespace llvm {
// InstValue is POD. // SimpleValue is POD.
template<> struct isPodLike<InstValue> { template<> struct isPodLike<SimpleValue> {
static const bool value = true; static const bool value = true;
}; };
template<> struct DenseMapInfo<InstValue> { template<> struct DenseMapInfo<SimpleValue> {
static inline InstValue getEmptyKey() { static inline SimpleValue getEmptyKey() {
return InstValue::get(DenseMapInfo<Instruction*>::getEmptyKey()); return SimpleValue::get(DenseMapInfo<Instruction*>::getEmptyKey());
} }
static inline InstValue getTombstoneKey() { static inline SimpleValue getTombstoneKey() {
return InstValue::get(DenseMapInfo<Instruction*>::getTombstoneKey()); return SimpleValue::get(DenseMapInfo<Instruction*>::getTombstoneKey());
} }
static unsigned getHashValue(InstValue Val); static unsigned getHashValue(SimpleValue Val);
static bool isEqual(InstValue LHS, InstValue RHS); static bool isEqual(SimpleValue LHS, SimpleValue RHS);
}; };
} }
@ -78,7 +82,7 @@ unsigned getHash(const void *V) {
return DenseMapInfo<const void*>::getHashValue(V); return DenseMapInfo<const void*>::getHashValue(V);
} }
unsigned DenseMapInfo<InstValue>::getHashValue(InstValue Val) { unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
Instruction *Inst = Val.Inst; Instruction *Inst = Val.Inst;
// Hash in all of the operands as pointers. // Hash in all of the operands as pointers.
@ -110,7 +114,7 @@ unsigned DenseMapInfo<InstValue>::getHashValue(InstValue Val) {
return (Res << 1) ^ Inst->getOpcode(); return (Res << 1) ^ Inst->getOpcode();
} }
bool DenseMapInfo<InstValue>::isEqual(InstValue LHS, InstValue RHS) { bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
if (LHS.isSentinel() || RHS.isSentinel()) if (LHS.isSentinel() || RHS.isSentinel())
@ -121,6 +125,10 @@ bool DenseMapInfo<InstValue>::isEqual(InstValue LHS, InstValue RHS) {
} }
//===----------------------------------------------------------------------===//
// EarlyCSE pass
//===----------------------------------------------------------------------===//
namespace { namespace {
/// EarlyCSE - This pass does a simple depth-first walk over the dominator /// EarlyCSE - This pass does a simple depth-first walk over the dominator
@ -134,14 +142,19 @@ public:
const TargetData *TD; const TargetData *TD;
DominatorTree *DT; DominatorTree *DT;
typedef RecyclingAllocator<BumpPtrAllocator, typedef RecyclingAllocator<BumpPtrAllocator,
ScopedHashTableVal<InstValue, Instruction*> > AllocatorTy; ScopedHashTableVal<SimpleValue, Value*> > AllocatorTy;
typedef ScopedHashTable<InstValue, Instruction*, DenseMapInfo<InstValue>, typedef ScopedHashTable<SimpleValue, Value*, DenseMapInfo<SimpleValue>,
AllocatorTy> ScopedHTType; AllocatorTy> ScopedHTType;
ScopedHTType *AvailableValues;
/// AvailableValues - This scoped hash table contains the current values of
/// all of our simple scalar expressions. As we walk down the domtree, we
/// look to see if instructions are in this: if so, we replace them with what
/// we find, otherwise we insert them so that dominated values can succeed in
/// their lookup.
ScopedHTType *AvailableValues;
static char ID; static char ID;
explicit EarlyCSE() explicit EarlyCSE() : FunctionPass(ID) {
: FunctionPass(ID) {
initializeEarlyCSEPass(*PassRegistry::getPassRegistry()); initializeEarlyCSEPass(*PassRegistry::getPassRegistry());
} }
@ -171,8 +184,10 @@ INITIALIZE_PASS_DEPENDENCY(DominatorTree)
INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false) INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
bool EarlyCSE::processNode(DomTreeNode *Node) { bool EarlyCSE::processNode(DomTreeNode *Node) {
// Define a scope in the scoped hash table. // Define a scope in the scoped hash table. When we are done processing this
ScopedHashTableScope<InstValue, Instruction*, DenseMapInfo<InstValue>, // domtree node and recurse back up to our parent domtree node, this will pop
// off all the values we install.
ScopedHashTableScope<SimpleValue, Value*, DenseMapInfo<SimpleValue>,
AllocatorTy> Scope(*AvailableValues); AllocatorTy> Scope(*AvailableValues);
BasicBlock *BB = Node->getBlock(); BasicBlock *BB = Node->getBlock();
@ -205,11 +220,11 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
} }
// If this instruction is something that we can't value number, ignore it. // If this instruction is something that we can't value number, ignore it.
if (!InstValue::canHandle(Inst)) if (!SimpleValue::canHandle(Inst))
continue; continue;
// See if the instruction has an available value. If so, use it. // See if the instruction has an available value. If so, use it.
if (Instruction *V = AvailableValues->lookup(InstValue::get(Inst))) { if (Value *V = AvailableValues->lookup(SimpleValue::get(Inst))) {
DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n'); DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
Inst->replaceAllUsesWith(V); Inst->replaceAllUsesWith(V);
Inst->eraseFromParent(); Inst->eraseFromParent();
@ -219,7 +234,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
} }
// Otherwise, just remember that this value is available. // Otherwise, just remember that this value is available.
AvailableValues->insert(InstValue::get(Inst), Inst); AvailableValues->insert(SimpleValue::get(Inst), Inst);
} }
@ -234,6 +249,6 @@ bool EarlyCSE::runOnFunction(Function &F) {
DT = &getAnalysis<DominatorTree>(); DT = &getAnalysis<DominatorTree>();
ScopedHTType AVTable; ScopedHTType AVTable;
AvailableValues = &AVTable; AvailableValues = &AVTable;
return processNode(DT->getRootNode()); return processNode(DT->getRootNode());
} }