blob: 9309623380f168d11d062c1f943bbbc3429a4408 [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
Chandler Carruthe8c686a2015-02-01 10:51:23 +000015#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Ilseman336cb792012-10-09 16:57:38 +000016#include "llvm/ADT/Hashing.h"
Chris Lattner18ae5432011-01-02 23:04:14 +000017#include "llvm/ADT/ScopedHashTable.h"
Chris Lattner8fac5db2011-01-02 23:19:45 +000018#include "llvm/ADT/Statistic.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000019#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Analysis/InstructionSimplify.h"
Chad Rosierf9327d62015-01-26 22:51:15 +000021#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000023#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Instructions.h"
Hal Finkel1e16fa32014-11-03 20:21:32 +000025#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/PatternMatch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Pass.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/RecyclingAllocator.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000030#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000031#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Transforms/Utils/Local.h"
Lenny Maiorani9eefc812014-09-20 13:29:20 +000033#include <deque>
Chris Lattner704541b2011-01-02 21:47:05 +000034using namespace llvm;
Hal Finkel1e16fa32014-11-03 20:21:32 +000035using namespace llvm::PatternMatch;
Chris Lattner704541b2011-01-02 21:47:05 +000036
Chandler Carruth964daaa2014-04-22 02:55:47 +000037#define DEBUG_TYPE "early-cse"
38
Chris Lattner4cb36542011-01-03 03:28:23 +000039STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd");
40STATISTIC(NumCSE, "Number of instructions CSE'd");
Chris Lattner92bb0f92011-01-03 03:41:27 +000041STATISTIC(NumCSELoad, "Number of load instructions CSE'd");
42STATISTIC(NumCSECall, "Number of call instructions CSE'd");
Chris Lattner9e5e9ed2011-01-03 04:17:24 +000043STATISTIC(NumDSE, "Number of trivial dead stores removed");
Chris Lattnerb9a8efc2011-01-03 03:18:43 +000044
Chris Lattner79d83062011-01-03 02:20:48 +000045//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +000046// SimpleValue
Chris Lattner79d83062011-01-03 02:20:48 +000047//===----------------------------------------------------------------------===//
48
Chris Lattner704541b2011-01-02 21:47:05 +000049namespace {
Chandler Carruth9dea5cd2015-01-24 11:44:32 +000050/// \brief Struct representing the available values in the scoped hash table.
Chandler Carruth7253bba2015-01-24 11:33:55 +000051struct SimpleValue {
52 Instruction *Inst;
Nadav Rotem465834c2012-07-24 10:51:42 +000053
Chandler Carruth7253bba2015-01-24 11:33:55 +000054 SimpleValue(Instruction *I) : Inst(I) {
55 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
56 }
Nadav Rotem465834c2012-07-24 10:51:42 +000057
Chandler Carruth7253bba2015-01-24 11:33:55 +000058 bool isSentinel() const {
59 return Inst == DenseMapInfo<Instruction *>::getEmptyKey() ||
60 Inst == DenseMapInfo<Instruction *>::getTombstoneKey();
61 }
Nadav Rotem465834c2012-07-24 10:51:42 +000062
Chandler Carruth7253bba2015-01-24 11:33:55 +000063 static bool canHandle(Instruction *Inst) {
64 // This can only handle non-void readnone functions.
65 if (CallInst *CI = dyn_cast<CallInst>(Inst))
66 return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy();
67 return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) ||
68 isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) ||
69 isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) ||
70 isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) ||
71 isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst);
72 }
73};
Chris Lattner18ae5432011-01-02 23:04:14 +000074}
75
76namespace llvm {
Chandler Carruth7253bba2015-01-24 11:33:55 +000077template <> struct DenseMapInfo<SimpleValue> {
Chris Lattner79d83062011-01-03 02:20:48 +000078 static inline SimpleValue getEmptyKey() {
Chandler Carruth7253bba2015-01-24 11:33:55 +000079 return DenseMapInfo<Instruction *>::getEmptyKey();
Chris Lattner18ae5432011-01-02 23:04:14 +000080 }
Chris Lattner79d83062011-01-03 02:20:48 +000081 static inline SimpleValue getTombstoneKey() {
Chandler Carruth7253bba2015-01-24 11:33:55 +000082 return DenseMapInfo<Instruction *>::getTombstoneKey();
Chris Lattner18ae5432011-01-02 23:04:14 +000083 }
Chris Lattner79d83062011-01-03 02:20:48 +000084 static unsigned getHashValue(SimpleValue Val);
85 static bool isEqual(SimpleValue LHS, SimpleValue RHS);
Chris Lattner18ae5432011-01-02 23:04:14 +000086};
87}
88
Chris Lattner79d83062011-01-03 02:20:48 +000089unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
Chris Lattner18ae5432011-01-02 23:04:14 +000090 Instruction *Inst = Val.Inst;
Chris Lattner02a97762011-01-03 01:10:08 +000091 // Hash in all of the operands as pointers.
Chandler Carruth7253bba2015-01-24 11:33:55 +000092 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst)) {
Michael Ilseman336cb792012-10-09 16:57:38 +000093 Value *LHS = BinOp->getOperand(0);
94 Value *RHS = BinOp->getOperand(1);
95 if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1))
96 std::swap(LHS, RHS);
Chris Lattner02a97762011-01-03 01:10:08 +000097
Michael Ilseman336cb792012-10-09 16:57:38 +000098 if (isa<OverflowingBinaryOperator>(BinOp)) {
99 // Hash the overflow behavior
100 unsigned Overflow =
Chandler Carruth7253bba2015-01-24 11:33:55 +0000101 BinOp->hasNoSignedWrap() * OverflowingBinaryOperator::NoSignedWrap |
102 BinOp->hasNoUnsignedWrap() *
103 OverflowingBinaryOperator::NoUnsignedWrap;
Michael Ilseman336cb792012-10-09 16:57:38 +0000104 return hash_combine(BinOp->getOpcode(), Overflow, LHS, RHS);
105 }
106
107 return hash_combine(BinOp->getOpcode(), LHS, RHS);
Chris Lattner02a97762011-01-03 01:10:08 +0000108 }
109
Michael Ilseman336cb792012-10-09 16:57:38 +0000110 if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
111 Value *LHS = CI->getOperand(0);
112 Value *RHS = CI->getOperand(1);
113 CmpInst::Predicate Pred = CI->getPredicate();
114 if (Inst->getOperand(0) > Inst->getOperand(1)) {
115 std::swap(LHS, RHS);
116 Pred = CI->getSwappedPredicate();
117 }
118 return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
119 }
120
121 if (CastInst *CI = dyn_cast<CastInst>(Inst))
122 return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0));
123
124 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst))
125 return hash_combine(EVI->getOpcode(), EVI->getOperand(0),
126 hash_combine_range(EVI->idx_begin(), EVI->idx_end()));
127
128 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst))
129 return hash_combine(IVI->getOpcode(), IVI->getOperand(0),
130 IVI->getOperand(1),
131 hash_combine_range(IVI->idx_begin(), IVI->idx_end()));
132
133 assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) ||
134 isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) ||
135 isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
Chandler Carruth7253bba2015-01-24 11:33:55 +0000136 isa<ShuffleVectorInst>(Inst)) &&
137 "Invalid/unknown instruction");
Michael Ilseman336cb792012-10-09 16:57:38 +0000138
Chris Lattner02a97762011-01-03 01:10:08 +0000139 // Mix in the opcode.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000140 return hash_combine(
141 Inst->getOpcode(),
142 hash_combine_range(Inst->value_op_begin(), Inst->value_op_end()));
Chris Lattner18ae5432011-01-02 23:04:14 +0000143}
144
Chris Lattner79d83062011-01-03 02:20:48 +0000145bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000146 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
147
148 if (LHS.isSentinel() || RHS.isSentinel())
149 return LHSI == RHSI;
Nadav Rotem465834c2012-07-24 10:51:42 +0000150
Chandler Carruth7253bba2015-01-24 11:33:55 +0000151 if (LHSI->getOpcode() != RHSI->getOpcode())
152 return false;
153 if (LHSI->isIdenticalTo(RHSI))
154 return true;
Michael Ilseman336cb792012-10-09 16:57:38 +0000155
156 // If we're not strictly identical, we still might be a commutable instruction
157 if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) {
158 if (!LHSBinOp->isCommutative())
159 return false;
160
Chandler Carruth7253bba2015-01-24 11:33:55 +0000161 assert(isa<BinaryOperator>(RHSI) &&
162 "same opcode, but different instruction type?");
Michael Ilseman336cb792012-10-09 16:57:38 +0000163 BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI);
164
165 // Check overflow attributes
166 if (isa<OverflowingBinaryOperator>(LHSBinOp)) {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000167 assert(isa<OverflowingBinaryOperator>(RHSBinOp) &&
168 "same opcode, but different operator type?");
Michael Ilseman336cb792012-10-09 16:57:38 +0000169 if (LHSBinOp->hasNoUnsignedWrap() != RHSBinOp->hasNoUnsignedWrap() ||
170 LHSBinOp->hasNoSignedWrap() != RHSBinOp->hasNoSignedWrap())
171 return false;
172 }
173
174 // Commuted equality
175 return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) &&
Chandler Carruth7253bba2015-01-24 11:33:55 +0000176 LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0);
Michael Ilseman336cb792012-10-09 16:57:38 +0000177 }
178 if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000179 assert(isa<CmpInst>(RHSI) &&
180 "same opcode, but different instruction type?");
Michael Ilseman336cb792012-10-09 16:57:38 +0000181 CmpInst *RHSCmp = cast<CmpInst>(RHSI);
182 // Commuted equality
183 return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) &&
Chandler Carruth7253bba2015-01-24 11:33:55 +0000184 LHSCmp->getOperand(1) == RHSCmp->getOperand(0) &&
185 LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate();
Michael Ilseman336cb792012-10-09 16:57:38 +0000186 }
187
188 return false;
Chris Lattner18ae5432011-01-02 23:04:14 +0000189}
190
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000191//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +0000192// CallValue
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000193//===----------------------------------------------------------------------===//
194
195namespace {
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000196/// \brief Struct representing the available call values in the scoped hash
197/// table.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000198struct CallValue {
199 Instruction *Inst;
Nadav Rotem465834c2012-07-24 10:51:42 +0000200
Chandler Carruth7253bba2015-01-24 11:33:55 +0000201 CallValue(Instruction *I) : Inst(I) {
202 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
203 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000204
Chandler Carruth7253bba2015-01-24 11:33:55 +0000205 bool isSentinel() const {
206 return Inst == DenseMapInfo<Instruction *>::getEmptyKey() ||
207 Inst == DenseMapInfo<Instruction *>::getTombstoneKey();
208 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000209
Chandler Carruth7253bba2015-01-24 11:33:55 +0000210 static bool canHandle(Instruction *Inst) {
211 // Don't value number anything that returns void.
212 if (Inst->getType()->isVoidTy())
213 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000214
Chandler Carruth7253bba2015-01-24 11:33:55 +0000215 CallInst *CI = dyn_cast<CallInst>(Inst);
216 if (!CI || !CI->onlyReadsMemory())
217 return false;
218 return true;
219 }
220};
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000221}
222
223namespace llvm {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000224template <> struct DenseMapInfo<CallValue> {
225 static inline CallValue getEmptyKey() {
226 return DenseMapInfo<Instruction *>::getEmptyKey();
227 }
228 static inline CallValue getTombstoneKey() {
229 return DenseMapInfo<Instruction *>::getTombstoneKey();
230 }
231 static unsigned getHashValue(CallValue Val);
232 static bool isEqual(CallValue LHS, CallValue RHS);
233};
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000234}
Chandler Carruth7253bba2015-01-24 11:33:55 +0000235
Chris Lattner92bb0f92011-01-03 03:41:27 +0000236unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000237 Instruction *Inst = Val.Inst;
Benjamin Kramer6ab86b12015-02-01 12:30:59 +0000238 // Hash all of the operands as pointers and mix in the opcode.
239 return hash_combine(
240 Inst->getOpcode(),
241 hash_combine_range(Inst->value_op_begin(), Inst->value_op_end()));
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000242}
243
Chris Lattner92bb0f92011-01-03 03:41:27 +0000244bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000245 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000246 if (LHS.isSentinel() || RHS.isSentinel())
247 return LHSI == RHSI;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000248 return LHSI->isIdenticalTo(RHSI);
249}
250
Chris Lattner79d83062011-01-03 02:20:48 +0000251//===----------------------------------------------------------------------===//
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000252// EarlyCSE implementation
Chris Lattner79d83062011-01-03 02:20:48 +0000253//===----------------------------------------------------------------------===//
254
Chris Lattner18ae5432011-01-02 23:04:14 +0000255namespace {
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000256/// \brief A simple and fast domtree-based CSE pass.
257///
258/// This pass does a simple depth-first walk over the dominator tree,
259/// eliminating trivially redundant instructions and using instsimplify to
260/// canonicalize things as it goes. It is intended to be fast and catch obvious
261/// cases so that instcombine and other passes are more effective. It is
262/// expected that a later pass of GVN will catch the interesting/hard cases.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000263class EarlyCSE {
Chris Lattner704541b2011-01-02 21:47:05 +0000264public:
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000265 Function &F;
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000266 const DataLayout *DL;
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000267 const TargetLibraryInfo &TLI;
268 const TargetTransformInfo &TTI;
269 DominatorTree &DT;
270 AssumptionCache &AC;
Chandler Carruth7253bba2015-01-24 11:33:55 +0000271 typedef RecyclingAllocator<
272 BumpPtrAllocator, ScopedHashTableVal<SimpleValue, Value *>> AllocatorTy;
273 typedef ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>,
Chris Lattnerd815f692011-01-03 01:42:46 +0000274 AllocatorTy> ScopedHTType;
Nadav Rotem465834c2012-07-24 10:51:42 +0000275
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000276 /// \brief A scoped hash table of the current values of all of our simple
277 /// scalar expressions.
278 ///
279 /// As we walk down the domtree, we look to see if instructions are in this:
280 /// if so, we replace them with what we find, otherwise we insert them so
281 /// that dominated values can succeed in their lookup.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000282 ScopedHTType AvailableValues;
Nadav Rotem465834c2012-07-24 10:51:42 +0000283
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000284 /// \brief A scoped hash table of the current values of loads.
285 ///
286 /// This allows us to get efficient access to dominating loads when we have
287 /// a fully redundant load. In addition to the most recent load, we keep
288 /// track of a generation count of the read, which is compared against the
289 /// current generation count. The current generation count is incremented
290 /// after every possibly writing memory operation, which ensures that we only
291 /// CSE loads with other loads that have no intervening store.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000292 typedef RecyclingAllocator<
293 BumpPtrAllocator,
294 ScopedHashTableVal<Value *, std::pair<Value *, unsigned>>>
295 LoadMapAllocator;
296 typedef ScopedHashTable<Value *, std::pair<Value *, unsigned>,
297 DenseMapInfo<Value *>, LoadMapAllocator> LoadHTType;
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000298 LoadHTType AvailableLoads;
Nadav Rotem465834c2012-07-24 10:51:42 +0000299
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000300 /// \brief A scoped hash table of the current values of read-only call
301 /// values.
302 ///
303 /// It uses the same generation count as loads.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000304 typedef ScopedHashTable<CallValue, std::pair<Value *, unsigned>> CallHTType;
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000305 CallHTType AvailableCalls;
Nadav Rotem465834c2012-07-24 10:51:42 +0000306
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000307 /// \brief This is the current generation of the memory value.
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000308 unsigned CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000309
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000310 /// \brief Set up the EarlyCSE runner for a particular function.
311 EarlyCSE(Function &F, const DataLayout *DL, const TargetLibraryInfo &TLI,
312 const TargetTransformInfo &TTI, DominatorTree &DT,
313 AssumptionCache &AC)
314 : F(F), DL(DL), TLI(TLI), TTI(TTI), DT(DT), AC(AC), CurrentGeneration(0) {
Chris Lattner704541b2011-01-02 21:47:05 +0000315 }
316
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000317 bool run();
Chris Lattner704541b2011-01-02 21:47:05 +0000318
319private:
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000320 // Almost a POD, but needs to call the constructors for the scoped hash
321 // tables so that a new scope gets pushed on. These are RAII so that the
322 // scope gets popped when the NodeScope is destroyed.
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000323 class NodeScope {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000324 public:
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000325 NodeScope(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads,
326 CallHTType &AvailableCalls)
327 : Scope(AvailableValues), LoadScope(AvailableLoads),
328 CallScope(AvailableCalls) {}
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000329
Chandler Carruth7253bba2015-01-24 11:33:55 +0000330 private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000331 NodeScope(const NodeScope &) = delete;
332 void operator=(const NodeScope &) = delete;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000333
334 ScopedHTType::ScopeTy Scope;
335 LoadHTType::ScopeTy LoadScope;
336 CallHTType::ScopeTy CallScope;
337 };
338
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000339 // Contains all the needed information to create a stack for doing a depth
340 // first tranversal of the tree. This includes scopes for values, loads, and
341 // calls as well as the generation. There is a child iterator so that the
342 // children do not need to be store spearately.
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000343 class StackNode {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000344 public:
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000345 StackNode(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads,
346 CallHTType &AvailableCalls, unsigned cg, DomTreeNode *n,
Chandler Carruth7253bba2015-01-24 11:33:55 +0000347 DomTreeNode::iterator child, DomTreeNode::iterator end)
348 : CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child),
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000349 EndIter(end), Scopes(AvailableValues, AvailableLoads, AvailableCalls),
Chandler Carruth7253bba2015-01-24 11:33:55 +0000350 Processed(false) {}
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000351
352 // Accessors.
353 unsigned currentGeneration() { return CurrentGeneration; }
354 unsigned childGeneration() { return ChildGeneration; }
355 void childGeneration(unsigned generation) { ChildGeneration = generation; }
356 DomTreeNode *node() { return Node; }
357 DomTreeNode::iterator childIter() { return ChildIter; }
358 DomTreeNode *nextChild() {
359 DomTreeNode *child = *ChildIter;
360 ++ChildIter;
361 return child;
362 }
363 DomTreeNode::iterator end() { return EndIter; }
364 bool isProcessed() { return Processed; }
365 void process() { Processed = true; }
366
Chandler Carruth7253bba2015-01-24 11:33:55 +0000367 private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000368 StackNode(const StackNode &) = delete;
369 void operator=(const StackNode &) = delete;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000370
371 // Members.
372 unsigned CurrentGeneration;
373 unsigned ChildGeneration;
374 DomTreeNode *Node;
375 DomTreeNode::iterator ChildIter;
376 DomTreeNode::iterator EndIter;
377 NodeScope Scopes;
378 bool Processed;
379 };
380
Chad Rosierf9327d62015-01-26 22:51:15 +0000381 /// \brief Wrapper class to handle memory instructions, including loads,
382 /// stores and intrinsic loads and stores defined by the target.
383 class ParseMemoryInst {
384 public:
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000385 ParseMemoryInst(Instruction *Inst, const TargetTransformInfo &TTI)
Chad Rosierf9327d62015-01-26 22:51:15 +0000386 : Load(false), Store(false), Vol(false), MayReadFromMemory(false),
387 MayWriteToMemory(false), MatchingId(-1), Ptr(nullptr) {
388 MayReadFromMemory = Inst->mayReadFromMemory();
389 MayWriteToMemory = Inst->mayWriteToMemory();
390 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
391 MemIntrinsicInfo Info;
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000392 if (!TTI.getTgtMemIntrinsic(II, Info))
Chad Rosierf9327d62015-01-26 22:51:15 +0000393 return;
394 if (Info.NumMemRefs == 1) {
395 Store = Info.WriteMem;
396 Load = Info.ReadMem;
397 MatchingId = Info.MatchingId;
398 MayReadFromMemory = Info.ReadMem;
399 MayWriteToMemory = Info.WriteMem;
400 Vol = Info.Vol;
401 Ptr = Info.PtrVal;
402 }
403 } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
404 Load = true;
405 Vol = !LI->isSimple();
406 Ptr = LI->getPointerOperand();
407 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
408 Store = true;
409 Vol = !SI->isSimple();
410 Ptr = SI->getPointerOperand();
411 }
412 }
413 bool isLoad() { return Load; }
414 bool isStore() { return Store; }
415 bool isVolatile() { return Vol; }
416 bool isMatchingMemLoc(const ParseMemoryInst &Inst) {
417 return Ptr == Inst.Ptr && MatchingId == Inst.MatchingId;
418 }
419 bool isValid() { return Ptr != nullptr; }
420 int getMatchingId() { return MatchingId; }
421 Value *getPtr() { return Ptr; }
422 bool mayReadFromMemory() { return MayReadFromMemory; }
423 bool mayWriteToMemory() { return MayWriteToMemory; }
424
425 private:
426 bool Load;
427 bool Store;
428 bool Vol;
429 bool MayReadFromMemory;
430 bool MayWriteToMemory;
431 // For regular (non-intrinsic) loads/stores, this is set to -1. For
432 // intrinsic loads/stores, the id is retrieved from the corresponding
433 // field in the MemIntrinsicInfo structure. That field contains
434 // non-negative values only.
435 int MatchingId;
436 Value *Ptr;
437 };
438
Chris Lattner18ae5432011-01-02 23:04:14 +0000439 bool processNode(DomTreeNode *Node);
Nadav Rotem465834c2012-07-24 10:51:42 +0000440
Chad Rosierf9327d62015-01-26 22:51:15 +0000441 Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const {
442 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
443 return LI;
444 else if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
445 return SI->getValueOperand();
446 assert(isa<IntrinsicInst>(Inst) && "Instruction not supported");
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000447 return TTI.getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst),
448 ExpectedType);
Chad Rosierf9327d62015-01-26 22:51:15 +0000449 }
Chris Lattner704541b2011-01-02 21:47:05 +0000450};
451}
452
Chris Lattner18ae5432011-01-02 23:04:14 +0000453bool EarlyCSE::processNode(DomTreeNode *Node) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000454 BasicBlock *BB = Node->getBlock();
Nadav Rotem465834c2012-07-24 10:51:42 +0000455
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000456 // If this block has a single predecessor, then the predecessor is the parent
457 // of the domtree node and all of the live out memory values are still current
458 // in this block. If this block has multiple predecessors, then they could
459 // have invalidated the live-out memory values of our parent value. For now,
460 // just be conservative and invalidate memory if this block has multiple
461 // predecessors.
Craig Topperf40110f2014-04-25 05:29:35 +0000462 if (!BB->getSinglePredecessor())
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000463 ++CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000464
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000465 /// LastStore - Keep track of the last non-volatile store that we saw... for
466 /// as long as there in no instruction that reads memory. If we see a store
467 /// to the same location, we delete the dead store. This zaps trivial dead
468 /// stores which can occur in bitfield code among other things.
Chad Rosierf9327d62015-01-26 22:51:15 +0000469 Instruction *LastStore = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +0000470
Chris Lattner18ae5432011-01-02 23:04:14 +0000471 bool Changed = false;
472
473 // See if any instructions in the block can be eliminated. If so, do it. If
474 // not, add them to AvailableValues.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000475 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000476 Instruction *Inst = I++;
Nadav Rotem465834c2012-07-24 10:51:42 +0000477
Chris Lattner18ae5432011-01-02 23:04:14 +0000478 // Dead instructions should just be removed.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000479 if (isInstructionTriviallyDead(Inst, &TLI)) {
Chris Lattner8fac5db2011-01-02 23:19:45 +0000480 DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n');
Chris Lattner18ae5432011-01-02 23:04:14 +0000481 Inst->eraseFromParent();
482 Changed = true;
Chris Lattner8fac5db2011-01-02 23:19:45 +0000483 ++NumSimplify;
Chris Lattner18ae5432011-01-02 23:04:14 +0000484 continue;
485 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000486
Hal Finkel1e16fa32014-11-03 20:21:32 +0000487 // Skip assume intrinsics, they don't really have side effects (although
488 // they're marked as such to ensure preservation of control dependencies),
489 // and this pass will not disturb any of the assumption's control
490 // dependencies.
491 if (match(Inst, m_Intrinsic<Intrinsic::assume>())) {
492 DEBUG(dbgs() << "EarlyCSE skipping assumption: " << *Inst << '\n');
493 continue;
494 }
495
Chris Lattner18ae5432011-01-02 23:04:14 +0000496 // If the instruction can be simplified (e.g. X+0 = X) then replace it with
497 // its simpler value.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000498 if (Value *V = SimplifyInstruction(Inst, DL, &TLI, &DT, &AC)) {
Chris Lattner8fac5db2011-01-02 23:19:45 +0000499 DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
Chris Lattner18ae5432011-01-02 23:04:14 +0000500 Inst->replaceAllUsesWith(V);
501 Inst->eraseFromParent();
502 Changed = true;
Chris Lattner8fac5db2011-01-02 23:19:45 +0000503 ++NumSimplify;
Chris Lattner18ae5432011-01-02 23:04:14 +0000504 continue;
505 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000506
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000507 // If this is a simple instruction that we can value number, process it.
508 if (SimpleValue::canHandle(Inst)) {
509 // See if the instruction has an available value. If so, use it.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000510 if (Value *V = AvailableValues.lookup(Inst)) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000511 DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
512 Inst->replaceAllUsesWith(V);
513 Inst->eraseFromParent();
514 Changed = true;
515 ++NumCSE;
516 continue;
517 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000518
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000519 // Otherwise, just remember that this value is available.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000520 AvailableValues.insert(Inst, Inst);
Chris Lattner18ae5432011-01-02 23:04:14 +0000521 continue;
522 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000523
Chad Rosierf9327d62015-01-26 22:51:15 +0000524 ParseMemoryInst MemInst(Inst, TTI);
Chris Lattner92bb0f92011-01-03 03:41:27 +0000525 // If this is a non-volatile load, process it.
Chad Rosierf9327d62015-01-26 22:51:15 +0000526 if (MemInst.isValid() && MemInst.isLoad()) {
Chris Lattner92bb0f92011-01-03 03:41:27 +0000527 // Ignore volatile loads.
Chad Rosierf9327d62015-01-26 22:51:15 +0000528 if (MemInst.isVolatile()) {
Craig Topperf40110f2014-04-25 05:29:35 +0000529 LastStore = nullptr;
David Majnemer76793002015-02-10 23:09:43 +0000530 // Don't CSE across synchronization boundaries.
531 if (Inst->mayWriteToMemory())
532 ++CurrentGeneration;
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000533 continue;
534 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000535
Chris Lattner92bb0f92011-01-03 03:41:27 +0000536 // If we have an available version of this load, and if it is the right
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000537 // generation, replace this instruction.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000538 std::pair<Value *, unsigned> InVal =
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000539 AvailableLoads.lookup(MemInst.getPtr());
Craig Topperf40110f2014-04-25 05:29:35 +0000540 if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
Chad Rosierf9327d62015-01-26 22:51:15 +0000541 Value *Op = getOrCreateResult(InVal.first, Inst->getType());
542 if (Op != nullptr) {
543 DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst
544 << " to: " << *InVal.first << '\n');
545 if (!Inst->use_empty())
546 Inst->replaceAllUsesWith(Op);
547 Inst->eraseFromParent();
548 Changed = true;
549 ++NumCSELoad;
550 continue;
551 }
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000552 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000553
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000554 // Otherwise, remember that we have this instruction.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000555 AvailableLoads.insert(MemInst.getPtr(), std::pair<Value *, unsigned>(
556 Inst, CurrentGeneration));
Craig Topperf40110f2014-04-25 05:29:35 +0000557 LastStore = nullptr;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000558 continue;
559 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000560
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000561 // If this instruction may read from memory, forget LastStore.
Chad Rosierf9327d62015-01-26 22:51:15 +0000562 // Load/store intrinsics will indicate both a read and a write to
563 // memory. The target may override this (e.g. so that a store intrinsic
564 // does not read from memory, and thus will be treated the same as a
565 // regular store for commoning purposes).
566 if (Inst->mayReadFromMemory() &&
567 !(MemInst.isValid() && !MemInst.mayReadFromMemory()))
Craig Topperf40110f2014-04-25 05:29:35 +0000568 LastStore = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +0000569
Chris Lattner92bb0f92011-01-03 03:41:27 +0000570 // If this is a read-only call, process it.
571 if (CallValue::canHandle(Inst)) {
572 // If we have an available version of this call, and if it is the right
573 // generation, replace this instruction.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000574 std::pair<Value *, unsigned> InVal = AvailableCalls.lookup(Inst);
Craig Topperf40110f2014-04-25 05:29:35 +0000575 if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000576 DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst
577 << " to: " << *InVal.first << '\n');
578 if (!Inst->use_empty())
579 Inst->replaceAllUsesWith(InVal.first);
Chris Lattner92bb0f92011-01-03 03:41:27 +0000580 Inst->eraseFromParent();
581 Changed = true;
582 ++NumCSECall;
583 continue;
584 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000585
Chris Lattner92bb0f92011-01-03 03:41:27 +0000586 // Otherwise, remember that we have this instruction.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000587 AvailableCalls.insert(
Chandler Carruth7253bba2015-01-24 11:33:55 +0000588 Inst, std::pair<Value *, unsigned>(Inst, CurrentGeneration));
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000589 continue;
590 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000591
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000592 // Okay, this isn't something we can CSE at all. Check to see if it is
593 // something that could modify memory. If so, our available memory values
594 // cannot be used so bump the generation count.
Chris Lattnere0e32a92011-01-03 03:46:34 +0000595 if (Inst->mayWriteToMemory()) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000596 ++CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000597
Chad Rosierf9327d62015-01-26 22:51:15 +0000598 if (MemInst.isValid() && MemInst.isStore()) {
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000599 // We do a trivial form of DSE if there are two stores to the same
600 // location with no intervening loads. Delete the earlier store.
Chad Rosierf9327d62015-01-26 22:51:15 +0000601 if (LastStore) {
602 ParseMemoryInst LastStoreMemInst(LastStore, TTI);
603 if (LastStoreMemInst.isMatchingMemLoc(MemInst)) {
604 DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore
605 << " due to: " << *Inst << '\n');
606 LastStore->eraseFromParent();
607 Changed = true;
608 ++NumDSE;
609 LastStore = nullptr;
610 }
Philip Reames018dbf12014-11-18 17:46:32 +0000611 // fallthrough - we can exploit information about this store
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000612 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000613
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000614 // Okay, we just invalidated anything we knew about loaded values. Try
615 // to salvage *something* by remembering that the stored value is a live
616 // version of the pointer. It is safe to forward from volatile stores
617 // to non-volatile loads, so we don't have to check for volatility of
618 // the store.
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000619 AvailableLoads.insert(MemInst.getPtr(), std::pair<Value *, unsigned>(
620 Inst, CurrentGeneration));
Nadav Rotem465834c2012-07-24 10:51:42 +0000621
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000622 // Remember that this was the last store we saw for DSE.
Chad Rosierf9327d62015-01-26 22:51:15 +0000623 if (!MemInst.isVolatile())
624 LastStore = Inst;
Chris Lattnere0e32a92011-01-03 03:46:34 +0000625 }
626 }
Chris Lattner18ae5432011-01-02 23:04:14 +0000627 }
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000628
Chris Lattner18ae5432011-01-02 23:04:14 +0000629 return Changed;
Chris Lattner704541b2011-01-02 21:47:05 +0000630}
Chris Lattner18ae5432011-01-02 23:04:14 +0000631
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000632bool EarlyCSE::run() {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000633 // Note, deque is being used here because there is significant performance
634 // gains over vector when the container becomes very large due to the
635 // specific access patterns. For more information see the mailing list
636 // discussion on this:
Lenny Maiorani9eefc812014-09-20 13:29:20 +0000637 // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html
638 std::deque<StackNode *> nodesToProcess;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000639
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000640 bool Changed = false;
641
642 // Process the root node.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000643 nodesToProcess.push_back(new StackNode(
644 AvailableValues, AvailableLoads, AvailableCalls, CurrentGeneration,
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000645 DT.getRootNode(), DT.getRootNode()->begin(), DT.getRootNode()->end()));
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000646
647 // Save the current generation.
648 unsigned LiveOutGeneration = CurrentGeneration;
649
650 // Process the stack.
651 while (!nodesToProcess.empty()) {
652 // Grab the first item off the stack. Set the current generation, remove
653 // the node from the stack, and process it.
Michael Gottesman2bf01732013-12-05 18:42:12 +0000654 StackNode *NodeToProcess = nodesToProcess.back();
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000655
656 // Initialize class members.
657 CurrentGeneration = NodeToProcess->currentGeneration();
658
659 // Check if the node needs to be processed.
660 if (!NodeToProcess->isProcessed()) {
661 // Process the node.
662 Changed |= processNode(NodeToProcess->node());
663 NodeToProcess->childGeneration(CurrentGeneration);
664 NodeToProcess->process();
665 } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
666 // Push the next child onto the stack.
667 DomTreeNode *child = NodeToProcess->nextChild();
Michael Gottesman2bf01732013-12-05 18:42:12 +0000668 nodesToProcess.push_back(
Chandler Carruth7253bba2015-01-24 11:33:55 +0000669 new StackNode(AvailableValues, AvailableLoads, AvailableCalls,
670 NodeToProcess->childGeneration(), child, child->begin(),
671 child->end()));
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000672 } else {
673 // It has been processed, and there are no more children to process,
674 // so delete it and pop it off the stack.
675 delete NodeToProcess;
Michael Gottesman2bf01732013-12-05 18:42:12 +0000676 nodesToProcess.pop_back();
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000677 }
678 } // while (!nodes...)
679
680 // Reset the current generation.
681 CurrentGeneration = LiveOutGeneration;
682
683 return Changed;
Chris Lattner18ae5432011-01-02 23:04:14 +0000684}
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000685
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000686PreservedAnalyses EarlyCSEPass::run(Function &F,
687 AnalysisManager<Function> *AM) {
688 const DataLayout *DL = F.getParent()->getDataLayout();
689
690 auto &TLI = AM->getResult<TargetLibraryAnalysis>(F);
691 auto &TTI = AM->getResult<TargetIRAnalysis>(F);
692 auto &DT = AM->getResult<DominatorTreeAnalysis>(F);
693 auto &AC = AM->getResult<AssumptionAnalysis>(F);
694
695 EarlyCSE CSE(F, DL, TLI, TTI, DT, AC);
696
697 if (!CSE.run())
698 return PreservedAnalyses::all();
699
700 // CSE preserves the dominator tree because it doesn't mutate the CFG.
701 // FIXME: Bundle this with other CFG-preservation.
702 PreservedAnalyses PA;
703 PA.preserve<DominatorTreeAnalysis>();
704 return PA;
705}
706
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000707namespace {
708/// \brief A simple and fast domtree-based CSE pass.
709///
710/// This pass does a simple depth-first walk over the dominator tree,
711/// eliminating trivially redundant instructions and using instsimplify to
712/// canonicalize things as it goes. It is intended to be fast and catch obvious
713/// cases so that instcombine and other passes are more effective. It is
714/// expected that a later pass of GVN will catch the interesting/hard cases.
715class EarlyCSELegacyPass : public FunctionPass {
716public:
717 static char ID;
718
719 EarlyCSELegacyPass() : FunctionPass(ID) {
720 initializeEarlyCSELegacyPassPass(*PassRegistry::getPassRegistry());
721 }
722
723 bool runOnFunction(Function &F) override {
724 if (skipOptnoneFunction(F))
725 return false;
726
727 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
728 auto *DL = DLP ? &DLP->getDataLayout() : nullptr;
729 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruthfdb9c572015-02-01 12:01:35 +0000730 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000731 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
732 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
733
734 EarlyCSE CSE(F, DL, TLI, TTI, DT, AC);
735
736 return CSE.run();
737 }
738
739 void getAnalysisUsage(AnalysisUsage &AU) const override {
740 AU.addRequired<AssumptionCacheTracker>();
741 AU.addRequired<DominatorTreeWrapperPass>();
742 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth705b1852015-01-31 03:43:40 +0000743 AU.addRequired<TargetTransformInfoWrapperPass>();
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000744 AU.setPreservesCFG();
745 }
746};
747}
748
749char EarlyCSELegacyPass::ID = 0;
750
751FunctionPass *llvm::createEarlyCSEPass() { return new EarlyCSELegacyPass(); }
752
753INITIALIZE_PASS_BEGIN(EarlyCSELegacyPass, "early-cse", "Early CSE", false,
754 false)
Chandler Carruth705b1852015-01-31 03:43:40 +0000755INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Chandler Carruthd649c0a2015-01-27 01:34:14 +0000756INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
757INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
758INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
759INITIALIZE_PASS_END(EarlyCSELegacyPass, "early-cse", "Early CSE", false, false)