blob: f4fe15e0e525ae40c1499dc110a041173a070c75 [file] [log] [blame]
Chris Lattner72bc70d2008-12-05 07:49:08 +00001//===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
Owen Anderson1ad2cb72007-07-24 17:55:58 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Anderson1ad2cb72007-07-24 17:55:58 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs global value numbering to eliminate fully redundant
11// instructions. It also performs simple dead load elimination.
12//
John Criswell090c0a22009-03-10 15:04:53 +000013// Note that this pass does the value numbering itself; it does not use the
Matthijs Kooijman845f5242008-06-05 07:55:49 +000014// ValueNumbering analysis passes.
15//
Owen Anderson1ad2cb72007-07-24 17:55:58 +000016//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "gvn"
Owen Anderson1ad2cb72007-07-24 17:55:58 +000019#include "llvm/Transforms/Scalar.h"
Owen Anderson0cd32032007-07-25 19:57:03 +000020#include "llvm/BasicBlock.h"
Owen Anderson45537912007-07-26 18:26:51 +000021#include "llvm/Constants.h"
Owen Anderson1ad2cb72007-07-24 17:55:58 +000022#include "llvm/DerivedTypes.h"
Owen Anderson45537912007-07-26 18:26:51 +000023#include "llvm/Function.h"
Devang Patelc64bc162009-03-06 02:59:27 +000024#include "llvm/IntrinsicInst.h"
Owen Andersond672ecb2009-07-03 00:17:18 +000025#include "llvm/LLVMContext.h"
Owen Anderson45537912007-07-26 18:26:51 +000026#include "llvm/Value.h"
Owen Anderson1ad2cb72007-07-24 17:55:58 +000027#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/DepthFirstIterator.h"
Owen Anderson255dafc2008-12-15 02:03:00 +000029#include "llvm/ADT/PostOrderIterator.h"
Owen Anderson1ad2cb72007-07-24 17:55:58 +000030#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/Statistic.h"
Owen Andersonb388ca92007-10-18 19:39:33 +000033#include "llvm/Analysis/Dominators.h"
34#include "llvm/Analysis/AliasAnalysis.h"
Owen Anderson1ad2cb72007-07-24 17:55:58 +000035#include "llvm/Analysis/MemoryDependenceAnalysis.h"
36#include "llvm/Support/CFG.h"
Owen Andersonaa0b6342008-06-19 19:57:25 +000037#include "llvm/Support/CommandLine.h"
Owen Anderson1ad2cb72007-07-24 17:55:58 +000038#include "llvm/Support/Compiler.h"
Chris Lattner9f8a6a72008-03-29 04:36:18 +000039#include "llvm/Support/Debug.h"
Owen Anderson5c274ee2008-06-19 19:54:19 +000040#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dale Johannesen42c3f552009-06-17 20:48:23 +000041#include "llvm/Transforms/Utils/Local.h"
Duncan Sands4520dd22008-10-08 07:23:46 +000042#include <cstdio>
Owen Anderson1ad2cb72007-07-24 17:55:58 +000043using namespace llvm;
44
Bill Wendling70ded192008-12-22 22:14:07 +000045STATISTIC(NumGVNInstr, "Number of instructions deleted");
46STATISTIC(NumGVNLoad, "Number of loads deleted");
47STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
Owen Anderson961edc82008-07-15 16:28:06 +000048STATISTIC(NumGVNBlocks, "Number of blocks merged");
Bill Wendling70ded192008-12-22 22:14:07 +000049STATISTIC(NumPRELoad, "Number of loads PRE'd");
Chris Lattnerd27290d2008-03-22 04:13:49 +000050
Evan Cheng88d11c02008-06-20 01:01:07 +000051static cl::opt<bool> EnablePRE("enable-pre",
Owen Andersonc2b856e2008-07-17 19:41:00 +000052 cl::init(true), cl::Hidden);
Dan Gohmanc915c952009-06-15 18:30:15 +000053static cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true));
Owen Andersonaa0b6342008-06-19 19:57:25 +000054
Owen Anderson1ad2cb72007-07-24 17:55:58 +000055//===----------------------------------------------------------------------===//
56// ValueTable Class
57//===----------------------------------------------------------------------===//
58
59/// This class holds the mapping between values and value numbers. It is used
60/// as an efficient mechanism to determine the expression-wise equivalence of
61/// two values.
62namespace {
63 struct VISIBILITY_HIDDEN Expression {
Dan Gohmanae3a0be2009-06-04 22:49:04 +000064 enum ExpressionOpcode { ADD, FADD, SUB, FSUB, MUL, FMUL,
65 UDIV, SDIV, FDIV, UREM, SREM,
Owen Anderson1ad2cb72007-07-24 17:55:58 +000066 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
67 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
68 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
69 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
70 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
71 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
72 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
73 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
Owen Anderson3b3f58c2008-05-13 08:17:22 +000074 PTRTOINT, INTTOPTR, BITCAST, GEP, CALL, CONSTANT,
Owen Anderson3cd8eb32008-06-19 17:25:39 +000075 EMPTY, TOMBSTONE };
Owen Anderson1ad2cb72007-07-24 17:55:58 +000076
77 ExpressionOpcode opcode;
78 const Type* type;
79 uint32_t firstVN;
80 uint32_t secondVN;
81 uint32_t thirdVN;
82 SmallVector<uint32_t, 4> varargs;
Owen Andersonb388ca92007-10-18 19:39:33 +000083 Value* function;
Owen Anderson1ad2cb72007-07-24 17:55:58 +000084
85 Expression() { }
86 Expression(ExpressionOpcode o) : opcode(o) { }
87
88 bool operator==(const Expression &other) const {
89 if (opcode != other.opcode)
90 return false;
91 else if (opcode == EMPTY || opcode == TOMBSTONE)
92 return true;
93 else if (type != other.type)
94 return false;
Owen Andersonb388ca92007-10-18 19:39:33 +000095 else if (function != other.function)
96 return false;
Owen Anderson1ad2cb72007-07-24 17:55:58 +000097 else if (firstVN != other.firstVN)
98 return false;
99 else if (secondVN != other.secondVN)
100 return false;
101 else if (thirdVN != other.thirdVN)
102 return false;
103 else {
104 if (varargs.size() != other.varargs.size())
105 return false;
106
107 for (size_t i = 0; i < varargs.size(); ++i)
108 if (varargs[i] != other.varargs[i])
109 return false;
110
111 return true;
112 }
113 }
114
115 bool operator!=(const Expression &other) const {
Bill Wendling75f02ee2008-12-22 22:16:31 +0000116 return !(*this == other);
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000117 }
118 };
119
120 class VISIBILITY_HIDDEN ValueTable {
121 private:
122 DenseMap<Value*, uint32_t> valueNumbering;
123 DenseMap<Expression, uint32_t> expressionNumbering;
Owen Andersona472c4a2008-05-12 20:15:55 +0000124 AliasAnalysis* AA;
125 MemoryDependenceAnalysis* MD;
126 DominatorTree* DT;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000127
128 uint32_t nextValueNumber;
129
130 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
131 Expression::ExpressionOpcode getOpcode(CmpInst* C);
132 Expression::ExpressionOpcode getOpcode(CastInst* C);
133 Expression create_expression(BinaryOperator* BO);
134 Expression create_expression(CmpInst* C);
135 Expression create_expression(ShuffleVectorInst* V);
136 Expression create_expression(ExtractElementInst* C);
137 Expression create_expression(InsertElementInst* V);
138 Expression create_expression(SelectInst* V);
139 Expression create_expression(CastInst* C);
140 Expression create_expression(GetElementPtrInst* G);
Owen Andersonb388ca92007-10-18 19:39:33 +0000141 Expression create_expression(CallInst* C);
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000142 Expression create_expression(Constant* C);
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000143 public:
Dan Gohmane63c4a22009-04-01 16:37:47 +0000144 ValueTable() : nextValueNumber(1) { }
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000145 uint32_t lookup_or_add(Value* V);
146 uint32_t lookup(Value* V) const;
147 void add(Value* V, uint32_t num);
148 void clear();
149 void erase(Value* v);
150 unsigned size();
Owen Andersona472c4a2008-05-12 20:15:55 +0000151 void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
Chris Lattner663e4412008-12-01 00:40:32 +0000152 AliasAnalysis *getAliasAnalysis() const { return AA; }
Owen Andersona472c4a2008-05-12 20:15:55 +0000153 void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
154 void setDomTree(DominatorTree* D) { DT = D; }
Owen Anderson0ae33ef2008-07-03 17:44:33 +0000155 uint32_t getNextUnusedValueNumber() { return nextValueNumber; }
Bill Wendling246dbbb2008-12-22 21:36:08 +0000156 void verifyRemoved(const Value *) const;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000157 };
158}
159
160namespace llvm {
Chris Lattner76c1b972007-09-17 18:34:04 +0000161template <> struct DenseMapInfo<Expression> {
Owen Anderson830db6a2007-08-02 18:16:06 +0000162 static inline Expression getEmptyKey() {
163 return Expression(Expression::EMPTY);
164 }
165
166 static inline Expression getTombstoneKey() {
167 return Expression(Expression::TOMBSTONE);
168 }
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000169
170 static unsigned getHashValue(const Expression e) {
171 unsigned hash = e.opcode;
172
173 hash = e.firstVN + hash * 37;
174 hash = e.secondVN + hash * 37;
175 hash = e.thirdVN + hash * 37;
176
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000177 hash = ((unsigned)((uintptr_t)e.type >> 4) ^
178 (unsigned)((uintptr_t)e.type >> 9)) +
179 hash * 37;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000180
Owen Anderson830db6a2007-08-02 18:16:06 +0000181 for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
182 E = e.varargs.end(); I != E; ++I)
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000183 hash = *I + hash * 37;
184
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000185 hash = ((unsigned)((uintptr_t)e.function >> 4) ^
186 (unsigned)((uintptr_t)e.function >> 9)) +
187 hash * 37;
Owen Andersonb388ca92007-10-18 19:39:33 +0000188
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000189 return hash;
190 }
Chris Lattner76c1b972007-09-17 18:34:04 +0000191 static bool isEqual(const Expression &LHS, const Expression &RHS) {
192 return LHS == RHS;
193 }
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000194 static bool isPod() { return true; }
195};
196}
197
198//===----------------------------------------------------------------------===//
199// ValueTable Internal Functions
200//===----------------------------------------------------------------------===//
Chris Lattner88365bb2008-03-21 21:14:38 +0000201Expression::ExpressionOpcode ValueTable::getOpcode(BinaryOperator* BO) {
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000202 switch(BO->getOpcode()) {
Chris Lattner88365bb2008-03-21 21:14:38 +0000203 default: // THIS SHOULD NEVER HAPPEN
204 assert(0 && "Binary operator with unknown opcode?");
205 case Instruction::Add: return Expression::ADD;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000206 case Instruction::FAdd: return Expression::FADD;
Chris Lattner88365bb2008-03-21 21:14:38 +0000207 case Instruction::Sub: return Expression::SUB;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000208 case Instruction::FSub: return Expression::FSUB;
Chris Lattner88365bb2008-03-21 21:14:38 +0000209 case Instruction::Mul: return Expression::MUL;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000210 case Instruction::FMul: return Expression::FMUL;
Chris Lattner88365bb2008-03-21 21:14:38 +0000211 case Instruction::UDiv: return Expression::UDIV;
212 case Instruction::SDiv: return Expression::SDIV;
213 case Instruction::FDiv: return Expression::FDIV;
214 case Instruction::URem: return Expression::UREM;
215 case Instruction::SRem: return Expression::SREM;
216 case Instruction::FRem: return Expression::FREM;
217 case Instruction::Shl: return Expression::SHL;
218 case Instruction::LShr: return Expression::LSHR;
219 case Instruction::AShr: return Expression::ASHR;
220 case Instruction::And: return Expression::AND;
221 case Instruction::Or: return Expression::OR;
222 case Instruction::Xor: return Expression::XOR;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000223 }
224}
225
226Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
Nate Begeman1d6e4092008-05-18 19:49:05 +0000227 if (isa<ICmpInst>(C) || isa<VICmpInst>(C)) {
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000228 switch (C->getPredicate()) {
Chris Lattner88365bb2008-03-21 21:14:38 +0000229 default: // THIS SHOULD NEVER HAPPEN
230 assert(0 && "Comparison with unknown predicate?");
231 case ICmpInst::ICMP_EQ: return Expression::ICMPEQ;
232 case ICmpInst::ICMP_NE: return Expression::ICMPNE;
233 case ICmpInst::ICMP_UGT: return Expression::ICMPUGT;
234 case ICmpInst::ICMP_UGE: return Expression::ICMPUGE;
235 case ICmpInst::ICMP_ULT: return Expression::ICMPULT;
236 case ICmpInst::ICMP_ULE: return Expression::ICMPULE;
237 case ICmpInst::ICMP_SGT: return Expression::ICMPSGT;
238 case ICmpInst::ICMP_SGE: return Expression::ICMPSGE;
239 case ICmpInst::ICMP_SLT: return Expression::ICMPSLT;
240 case ICmpInst::ICMP_SLE: return Expression::ICMPSLE;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000241 }
Chris Lattner88365bb2008-03-21 21:14:38 +0000242 }
Nate Begeman1d6e4092008-05-18 19:49:05 +0000243 assert((isa<FCmpInst>(C) || isa<VFCmpInst>(C)) && "Unknown compare");
Chris Lattner88365bb2008-03-21 21:14:38 +0000244 switch (C->getPredicate()) {
245 default: // THIS SHOULD NEVER HAPPEN
246 assert(0 && "Comparison with unknown predicate?");
247 case FCmpInst::FCMP_OEQ: return Expression::FCMPOEQ;
248 case FCmpInst::FCMP_OGT: return Expression::FCMPOGT;
249 case FCmpInst::FCMP_OGE: return Expression::FCMPOGE;
250 case FCmpInst::FCMP_OLT: return Expression::FCMPOLT;
251 case FCmpInst::FCMP_OLE: return Expression::FCMPOLE;
252 case FCmpInst::FCMP_ONE: return Expression::FCMPONE;
253 case FCmpInst::FCMP_ORD: return Expression::FCMPORD;
254 case FCmpInst::FCMP_UNO: return Expression::FCMPUNO;
255 case FCmpInst::FCMP_UEQ: return Expression::FCMPUEQ;
256 case FCmpInst::FCMP_UGT: return Expression::FCMPUGT;
257 case FCmpInst::FCMP_UGE: return Expression::FCMPUGE;
258 case FCmpInst::FCMP_ULT: return Expression::FCMPULT;
259 case FCmpInst::FCMP_ULE: return Expression::FCMPULE;
260 case FCmpInst::FCMP_UNE: return Expression::FCMPUNE;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000261 }
262}
263
Chris Lattner88365bb2008-03-21 21:14:38 +0000264Expression::ExpressionOpcode ValueTable::getOpcode(CastInst* C) {
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000265 switch(C->getOpcode()) {
Chris Lattner88365bb2008-03-21 21:14:38 +0000266 default: // THIS SHOULD NEVER HAPPEN
267 assert(0 && "Cast operator with unknown opcode?");
268 case Instruction::Trunc: return Expression::TRUNC;
269 case Instruction::ZExt: return Expression::ZEXT;
270 case Instruction::SExt: return Expression::SEXT;
271 case Instruction::FPToUI: return Expression::FPTOUI;
272 case Instruction::FPToSI: return Expression::FPTOSI;
273 case Instruction::UIToFP: return Expression::UITOFP;
274 case Instruction::SIToFP: return Expression::SITOFP;
275 case Instruction::FPTrunc: return Expression::FPTRUNC;
276 case Instruction::FPExt: return Expression::FPEXT;
277 case Instruction::PtrToInt: return Expression::PTRTOINT;
278 case Instruction::IntToPtr: return Expression::INTTOPTR;
279 case Instruction::BitCast: return Expression::BITCAST;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000280 }
281}
282
Owen Andersonb388ca92007-10-18 19:39:33 +0000283Expression ValueTable::create_expression(CallInst* C) {
284 Expression e;
285
286 e.type = C->getType();
287 e.firstVN = 0;
288 e.secondVN = 0;
289 e.thirdVN = 0;
290 e.function = C->getCalledFunction();
291 e.opcode = Expression::CALL;
292
293 for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
294 I != E; ++I)
Owen Anderson8f46c782008-04-11 05:11:49 +0000295 e.varargs.push_back(lookup_or_add(*I));
Owen Andersonb388ca92007-10-18 19:39:33 +0000296
297 return e;
298}
299
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000300Expression ValueTable::create_expression(BinaryOperator* BO) {
301 Expression e;
302
Owen Anderson8f46c782008-04-11 05:11:49 +0000303 e.firstVN = lookup_or_add(BO->getOperand(0));
304 e.secondVN = lookup_or_add(BO->getOperand(1));
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000305 e.thirdVN = 0;
Owen Andersonb388ca92007-10-18 19:39:33 +0000306 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000307 e.type = BO->getType();
308 e.opcode = getOpcode(BO);
309
310 return e;
311}
312
313Expression ValueTable::create_expression(CmpInst* C) {
314 Expression e;
315
Owen Anderson8f46c782008-04-11 05:11:49 +0000316 e.firstVN = lookup_or_add(C->getOperand(0));
317 e.secondVN = lookup_or_add(C->getOperand(1));
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000318 e.thirdVN = 0;
Owen Andersonb388ca92007-10-18 19:39:33 +0000319 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000320 e.type = C->getType();
321 e.opcode = getOpcode(C);
322
323 return e;
324}
325
326Expression ValueTable::create_expression(CastInst* C) {
327 Expression e;
328
Owen Anderson8f46c782008-04-11 05:11:49 +0000329 e.firstVN = lookup_or_add(C->getOperand(0));
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000330 e.secondVN = 0;
331 e.thirdVN = 0;
Owen Andersonb388ca92007-10-18 19:39:33 +0000332 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000333 e.type = C->getType();
334 e.opcode = getOpcode(C);
335
336 return e;
337}
338
339Expression ValueTable::create_expression(ShuffleVectorInst* S) {
340 Expression e;
341
Owen Anderson8f46c782008-04-11 05:11:49 +0000342 e.firstVN = lookup_or_add(S->getOperand(0));
343 e.secondVN = lookup_or_add(S->getOperand(1));
344 e.thirdVN = lookup_or_add(S->getOperand(2));
Owen Andersonb388ca92007-10-18 19:39:33 +0000345 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000346 e.type = S->getType();
347 e.opcode = Expression::SHUFFLE;
348
349 return e;
350}
351
352Expression ValueTable::create_expression(ExtractElementInst* E) {
353 Expression e;
354
Owen Anderson8f46c782008-04-11 05:11:49 +0000355 e.firstVN = lookup_or_add(E->getOperand(0));
356 e.secondVN = lookup_or_add(E->getOperand(1));
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000357 e.thirdVN = 0;
Owen Andersonb388ca92007-10-18 19:39:33 +0000358 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000359 e.type = E->getType();
360 e.opcode = Expression::EXTRACT;
361
362 return e;
363}
364
365Expression ValueTable::create_expression(InsertElementInst* I) {
366 Expression e;
367
Owen Anderson8f46c782008-04-11 05:11:49 +0000368 e.firstVN = lookup_or_add(I->getOperand(0));
369 e.secondVN = lookup_or_add(I->getOperand(1));
370 e.thirdVN = lookup_or_add(I->getOperand(2));
Owen Andersonb388ca92007-10-18 19:39:33 +0000371 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000372 e.type = I->getType();
373 e.opcode = Expression::INSERT;
374
375 return e;
376}
377
378Expression ValueTable::create_expression(SelectInst* I) {
379 Expression e;
380
Owen Anderson8f46c782008-04-11 05:11:49 +0000381 e.firstVN = lookup_or_add(I->getCondition());
382 e.secondVN = lookup_or_add(I->getTrueValue());
383 e.thirdVN = lookup_or_add(I->getFalseValue());
Owen Andersonb388ca92007-10-18 19:39:33 +0000384 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000385 e.type = I->getType();
386 e.opcode = Expression::SELECT;
387
388 return e;
389}
390
391Expression ValueTable::create_expression(GetElementPtrInst* G) {
392 Expression e;
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000393
Owen Anderson8f46c782008-04-11 05:11:49 +0000394 e.firstVN = lookup_or_add(G->getPointerOperand());
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000395 e.secondVN = 0;
396 e.thirdVN = 0;
Owen Andersonb388ca92007-10-18 19:39:33 +0000397 e.function = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000398 e.type = G->getType();
399 e.opcode = Expression::GEP;
400
401 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
402 I != E; ++I)
Owen Anderson8f46c782008-04-11 05:11:49 +0000403 e.varargs.push_back(lookup_or_add(*I));
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000404
405 return e;
406}
407
408//===----------------------------------------------------------------------===//
409// ValueTable External Functions
410//===----------------------------------------------------------------------===//
411
Owen Andersonb2303722008-06-18 21:41:49 +0000412/// add - Insert a value into the table with a specified value number.
413void ValueTable::add(Value* V, uint32_t num) {
414 valueNumbering.insert(std::make_pair(V, num));
415}
416
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000417/// lookup_or_add - Returns the value number for the specified value, assigning
418/// it a new number if it did not have one before.
419uint32_t ValueTable::lookup_or_add(Value* V) {
420 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
421 if (VI != valueNumbering.end())
422 return VI->second;
423
Owen Andersonb388ca92007-10-18 19:39:33 +0000424 if (CallInst* C = dyn_cast<CallInst>(V)) {
Owen Anderson8f46c782008-04-11 05:11:49 +0000425 if (AA->doesNotAccessMemory(C)) {
Owen Andersonb388ca92007-10-18 19:39:33 +0000426 Expression e = create_expression(C);
427
428 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
429 if (EI != expressionNumbering.end()) {
430 valueNumbering.insert(std::make_pair(V, EI->second));
431 return EI->second;
432 } else {
433 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
434 valueNumbering.insert(std::make_pair(V, nextValueNumber));
435
436 return nextValueNumber++;
437 }
Owen Anderson241f6532008-04-17 05:36:50 +0000438 } else if (AA->onlyReadsMemory(C)) {
439 Expression e = create_expression(C);
440
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000441 if (expressionNumbering.find(e) == expressionNumbering.end()) {
Owen Anderson241f6532008-04-17 05:36:50 +0000442 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
443 valueNumbering.insert(std::make_pair(V, nextValueNumber));
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000444 return nextValueNumber++;
445 }
Owen Anderson241f6532008-04-17 05:36:50 +0000446
Chris Lattner4c724002008-11-29 02:29:27 +0000447 MemDepResult local_dep = MD->getDependency(C);
Owen Andersonc4f406e2008-05-13 23:18:30 +0000448
Chris Lattnerb51deb92008-12-05 21:04:20 +0000449 if (!local_dep.isDef() && !local_dep.isNonLocal()) {
Owen Andersonc4f406e2008-05-13 23:18:30 +0000450 valueNumbering.insert(std::make_pair(V, nextValueNumber));
451 return nextValueNumber++;
Chris Lattner1440ac52008-11-30 23:39:23 +0000452 }
Chris Lattnerb51deb92008-12-05 21:04:20 +0000453
454 if (local_dep.isDef()) {
455 CallInst* local_cdep = cast<CallInst>(local_dep.getInst());
456
457 if (local_cdep->getNumOperands() != C->getNumOperands()) {
Owen Andersonc4f406e2008-05-13 23:18:30 +0000458 valueNumbering.insert(std::make_pair(V, nextValueNumber));
459 return nextValueNumber++;
460 }
Chris Lattnerb51deb92008-12-05 21:04:20 +0000461
Chris Lattner1440ac52008-11-30 23:39:23 +0000462 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
463 uint32_t c_vn = lookup_or_add(C->getOperand(i));
464 uint32_t cd_vn = lookup_or_add(local_cdep->getOperand(i));
465 if (c_vn != cd_vn) {
466 valueNumbering.insert(std::make_pair(V, nextValueNumber));
467 return nextValueNumber++;
468 }
469 }
470
471 uint32_t v = lookup_or_add(local_cdep);
472 valueNumbering.insert(std::make_pair(V, v));
473 return v;
Owen Andersonc4f406e2008-05-13 23:18:30 +0000474 }
Chris Lattnerbf145d62008-12-01 01:15:42 +0000475
Chris Lattnerb51deb92008-12-05 21:04:20 +0000476 // Non-local case.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000477 const MemoryDependenceAnalysis::NonLocalDepInfo &deps =
Chris Lattner1559b362008-12-09 19:38:05 +0000478 MD->getNonLocalCallDependency(CallSite(C));
Chris Lattnerb51deb92008-12-05 21:04:20 +0000479 // FIXME: call/call dependencies for readonly calls should return def, not
480 // clobber! Move the checking logic to MemDep!
Owen Anderson16db1f72008-05-13 13:41:23 +0000481 CallInst* cdep = 0;
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000482
Chris Lattner1440ac52008-11-30 23:39:23 +0000483 // Check to see if we have a single dominating call instruction that is
484 // identical to C.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000485 for (unsigned i = 0, e = deps.size(); i != e; ++i) {
486 const MemoryDependenceAnalysis::NonLocalDepEntry *I = &deps[i];
Chris Lattner1440ac52008-11-30 23:39:23 +0000487 // Ignore non-local dependencies.
488 if (I->second.isNonLocal())
489 continue;
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000490
Chris Lattner1440ac52008-11-30 23:39:23 +0000491 // We don't handle non-depedencies. If we already have a call, reject
492 // instruction dependencies.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000493 if (I->second.isClobber() || cdep != 0) {
Chris Lattner1440ac52008-11-30 23:39:23 +0000494 cdep = 0;
495 break;
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000496 }
Chris Lattner1440ac52008-11-30 23:39:23 +0000497
498 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->second.getInst());
499 // FIXME: All duplicated with non-local case.
500 if (NonLocalDepCall && DT->properlyDominates(I->first, C->getParent())){
501 cdep = NonLocalDepCall;
502 continue;
503 }
504
505 cdep = 0;
506 break;
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000507 }
508
Owen Anderson16db1f72008-05-13 13:41:23 +0000509 if (!cdep) {
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000510 valueNumbering.insert(std::make_pair(V, nextValueNumber));
Owen Anderson241f6532008-04-17 05:36:50 +0000511 return nextValueNumber++;
512 }
513
Chris Lattnerb51deb92008-12-05 21:04:20 +0000514 if (cdep->getNumOperands() != C->getNumOperands()) {
Owen Anderson3b3f58c2008-05-13 08:17:22 +0000515 valueNumbering.insert(std::make_pair(V, nextValueNumber));
Owen Anderson241f6532008-04-17 05:36:50 +0000516 return nextValueNumber++;
Owen Anderson241f6532008-04-17 05:36:50 +0000517 }
Chris Lattner1440ac52008-11-30 23:39:23 +0000518 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
519 uint32_t c_vn = lookup_or_add(C->getOperand(i));
520 uint32_t cd_vn = lookup_or_add(cdep->getOperand(i));
521 if (c_vn != cd_vn) {
522 valueNumbering.insert(std::make_pair(V, nextValueNumber));
523 return nextValueNumber++;
524 }
525 }
526
527 uint32_t v = lookup_or_add(cdep);
528 valueNumbering.insert(std::make_pair(V, v));
529 return v;
Owen Anderson241f6532008-04-17 05:36:50 +0000530
Owen Andersonb388ca92007-10-18 19:39:33 +0000531 } else {
532 valueNumbering.insert(std::make_pair(V, nextValueNumber));
533 return nextValueNumber++;
534 }
535 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000536 Expression e = create_expression(BO);
537
538 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
539 if (EI != expressionNumbering.end()) {
540 valueNumbering.insert(std::make_pair(V, EI->second));
541 return EI->second;
542 } else {
543 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
544 valueNumbering.insert(std::make_pair(V, nextValueNumber));
545
546 return nextValueNumber++;
547 }
548 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
549 Expression e = create_expression(C);
550
551 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
552 if (EI != expressionNumbering.end()) {
553 valueNumbering.insert(std::make_pair(V, EI->second));
554 return EI->second;
555 } else {
556 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
557 valueNumbering.insert(std::make_pair(V, nextValueNumber));
558
559 return nextValueNumber++;
560 }
561 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
562 Expression e = create_expression(U);
563
564 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
565 if (EI != expressionNumbering.end()) {
566 valueNumbering.insert(std::make_pair(V, EI->second));
567 return EI->second;
568 } else {
569 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
570 valueNumbering.insert(std::make_pair(V, nextValueNumber));
571
572 return nextValueNumber++;
573 }
574 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
575 Expression e = create_expression(U);
576
577 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
578 if (EI != expressionNumbering.end()) {
579 valueNumbering.insert(std::make_pair(V, EI->second));
580 return EI->second;
581 } else {
582 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
583 valueNumbering.insert(std::make_pair(V, nextValueNumber));
584
585 return nextValueNumber++;
586 }
587 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
588 Expression e = create_expression(U);
589
590 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
591 if (EI != expressionNumbering.end()) {
592 valueNumbering.insert(std::make_pair(V, EI->second));
593 return EI->second;
594 } else {
595 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
596 valueNumbering.insert(std::make_pair(V, nextValueNumber));
597
598 return nextValueNumber++;
599 }
600 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
601 Expression e = create_expression(U);
602
603 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
604 if (EI != expressionNumbering.end()) {
605 valueNumbering.insert(std::make_pair(V, EI->second));
606 return EI->second;
607 } else {
608 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
609 valueNumbering.insert(std::make_pair(V, nextValueNumber));
610
611 return nextValueNumber++;
612 }
613 } else if (CastInst* U = dyn_cast<CastInst>(V)) {
614 Expression e = create_expression(U);
615
616 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
617 if (EI != expressionNumbering.end()) {
618 valueNumbering.insert(std::make_pair(V, EI->second));
619 return EI->second;
620 } else {
621 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
622 valueNumbering.insert(std::make_pair(V, nextValueNumber));
623
624 return nextValueNumber++;
625 }
626 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
627 Expression e = create_expression(U);
628
629 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
630 if (EI != expressionNumbering.end()) {
631 valueNumbering.insert(std::make_pair(V, EI->second));
632 return EI->second;
633 } else {
634 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
635 valueNumbering.insert(std::make_pair(V, nextValueNumber));
636
637 return nextValueNumber++;
638 }
639 } else {
640 valueNumbering.insert(std::make_pair(V, nextValueNumber));
641 return nextValueNumber++;
642 }
643}
644
645/// lookup - Returns the value number of the specified value. Fails if
646/// the value has not yet been numbered.
647uint32_t ValueTable::lookup(Value* V) const {
648 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
Chris Lattner88365bb2008-03-21 21:14:38 +0000649 assert(VI != valueNumbering.end() && "Value not numbered?");
650 return VI->second;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000651}
652
653/// clear - Remove all entries from the ValueTable
654void ValueTable::clear() {
655 valueNumbering.clear();
656 expressionNumbering.clear();
657 nextValueNumber = 1;
658}
659
Owen Andersonbf7d0bc2007-07-31 23:27:13 +0000660/// erase - Remove a value from the value numbering
661void ValueTable::erase(Value* V) {
662 valueNumbering.erase(V);
663}
664
Bill Wendling246dbbb2008-12-22 21:36:08 +0000665/// verifyRemoved - Verify that the value is removed from all internal data
666/// structures.
667void ValueTable::verifyRemoved(const Value *V) const {
668 for (DenseMap<Value*, uint32_t>::iterator
669 I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
670 assert(I->first != V && "Inst still occurs in value numbering map!");
671 }
672}
673
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000674//===----------------------------------------------------------------------===//
Bill Wendling30788b82008-12-22 22:32:22 +0000675// GVN Pass
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000676//===----------------------------------------------------------------------===//
677
678namespace {
Owen Anderson6fafe842008-06-20 01:15:47 +0000679 struct VISIBILITY_HIDDEN ValueNumberScope {
680 ValueNumberScope* parent;
681 DenseMap<uint32_t, Value*> table;
682
683 ValueNumberScope(ValueNumberScope* p) : parent(p) { }
684 };
685}
686
687namespace {
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000688
689 class VISIBILITY_HIDDEN GVN : public FunctionPass {
690 bool runOnFunction(Function &F);
691 public:
692 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000693 GVN() : FunctionPass(&ID) { }
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000694
695 private:
Chris Lattner663e4412008-12-01 00:40:32 +0000696 MemoryDependenceAnalysis *MD;
697 DominatorTree *DT;
698
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000699 ValueTable VN;
Owen Anderson6fafe842008-06-20 01:15:47 +0000700 DenseMap<BasicBlock*, ValueNumberScope*> localAvail;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000701
Owen Andersona37226a2007-08-07 23:12:31 +0000702 typedef DenseMap<Value*, SmallPtrSet<Instruction*, 4> > PhiMapType;
703 PhiMapType phiMap;
704
705
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000706 // This transformation requires dominator postdominator info
707 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000708 AU.addRequired<DominatorTree>();
709 AU.addRequired<MemoryDependenceAnalysis>();
Owen Andersonb388ca92007-10-18 19:39:33 +0000710 AU.addRequired<AliasAnalysis>();
Owen Andersonb70a5712008-06-23 17:49:45 +0000711
712 AU.addPreserved<DominatorTree>();
Owen Andersonb388ca92007-10-18 19:39:33 +0000713 AU.addPreserved<AliasAnalysis>();
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000714 }
715
716 // Helper fuctions
717 // FIXME: eliminate or document these better
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000718 bool processLoad(LoadInst* L,
Chris Lattner8e1e95c2008-03-21 22:01:16 +0000719 SmallVectorImpl<Instruction*> &toErase);
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000720 bool processInstruction(Instruction* I,
Chris Lattner8e1e95c2008-03-21 22:01:16 +0000721 SmallVectorImpl<Instruction*> &toErase);
Owen Anderson830db6a2007-08-02 18:16:06 +0000722 bool processNonLocalLoad(LoadInst* L,
Chris Lattner8e1e95c2008-03-21 22:01:16 +0000723 SmallVectorImpl<Instruction*> &toErase);
Owen Anderson255dafc2008-12-15 02:03:00 +0000724 bool processBlock(BasicBlock* BB);
Owen Andersoncbe1d942008-12-14 19:10:35 +0000725 Value *GetValueForBlock(BasicBlock *BB, Instruction* orig,
Owen Anderson1c2763d2007-08-02 17:56:05 +0000726 DenseMap<BasicBlock*, Value*> &Phis,
727 bool top_level = false);
Owen Andersonb2303722008-06-18 21:41:49 +0000728 void dump(DenseMap<uint32_t, Value*>& d);
Owen Anderson3e75a422007-08-14 18:04:11 +0000729 bool iterateOnFunction(Function &F);
Owen Anderson1defe2d2007-08-16 22:51:56 +0000730 Value* CollapsePhi(PHINode* p);
Owen Anderson24866862007-09-16 08:04:16 +0000731 bool isSafeReplacement(PHINode* p, Instruction* inst);
Owen Andersonb2303722008-06-18 21:41:49 +0000732 bool performPRE(Function& F);
Owen Anderson6fafe842008-06-20 01:15:47 +0000733 Value* lookupNumber(BasicBlock* BB, uint32_t num);
Owen Anderson961edc82008-07-15 16:28:06 +0000734 bool mergeBlockIntoPredecessor(BasicBlock* BB);
Owen Anderson255dafc2008-12-15 02:03:00 +0000735 Value* AttemptRedundancyElimination(Instruction* orig, unsigned valno);
Nuno Lopes7cdd9ee2008-10-10 16:25:50 +0000736 void cleanupGlobalSets();
Bill Wendling246dbbb2008-12-22 21:36:08 +0000737 void verifyRemoved(const Instruction *I) const;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000738 };
739
740 char GVN::ID = 0;
Owen Anderson1ad2cb72007-07-24 17:55:58 +0000741}
742
743// createGVNPass - The public interface to this file...
744FunctionPass *llvm::createGVNPass() { return new GVN(); }
745
746static RegisterPass<GVN> X("gvn",
747 "Global Value Numbering");
748
Owen Andersonb2303722008-06-18 21:41:49 +0000749void GVN::dump(DenseMap<uint32_t, Value*>& d) {
Owen Anderson0cd32032007-07-25 19:57:03 +0000750 printf("{\n");
Owen Andersonb2303722008-06-18 21:41:49 +0000751 for (DenseMap<uint32_t, Value*>::iterator I = d.begin(),
Owen Anderson0cd32032007-07-25 19:57:03 +0000752 E = d.end(); I != E; ++I) {
Owen Andersonb2303722008-06-18 21:41:49 +0000753 printf("%d\n", I->first);
Owen Anderson0cd32032007-07-25 19:57:03 +0000754 I->second->dump();
755 }
756 printf("}\n");
757}
758
Owen Anderson1defe2d2007-08-16 22:51:56 +0000759Value* GVN::CollapsePhi(PHINode* p) {
Owen Anderson1defe2d2007-08-16 22:51:56 +0000760 Value* constVal = p->hasConstantValue();
Chris Lattner88365bb2008-03-21 21:14:38 +0000761 if (!constVal) return 0;
Owen Anderson1defe2d2007-08-16 22:51:56 +0000762
Chris Lattner88365bb2008-03-21 21:14:38 +0000763 Instruction* inst = dyn_cast<Instruction>(constVal);
764 if (!inst)
765 return constVal;
766
Chris Lattner663e4412008-12-01 00:40:32 +0000767 if (DT->dominates(inst, p))
Chris Lattner88365bb2008-03-21 21:14:38 +0000768 if (isSafeReplacement(p, inst))
769 return inst;
Owen Anderson1defe2d2007-08-16 22:51:56 +0000770 return 0;
771}
Owen Anderson0cd32032007-07-25 19:57:03 +0000772
Owen Anderson24866862007-09-16 08:04:16 +0000773bool GVN::isSafeReplacement(PHINode* p, Instruction* inst) {
774 if (!isa<PHINode>(inst))
775 return true;
776
777 for (Instruction::use_iterator UI = p->use_begin(), E = p->use_end();
778 UI != E; ++UI)
779 if (PHINode* use_phi = dyn_cast<PHINode>(UI))
780 if (use_phi->getParent() == inst->getParent())
781 return false;
782
783 return true;
784}
785
Owen Anderson45537912007-07-26 18:26:51 +0000786/// GetValueForBlock - Get the value to use within the specified basic block.
787/// available values are in Phis.
Owen Andersoncbe1d942008-12-14 19:10:35 +0000788Value *GVN::GetValueForBlock(BasicBlock *BB, Instruction* orig,
Chris Lattner88365bb2008-03-21 21:14:38 +0000789 DenseMap<BasicBlock*, Value*> &Phis,
790 bool top_level) {
Owen Anderson45537912007-07-26 18:26:51 +0000791
792 // If we have already computed this value, return the previously computed val.
Owen Andersonab870272007-08-03 19:59:35 +0000793 DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
794 if (V != Phis.end() && !top_level) return V->second;
Owen Anderson45537912007-07-26 18:26:51 +0000795
Owen Andersoncb29a4f2008-07-02 18:15:31 +0000796 // If the block is unreachable, just return undef, since this path
797 // can't actually occur at runtime.
Chris Lattner663e4412008-12-01 00:40:32 +0000798 if (!DT->isReachableFromEntry(BB))
Owen Andersond672ecb2009-07-03 00:17:18 +0000799 return Phis[BB] = Context->getUndef(orig->getType());
Owen Andersonf2aa1602008-07-02 17:20:16 +0000800
Chris Lattnerae199312008-12-09 19:21:47 +0000801 if (BasicBlock *Pred = BB->getSinglePredecessor()) {
802 Value *ret = GetValueForBlock(Pred, orig, Phis);
Owen Andersonab870272007-08-03 19:59:35 +0000803 Phis[BB] = ret;
804 return ret;
Owen Anderson4b55c3b2007-08-03 11:03:26 +0000805 }
Chris Lattnerae199312008-12-09 19:21:47 +0000806
807 // Get the number of predecessors of this block so we can reserve space later.
808 // If there is already a PHI in it, use the #preds from it, otherwise count.
809 // Getting it from the PHI is constant time.
810 unsigned NumPreds;
811 if (PHINode *ExistingPN = dyn_cast<PHINode>(BB->begin()))
812 NumPreds = ExistingPN->getNumIncomingValues();
813 else
814 NumPreds = std::distance(pred_begin(BB), pred_end(BB));
Chris Lattner88365bb2008-03-21 21:14:38 +0000815
Owen Anderson45537912007-07-26 18:26:51 +0000816 // Otherwise, the idom is the loop, so we need to insert a PHI node. Do so
817 // now, then get values to fill in the incoming values for the PHI.
Gabor Greif051a9502008-04-06 20:25:17 +0000818 PHINode *PN = PHINode::Create(orig->getType(), orig->getName()+".rle",
819 BB->begin());
Chris Lattnerae199312008-12-09 19:21:47 +0000820 PN->reserveOperandSpace(NumPreds);
Owen Andersonab870272007-08-03 19:59:35 +0000821
Chris Lattnerae199312008-12-09 19:21:47 +0000822 Phis.insert(std::make_pair(BB, PN));
Owen Anderson4f9ba7c2007-07-30 16:57:08 +0000823
Owen Anderson45537912007-07-26 18:26:51 +0000824 // Fill in the incoming values for the block.
Owen Anderson054ab942007-07-31 17:43:14 +0000825 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
826 Value* val = GetValueForBlock(*PI, orig, Phis);
Owen Anderson054ab942007-07-31 17:43:14 +0000827 PN->addIncoming(val, *PI);
828 }
Chris Lattner88365bb2008-03-21 21:14:38 +0000829
Chris Lattner663e4412008-12-01 00:40:32 +0000830 VN.getAliasAnalysis()->copyValue(orig, PN);
Owen Anderson054ab942007-07-31 17:43:14 +0000831
Owen Anderson62bc33c2007-08-16 22:02:55 +0000832 // Attempt to collapse PHI nodes that are trivially redundant
Owen Anderson1defe2d2007-08-16 22:51:56 +0000833 Value* v = CollapsePhi(PN);
Chris Lattner88365bb2008-03-21 21:14:38 +0000834 if (!v) {
835 // Cache our phi construction results
Owen Andersoncbe1d942008-12-14 19:10:35 +0000836 if (LoadInst* L = dyn_cast<LoadInst>(orig))
837 phiMap[L->getPointerOperand()].insert(PN);
838 else
839 phiMap[orig].insert(PN);
840
Chris Lattner88365bb2008-03-21 21:14:38 +0000841 return PN;
Owen Anderson054ab942007-07-31 17:43:14 +0000842 }
Owen Andersona472c4a2008-05-12 20:15:55 +0000843
Chris Lattner88365bb2008-03-21 21:14:38 +0000844 PN->replaceAllUsesWith(v);
Chris Lattnerbc99be12008-12-09 22:06:23 +0000845 if (isa<PointerType>(v->getType()))
846 MD->invalidateCachedPointerInfo(v);
Chris Lattner88365bb2008-03-21 21:14:38 +0000847
848 for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
849 E = Phis.end(); I != E; ++I)
850 if (I->second == PN)
851 I->second = v;
852
Chris Lattner663e4412008-12-01 00:40:32 +0000853 DEBUG(cerr << "GVN removed: " << *PN);
854 MD->removeInstruction(PN);
Chris Lattner88365bb2008-03-21 21:14:38 +0000855 PN->eraseFromParent();
Bill Wendling246dbbb2008-12-22 21:36:08 +0000856 DEBUG(verifyRemoved(PN));
Chris Lattner88365bb2008-03-21 21:14:38 +0000857
858 Phis[BB] = v;
859 return v;
Owen Anderson0cd32032007-07-25 19:57:03 +0000860}
861
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000862/// IsValueFullyAvailableInBlock - Return true if we can prove that the value
863/// we're analyzing is fully available in the specified block. As we go, keep
Chris Lattner72bc70d2008-12-05 07:49:08 +0000864/// track of which blocks we know are fully alive in FullyAvailableBlocks. This
865/// map is actually a tri-state map with the following values:
866/// 0) we know the block *is not* fully available.
867/// 1) we know the block *is* fully available.
868/// 2) we do not know whether the block is fully available or not, but we are
869/// currently speculating that it will be.
870/// 3) we are speculating for this block and have used that to speculate for
871/// other blocks.
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000872static bool IsValueFullyAvailableInBlock(BasicBlock *BB,
Chris Lattner72bc70d2008-12-05 07:49:08 +0000873 DenseMap<BasicBlock*, char> &FullyAvailableBlocks) {
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000874 // Optimistically assume that the block is fully available and check to see
875 // if we already know about this block in one lookup.
Chris Lattner72bc70d2008-12-05 07:49:08 +0000876 std::pair<DenseMap<BasicBlock*, char>::iterator, char> IV =
877 FullyAvailableBlocks.insert(std::make_pair(BB, 2));
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000878
879 // If the entry already existed for this block, return the precomputed value.
Chris Lattner72bc70d2008-12-05 07:49:08 +0000880 if (!IV.second) {
881 // If this is a speculative "available" value, mark it as being used for
882 // speculation of other blocks.
883 if (IV.first->second == 2)
884 IV.first->second = 3;
885 return IV.first->second != 0;
886 }
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000887
888 // Otherwise, see if it is fully available in all predecessors.
889 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
890
891 // If this block has no predecessors, it isn't live-in here.
892 if (PI == PE)
Chris Lattner72bc70d2008-12-05 07:49:08 +0000893 goto SpeculationFailure;
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000894
895 for (; PI != PE; ++PI)
896 // If the value isn't fully available in one of our predecessors, then it
897 // isn't fully available in this block either. Undo our previous
898 // optimistic assumption and bail out.
899 if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks))
Chris Lattner72bc70d2008-12-05 07:49:08 +0000900 goto SpeculationFailure;
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000901
902 return true;
Chris Lattner72bc70d2008-12-05 07:49:08 +0000903
904// SpeculationFailure - If we get here, we found out that this is not, after
905// all, a fully-available block. We have a problem if we speculated on this and
906// used the speculation to mark other blocks as available.
907SpeculationFailure:
908 char &BBVal = FullyAvailableBlocks[BB];
909
910 // If we didn't speculate on this, just return with it set to false.
911 if (BBVal == 2) {
912 BBVal = 0;
913 return false;
914 }
915
916 // If we did speculate on this value, we could have blocks set to 1 that are
917 // incorrect. Walk the (transitive) successors of this block and mark them as
918 // 0 if set to one.
919 SmallVector<BasicBlock*, 32> BBWorklist;
920 BBWorklist.push_back(BB);
921
922 while (!BBWorklist.empty()) {
923 BasicBlock *Entry = BBWorklist.pop_back_val();
924 // Note that this sets blocks to 0 (unavailable) if they happen to not
925 // already be in FullyAvailableBlocks. This is safe.
926 char &EntryVal = FullyAvailableBlocks[Entry];
927 if (EntryVal == 0) continue; // Already unavailable.
928
929 // Mark as unavailable.
930 EntryVal = 0;
931
932 for (succ_iterator I = succ_begin(Entry), E = succ_end(Entry); I != E; ++I)
933 BBWorklist.push_back(*I);
934 }
935
936 return false;
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000937}
938
Owen Anderson62bc33c2007-08-16 22:02:55 +0000939/// processNonLocalLoad - Attempt to eliminate a load whose dependencies are
940/// non-local by performing PHI construction.
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000941bool GVN::processNonLocalLoad(LoadInst *LI,
Chris Lattner8e1e95c2008-03-21 22:01:16 +0000942 SmallVectorImpl<Instruction*> &toErase) {
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000943 // Find the non-local dependencies of the load.
Chris Lattner91bcf642008-12-09 19:25:07 +0000944 SmallVector<MemoryDependenceAnalysis::NonLocalDepEntry, 64> Deps;
945 MD->getNonLocalPointerDependency(LI->getOperand(0), true, LI->getParent(),
946 Deps);
947 //DEBUG(cerr << "INVESTIGATING NONLOCAL LOAD: " << Deps.size() << *LI);
Owen Anderson0cd32032007-07-25 19:57:03 +0000948
Owen Anderson516eb1c2008-08-26 22:07:42 +0000949 // If we had to process more than one hundred blocks to find the
950 // dependencies, this load isn't worth worrying about. Optimizing
951 // it will be too expensive.
Chris Lattner91bcf642008-12-09 19:25:07 +0000952 if (Deps.size() > 100)
Owen Anderson516eb1c2008-08-26 22:07:42 +0000953 return false;
Chris Lattner5f4f84b2008-12-18 00:51:32 +0000954
955 // If we had a phi translation failure, we'll have a single entry which is a
956 // clobber in the current block. Reject this early.
Torok Edwin4306b1a2009-06-17 18:48:18 +0000957 if (Deps.size() == 1 && Deps[0].second.isClobber()) {
958 DEBUG(
959 DOUT << "GVN: non-local load ";
960 WriteAsOperand(*DOUT.stream(), LI);
961 DOUT << " is clobbered by " << *Deps[0].second.getInst();
962 );
Chris Lattner5f4f84b2008-12-18 00:51:32 +0000963 return false;
Torok Edwin4306b1a2009-06-17 18:48:18 +0000964 }
Owen Anderson516eb1c2008-08-26 22:07:42 +0000965
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000966 // Filter out useless results (non-locals, etc). Keep track of the blocks
967 // where we have a value available in repl, also keep track of whether we see
968 // dependencies that produce an unknown value for the load (such as a call
969 // that could potentially clobber the load).
970 SmallVector<std::pair<BasicBlock*, Value*>, 16> ValuesPerBlock;
971 SmallVector<BasicBlock*, 16> UnavailableBlocks;
Owen Andersona37226a2007-08-07 23:12:31 +0000972
Chris Lattner91bcf642008-12-09 19:25:07 +0000973 for (unsigned i = 0, e = Deps.size(); i != e; ++i) {
974 BasicBlock *DepBB = Deps[i].first;
975 MemDepResult DepInfo = Deps[i].second;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000976
Chris Lattnerb51deb92008-12-05 21:04:20 +0000977 if (DepInfo.isClobber()) {
978 UnavailableBlocks.push_back(DepBB);
979 continue;
980 }
981
982 Instruction *DepInst = DepInfo.getInst();
983
984 // Loading the allocation -> undef.
985 if (isa<AllocationInst>(DepInst)) {
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000986 ValuesPerBlock.push_back(std::make_pair(DepBB,
Owen Andersond672ecb2009-07-03 00:17:18 +0000987 Context->getUndef(LI->getType())));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000988 continue;
989 }
Chris Lattner88365bb2008-03-21 21:14:38 +0000990
Chris Lattner91bcf642008-12-09 19:25:07 +0000991 if (StoreInst* S = dyn_cast<StoreInst>(DepInst)) {
Chris Lattner978796e2008-12-01 01:31:36 +0000992 // Reject loads and stores that are to the same address but are of
993 // different types.
994 // NOTE: 403.gcc does have this case (e.g. in readonly_fields_p) because
995 // of bitfield access, it would be interesting to optimize for it at some
996 // point.
Chris Lattnerc89c6a92008-12-02 08:16:11 +0000997 if (S->getOperand(0)->getType() != LI->getType()) {
998 UnavailableBlocks.push_back(DepBB);
999 continue;
1000 }
Chris Lattner978796e2008-12-01 01:31:36 +00001001
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001002 ValuesPerBlock.push_back(std::make_pair(DepBB, S->getOperand(0)));
Chris Lattner978796e2008-12-01 01:31:36 +00001003
Chris Lattner91bcf642008-12-09 19:25:07 +00001004 } else if (LoadInst* LD = dyn_cast<LoadInst>(DepInst)) {
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001005 if (LD->getType() != LI->getType()) {
1006 UnavailableBlocks.push_back(DepBB);
1007 continue;
1008 }
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001009 ValuesPerBlock.push_back(std::make_pair(DepBB, LD));
Owen Anderson0cd32032007-07-25 19:57:03 +00001010 } else {
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001011 UnavailableBlocks.push_back(DepBB);
1012 continue;
Owen Anderson0cd32032007-07-25 19:57:03 +00001013 }
Chris Lattner88365bb2008-03-21 21:14:38 +00001014 }
Owen Anderson0cd32032007-07-25 19:57:03 +00001015
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001016 // If we have no predecessors that produce a known value for this load, exit
1017 // early.
1018 if (ValuesPerBlock.empty()) return false;
1019
1020 // If all of the instructions we depend on produce a known value for this
1021 // load, then it is fully redundant and we can use PHI insertion to compute
1022 // its value. Insert PHIs and remove the fully redundant value now.
1023 if (UnavailableBlocks.empty()) {
1024 // Use cached PHI construction information from previous runs
1025 SmallPtrSet<Instruction*, 4> &p = phiMap[LI->getPointerOperand()];
Chris Lattner91bcf642008-12-09 19:25:07 +00001026 // FIXME: What does phiMap do? Are we positive it isn't getting invalidated?
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001027 for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
1028 I != E; ++I) {
1029 if ((*I)->getParent() == LI->getParent()) {
1030 DEBUG(cerr << "GVN REMOVING NONLOCAL LOAD #1: " << *LI);
1031 LI->replaceAllUsesWith(*I);
Chris Lattnerbc99be12008-12-09 22:06:23 +00001032 if (isa<PointerType>((*I)->getType()))
1033 MD->invalidateCachedPointerInfo(*I);
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001034 toErase.push_back(LI);
1035 NumGVNLoad++;
1036 return true;
1037 }
1038
1039 ValuesPerBlock.push_back(std::make_pair((*I)->getParent(), *I));
Owen Andersona37226a2007-08-07 23:12:31 +00001040 }
Chris Lattner88365bb2008-03-21 21:14:38 +00001041
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001042 DEBUG(cerr << "GVN REMOVING NONLOCAL LOAD: " << *LI);
1043
1044 DenseMap<BasicBlock*, Value*> BlockReplValues;
1045 BlockReplValues.insert(ValuesPerBlock.begin(), ValuesPerBlock.end());
1046 // Perform PHI construction.
1047 Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
1048 LI->replaceAllUsesWith(v);
Chris Lattnerf3313162008-12-15 03:46:38 +00001049
Chris Lattner0aefc0e2009-02-12 07:00:35 +00001050 if (isa<PHINode>(v))
Chris Lattnerf3313162008-12-15 03:46:38 +00001051 v->takeName(LI);
Chris Lattnerbc99be12008-12-09 22:06:23 +00001052 if (isa<PointerType>(v->getType()))
1053 MD->invalidateCachedPointerInfo(v);
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001054 toErase.push_back(LI);
1055 NumGVNLoad++;
1056 return true;
1057 }
1058
1059 if (!EnablePRE || !EnableLoadPRE)
1060 return false;
1061
1062 // Okay, we have *some* definitions of the value. This means that the value
1063 // is available in some of our (transitive) predecessors. Lets think about
1064 // doing PRE of this load. This will involve inserting a new load into the
1065 // predecessor when it's not available. We could do this in general, but
1066 // prefer to not increase code size. As such, we only do this when we know
1067 // that we only have to insert *one* load (which means we're basically moving
1068 // the load, not inserting a new one).
1069
Owen Anderson88554df2009-05-31 09:03:40 +00001070 SmallPtrSet<BasicBlock *, 4> Blockers;
1071 for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
1072 Blockers.insert(UnavailableBlocks[i]);
1073
1074 // Lets find first basic block with more than one predecessor. Walk backwards
1075 // through predecessors if needed.
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001076 BasicBlock *LoadBB = LI->getParent();
Owen Anderson88554df2009-05-31 09:03:40 +00001077 BasicBlock *TmpBB = LoadBB;
1078
1079 bool isSinglePred = false;
Dale Johannesen42c3f552009-06-17 20:48:23 +00001080 bool allSingleSucc = true;
Owen Anderson88554df2009-05-31 09:03:40 +00001081 while (TmpBB->getSinglePredecessor()) {
1082 isSinglePred = true;
1083 TmpBB = TmpBB->getSinglePredecessor();
1084 if (!TmpBB) // If haven't found any, bail now.
1085 return false;
1086 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1087 return false;
1088 if (Blockers.count(TmpBB))
1089 return false;
Dale Johannesen42c3f552009-06-17 20:48:23 +00001090 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1091 allSingleSucc = false;
Owen Anderson88554df2009-05-31 09:03:40 +00001092 }
1093
1094 assert(TmpBB);
1095 LoadBB = TmpBB;
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001096
1097 // If we have a repl set with LI itself in it, this means we have a loop where
1098 // at least one of the values is LI. Since this means that we won't be able
1099 // to eliminate LI even if we insert uses in the other predecessors, we will
1100 // end up increasing code size. Reject this by scanning for LI.
1101 for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
1102 if (ValuesPerBlock[i].second == LI)
1103 return false;
1104
Owen Anderson88554df2009-05-31 09:03:40 +00001105 if (isSinglePred) {
1106 bool isHot = false;
1107 for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
1108 if (Instruction *I = dyn_cast<Instruction>(ValuesPerBlock[i].second))
1109 // "Hot" Instruction is in some loop (because it dominates its dep.
1110 // instruction).
1111 if (DT->dominates(LI, I)) {
1112 isHot = true;
1113 break;
1114 }
1115
1116 // We are interested only in "hot" instructions. We don't want to do any
1117 // mis-optimizations here.
1118 if (!isHot)
1119 return false;
1120 }
1121
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001122 // Okay, we have some hope :). Check to see if the loaded value is fully
1123 // available in all but one predecessor.
1124 // FIXME: If we could restructure the CFG, we could make a common pred with
1125 // all the preds that don't have an available LI and insert a new load into
1126 // that one block.
1127 BasicBlock *UnavailablePred = 0;
1128
Chris Lattner72bc70d2008-12-05 07:49:08 +00001129 DenseMap<BasicBlock*, char> FullyAvailableBlocks;
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001130 for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
1131 FullyAvailableBlocks[ValuesPerBlock[i].first] = true;
1132 for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
1133 FullyAvailableBlocks[UnavailableBlocks[i]] = false;
1134
1135 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
1136 PI != E; ++PI) {
1137 if (IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks))
1138 continue;
1139
1140 // If this load is not available in multiple predecessors, reject it.
1141 if (UnavailablePred && UnavailablePred != *PI)
1142 return false;
1143 UnavailablePred = *PI;
1144 }
1145
1146 assert(UnavailablePred != 0 &&
1147 "Fully available value should be eliminated above!");
1148
1149 // If the loaded pointer is PHI node defined in this block, do PHI translation
1150 // to get its value in the predecessor.
1151 Value *LoadPtr = LI->getOperand(0)->DoPHITranslation(LoadBB, UnavailablePred);
1152
1153 // Make sure the value is live in the predecessor. If it was defined by a
1154 // non-PHI instruction in this block, we don't know how to recompute it above.
1155 if (Instruction *LPInst = dyn_cast<Instruction>(LoadPtr))
1156 if (!DT->dominates(LPInst->getParent(), UnavailablePred)) {
1157 DEBUG(cerr << "COULDN'T PRE LOAD BECAUSE PTR IS UNAVAILABLE IN PRED: "
1158 << *LPInst << *LI << "\n");
1159 return false;
1160 }
1161
1162 // We don't currently handle critical edges :(
1163 if (UnavailablePred->getTerminator()->getNumSuccessors() != 1) {
1164 DEBUG(cerr << "COULD NOT PRE LOAD BECAUSE OF CRITICAL EDGE '"
1165 << UnavailablePred->getName() << "': " << *LI);
1166 return false;
Owen Andersona37226a2007-08-07 23:12:31 +00001167 }
Dale Johannesen42c3f552009-06-17 20:48:23 +00001168
1169 // Make sure it is valid to move this load here. We have to watch out for:
1170 // @1 = getelementptr (i8* p, ...
1171 // test p and branch if == 0
1172 // load @1
1173 // It is valid to have the getelementptr before the test, even if p can be 0,
1174 // as getelementptr only does address arithmetic.
1175 // If we are not pushing the value through any multiple-successor blocks
1176 // we do not have this case. Otherwise, check that the load is safe to
1177 // put anywhere; this can be improved, but should be conservatively safe.
1178 if (!allSingleSucc &&
1179 !isSafeToLoadUnconditionally(LoadPtr, UnavailablePred->getTerminator()))
1180 return false;
1181
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001182 // Okay, we can eliminate this load by inserting a reload in the predecessor
1183 // and using PHI construction to get the value in the other predecessors, do
1184 // it.
Chris Lattner7f7c7362008-12-05 17:04:12 +00001185 DEBUG(cerr << "GVN REMOVING PRE LOAD: " << *LI);
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001186
1187 Value *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre", false,
1188 LI->getAlignment(),
1189 UnavailablePred->getTerminator());
1190
Owen Anderson246ddf52009-05-29 05:37:54 +00001191 SmallPtrSet<Instruction*, 4> &p = phiMap[LI->getPointerOperand()];
1192 for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
1193 I != E; ++I)
1194 ValuesPerBlock.push_back(std::make_pair((*I)->getParent(), *I));
1195
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001196 DenseMap<BasicBlock*, Value*> BlockReplValues;
1197 BlockReplValues.insert(ValuesPerBlock.begin(), ValuesPerBlock.end());
1198 BlockReplValues[UnavailablePred] = NewLoad;
1199
1200 // Perform PHI construction.
1201 Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
1202 LI->replaceAllUsesWith(v);
Chris Lattner0aefc0e2009-02-12 07:00:35 +00001203 if (isa<PHINode>(v))
Chris Lattnerf3313162008-12-15 03:46:38 +00001204 v->takeName(LI);
Chris Lattnerbc99be12008-12-09 22:06:23 +00001205 if (isa<PointerType>(v->getType()))
1206 MD->invalidateCachedPointerInfo(v);
Chris Lattnerc89c6a92008-12-02 08:16:11 +00001207 toErase.push_back(LI);
1208 NumPRELoad++;
Owen Anderson0cd32032007-07-25 19:57:03 +00001209 return true;
1210}
1211
Owen Anderson62bc33c2007-08-16 22:02:55 +00001212/// processLoad - Attempt to eliminate a load, first by eliminating it
1213/// locally, and then attempting non-local elimination if that fails.
Chris Lattnerb51deb92008-12-05 21:04:20 +00001214bool GVN::processLoad(LoadInst *L, SmallVectorImpl<Instruction*> &toErase) {
1215 if (L->isVolatile())
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001216 return false;
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001217
1218 Value* pointer = L->getPointerOperand();
Chris Lattnerb51deb92008-12-05 21:04:20 +00001219
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001220 // ... to a pointer that has been loaded from before...
Chris Lattner663e4412008-12-01 00:40:32 +00001221 MemDepResult dep = MD->getDependency(L);
Owen Anderson8e8278e2007-08-14 17:59:48 +00001222
Chris Lattnerb51deb92008-12-05 21:04:20 +00001223 // If the value isn't available, don't do anything!
Torok Edwin3f3c6d42009-05-29 09:46:03 +00001224 if (dep.isClobber()) {
1225 DEBUG(
1226 // fast print dep, using operator<< on instruction would be too slow
1227 DOUT << "GVN: load ";
1228 WriteAsOperand(*DOUT.stream(), L);
1229 Instruction *I = dep.getInst();
Torok Edwin70ac1422009-05-29 16:58:36 +00001230 DOUT << " is clobbered by " << *I;
Torok Edwin3f3c6d42009-05-29 09:46:03 +00001231 );
Chris Lattnerb51deb92008-12-05 21:04:20 +00001232 return false;
Torok Edwin3f3c6d42009-05-29 09:46:03 +00001233 }
Chris Lattnerb51deb92008-12-05 21:04:20 +00001234
1235 // If it is defined in another block, try harder.
Chris Lattnerae199312008-12-09 19:21:47 +00001236 if (dep.isNonLocal())
Chris Lattnerb51deb92008-12-05 21:04:20 +00001237 return processNonLocalLoad(L, toErase);
Eli Friedmanb6c36e42008-02-12 12:08:14 +00001238
Chris Lattnerb51deb92008-12-05 21:04:20 +00001239 Instruction *DepInst = dep.getInst();
1240 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
1241 // Only forward substitute stores to loads of the same type.
1242 // FIXME: Could do better!
1243 if (DepSI->getPointerOperand()->getType() != pointer->getType())
1244 return false;
1245
1246 // Remove it!
1247 L->replaceAllUsesWith(DepSI->getOperand(0));
Chris Lattnerbc99be12008-12-09 22:06:23 +00001248 if (isa<PointerType>(DepSI->getOperand(0)->getType()))
1249 MD->invalidateCachedPointerInfo(DepSI->getOperand(0));
Chris Lattnerb51deb92008-12-05 21:04:20 +00001250 toErase.push_back(L);
1251 NumGVNLoad++;
1252 return true;
1253 }
1254
1255 if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInst)) {
1256 // Only forward substitute stores to loads of the same type.
1257 // FIXME: Could do better! load i32 -> load i8 -> truncate on little endian.
1258 if (DepLI->getType() != L->getType())
1259 return false;
1260
1261 // Remove it!
1262 L->replaceAllUsesWith(DepLI);
Chris Lattnerbc99be12008-12-09 22:06:23 +00001263 if (isa<PointerType>(DepLI->getType()))
1264 MD->invalidateCachedPointerInfo(DepLI);
Chris Lattnerb51deb92008-12-05 21:04:20 +00001265 toErase.push_back(L);
1266 NumGVNLoad++;
1267 return true;
1268 }
1269
Chris Lattner237a8282008-11-30 01:39:32 +00001270 // If this load really doesn't depend on anything, then we must be loading an
1271 // undef value. This can happen when loading for a fresh allocation with no
1272 // intervening stores, for example.
Chris Lattnerb51deb92008-12-05 21:04:20 +00001273 if (isa<AllocationInst>(DepInst)) {
Owen Andersond672ecb2009-07-03 00:17:18 +00001274 L->replaceAllUsesWith(Context->getUndef(L->getType()));
Chris Lattner237a8282008-11-30 01:39:32 +00001275 toErase.push_back(L);
Chris Lattner237a8282008-11-30 01:39:32 +00001276 NumGVNLoad++;
Chris Lattnerb51deb92008-12-05 21:04:20 +00001277 return true;
Eli Friedmanb6c36e42008-02-12 12:08:14 +00001278 }
1279
Chris Lattnerb51deb92008-12-05 21:04:20 +00001280 return false;
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001281}
1282
Owen Anderson6fafe842008-06-20 01:15:47 +00001283Value* GVN::lookupNumber(BasicBlock* BB, uint32_t num) {
Owen Andersonb70a5712008-06-23 17:49:45 +00001284 DenseMap<BasicBlock*, ValueNumberScope*>::iterator I = localAvail.find(BB);
1285 if (I == localAvail.end())
1286 return 0;
1287
1288 ValueNumberScope* locals = I->second;
Owen Anderson6fafe842008-06-20 01:15:47 +00001289
1290 while (locals) {
1291 DenseMap<uint32_t, Value*>::iterator I = locals->table.find(num);
1292 if (I != locals->table.end())
1293 return I->second;
1294 else
1295 locals = locals->parent;
1296 }
1297
1298 return 0;
1299}
1300
Owen Anderson255dafc2008-12-15 02:03:00 +00001301/// AttemptRedundancyElimination - If the "fast path" of redundancy elimination
1302/// by inheritance from the dominator fails, see if we can perform phi
1303/// construction to eliminate the redundancy.
1304Value* GVN::AttemptRedundancyElimination(Instruction* orig, unsigned valno) {
1305 BasicBlock* BaseBlock = orig->getParent();
1306
1307 SmallPtrSet<BasicBlock*, 4> Visited;
1308 SmallVector<BasicBlock*, 8> Stack;
1309 Stack.push_back(BaseBlock);
1310
1311 DenseMap<BasicBlock*, Value*> Results;
1312
1313 // Walk backwards through our predecessors, looking for instances of the
1314 // value number we're looking for. Instances are recorded in the Results
1315 // map, which is then used to perform phi construction.
1316 while (!Stack.empty()) {
1317 BasicBlock* Current = Stack.back();
1318 Stack.pop_back();
1319
1320 // If we've walked all the way to a proper dominator, then give up. Cases
1321 // where the instance is in the dominator will have been caught by the fast
1322 // path, and any cases that require phi construction further than this are
1323 // probably not worth it anyways. Note that this is a SIGNIFICANT compile
1324 // time improvement.
1325 if (DT->properlyDominates(Current, orig->getParent())) return 0;
1326
1327 DenseMap<BasicBlock*, ValueNumberScope*>::iterator LA =
1328 localAvail.find(Current);
1329 if (LA == localAvail.end()) return 0;
Chris Lattner2f39b292009-01-19 22:00:18 +00001330 DenseMap<uint32_t, Value*>::iterator V = LA->second->table.find(valno);
Owen Anderson255dafc2008-12-15 02:03:00 +00001331
1332 if (V != LA->second->table.end()) {
1333 // Found an instance, record it.
1334 Results.insert(std::make_pair(Current, V->second));
1335 continue;
1336 }
1337
1338 // If we reach the beginning of the function, then give up.
1339 if (pred_begin(Current) == pred_end(Current))
1340 return 0;
1341
1342 for (pred_iterator PI = pred_begin(Current), PE = pred_end(Current);
1343 PI != PE; ++PI)
1344 if (Visited.insert(*PI))
1345 Stack.push_back(*PI);
1346 }
1347
1348 // If we didn't find instances, give up. Otherwise, perform phi construction.
1349 if (Results.size() == 0)
1350 return 0;
1351 else
1352 return GetValueForBlock(BaseBlock, orig, Results, true);
1353}
1354
Owen Anderson36057c72007-08-14 18:16:29 +00001355/// processInstruction - When calculating availability, handle an instruction
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001356/// by inserting it into the appropriate sets
Owen Andersonaf4240a2008-06-12 19:25:32 +00001357bool GVN::processInstruction(Instruction *I,
Chris Lattner8e1e95c2008-03-21 22:01:16 +00001358 SmallVectorImpl<Instruction*> &toErase) {
Owen Andersonb2303722008-06-18 21:41:49 +00001359 if (LoadInst* L = dyn_cast<LoadInst>(I)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +00001360 bool changed = processLoad(L, toErase);
Owen Andersonb2303722008-06-18 21:41:49 +00001361
1362 if (!changed) {
1363 unsigned num = VN.lookup_or_add(L);
Owen Anderson6fafe842008-06-20 01:15:47 +00001364 localAvail[I->getParent()]->table.insert(std::make_pair(num, L));
Owen Andersonb2303722008-06-18 21:41:49 +00001365 }
1366
1367 return changed;
1368 }
1369
Owen Anderson0ae33ef2008-07-03 17:44:33 +00001370 uint32_t nextNum = VN.getNextUnusedValueNumber();
Owen Andersonb2303722008-06-18 21:41:49 +00001371 unsigned num = VN.lookup_or_add(I);
Chris Lattner8e1e95c2008-03-21 22:01:16 +00001372
Owen Andersone8a290f2009-04-01 23:53:49 +00001373 if (BranchInst* BI = dyn_cast<BranchInst>(I)) {
1374 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
1375
1376 if (!BI->isConditional() || isa<Constant>(BI->getCondition()))
1377 return false;
1378
1379 Value* branchCond = BI->getCondition();
1380 uint32_t condVN = VN.lookup_or_add(branchCond);
1381
1382 BasicBlock* trueSucc = BI->getSuccessor(0);
1383 BasicBlock* falseSucc = BI->getSuccessor(1);
1384
1385 if (trueSucc->getSinglePredecessor())
Owen Andersond672ecb2009-07-03 00:17:18 +00001386 localAvail[trueSucc]->table[condVN] = Context->getConstantIntTrue();
Owen Andersone8a290f2009-04-01 23:53:49 +00001387 if (falseSucc->getSinglePredecessor())
Owen Andersond672ecb2009-07-03 00:17:18 +00001388 localAvail[falseSucc]->table[condVN] = Context->getConstantIntFalse();
Owen Andersone8a290f2009-04-01 23:53:49 +00001389
1390 return false;
1391
Owen Andersone5ffa902008-04-07 09:59:07 +00001392 // Allocations are always uniquely numbered, so we can save time and memory
Owen Andersone8a290f2009-04-01 23:53:49 +00001393 // by fast failing them.
1394 } else if (isa<AllocationInst>(I) || isa<TerminatorInst>(I)) {
Owen Anderson6fafe842008-06-20 01:15:47 +00001395 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
Owen Andersone5ffa902008-04-07 09:59:07 +00001396 return false;
Owen Andersonb2303722008-06-18 21:41:49 +00001397 }
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001398
Owen Anderson62bc33c2007-08-16 22:02:55 +00001399 // Collapse PHI nodes
Owen Anderson31f49672007-08-14 18:33:27 +00001400 if (PHINode* p = dyn_cast<PHINode>(I)) {
Owen Anderson1defe2d2007-08-16 22:51:56 +00001401 Value* constVal = CollapsePhi(p);
Owen Anderson31f49672007-08-14 18:33:27 +00001402
1403 if (constVal) {
Owen Anderson1defe2d2007-08-16 22:51:56 +00001404 for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
1405 PI != PE; ++PI)
Chris Lattnerae199312008-12-09 19:21:47 +00001406 PI->second.erase(p);
Owen Anderson31f49672007-08-14 18:33:27 +00001407
Owen Anderson1defe2d2007-08-16 22:51:56 +00001408 p->replaceAllUsesWith(constVal);
Chris Lattnerbc99be12008-12-09 22:06:23 +00001409 if (isa<PointerType>(constVal->getType()))
1410 MD->invalidateCachedPointerInfo(constVal);
Owen Andersonae53c932008-12-23 00:49:51 +00001411 VN.erase(p);
1412
Owen Anderson1defe2d2007-08-16 22:51:56 +00001413 toErase.push_back(p);
Owen Andersonb2303722008-06-18 21:41:49 +00001414 } else {
Owen Anderson6fafe842008-06-20 01:15:47 +00001415 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
Owen Anderson31f49672007-08-14 18:33:27 +00001416 }
Owen Anderson0ae33ef2008-07-03 17:44:33 +00001417
1418 // If the number we were assigned was a brand new VN, then we don't
1419 // need to do a lookup to see if the number already exists
1420 // somewhere in the domtree: it can't!
1421 } else if (num == nextNum) {
1422 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
1423
Owen Anderson255dafc2008-12-15 02:03:00 +00001424 // Perform fast-path value-number based elimination of values inherited from
1425 // dominators.
Owen Anderson6fafe842008-06-20 01:15:47 +00001426 } else if (Value* repl = lookupNumber(I->getParent(), num)) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001427 // Remove it!
Owen Andersonbf7d0bc2007-07-31 23:27:13 +00001428 VN.erase(I);
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001429 I->replaceAllUsesWith(repl);
Chris Lattnerbc99be12008-12-09 22:06:23 +00001430 if (isa<PointerType>(repl->getType()))
1431 MD->invalidateCachedPointerInfo(repl);
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001432 toErase.push_back(I);
1433 return true;
Owen Anderson255dafc2008-12-15 02:03:00 +00001434
1435#if 0
1436 // Perform slow-pathvalue-number based elimination with phi construction.
1437 } else if (Value* repl = AttemptRedundancyElimination(I, num)) {
1438 // Remove it!
1439 VN.erase(I);
1440 I->replaceAllUsesWith(repl);
1441 if (isa<PointerType>(repl->getType()))
1442 MD->invalidateCachedPointerInfo(repl);
1443 toErase.push_back(I);
1444 return true;
1445#endif
Owen Anderson0ae33ef2008-07-03 17:44:33 +00001446 } else {
Owen Anderson6fafe842008-06-20 01:15:47 +00001447 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001448 }
1449
1450 return false;
1451}
1452
Bill Wendling30788b82008-12-22 22:32:22 +00001453/// runOnFunction - This is the main transformation entry point for a function.
Owen Anderson3e75a422007-08-14 18:04:11 +00001454bool GVN::runOnFunction(Function& F) {
Chris Lattner663e4412008-12-01 00:40:32 +00001455 MD = &getAnalysis<MemoryDependenceAnalysis>();
1456 DT = &getAnalysis<DominatorTree>();
Owen Andersona472c4a2008-05-12 20:15:55 +00001457 VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
Chris Lattner663e4412008-12-01 00:40:32 +00001458 VN.setMemDep(MD);
1459 VN.setDomTree(DT);
Owen Andersonb388ca92007-10-18 19:39:33 +00001460
Owen Anderson3e75a422007-08-14 18:04:11 +00001461 bool changed = false;
1462 bool shouldContinue = true;
1463
Owen Anderson5d0af032008-07-16 17:52:31 +00001464 // Merge unconditional branches, allowing PRE to catch more
1465 // optimization opportunities.
1466 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
1467 BasicBlock* BB = FI;
1468 ++FI;
Owen Andersonb31b06d2008-07-17 00:01:40 +00001469 bool removedBlock = MergeBlockIntoPredecessor(BB, this);
1470 if (removedBlock) NumGVNBlocks++;
1471
1472 changed |= removedBlock;
Owen Anderson5d0af032008-07-16 17:52:31 +00001473 }
1474
Chris Lattnerae199312008-12-09 19:21:47 +00001475 unsigned Iteration = 0;
1476
Owen Anderson3e75a422007-08-14 18:04:11 +00001477 while (shouldContinue) {
Chris Lattnerae199312008-12-09 19:21:47 +00001478 DEBUG(cerr << "GVN iteration: " << Iteration << "\n");
Owen Anderson3e75a422007-08-14 18:04:11 +00001479 shouldContinue = iterateOnFunction(F);
1480 changed |= shouldContinue;
Chris Lattnerae199312008-12-09 19:21:47 +00001481 ++Iteration;
Owen Anderson3e75a422007-08-14 18:04:11 +00001482 }
1483
Owen Andersone98c54c2008-07-18 18:03:38 +00001484 if (EnablePRE) {
Owen Anderson0c7f91c2008-09-03 23:06:07 +00001485 bool PREChanged = true;
1486 while (PREChanged) {
1487 PREChanged = performPRE(F);
Owen Andersone98c54c2008-07-18 18:03:38 +00001488 changed |= PREChanged;
Owen Anderson0c7f91c2008-09-03 23:06:07 +00001489 }
Owen Andersone98c54c2008-07-18 18:03:38 +00001490 }
Chris Lattnerae199312008-12-09 19:21:47 +00001491 // FIXME: Should perform GVN again after PRE does something. PRE can move
1492 // computations into blocks where they become fully redundant. Note that
1493 // we can't do this until PRE's critical edge splitting updates memdep.
1494 // Actually, when this happens, we should just fully integrate PRE into GVN.
Nuno Lopes7cdd9ee2008-10-10 16:25:50 +00001495
1496 cleanupGlobalSets();
1497
Owen Anderson3e75a422007-08-14 18:04:11 +00001498 return changed;
1499}
1500
1501
Owen Anderson255dafc2008-12-15 02:03:00 +00001502bool GVN::processBlock(BasicBlock* BB) {
Chris Lattnerae199312008-12-09 19:21:47 +00001503 // FIXME: Kill off toErase by doing erasing eagerly in a helper function (and
1504 // incrementing BI before processing an instruction).
Owen Andersonaf4240a2008-06-12 19:25:32 +00001505 SmallVector<Instruction*, 8> toErase;
Owen Andersonaf4240a2008-06-12 19:25:32 +00001506 bool changed_function = false;
Owen Andersonb2303722008-06-18 21:41:49 +00001507
Owen Andersonaf4240a2008-06-12 19:25:32 +00001508 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1509 BI != BE;) {
Chris Lattnerb51deb92008-12-05 21:04:20 +00001510 changed_function |= processInstruction(BI, toErase);
Owen Andersonaf4240a2008-06-12 19:25:32 +00001511 if (toErase.empty()) {
1512 ++BI;
1513 continue;
1514 }
1515
1516 // If we need some instructions deleted, do it now.
1517 NumGVNInstr += toErase.size();
1518
1519 // Avoid iterator invalidation.
1520 bool AtStart = BI == BB->begin();
1521 if (!AtStart)
1522 --BI;
1523
1524 for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
Chris Lattner663e4412008-12-01 00:40:32 +00001525 E = toErase.end(); I != E; ++I) {
1526 DEBUG(cerr << "GVN removed: " << **I);
1527 MD->removeInstruction(*I);
Owen Andersonaf4240a2008-06-12 19:25:32 +00001528 (*I)->eraseFromParent();
Bill Wendlingec40d502008-12-22 21:57:30 +00001529 DEBUG(verifyRemoved(*I));
Chris Lattner663e4412008-12-01 00:40:32 +00001530 }
Chris Lattnerae199312008-12-09 19:21:47 +00001531 toErase.clear();
Owen Andersonaf4240a2008-06-12 19:25:32 +00001532
1533 if (AtStart)
1534 BI = BB->begin();
1535 else
1536 ++BI;
Owen Andersonaf4240a2008-06-12 19:25:32 +00001537 }
1538
Owen Andersonaf4240a2008-06-12 19:25:32 +00001539 return changed_function;
1540}
1541
Owen Andersonb2303722008-06-18 21:41:49 +00001542/// performPRE - Perform a purely local form of PRE that looks for diamond
1543/// control flow patterns and attempts to perform simple PRE at the join point.
1544bool GVN::performPRE(Function& F) {
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001545 bool Changed = false;
Owen Anderson5c274ee2008-06-19 19:54:19 +00001546 SmallVector<std::pair<TerminatorInst*, unsigned>, 4> toSplit;
Chris Lattner09713792008-12-01 07:29:03 +00001547 DenseMap<BasicBlock*, Value*> predMap;
Owen Andersonb2303722008-06-18 21:41:49 +00001548 for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
1549 DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
1550 BasicBlock* CurrentBlock = *DI;
1551
1552 // Nothing to PRE in the entry block.
1553 if (CurrentBlock == &F.getEntryBlock()) continue;
1554
1555 for (BasicBlock::iterator BI = CurrentBlock->begin(),
1556 BE = CurrentBlock->end(); BI != BE; ) {
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001557 Instruction *CurInst = BI++;
Duncan Sands7af1c782009-05-06 06:49:50 +00001558
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001559 if (isa<AllocationInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
John Criswell090c0a22009-03-10 15:04:53 +00001560 isa<PHINode>(CurInst) || (CurInst->getType() == Type::VoidTy) ||
Duncan Sands7af1c782009-05-06 06:49:50 +00001561 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
John Criswell090c0a22009-03-10 15:04:53 +00001562 isa<DbgInfoIntrinsic>(CurInst))
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001563 continue;
Duncan Sands7af1c782009-05-06 06:49:50 +00001564
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001565 uint32_t valno = VN.lookup(CurInst);
Owen Andersonb2303722008-06-18 21:41:49 +00001566
1567 // Look for the predecessors for PRE opportunities. We're
1568 // only trying to solve the basic diamond case, where
1569 // a value is computed in the successor and one predecessor,
1570 // but not the other. We also explicitly disallow cases
1571 // where the successor is its own predecessor, because they're
1572 // more complicated to get right.
1573 unsigned numWith = 0;
1574 unsigned numWithout = 0;
1575 BasicBlock* PREPred = 0;
Chris Lattner09713792008-12-01 07:29:03 +00001576 predMap.clear();
1577
Owen Andersonb2303722008-06-18 21:41:49 +00001578 for (pred_iterator PI = pred_begin(CurrentBlock),
1579 PE = pred_end(CurrentBlock); PI != PE; ++PI) {
1580 // We're not interested in PRE where the block is its
Owen Anderson6fafe842008-06-20 01:15:47 +00001581 // own predecessor, on in blocks with predecessors
1582 // that are not reachable.
1583 if (*PI == CurrentBlock) {
Owen Andersonb2303722008-06-18 21:41:49 +00001584 numWithout = 2;
Owen Anderson6fafe842008-06-20 01:15:47 +00001585 break;
1586 } else if (!localAvail.count(*PI)) {
1587 numWithout = 2;
1588 break;
1589 }
1590
1591 DenseMap<uint32_t, Value*>::iterator predV =
1592 localAvail[*PI]->table.find(valno);
1593 if (predV == localAvail[*PI]->table.end()) {
Owen Andersonb2303722008-06-18 21:41:49 +00001594 PREPred = *PI;
1595 numWithout++;
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001596 } else if (predV->second == CurInst) {
Owen Andersonb2303722008-06-18 21:41:49 +00001597 numWithout = 2;
1598 } else {
Owen Anderson6fafe842008-06-20 01:15:47 +00001599 predMap[*PI] = predV->second;
Owen Andersonb2303722008-06-18 21:41:49 +00001600 numWith++;
1601 }
1602 }
1603
1604 // Don't do PRE when it might increase code size, i.e. when
1605 // we would need to insert instructions in more than one pred.
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001606 if (numWithout != 1 || numWith == 0)
Owen Andersonb2303722008-06-18 21:41:49 +00001607 continue;
Owen Andersonb2303722008-06-18 21:41:49 +00001608
Owen Anderson5c274ee2008-06-19 19:54:19 +00001609 // We can't do PRE safely on a critical edge, so instead we schedule
1610 // the edge to be split and perform the PRE the next time we iterate
1611 // on the function.
1612 unsigned succNum = 0;
1613 for (unsigned i = 0, e = PREPred->getTerminator()->getNumSuccessors();
1614 i != e; ++i)
Owen Anderson0c7f91c2008-09-03 23:06:07 +00001615 if (PREPred->getTerminator()->getSuccessor(i) == CurrentBlock) {
Owen Anderson5c274ee2008-06-19 19:54:19 +00001616 succNum = i;
1617 break;
1618 }
1619
1620 if (isCriticalEdge(PREPred->getTerminator(), succNum)) {
1621 toSplit.push_back(std::make_pair(PREPred->getTerminator(), succNum));
Owen Anderson5c274ee2008-06-19 19:54:19 +00001622 continue;
1623 }
1624
Owen Andersonb2303722008-06-18 21:41:49 +00001625 // Instantiate the expression the in predecessor that lacked it.
1626 // Because we are going top-down through the block, all value numbers
1627 // will be available in the predecessor by the time we need them. Any
1628 // that weren't original present will have been instantiated earlier
1629 // in this loop.
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001630 Instruction* PREInstr = CurInst->clone();
Owen Andersonb2303722008-06-18 21:41:49 +00001631 bool success = true;
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001632 for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) {
1633 Value *Op = PREInstr->getOperand(i);
1634 if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
1635 continue;
1636
1637 if (Value *V = lookupNumber(PREPred, VN.lookup(Op))) {
1638 PREInstr->setOperand(i, V);
1639 } else {
1640 success = false;
1641 break;
Owen Andersonc45996b2008-07-11 20:05:13 +00001642 }
Owen Andersonb2303722008-06-18 21:41:49 +00001643 }
1644
1645 // Fail out if we encounter an operand that is not available in
1646 // the PRE predecessor. This is typically because of loads which
1647 // are not value numbered precisely.
1648 if (!success) {
1649 delete PREInstr;
Bill Wendling70ded192008-12-22 22:14:07 +00001650 DEBUG(verifyRemoved(PREInstr));
Owen Andersonb2303722008-06-18 21:41:49 +00001651 continue;
1652 }
1653
1654 PREInstr->insertBefore(PREPred->getTerminator());
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001655 PREInstr->setName(CurInst->getName() + ".pre");
Owen Anderson6fafe842008-06-20 01:15:47 +00001656 predMap[PREPred] = PREInstr;
Owen Andersonb2303722008-06-18 21:41:49 +00001657 VN.add(PREInstr, valno);
1658 NumGVNPRE++;
1659
1660 // Update the availability map to include the new instruction.
Owen Anderson6fafe842008-06-20 01:15:47 +00001661 localAvail[PREPred]->table.insert(std::make_pair(valno, PREInstr));
Owen Andersonb2303722008-06-18 21:41:49 +00001662
1663 // Create a PHI to make the value available in this block.
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001664 PHINode* Phi = PHINode::Create(CurInst->getType(),
1665 CurInst->getName() + ".pre-phi",
Owen Andersonb2303722008-06-18 21:41:49 +00001666 CurrentBlock->begin());
1667 for (pred_iterator PI = pred_begin(CurrentBlock),
1668 PE = pred_end(CurrentBlock); PI != PE; ++PI)
Owen Anderson6fafe842008-06-20 01:15:47 +00001669 Phi->addIncoming(predMap[*PI], *PI);
Owen Andersonb2303722008-06-18 21:41:49 +00001670
1671 VN.add(Phi, valno);
Owen Anderson6fafe842008-06-20 01:15:47 +00001672 localAvail[CurrentBlock]->table[valno] = Phi;
Owen Andersonb2303722008-06-18 21:41:49 +00001673
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001674 CurInst->replaceAllUsesWith(Phi);
Chris Lattnerbc99be12008-12-09 22:06:23 +00001675 if (isa<PointerType>(Phi->getType()))
1676 MD->invalidateCachedPointerInfo(Phi);
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001677 VN.erase(CurInst);
Owen Andersonb2303722008-06-18 21:41:49 +00001678
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001679 DEBUG(cerr << "GVN PRE removed: " << *CurInst);
1680 MD->removeInstruction(CurInst);
1681 CurInst->eraseFromParent();
Bill Wendlingec40d502008-12-22 21:57:30 +00001682 DEBUG(verifyRemoved(CurInst));
Chris Lattnerd0f5bfc2008-12-01 07:35:54 +00001683 Changed = true;
Owen Andersonb2303722008-06-18 21:41:49 +00001684 }
1685 }
1686
Owen Anderson5c274ee2008-06-19 19:54:19 +00001687 for (SmallVector<std::pair<TerminatorInst*, unsigned>, 4>::iterator
Anton Korobeynikov64b53562008-12-05 19:38:49 +00001688 I = toSplit.begin(), E = toSplit.end(); I != E; ++I)
Owen Anderson5c274ee2008-06-19 19:54:19 +00001689 SplitCriticalEdge(I->first, I->second, this);
1690
Anton Korobeynikov64b53562008-12-05 19:38:49 +00001691 return Changed || toSplit.size();
Owen Andersonb2303722008-06-18 21:41:49 +00001692}
1693
Bill Wendling30788b82008-12-22 22:32:22 +00001694/// iterateOnFunction - Executes one iteration of GVN
Owen Anderson3e75a422007-08-14 18:04:11 +00001695bool GVN::iterateOnFunction(Function &F) {
Nuno Lopes7cdd9ee2008-10-10 16:25:50 +00001696 cleanupGlobalSets();
Chris Lattner2e607012008-03-21 21:33:23 +00001697
Owen Andersone8a290f2009-04-01 23:53:49 +00001698 for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
1699 DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
1700 if (DI->getIDom())
1701 localAvail[DI->getBlock()] =
1702 new ValueNumberScope(localAvail[DI->getIDom()->getBlock()]);
1703 else
1704 localAvail[DI->getBlock()] = new ValueNumberScope(0);
1705 }
1706
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001707 // Top-down walk of the dominator tree
Owen Andersonb2303722008-06-18 21:41:49 +00001708 bool changed = false;
Owen Andersonc34d1122008-12-15 03:52:17 +00001709#if 0
1710 // Needed for value numbering with phi construction to work.
Owen Anderson255dafc2008-12-15 02:03:00 +00001711 ReversePostOrderTraversal<Function*> RPOT(&F);
1712 for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(),
1713 RE = RPOT.end(); RI != RE; ++RI)
1714 changed |= processBlock(*RI);
Owen Andersonc34d1122008-12-15 03:52:17 +00001715#else
1716 for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
1717 DE = df_end(DT->getRootNode()); DI != DE; ++DI)
1718 changed |= processBlock(DI->getBlock());
1719#endif
1720
Owen Anderson5d0af032008-07-16 17:52:31 +00001721 return changed;
Owen Anderson1ad2cb72007-07-24 17:55:58 +00001722}
Nuno Lopes7cdd9ee2008-10-10 16:25:50 +00001723
1724void GVN::cleanupGlobalSets() {
1725 VN.clear();
1726 phiMap.clear();
1727
1728 for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
1729 I = localAvail.begin(), E = localAvail.end(); I != E; ++I)
1730 delete I->second;
1731 localAvail.clear();
1732}
Bill Wendling246dbbb2008-12-22 21:36:08 +00001733
1734/// verifyRemoved - Verify that the specified instruction does not occur in our
1735/// internal data structures.
Bill Wendling6d463f22008-12-22 22:28:56 +00001736void GVN::verifyRemoved(const Instruction *Inst) const {
1737 VN.verifyRemoved(Inst);
Bill Wendling70ded192008-12-22 22:14:07 +00001738
1739 // Walk through the PHI map to make sure the instruction isn't hiding in there
1740 // somewhere.
1741 for (PhiMapType::iterator
Bill Wendling6d463f22008-12-22 22:28:56 +00001742 I = phiMap.begin(), E = phiMap.end(); I != E; ++I) {
1743 assert(I->first != Inst && "Inst is still a key in PHI map!");
Bill Wendling70ded192008-12-22 22:14:07 +00001744
1745 for (SmallPtrSet<Instruction*, 4>::iterator
Bill Wendling6d463f22008-12-22 22:28:56 +00001746 II = I->second.begin(), IE = I->second.end(); II != IE; ++II) {
1747 assert(*II != Inst && "Inst is still a value in PHI map!");
1748 }
1749 }
1750
1751 // Walk through the value number scope to make sure the instruction isn't
1752 // ferreted away in it.
1753 for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
1754 I = localAvail.begin(), E = localAvail.end(); I != E; ++I) {
1755 const ValueNumberScope *VNS = I->second;
1756
1757 while (VNS) {
1758 for (DenseMap<uint32_t, Value*>::iterator
1759 II = VNS->table.begin(), IE = VNS->table.end(); II != IE; ++II) {
1760 assert(II->second != Inst && "Inst still in value numbering scope!");
1761 }
1762
1763 VNS = VNS->parent;
Bill Wendling70ded192008-12-22 22:14:07 +00001764 }
1765 }
Bill Wendling246dbbb2008-12-22 21:36:08 +00001766}