1 //===- EarlyCSE.cpp - Simple and fast CSE pass ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs a simple dominator tree walk that eliminates trivially
11 // redundant instructions.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "early-cse"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Analysis/Dominators.h"
19 #include "llvm/Analysis/InstructionSimplify.h"
20 #include "llvm/Analysis/InstructionSimplify.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Transforms/Utils/Local.h"
23 #include "llvm/ADT/ScopedHashTable.h"
27 /// InstValue - Instances of this struct represent available values in the
28 /// scoped hash table.
32 bool isSentinel() const {
33 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() ||
34 Inst == DenseMapInfo<Instruction*>::getTombstoneKey();
37 static bool canHandle(Instruction *Inst) {
38 return isa<CastInst>(Inst);
41 static InstValue get(Instruction *I) {
42 InstValue X; X.Inst = I;
43 assert((X.isSentinel() || canHandle(I)) && "Inst can't be handled!");
51 template<> struct isPodLike<InstValue> {
52 static const bool value = true;
55 template<> struct DenseMapInfo<InstValue> {
56 static inline InstValue getEmptyKey() {
57 return InstValue::get(DenseMapInfo<Instruction*>::getEmptyKey());
59 static inline InstValue getTombstoneKey() {
60 return InstValue::get(DenseMapInfo<Instruction*>::getTombstoneKey());
62 static unsigned getHashValue(InstValue Val);
63 static bool isEqual(InstValue LHS, InstValue RHS);
67 unsigned getHash(const void *V) {
68 return DenseMapInfo<const void*>::getHashValue(V);
71 unsigned DenseMapInfo<InstValue>::getHashValue(InstValue Val) {
72 Instruction *Inst = Val.Inst;
74 if (CastInst *CI = dyn_cast<CastInst>(Inst))
75 Res = getHash(CI->getOperand(0)) ^ getHash(CI->getType());
77 assert(0 && "Unhandled instruction kind");
79 return (Res << 1) ^ Inst->getOpcode();
82 bool DenseMapInfo<InstValue>::isEqual(InstValue LHS, InstValue RHS) {
83 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
85 if (LHS.isSentinel() || RHS.isSentinel())
88 if (LHSI->getOpcode() != RHSI->getOpcode()) return false;
89 return LHSI->isIdenticalTo(RHSI);
95 /// EarlyCSE - This pass does a simple depth-first walk over the dominator
96 /// tree, eliminating trivially redundant instructions and using instsimplify
97 /// to canonicalize things as it goes. It is intended to be fast and catch
98 /// obvious cases so that instcombine and other passes are more effective. It
99 /// is expected that a later pass of GVN will catch the interesting/hard
101 class EarlyCSE : public FunctionPass {
103 const TargetData *TD;
105 ScopedHashTable<InstValue, Instruction*> *AvailableValues;
110 initializeEarlyCSEPass(*PassRegistry::getPassRegistry());
113 bool runOnFunction(Function &F);
117 bool processNode(DomTreeNode *Node);
119 // This transformation requires dominator postdominator info
120 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
121 AU.addRequired<DominatorTree>();
122 AU.setPreservesCFG();
127 char EarlyCSE::ID = 0;
129 // createEarlyCSEPass - The public interface to this file.
130 FunctionPass *llvm::createEarlyCSEPass() {
131 return new EarlyCSE();
134 INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false)
135 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
136 INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
138 // FIXME: Should bump pointer allocate entries in scoped hash table.
140 bool EarlyCSE::processNode(DomTreeNode *Node) {
141 // Define a scope in the scoped hash table.
142 ScopedHashTableScope<InstValue, Instruction*> Scope(*AvailableValues);
144 BasicBlock *BB = Node->getBlock();
146 bool Changed = false;
148 // See if any instructions in the block can be eliminated. If so, do it. If
149 // not, add them to AvailableValues.
150 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
151 Instruction *Inst = I++;
153 // Dead instructions should just be removed.
154 if (isInstructionTriviallyDead(Inst)) {
155 Inst->eraseFromParent();
160 // If the instruction can be simplified (e.g. X+0 = X) then replace it with
161 // its simpler value.
162 if (Value *V = SimplifyInstruction(Inst, TD, DT)) {
163 Inst->replaceAllUsesWith(V);
164 Inst->eraseFromParent();
169 // If this instruction is something that we can't value number, ignore it.
170 if (!InstValue::canHandle(Inst))
173 // See if the instruction has an available value. If so, use it.
174 if (Instruction *V = AvailableValues->lookup(InstValue::get(Inst))) {
175 Inst->replaceAllUsesWith(V);
176 Inst->eraseFromParent();
181 // Otherwise, just remember that this value is available.
182 AvailableValues->insert(InstValue::get(Inst), Inst);
186 for (DomTreeNode::iterator I = Node->begin(), E = Node->end(); I != E; ++I)
187 Changed |= processNode(*I);
192 bool EarlyCSE::runOnFunction(Function &F) {
193 TD = getAnalysisIfAvailable<TargetData>();
194 DT = &getAnalysis<DominatorTree>();
195 ScopedHashTable<InstValue, Instruction*> AVTable;
196 AvailableValues = &AVTable;
197 return processNode(DT->getRootNode());