1 //===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This pass performs a hybrid of global value numbering and partial redundancy
11 // elimination, known as GVN-PRE. It performs partial redundancy elimination on
12 // values, rather than lexical expressions, allowing a more comprehensive view
13 // the optimization. It replaces redundant values with uses of earlier
14 // occurences of the same value. While this is beneficial in that it eliminates
15 // unneeded computation, it also increases register pressure by creating large
16 // live ranges, and should be used with caution on platforms that are very
17 // sensitive to register pressure.
19 //===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "gvnpre"
22 #include "llvm/Value.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Function.h"
26 #include "llvm/Analysis/Dominators.h"
27 #include "llvm/Analysis/PostDominators.h"
28 #include "llvm/ADT/DepthFirstIterator.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/Support/CFG.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
41 bool operator()(Value* left, Value* right) {
42 if (BinaryOperator* leftBO = dyn_cast<BinaryOperator>(left)) {
43 if (BinaryOperator* rightBO = dyn_cast<BinaryOperator>(right))
44 return cmpBinaryOperator(leftBO, rightBO);
46 if (isa<CmpInst>(right)) {
51 } else if (CmpInst* leftCmp = dyn_cast<CmpInst>(left)) {
52 if (CmpInst* rightCmp = dyn_cast<CmpInst>(right))
53 return cmpComparison(leftCmp, rightCmp);
57 if (isa<BinaryOperator>(right) || isa<CmpInst>(right))
64 bool cmpBinaryOperator(BinaryOperator* left, BinaryOperator* right) {
65 if (left->getOpcode() != right->getOpcode())
66 return left->getOpcode() < right->getOpcode();
67 else if ((*this)(left->getOperand(0), right->getOperand(0)))
69 else if ((*this)(right->getOperand(0), left->getOperand(0)))
72 return (*this)(left->getOperand(1), right->getOperand(1));
75 bool cmpComparison(CmpInst* left, CmpInst* right) {
76 if (left->getOpcode() != right->getOpcode())
77 return left->getOpcode() < right->getOpcode();
78 else if (left->getPredicate() != right->getPredicate())
79 return left->getPredicate() < right->getPredicate();
80 else if ((*this)(left->getOperand(0), right->getOperand(0)))
82 else if ((*this)(right->getOperand(0), left->getOperand(0)))
85 return (*this)(left->getOperand(1), right->getOperand(1));
91 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
92 bool runOnFunction(Function &F);
94 static char ID; // Pass identification, replacement for typeid
95 GVNPRE() : FunctionPass((intptr_t)&ID) { nextValueNumber = 1; }
98 uint32_t nextValueNumber;
99 typedef std::map<Value*, uint32_t, ExprLT> ValueTable;
101 std::set<Value*, ExprLT> MS;
102 std::vector<Instruction*> createdExpressions;
104 std::map<BasicBlock*, std::set<Value*, ExprLT> > availableOut;
105 std::map<BasicBlock*, std::set<Value*, ExprLT> > anticipatedIn;
107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.setPreservesCFG();
109 AU.addRequired<DominatorTree>();
110 AU.addRequired<PostDominatorTree>();
114 // FIXME: eliminate or document these better
115 void dump(const std::set<Value*>& s) const;
116 void dump_unique(const std::set<Value*, ExprLT>& s) const;
117 void clean(std::set<Value*, ExprLT>& set);
118 bool add(Value* V, uint32_t number);
119 Value* find_leader(std::set<Value*, ExprLT>& vals,
121 Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ);
122 void phi_translate_set(std::set<Value*, ExprLT>& anticIn, BasicBlock* pred,
123 BasicBlock* succ, std::set<Value*, ExprLT>& out);
125 void topo_sort(std::set<Value*, ExprLT>& set,
126 std::vector<Value*>& vec);
128 // For a given block, calculate the generated expressions, temporaries,
129 // and the AVAIL_OUT set
130 void CalculateAvailOut(DomTreeNode* DI,
131 std::set<Value*, ExprLT>& currExps,
132 std::set<PHINode*>& currPhis,
133 std::set<Value*>& currTemps,
134 std::set<Value*, ExprLT>& currAvail,
135 std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut);
145 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
147 RegisterPass<GVNPRE> X("gvnpre",
148 "Global Value Numbering/Partial Redundancy Elimination");
151 STATISTIC(NumInsertedVals, "Number of values inserted");
152 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
153 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
156 bool GVNPRE::add(Value* V, uint32_t number) {
157 std::pair<ValueTable::iterator, bool> ret = VN.insert(std::make_pair(V, number));
158 if (isa<BinaryOperator>(V) || isa<PHINode>(V) || isa<CmpInst>(V))
163 Value* GVNPRE::find_leader(std::set<Value*, ExprLT>& vals, Value* v) {
164 if (!isa<Instruction>(v))
167 for (std::set<Value*, ExprLT>::iterator I = vals.begin(), E = vals.end();
169 assert(VN.find(v) != VN.end() && "Value not numbered?");
170 assert(VN.find(*I) != VN.end() && "Value not numbered?");
178 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
182 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
183 Value* newOp1 = isa<Instruction>(BO->getOperand(0))
185 find_leader(anticipatedIn[succ], BO->getOperand(0)),
191 Value* newOp2 = isa<Instruction>(BO->getOperand(1))
193 find_leader(anticipatedIn[succ], BO->getOperand(1)),
199 if (newOp1 != BO->getOperand(0) || newOp2 != BO->getOperand(1)) {
200 Instruction* newVal = BinaryOperator::create(BO->getOpcode(),
202 BO->getName()+".gvnpre");
204 if (add(newVal, nextValueNumber))
207 Value* leader = find_leader(availableOut[pred], newVal);
209 DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
210 createdExpressions.push_back(newVal);
213 ValueTable::iterator I = VN.find(newVal);
214 if (I->first == newVal)
217 std::set<Value*, ExprLT>::iterator F = MS.find(newVal);
225 } else if (PHINode* P = dyn_cast<PHINode>(V)) {
226 if (P->getParent() == succ)
227 return P->getIncomingValueForBlock(pred);
228 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
229 Value* newOp1 = isa<Instruction>(C->getOperand(0))
231 find_leader(anticipatedIn[succ], C->getOperand(0)),
237 Value* newOp2 = isa<Instruction>(C->getOperand(1))
239 find_leader(anticipatedIn[succ], C->getOperand(1)),
245 if (newOp1 != C->getOperand(0) || newOp2 != C->getOperand(1)) {
246 Instruction* newVal = CmpInst::create(C->getOpcode(),
249 C->getName()+".gvnpre");
251 if (add(newVal, nextValueNumber))
254 Value* leader = find_leader(availableOut[pred], newVal);
256 DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
257 createdExpressions.push_back(newVal);
260 ValueTable::iterator I = VN.find(newVal);
261 if (I->first == newVal)
264 std::set<Value*, ExprLT>::iterator F = MS.find(newVal);
277 void GVNPRE::phi_translate_set(std::set<Value*, ExprLT>& anticIn,
278 BasicBlock* pred, BasicBlock* succ,
279 std::set<Value*, ExprLT>& out) {
280 for (std::set<Value*, ExprLT>::iterator I = anticIn.begin(),
281 E = anticIn.end(); I != E; ++I) {
282 Value* V = phi_translate(*I, pred, succ);
288 bool dependsOnInvoke(Value* V) {
292 User* U = cast<User>(V);
293 std::vector<Value*> worklist(U->op_begin(), U->op_end());
294 std::set<Value*> visited;
296 while (!worklist.empty()) {
297 Value* current = worklist.back();
299 visited.insert(current);
301 if (!isa<User>(current))
303 else if (isa<InvokeInst>(current))
306 User* curr = cast<User>(current);
307 for (unsigned i = 0; i < curr->getNumOperands(); ++i)
308 if (visited.find(curr->getOperand(i)) == visited.end())
309 worklist.push_back(curr->getOperand(i));
315 // Remove all expressions whose operands are not themselves in the set
316 void GVNPRE::clean(std::set<Value*, ExprLT>& set) {
317 std::vector<Value*> worklist;
318 topo_sort(set, worklist);
320 for (unsigned i = 0; i < worklist.size(); ++i) {
321 Value* v = worklist[i];
323 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(v)) {
324 bool lhsValid = !isa<Instruction>(BO->getOperand(0));
326 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
328 if (VN[*I] == VN[BO->getOperand(0)]) {
333 // Check for dependency on invoke insts
334 // NOTE: This check is expensive, so don't do it if we
337 lhsValid = !dependsOnInvoke(BO->getOperand(0));
339 bool rhsValid = !isa<Instruction>(BO->getOperand(1));
341 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
343 if (VN[*I] == VN[BO->getOperand(1)]) {
348 // Check for dependency on invoke insts
349 // NOTE: This check is expensive, so don't do it if we
352 rhsValid = !dependsOnInvoke(BO->getOperand(1));
354 if (!lhsValid || !rhsValid)
356 } else if (CmpInst* C = dyn_cast<CmpInst>(v)) {
357 bool lhsValid = !isa<Instruction>(C->getOperand(0));
359 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
361 if (VN[*I] == VN[C->getOperand(0)]) {
365 lhsValid &= !dependsOnInvoke(C->getOperand(0));
367 bool rhsValid = !isa<Instruction>(C->getOperand(1));
369 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
371 if (VN[*I] == VN[C->getOperand(1)]) {
375 rhsValid &= !dependsOnInvoke(C->getOperand(1));
377 if (!lhsValid || !rhsValid)
383 void GVNPRE::topo_sort(std::set<Value*, ExprLT>& set,
384 std::vector<Value*>& vec) {
385 std::set<Value*, ExprLT> toErase;
386 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
388 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(*I))
389 for (std::set<Value*, ExprLT>::iterator SI = set.begin(); SI != E; ++SI) {
390 if (VN[BO->getOperand(0)] == VN[*SI] ||
391 VN[BO->getOperand(1)] == VN[*SI]) {
395 else if (CmpInst* C = dyn_cast<CmpInst>(*I))
396 for (std::set<Value*, ExprLT>::iterator SI = set.begin(); SI != E; ++SI) {
397 if (VN[C->getOperand(0)] == VN[*SI] ||
398 VN[C->getOperand(1)] == VN[*SI]) {
404 std::vector<Value*> Q;
405 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
407 if (toErase.find(*I) == toErase.end())
411 std::set<Value*> visited;
415 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(e)) {
416 Value* l = find_leader(set, BO->getOperand(0));
417 Value* r = find_leader(set, BO->getOperand(1));
419 if (l != 0 && isa<Instruction>(l) &&
420 visited.find(l) == visited.end())
422 else if (r != 0 && isa<Instruction>(r) &&
423 visited.find(r) == visited.end())
430 } else if (CmpInst* C = dyn_cast<CmpInst>(e)) {
431 Value* l = find_leader(set, C->getOperand(0));
432 Value* r = find_leader(set, C->getOperand(1));
434 if (l != 0 && isa<Instruction>(l) &&
435 visited.find(l) == visited.end())
437 else if (r != 0 && isa<Instruction>(r) &&
438 visited.find(r) == visited.end())
454 void GVNPRE::dump(const std::set<Value*>& s) const {
456 for (std::set<Value*>::iterator I = s.begin(), E = s.end();
463 void GVNPRE::dump_unique(const std::set<Value*, ExprLT>& s) const {
465 for (std::set<Value*>::iterator I = s.begin(), E = s.end();
472 void GVNPRE::CalculateAvailOut(DomTreeNode* DI,
473 std::set<Value*, ExprLT>& currExps,
474 std::set<PHINode*>& currPhis,
475 std::set<Value*>& currTemps,
476 std::set<Value*, ExprLT>& currAvail,
477 std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut) {
479 BasicBlock* BB = DI->getBlock();
481 // A block inherits AVAIL_OUT from its dominator
482 if (DI->getIDom() != 0)
483 currAvail.insert(availOut[DI->getIDom()->getBlock()].begin(),
484 availOut[DI->getIDom()->getBlock()].end());
487 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
490 // Handle PHI nodes...
491 if (PHINode* p = dyn_cast<PHINode>(BI)) {
492 if (add(p, nextValueNumber))
496 // Handle binary ops...
497 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
498 Value* leftValue = BO->getOperand(0);
499 Value* rightValue = BO->getOperand(1);
501 if (add(BO, nextValueNumber))
504 if (isa<Instruction>(leftValue))
505 currExps.insert(leftValue);
506 if (isa<Instruction>(rightValue))
507 currExps.insert(rightValue);
511 } else if (CmpInst* C = dyn_cast<CmpInst>(BI)) {
512 Value* leftValue = C->getOperand(0);
513 Value* rightValue = C->getOperand(1);
515 if (add(C, nextValueNumber))
518 if (isa<Instruction>(leftValue))
519 currExps.insert(leftValue);
520 if (isa<Instruction>(rightValue))
521 currExps.insert(rightValue);
524 // Handle unsupported ops
525 } else if (!BI->isTerminator()){
526 if (add(BI, nextValueNumber))
528 currTemps.insert(BI);
531 if (!BI->isTerminator())
532 currAvail.insert(BI);
536 void GVNPRE::elimination() {
537 DOUT << "\n\nPhase 3: Elimination\n\n";
539 std::vector<std::pair<Instruction*, Value*> > replace;
540 std::vector<Instruction*> erase;
542 DominatorTree& DT = getAnalysis<DominatorTree>();
544 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
545 E = df_end(DT.getRootNode()); DI != E; ++DI) {
546 BasicBlock* BB = DI->getBlock();
548 DOUT << "Block: " << BB->getName() << "\n";
549 dump_unique(availableOut[BB]);
552 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
555 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI)) {
556 Value *leader = find_leader(availableOut[BB], BI);
559 if (Instruction* Instr = dyn_cast<Instruction>(leader))
560 if (Instr->getParent() != 0 && Instr != BI) {
561 replace.push_back(std::make_pair(BI, leader));
569 while (!replace.empty()) {
570 std::pair<Instruction*, Value*> rep = replace.back();
572 rep.first->replaceAllUsesWith(rep.second);
575 for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
577 (*I)->eraseFromParent();
581 void GVNPRE::cleanup() {
582 while (!createdExpressions.empty()) {
583 Instruction* I = createdExpressions.back();
584 createdExpressions.pop_back();
590 bool GVNPRE::runOnFunction(Function &F) {
593 createdExpressions.clear();
594 availableOut.clear();
595 anticipatedIn.clear();
597 std::map<BasicBlock*, std::set<Value*, ExprLT> > generatedExpressions;
598 std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
599 std::map<BasicBlock*, std::set<Value*> > generatedTemporaries;
602 DominatorTree &DT = getAnalysis<DominatorTree>();
604 // Phase 1: BuildSets
606 // Phase 1, Part 1: calculate AVAIL_OUT
608 // Top-down walk of the dominator tree
609 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
610 E = df_end(DT.getRootNode()); DI != E; ++DI) {
612 // Get the sets to update for this block
613 std::set<Value*, ExprLT>& currExps = generatedExpressions[DI->getBlock()];
614 std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
615 std::set<Value*>& currTemps = generatedTemporaries[DI->getBlock()];
616 std::set<Value*, ExprLT>& currAvail = availableOut[DI->getBlock()];
618 CalculateAvailOut(*DI, currExps, currPhis,
619 currTemps, currAvail, availableOut);
622 DOUT << "Maximal Set: ";
626 // If function has no exit blocks, only perform GVN
627 PostDominatorTree &PDT = getAnalysis<PostDominatorTree>();
628 if (PDT[&F.getEntryBlock()] == 0) {
636 // Phase 1, Part 2: calculate ANTIC_IN
638 std::set<BasicBlock*> visited;
641 unsigned iterations = 0;
644 std::set<Value*, ExprLT> anticOut;
646 // Top-down walk of the postdominator tree
647 for (df_iterator<DomTreeNode*> PDI =
648 df_begin(PDT.getRootNode()), E = df_end(PDT.getRootNode());
650 BasicBlock* BB = PDI->getBlock();
654 DOUT << "Block: " << BB->getName() << "\n";
656 dump(generatedTemporaries[BB]);
660 dump_unique(generatedExpressions[BB]);
663 std::set<Value*, ExprLT>& anticIn = anticipatedIn[BB];
664 std::set<Value*, ExprLT> old (anticIn.begin(), anticIn.end());
666 if (BB->getTerminator()->getNumSuccessors() == 1) {
667 if (visited.find(BB->getTerminator()->getSuccessor(0)) ==
669 phi_translate_set(MS, BB, BB->getTerminator()->getSuccessor(0),
672 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
673 BB, BB->getTerminator()->getSuccessor(0),
675 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
676 BasicBlock* first = BB->getTerminator()->getSuccessor(0);
677 anticOut.insert(anticipatedIn[first].begin(),
678 anticipatedIn[first].end());
679 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
680 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
681 std::set<Value*, ExprLT>& succAnticIn = anticipatedIn[currSucc];
683 std::set<Value*, ExprLT> temp;
684 std::insert_iterator<std::set<Value*, ExprLT> > temp_ins(temp,
686 std::set_intersection(anticOut.begin(), anticOut.end(),
687 succAnticIn.begin(), succAnticIn.end(),
691 anticOut.insert(temp.begin(), temp.end());
695 DOUT << "ANTIC_OUT: ";
696 dump_unique(anticOut);
699 std::set<Value*, ExprLT> S;
700 std::insert_iterator<std::set<Value*, ExprLT> > s_ins(S, S.begin());
701 std::set_union(anticOut.begin(), anticOut.end(),
702 generatedExpressions[BB].begin(),
703 generatedExpressions[BB].end(),
708 for (std::set<Value*, ExprLT>::iterator I = S.begin(), E = S.end();
710 if (generatedTemporaries[BB].find(*I) == generatedTemporaries[BB].end())
716 DOUT << "ANTIC_IN: ";
717 dump_unique(anticIn);
720 if (old.size() != anticIn.size())
729 DOUT << "Iterations: " << iterations << "\n";
731 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
732 DOUT << "Name: " << I->getName().c_str() << "\n";
735 dump(generatedTemporaries[I]);
739 dump_unique(generatedExpressions[I]);
742 DOUT << "ANTIC_IN: ";
743 dump_unique(anticipatedIn[I]);
746 DOUT << "AVAIL_OUT: ";
747 dump_unique(availableOut[I]);
752 DOUT<< "\nPhase 2: Insertion\n";
754 std::map<BasicBlock*, std::set<Value*, ExprLT> > new_sets;
755 unsigned i_iterations = 0;
756 bool new_stuff = true;
759 DOUT << "Iteration: " << i_iterations << "\n\n";
760 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
761 E = df_end(DT.getRootNode()); DI != E; ++DI) {
762 BasicBlock* BB = DI->getBlock();
767 std::set<Value*, ExprLT>& new_set = new_sets[BB];
768 std::set<Value*, ExprLT>& availOut = availableOut[BB];
769 std::set<Value*, ExprLT>& anticIn = anticipatedIn[BB];
773 // Replace leaders with leaders inherited from dominator
774 if (DI->getIDom() != 0) {
775 std::set<Value*, ExprLT>& dom_set = new_sets[DI->getIDom()->getBlock()];
776 for (std::set<Value*, ExprLT>::iterator I = dom_set.begin(),
777 E = dom_set.end(); I != E; ++I) {
780 Value* val = find_leader(availOut, *I);
783 val = find_leader(availOut, *I);
789 // If there is more than one predecessor...
790 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
791 std::vector<Value*> workList;
792 topo_sort(anticIn, workList);
794 DOUT << "Merge Block: " << BB->getName() << "\n";
795 DOUT << "ANTIC_IN: ";
796 dump_unique(anticIn);
799 for (unsigned i = 0; i < workList.size(); ++i) {
800 Value* e = workList[i];
802 if (isa<BinaryOperator>(e) || isa<CmpInst>(e)) {
803 if (find_leader(availableOut[DI->getIDom()->getBlock()], e) != 0)
806 std::map<BasicBlock*, Value*> avail;
807 bool by_some = false;
810 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
812 Value *e2 = phi_translate(e, *PI, BB);
813 Value *e3 = find_leader(availableOut[*PI], e2);
816 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
817 if (av != avail.end())
819 avail.insert(std::make_pair(*PI, e2));
821 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
822 if (av != avail.end())
824 avail.insert(std::make_pair(*PI, e3));
832 num_avail < std::distance(pred_begin(BB), pred_end(BB))) {
833 DOUT << "Processing Value: ";
837 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
839 Value* e2 = avail[*PI];
840 if (!find_leader(availableOut[*PI], e2)) {
841 User* U = cast<User>(e2);
844 if (isa<BinaryOperator>(U->getOperand(0)) ||
845 isa<CmpInst>(U->getOperand(0)))
846 s1 = find_leader(availableOut[*PI], U->getOperand(0));
848 s1 = U->getOperand(0);
851 if (isa<BinaryOperator>(U->getOperand(1)) ||
852 isa<CmpInst>(U->getOperand(1)))
853 s2 = find_leader(availableOut[*PI], U->getOperand(1));
855 s2 = U->getOperand(1);
858 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
859 newVal = BinaryOperator::create(BO->getOpcode(),
861 BO->getName()+".gvnpre",
862 (*PI)->getTerminator());
863 else if (CmpInst* C = dyn_cast<CmpInst>(U))
864 newVal = CmpInst::create(C->getOpcode(),
867 C->getName()+".gvnpre",
868 (*PI)->getTerminator());
872 std::set<Value*, ExprLT>& predAvail = availableOut[*PI];
873 Value* val = find_leader(predAvail, newVal);
875 predAvail.erase(val);
876 val = find_leader(predAvail, newVal);
878 predAvail.insert(newVal);
880 DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
882 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
883 if (av != avail.end())
885 avail.insert(std::make_pair(*PI, newVal));
893 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
896 p = new PHINode(avail[*PI]->getType(), "gvnpre-join",
899 p->addIncoming(avail[*PI], *PI);
903 DOUT << "Creating value: " << std::hex << p << std::dec << "\n";
905 Value* val = find_leader(availOut, p);
908 val = find_leader(availOut, p);
914 DOUT << "Preds After Processing: ";
915 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
917 DEBUG((*PI)->dump());
920 DOUT << "Merge Block After Processing: ";
935 // Phase 3: Eliminate