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 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
105 AU.setPreservesCFG();
106 AU.addRequired<DominatorTree>();
107 AU.addRequired<PostDominatorTree>();
111 // FIXME: eliminate or document these better
112 void dump(const std::set<Value*>& s) const;
113 void dump_unique(const std::set<Value*, ExprLT>& s) const;
114 void clean(std::set<Value*, ExprLT>& set);
115 bool add(Value* V, uint32_t number);
116 Value* find_leader(std::set<Value*, ExprLT>& vals,
118 Value* phi_translate(std::set<Value*, ExprLT>& set,
119 Value* V, BasicBlock* pred, BasicBlock* succ);
120 void phi_translate_set(std::set<Value*, ExprLT>& anticIn, BasicBlock* pred,
121 BasicBlock* succ, std::set<Value*, ExprLT>& out);
123 void topo_sort(std::set<Value*, ExprLT>& set,
124 std::vector<Value*>& vec);
126 // For a given block, calculate the generated expressions, temporaries,
127 // and the AVAIL_OUT set
128 void CalculateAvailOut(DomTreeNode* DI,
129 std::set<Value*, ExprLT>& currExps,
130 std::set<PHINode*>& currPhis,
131 std::set<Value*>& currTemps,
132 std::set<Value*, ExprLT>& currAvail,
133 std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut);
141 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
143 RegisterPass<GVNPRE> X("gvnpre",
144 "Global Value Numbering/Partial Redundancy Elimination");
147 STATISTIC(NumInsertedVals, "Number of values inserted");
148 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
149 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
152 bool GVNPRE::add(Value* V, uint32_t number) {
153 std::pair<ValueTable::iterator, bool> ret = VN.insert(std::make_pair(V, number));
154 if (isa<BinaryOperator>(V) || isa<PHINode>(V) || isa<CmpInst>(V))
159 Value* GVNPRE::find_leader(std::set<Value*, ExprLT>& vals, Value* v) {
160 for (std::set<Value*, ExprLT>::iterator I = vals.begin(), E = vals.end();
162 assert(VN.find(v) != VN.end() && "Value not numbered?");
163 assert(VN.find(*I) != VN.end() && "Value not numbered?");
171 Value* GVNPRE::phi_translate(std::set<Value*, ExprLT>& set,
172 Value* V, BasicBlock* pred, BasicBlock* succ) {
176 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
177 Value* newOp1 = isa<Instruction>(BO->getOperand(0))
179 find_leader(set, BO->getOperand(0)),
185 Value* newOp2 = isa<Instruction>(BO->getOperand(1))
187 find_leader(set, BO->getOperand(1)),
193 if (newOp1 != BO->getOperand(0) || newOp2 != BO->getOperand(1)) {
194 Instruction* newVal = BinaryOperator::create(BO->getOpcode(),
196 BO->getName()+".gvnpre");
198 if (add(newVal, nextValueNumber))
200 if (!find_leader(set, newVal)) {
201 DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
202 createdExpressions.push_back(newVal);
205 ValueTable::iterator I = VN.find(newVal);
206 if (I->first == newVal)
209 std::set<Value*, ExprLT>::iterator F = MS.find(newVal);
217 } else if (PHINode* P = dyn_cast<PHINode>(V)) {
218 if (P->getParent() == succ)
219 return P->getIncomingValueForBlock(pred);
220 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
221 Value* newOp1 = isa<Instruction>(C->getOperand(0))
223 find_leader(set, C->getOperand(0)),
229 Value* newOp2 = isa<Instruction>(C->getOperand(1))
231 find_leader(set, C->getOperand(1)),
237 if (newOp1 != C->getOperand(0) || newOp2 != C->getOperand(1)) {
238 Instruction* newVal = CmpInst::create(C->getOpcode(),
241 C->getName()+".gvnpre");
243 if (add(newVal, nextValueNumber))
245 if (!find_leader(set, newVal)) {
246 DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
247 createdExpressions.push_back(newVal);
250 ValueTable::iterator I = VN.find(newVal);
251 if (I->first == newVal)
254 std::set<Value*, ExprLT>::iterator F = MS.find(newVal);
267 void GVNPRE::phi_translate_set(std::set<Value*, ExprLT>& anticIn,
268 BasicBlock* pred, BasicBlock* succ,
269 std::set<Value*, ExprLT>& out) {
270 for (std::set<Value*, ExprLT>::iterator I = anticIn.begin(),
271 E = anticIn.end(); I != E; ++I) {
272 Value* V = phi_translate(anticIn, *I, pred, succ);
278 // Remove all expressions whose operands are not themselves in the set
279 void GVNPRE::clean(std::set<Value*, ExprLT>& set) {
280 std::vector<Value*> worklist;
281 topo_sort(set, worklist);
283 for (unsigned i = 0; i < worklist.size(); ++i) {
284 Value* v = worklist[i];
286 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(v)) {
287 bool lhsValid = !isa<Instruction>(BO->getOperand(0));
289 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
291 if (VN[*I] == VN[BO->getOperand(0)]) {
296 bool rhsValid = !isa<Instruction>(BO->getOperand(1));
298 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
300 if (VN[*I] == VN[BO->getOperand(1)]) {
305 if (!lhsValid || !rhsValid)
307 } else if (CmpInst* C = dyn_cast<CmpInst>(v)) {
308 bool lhsValid = !isa<Instruction>(C->getOperand(0));
310 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
312 if (VN[*I] == VN[C->getOperand(0)]) {
317 bool rhsValid = !isa<Instruction>(C->getOperand(1));
319 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
321 if (VN[*I] == VN[C->getOperand(1)]) {
326 if (!lhsValid || !rhsValid)
332 void GVNPRE::topo_sort(std::set<Value*, ExprLT>& set,
333 std::vector<Value*>& vec) {
334 std::set<Value*, ExprLT> toErase;
335 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
337 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(*I))
338 for (std::set<Value*, ExprLT>::iterator SI = set.begin(); SI != E; ++SI) {
339 if (VN[BO->getOperand(0)] == VN[*SI] ||
340 VN[BO->getOperand(1)] == VN[*SI]) {
344 else if (CmpInst* C = dyn_cast<CmpInst>(*I))
345 for (std::set<Value*, ExprLT>::iterator SI = set.begin(); SI != E; ++SI) {
346 if (VN[C->getOperand(0)] == VN[*SI] ||
347 VN[C->getOperand(1)] == VN[*SI]) {
353 std::vector<Value*> Q;
354 for (std::set<Value*, ExprLT>::iterator I = set.begin(), E = set.end();
356 if (toErase.find(*I) == toErase.end())
360 std::set<Value*> visited;
364 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(e)) {
365 Value* l = find_leader(set, BO->getOperand(0));
366 Value* r = find_leader(set, BO->getOperand(1));
368 if (l != 0 && isa<Instruction>(l) &&
369 visited.find(l) == visited.end())
371 else if (r != 0 && isa<Instruction>(r) &&
372 visited.find(r) == visited.end())
379 } else if (CmpInst* C = dyn_cast<CmpInst>(e)) {
380 Value* l = find_leader(set, C->getOperand(0));
381 Value* r = find_leader(set, C->getOperand(1));
383 if (l != 0 && isa<Instruction>(l) &&
384 visited.find(l) == visited.end())
386 else if (r != 0 && isa<Instruction>(r) &&
387 visited.find(r) == visited.end())
403 void GVNPRE::dump(const std::set<Value*>& s) const {
405 for (std::set<Value*>::iterator I = s.begin(), E = s.end();
412 void GVNPRE::dump_unique(const std::set<Value*, ExprLT>& s) const {
414 for (std::set<Value*>::iterator I = s.begin(), E = s.end();
421 void GVNPRE::CalculateAvailOut(DomTreeNode* DI,
422 std::set<Value*, ExprLT>& currExps,
423 std::set<PHINode*>& currPhis,
424 std::set<Value*>& currTemps,
425 std::set<Value*, ExprLT>& currAvail,
426 std::map<BasicBlock*, std::set<Value*, ExprLT> > availOut) {
428 BasicBlock* BB = DI->getBlock();
430 // A block inherits AVAIL_OUT from its dominator
431 if (DI->getIDom() != 0)
432 currAvail.insert(availOut[DI->getIDom()->getBlock()].begin(),
433 availOut[DI->getIDom()->getBlock()].end());
436 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
439 // Handle PHI nodes...
440 if (PHINode* p = dyn_cast<PHINode>(BI)) {
441 if (add(p, nextValueNumber))
445 // Handle binary ops...
446 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
447 Value* leftValue = BO->getOperand(0);
448 Value* rightValue = BO->getOperand(1);
450 if (add(BO, nextValueNumber))
453 if (isa<Instruction>(leftValue))
454 currExps.insert(leftValue);
455 if (isa<Instruction>(rightValue))
456 currExps.insert(rightValue);
460 } else if (CmpInst* C = dyn_cast<CmpInst>(BI)) {
461 Value* leftValue = C->getOperand(0);
462 Value* rightValue = C->getOperand(1);
464 if (add(C, nextValueNumber))
467 if (isa<Instruction>(leftValue))
468 currExps.insert(leftValue);
469 if (isa<Instruction>(rightValue))
470 currExps.insert(rightValue);
473 // Handle unsupported ops
474 } else if (!BI->isTerminator()){
475 if (add(BI, nextValueNumber))
477 currTemps.insert(BI);
480 if (!BI->isTerminator())
481 currAvail.insert(BI);
485 bool GVNPRE::runOnFunction(Function &F) {
488 createdExpressions.clear();
490 std::map<BasicBlock*, std::set<Value*, ExprLT> > generatedExpressions;
491 std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
492 std::map<BasicBlock*, std::set<Value*> > generatedTemporaries;
493 std::map<BasicBlock*, std::set<Value*, ExprLT> > availableOut;
494 std::map<BasicBlock*, std::set<Value*, ExprLT> > anticipatedIn;
496 DominatorTree &DT = getAnalysis<DominatorTree>();
498 // Phase 1: BuildSets
500 // Phase 1, Part 1: calculate AVAIL_OUT
502 // Top-down walk of the dominator tree
503 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
504 E = df_end(DT.getRootNode()); DI != E; ++DI) {
506 // Get the sets to update for this block
507 std::set<Value*, ExprLT>& currExps = generatedExpressions[DI->getBlock()];
508 std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
509 std::set<Value*>& currTemps = generatedTemporaries[DI->getBlock()];
510 std::set<Value*, ExprLT>& currAvail = availableOut[DI->getBlock()];
512 CalculateAvailOut(*DI, currExps, currPhis,
513 currTemps, currAvail, availableOut);
516 DOUT << "Maximal Set: ";
520 PostDominatorTree &PDT = getAnalysis<PostDominatorTree>();
522 // Phase 1, Part 2: calculate ANTIC_IN
524 std::set<BasicBlock*> visited;
527 unsigned iterations = 0;
530 std::set<Value*, ExprLT> anticOut;
532 // Top-down walk of the postdominator tree
533 for (df_iterator<DomTreeNode*> PDI =
534 df_begin(PDT.getRootNode()), E = df_end(PDT.getRootNode());
536 BasicBlock* BB = PDI->getBlock();
540 DOUT << "Block: " << BB->getName() << "\n";
542 dump(generatedTemporaries[BB]);
546 dump_unique(generatedExpressions[BB]);
549 std::set<Value*, ExprLT>& anticIn = anticipatedIn[BB];
550 std::set<Value*, ExprLT> old (anticIn.begin(), anticIn.end());
552 if (BB->getTerminator()->getNumSuccessors() == 1) {
553 if (visited.find(BB->getTerminator()->getSuccessor(0)) ==
555 phi_translate_set(MS, BB, BB->getTerminator()->getSuccessor(0),
558 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
559 BB, BB->getTerminator()->getSuccessor(0),
561 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
562 BasicBlock* first = BB->getTerminator()->getSuccessor(0);
563 anticOut.insert(anticipatedIn[first].begin(),
564 anticipatedIn[first].end());
565 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
566 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
567 std::set<Value*, ExprLT>& succAnticIn = anticipatedIn[currSucc];
569 std::set<Value*, ExprLT> temp;
570 std::insert_iterator<std::set<Value*, ExprLT> > temp_ins(temp,
572 std::set_intersection(anticOut.begin(), anticOut.end(),
573 succAnticIn.begin(), succAnticIn.end(),
577 anticOut.insert(temp.begin(), temp.end());
581 DOUT << "ANTIC_OUT: ";
582 dump_unique(anticOut);
585 std::set<Value*, ExprLT> S;
586 std::insert_iterator<std::set<Value*, ExprLT> > s_ins(S, S.begin());
587 std::set_union(anticOut.begin(), anticOut.end(),
588 generatedExpressions[BB].begin(),
589 generatedExpressions[BB].end(),
594 for (std::set<Value*, ExprLT>::iterator I = S.begin(), E = S.end();
596 if (generatedTemporaries[BB].find(*I) == generatedTemporaries[BB].end())
602 DOUT << "ANTIC_IN: ";
603 dump_unique(anticIn);
606 if (old.size() != anticIn.size())
615 DOUT << "Iterations: " << iterations << "\n";
617 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
618 DOUT << "Name: " << I->getName().c_str() << "\n";
621 dump(generatedTemporaries[I]);
625 dump_unique(generatedExpressions[I]);
628 DOUT << "ANTIC_IN: ";
629 dump_unique(anticipatedIn[I]);
632 DOUT << "AVAIL_OUT: ";
633 dump_unique(availableOut[I]);
639 DOUT<< "\nPhase 2: Insertion\n";
641 std::map<BasicBlock*, std::set<Value*, ExprLT> > new_sets;
642 unsigned i_iterations = 0;
643 bool new_stuff = true;
646 DOUT << "Iteration: " << i_iterations << "\n\n";
647 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
648 E = df_end(DT.getRootNode()); DI != E; ++DI) {
649 BasicBlock* BB = DI->getBlock();
654 std::set<Value*, ExprLT>& new_set = new_sets[BB];
655 std::set<Value*, ExprLT>& availOut = availableOut[BB];
656 std::set<Value*, ExprLT>& anticIn = anticipatedIn[BB];
660 // Replace leaders with leaders inherited from dominator
661 if (DI->getIDom() != 0) {
662 std::set<Value*, ExprLT>& dom_set = new_sets[DI->getIDom()->getBlock()];
663 for (std::set<Value*, ExprLT>::iterator I = dom_set.begin(),
664 E = dom_set.end(); I != E; ++I) {
667 Value* val = find_leader(availOut, *I);
670 val = find_leader(availOut, *I);
676 // If there is more than one predecessor...
677 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
678 std::vector<Value*> workList;
679 topo_sort(anticIn, workList);
681 DOUT << "Merge Block: " << BB->getName() << "\n";
682 DOUT << "ANTIC_IN: ";
683 dump_unique(anticIn);
686 for (unsigned i = 0; i < workList.size(); ++i) {
687 Value* e = workList[i];
689 if (isa<BinaryOperator>(e) || isa<CmpInst>(e)) {
690 if (find_leader(availableOut[DI->getIDom()->getBlock()], e) != 0)
693 std::map<BasicBlock*, Value*> avail;
694 bool by_some = false;
697 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
699 Value *e2 = phi_translate(anticIn, e, *PI, BB);
700 Value *e3 = find_leader(availableOut[*PI], e2);
703 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
704 if (av != avail.end())
706 avail.insert(std::make_pair(*PI, e2));
708 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
709 if (av != avail.end())
711 avail.insert(std::make_pair(*PI, e3));
719 num_avail < std::distance(pred_begin(BB), pred_end(BB))) {
720 DOUT << "Processing Value: ";
724 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
726 Value* e2 = avail[*PI];
727 if (!find_leader(availableOut[*PI], e2)) {
728 User* U = cast<User>(e2);
731 if (isa<Instruction>(U->getOperand(0)))
732 s1 = find_leader(availableOut[*PI],
733 phi_translate(availableOut[*PI],
738 s1 = U->getOperand(0);
741 if (isa<Instruction>(U->getOperand(1)))
742 s2 = find_leader(availableOut[*PI],
743 phi_translate(availableOut[*PI],
748 s2 = U->getOperand(1);
751 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
752 newVal = BinaryOperator::create(BO->getOpcode(),
754 BO->getName()+".gvnpre",
755 (*PI)->getTerminator());
756 else if (CmpInst* C = dyn_cast<CmpInst>(U))
757 newVal = CmpInst::create(C->getOpcode(),
760 C->getName()+".gvnpre",
761 (*PI)->getTerminator());
765 std::set<Value*, ExprLT>& predAvail = availableOut[*PI];
766 Value* val = find_leader(predAvail, newVal);
768 predAvail.erase(val);
769 val = find_leader(predAvail, newVal);
771 predAvail.insert(newVal);
773 DOUT << "Creating value: " << std::hex << newVal << std::dec << "\n";
775 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
776 if (av != avail.end())
778 avail.insert(std::make_pair(*PI, newVal));
786 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
789 p = new PHINode(avail[*PI]->getType(), "gvnpre-join",
792 p->addIncoming(avail[*PI], *PI);
796 DOUT << "Creating value: " << std::hex << p << std::dec << "\n";
798 Value* val = find_leader(availOut, p);
801 val = find_leader(availOut, p);
807 DOUT << "Preds After Processing: ";
808 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
810 DEBUG((*PI)->dump());
813 DOUT << "Merge Block After Processing: ";
828 // Phase 3: Eliminate
829 DOUT << "\n\nPhase 3: Elimination\n\n";
831 std::vector<std::pair<Instruction*, Value*> > replace;
832 std::vector<Instruction*> erase;
834 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
835 E = df_end(DT.getRootNode()); DI != E; ++DI) {
836 BasicBlock* BB = DI->getBlock();
838 DOUT << "Block: " << BB->getName() << "\n";
839 dump_unique(availableOut[BB]);
842 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
845 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI)) {
846 Value *leader = find_leader(availableOut[BB], BI);
849 if (Instruction* Instr = dyn_cast<Instruction>(leader))
850 if (Instr->getParent() != 0 && Instr != BI) {
851 replace.push_back(std::make_pair(BI, leader));
859 while (!replace.empty()) {
860 std::pair<Instruction*, Value*> rep = replace.back();
862 rep.first->replaceAllUsesWith(rep.second);
865 for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
867 (*I)->eraseFromParent();
870 while (!createdExpressions.empty()) {
871 Instruction* I = createdExpressions.back();
872 createdExpressions.pop_back();