Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 1 | //===- EarlyCSE.cpp - Simple and fast CSE pass ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs a simple dominator tree walk that eliminates trivially |
| 11 | // redundant instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "early-cse" |
| 16 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Dominators.h" |
| 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
| 22 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Chris Lattner | 82dcd5e | 2011-01-03 01:42:46 +0000 | [diff] [blame] | 24 | #include "llvm/Support/RecyclingAllocator.h" |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/ScopedHashTable.h" |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 29 | STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd"); |
| 30 | STATISTIC(NumCSE, "Number of instructions CSE'd"); |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 31 | STATISTIC(NumCSELoad, "Number of load instructions CSE'd"); |
| 32 | STATISTIC(NumCSECall, "Number of call instructions CSE'd"); |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 33 | |
| 34 | static unsigned getHash(const void *V) { |
| 35 | return DenseMapInfo<const void*>::getHashValue(V); |
| 36 | } |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 37 | |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | // SimpleValue |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 42 | namespace { |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 43 | /// SimpleValue - Instances of this struct represent available values in the |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 44 | /// scoped hash table. |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 45 | struct SimpleValue { |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 46 | Instruction *Inst; |
| 47 | |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 48 | SimpleValue(Instruction *I) : Inst(I) { |
| 49 | assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); |
| 50 | } |
| 51 | |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 52 | bool isSentinel() const { |
| 53 | return Inst == DenseMapInfo<Instruction*>::getEmptyKey() || |
| 54 | Inst == DenseMapInfo<Instruction*>::getTombstoneKey(); |
| 55 | } |
| 56 | |
| 57 | static bool canHandle(Instruction *Inst) { |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 58 | return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) || |
| 59 | isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) || |
| 60 | isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) || |
| 61 | isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) || |
| 62 | isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 64 | }; |
| 65 | } |
| 66 | |
| 67 | namespace llvm { |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 68 | // SimpleValue is POD. |
| 69 | template<> struct isPodLike<SimpleValue> { |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 70 | static const bool value = true; |
| 71 | }; |
| 72 | |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 73 | template<> struct DenseMapInfo<SimpleValue> { |
| 74 | static inline SimpleValue getEmptyKey() { |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 75 | return DenseMapInfo<Instruction*>::getEmptyKey(); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 77 | static inline SimpleValue getTombstoneKey() { |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 78 | return DenseMapInfo<Instruction*>::getTombstoneKey(); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 80 | static unsigned getHashValue(SimpleValue Val); |
| 81 | static bool isEqual(SimpleValue LHS, SimpleValue RHS); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 82 | }; |
| 83 | } |
| 84 | |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 85 | unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) { |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 86 | Instruction *Inst = Val.Inst; |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 87 | |
Chris Lattner | d957c71 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 88 | // Hash in all of the operands as pointers. |
| 89 | unsigned Res = 0; |
| 90 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 91 | Res ^= getHash(Inst->getOperand(i)) << i; |
| 92 | |
| 93 | if (CastInst *CI = dyn_cast<CastInst>(Inst)) |
| 94 | Res ^= getHash(CI->getType()); |
| 95 | else if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) |
| 96 | Res ^= CI->getPredicate(); |
| 97 | else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst)) { |
| 98 | for (ExtractValueInst::idx_iterator I = EVI->idx_begin(), |
| 99 | E = EVI->idx_end(); I != E; ++I) |
| 100 | Res ^= *I; |
| 101 | } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst)) { |
| 102 | for (InsertValueInst::idx_iterator I = IVI->idx_begin(), |
| 103 | E = IVI->idx_end(); I != E; ++I) |
| 104 | Res ^= *I; |
| 105 | } else { |
| 106 | // nothing extra to hash in. |
| 107 | assert((isa<BinaryOperator>(Inst) || isa<GetElementPtrInst>(Inst) || |
| 108 | isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) || |
| 109 | isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst)) && |
| 110 | "Invalid/unknown instruction"); |
| 111 | } |
| 112 | |
| 113 | // Mix in the opcode. |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 114 | return (Res << 1) ^ Inst->getOpcode(); |
| 115 | } |
| 116 | |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 117 | bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) { |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 118 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
| 119 | |
| 120 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 121 | return LHSI == RHSI; |
| 122 | |
| 123 | if (LHSI->getOpcode() != RHSI->getOpcode()) return false; |
| 124 | return LHSI->isIdenticalTo(RHSI); |
| 125 | } |
| 126 | |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 127 | //===----------------------------------------------------------------------===// |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 128 | // CallValue |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
| 130 | |
| 131 | namespace { |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 132 | /// CallValue - Instances of this struct represent available call values in |
| 133 | /// the scoped hash table. |
| 134 | struct CallValue { |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 135 | Instruction *Inst; |
| 136 | |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 137 | CallValue(Instruction *I) : Inst(I) { |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 138 | assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); |
| 139 | } |
| 140 | |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 141 | bool isSentinel() const { |
| 142 | return Inst == DenseMapInfo<Instruction*>::getEmptyKey() || |
| 143 | Inst == DenseMapInfo<Instruction*>::getTombstoneKey(); |
| 144 | } |
| 145 | |
| 146 | static bool canHandle(Instruction *Inst) { |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 147 | if (CallInst *CI = dyn_cast<CallInst>(Inst)) |
| 148 | return CI->onlyReadsMemory(); |
| 149 | return false; |
| 150 | } |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 151 | }; |
| 152 | } |
| 153 | |
| 154 | namespace llvm { |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 155 | // CallValue is POD. |
| 156 | template<> struct isPodLike<CallValue> { |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 157 | static const bool value = true; |
| 158 | }; |
| 159 | |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 160 | template<> struct DenseMapInfo<CallValue> { |
| 161 | static inline CallValue getEmptyKey() { |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 162 | return DenseMapInfo<Instruction*>::getEmptyKey(); |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 163 | } |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 164 | static inline CallValue getTombstoneKey() { |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 165 | return DenseMapInfo<Instruction*>::getTombstoneKey(); |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 166 | } |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 167 | static unsigned getHashValue(CallValue Val); |
| 168 | static bool isEqual(CallValue LHS, CallValue RHS); |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 169 | }; |
| 170 | } |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 171 | unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) { |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 172 | Instruction *Inst = Val.Inst; |
| 173 | // Hash in all of the operands as pointers. |
| 174 | unsigned Res = 0; |
| 175 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 176 | Res ^= getHash(Inst->getOperand(i)) << i; |
| 177 | // Mix in the opcode. |
| 178 | return (Res << 1) ^ Inst->getOpcode(); |
| 179 | } |
| 180 | |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 181 | bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) { |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 182 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 183 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 184 | return LHSI == RHSI; |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 185 | return LHSI->isIdenticalTo(RHSI); |
| 186 | } |
| 187 | |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 188 | |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 189 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 190 | // EarlyCSE pass. |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
| 192 | |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 193 | namespace { |
| 194 | |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 195 | /// EarlyCSE - This pass does a simple depth-first walk over the dominator |
| 196 | /// tree, eliminating trivially redundant instructions and using instsimplify |
| 197 | /// to canonicalize things as it goes. It is intended to be fast and catch |
| 198 | /// obvious cases so that instcombine and other passes are more effective. It |
| 199 | /// is expected that a later pass of GVN will catch the interesting/hard |
| 200 | /// cases. |
| 201 | class EarlyCSE : public FunctionPass { |
| 202 | public: |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 203 | const TargetData *TD; |
| 204 | DominatorTree *DT; |
Chris Lattner | 82dcd5e | 2011-01-03 01:42:46 +0000 | [diff] [blame] | 205 | typedef RecyclingAllocator<BumpPtrAllocator, |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 206 | ScopedHashTableVal<SimpleValue, Value*> > AllocatorTy; |
| 207 | typedef ScopedHashTable<SimpleValue, Value*, DenseMapInfo<SimpleValue>, |
Chris Lattner | 82dcd5e | 2011-01-03 01:42:46 +0000 | [diff] [blame] | 208 | AllocatorTy> ScopedHTType; |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 209 | |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 210 | /// AvailableValues - This scoped hash table contains the current values of |
| 211 | /// all of our simple scalar expressions. As we walk down the domtree, we |
| 212 | /// look to see if instructions are in this: if so, we replace them with what |
| 213 | /// we find, otherwise we insert them so that dominated values can succeed in |
| 214 | /// their lookup. |
| 215 | ScopedHTType *AvailableValues; |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 217 | /// AvailableLoads - This scoped hash table contains the current values |
| 218 | /// of loads. This allows us to get efficient access to dominating loads when |
| 219 | /// we have a fully redundant load. In addition to the most recent load, we |
| 220 | /// keep track of a generation count of the read, which is compared against |
| 221 | /// the current generation count. The current generation count is |
| 222 | /// incremented after every possibly writing memory operation, which ensures |
| 223 | /// that we only CSE loads with other loads that have no intervening store. |
| 224 | typedef ScopedHashTable<Value*, std::pair<Value*, unsigned> > LoadHTType; |
| 225 | LoadHTType *AvailableLoads; |
| 226 | |
| 227 | /// AvailableCalls - This scoped hash table contains the current values |
| 228 | /// of read-only call values. It uses the same generation count as loads. |
| 229 | typedef ScopedHashTable<CallValue, std::pair<Value*, unsigned> > CallHTType; |
| 230 | CallHTType *AvailableCalls; |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 231 | |
| 232 | /// CurrentGeneration - This is the current generation of the memory value. |
| 233 | unsigned CurrentGeneration; |
| 234 | |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 235 | static char ID; |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 236 | explicit EarlyCSE() : FunctionPass(ID) { |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 237 | initializeEarlyCSEPass(*PassRegistry::getPassRegistry()); |
| 238 | } |
| 239 | |
| 240 | bool runOnFunction(Function &F); |
| 241 | |
| 242 | private: |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 243 | |
| 244 | bool processNode(DomTreeNode *Node); |
| 245 | |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 246 | // This transformation requires dominator postdominator info |
| 247 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 248 | AU.addRequired<DominatorTree>(); |
| 249 | AU.setPreservesCFG(); |
| 250 | } |
| 251 | }; |
| 252 | } |
| 253 | |
| 254 | char EarlyCSE::ID = 0; |
| 255 | |
| 256 | // createEarlyCSEPass - The public interface to this file. |
| 257 | FunctionPass *llvm::createEarlyCSEPass() { |
| 258 | return new EarlyCSE(); |
| 259 | } |
| 260 | |
| 261 | INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false) |
| 262 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 263 | INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false) |
| 264 | |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 265 | bool EarlyCSE::processNode(DomTreeNode *Node) { |
Chris Lattner | f197459 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 266 | // Define a scope in the scoped hash table. When we are done processing this |
| 267 | // domtree node and recurse back up to our parent domtree node, this will pop |
| 268 | // off all the values we install. |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 269 | ScopedHTType::ScopeTy Scope(*AvailableValues); |
| 270 | |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 271 | // Define a scope for the load values so that anything we add will get |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 272 | // popped when we recurse back up to our parent domtree node. |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 273 | LoadHTType::ScopeTy LoadScope(*AvailableLoads); |
| 274 | |
| 275 | // Define a scope for the call values so that anything we add will get |
| 276 | // popped when we recurse back up to our parent domtree node. |
| 277 | CallHTType::ScopeTy CallScope(*AvailableCalls); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 278 | |
| 279 | BasicBlock *BB = Node->getBlock(); |
| 280 | |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 281 | // If this block has a single predecessor, then the predecessor is the parent |
| 282 | // of the domtree node and all of the live out memory values are still current |
| 283 | // in this block. If this block has multiple predecessors, then they could |
| 284 | // have invalidated the live-out memory values of our parent value. For now, |
| 285 | // just be conservative and invalidate memory if this block has multiple |
| 286 | // predecessors. |
| 287 | if (BB->getSinglePredecessor() == 0) |
| 288 | ++CurrentGeneration; |
| 289 | |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 290 | bool Changed = false; |
| 291 | |
| 292 | // See if any instructions in the block can be eliminated. If so, do it. If |
| 293 | // not, add them to AvailableValues. |
| 294 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 295 | Instruction *Inst = I++; |
| 296 | |
| 297 | // Dead instructions should just be removed. |
| 298 | if (isInstructionTriviallyDead(Inst)) { |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 299 | DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n'); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 300 | Inst->eraseFromParent(); |
| 301 | Changed = true; |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 302 | ++NumSimplify; |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 303 | continue; |
| 304 | } |
| 305 | |
| 306 | // If the instruction can be simplified (e.g. X+0 = X) then replace it with |
| 307 | // its simpler value. |
| 308 | if (Value *V = SimplifyInstruction(Inst, TD, DT)) { |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 309 | DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n'); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 310 | Inst->replaceAllUsesWith(V); |
| 311 | Inst->eraseFromParent(); |
| 312 | Changed = true; |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 313 | ++NumSimplify; |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 314 | continue; |
| 315 | } |
| 316 | |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 317 | // If this is a simple instruction that we can value number, process it. |
| 318 | if (SimpleValue::canHandle(Inst)) { |
| 319 | // See if the instruction has an available value. If so, use it. |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 320 | if (Value *V = AvailableValues->lookup(Inst)) { |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 321 | DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n'); |
| 322 | Inst->replaceAllUsesWith(V); |
| 323 | Inst->eraseFromParent(); |
| 324 | Changed = true; |
| 325 | ++NumCSE; |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | // Otherwise, just remember that this value is available. |
Chris Lattner | a60a8b0 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 330 | AvailableValues->insert(Inst, Inst); |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 331 | continue; |
| 332 | } |
| 333 | |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 334 | // If this is a non-volatile load, process it. |
| 335 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 336 | // Ignore volatile loads. |
| 337 | if (LI->isVolatile()) continue; |
| 338 | |
| 339 | // If we have an available version of this load, and if it is the right |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 340 | // generation, replace this instruction. |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 341 | std::pair<Value*, unsigned> InVal = |
| 342 | AvailableLoads->lookup(Inst->getOperand(0)); |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 343 | if (InVal.first != 0 && InVal.second == CurrentGeneration) { |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 344 | DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst << " to: " |
| 345 | << *InVal.first << '\n'); |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 346 | if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first); |
| 347 | Inst->eraseFromParent(); |
| 348 | Changed = true; |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 349 | ++NumCSELoad; |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 350 | continue; |
| 351 | } |
| 352 | |
| 353 | // Otherwise, remember that we have this instruction. |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 354 | AvailableLoads->insert(Inst->getOperand(0), |
| 355 | std::pair<Value*, unsigned>(Inst, CurrentGeneration)); |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | // If this is a read-only call, process it. |
| 360 | if (CallValue::canHandle(Inst)) { |
| 361 | // If we have an available version of this call, and if it is the right |
| 362 | // generation, replace this instruction. |
| 363 | std::pair<Value*, unsigned> InVal = AvailableCalls->lookup(Inst); |
| 364 | if (InVal.first != 0 && InVal.second == CurrentGeneration) { |
| 365 | DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst << " to: " |
| 366 | << *InVal.first << '\n'); |
| 367 | if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first); |
| 368 | Inst->eraseFromParent(); |
| 369 | Changed = true; |
| 370 | ++NumCSECall; |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | // Otherwise, remember that we have this instruction. |
| 375 | AvailableCalls->insert(Inst, |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 376 | std::pair<Value*, unsigned>(Inst, CurrentGeneration)); |
| 377 | continue; |
| 378 | } |
| 379 | |
| 380 | // Okay, this isn't something we can CSE at all. Check to see if it is |
| 381 | // something that could modify memory. If so, our available memory values |
| 382 | // cannot be used so bump the generation count. |
Chris Lattner | ef87fc2 | 2011-01-03 03:46:34 +0000 | [diff] [blame^] | 383 | if (Inst->mayWriteToMemory()) { |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 384 | ++CurrentGeneration; |
Chris Lattner | ef87fc2 | 2011-01-03 03:46:34 +0000 | [diff] [blame^] | 385 | |
| 386 | // Okay, we just invalidated anything we knew about loaded values. Try to |
| 387 | // salvage *something* by remembering that the stored value is a live |
| 388 | // version of the pointer. It is safe to forward from volatile stores to |
| 389 | // non-volatile loads, so we don't have to check for volatility of the |
| 390 | // store. |
| 391 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 392 | AvailableLoads->insert(SI->getPointerOperand(), |
| 393 | std::pair<Value*, unsigned>(SI->getValueOperand(), CurrentGeneration)); |
| 394 | } |
| 395 | } |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 398 | unsigned LiveOutGeneration = CurrentGeneration; |
| 399 | for (DomTreeNode::iterator I = Node->begin(), E = Node->end(); I != E; ++I) { |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 400 | Changed |= processNode(*I); |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 401 | // Pop any generation changes off the stack from the recursive walk. |
| 402 | CurrentGeneration = LiveOutGeneration; |
| 403 | } |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 404 | return Changed; |
Chris Lattner | 12be936 | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 405 | } |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 406 | |
| 407 | |
| 408 | bool EarlyCSE::runOnFunction(Function &F) { |
| 409 | TD = getAnalysisIfAvailable<TargetData>(); |
| 410 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 411 | |
| 412 | // Tables that the pass uses when walking the domtree. |
Chris Lattner | 82dcd5e | 2011-01-03 01:42:46 +0000 | [diff] [blame] | 413 | ScopedHTType AVTable; |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 414 | AvailableValues = &AVTable; |
Chris Lattner | 85db610 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 415 | LoadHTType LoadTable; |
| 416 | AvailableLoads = &LoadTable; |
| 417 | CallHTType CallTable; |
| 418 | AvailableCalls = &CallTable; |
Chris Lattner | 8e7f0d7 | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 419 | |
| 420 | CurrentGeneration = 0; |
Chris Lattner | cc9eab2 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 421 | return processNode(DT->getRootNode()); |
| 422 | } |