blob: db87874107adabcd6e4a5a9e735fb344f0007b2c [file] [log] [blame]
Owen Anderson14d03332009-10-26 23:55:47 +00001//===- SCCVN.cpp - Eliminate redundant values -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs global value numbering to eliminate fully redundant
11// instructions. This is based on the paper "SCC-based Value Numbering"
12// by Cooper, et al.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "sccvn"
17#include "llvm/Transforms/Scalar.h"
18#include "llvm/BasicBlock.h"
19#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
Owen Anderson14d03332009-10-26 23:55:47 +000022#include "llvm/Operator.h"
23#include "llvm/Value.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/DepthFirstIterator.h"
26#include "llvm/ADT/PostOrderIterator.h"
27#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/SparseBitVector.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Analysis/Dominators.h"
32#include "llvm/Support/CFG.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Transforms/Utils/SSAUpdater.h"
37#include <cstdio>
38using namespace llvm;
39
40STATISTIC(NumSCCVNInstr, "Number of instructions deleted by SCCVN");
41STATISTIC(NumSCCVNPhi, "Number of phis deleted by SCCVN");
42
43//===----------------------------------------------------------------------===//
44// ValueTable Class
45//===----------------------------------------------------------------------===//
46
47/// This class holds the mapping between values and value numbers. It is used
48/// as an efficient mechanism to determine the expression-wise equivalence of
49/// two values.
50namespace {
51 struct Expression {
52 enum ExpressionOpcode { ADD, FADD, SUB, FSUB, MUL, FMUL,
53 UDIV, SDIV, FDIV, UREM, SREM,
54 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
55 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
56 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
57 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
58 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
59 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
60 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
61 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
62 PTRTOINT, INTTOPTR, BITCAST, GEP, CALL, CONSTANT,
63 INSERTVALUE, EXTRACTVALUE, EMPTY, TOMBSTONE };
64
65 ExpressionOpcode opcode;
66 const Type* type;
67 SmallVector<uint32_t, 4> varargs;
68
69 Expression() { }
70 Expression(ExpressionOpcode o) : opcode(o) { }
71
72 bool operator==(const Expression &other) const {
73 if (opcode != other.opcode)
74 return false;
75 else if (opcode == EMPTY || opcode == TOMBSTONE)
76 return true;
77 else if (type != other.type)
78 return false;
79 else {
80 if (varargs.size() != other.varargs.size())
81 return false;
82
83 for (size_t i = 0; i < varargs.size(); ++i)
84 if (varargs[i] != other.varargs[i])
85 return false;
86
87 return true;
88 }
89 }
90
91 bool operator!=(const Expression &other) const {
92 return !(*this == other);
93 }
94 };
95
96 class ValueTable {
97 private:
98 DenseMap<Value*, uint32_t> valueNumbering;
99 DenseMap<Expression, uint32_t> expressionNumbering;
100 DenseMap<Value*, uint32_t> constantsNumbering;
101
102 uint32_t nextValueNumber;
103
104 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
105 Expression::ExpressionOpcode getOpcode(CmpInst* C);
106 Expression::ExpressionOpcode getOpcode(CastInst* C);
107 Expression create_expression(BinaryOperator* BO);
108 Expression create_expression(CmpInst* C);
109 Expression create_expression(ShuffleVectorInst* V);
110 Expression create_expression(ExtractElementInst* C);
111 Expression create_expression(InsertElementInst* V);
112 Expression create_expression(SelectInst* V);
113 Expression create_expression(CastInst* C);
114 Expression create_expression(GetElementPtrInst* G);
115 Expression create_expression(CallInst* C);
116 Expression create_expression(Constant* C);
117 Expression create_expression(ExtractValueInst* C);
118 Expression create_expression(InsertValueInst* C);
119 public:
120 ValueTable() : nextValueNumber(1) { }
121 uint32_t computeNumber(Value *V);
122 uint32_t lookup(Value *V);
123 void add(Value *V, uint32_t num);
124 void clear();
125 void clearExpressions();
126 void erase(Value *v);
127 unsigned size();
128 void verifyRemoved(const Value *) const;
129 };
130}
131
132namespace llvm {
133template <> struct DenseMapInfo<Expression> {
134 static inline Expression getEmptyKey() {
135 return Expression(Expression::EMPTY);
136 }
137
138 static inline Expression getTombstoneKey() {
139 return Expression(Expression::TOMBSTONE);
140 }
141
142 static unsigned getHashValue(const Expression e) {
143 unsigned hash = e.opcode;
144
145 hash = ((unsigned)((uintptr_t)e.type >> 4) ^
146 (unsigned)((uintptr_t)e.type >> 9));
147
148 for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
149 E = e.varargs.end(); I != E; ++I)
150 hash = *I + hash * 37;
151
152 return hash;
153 }
154 static bool isEqual(const Expression &LHS, const Expression &RHS) {
155 return LHS == RHS;
156 }
157 static bool isPod() { return true; }
158};
159}
160
161//===----------------------------------------------------------------------===//
162// ValueTable Internal Functions
163//===----------------------------------------------------------------------===//
164Expression::ExpressionOpcode ValueTable::getOpcode(BinaryOperator* BO) {
165 switch(BO->getOpcode()) {
166 default: // THIS SHOULD NEVER HAPPEN
167 llvm_unreachable("Binary operator with unknown opcode?");
168 case Instruction::Add: return Expression::ADD;
169 case Instruction::FAdd: return Expression::FADD;
170 case Instruction::Sub: return Expression::SUB;
171 case Instruction::FSub: return Expression::FSUB;
172 case Instruction::Mul: return Expression::MUL;
173 case Instruction::FMul: return Expression::FMUL;
174 case Instruction::UDiv: return Expression::UDIV;
175 case Instruction::SDiv: return Expression::SDIV;
176 case Instruction::FDiv: return Expression::FDIV;
177 case Instruction::URem: return Expression::UREM;
178 case Instruction::SRem: return Expression::SREM;
179 case Instruction::FRem: return Expression::FREM;
180 case Instruction::Shl: return Expression::SHL;
181 case Instruction::LShr: return Expression::LSHR;
182 case Instruction::AShr: return Expression::ASHR;
183 case Instruction::And: return Expression::AND;
184 case Instruction::Or: return Expression::OR;
185 case Instruction::Xor: return Expression::XOR;
186 }
187}
188
189Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
190 if (isa<ICmpInst>(C)) {
191 switch (C->getPredicate()) {
192 default: // THIS SHOULD NEVER HAPPEN
193 llvm_unreachable("Comparison with unknown predicate?");
194 case ICmpInst::ICMP_EQ: return Expression::ICMPEQ;
195 case ICmpInst::ICMP_NE: return Expression::ICMPNE;
196 case ICmpInst::ICMP_UGT: return Expression::ICMPUGT;
197 case ICmpInst::ICMP_UGE: return Expression::ICMPUGE;
198 case ICmpInst::ICMP_ULT: return Expression::ICMPULT;
199 case ICmpInst::ICMP_ULE: return Expression::ICMPULE;
200 case ICmpInst::ICMP_SGT: return Expression::ICMPSGT;
201 case ICmpInst::ICMP_SGE: return Expression::ICMPSGE;
202 case ICmpInst::ICMP_SLT: return Expression::ICMPSLT;
203 case ICmpInst::ICMP_SLE: return Expression::ICMPSLE;
204 }
205 } else {
206 switch (C->getPredicate()) {
207 default: // THIS SHOULD NEVER HAPPEN
208 llvm_unreachable("Comparison with unknown predicate?");
209 case FCmpInst::FCMP_OEQ: return Expression::FCMPOEQ;
210 case FCmpInst::FCMP_OGT: return Expression::FCMPOGT;
211 case FCmpInst::FCMP_OGE: return Expression::FCMPOGE;
212 case FCmpInst::FCMP_OLT: return Expression::FCMPOLT;
213 case FCmpInst::FCMP_OLE: return Expression::FCMPOLE;
214 case FCmpInst::FCMP_ONE: return Expression::FCMPONE;
215 case FCmpInst::FCMP_ORD: return Expression::FCMPORD;
216 case FCmpInst::FCMP_UNO: return Expression::FCMPUNO;
217 case FCmpInst::FCMP_UEQ: return Expression::FCMPUEQ;
218 case FCmpInst::FCMP_UGT: return Expression::FCMPUGT;
219 case FCmpInst::FCMP_UGE: return Expression::FCMPUGE;
220 case FCmpInst::FCMP_ULT: return Expression::FCMPULT;
221 case FCmpInst::FCMP_ULE: return Expression::FCMPULE;
222 case FCmpInst::FCMP_UNE: return Expression::FCMPUNE;
223 }
224 }
225}
226
227Expression::ExpressionOpcode ValueTable::getOpcode(CastInst* C) {
228 switch(C->getOpcode()) {
229 default: // THIS SHOULD NEVER HAPPEN
230 llvm_unreachable("Cast operator with unknown opcode?");
231 case Instruction::Trunc: return Expression::TRUNC;
232 case Instruction::ZExt: return Expression::ZEXT;
233 case Instruction::SExt: return Expression::SEXT;
234 case Instruction::FPToUI: return Expression::FPTOUI;
235 case Instruction::FPToSI: return Expression::FPTOSI;
236 case Instruction::UIToFP: return Expression::UITOFP;
237 case Instruction::SIToFP: return Expression::SITOFP;
238 case Instruction::FPTrunc: return Expression::FPTRUNC;
239 case Instruction::FPExt: return Expression::FPEXT;
240 case Instruction::PtrToInt: return Expression::PTRTOINT;
241 case Instruction::IntToPtr: return Expression::INTTOPTR;
242 case Instruction::BitCast: return Expression::BITCAST;
243 }
244}
245
246Expression ValueTable::create_expression(CallInst* C) {
247 Expression e;
248
249 e.type = C->getType();
250 e.opcode = Expression::CALL;
251
252 e.varargs.push_back(lookup(C->getCalledFunction()));
253 for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
254 I != E; ++I)
255 e.varargs.push_back(lookup(*I));
256
257 return e;
258}
259
260Expression ValueTable::create_expression(BinaryOperator* BO) {
261 Expression e;
262 e.varargs.push_back(lookup(BO->getOperand(0)));
263 e.varargs.push_back(lookup(BO->getOperand(1)));
264 e.type = BO->getType();
265 e.opcode = getOpcode(BO);
266
267 return e;
268}
269
270Expression ValueTable::create_expression(CmpInst* C) {
271 Expression e;
272
273 e.varargs.push_back(lookup(C->getOperand(0)));
274 e.varargs.push_back(lookup(C->getOperand(1)));
275 e.type = C->getType();
276 e.opcode = getOpcode(C);
277
278 return e;
279}
280
281Expression ValueTable::create_expression(CastInst* C) {
282 Expression e;
283
284 e.varargs.push_back(lookup(C->getOperand(0)));
285 e.type = C->getType();
286 e.opcode = getOpcode(C);
287
288 return e;
289}
290
291Expression ValueTable::create_expression(ShuffleVectorInst* S) {
292 Expression e;
293
294 e.varargs.push_back(lookup(S->getOperand(0)));
295 e.varargs.push_back(lookup(S->getOperand(1)));
296 e.varargs.push_back(lookup(S->getOperand(2)));
297 e.type = S->getType();
298 e.opcode = Expression::SHUFFLE;
299
300 return e;
301}
302
303Expression ValueTable::create_expression(ExtractElementInst* E) {
304 Expression e;
305
306 e.varargs.push_back(lookup(E->getOperand(0)));
307 e.varargs.push_back(lookup(E->getOperand(1)));
308 e.type = E->getType();
309 e.opcode = Expression::EXTRACT;
310
311 return e;
312}
313
314Expression ValueTable::create_expression(InsertElementInst* I) {
315 Expression e;
316
317 e.varargs.push_back(lookup(I->getOperand(0)));
318 e.varargs.push_back(lookup(I->getOperand(1)));
319 e.varargs.push_back(lookup(I->getOperand(2)));
320 e.type = I->getType();
321 e.opcode = Expression::INSERT;
322
323 return e;
324}
325
326Expression ValueTable::create_expression(SelectInst* I) {
327 Expression e;
328
329 e.varargs.push_back(lookup(I->getCondition()));
330 e.varargs.push_back(lookup(I->getTrueValue()));
331 e.varargs.push_back(lookup(I->getFalseValue()));
332 e.type = I->getType();
333 e.opcode = Expression::SELECT;
334
335 return e;
336}
337
338Expression ValueTable::create_expression(GetElementPtrInst* G) {
339 Expression e;
340
341 e.varargs.push_back(lookup(G->getPointerOperand()));
342 e.type = G->getType();
343 e.opcode = Expression::GEP;
344
345 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
346 I != E; ++I)
347 e.varargs.push_back(lookup(*I));
348
349 return e;
350}
351
352Expression ValueTable::create_expression(ExtractValueInst* E) {
353 Expression e;
354
355 e.varargs.push_back(lookup(E->getAggregateOperand()));
356 for (ExtractValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end();
357 II != IE; ++II)
358 e.varargs.push_back(*II);
359 e.type = E->getType();
360 e.opcode = Expression::EXTRACTVALUE;
361
362 return e;
363}
364
365Expression ValueTable::create_expression(InsertValueInst* E) {
366 Expression e;
367
368 e.varargs.push_back(lookup(E->getAggregateOperand()));
369 e.varargs.push_back(lookup(E->getInsertedValueOperand()));
370 for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end();
371 II != IE; ++II)
372 e.varargs.push_back(*II);
373 e.type = E->getType();
374 e.opcode = Expression::INSERTVALUE;
375
376 return e;
377}
378
379//===----------------------------------------------------------------------===//
380// ValueTable External Functions
381//===----------------------------------------------------------------------===//
382
383/// add - Insert a value into the table with a specified value number.
384void ValueTable::add(Value *V, uint32_t num) {
385 valueNumbering[V] = num;
386}
387
388/// computeNumber - Returns the value number for the specified value, assigning
389/// it a new number if it did not have one before.
390uint32_t ValueTable::computeNumber(Value *V) {
391 if (uint32_t v = valueNumbering[V])
392 return v;
393 else if (uint32_t v= constantsNumbering[V])
394 return v;
395
396 if (!isa<Instruction>(V)) {
397 constantsNumbering[V] = nextValueNumber;
398 return nextValueNumber++;
399 }
400
401 Instruction* I = cast<Instruction>(V);
402 Expression exp;
403 switch (I->getOpcode()) {
404 case Instruction::Add:
405 case Instruction::FAdd:
406 case Instruction::Sub:
407 case Instruction::FSub:
408 case Instruction::Mul:
409 case Instruction::FMul:
410 case Instruction::UDiv:
411 case Instruction::SDiv:
412 case Instruction::FDiv:
413 case Instruction::URem:
414 case Instruction::SRem:
415 case Instruction::FRem:
416 case Instruction::Shl:
417 case Instruction::LShr:
418 case Instruction::AShr:
419 case Instruction::And:
420 case Instruction::Or :
421 case Instruction::Xor:
422 exp = create_expression(cast<BinaryOperator>(I));
423 break;
424 case Instruction::ICmp:
425 case Instruction::FCmp:
426 exp = create_expression(cast<CmpInst>(I));
427 break;
428 case Instruction::Trunc:
429 case Instruction::ZExt:
430 case Instruction::SExt:
431 case Instruction::FPToUI:
432 case Instruction::FPToSI:
433 case Instruction::UIToFP:
434 case Instruction::SIToFP:
435 case Instruction::FPTrunc:
436 case Instruction::FPExt:
437 case Instruction::PtrToInt:
438 case Instruction::IntToPtr:
439 case Instruction::BitCast:
440 exp = create_expression(cast<CastInst>(I));
441 break;
442 case Instruction::Select:
443 exp = create_expression(cast<SelectInst>(I));
444 break;
445 case Instruction::ExtractElement:
446 exp = create_expression(cast<ExtractElementInst>(I));
447 break;
448 case Instruction::InsertElement:
449 exp = create_expression(cast<InsertElementInst>(I));
450 break;
451 case Instruction::ShuffleVector:
452 exp = create_expression(cast<ShuffleVectorInst>(I));
453 break;
454 case Instruction::ExtractValue:
455 exp = create_expression(cast<ExtractValueInst>(I));
456 break;
457 case Instruction::InsertValue:
458 exp = create_expression(cast<InsertValueInst>(I));
459 break;
460 case Instruction::GetElementPtr:
461 exp = create_expression(cast<GetElementPtrInst>(I));
462 break;
463 default:
464 valueNumbering[V] = nextValueNumber;
465 return nextValueNumber++;
466 }
467
468 uint32_t& e = expressionNumbering[exp];
469 if (!e) e = nextValueNumber++;
470 valueNumbering[V] = e;
471
472 return e;
473}
474
475/// lookup - Returns the value number of the specified value. Returns 0 if
476/// the value has not yet been numbered.
477uint32_t ValueTable::lookup(Value *V) {
478 if (!isa<Instruction>(V)) {
479 if (!constantsNumbering.count(V))
480 constantsNumbering[V] = nextValueNumber++;
481 return constantsNumbering[V];
482 }
483
484 return valueNumbering[V];
485}
486
487/// clear - Remove all entries from the ValueTable
488void ValueTable::clear() {
489 valueNumbering.clear();
490 expressionNumbering.clear();
491 constantsNumbering.clear();
492 nextValueNumber = 1;
493}
494
495void ValueTable::clearExpressions() {
496 expressionNumbering.clear();
497 constantsNumbering.clear();
498 nextValueNumber = 1;
499}
500
501/// erase - Remove a value from the value numbering
502void ValueTable::erase(Value *V) {
503 valueNumbering.erase(V);
504}
505
506/// verifyRemoved - Verify that the value is removed from all internal data
507/// structures.
508void ValueTable::verifyRemoved(const Value *V) const {
Jeffrey Yasskin8154d2e2009-11-10 01:02:17 +0000509 for (DenseMap<Value*, uint32_t>::const_iterator
Owen Anderson14d03332009-10-26 23:55:47 +0000510 I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
511 assert(I->first != V && "Inst still occurs in value numbering map!");
512 }
513}
514
515//===----------------------------------------------------------------------===//
516// SCCVN Pass
517//===----------------------------------------------------------------------===//
518
519namespace {
520
521 struct ValueNumberScope {
522 ValueNumberScope* parent;
523 DenseMap<uint32_t, Value*> table;
524 SparseBitVector<128> availIn;
525 SparseBitVector<128> availOut;
526
527 ValueNumberScope(ValueNumberScope* p) : parent(p) { }
528 };
529
530 class SCCVN : public FunctionPass {
531 bool runOnFunction(Function &F);
532 public:
533 static char ID; // Pass identification, replacement for typeid
534 SCCVN() : FunctionPass(&ID) { }
535
536 private:
537 ValueTable VT;
538 DenseMap<BasicBlock*, ValueNumberScope*> BBMap;
539
540 // This transformation requires dominator postdominator info
541 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
542 AU.addRequired<DominatorTree>();
543
544 AU.addPreserved<DominatorTree>();
545 AU.setPreservesCFG();
546 }
547 };
548
549 char SCCVN::ID = 0;
550}
551
552// createSCCVNPass - The public interface to this file...
553FunctionPass *llvm::createSCCVNPass() { return new SCCVN(); }
554
555static RegisterPass<SCCVN> X("sccvn",
556 "SCC Value Numbering");
557
558static Value *lookupNumber(ValueNumberScope *Locals, uint32_t num) {
559 while (Locals) {
560 DenseMap<uint32_t, Value*>::iterator I = Locals->table.find(num);
561 if (I != Locals->table.end())
562 return I->second;
563 Locals = Locals->parent;
564 }
565
566 return 0;
567}
568
569bool SCCVN::runOnFunction(Function& F) {
570 // Implement the RPO version of the SCCVN algorithm. Conceptually,
571 // we optimisitically assume that all instructions with the same opcode have
572 // the same VN. Then we deepen our comparison by one level, to all
573 // instructions whose operands have the same opcodes get the same VN. We
574 // iterate this process until the partitioning stops changing, at which
575 // point we have computed a full numbering.
576 ReversePostOrderTraversal<Function*> RPOT(&F);
577 bool done = false;
578 while (!done) {
579 done = true;
580 VT.clearExpressions();
581 for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
582 E = RPOT.end(); I != E; ++I) {
583 BasicBlock* BB = *I;
584 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
585 BI != BE; ++BI) {
586 uint32_t origVN = VT.lookup(BI);
587 uint32_t newVN = VT.computeNumber(BI);
588 if (origVN != newVN)
589 done = false;
590 }
591 }
592 }
593
594 // Now, do a dominator walk, eliminating simple, dominated redundancies as we
595 // go. Also, build the ValueNumberScope structure that will be used for
596 // computing full availability.
597 DominatorTree& DT = getAnalysis<DominatorTree>();
598 bool changed = false;
599 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
600 DE = df_end(DT.getRootNode()); DI != DE; ++DI) {
601 BasicBlock* BB = DI->getBlock();
602 if (DI->getIDom())
603 BBMap[BB] = new ValueNumberScope(BBMap[DI->getIDom()->getBlock()]);
604 else
605 BBMap[BB] = new ValueNumberScope(0);
606
607 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
608 uint32_t num = VT.lookup(I);
609 Value* repl = lookupNumber(BBMap[BB], num);
610
611 if (repl) {
612 if (isa<PHINode>(I))
613 ++NumSCCVNPhi;
614 else
615 ++NumSCCVNInstr;
616 I->replaceAllUsesWith(repl);
617 Instruction* OldInst = I;
618 ++I;
619 BBMap[BB]->table[num] = repl;
620 OldInst->eraseFromParent();
621 changed = true;
622 } else {
623 BBMap[BB]->table[num] = I;
624 BBMap[BB]->availOut.set(num);
625
626 ++I;
627 }
628 }
629 }
630
Owen Anderson14d03332009-10-26 23:55:47 +0000631 // Perform a forward data-flow to compute availability at all points on
632 // the CFG.
633 do {
634 changed = false;
635 for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
636 E = RPOT.end(); I != E; ++I) {
637 BasicBlock* BB = *I;
638 ValueNumberScope *VNS = BBMap[BB];
639
640 SparseBitVector<128> preds;
641 bool first = true;
642 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
643 PI != PE; ++PI) {
644 if (first) {
645 preds = BBMap[*PI]->availOut;
646 first = false;
647 } else {
648 preds &= BBMap[*PI]->availOut;
649 }
650 }
651
652 changed |= (VNS->availIn |= preds);
653 changed |= (VNS->availOut |= preds);
654 }
655 } while (changed);
656
657 // Use full availability information to perform non-dominated replacements.
658 SSAUpdater SSU;
659 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
660 if (!BBMap.count(FI)) continue;
661 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
662 BI != BE; ) {
663 uint32_t num = VT.lookup(BI);
664 if (!BBMap[FI]->availIn.test(num)) {
665 ++BI;
666 continue;
667 }
668
669 SSU.Initialize(BI);
670
671 SmallPtrSet<BasicBlock*, 8> visited;
672 SmallVector<BasicBlock*, 8> stack;
673 visited.insert(FI);
674 for (pred_iterator PI = pred_begin(FI), PE = pred_end(FI);
675 PI != PE; ++PI)
676 if (!visited.count(*PI))
677 stack.push_back(*PI);
678
679 while (!stack.empty()) {
680 BasicBlock* CurrBB = stack.back();
681 stack.pop_back();
682 visited.insert(CurrBB);
683
684 ValueNumberScope* S = BBMap[CurrBB];
685 if (S->table.count(num)) {
686 SSU.AddAvailableValue(CurrBB, S->table[num]);
687 } else {
688 for (pred_iterator PI = pred_begin(CurrBB), PE = pred_end(CurrBB);
689 PI != PE; ++PI)
690 if (!visited.count(*PI))
691 stack.push_back(*PI);
692 }
693 }
694
695 Value* repl = SSU.GetValueInMiddleOfBlock(FI);
696 BI->replaceAllUsesWith(repl);
697 Instruction* CurInst = BI;
698 ++BI;
699 BBMap[FI]->table[num] = repl;
700 if (isa<PHINode>(CurInst))
701 ++NumSCCVNPhi;
702 else
703 ++NumSCCVNInstr;
704
705 CurInst->eraseFromParent();
706 }
707 }
Owen Anderson14d03332009-10-26 23:55:47 +0000708
709 VT.clear();
710 for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
711 I = BBMap.begin(), E = BBMap.end(); I != E; ++I)
712 delete I->second;
713 BBMap.clear();
714
715 return changed;
Edward O'Callaghanf9f7a942009-10-28 15:04:53 +0000716}