blob: 8a363286c06c99aac6a53b3998b2f19b769306ac [file] [log] [blame]
Chris Lattner704541b2011-01-02 21:47:05 +00001//===- 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"
Michael Ilseman336cb792012-10-09 16:57:38 +000017#include "llvm/ADT/Hashing.h"
Chris Lattner18ae5432011-01-02 23:04:14 +000018#include "llvm/ADT/ScopedHashTable.h"
Chris Lattner8fac5db2011-01-02 23:19:45 +000019#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Analysis/Dominators.h"
21#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Pass.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/RecyclingAllocator.h"
27#include "llvm/Target/TargetLibraryInfo.h"
28#include "llvm/Transforms/Utils/Local.h"
Michael Gottesman2bf01732013-12-05 18:42:12 +000029#include <vector>
Chris Lattner704541b2011-01-02 21:47:05 +000030using namespace llvm;
31
Chris Lattner4cb36542011-01-03 03:28:23 +000032STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd");
33STATISTIC(NumCSE, "Number of instructions CSE'd");
Chris Lattner92bb0f92011-01-03 03:41:27 +000034STATISTIC(NumCSELoad, "Number of load instructions CSE'd");
35STATISTIC(NumCSECall, "Number of call instructions CSE'd");
Chris Lattner9e5e9ed2011-01-03 04:17:24 +000036STATISTIC(NumDSE, "Number of trivial dead stores removed");
Chris Lattnerb9a8efc2011-01-03 03:18:43 +000037
38static unsigned getHash(const void *V) {
39 return DenseMapInfo<const void*>::getHashValue(V);
40}
Chris Lattner8fac5db2011-01-02 23:19:45 +000041
Chris Lattner79d83062011-01-03 02:20:48 +000042//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +000043// SimpleValue
Chris Lattner79d83062011-01-03 02:20:48 +000044//===----------------------------------------------------------------------===//
45
Chris Lattner704541b2011-01-02 21:47:05 +000046namespace {
Chris Lattner79d83062011-01-03 02:20:48 +000047 /// SimpleValue - Instances of this struct represent available values in the
Chris Lattner18ae5432011-01-02 23:04:14 +000048 /// scoped hash table.
Chris Lattner79d83062011-01-03 02:20:48 +000049 struct SimpleValue {
Chris Lattner18ae5432011-01-02 23:04:14 +000050 Instruction *Inst;
Nadav Rotem465834c2012-07-24 10:51:42 +000051
Chris Lattner4cb36542011-01-03 03:28:23 +000052 SimpleValue(Instruction *I) : Inst(I) {
53 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
54 }
Nadav Rotem465834c2012-07-24 10:51:42 +000055
Chris Lattner18ae5432011-01-02 23:04:14 +000056 bool isSentinel() const {
57 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() ||
58 Inst == DenseMapInfo<Instruction*>::getTombstoneKey();
59 }
Nadav Rotem465834c2012-07-24 10:51:42 +000060
Chris Lattner18ae5432011-01-02 23:04:14 +000061 static bool canHandle(Instruction *Inst) {
Chris Lattnerbde6ec12011-01-03 23:38:13 +000062 // This can only handle non-void readnone functions.
63 if (CallInst *CI = dyn_cast<CallInst>(Inst))
64 return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy();
Chris Lattner8fac5db2011-01-02 23:19:45 +000065 return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) ||
66 isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) ||
67 isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) ||
68 isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) ||
69 isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst);
Chris Lattner18ae5432011-01-02 23:04:14 +000070 }
Chris Lattner18ae5432011-01-02 23:04:14 +000071 };
72}
73
74namespace llvm {
Chris Lattner79d83062011-01-03 02:20:48 +000075template<> struct DenseMapInfo<SimpleValue> {
76 static inline SimpleValue getEmptyKey() {
Chris Lattner4cb36542011-01-03 03:28:23 +000077 return DenseMapInfo<Instruction*>::getEmptyKey();
Chris Lattner18ae5432011-01-02 23:04:14 +000078 }
Chris Lattner79d83062011-01-03 02:20:48 +000079 static inline SimpleValue getTombstoneKey() {
Chris Lattner4cb36542011-01-03 03:28:23 +000080 return DenseMapInfo<Instruction*>::getTombstoneKey();
Chris Lattner18ae5432011-01-02 23:04:14 +000081 }
Chris Lattner79d83062011-01-03 02:20:48 +000082 static unsigned getHashValue(SimpleValue Val);
83 static bool isEqual(SimpleValue LHS, SimpleValue RHS);
Chris Lattner18ae5432011-01-02 23:04:14 +000084};
85}
86
Chris Lattner79d83062011-01-03 02:20:48 +000087unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
Chris Lattner18ae5432011-01-02 23:04:14 +000088 Instruction *Inst = Val.Inst;
Chris Lattner02a97762011-01-03 01:10:08 +000089 // Hash in all of the operands as pointers.
Michael Ilseman336cb792012-10-09 16:57:38 +000090 if (BinaryOperator* BinOp = dyn_cast<BinaryOperator>(Inst)) {
91 Value *LHS = BinOp->getOperand(0);
92 Value *RHS = BinOp->getOperand(1);
93 if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1))
94 std::swap(LHS, RHS);
Chris Lattner02a97762011-01-03 01:10:08 +000095
Michael Ilseman336cb792012-10-09 16:57:38 +000096 if (isa<OverflowingBinaryOperator>(BinOp)) {
97 // Hash the overflow behavior
98 unsigned Overflow =
99 BinOp->hasNoSignedWrap() * OverflowingBinaryOperator::NoSignedWrap |
100 BinOp->hasNoUnsignedWrap() * OverflowingBinaryOperator::NoUnsignedWrap;
101 return hash_combine(BinOp->getOpcode(), Overflow, LHS, RHS);
102 }
103
104 return hash_combine(BinOp->getOpcode(), LHS, RHS);
Chris Lattner02a97762011-01-03 01:10:08 +0000105 }
106
Michael Ilseman336cb792012-10-09 16:57:38 +0000107 if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
108 Value *LHS = CI->getOperand(0);
109 Value *RHS = CI->getOperand(1);
110 CmpInst::Predicate Pred = CI->getPredicate();
111 if (Inst->getOperand(0) > Inst->getOperand(1)) {
112 std::swap(LHS, RHS);
113 Pred = CI->getSwappedPredicate();
114 }
115 return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
116 }
117
118 if (CastInst *CI = dyn_cast<CastInst>(Inst))
119 return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0));
120
121 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst))
122 return hash_combine(EVI->getOpcode(), EVI->getOperand(0),
123 hash_combine_range(EVI->idx_begin(), EVI->idx_end()));
124
125 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst))
126 return hash_combine(IVI->getOpcode(), IVI->getOperand(0),
127 IVI->getOperand(1),
128 hash_combine_range(IVI->idx_begin(), IVI->idx_end()));
129
130 assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) ||
131 isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) ||
132 isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
133 isa<ShuffleVectorInst>(Inst)) && "Invalid/unknown instruction");
134
Chris Lattner02a97762011-01-03 01:10:08 +0000135 // Mix in the opcode.
Michael Ilseman336cb792012-10-09 16:57:38 +0000136 return hash_combine(Inst->getOpcode(),
137 hash_combine_range(Inst->value_op_begin(),
138 Inst->value_op_end()));
Chris Lattner18ae5432011-01-02 23:04:14 +0000139}
140
Chris Lattner79d83062011-01-03 02:20:48 +0000141bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000142 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
143
144 if (LHS.isSentinel() || RHS.isSentinel())
145 return LHSI == RHSI;
Nadav Rotem465834c2012-07-24 10:51:42 +0000146
Chris Lattner18ae5432011-01-02 23:04:14 +0000147 if (LHSI->getOpcode() != RHSI->getOpcode()) return false;
Michael Ilseman336cb792012-10-09 16:57:38 +0000148 if (LHSI->isIdenticalTo(RHSI)) return true;
149
150 // If we're not strictly identical, we still might be a commutable instruction
151 if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) {
152 if (!LHSBinOp->isCommutative())
153 return false;
154
155 assert(isa<BinaryOperator>(RHSI)
156 && "same opcode, but different instruction type?");
157 BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI);
158
159 // Check overflow attributes
160 if (isa<OverflowingBinaryOperator>(LHSBinOp)) {
161 assert(isa<OverflowingBinaryOperator>(RHSBinOp)
162 && "same opcode, but different operator type?");
163 if (LHSBinOp->hasNoUnsignedWrap() != RHSBinOp->hasNoUnsignedWrap() ||
164 LHSBinOp->hasNoSignedWrap() != RHSBinOp->hasNoSignedWrap())
165 return false;
166 }
167
168 // Commuted equality
169 return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) &&
170 LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0);
171 }
172 if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) {
173 assert(isa<CmpInst>(RHSI)
174 && "same opcode, but different instruction type?");
175 CmpInst *RHSCmp = cast<CmpInst>(RHSI);
176 // Commuted equality
177 return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) &&
178 LHSCmp->getOperand(1) == RHSCmp->getOperand(0) &&
179 LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate();
180 }
181
182 return false;
Chris Lattner18ae5432011-01-02 23:04:14 +0000183}
184
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000185//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +0000186// CallValue
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000187//===----------------------------------------------------------------------===//
188
189namespace {
Chris Lattner92bb0f92011-01-03 03:41:27 +0000190 /// CallValue - Instances of this struct represent available call values in
191 /// the scoped hash table.
192 struct CallValue {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000193 Instruction *Inst;
Nadav Rotem465834c2012-07-24 10:51:42 +0000194
Chris Lattner92bb0f92011-01-03 03:41:27 +0000195 CallValue(Instruction *I) : Inst(I) {
Chris Lattner4cb36542011-01-03 03:28:23 +0000196 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
197 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000198
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000199 bool isSentinel() const {
200 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() ||
201 Inst == DenseMapInfo<Instruction*>::getTombstoneKey();
202 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000203
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000204 static bool canHandle(Instruction *Inst) {
Chris Lattner16ca19f2011-01-03 18:43:03 +0000205 // Don't value number anything that returns void.
206 if (Inst->getType()->isVoidTy())
207 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000208
Chris Lattner142f1cd2011-01-03 18:28:15 +0000209 CallInst *CI = dyn_cast<CallInst>(Inst);
210 if (CI == 0 || !CI->onlyReadsMemory())
211 return false;
Chris Lattner142f1cd2011-01-03 18:28:15 +0000212 return true;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000213 }
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000214 };
215}
216
217namespace llvm {
Chris Lattner92bb0f92011-01-03 03:41:27 +0000218 template<> struct DenseMapInfo<CallValue> {
219 static inline CallValue getEmptyKey() {
Chris Lattner4cb36542011-01-03 03:28:23 +0000220 return DenseMapInfo<Instruction*>::getEmptyKey();
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000221 }
Chris Lattner92bb0f92011-01-03 03:41:27 +0000222 static inline CallValue getTombstoneKey() {
Chris Lattner4cb36542011-01-03 03:28:23 +0000223 return DenseMapInfo<Instruction*>::getTombstoneKey();
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000224 }
Chris Lattner92bb0f92011-01-03 03:41:27 +0000225 static unsigned getHashValue(CallValue Val);
226 static bool isEqual(CallValue LHS, CallValue RHS);
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000227 };
228}
Chris Lattner92bb0f92011-01-03 03:41:27 +0000229unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000230 Instruction *Inst = Val.Inst;
231 // Hash in all of the operands as pointers.
232 unsigned Res = 0;
Chris Lattner16ca19f2011-01-03 18:43:03 +0000233 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) {
234 assert(!Inst->getOperand(i)->getType()->isMetadataTy() &&
235 "Cannot value number calls with metadata operands");
Eli Friedman154a9672011-10-12 22:00:26 +0000236 Res ^= getHash(Inst->getOperand(i)) << (i & 0xF);
Chris Lattner16ca19f2011-01-03 18:43:03 +0000237 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000238
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000239 // Mix in the opcode.
240 return (Res << 1) ^ Inst->getOpcode();
241}
242
Chris Lattner92bb0f92011-01-03 03:41:27 +0000243bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000244 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000245 if (LHS.isSentinel() || RHS.isSentinel())
246 return LHSI == RHSI;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000247 return LHSI->isIdenticalTo(RHSI);
248}
249
Chris Lattner18ae5432011-01-02 23:04:14 +0000250
Chris Lattner79d83062011-01-03 02:20:48 +0000251//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +0000252// EarlyCSE pass.
Chris Lattner79d83062011-01-03 02:20:48 +0000253//===----------------------------------------------------------------------===//
254
Chris Lattner18ae5432011-01-02 23:04:14 +0000255namespace {
Nadav Rotem465834c2012-07-24 10:51:42 +0000256
Chris Lattner704541b2011-01-02 21:47:05 +0000257/// EarlyCSE - This pass does a simple depth-first walk over the dominator
258/// tree, eliminating trivially redundant instructions and using instsimplify
259/// to canonicalize things as it goes. It is intended to be fast and catch
260/// obvious cases so that instcombine and other passes are more effective. It
261/// is expected that a later pass of GVN will catch the interesting/hard
262/// cases.
263class EarlyCSE : public FunctionPass {
264public:
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000265 const DataLayout *TD;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000266 const TargetLibraryInfo *TLI;
Chris Lattner18ae5432011-01-02 23:04:14 +0000267 DominatorTree *DT;
Chris Lattnerd815f692011-01-03 01:42:46 +0000268 typedef RecyclingAllocator<BumpPtrAllocator,
Chris Lattner79d83062011-01-03 02:20:48 +0000269 ScopedHashTableVal<SimpleValue, Value*> > AllocatorTy;
270 typedef ScopedHashTable<SimpleValue, Value*, DenseMapInfo<SimpleValue>,
Chris Lattnerd815f692011-01-03 01:42:46 +0000271 AllocatorTy> ScopedHTType;
Nadav Rotem465834c2012-07-24 10:51:42 +0000272
Chris Lattner79d83062011-01-03 02:20:48 +0000273 /// AvailableValues - This scoped hash table contains the current values of
274 /// all of our simple scalar expressions. As we walk down the domtree, we
275 /// look to see if instructions are in this: if so, we replace them with what
276 /// we find, otherwise we insert them so that dominated values can succeed in
277 /// their lookup.
278 ScopedHTType *AvailableValues;
Nadav Rotem465834c2012-07-24 10:51:42 +0000279
Chris Lattner92bb0f92011-01-03 03:41:27 +0000280 /// AvailableLoads - This scoped hash table contains the current values
281 /// of loads. This allows us to get efficient access to dominating loads when
282 /// we have a fully redundant load. In addition to the most recent load, we
283 /// keep track of a generation count of the read, which is compared against
284 /// the current generation count. The current generation count is
285 /// incremented after every possibly writing memory operation, which ensures
286 /// that we only CSE loads with other loads that have no intervening store.
Chris Lattner4b9a5252011-01-03 03:53:50 +0000287 typedef RecyclingAllocator<BumpPtrAllocator,
288 ScopedHashTableVal<Value*, std::pair<Value*, unsigned> > > LoadMapAllocator;
289 typedef ScopedHashTable<Value*, std::pair<Value*, unsigned>,
290 DenseMapInfo<Value*>, LoadMapAllocator> LoadHTType;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000291 LoadHTType *AvailableLoads;
Nadav Rotem465834c2012-07-24 10:51:42 +0000292
Chris Lattner92bb0f92011-01-03 03:41:27 +0000293 /// AvailableCalls - This scoped hash table contains the current values
294 /// of read-only call values. It uses the same generation count as loads.
295 typedef ScopedHashTable<CallValue, std::pair<Value*, unsigned> > CallHTType;
296 CallHTType *AvailableCalls;
Nadav Rotem465834c2012-07-24 10:51:42 +0000297
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000298 /// CurrentGeneration - This is the current generation of the memory value.
299 unsigned CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000300
Chris Lattner704541b2011-01-02 21:47:05 +0000301 static char ID;
Chris Lattner79d83062011-01-03 02:20:48 +0000302 explicit EarlyCSE() : FunctionPass(ID) {
Chris Lattner704541b2011-01-02 21:47:05 +0000303 initializeEarlyCSEPass(*PassRegistry::getPassRegistry());
304 }
305
306 bool runOnFunction(Function &F);
307
308private:
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000309
310 // NodeScope - almost a POD, but needs to call the constructors for the
311 // scoped hash tables so that a new scope gets pushed on. These are RAII so
312 // that the scope gets popped when the NodeScope is destroyed.
313 class NodeScope {
314 public:
315 NodeScope(ScopedHTType *availableValues,
316 LoadHTType *availableLoads,
317 CallHTType *availableCalls) :
318 Scope(*availableValues),
319 LoadScope(*availableLoads),
320 CallScope(*availableCalls) {}
321
322 private:
Craig Toppera60c0f12012-09-15 17:09:36 +0000323 NodeScope(const NodeScope&) LLVM_DELETED_FUNCTION;
324 void operator=(const NodeScope&) LLVM_DELETED_FUNCTION;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000325
326 ScopedHTType::ScopeTy Scope;
327 LoadHTType::ScopeTy LoadScope;
328 CallHTType::ScopeTy CallScope;
329 };
330
331 // StackNode - contains all the needed information to create a stack for
332 // doing a depth first tranversal of the tree. This includes scopes for
333 // values, loads, and calls as well as the generation. There is a child
334 // iterator so that the children do not need to be store spearately.
335 class StackNode {
336 public:
337 StackNode(ScopedHTType *availableValues,
338 LoadHTType *availableLoads,
339 CallHTType *availableCalls,
340 unsigned cg, DomTreeNode *n,
341 DomTreeNode::iterator child, DomTreeNode::iterator end) :
342 CurrentGeneration(cg), ChildGeneration(cg), Node(n),
343 ChildIter(child), EndIter(end),
344 Scopes(availableValues, availableLoads, availableCalls),
345 Processed(false) {}
346
347 // Accessors.
348 unsigned currentGeneration() { return CurrentGeneration; }
349 unsigned childGeneration() { return ChildGeneration; }
350 void childGeneration(unsigned generation) { ChildGeneration = generation; }
351 DomTreeNode *node() { return Node; }
352 DomTreeNode::iterator childIter() { return ChildIter; }
353 DomTreeNode *nextChild() {
354 DomTreeNode *child = *ChildIter;
355 ++ChildIter;
356 return child;
357 }
358 DomTreeNode::iterator end() { return EndIter; }
359 bool isProcessed() { return Processed; }
360 void process() { Processed = true; }
361
362 private:
Craig Toppera60c0f12012-09-15 17:09:36 +0000363 StackNode(const StackNode&) LLVM_DELETED_FUNCTION;
364 void operator=(const StackNode&) LLVM_DELETED_FUNCTION;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000365
366 // Members.
367 unsigned CurrentGeneration;
368 unsigned ChildGeneration;
369 DomTreeNode *Node;
370 DomTreeNode::iterator ChildIter;
371 DomTreeNode::iterator EndIter;
372 NodeScope Scopes;
373 bool Processed;
374 };
375
Chris Lattner18ae5432011-01-02 23:04:14 +0000376 bool processNode(DomTreeNode *Node);
Nadav Rotem465834c2012-07-24 10:51:42 +0000377
Chris Lattner704541b2011-01-02 21:47:05 +0000378 // This transformation requires dominator postdominator info
379 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
380 AU.addRequired<DominatorTree>();
Chad Rosierc24b86f2011-12-01 03:08:23 +0000381 AU.addRequired<TargetLibraryInfo>();
Chris Lattner704541b2011-01-02 21:47:05 +0000382 AU.setPreservesCFG();
383 }
384};
385}
386
387char EarlyCSE::ID = 0;
388
389// createEarlyCSEPass - The public interface to this file.
390FunctionPass *llvm::createEarlyCSEPass() {
391 return new EarlyCSE();
392}
393
394INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false)
395INITIALIZE_PASS_DEPENDENCY(DominatorTree)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000396INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Chris Lattner704541b2011-01-02 21:47:05 +0000397INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
398
Chris Lattner18ae5432011-01-02 23:04:14 +0000399bool EarlyCSE::processNode(DomTreeNode *Node) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000400 BasicBlock *BB = Node->getBlock();
Nadav Rotem465834c2012-07-24 10:51:42 +0000401
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000402 // If this block has a single predecessor, then the predecessor is the parent
403 // of the domtree node and all of the live out memory values are still current
404 // in this block. If this block has multiple predecessors, then they could
405 // have invalidated the live-out memory values of our parent value. For now,
406 // just be conservative and invalidate memory if this block has multiple
407 // predecessors.
408 if (BB->getSinglePredecessor() == 0)
409 ++CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000410
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000411 /// LastStore - Keep track of the last non-volatile store that we saw... for
412 /// as long as there in no instruction that reads memory. If we see a store
413 /// to the same location, we delete the dead store. This zaps trivial dead
414 /// stores which can occur in bitfield code among other things.
415 StoreInst *LastStore = 0;
Nadav Rotem465834c2012-07-24 10:51:42 +0000416
Chris Lattner18ae5432011-01-02 23:04:14 +0000417 bool Changed = false;
418
419 // See if any instructions in the block can be eliminated. If so, do it. If
420 // not, add them to AvailableValues.
421 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
422 Instruction *Inst = I++;
Nadav Rotem465834c2012-07-24 10:51:42 +0000423
Chris Lattner18ae5432011-01-02 23:04:14 +0000424 // Dead instructions should just be removed.
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000425 if (isInstructionTriviallyDead(Inst, TLI)) {
Chris Lattner8fac5db2011-01-02 23:19:45 +0000426 DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n');
Chris Lattner18ae5432011-01-02 23:04:14 +0000427 Inst->eraseFromParent();
428 Changed = true;
Chris Lattner8fac5db2011-01-02 23:19:45 +0000429 ++NumSimplify;
Chris Lattner18ae5432011-01-02 23:04:14 +0000430 continue;
431 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000432
Chris Lattner18ae5432011-01-02 23:04:14 +0000433 // If the instruction can be simplified (e.g. X+0 = X) then replace it with
434 // its simpler value.
Chad Rosierc24b86f2011-12-01 03:08:23 +0000435 if (Value *V = SimplifyInstruction(Inst, TD, TLI, DT)) {
Chris Lattner8fac5db2011-01-02 23:19:45 +0000436 DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
Chris Lattner18ae5432011-01-02 23:04:14 +0000437 Inst->replaceAllUsesWith(V);
438 Inst->eraseFromParent();
439 Changed = true;
Chris Lattner8fac5db2011-01-02 23:19:45 +0000440 ++NumSimplify;
Chris Lattner18ae5432011-01-02 23:04:14 +0000441 continue;
442 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000443
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000444 // If this is a simple instruction that we can value number, process it.
445 if (SimpleValue::canHandle(Inst)) {
446 // See if the instruction has an available value. If so, use it.
Chris Lattner4cb36542011-01-03 03:28:23 +0000447 if (Value *V = AvailableValues->lookup(Inst)) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000448 DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
449 Inst->replaceAllUsesWith(V);
450 Inst->eraseFromParent();
451 Changed = true;
452 ++NumCSE;
453 continue;
454 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000455
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000456 // Otherwise, just remember that this value is available.
Chris Lattner4cb36542011-01-03 03:28:23 +0000457 AvailableValues->insert(Inst, Inst);
Chris Lattner18ae5432011-01-02 23:04:14 +0000458 continue;
459 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000460
Chris Lattner92bb0f92011-01-03 03:41:27 +0000461 // If this is a non-volatile load, process it.
462 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
463 // Ignore volatile loads.
Eli Friedman7c5dc122011-09-12 20:23:13 +0000464 if (!LI->isSimple()) {
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000465 LastStore = 0;
466 continue;
467 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000468
Chris Lattner92bb0f92011-01-03 03:41:27 +0000469 // If we have an available version of this load, and if it is the right
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000470 // generation, replace this instruction.
Chris Lattner92bb0f92011-01-03 03:41:27 +0000471 std::pair<Value*, unsigned> InVal =
472 AvailableLoads->lookup(Inst->getOperand(0));
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000473 if (InVal.first != 0 && InVal.second == CurrentGeneration) {
Chris Lattner92bb0f92011-01-03 03:41:27 +0000474 DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst << " to: "
475 << *InVal.first << '\n');
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000476 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
477 Inst->eraseFromParent();
478 Changed = true;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000479 ++NumCSELoad;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000480 continue;
481 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000482
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000483 // Otherwise, remember that we have this instruction.
Chris Lattner92bb0f92011-01-03 03:41:27 +0000484 AvailableLoads->insert(Inst->getOperand(0),
485 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000486 LastStore = 0;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000487 continue;
488 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000489
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000490 // If this instruction may read from memory, forget LastStore.
491 if (Inst->mayReadFromMemory())
492 LastStore = 0;
Nadav Rotem465834c2012-07-24 10:51:42 +0000493
Chris Lattner92bb0f92011-01-03 03:41:27 +0000494 // If this is a read-only call, process it.
495 if (CallValue::canHandle(Inst)) {
496 // If we have an available version of this call, and if it is the right
497 // generation, replace this instruction.
498 std::pair<Value*, unsigned> InVal = AvailableCalls->lookup(Inst);
499 if (InVal.first != 0 && InVal.second == CurrentGeneration) {
500 DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst << " to: "
501 << *InVal.first << '\n');
502 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
503 Inst->eraseFromParent();
504 Changed = true;
505 ++NumCSECall;
506 continue;
507 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000508
Chris Lattner92bb0f92011-01-03 03:41:27 +0000509 // Otherwise, remember that we have this instruction.
510 AvailableCalls->insert(Inst,
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000511 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
512 continue;
513 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000514
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000515 // Okay, this isn't something we can CSE at all. Check to see if it is
516 // something that could modify memory. If so, our available memory values
517 // cannot be used so bump the generation count.
Chris Lattnere0e32a92011-01-03 03:46:34 +0000518 if (Inst->mayWriteToMemory()) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000519 ++CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000520
Chris Lattnere0e32a92011-01-03 03:46:34 +0000521 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000522 // We do a trivial form of DSE if there are two stores to the same
523 // location with no intervening loads. Delete the earlier store.
524 if (LastStore &&
525 LastStore->getPointerOperand() == SI->getPointerOperand()) {
526 DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore << " due to: "
Chris Lattner142f1cd2011-01-03 18:28:15 +0000527 << *Inst << '\n');
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000528 LastStore->eraseFromParent();
529 Changed = true;
530 ++NumDSE;
531 LastStore = 0;
532 continue;
533 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000534
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000535 // Okay, we just invalidated anything we knew about loaded values. Try
536 // to salvage *something* by remembering that the stored value is a live
537 // version of the pointer. It is safe to forward from volatile stores
538 // to non-volatile loads, so we don't have to check for volatility of
539 // the store.
Chris Lattnere0e32a92011-01-03 03:46:34 +0000540 AvailableLoads->insert(SI->getPointerOperand(),
541 std::pair<Value*, unsigned>(SI->getValueOperand(), CurrentGeneration));
Nadav Rotem465834c2012-07-24 10:51:42 +0000542
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000543 // Remember that this was the last store we saw for DSE.
Eli Friedman7c5dc122011-09-12 20:23:13 +0000544 if (SI->isSimple())
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000545 LastStore = SI;
Chris Lattnere0e32a92011-01-03 03:46:34 +0000546 }
547 }
Chris Lattner18ae5432011-01-02 23:04:14 +0000548 }
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000549
Chris Lattner18ae5432011-01-02 23:04:14 +0000550 return Changed;
Chris Lattner704541b2011-01-02 21:47:05 +0000551}
Chris Lattner18ae5432011-01-02 23:04:14 +0000552
553
554bool EarlyCSE::runOnFunction(Function &F) {
Michael Gottesman2bf01732013-12-05 18:42:12 +0000555 std::vector<StackNode *> nodesToProcess;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000556
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000557 TD = getAnalysisIfAvailable<DataLayout>();
Chad Rosierc24b86f2011-12-01 03:08:23 +0000558 TLI = &getAnalysis<TargetLibraryInfo>();
Chris Lattner18ae5432011-01-02 23:04:14 +0000559 DT = &getAnalysis<DominatorTree>();
Nadav Rotem465834c2012-07-24 10:51:42 +0000560
Chris Lattner92bb0f92011-01-03 03:41:27 +0000561 // Tables that the pass uses when walking the domtree.
Chris Lattnerd815f692011-01-03 01:42:46 +0000562 ScopedHTType AVTable;
Chris Lattner18ae5432011-01-02 23:04:14 +0000563 AvailableValues = &AVTable;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000564 LoadHTType LoadTable;
565 AvailableLoads = &LoadTable;
566 CallHTType CallTable;
567 AvailableCalls = &CallTable;
Nadav Rotem465834c2012-07-24 10:51:42 +0000568
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000569 CurrentGeneration = 0;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000570 bool Changed = false;
571
572 // Process the root node.
Michael Gottesman2bf01732013-12-05 18:42:12 +0000573 nodesToProcess.push_back(
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000574 new StackNode(AvailableValues, AvailableLoads, AvailableCalls,
575 CurrentGeneration, DT->getRootNode(),
576 DT->getRootNode()->begin(),
577 DT->getRootNode()->end()));
578
579 // Save the current generation.
580 unsigned LiveOutGeneration = CurrentGeneration;
581
582 // Process the stack.
583 while (!nodesToProcess.empty()) {
584 // Grab the first item off the stack. Set the current generation, remove
585 // the node from the stack, and process it.
Michael Gottesman2bf01732013-12-05 18:42:12 +0000586 StackNode *NodeToProcess = nodesToProcess.back();
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000587
588 // Initialize class members.
589 CurrentGeneration = NodeToProcess->currentGeneration();
590
591 // Check if the node needs to be processed.
592 if (!NodeToProcess->isProcessed()) {
593 // Process the node.
594 Changed |= processNode(NodeToProcess->node());
595 NodeToProcess->childGeneration(CurrentGeneration);
596 NodeToProcess->process();
597 } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
598 // Push the next child onto the stack.
599 DomTreeNode *child = NodeToProcess->nextChild();
Michael Gottesman2bf01732013-12-05 18:42:12 +0000600 nodesToProcess.push_back(
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000601 new StackNode(AvailableValues,
602 AvailableLoads,
603 AvailableCalls,
604 NodeToProcess->childGeneration(), child,
605 child->begin(), child->end()));
606 } else {
607 // It has been processed, and there are no more children to process,
608 // so delete it and pop it off the stack.
609 delete NodeToProcess;
Michael Gottesman2bf01732013-12-05 18:42:12 +0000610 nodesToProcess.pop_back();
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000611 }
612 } // while (!nodes...)
613
614 // Reset the current generation.
615 CurrentGeneration = LiveOutGeneration;
616
617 return Changed;
Chris Lattner18ae5432011-01-02 23:04:14 +0000618}