blob: fd6ad19f3d8fd34ce591f55b87d937388e4c455d [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
Chris Lattner704541b2011-01-02 21:47:05 +000015#include "llvm/Transforms/Scalar.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 Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Transforms/Utils/Local.h"
Lenny Maiorani9eefc812014-09-20 13:29:20 +000032#include <deque>
Chris Lattner704541b2011-01-02 21:47:05 +000033using namespace llvm;
Hal Finkel1e16fa32014-11-03 20:21:32 +000034using namespace llvm::PatternMatch;
Chris Lattner704541b2011-01-02 21:47:05 +000035
Chandler Carruth964daaa2014-04-22 02:55:47 +000036#define DEBUG_TYPE "early-cse"
37
Chris Lattner4cb36542011-01-03 03:28:23 +000038STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd");
39STATISTIC(NumCSE, "Number of instructions CSE'd");
Chris Lattner92bb0f92011-01-03 03:41:27 +000040STATISTIC(NumCSELoad, "Number of load instructions CSE'd");
41STATISTIC(NumCSECall, "Number of call instructions CSE'd");
Chris Lattner9e5e9ed2011-01-03 04:17:24 +000042STATISTIC(NumDSE, "Number of trivial dead stores removed");
Chris Lattnerb9a8efc2011-01-03 03:18:43 +000043
44static unsigned getHash(const void *V) {
45 return DenseMapInfo<const void*>::getHashValue(V);
46}
Chris Lattner8fac5db2011-01-02 23:19:45 +000047
Chris Lattner79d83062011-01-03 02:20:48 +000048//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +000049// SimpleValue
Chris Lattner79d83062011-01-03 02:20:48 +000050//===----------------------------------------------------------------------===//
51
Chris Lattner704541b2011-01-02 21:47:05 +000052namespace {
Chandler Carruth9dea5cd2015-01-24 11:44:32 +000053/// \brief Struct representing the available values in the scoped hash table.
Chandler Carruth7253bba2015-01-24 11:33:55 +000054struct SimpleValue {
55 Instruction *Inst;
Nadav Rotem465834c2012-07-24 10:51:42 +000056
Chandler Carruth7253bba2015-01-24 11:33:55 +000057 SimpleValue(Instruction *I) : Inst(I) {
58 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
59 }
Nadav Rotem465834c2012-07-24 10:51:42 +000060
Chandler Carruth7253bba2015-01-24 11:33:55 +000061 bool isSentinel() const {
62 return Inst == DenseMapInfo<Instruction *>::getEmptyKey() ||
63 Inst == DenseMapInfo<Instruction *>::getTombstoneKey();
64 }
Nadav Rotem465834c2012-07-24 10:51:42 +000065
Chandler Carruth7253bba2015-01-24 11:33:55 +000066 static bool canHandle(Instruction *Inst) {
67 // This can only handle non-void readnone functions.
68 if (CallInst *CI = dyn_cast<CallInst>(Inst))
69 return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy();
70 return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) ||
71 isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) ||
72 isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) ||
73 isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) ||
74 isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst);
75 }
76};
Chris Lattner18ae5432011-01-02 23:04:14 +000077}
78
79namespace llvm {
Chandler Carruth7253bba2015-01-24 11:33:55 +000080template <> struct DenseMapInfo<SimpleValue> {
Chris Lattner79d83062011-01-03 02:20:48 +000081 static inline SimpleValue getEmptyKey() {
Chandler Carruth7253bba2015-01-24 11:33:55 +000082 return DenseMapInfo<Instruction *>::getEmptyKey();
Chris Lattner18ae5432011-01-02 23:04:14 +000083 }
Chris Lattner79d83062011-01-03 02:20:48 +000084 static inline SimpleValue getTombstoneKey() {
Chandler Carruth7253bba2015-01-24 11:33:55 +000085 return DenseMapInfo<Instruction *>::getTombstoneKey();
Chris Lattner18ae5432011-01-02 23:04:14 +000086 }
Chris Lattner79d83062011-01-03 02:20:48 +000087 static unsigned getHashValue(SimpleValue Val);
88 static bool isEqual(SimpleValue LHS, SimpleValue RHS);
Chris Lattner18ae5432011-01-02 23:04:14 +000089};
90}
91
Chris Lattner79d83062011-01-03 02:20:48 +000092unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
Chris Lattner18ae5432011-01-02 23:04:14 +000093 Instruction *Inst = Val.Inst;
Chris Lattner02a97762011-01-03 01:10:08 +000094 // Hash in all of the operands as pointers.
Chandler Carruth7253bba2015-01-24 11:33:55 +000095 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst)) {
Michael Ilseman336cb792012-10-09 16:57:38 +000096 Value *LHS = BinOp->getOperand(0);
97 Value *RHS = BinOp->getOperand(1);
98 if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1))
99 std::swap(LHS, RHS);
Chris Lattner02a97762011-01-03 01:10:08 +0000100
Michael Ilseman336cb792012-10-09 16:57:38 +0000101 if (isa<OverflowingBinaryOperator>(BinOp)) {
102 // Hash the overflow behavior
103 unsigned Overflow =
Chandler Carruth7253bba2015-01-24 11:33:55 +0000104 BinOp->hasNoSignedWrap() * OverflowingBinaryOperator::NoSignedWrap |
105 BinOp->hasNoUnsignedWrap() *
106 OverflowingBinaryOperator::NoUnsignedWrap;
Michael Ilseman336cb792012-10-09 16:57:38 +0000107 return hash_combine(BinOp->getOpcode(), Overflow, LHS, RHS);
108 }
109
110 return hash_combine(BinOp->getOpcode(), LHS, RHS);
Chris Lattner02a97762011-01-03 01:10:08 +0000111 }
112
Michael Ilseman336cb792012-10-09 16:57:38 +0000113 if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
114 Value *LHS = CI->getOperand(0);
115 Value *RHS = CI->getOperand(1);
116 CmpInst::Predicate Pred = CI->getPredicate();
117 if (Inst->getOperand(0) > Inst->getOperand(1)) {
118 std::swap(LHS, RHS);
119 Pred = CI->getSwappedPredicate();
120 }
121 return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
122 }
123
124 if (CastInst *CI = dyn_cast<CastInst>(Inst))
125 return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0));
126
127 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst))
128 return hash_combine(EVI->getOpcode(), EVI->getOperand(0),
129 hash_combine_range(EVI->idx_begin(), EVI->idx_end()));
130
131 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst))
132 return hash_combine(IVI->getOpcode(), IVI->getOperand(0),
133 IVI->getOperand(1),
134 hash_combine_range(IVI->idx_begin(), IVI->idx_end()));
135
136 assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) ||
137 isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) ||
138 isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
Chandler Carruth7253bba2015-01-24 11:33:55 +0000139 isa<ShuffleVectorInst>(Inst)) &&
140 "Invalid/unknown instruction");
Michael Ilseman336cb792012-10-09 16:57:38 +0000141
Chris Lattner02a97762011-01-03 01:10:08 +0000142 // Mix in the opcode.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000143 return hash_combine(
144 Inst->getOpcode(),
145 hash_combine_range(Inst->value_op_begin(), Inst->value_op_end()));
Chris Lattner18ae5432011-01-02 23:04:14 +0000146}
147
Chris Lattner79d83062011-01-03 02:20:48 +0000148bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000149 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
150
151 if (LHS.isSentinel() || RHS.isSentinel())
152 return LHSI == RHSI;
Nadav Rotem465834c2012-07-24 10:51:42 +0000153
Chandler Carruth7253bba2015-01-24 11:33:55 +0000154 if (LHSI->getOpcode() != RHSI->getOpcode())
155 return false;
156 if (LHSI->isIdenticalTo(RHSI))
157 return true;
Michael Ilseman336cb792012-10-09 16:57:38 +0000158
159 // If we're not strictly identical, we still might be a commutable instruction
160 if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) {
161 if (!LHSBinOp->isCommutative())
162 return false;
163
Chandler Carruth7253bba2015-01-24 11:33:55 +0000164 assert(isa<BinaryOperator>(RHSI) &&
165 "same opcode, but different instruction type?");
Michael Ilseman336cb792012-10-09 16:57:38 +0000166 BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI);
167
168 // Check overflow attributes
169 if (isa<OverflowingBinaryOperator>(LHSBinOp)) {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000170 assert(isa<OverflowingBinaryOperator>(RHSBinOp) &&
171 "same opcode, but different operator type?");
Michael Ilseman336cb792012-10-09 16:57:38 +0000172 if (LHSBinOp->hasNoUnsignedWrap() != RHSBinOp->hasNoUnsignedWrap() ||
173 LHSBinOp->hasNoSignedWrap() != RHSBinOp->hasNoSignedWrap())
174 return false;
175 }
176
177 // Commuted equality
178 return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) &&
Chandler Carruth7253bba2015-01-24 11:33:55 +0000179 LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0);
Michael Ilseman336cb792012-10-09 16:57:38 +0000180 }
181 if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000182 assert(isa<CmpInst>(RHSI) &&
183 "same opcode, but different instruction type?");
Michael Ilseman336cb792012-10-09 16:57:38 +0000184 CmpInst *RHSCmp = cast<CmpInst>(RHSI);
185 // Commuted equality
186 return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) &&
Chandler Carruth7253bba2015-01-24 11:33:55 +0000187 LHSCmp->getOperand(1) == RHSCmp->getOperand(0) &&
188 LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate();
Michael Ilseman336cb792012-10-09 16:57:38 +0000189 }
190
191 return false;
Chris Lattner18ae5432011-01-02 23:04:14 +0000192}
193
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000194//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +0000195// CallValue
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000196//===----------------------------------------------------------------------===//
197
198namespace {
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000199/// \brief Struct representing the available call values in the scoped hash
200/// table.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000201struct CallValue {
202 Instruction *Inst;
Nadav Rotem465834c2012-07-24 10:51:42 +0000203
Chandler Carruth7253bba2015-01-24 11:33:55 +0000204 CallValue(Instruction *I) : Inst(I) {
205 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
206 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000207
Chandler Carruth7253bba2015-01-24 11:33:55 +0000208 bool isSentinel() const {
209 return Inst == DenseMapInfo<Instruction *>::getEmptyKey() ||
210 Inst == DenseMapInfo<Instruction *>::getTombstoneKey();
211 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000212
Chandler Carruth7253bba2015-01-24 11:33:55 +0000213 static bool canHandle(Instruction *Inst) {
214 // Don't value number anything that returns void.
215 if (Inst->getType()->isVoidTy())
216 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000217
Chandler Carruth7253bba2015-01-24 11:33:55 +0000218 CallInst *CI = dyn_cast<CallInst>(Inst);
219 if (!CI || !CI->onlyReadsMemory())
220 return false;
221 return true;
222 }
223};
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000224}
225
226namespace llvm {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000227template <> struct DenseMapInfo<CallValue> {
228 static inline CallValue getEmptyKey() {
229 return DenseMapInfo<Instruction *>::getEmptyKey();
230 }
231 static inline CallValue getTombstoneKey() {
232 return DenseMapInfo<Instruction *>::getTombstoneKey();
233 }
234 static unsigned getHashValue(CallValue Val);
235 static bool isEqual(CallValue LHS, CallValue RHS);
236};
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000237}
Chandler Carruth7253bba2015-01-24 11:33:55 +0000238
Chris Lattner92bb0f92011-01-03 03:41:27 +0000239unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000240 Instruction *Inst = Val.Inst;
241 // Hash in all of the operands as pointers.
242 unsigned Res = 0;
Chris Lattner16ca19f2011-01-03 18:43:03 +0000243 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) {
244 assert(!Inst->getOperand(i)->getType()->isMetadataTy() &&
245 "Cannot value number calls with metadata operands");
Eli Friedman154a9672011-10-12 22:00:26 +0000246 Res ^= getHash(Inst->getOperand(i)) << (i & 0xF);
Chris Lattner16ca19f2011-01-03 18:43:03 +0000247 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000248
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000249 // Mix in the opcode.
250 return (Res << 1) ^ Inst->getOpcode();
251}
252
Chris Lattner92bb0f92011-01-03 03:41:27 +0000253bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000254 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000255 if (LHS.isSentinel() || RHS.isSentinel())
256 return LHSI == RHSI;
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000257 return LHSI->isIdenticalTo(RHSI);
258}
259
Chris Lattner79d83062011-01-03 02:20:48 +0000260//===----------------------------------------------------------------------===//
Nadav Rotem465834c2012-07-24 10:51:42 +0000261// EarlyCSE pass.
Chris Lattner79d83062011-01-03 02:20:48 +0000262//===----------------------------------------------------------------------===//
263
Chris Lattner18ae5432011-01-02 23:04:14 +0000264namespace {
Nadav Rotem465834c2012-07-24 10:51:42 +0000265
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000266/// \brief A simple and fast domtree-based CSE pass.
267///
268/// This pass does a simple depth-first walk over the dominator tree,
269/// eliminating trivially redundant instructions and using instsimplify to
270/// canonicalize things as it goes. It is intended to be fast and catch obvious
271/// cases so that instcombine and other passes are more effective. It is
272/// expected that a later pass of GVN will catch the interesting/hard cases.
Chris Lattner704541b2011-01-02 21:47:05 +0000273class EarlyCSE : public FunctionPass {
274public:
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000275 const DataLayout *DL;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000276 const TargetLibraryInfo *TLI;
Chad Rosierf9327d62015-01-26 22:51:15 +0000277 const TargetTransformInfo *TTI;
Chris Lattner18ae5432011-01-02 23:04:14 +0000278 DominatorTree *DT;
Chandler Carruth66b31302015-01-04 12:03:27 +0000279 AssumptionCache *AC;
Chandler Carruth7253bba2015-01-24 11:33:55 +0000280 typedef RecyclingAllocator<
281 BumpPtrAllocator, ScopedHashTableVal<SimpleValue, Value *>> AllocatorTy;
282 typedef ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>,
Chris Lattnerd815f692011-01-03 01:42:46 +0000283 AllocatorTy> ScopedHTType;
Nadav Rotem465834c2012-07-24 10:51:42 +0000284
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000285 /// \brief A scoped hash table of the current values of all of our simple
286 /// scalar expressions.
287 ///
288 /// As we walk down the domtree, we look to see if instructions are in this:
289 /// if so, we replace them with what we find, otherwise we insert them so
290 /// that dominated values can succeed in their lookup.
Chris Lattner79d83062011-01-03 02:20:48 +0000291 ScopedHTType *AvailableValues;
Nadav Rotem465834c2012-07-24 10:51:42 +0000292
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000293 /// \brief A scoped hash table of the current values of loads.
294 ///
295 /// This allows us to get efficient access to dominating loads when we have
296 /// a fully redundant load. In addition to the most recent load, we keep
297 /// track of a generation count of the read, which is compared against the
298 /// current generation count. The current generation count is incremented
299 /// after every possibly writing memory operation, which ensures that we only
300 /// CSE loads with other loads that have no intervening store.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000301 typedef RecyclingAllocator<
302 BumpPtrAllocator,
303 ScopedHashTableVal<Value *, std::pair<Value *, unsigned>>>
304 LoadMapAllocator;
305 typedef ScopedHashTable<Value *, std::pair<Value *, unsigned>,
306 DenseMapInfo<Value *>, LoadMapAllocator> LoadHTType;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000307 LoadHTType *AvailableLoads;
Nadav Rotem465834c2012-07-24 10:51:42 +0000308
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000309 /// \brief A scoped hash table of the current values of read-only call
310 /// values.
311 ///
312 /// It uses the same generation count as loads.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000313 typedef ScopedHashTable<CallValue, std::pair<Value *, unsigned>> CallHTType;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000314 CallHTType *AvailableCalls;
Nadav Rotem465834c2012-07-24 10:51:42 +0000315
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000316 /// \brief This is the current generation of the memory value.
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000317 unsigned CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000318
Chris Lattner704541b2011-01-02 21:47:05 +0000319 static char ID;
Chris Lattner79d83062011-01-03 02:20:48 +0000320 explicit EarlyCSE() : FunctionPass(ID) {
Chris Lattner704541b2011-01-02 21:47:05 +0000321 initializeEarlyCSEPass(*PassRegistry::getPassRegistry());
322 }
323
Craig Topper3e4c6972014-03-05 09:10:37 +0000324 bool runOnFunction(Function &F) override;
Chris Lattner704541b2011-01-02 21:47:05 +0000325
326private:
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000327 // Almost a POD, but needs to call the constructors for the scoped hash
328 // tables so that a new scope gets pushed on. These are RAII so that the
329 // scope gets popped when the NodeScope is destroyed.
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000330 class NodeScope {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000331 public:
332 NodeScope(ScopedHTType *availableValues, LoadHTType *availableLoads,
333 CallHTType *availableCalls)
334 : Scope(*availableValues), LoadScope(*availableLoads),
335 CallScope(*availableCalls) {}
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000336
Chandler Carruth7253bba2015-01-24 11:33:55 +0000337 private:
338 NodeScope(const NodeScope &) LLVM_DELETED_FUNCTION;
339 void operator=(const NodeScope &) LLVM_DELETED_FUNCTION;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000340
341 ScopedHTType::ScopeTy Scope;
342 LoadHTType::ScopeTy LoadScope;
343 CallHTType::ScopeTy CallScope;
344 };
345
Chandler Carruth9dea5cd2015-01-24 11:44:32 +0000346 // Contains all the needed information to create a stack for doing a depth
347 // first tranversal of the tree. This includes scopes for values, loads, and
348 // calls as well as the generation. There is a child iterator so that the
349 // children do not need to be store spearately.
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000350 class StackNode {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000351 public:
352 StackNode(ScopedHTType *availableValues, LoadHTType *availableLoads,
353 CallHTType *availableCalls, unsigned cg, DomTreeNode *n,
354 DomTreeNode::iterator child, DomTreeNode::iterator end)
355 : CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child),
356 EndIter(end), Scopes(availableValues, availableLoads, availableCalls),
357 Processed(false) {}
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000358
359 // Accessors.
360 unsigned currentGeneration() { return CurrentGeneration; }
361 unsigned childGeneration() { return ChildGeneration; }
362 void childGeneration(unsigned generation) { ChildGeneration = generation; }
363 DomTreeNode *node() { return Node; }
364 DomTreeNode::iterator childIter() { return ChildIter; }
365 DomTreeNode *nextChild() {
366 DomTreeNode *child = *ChildIter;
367 ++ChildIter;
368 return child;
369 }
370 DomTreeNode::iterator end() { return EndIter; }
371 bool isProcessed() { return Processed; }
372 void process() { Processed = true; }
373
Chandler Carruth7253bba2015-01-24 11:33:55 +0000374 private:
375 StackNode(const StackNode &) LLVM_DELETED_FUNCTION;
376 void operator=(const StackNode &) LLVM_DELETED_FUNCTION;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000377
378 // Members.
379 unsigned CurrentGeneration;
380 unsigned ChildGeneration;
381 DomTreeNode *Node;
382 DomTreeNode::iterator ChildIter;
383 DomTreeNode::iterator EndIter;
384 NodeScope Scopes;
385 bool Processed;
386 };
387
Chad Rosierf9327d62015-01-26 22:51:15 +0000388 /// \brief Wrapper class to handle memory instructions, including loads,
389 /// stores and intrinsic loads and stores defined by the target.
390 class ParseMemoryInst {
391 public:
392 ParseMemoryInst(Instruction *Inst, const TargetTransformInfo *TTI)
393 : Load(false), Store(false), Vol(false), MayReadFromMemory(false),
394 MayWriteToMemory(false), MatchingId(-1), Ptr(nullptr) {
395 MayReadFromMemory = Inst->mayReadFromMemory();
396 MayWriteToMemory = Inst->mayWriteToMemory();
397 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
398 MemIntrinsicInfo Info;
399 if (!TTI->getTgtMemIntrinsic(II, Info))
400 return;
401 if (Info.NumMemRefs == 1) {
402 Store = Info.WriteMem;
403 Load = Info.ReadMem;
404 MatchingId = Info.MatchingId;
405 MayReadFromMemory = Info.ReadMem;
406 MayWriteToMemory = Info.WriteMem;
407 Vol = Info.Vol;
408 Ptr = Info.PtrVal;
409 }
410 } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
411 Load = true;
412 Vol = !LI->isSimple();
413 Ptr = LI->getPointerOperand();
414 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
415 Store = true;
416 Vol = !SI->isSimple();
417 Ptr = SI->getPointerOperand();
418 }
419 }
420 bool isLoad() { return Load; }
421 bool isStore() { return Store; }
422 bool isVolatile() { return Vol; }
423 bool isMatchingMemLoc(const ParseMemoryInst &Inst) {
424 return Ptr == Inst.Ptr && MatchingId == Inst.MatchingId;
425 }
426 bool isValid() { return Ptr != nullptr; }
427 int getMatchingId() { return MatchingId; }
428 Value *getPtr() { return Ptr; }
429 bool mayReadFromMemory() { return MayReadFromMemory; }
430 bool mayWriteToMemory() { return MayWriteToMemory; }
431
432 private:
433 bool Load;
434 bool Store;
435 bool Vol;
436 bool MayReadFromMemory;
437 bool MayWriteToMemory;
438 // For regular (non-intrinsic) loads/stores, this is set to -1. For
439 // intrinsic loads/stores, the id is retrieved from the corresponding
440 // field in the MemIntrinsicInfo structure. That field contains
441 // non-negative values only.
442 int MatchingId;
443 Value *Ptr;
444 };
445
Chris Lattner18ae5432011-01-02 23:04:14 +0000446 bool processNode(DomTreeNode *Node);
Nadav Rotem465834c2012-07-24 10:51:42 +0000447
Craig Topper3e4c6972014-03-05 09:10:37 +0000448 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth66b31302015-01-04 12:03:27 +0000449 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruth73523022014-01-13 13:07:17 +0000450 AU.addRequired<DominatorTreeWrapperPass>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000451 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosierf9327d62015-01-26 22:51:15 +0000452 AU.addRequired<TargetTransformInfo>();
Chris Lattner704541b2011-01-02 21:47:05 +0000453 AU.setPreservesCFG();
454 }
Chad Rosierf9327d62015-01-26 22:51:15 +0000455
456 Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const {
457 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
458 return LI;
459 else if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
460 return SI->getValueOperand();
461 assert(isa<IntrinsicInst>(Inst) && "Instruction not supported");
462 return TTI->getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst),
463 ExpectedType);
464 }
Chris Lattner704541b2011-01-02 21:47:05 +0000465};
466}
467
468char EarlyCSE::ID = 0;
469
Chandler Carruth7253bba2015-01-24 11:33:55 +0000470FunctionPass *llvm::createEarlyCSEPass() { return new EarlyCSE(); }
Chris Lattner704541b2011-01-02 21:47:05 +0000471
472INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +0000473INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth73523022014-01-13 13:07:17 +0000474INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000475INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chris Lattner704541b2011-01-02 21:47:05 +0000476INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
477
Chris Lattner18ae5432011-01-02 23:04:14 +0000478bool EarlyCSE::processNode(DomTreeNode *Node) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000479 BasicBlock *BB = Node->getBlock();
Nadav Rotem465834c2012-07-24 10:51:42 +0000480
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000481 // If this block has a single predecessor, then the predecessor is the parent
482 // of the domtree node and all of the live out memory values are still current
483 // in this block. If this block has multiple predecessors, then they could
484 // have invalidated the live-out memory values of our parent value. For now,
485 // just be conservative and invalidate memory if this block has multiple
486 // predecessors.
Craig Topperf40110f2014-04-25 05:29:35 +0000487 if (!BB->getSinglePredecessor())
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000488 ++CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000489
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000490 /// LastStore - Keep track of the last non-volatile store that we saw... for
491 /// as long as there in no instruction that reads memory. If we see a store
492 /// to the same location, we delete the dead store. This zaps trivial dead
493 /// stores which can occur in bitfield code among other things.
Chad Rosierf9327d62015-01-26 22:51:15 +0000494 Instruction *LastStore = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +0000495
Chris Lattner18ae5432011-01-02 23:04:14 +0000496 bool Changed = false;
497
498 // See if any instructions in the block can be eliminated. If so, do it. If
499 // not, add them to AvailableValues.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000500 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
Chris Lattner18ae5432011-01-02 23:04:14 +0000501 Instruction *Inst = I++;
Nadav Rotem465834c2012-07-24 10:51:42 +0000502
Chris Lattner18ae5432011-01-02 23:04:14 +0000503 // Dead instructions should just be removed.
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000504 if (isInstructionTriviallyDead(Inst, TLI)) {
Chris Lattner8fac5db2011-01-02 23:19:45 +0000505 DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n');
Chris Lattner18ae5432011-01-02 23:04:14 +0000506 Inst->eraseFromParent();
507 Changed = true;
Chris Lattner8fac5db2011-01-02 23:19:45 +0000508 ++NumSimplify;
Chris Lattner18ae5432011-01-02 23:04:14 +0000509 continue;
510 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000511
Hal Finkel1e16fa32014-11-03 20:21:32 +0000512 // Skip assume intrinsics, they don't really have side effects (although
513 // they're marked as such to ensure preservation of control dependencies),
514 // and this pass will not disturb any of the assumption's control
515 // dependencies.
516 if (match(Inst, m_Intrinsic<Intrinsic::assume>())) {
517 DEBUG(dbgs() << "EarlyCSE skipping assumption: " << *Inst << '\n');
518 continue;
519 }
520
Chris Lattner18ae5432011-01-02 23:04:14 +0000521 // If the instruction can be simplified (e.g. X+0 = X) then replace it with
522 // its simpler value.
Chandler Carruth66b31302015-01-04 12:03:27 +0000523 if (Value *V = SimplifyInstruction(Inst, DL, TLI, DT, AC)) {
Chris Lattner8fac5db2011-01-02 23:19:45 +0000524 DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
Chris Lattner18ae5432011-01-02 23:04:14 +0000525 Inst->replaceAllUsesWith(V);
526 Inst->eraseFromParent();
527 Changed = true;
Chris Lattner8fac5db2011-01-02 23:19:45 +0000528 ++NumSimplify;
Chris Lattner18ae5432011-01-02 23:04:14 +0000529 continue;
530 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000531
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000532 // If this is a simple instruction that we can value number, process it.
533 if (SimpleValue::canHandle(Inst)) {
534 // See if the instruction has an available value. If so, use it.
Chris Lattner4cb36542011-01-03 03:28:23 +0000535 if (Value *V = AvailableValues->lookup(Inst)) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000536 DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
537 Inst->replaceAllUsesWith(V);
538 Inst->eraseFromParent();
539 Changed = true;
540 ++NumCSE;
541 continue;
542 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000543
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000544 // Otherwise, just remember that this value is available.
Chris Lattner4cb36542011-01-03 03:28:23 +0000545 AvailableValues->insert(Inst, Inst);
Chris Lattner18ae5432011-01-02 23:04:14 +0000546 continue;
547 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000548
Chad Rosierf9327d62015-01-26 22:51:15 +0000549 ParseMemoryInst MemInst(Inst, TTI);
Chris Lattner92bb0f92011-01-03 03:41:27 +0000550 // If this is a non-volatile load, process it.
Chad Rosierf9327d62015-01-26 22:51:15 +0000551 if (MemInst.isValid() && MemInst.isLoad()) {
Chris Lattner92bb0f92011-01-03 03:41:27 +0000552 // Ignore volatile loads.
Chad Rosierf9327d62015-01-26 22:51:15 +0000553 if (MemInst.isVolatile()) {
Craig Topperf40110f2014-04-25 05:29:35 +0000554 LastStore = nullptr;
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000555 continue;
556 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000557
Chris Lattner92bb0f92011-01-03 03:41:27 +0000558 // If we have an available version of this load, and if it is the right
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000559 // generation, replace this instruction.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000560 std::pair<Value *, unsigned> InVal =
Chad Rosierf9327d62015-01-26 22:51:15 +0000561 AvailableLoads->lookup(MemInst.getPtr());
Craig Topperf40110f2014-04-25 05:29:35 +0000562 if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
Chad Rosierf9327d62015-01-26 22:51:15 +0000563 Value *Op = getOrCreateResult(InVal.first, Inst->getType());
564 if (Op != nullptr) {
565 DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst
566 << " to: " << *InVal.first << '\n');
567 if (!Inst->use_empty())
568 Inst->replaceAllUsesWith(Op);
569 Inst->eraseFromParent();
570 Changed = true;
571 ++NumCSELoad;
572 continue;
573 }
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000574 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000575
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000576 // Otherwise, remember that we have this instruction.
Chad Rosierf9327d62015-01-26 22:51:15 +0000577 AvailableLoads->insert(MemInst.getPtr(), std::pair<Value *, unsigned>(
578 Inst, CurrentGeneration));
Craig Topperf40110f2014-04-25 05:29:35 +0000579 LastStore = nullptr;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000580 continue;
581 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000582
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000583 // If this instruction may read from memory, forget LastStore.
Chad Rosierf9327d62015-01-26 22:51:15 +0000584 // Load/store intrinsics will indicate both a read and a write to
585 // memory. The target may override this (e.g. so that a store intrinsic
586 // does not read from memory, and thus will be treated the same as a
587 // regular store for commoning purposes).
588 if (Inst->mayReadFromMemory() &&
589 !(MemInst.isValid() && !MemInst.mayReadFromMemory()))
Craig Topperf40110f2014-04-25 05:29:35 +0000590 LastStore = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +0000591
Chris Lattner92bb0f92011-01-03 03:41:27 +0000592 // If this is a read-only call, process it.
593 if (CallValue::canHandle(Inst)) {
594 // If we have an available version of this call, and if it is the right
595 // generation, replace this instruction.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000596 std::pair<Value *, unsigned> InVal = AvailableCalls->lookup(Inst);
Craig Topperf40110f2014-04-25 05:29:35 +0000597 if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
Chandler Carruth7253bba2015-01-24 11:33:55 +0000598 DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst
599 << " to: " << *InVal.first << '\n');
600 if (!Inst->use_empty())
601 Inst->replaceAllUsesWith(InVal.first);
Chris Lattner92bb0f92011-01-03 03:41:27 +0000602 Inst->eraseFromParent();
603 Changed = true;
604 ++NumCSECall;
605 continue;
606 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000607
Chris Lattner92bb0f92011-01-03 03:41:27 +0000608 // Otherwise, remember that we have this instruction.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000609 AvailableCalls->insert(
610 Inst, std::pair<Value *, unsigned>(Inst, CurrentGeneration));
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000611 continue;
612 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000613
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000614 // Okay, this isn't something we can CSE at all. Check to see if it is
615 // something that could modify memory. If so, our available memory values
616 // cannot be used so bump the generation count.
Chris Lattnere0e32a92011-01-03 03:46:34 +0000617 if (Inst->mayWriteToMemory()) {
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000618 ++CurrentGeneration;
Nadav Rotem465834c2012-07-24 10:51:42 +0000619
Chad Rosierf9327d62015-01-26 22:51:15 +0000620 if (MemInst.isValid() && MemInst.isStore()) {
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000621 // We do a trivial form of DSE if there are two stores to the same
622 // location with no intervening loads. Delete the earlier store.
Chad Rosierf9327d62015-01-26 22:51:15 +0000623 if (LastStore) {
624 ParseMemoryInst LastStoreMemInst(LastStore, TTI);
625 if (LastStoreMemInst.isMatchingMemLoc(MemInst)) {
626 DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore
627 << " due to: " << *Inst << '\n');
628 LastStore->eraseFromParent();
629 Changed = true;
630 ++NumDSE;
631 LastStore = nullptr;
632 }
Philip Reames018dbf12014-11-18 17:46:32 +0000633 // fallthrough - we can exploit information about this store
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000634 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000635
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000636 // Okay, we just invalidated anything we knew about loaded values. Try
637 // to salvage *something* by remembering that the stored value is a live
638 // version of the pointer. It is safe to forward from volatile stores
639 // to non-volatile loads, so we don't have to check for volatility of
640 // the store.
Chad Rosierf9327d62015-01-26 22:51:15 +0000641 AvailableLoads->insert(MemInst.getPtr(), std::pair<Value *, unsigned>(
642 Inst, CurrentGeneration));
Nadav Rotem465834c2012-07-24 10:51:42 +0000643
Chris Lattner9e5e9ed2011-01-03 04:17:24 +0000644 // Remember that this was the last store we saw for DSE.
Chad Rosierf9327d62015-01-26 22:51:15 +0000645 if (!MemInst.isVolatile())
646 LastStore = Inst;
Chris Lattnere0e32a92011-01-03 03:46:34 +0000647 }
648 }
Chris Lattner18ae5432011-01-02 23:04:14 +0000649 }
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000650
Chris Lattner18ae5432011-01-02 23:04:14 +0000651 return Changed;
Chris Lattner704541b2011-01-02 21:47:05 +0000652}
Chris Lattner18ae5432011-01-02 23:04:14 +0000653
Chris Lattner18ae5432011-01-02 23:04:14 +0000654bool EarlyCSE::runOnFunction(Function &F) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000655 if (skipOptnoneFunction(F))
656 return false;
657
Chandler Carruth7253bba2015-01-24 11:33:55 +0000658 // Note, deque is being used here because there is significant performance
659 // gains over vector when the container becomes very large due to the
660 // specific access patterns. For more information see the mailing list
661 // discussion on this:
Lenny Maiorani9eefc812014-09-20 13:29:20 +0000662 // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html
663 std::deque<StackNode *> nodesToProcess;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000664
Rafael Espindola93512512014-02-25 17:30:31 +0000665 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +0000666 DL = DLP ? &DLP->getDataLayout() : nullptr;
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000667 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosierf9327d62015-01-26 22:51:15 +0000668 TTI = &getAnalysis<TargetTransformInfo>();
Chandler Carruth73523022014-01-13 13:07:17 +0000669 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruth66b31302015-01-04 12:03:27 +0000670 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Nadav Rotem465834c2012-07-24 10:51:42 +0000671
Chris Lattner92bb0f92011-01-03 03:41:27 +0000672 // Tables that the pass uses when walking the domtree.
Chris Lattnerd815f692011-01-03 01:42:46 +0000673 ScopedHTType AVTable;
Chris Lattner18ae5432011-01-02 23:04:14 +0000674 AvailableValues = &AVTable;
Chris Lattner92bb0f92011-01-03 03:41:27 +0000675 LoadHTType LoadTable;
676 AvailableLoads = &LoadTable;
677 CallHTType CallTable;
678 AvailableCalls = &CallTable;
Nadav Rotem465834c2012-07-24 10:51:42 +0000679
Chris Lattnerb9a8efc2011-01-03 03:18:43 +0000680 CurrentGeneration = 0;
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000681 bool Changed = false;
682
683 // Process the root node.
Chandler Carruth7253bba2015-01-24 11:33:55 +0000684 nodesToProcess.push_back(new StackNode(
685 AvailableValues, AvailableLoads, AvailableCalls, CurrentGeneration,
686 DT->getRootNode(), DT->getRootNode()->begin(), DT->getRootNode()->end()));
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000687
688 // Save the current generation.
689 unsigned LiveOutGeneration = CurrentGeneration;
690
691 // Process the stack.
692 while (!nodesToProcess.empty()) {
693 // Grab the first item off the stack. Set the current generation, remove
694 // the node from the stack, and process it.
Michael Gottesman2bf01732013-12-05 18:42:12 +0000695 StackNode *NodeToProcess = nodesToProcess.back();
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000696
697 // Initialize class members.
698 CurrentGeneration = NodeToProcess->currentGeneration();
699
700 // Check if the node needs to be processed.
701 if (!NodeToProcess->isProcessed()) {
702 // Process the node.
703 Changed |= processNode(NodeToProcess->node());
704 NodeToProcess->childGeneration(CurrentGeneration);
705 NodeToProcess->process();
706 } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
707 // Push the next child onto the stack.
708 DomTreeNode *child = NodeToProcess->nextChild();
Michael Gottesman2bf01732013-12-05 18:42:12 +0000709 nodesToProcess.push_back(
Chandler Carruth7253bba2015-01-24 11:33:55 +0000710 new StackNode(AvailableValues, AvailableLoads, AvailableCalls,
711 NodeToProcess->childGeneration(), child, child->begin(),
712 child->end()));
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000713 } else {
714 // It has been processed, and there are no more children to process,
715 // so delete it and pop it off the stack.
716 delete NodeToProcess;
Michael Gottesman2bf01732013-12-05 18:42:12 +0000717 nodesToProcess.pop_back();
Lenny Maiorani8d670b82012-01-31 23:14:41 +0000718 }
719 } // while (!nodes...)
720
721 // Reset the current generation.
722 CurrentGeneration = LiveOutGeneration;
723
724 return Changed;
Chris Lattner18ae5432011-01-02 23:04:14 +0000725}