blob: 11fa336c7bbeb707f23ec5f706edd57ad7485d1b [file] [log] [blame]
Owen Anderson85c40642007-07-24 17:55:58 +00001//===- GVN.cpp - Eliminate redundant values and loads ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Owen Anderson and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs global value numbering to eliminate fully redundant
11// instructions. It also performs simple dead load elimination.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "gvn"
Owen Andersonacfa3ad2007-07-26 18:26:51 +000016
Owen Anderson85c40642007-07-24 17:55:58 +000017#include "llvm/Transforms/Scalar.h"
Owen Anderson5d72a422007-07-25 19:57:03 +000018#include "llvm/BasicBlock.h"
Owen Andersonacfa3ad2007-07-26 18:26:51 +000019#include "llvm/Constants.h"
Owen Anderson85c40642007-07-24 17:55:58 +000020#include "llvm/DerivedTypes.h"
Owen Andersonacfa3ad2007-07-26 18:26:51 +000021#include "llvm/Function.h"
22#include "llvm/Instructions.h"
23#include "llvm/Value.h"
Owen Anderson85c40642007-07-24 17:55:58 +000024#include "llvm/ADT/BitVector.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/DepthFirstIterator.h"
27#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/Statistic.h"
Owen Anderson5e9366f2007-10-18 19:39:33 +000030#include "llvm/Analysis/Dominators.h"
31#include "llvm/Analysis/AliasAnalysis.h"
Owen Anderson85c40642007-07-24 17:55:58 +000032#include "llvm/Analysis/MemoryDependenceAnalysis.h"
33#include "llvm/Support/CFG.h"
34#include "llvm/Support/Compiler.h"
35using namespace llvm;
36
37//===----------------------------------------------------------------------===//
38// ValueTable Class
39//===----------------------------------------------------------------------===//
40
41/// This class holds the mapping between values and value numbers. It is used
42/// as an efficient mechanism to determine the expression-wise equivalence of
43/// two values.
44namespace {
45 struct VISIBILITY_HIDDEN Expression {
46 enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM,
47 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
48 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
49 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
50 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
51 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
52 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
53 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
54 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
Owen Anderson5e9366f2007-10-18 19:39:33 +000055 PTRTOINT, INTTOPTR, BITCAST, GEP, CALL, EMPTY,
Owen Anderson85c40642007-07-24 17:55:58 +000056 TOMBSTONE };
57
58 ExpressionOpcode opcode;
59 const Type* type;
60 uint32_t firstVN;
61 uint32_t secondVN;
62 uint32_t thirdVN;
63 SmallVector<uint32_t, 4> varargs;
Owen Anderson5e9366f2007-10-18 19:39:33 +000064 Value* function;
Owen Anderson85c40642007-07-24 17:55:58 +000065
66 Expression() { }
67 Expression(ExpressionOpcode o) : opcode(o) { }
68
69 bool operator==(const Expression &other) const {
70 if (opcode != other.opcode)
71 return false;
72 else if (opcode == EMPTY || opcode == TOMBSTONE)
73 return true;
74 else if (type != other.type)
75 return false;
Owen Anderson5e9366f2007-10-18 19:39:33 +000076 else if (function != other.function)
77 return false;
Owen Anderson85c40642007-07-24 17:55:58 +000078 else if (firstVN != other.firstVN)
79 return false;
80 else if (secondVN != other.secondVN)
81 return false;
82 else if (thirdVN != other.thirdVN)
83 return false;
84 else {
85 if (varargs.size() != other.varargs.size())
86 return false;
87
88 for (size_t i = 0; i < varargs.size(); ++i)
89 if (varargs[i] != other.varargs[i])
90 return false;
91
92 return true;
93 }
94 }
95
96 bool operator!=(const Expression &other) const {
97 if (opcode != other.opcode)
98 return true;
99 else if (opcode == EMPTY || opcode == TOMBSTONE)
100 return false;
101 else if (type != other.type)
102 return true;
Owen Anderson5e9366f2007-10-18 19:39:33 +0000103 else if (function != other.function)
104 return true;
Owen Anderson85c40642007-07-24 17:55:58 +0000105 else if (firstVN != other.firstVN)
106 return true;
107 else if (secondVN != other.secondVN)
108 return true;
109 else if (thirdVN != other.thirdVN)
110 return true;
111 else {
112 if (varargs.size() != other.varargs.size())
113 return true;
114
115 for (size_t i = 0; i < varargs.size(); ++i)
116 if (varargs[i] != other.varargs[i])
117 return true;
118
119 return false;
120 }
121 }
122 };
123
124 class VISIBILITY_HIDDEN ValueTable {
125 private:
126 DenseMap<Value*, uint32_t> valueNumbering;
127 DenseMap<Expression, uint32_t> expressionNumbering;
Owen Anderson5e9366f2007-10-18 19:39:33 +0000128 AliasAnalysis* AA;
Owen Anderson85c40642007-07-24 17:55:58 +0000129
130 uint32_t nextValueNumber;
131
132 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
133 Expression::ExpressionOpcode getOpcode(CmpInst* C);
134 Expression::ExpressionOpcode getOpcode(CastInst* C);
135 Expression create_expression(BinaryOperator* BO);
136 Expression create_expression(CmpInst* C);
137 Expression create_expression(ShuffleVectorInst* V);
138 Expression create_expression(ExtractElementInst* C);
139 Expression create_expression(InsertElementInst* V);
140 Expression create_expression(SelectInst* V);
141 Expression create_expression(CastInst* C);
142 Expression create_expression(GetElementPtrInst* G);
Owen Anderson5e9366f2007-10-18 19:39:33 +0000143 Expression create_expression(CallInst* C);
Owen Anderson85c40642007-07-24 17:55:58 +0000144 public:
Owen Anderson5e9366f2007-10-18 19:39:33 +0000145 ValueTable() : nextValueNumber(1) { }
Owen Anderson85c40642007-07-24 17:55:58 +0000146 uint32_t lookup_or_add(Value* V);
147 uint32_t lookup(Value* V) const;
148 void add(Value* V, uint32_t num);
149 void clear();
150 void erase(Value* v);
151 unsigned size();
Owen Anderson5e9366f2007-10-18 19:39:33 +0000152 void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
Owen Anderson85c40642007-07-24 17:55:58 +0000153 };
154}
155
156namespace llvm {
Chris Lattner92eea072007-09-17 18:34:04 +0000157template <> struct DenseMapInfo<Expression> {
Owen Andersonbf8a3eb2007-08-02 18:16:06 +0000158 static inline Expression getEmptyKey() {
159 return Expression(Expression::EMPTY);
160 }
161
162 static inline Expression getTombstoneKey() {
163 return Expression(Expression::TOMBSTONE);
164 }
Owen Anderson85c40642007-07-24 17:55:58 +0000165
166 static unsigned getHashValue(const Expression e) {
167 unsigned hash = e.opcode;
168
169 hash = e.firstVN + hash * 37;
170 hash = e.secondVN + hash * 37;
171 hash = e.thirdVN + hash * 37;
172
173 hash = (unsigned)((uintptr_t)e.type >> 4) ^
174 (unsigned)((uintptr_t)e.type >> 9) +
175 hash * 37;
176
Owen Andersonbf8a3eb2007-08-02 18:16:06 +0000177 for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
178 E = e.varargs.end(); I != E; ++I)
Owen Anderson85c40642007-07-24 17:55:58 +0000179 hash = *I + hash * 37;
180
Owen Anderson5e9366f2007-10-18 19:39:33 +0000181 hash = (unsigned)((uintptr_t)e.function >> 4) ^
182 (unsigned)((uintptr_t)e.function >> 9) +
183 hash * 37;
184
Owen Anderson85c40642007-07-24 17:55:58 +0000185 return hash;
186 }
Chris Lattner92eea072007-09-17 18:34:04 +0000187 static bool isEqual(const Expression &LHS, const Expression &RHS) {
188 return LHS == RHS;
189 }
Owen Anderson85c40642007-07-24 17:55:58 +0000190 static bool isPod() { return true; }
191};
192}
193
194//===----------------------------------------------------------------------===//
195// ValueTable Internal Functions
196//===----------------------------------------------------------------------===//
197Expression::ExpressionOpcode
198 ValueTable::getOpcode(BinaryOperator* BO) {
199 switch(BO->getOpcode()) {
200 case Instruction::Add:
201 return Expression::ADD;
202 case Instruction::Sub:
203 return Expression::SUB;
204 case Instruction::Mul:
205 return Expression::MUL;
206 case Instruction::UDiv:
207 return Expression::UDIV;
208 case Instruction::SDiv:
209 return Expression::SDIV;
210 case Instruction::FDiv:
211 return Expression::FDIV;
212 case Instruction::URem:
213 return Expression::UREM;
214 case Instruction::SRem:
215 return Expression::SREM;
216 case Instruction::FRem:
217 return Expression::FREM;
218 case Instruction::Shl:
219 return Expression::SHL;
220 case Instruction::LShr:
221 return Expression::LSHR;
222 case Instruction::AShr:
223 return Expression::ASHR;
224 case Instruction::And:
225 return Expression::AND;
226 case Instruction::Or:
227 return Expression::OR;
228 case Instruction::Xor:
229 return Expression::XOR;
230
231 // THIS SHOULD NEVER HAPPEN
232 default:
233 assert(0 && "Binary operator with unknown opcode?");
234 return Expression::ADD;
235 }
236}
237
238Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
239 if (C->getOpcode() == Instruction::ICmp) {
240 switch (C->getPredicate()) {
241 case ICmpInst::ICMP_EQ:
242 return Expression::ICMPEQ;
243 case ICmpInst::ICMP_NE:
244 return Expression::ICMPNE;
245 case ICmpInst::ICMP_UGT:
246 return Expression::ICMPUGT;
247 case ICmpInst::ICMP_UGE:
248 return Expression::ICMPUGE;
249 case ICmpInst::ICMP_ULT:
250 return Expression::ICMPULT;
251 case ICmpInst::ICMP_ULE:
252 return Expression::ICMPULE;
253 case ICmpInst::ICMP_SGT:
254 return Expression::ICMPSGT;
255 case ICmpInst::ICMP_SGE:
256 return Expression::ICMPSGE;
257 case ICmpInst::ICMP_SLT:
258 return Expression::ICMPSLT;
259 case ICmpInst::ICMP_SLE:
260 return Expression::ICMPSLE;
261
262 // THIS SHOULD NEVER HAPPEN
263 default:
264 assert(0 && "Comparison with unknown predicate?");
265 return Expression::ICMPEQ;
266 }
267 } else {
268 switch (C->getPredicate()) {
269 case FCmpInst::FCMP_OEQ:
270 return Expression::FCMPOEQ;
271 case FCmpInst::FCMP_OGT:
272 return Expression::FCMPOGT;
273 case FCmpInst::FCMP_OGE:
274 return Expression::FCMPOGE;
275 case FCmpInst::FCMP_OLT:
276 return Expression::FCMPOLT;
277 case FCmpInst::FCMP_OLE:
278 return Expression::FCMPOLE;
279 case FCmpInst::FCMP_ONE:
280 return Expression::FCMPONE;
281 case FCmpInst::FCMP_ORD:
282 return Expression::FCMPORD;
283 case FCmpInst::FCMP_UNO:
284 return Expression::FCMPUNO;
285 case FCmpInst::FCMP_UEQ:
286 return Expression::FCMPUEQ;
287 case FCmpInst::FCMP_UGT:
288 return Expression::FCMPUGT;
289 case FCmpInst::FCMP_UGE:
290 return Expression::FCMPUGE;
291 case FCmpInst::FCMP_ULT:
292 return Expression::FCMPULT;
293 case FCmpInst::FCMP_ULE:
294 return Expression::FCMPULE;
295 case FCmpInst::FCMP_UNE:
296 return Expression::FCMPUNE;
297
298 // THIS SHOULD NEVER HAPPEN
299 default:
300 assert(0 && "Comparison with unknown predicate?");
301 return Expression::FCMPOEQ;
302 }
303 }
304}
305
306Expression::ExpressionOpcode
307 ValueTable::getOpcode(CastInst* C) {
308 switch(C->getOpcode()) {
309 case Instruction::Trunc:
310 return Expression::TRUNC;
311 case Instruction::ZExt:
312 return Expression::ZEXT;
313 case Instruction::SExt:
314 return Expression::SEXT;
315 case Instruction::FPToUI:
316 return Expression::FPTOUI;
317 case Instruction::FPToSI:
318 return Expression::FPTOSI;
319 case Instruction::UIToFP:
320 return Expression::UITOFP;
321 case Instruction::SIToFP:
322 return Expression::SITOFP;
323 case Instruction::FPTrunc:
324 return Expression::FPTRUNC;
325 case Instruction::FPExt:
326 return Expression::FPEXT;
327 case Instruction::PtrToInt:
328 return Expression::PTRTOINT;
329 case Instruction::IntToPtr:
330 return Expression::INTTOPTR;
331 case Instruction::BitCast:
332 return Expression::BITCAST;
333
334 // THIS SHOULD NEVER HAPPEN
335 default:
336 assert(0 && "Cast operator with unknown opcode?");
337 return Expression::BITCAST;
338 }
339}
340
Owen Anderson5e9366f2007-10-18 19:39:33 +0000341Expression ValueTable::create_expression(CallInst* C) {
342 Expression e;
343
344 e.type = C->getType();
345 e.firstVN = 0;
346 e.secondVN = 0;
347 e.thirdVN = 0;
348 e.function = C->getCalledFunction();
349 e.opcode = Expression::CALL;
350
351 for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
352 I != E; ++I)
353 e.varargs.push_back(lookup_or_add(*I));
354
355 return e;
356}
357
Owen Anderson85c40642007-07-24 17:55:58 +0000358Expression ValueTable::create_expression(BinaryOperator* BO) {
359 Expression e;
360
361 e.firstVN = lookup_or_add(BO->getOperand(0));
362 e.secondVN = lookup_or_add(BO->getOperand(1));
363 e.thirdVN = 0;
Owen Anderson5e9366f2007-10-18 19:39:33 +0000364 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000365 e.type = BO->getType();
366 e.opcode = getOpcode(BO);
367
368 return e;
369}
370
371Expression ValueTable::create_expression(CmpInst* C) {
372 Expression e;
373
374 e.firstVN = lookup_or_add(C->getOperand(0));
375 e.secondVN = lookup_or_add(C->getOperand(1));
376 e.thirdVN = 0;
Owen Anderson5e9366f2007-10-18 19:39:33 +0000377 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000378 e.type = C->getType();
379 e.opcode = getOpcode(C);
380
381 return e;
382}
383
384Expression ValueTable::create_expression(CastInst* C) {
385 Expression e;
386
387 e.firstVN = lookup_or_add(C->getOperand(0));
388 e.secondVN = 0;
389 e.thirdVN = 0;
Owen Anderson5e9366f2007-10-18 19:39:33 +0000390 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000391 e.type = C->getType();
392 e.opcode = getOpcode(C);
393
394 return e;
395}
396
397Expression ValueTable::create_expression(ShuffleVectorInst* S) {
398 Expression e;
399
400 e.firstVN = lookup_or_add(S->getOperand(0));
401 e.secondVN = lookup_or_add(S->getOperand(1));
402 e.thirdVN = lookup_or_add(S->getOperand(2));
Owen Anderson5e9366f2007-10-18 19:39:33 +0000403 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000404 e.type = S->getType();
405 e.opcode = Expression::SHUFFLE;
406
407 return e;
408}
409
410Expression ValueTable::create_expression(ExtractElementInst* E) {
411 Expression e;
412
413 e.firstVN = lookup_or_add(E->getOperand(0));
414 e.secondVN = lookup_or_add(E->getOperand(1));
415 e.thirdVN = 0;
Owen Anderson5e9366f2007-10-18 19:39:33 +0000416 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000417 e.type = E->getType();
418 e.opcode = Expression::EXTRACT;
419
420 return e;
421}
422
423Expression ValueTable::create_expression(InsertElementInst* I) {
424 Expression e;
425
426 e.firstVN = lookup_or_add(I->getOperand(0));
427 e.secondVN = lookup_or_add(I->getOperand(1));
428 e.thirdVN = lookup_or_add(I->getOperand(2));
Owen Anderson5e9366f2007-10-18 19:39:33 +0000429 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000430 e.type = I->getType();
431 e.opcode = Expression::INSERT;
432
433 return e;
434}
435
436Expression ValueTable::create_expression(SelectInst* I) {
437 Expression e;
438
439 e.firstVN = lookup_or_add(I->getCondition());
440 e.secondVN = lookup_or_add(I->getTrueValue());
441 e.thirdVN = lookup_or_add(I->getFalseValue());
Owen Anderson5e9366f2007-10-18 19:39:33 +0000442 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000443 e.type = I->getType();
444 e.opcode = Expression::SELECT;
445
446 return e;
447}
448
449Expression ValueTable::create_expression(GetElementPtrInst* G) {
450 Expression e;
451
452 e.firstVN = lookup_or_add(G->getPointerOperand());
453 e.secondVN = 0;
454 e.thirdVN = 0;
Owen Anderson5e9366f2007-10-18 19:39:33 +0000455 e.function = 0;
Owen Anderson85c40642007-07-24 17:55:58 +0000456 e.type = G->getType();
457 e.opcode = Expression::GEP;
458
459 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
460 I != E; ++I)
461 e.varargs.push_back(lookup_or_add(*I));
462
463 return e;
464}
465
466//===----------------------------------------------------------------------===//
467// ValueTable External Functions
468//===----------------------------------------------------------------------===//
469
470/// lookup_or_add - Returns the value number for the specified value, assigning
471/// it a new number if it did not have one before.
472uint32_t ValueTable::lookup_or_add(Value* V) {
473 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
474 if (VI != valueNumbering.end())
475 return VI->second;
476
Owen Anderson5e9366f2007-10-18 19:39:33 +0000477 if (CallInst* C = dyn_cast<CallInst>(V)) {
478 if (C->getCalledFunction() &&
Owen Anderson8b6f04e2007-11-26 02:26:36 +0000479 (AA->doesNotAccessMemory(C->getCalledFunction()) ||
480 AA->onlyReadsMemory(C->getCalledFunction()))) {
Owen Anderson5e9366f2007-10-18 19:39:33 +0000481 Expression e = create_expression(C);
482
483 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
484 if (EI != expressionNumbering.end()) {
485 valueNumbering.insert(std::make_pair(V, EI->second));
486 return EI->second;
487 } else {
488 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
489 valueNumbering.insert(std::make_pair(V, nextValueNumber));
490
491 return nextValueNumber++;
492 }
493 } else {
494 valueNumbering.insert(std::make_pair(V, nextValueNumber));
495 return nextValueNumber++;
496 }
497 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
Owen Anderson85c40642007-07-24 17:55:58 +0000498 Expression e = create_expression(BO);
499
500 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
501 if (EI != expressionNumbering.end()) {
502 valueNumbering.insert(std::make_pair(V, EI->second));
503 return EI->second;
504 } else {
505 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
506 valueNumbering.insert(std::make_pair(V, nextValueNumber));
507
508 return nextValueNumber++;
509 }
510 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
511 Expression e = create_expression(C);
512
513 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
514 if (EI != expressionNumbering.end()) {
515 valueNumbering.insert(std::make_pair(V, EI->second));
516 return EI->second;
517 } else {
518 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
519 valueNumbering.insert(std::make_pair(V, nextValueNumber));
520
521 return nextValueNumber++;
522 }
523 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
524 Expression e = create_expression(U);
525
526 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
527 if (EI != expressionNumbering.end()) {
528 valueNumbering.insert(std::make_pair(V, EI->second));
529 return EI->second;
530 } else {
531 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
532 valueNumbering.insert(std::make_pair(V, nextValueNumber));
533
534 return nextValueNumber++;
535 }
536 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
537 Expression e = create_expression(U);
538
539 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
540 if (EI != expressionNumbering.end()) {
541 valueNumbering.insert(std::make_pair(V, EI->second));
542 return EI->second;
543 } else {
544 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
545 valueNumbering.insert(std::make_pair(V, nextValueNumber));
546
547 return nextValueNumber++;
548 }
549 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
550 Expression e = create_expression(U);
551
552 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
553 if (EI != expressionNumbering.end()) {
554 valueNumbering.insert(std::make_pair(V, EI->second));
555 return EI->second;
556 } else {
557 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
558 valueNumbering.insert(std::make_pair(V, nextValueNumber));
559
560 return nextValueNumber++;
561 }
562 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
563 Expression e = create_expression(U);
564
565 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
566 if (EI != expressionNumbering.end()) {
567 valueNumbering.insert(std::make_pair(V, EI->second));
568 return EI->second;
569 } else {
570 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
571 valueNumbering.insert(std::make_pair(V, nextValueNumber));
572
573 return nextValueNumber++;
574 }
575 } else if (CastInst* U = dyn_cast<CastInst>(V)) {
576 Expression e = create_expression(U);
577
578 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
579 if (EI != expressionNumbering.end()) {
580 valueNumbering.insert(std::make_pair(V, EI->second));
581 return EI->second;
582 } else {
583 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
584 valueNumbering.insert(std::make_pair(V, nextValueNumber));
585
586 return nextValueNumber++;
587 }
588 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
589 Expression e = create_expression(U);
590
591 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
592 if (EI != expressionNumbering.end()) {
593 valueNumbering.insert(std::make_pair(V, EI->second));
594 return EI->second;
595 } else {
596 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
597 valueNumbering.insert(std::make_pair(V, nextValueNumber));
598
599 return nextValueNumber++;
600 }
601 } else {
602 valueNumbering.insert(std::make_pair(V, nextValueNumber));
603 return nextValueNumber++;
604 }
605}
606
607/// lookup - Returns the value number of the specified value. Fails if
608/// the value has not yet been numbered.
609uint32_t ValueTable::lookup(Value* V) const {
610 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
611 if (VI != valueNumbering.end())
612 return VI->second;
613 else
614 assert(0 && "Value not numbered?");
615
616 return 0;
617}
618
619/// clear - Remove all entries from the ValueTable
620void ValueTable::clear() {
621 valueNumbering.clear();
622 expressionNumbering.clear();
623 nextValueNumber = 1;
624}
625
Owen Anderson5aff8002007-07-31 23:27:13 +0000626/// erase - Remove a value from the value numbering
627void ValueTable::erase(Value* V) {
628 valueNumbering.erase(V);
629}
630
Owen Anderson85c40642007-07-24 17:55:58 +0000631//===----------------------------------------------------------------------===//
632// ValueNumberedSet Class
633//===----------------------------------------------------------------------===//
634namespace {
635class ValueNumberedSet {
636 private:
637 SmallPtrSet<Value*, 8> contents;
638 BitVector numbers;
639 public:
640 ValueNumberedSet() { numbers.resize(1); }
641 ValueNumberedSet(const ValueNumberedSet& other) {
642 numbers = other.numbers;
643 contents = other.contents;
644 }
645
646 typedef SmallPtrSet<Value*, 8>::iterator iterator;
647
648 iterator begin() { return contents.begin(); }
649 iterator end() { return contents.end(); }
650
651 bool insert(Value* v) { return contents.insert(v); }
652 void insert(iterator I, iterator E) { contents.insert(I, E); }
653 void erase(Value* v) { contents.erase(v); }
654 unsigned count(Value* v) { return contents.count(v); }
655 size_t size() { return contents.size(); }
656
657 void set(unsigned i) {
658 if (i >= numbers.size())
659 numbers.resize(i+1);
660
661 numbers.set(i);
662 }
663
664 void operator=(const ValueNumberedSet& other) {
665 contents = other.contents;
666 numbers = other.numbers;
667 }
668
669 void reset(unsigned i) {
670 if (i < numbers.size())
671 numbers.reset(i);
672 }
673
674 bool test(unsigned i) {
675 if (i >= numbers.size())
676 return false;
677
678 return numbers.test(i);
679 }
680
681 void clear() {
682 contents.clear();
683 numbers.clear();
684 }
685};
686}
687
688//===----------------------------------------------------------------------===//
689// GVN Pass
690//===----------------------------------------------------------------------===//
691
692namespace {
693
694 class VISIBILITY_HIDDEN GVN : public FunctionPass {
695 bool runOnFunction(Function &F);
696 public:
697 static char ID; // Pass identification, replacement for typeid
698 GVN() : FunctionPass((intptr_t)&ID) { }
699
700 private:
701 ValueTable VN;
702
703 DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
704
Owen Anderson5b299672007-08-07 23:12:31 +0000705 typedef DenseMap<Value*, SmallPtrSet<Instruction*, 4> > PhiMapType;
706 PhiMapType phiMap;
707
708
Owen Anderson85c40642007-07-24 17:55:58 +0000709 // This transformation requires dominator postdominator info
710 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
711 AU.setPreservesCFG();
712 AU.addRequired<DominatorTree>();
713 AU.addRequired<MemoryDependenceAnalysis>();
Owen Anderson5e9366f2007-10-18 19:39:33 +0000714 AU.addRequired<AliasAnalysis>();
715 AU.addPreserved<AliasAnalysis>();
Owen Anderson85c40642007-07-24 17:55:58 +0000716 AU.addPreserved<MemoryDependenceAnalysis>();
717 }
718
719 // Helper fuctions
720 // FIXME: eliminate or document these better
721 Value* find_leader(ValueNumberedSet& vals, uint32_t v) ;
722 void val_insert(ValueNumberedSet& s, Value* v);
723 bool processLoad(LoadInst* L,
724 DenseMap<Value*, LoadInst*>& lastLoad,
725 SmallVector<Instruction*, 4>& toErase);
726 bool processInstruction(Instruction* I,
727 ValueNumberedSet& currAvail,
728 DenseMap<Value*, LoadInst*>& lastSeenLoad,
729 SmallVector<Instruction*, 4>& toErase);
Owen Andersonbf8a3eb2007-08-02 18:16:06 +0000730 bool processNonLocalLoad(LoadInst* L,
731 SmallVector<Instruction*, 4>& toErase);
Owen Andersonacfa3ad2007-07-26 18:26:51 +0000732 Value *GetValueForBlock(BasicBlock *BB, LoadInst* orig,
Owen Andersonc6a31b92007-08-02 17:56:05 +0000733 DenseMap<BasicBlock*, Value*> &Phis,
734 bool top_level = false);
Owen Anderson5d72a422007-07-25 19:57:03 +0000735 void dump(DenseMap<BasicBlock*, Value*>& d);
Owen Andersonbe168b32007-08-14 18:04:11 +0000736 bool iterateOnFunction(Function &F);
Owen Andersone02ad522007-08-16 22:51:56 +0000737 Value* CollapsePhi(PHINode* p);
Owen Anderson19625972007-09-16 08:04:16 +0000738 bool isSafeReplacement(PHINode* p, Instruction* inst);
Owen Anderson85c40642007-07-24 17:55:58 +0000739 };
740
741 char GVN::ID = 0;
742
743}
744
745// createGVNPass - The public interface to this file...
746FunctionPass *llvm::createGVNPass() { return new GVN(); }
747
748static RegisterPass<GVN> X("gvn",
749 "Global Value Numbering");
750
751STATISTIC(NumGVNInstr, "Number of instructions deleted");
752STATISTIC(NumGVNLoad, "Number of loads deleted");
753
754/// find_leader - Given a set and a value number, return the first
755/// element of the set with that value number, or 0 if no such element
756/// is present
757Value* GVN::find_leader(ValueNumberedSet& vals, uint32_t v) {
758 if (!vals.test(v))
759 return 0;
760
761 for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
762 I != E; ++I)
763 if (v == VN.lookup(*I))
764 return *I;
765
766 assert(0 && "No leader found, but present bit is set?");
767 return 0;
768}
769
770/// val_insert - Insert a value into a set only if there is not a value
771/// with the same value number already in the set
772void GVN::val_insert(ValueNumberedSet& s, Value* v) {
773 uint32_t num = VN.lookup(v);
774 if (!s.test(num))
775 s.insert(v);
776}
777
Owen Anderson5d72a422007-07-25 19:57:03 +0000778void GVN::dump(DenseMap<BasicBlock*, Value*>& d) {
779 printf("{\n");
780 for (DenseMap<BasicBlock*, Value*>::iterator I = d.begin(),
781 E = d.end(); I != E; ++I) {
782 if (I->second == MemoryDependenceAnalysis::None)
783 printf("None\n");
784 else
785 I->second->dump();
786 }
787 printf("}\n");
788}
789
Owen Andersone02ad522007-08-16 22:51:56 +0000790Value* GVN::CollapsePhi(PHINode* p) {
791 DominatorTree &DT = getAnalysis<DominatorTree>();
792 Value* constVal = p->hasConstantValue();
793
794 if (constVal) {
795 if (Instruction* inst = dyn_cast<Instruction>(constVal)) {
796 if (DT.dominates(inst, p))
Owen Anderson19625972007-09-16 08:04:16 +0000797 if (isSafeReplacement(p, inst))
798 return inst;
Owen Andersone02ad522007-08-16 22:51:56 +0000799 } else {
800 return constVal;
801 }
802 }
803
804 return 0;
805}
Owen Anderson5d72a422007-07-25 19:57:03 +0000806
Owen Anderson19625972007-09-16 08:04:16 +0000807bool GVN::isSafeReplacement(PHINode* p, Instruction* inst) {
808 if (!isa<PHINode>(inst))
809 return true;
810
811 for (Instruction::use_iterator UI = p->use_begin(), E = p->use_end();
812 UI != E; ++UI)
813 if (PHINode* use_phi = dyn_cast<PHINode>(UI))
814 if (use_phi->getParent() == inst->getParent())
815 return false;
816
817 return true;
818}
819
Owen Andersonacfa3ad2007-07-26 18:26:51 +0000820/// GetValueForBlock - Get the value to use within the specified basic block.
821/// available values are in Phis.
822Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig,
Owen Andersonc6a31b92007-08-02 17:56:05 +0000823 DenseMap<BasicBlock*, Value*> &Phis,
824 bool top_level) {
Owen Andersonacfa3ad2007-07-26 18:26:51 +0000825
826 // If we have already computed this value, return the previously computed val.
Owen Andersoned7f9932007-08-03 19:59:35 +0000827 DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
828 if (V != Phis.end() && !top_level) return V->second;
Owen Andersonacfa3ad2007-07-26 18:26:51 +0000829
Owen Anderson3f75d122007-08-01 22:01:54 +0000830 BasicBlock* singlePred = BB->getSinglePredecessor();
Owen Anderson30463f12007-08-03 11:03:26 +0000831 if (singlePred) {
Owen Andersoned7f9932007-08-03 19:59:35 +0000832 Value *ret = GetValueForBlock(singlePred, orig, Phis);
833 Phis[BB] = ret;
834 return ret;
Owen Anderson30463f12007-08-03 11:03:26 +0000835 }
Owen Andersonacfa3ad2007-07-26 18:26:51 +0000836 // Otherwise, the idom is the loop, so we need to insert a PHI node. Do so
837 // now, then get values to fill in the incoming values for the PHI.
838 PHINode *PN = new PHINode(orig->getType(), orig->getName()+".rle",
839 BB->begin());
840 PN->reserveOperandSpace(std::distance(pred_begin(BB), pred_end(BB)));
Owen Andersoned7f9932007-08-03 19:59:35 +0000841
842 if (Phis.count(BB) == 0)
843 Phis.insert(std::make_pair(BB, PN));
Owen Anderson48a2c562007-07-30 16:57:08 +0000844
Owen Andersonacfa3ad2007-07-26 18:26:51 +0000845 // Fill in the incoming values for the block.
Owen Anderson9f577412007-07-31 17:43:14 +0000846 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
847 Value* val = GetValueForBlock(*PI, orig, Phis);
Owen Anderson9f577412007-07-31 17:43:14 +0000848
849 PN->addIncoming(val, *PI);
850 }
851
Owen Andersone0143452007-08-16 22:02:55 +0000852 // Attempt to collapse PHI nodes that are trivially redundant
Owen Andersone02ad522007-08-16 22:51:56 +0000853 Value* v = CollapsePhi(PN);
Owen Andersonf631bb62007-08-14 18:16:29 +0000854 if (v) {
Owen Andersone02ad522007-08-16 22:51:56 +0000855 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
Owen Andersonf631bb62007-08-14 18:16:29 +0000856
Owen Andersone02ad522007-08-16 22:51:56 +0000857 MD.removeInstruction(PN);
858 PN->replaceAllUsesWith(v);
Owen Andersonf631bb62007-08-14 18:16:29 +0000859
Owen Andersone02ad522007-08-16 22:51:56 +0000860 for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
861 E = Phis.end(); I != E; ++I)
862 if (I->second == PN)
863 I->second = v;
Owen Andersonf631bb62007-08-14 18:16:29 +0000864
Owen Andersone02ad522007-08-16 22:51:56 +0000865 PN->eraseFromParent();
Owen Andersonf631bb62007-08-14 18:16:29 +0000866
Owen Andersone02ad522007-08-16 22:51:56 +0000867 Phis[BB] = v;
Owen Andersonf631bb62007-08-14 18:16:29 +0000868
Owen Andersone02ad522007-08-16 22:51:56 +0000869 return v;
Owen Anderson9f577412007-07-31 17:43:14 +0000870 }
871
Owen Andersone0143452007-08-16 22:02:55 +0000872 // Cache our phi construction results
Owen Anderson5b299672007-08-07 23:12:31 +0000873 phiMap[orig->getPointerOperand()].insert(PN);
Owen Andersonacfa3ad2007-07-26 18:26:51 +0000874 return PN;
Owen Anderson5d72a422007-07-25 19:57:03 +0000875}
876
Owen Andersone0143452007-08-16 22:02:55 +0000877/// processNonLocalLoad - Attempt to eliminate a load whose dependencies are
878/// non-local by performing PHI construction.
Owen Andersonbf8a3eb2007-08-02 18:16:06 +0000879bool GVN::processNonLocalLoad(LoadInst* L,
880 SmallVector<Instruction*, 4>& toErase) {
Owen Anderson5d72a422007-07-25 19:57:03 +0000881 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
882
Owen Andersone0143452007-08-16 22:02:55 +0000883 // Find the non-local dependencies of the load
Owen Anderson5d72a422007-07-25 19:57:03 +0000884 DenseMap<BasicBlock*, Value*> deps;
Owen Anderson3f75d122007-08-01 22:01:54 +0000885 MD.getNonLocalDependency(L, deps);
Owen Anderson5d72a422007-07-25 19:57:03 +0000886
887 DenseMap<BasicBlock*, Value*> repl;
Owen Anderson5b299672007-08-07 23:12:31 +0000888
Owen Andersone0143452007-08-16 22:02:55 +0000889 // Filter out useless results (non-locals, etc)
Owen Anderson5d72a422007-07-25 19:57:03 +0000890 for (DenseMap<BasicBlock*, Value*>::iterator I = deps.begin(), E = deps.end();
891 I != E; ++I)
892 if (I->second == MemoryDependenceAnalysis::None) {
893 return false;
Owen Andersonb484d1f2007-07-30 17:29:24 +0000894 } else if (I->second == MemoryDependenceAnalysis::NonLocal) {
895 continue;
Owen Anderson05749072007-09-21 03:53:52 +0000896 } else if (StoreInst* S = dyn_cast<StoreInst>(I->second)) {
Owen Anderson5d72a422007-07-25 19:57:03 +0000897 if (S->getPointerOperand() == L->getPointerOperand())
Owen Anderson5b299672007-08-07 23:12:31 +0000898 repl[I->first] = S->getOperand(0);
Owen Anderson5d72a422007-07-25 19:57:03 +0000899 else
900 return false;
901 } else if (LoadInst* LD = dyn_cast<LoadInst>(I->second)) {
902 if (LD->getPointerOperand() == L->getPointerOperand())
Owen Anderson5b299672007-08-07 23:12:31 +0000903 repl[I->first] = LD;
Owen Anderson5d72a422007-07-25 19:57:03 +0000904 else
905 return false;
906 } else {
907 return false;
908 }
909
Owen Andersone0143452007-08-16 22:02:55 +0000910 // Use cached PHI construction information from previous runs
Owen Anderson5b299672007-08-07 23:12:31 +0000911 SmallPtrSet<Instruction*, 4>& p = phiMap[L->getPointerOperand()];
912 for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
913 I != E; ++I) {
914 if ((*I)->getParent() == L->getParent()) {
915 MD.removeInstruction(L);
916 L->replaceAllUsesWith(*I);
917 toErase.push_back(L);
918 NumGVNLoad++;
919
920 return true;
921 } else {
922 repl.insert(std::make_pair((*I)->getParent(), *I));
923 }
924 }
925
Owen Andersone0143452007-08-16 22:02:55 +0000926 // Perform PHI construction
Owen Anderson6ce3ae22007-07-25 22:03:06 +0000927 SmallPtrSet<BasicBlock*, 4> visited;
Owen Andersonc6a31b92007-08-02 17:56:05 +0000928 Value* v = GetValueForBlock(L->getParent(), L, repl, true);
Owen Anderson5d72a422007-07-25 19:57:03 +0000929
930 MD.removeInstruction(L);
931 L->replaceAllUsesWith(v);
932 toErase.push_back(L);
Owen Anderson5b299672007-08-07 23:12:31 +0000933 NumGVNLoad++;
Owen Anderson5d72a422007-07-25 19:57:03 +0000934
935 return true;
936}
937
Owen Andersone0143452007-08-16 22:02:55 +0000938/// processLoad - Attempt to eliminate a load, first by eliminating it
939/// locally, and then attempting non-local elimination if that fails.
Owen Anderson85c40642007-07-24 17:55:58 +0000940bool GVN::processLoad(LoadInst* L,
941 DenseMap<Value*, LoadInst*>& lastLoad,
942 SmallVector<Instruction*, 4>& toErase) {
943 if (L->isVolatile()) {
944 lastLoad[L->getPointerOperand()] = L;
945 return false;
946 }
947
948 Value* pointer = L->getPointerOperand();
949 LoadInst*& last = lastLoad[pointer];
950
951 // ... to a pointer that has been loaded from before...
952 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
Owen Andersoncc8b3a82007-08-14 17:59:48 +0000953 bool removedNonLocal = false;
Owen Anderson935e39b2007-08-09 04:42:44 +0000954 Instruction* dep = MD.getDependency(L);
Owen Anderson5d72a422007-07-25 19:57:03 +0000955 if (dep == MemoryDependenceAnalysis::NonLocal &&
Owen Andersoncc8b3a82007-08-14 17:59:48 +0000956 L->getParent() != &L->getParent()->getParent()->getEntryBlock()) {
957 removedNonLocal = processNonLocalLoad(L, toErase);
958
959 if (!removedNonLocal)
960 last = L;
961
962 return removedNonLocal;
963 }
964
965
Owen Anderson85c40642007-07-24 17:55:58 +0000966 bool deletedLoad = false;
967
Owen Andersone0143452007-08-16 22:02:55 +0000968 // Walk up the dependency chain until we either find
969 // a dependency we can use, or we can't walk any further
Owen Anderson85c40642007-07-24 17:55:58 +0000970 while (dep != MemoryDependenceAnalysis::None &&
971 dep != MemoryDependenceAnalysis::NonLocal &&
972 (isa<LoadInst>(dep) || isa<StoreInst>(dep))) {
973 // ... that depends on a store ...
974 if (StoreInst* S = dyn_cast<StoreInst>(dep)) {
975 if (S->getPointerOperand() == pointer) {
976 // Remove it!
977 MD.removeInstruction(L);
978
979 L->replaceAllUsesWith(S->getOperand(0));
980 toErase.push_back(L);
981 deletedLoad = true;
982 NumGVNLoad++;
983 }
984
985 // Whether we removed it or not, we can't
986 // go any further
987 break;
988 } else if (!last) {
989 // If we don't depend on a store, and we haven't
990 // been loaded before, bail.
991 break;
992 } else if (dep == last) {
993 // Remove it!
994 MD.removeInstruction(L);
995
996 L->replaceAllUsesWith(last);
997 toErase.push_back(L);
998 deletedLoad = true;
999 NumGVNLoad++;
1000
1001 break;
1002 } else {
Owen Anderson935e39b2007-08-09 04:42:44 +00001003 dep = MD.getDependency(L, dep);
Owen Anderson85c40642007-07-24 17:55:58 +00001004 }
1005 }
1006
1007 if (!deletedLoad)
1008 last = L;
1009
1010 return deletedLoad;
1011}
1012
Owen Andersonf631bb62007-08-14 18:16:29 +00001013/// processInstruction - When calculating availability, handle an instruction
Owen Anderson85c40642007-07-24 17:55:58 +00001014/// by inserting it into the appropriate sets
1015bool GVN::processInstruction(Instruction* I,
1016 ValueNumberedSet& currAvail,
1017 DenseMap<Value*, LoadInst*>& lastSeenLoad,
1018 SmallVector<Instruction*, 4>& toErase) {
1019 if (LoadInst* L = dyn_cast<LoadInst>(I)) {
1020 return processLoad(L, lastSeenLoad, toErase);
1021 }
1022
1023 unsigned num = VN.lookup_or_add(I);
1024
Owen Andersone0143452007-08-16 22:02:55 +00001025 // Collapse PHI nodes
Owen Anderson98f6a6b2007-08-14 18:33:27 +00001026 if (PHINode* p = dyn_cast<PHINode>(I)) {
Owen Andersone02ad522007-08-16 22:51:56 +00001027 Value* constVal = CollapsePhi(p);
Owen Anderson98f6a6b2007-08-14 18:33:27 +00001028
1029 if (constVal) {
Owen Andersone02ad522007-08-16 22:51:56 +00001030 for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
1031 PI != PE; ++PI)
1032 if (PI->second.count(p))
1033 PI->second.erase(p);
Owen Anderson98f6a6b2007-08-14 18:33:27 +00001034
Owen Andersone02ad522007-08-16 22:51:56 +00001035 p->replaceAllUsesWith(constVal);
1036 toErase.push_back(p);
Owen Anderson98f6a6b2007-08-14 18:33:27 +00001037 }
Owen Andersone0143452007-08-16 22:02:55 +00001038 // Perform value-number based elimination
Owen Anderson98f6a6b2007-08-14 18:33:27 +00001039 } else if (currAvail.test(num)) {
Owen Anderson85c40642007-07-24 17:55:58 +00001040 Value* repl = find_leader(currAvail, num);
1041
Owen Anderson8b6f04e2007-11-26 02:26:36 +00001042 if (CallInst* CI = dyn_cast<CallInst>(I)) {
1043 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
1044 if (CI->getCalledFunction() &&
1045 !AA.doesNotAccessMemory(CI->getCalledFunction())) {
1046 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
1047 if (MD.getDependency(CI) != MD.getDependency(cast<CallInst>(repl))) {
1048 // There must be an intervening may-alias store, so nothing from
1049 // this point on will be able to be replaced with the preceding call
1050 currAvail.erase(repl);
1051 currAvail.insert(I);
1052
1053 return false;
1054 }
1055 }
1056 }
1057
Owen Anderson5aff8002007-07-31 23:27:13 +00001058 VN.erase(I);
Owen Anderson85c40642007-07-24 17:55:58 +00001059 I->replaceAllUsesWith(repl);
1060 toErase.push_back(I);
1061 return true;
1062 } else if (!I->isTerminator()) {
1063 currAvail.set(num);
1064 currAvail.insert(I);
1065 }
1066
1067 return false;
1068}
1069
1070// GVN::runOnFunction - This is the main transformation entry point for a
1071// function.
1072//
Owen Andersonbe168b32007-08-14 18:04:11 +00001073bool GVN::runOnFunction(Function& F) {
Owen Anderson5e9366f2007-10-18 19:39:33 +00001074 VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
1075
Owen Andersonbe168b32007-08-14 18:04:11 +00001076 bool changed = false;
1077 bool shouldContinue = true;
1078
1079 while (shouldContinue) {
1080 shouldContinue = iterateOnFunction(F);
1081 changed |= shouldContinue;
1082 }
1083
1084 return changed;
1085}
1086
1087
1088// GVN::iterateOnFunction - Executes one iteration of GVN
1089bool GVN::iterateOnFunction(Function &F) {
Owen Anderson85c40642007-07-24 17:55:58 +00001090 // Clean out global sets from any previous functions
1091 VN.clear();
1092 availableOut.clear();
Owen Anderson5b299672007-08-07 23:12:31 +00001093 phiMap.clear();
Owen Anderson85c40642007-07-24 17:55:58 +00001094
1095 bool changed_function = false;
1096
1097 DominatorTree &DT = getAnalysis<DominatorTree>();
1098
1099 SmallVector<Instruction*, 4> toErase;
1100
1101 // Top-down walk of the dominator tree
1102 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1103 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1104
1105 // Get the set to update for this block
1106 ValueNumberedSet& currAvail = availableOut[DI->getBlock()];
1107 DenseMap<Value*, LoadInst*> lastSeenLoad;
1108
1109 BasicBlock* BB = DI->getBlock();
1110
1111 // A block inherits AVAIL_OUT from its dominator
1112 if (DI->getIDom() != 0)
1113 currAvail = availableOut[DI->getIDom()->getBlock()];
1114
1115 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
Owen Andersonc0403802007-07-30 21:26:39 +00001116 BI != BE; ) {
Owen Andersonbf8a3eb2007-08-02 18:16:06 +00001117 changed_function |= processInstruction(BI, currAvail,
1118 lastSeenLoad, toErase);
Owen Anderson5d72a422007-07-25 19:57:03 +00001119
1120 NumGVNInstr += toErase.size();
1121
Owen Andersonc0403802007-07-30 21:26:39 +00001122 // Avoid iterator invalidation
1123 ++BI;
1124
Owen Anderson5d72a422007-07-25 19:57:03 +00001125 for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
1126 E = toErase.end(); I != E; ++I)
1127 (*I)->eraseFromParent();
1128
1129 toErase.clear();
Owen Anderson85c40642007-07-24 17:55:58 +00001130 }
1131 }
1132
Owen Anderson85c40642007-07-24 17:55:58 +00001133 return changed_function;
1134}