LLVM API Documentation
00001 //===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass performs a hybrid of global value numbering and partial redundancy 00011 // elimination, known as GVN-PRE. It performs partial redundancy elimination on 00012 // values, rather than lexical expressions, allowing a more comprehensive view 00013 // the optimization. It replaces redundant values with uses of earlier 00014 // occurences of the same value. While this is beneficial in that it eliminates 00015 // unneeded computation, it also increases register pressure by creating large 00016 // live ranges, and should be used with caution on platforms that are very 00017 // sensitive to register pressure. 00018 // 00019 // Note that this pass does the value numbering itself, it does not use the 00020 // ValueNumbering analysis passes. 00021 // 00022 //===----------------------------------------------------------------------===// 00023 00024 #define DEBUG_TYPE "gvnpre" 00025 #include "llvm/Value.h" 00026 #include "llvm/Transforms/Scalar.h" 00027 #include "llvm/Instructions.h" 00028 #include "llvm/Function.h" 00029 #include "llvm/DerivedTypes.h" 00030 #include "llvm/Analysis/Dominators.h" 00031 #include "llvm/ADT/BitVector.h" 00032 #include "llvm/ADT/DenseMap.h" 00033 #include "llvm/ADT/DepthFirstIterator.h" 00034 #include "llvm/ADT/PostOrderIterator.h" 00035 #include "llvm/ADT/SmallPtrSet.h" 00036 #include "llvm/ADT/SmallVector.h" 00037 #include "llvm/ADT/Statistic.h" 00038 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 00039 #include "llvm/Support/CFG.h" 00040 #include "llvm/Support/Compiler.h" 00041 #include "llvm/Support/Debug.h" 00042 #include <algorithm> 00043 #include <deque> 00044 #include <map> 00045 using namespace llvm; 00046 00047 //===----------------------------------------------------------------------===// 00048 // ValueTable Class 00049 //===----------------------------------------------------------------------===// 00050 00051 namespace { 00052 00053 /// This class holds the mapping between values and value numbers. It is used 00054 /// as an efficient mechanism to determine the expression-wise equivalence of 00055 /// two values. 00056 00057 struct Expression { 00058 enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM, 00059 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ, 00060 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE, 00061 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ, 00062 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE, 00063 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE, 00064 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT, 00065 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI, 00066 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT, 00067 PTRTOINT, INTTOPTR, BITCAST, GEP, EMPTY, 00068 TOMBSTONE }; 00069 00070 ExpressionOpcode opcode; 00071 const Type* type; 00072 uint32_t firstVN; 00073 uint32_t secondVN; 00074 uint32_t thirdVN; 00075 SmallVector<uint32_t, 4> varargs; 00076 00077 Expression() { } 00078 explicit Expression(ExpressionOpcode o) : opcode(o) { } 00079 00080 bool operator==(const Expression &other) const { 00081 if (opcode != other.opcode) 00082 return false; 00083 else if (opcode == EMPTY || opcode == TOMBSTONE) 00084 return true; 00085 else if (type != other.type) 00086 return false; 00087 else if (firstVN != other.firstVN) 00088 return false; 00089 else if (secondVN != other.secondVN) 00090 return false; 00091 else if (thirdVN != other.thirdVN) 00092 return false; 00093 else { 00094 if (varargs.size() != other.varargs.size()) 00095 return false; 00096 00097 for (size_t i = 0; i < varargs.size(); ++i) 00098 if (varargs[i] != other.varargs[i]) 00099 return false; 00100 00101 return true; 00102 } 00103 } 00104 00105 bool operator!=(const Expression &other) const { 00106 if (opcode != other.opcode) 00107 return true; 00108 else if (opcode == EMPTY || opcode == TOMBSTONE) 00109 return false; 00110 else if (type != other.type) 00111 return true; 00112 else if (firstVN != other.firstVN) 00113 return true; 00114 else if (secondVN != other.secondVN) 00115 return true; 00116 else if (thirdVN != other.thirdVN) 00117 return true; 00118 else { 00119 if (varargs.size() != other.varargs.size()) 00120 return true; 00121 00122 for (size_t i = 0; i < varargs.size(); ++i) 00123 if (varargs[i] != other.varargs[i]) 00124 return true; 00125 00126 return false; 00127 } 00128 } 00129 }; 00130 00131 } 00132 00133 namespace { 00134 class VISIBILITY_HIDDEN ValueTable { 00135 private: 00136 DenseMap<Value*, uint32_t> valueNumbering; 00137 DenseMap<Expression, uint32_t> expressionNumbering; 00138 00139 uint32_t nextValueNumber; 00140 00141 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO); 00142 Expression::ExpressionOpcode getOpcode(CmpInst* C); 00143 Expression::ExpressionOpcode getOpcode(CastInst* C); 00144 Expression create_expression(BinaryOperator* BO); 00145 Expression create_expression(CmpInst* C); 00146 Expression create_expression(ShuffleVectorInst* V); 00147 Expression create_expression(ExtractElementInst* C); 00148 Expression create_expression(InsertElementInst* V); 00149 Expression create_expression(SelectInst* V); 00150 Expression create_expression(CastInst* C); 00151 Expression create_expression(GetElementPtrInst* G); 00152 public: 00153 ValueTable() { nextValueNumber = 1; } 00154 uint32_t lookup_or_add(Value* V); 00155 uint32_t lookup(Value* V) const; 00156 void add(Value* V, uint32_t num); 00157 void clear(); 00158 void erase(Value* v); 00159 unsigned size(); 00160 }; 00161 } 00162 00163 namespace llvm { 00164 template <> struct DenseMapInfo<Expression> { 00165 static inline Expression getEmptyKey() { 00166 return Expression(Expression::EMPTY); 00167 } 00168 00169 static inline Expression getTombstoneKey() { 00170 return Expression(Expression::TOMBSTONE); 00171 } 00172 00173 static unsigned getHashValue(const Expression e) { 00174 unsigned hash = e.opcode; 00175 00176 hash = e.firstVN + hash * 37; 00177 hash = e.secondVN + hash * 37; 00178 hash = e.thirdVN + hash * 37; 00179 00180 hash = ((unsigned)((uintptr_t)e.type >> 4) ^ 00181 (unsigned)((uintptr_t)e.type >> 9)) + 00182 hash * 37; 00183 00184 for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(), 00185 E = e.varargs.end(); I != E; ++I) 00186 hash = *I + hash * 37; 00187 00188 return hash; 00189 } 00190 static bool isEqual(const Expression &LHS, const Expression &RHS) { 00191 return LHS == RHS; 00192 } 00193 static bool isPod() { return true; } 00194 }; 00195 } 00196 00197 //===----------------------------------------------------------------------===// 00198 // ValueTable Internal Functions 00199 //===----------------------------------------------------------------------===// 00200 Expression::ExpressionOpcode 00201 ValueTable::getOpcode(BinaryOperator* BO) { 00202 switch(BO->getOpcode()) { 00203 case Instruction::Add: 00204 return Expression::ADD; 00205 case Instruction::Sub: 00206 return Expression::SUB; 00207 case Instruction::Mul: 00208 return Expression::MUL; 00209 case Instruction::UDiv: 00210 return Expression::UDIV; 00211 case Instruction::SDiv: 00212 return Expression::SDIV; 00213 case Instruction::FDiv: 00214 return Expression::FDIV; 00215 case Instruction::URem: 00216 return Expression::UREM; 00217 case Instruction::SRem: 00218 return Expression::SREM; 00219 case Instruction::FRem: 00220 return Expression::FREM; 00221 case Instruction::Shl: 00222 return Expression::SHL; 00223 case Instruction::LShr: 00224 return Expression::LSHR; 00225 case Instruction::AShr: 00226 return Expression::ASHR; 00227 case Instruction::And: 00228 return Expression::AND; 00229 case Instruction::Or: 00230 return Expression::OR; 00231 case Instruction::Xor: 00232 return Expression::XOR; 00233 00234 // THIS SHOULD NEVER HAPPEN 00235 default: 00236 assert(0 && "Binary operator with unknown opcode?"); 00237 return Expression::ADD; 00238 } 00239 } 00240 00241 Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) { 00242 if (C->getOpcode() == Instruction::ICmp) { 00243 switch (C->getPredicate()) { 00244 case ICmpInst::ICMP_EQ: 00245 return Expression::ICMPEQ; 00246 case ICmpInst::ICMP_NE: 00247 return Expression::ICMPNE; 00248 case ICmpInst::ICMP_UGT: 00249 return Expression::ICMPUGT; 00250 case ICmpInst::ICMP_UGE: 00251 return Expression::ICMPUGE; 00252 case ICmpInst::ICMP_ULT: 00253 return Expression::ICMPULT; 00254 case ICmpInst::ICMP_ULE: 00255 return Expression::ICMPULE; 00256 case ICmpInst::ICMP_SGT: 00257 return Expression::ICMPSGT; 00258 case ICmpInst::ICMP_SGE: 00259 return Expression::ICMPSGE; 00260 case ICmpInst::ICMP_SLT: 00261 return Expression::ICMPSLT; 00262 case ICmpInst::ICMP_SLE: 00263 return Expression::ICMPSLE; 00264 00265 // THIS SHOULD NEVER HAPPEN 00266 default: 00267 assert(0 && "Comparison with unknown predicate?"); 00268 return Expression::ICMPEQ; 00269 } 00270 } else { 00271 switch (C->getPredicate()) { 00272 case FCmpInst::FCMP_OEQ: 00273 return Expression::FCMPOEQ; 00274 case FCmpInst::FCMP_OGT: 00275 return Expression::FCMPOGT; 00276 case FCmpInst::FCMP_OGE: 00277 return Expression::FCMPOGE; 00278 case FCmpInst::FCMP_OLT: 00279 return Expression::FCMPOLT; 00280 case FCmpInst::FCMP_OLE: 00281 return Expression::FCMPOLE; 00282 case FCmpInst::FCMP_ONE: 00283 return Expression::FCMPONE; 00284 case FCmpInst::FCMP_ORD: 00285 return Expression::FCMPORD; 00286 case FCmpInst::FCMP_UNO: 00287 return Expression::FCMPUNO; 00288 case FCmpInst::FCMP_UEQ: 00289 return Expression::FCMPUEQ; 00290 case FCmpInst::FCMP_UGT: 00291 return Expression::FCMPUGT; 00292 case FCmpInst::FCMP_UGE: 00293 return Expression::FCMPUGE; 00294 case FCmpInst::FCMP_ULT: 00295 return Expression::FCMPULT; 00296 case FCmpInst::FCMP_ULE: 00297 return Expression::FCMPULE; 00298 case FCmpInst::FCMP_UNE: 00299 return Expression::FCMPUNE; 00300 00301 // THIS SHOULD NEVER HAPPEN 00302 default: 00303 assert(0 && "Comparison with unknown predicate?"); 00304 return Expression::FCMPOEQ; 00305 } 00306 } 00307 } 00308 00309 Expression::ExpressionOpcode 00310 ValueTable::getOpcode(CastInst* C) { 00311 switch(C->getOpcode()) { 00312 case Instruction::Trunc: 00313 return Expression::TRUNC; 00314 case Instruction::ZExt: 00315 return Expression::ZEXT; 00316 case Instruction::SExt: 00317 return Expression::SEXT; 00318 case Instruction::FPToUI: 00319 return Expression::FPTOUI; 00320 case Instruction::FPToSI: 00321 return Expression::FPTOSI; 00322 case Instruction::UIToFP: 00323 return Expression::UITOFP; 00324 case Instruction::SIToFP: 00325 return Expression::SITOFP; 00326 case Instruction::FPTrunc: 00327 return Expression::FPTRUNC; 00328 case Instruction::FPExt: 00329 return Expression::FPEXT; 00330 case Instruction::PtrToInt: 00331 return Expression::PTRTOINT; 00332 case Instruction::IntToPtr: 00333 return Expression::INTTOPTR; 00334 case Instruction::BitCast: 00335 return Expression::BITCAST; 00336 00337 // THIS SHOULD NEVER HAPPEN 00338 default: 00339 assert(0 && "Cast operator with unknown opcode?"); 00340 return Expression::BITCAST; 00341 } 00342 } 00343 00344 Expression ValueTable::create_expression(BinaryOperator* BO) { 00345 Expression e; 00346 00347 e.firstVN = lookup_or_add(BO->getOperand(0)); 00348 e.secondVN = lookup_or_add(BO->getOperand(1)); 00349 e.thirdVN = 0; 00350 e.type = BO->getType(); 00351 e.opcode = getOpcode(BO); 00352 00353 return e; 00354 } 00355 00356 Expression ValueTable::create_expression(CmpInst* C) { 00357 Expression e; 00358 00359 e.firstVN = lookup_or_add(C->getOperand(0)); 00360 e.secondVN = lookup_or_add(C->getOperand(1)); 00361 e.thirdVN = 0; 00362 e.type = C->getType(); 00363 e.opcode = getOpcode(C); 00364 00365 return e; 00366 } 00367 00368 Expression ValueTable::create_expression(CastInst* C) { 00369 Expression e; 00370 00371 e.firstVN = lookup_or_add(C->getOperand(0)); 00372 e.secondVN = 0; 00373 e.thirdVN = 0; 00374 e.type = C->getType(); 00375 e.opcode = getOpcode(C); 00376 00377 return e; 00378 } 00379 00380 Expression ValueTable::create_expression(ShuffleVectorInst* S) { 00381 Expression e; 00382 00383 e.firstVN = lookup_or_add(S->getOperand(0)); 00384 e.secondVN = lookup_or_add(S->getOperand(1)); 00385 e.thirdVN = lookup_or_add(S->getOperand(2)); 00386 e.type = S->getType(); 00387 e.opcode = Expression::SHUFFLE; 00388 00389 return e; 00390 } 00391 00392 Expression ValueTable::create_expression(ExtractElementInst* E) { 00393 Expression e; 00394 00395 e.firstVN = lookup_or_add(E->getOperand(0)); 00396 e.secondVN = lookup_or_add(E->getOperand(1)); 00397 e.thirdVN = 0; 00398 e.type = E->getType(); 00399 e.opcode = Expression::EXTRACT; 00400 00401 return e; 00402 } 00403 00404 Expression ValueTable::create_expression(InsertElementInst* I) { 00405 Expression e; 00406 00407 e.firstVN = lookup_or_add(I->getOperand(0)); 00408 e.secondVN = lookup_or_add(I->getOperand(1)); 00409 e.thirdVN = lookup_or_add(I->getOperand(2)); 00410 e.type = I->getType(); 00411 e.opcode = Expression::INSERT; 00412 00413 return e; 00414 } 00415 00416 Expression ValueTable::create_expression(SelectInst* I) { 00417 Expression e; 00418 00419 e.firstVN = lookup_or_add(I->getCondition()); 00420 e.secondVN = lookup_or_add(I->getTrueValue()); 00421 e.thirdVN = lookup_or_add(I->getFalseValue()); 00422 e.type = I->getType(); 00423 e.opcode = Expression::SELECT; 00424 00425 return e; 00426 } 00427 00428 Expression ValueTable::create_expression(GetElementPtrInst* G) { 00429 Expression e; 00430 00431 e.firstVN = lookup_or_add(G->getPointerOperand()); 00432 e.secondVN = 0; 00433 e.thirdVN = 0; 00434 e.type = G->getType(); 00435 e.opcode = Expression::GEP; 00436 00437 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end(); 00438 I != E; ++I) 00439 e.varargs.push_back(lookup_or_add(*I)); 00440 00441 return e; 00442 } 00443 00444 //===----------------------------------------------------------------------===// 00445 // ValueTable External Functions 00446 //===----------------------------------------------------------------------===// 00447 00448 /// lookup_or_add - Returns the value number for the specified value, assigning 00449 /// it a new number if it did not have one before. 00450 uint32_t ValueTable::lookup_or_add(Value* V) { 00451 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); 00452 if (VI != valueNumbering.end()) 00453 return VI->second; 00454 00455 00456 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) { 00457 Expression e = create_expression(BO); 00458 00459 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00460 if (EI != expressionNumbering.end()) { 00461 valueNumbering.insert(std::make_pair(V, EI->second)); 00462 return EI->second; 00463 } else { 00464 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00465 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00466 00467 return nextValueNumber++; 00468 } 00469 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) { 00470 Expression e = create_expression(C); 00471 00472 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00473 if (EI != expressionNumbering.end()) { 00474 valueNumbering.insert(std::make_pair(V, EI->second)); 00475 return EI->second; 00476 } else { 00477 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00478 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00479 00480 return nextValueNumber++; 00481 } 00482 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) { 00483 Expression e = create_expression(U); 00484 00485 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00486 if (EI != expressionNumbering.end()) { 00487 valueNumbering.insert(std::make_pair(V, EI->second)); 00488 return EI->second; 00489 } else { 00490 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00491 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00492 00493 return nextValueNumber++; 00494 } 00495 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) { 00496 Expression e = create_expression(U); 00497 00498 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00499 if (EI != expressionNumbering.end()) { 00500 valueNumbering.insert(std::make_pair(V, EI->second)); 00501 return EI->second; 00502 } else { 00503 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00504 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00505 00506 return nextValueNumber++; 00507 } 00508 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) { 00509 Expression e = create_expression(U); 00510 00511 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00512 if (EI != expressionNumbering.end()) { 00513 valueNumbering.insert(std::make_pair(V, EI->second)); 00514 return EI->second; 00515 } else { 00516 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00517 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00518 00519 return nextValueNumber++; 00520 } 00521 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) { 00522 Expression e = create_expression(U); 00523 00524 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00525 if (EI != expressionNumbering.end()) { 00526 valueNumbering.insert(std::make_pair(V, EI->second)); 00527 return EI->second; 00528 } else { 00529 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00530 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00531 00532 return nextValueNumber++; 00533 } 00534 } else if (CastInst* U = dyn_cast<CastInst>(V)) { 00535 Expression e = create_expression(U); 00536 00537 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00538 if (EI != expressionNumbering.end()) { 00539 valueNumbering.insert(std::make_pair(V, EI->second)); 00540 return EI->second; 00541 } else { 00542 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00543 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00544 00545 return nextValueNumber++; 00546 } 00547 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) { 00548 Expression e = create_expression(U); 00549 00550 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); 00551 if (EI != expressionNumbering.end()) { 00552 valueNumbering.insert(std::make_pair(V, EI->second)); 00553 return EI->second; 00554 } else { 00555 expressionNumbering.insert(std::make_pair(e, nextValueNumber)); 00556 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00557 00558 return nextValueNumber++; 00559 } 00560 } else { 00561 valueNumbering.insert(std::make_pair(V, nextValueNumber)); 00562 return nextValueNumber++; 00563 } 00564 } 00565 00566 /// lookup - Returns the value number of the specified value. Fails if 00567 /// the value has not yet been numbered. 00568 uint32_t ValueTable::lookup(Value* V) const { 00569 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); 00570 if (VI != valueNumbering.end()) 00571 return VI->second; 00572 else 00573 assert(0 && "Value not numbered?"); 00574 00575 return 0; 00576 } 00577 00578 /// add - Add the specified value with the given value number, removing 00579 /// its old number, if any 00580 void ValueTable::add(Value* V, uint32_t num) { 00581 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); 00582 if (VI != valueNumbering.end()) 00583 valueNumbering.erase(VI); 00584 valueNumbering.insert(std::make_pair(V, num)); 00585 } 00586 00587 /// clear - Remove all entries from the ValueTable 00588 void ValueTable::clear() { 00589 valueNumbering.clear(); 00590 expressionNumbering.clear(); 00591 nextValueNumber = 1; 00592 } 00593 00594 /// erase - Remove a value from the value numbering 00595 void ValueTable::erase(Value* V) { 00596 valueNumbering.erase(V); 00597 } 00598 00599 /// size - Return the number of assigned value numbers 00600 unsigned ValueTable::size() { 00601 // NOTE: zero is never assigned 00602 return nextValueNumber; 00603 } 00604 00605 namespace { 00606 00607 //===----------------------------------------------------------------------===// 00608 // ValueNumberedSet Class 00609 //===----------------------------------------------------------------------===// 00610 00611 class ValueNumberedSet { 00612 private: 00613 SmallPtrSet<Value*, 8> contents; 00614 BitVector numbers; 00615 public: 00616 ValueNumberedSet() { numbers.resize(1); } 00617 ValueNumberedSet(const ValueNumberedSet& other) { 00618 numbers = other.numbers; 00619 contents = other.contents; 00620 } 00621 00622 typedef SmallPtrSet<Value*, 8>::iterator iterator; 00623 00624 iterator begin() { return contents.begin(); } 00625 iterator end() { return contents.end(); } 00626 00627 bool insert(Value* v) { return contents.insert(v); } 00628 void insert(iterator I, iterator E) { contents.insert(I, E); } 00629 void erase(Value* v) { contents.erase(v); } 00630 unsigned count(Value* v) { return contents.count(v); } 00631 size_t size() { return contents.size(); } 00632 00633 void set(unsigned i) { 00634 if (i >= numbers.size()) 00635 numbers.resize(i+1); 00636 00637 numbers.set(i); 00638 } 00639 00640 void operator=(const ValueNumberedSet& other) { 00641 contents = other.contents; 00642 numbers = other.numbers; 00643 } 00644 00645 void reset(unsigned i) { 00646 if (i < numbers.size()) 00647 numbers.reset(i); 00648 } 00649 00650 bool test(unsigned i) { 00651 if (i >= numbers.size()) 00652 return false; 00653 00654 return numbers.test(i); 00655 } 00656 00657 void clear() { 00658 contents.clear(); 00659 numbers.clear(); 00660 } 00661 }; 00662 00663 } 00664 00665 //===----------------------------------------------------------------------===// 00666 // GVNPRE Pass 00667 //===----------------------------------------------------------------------===// 00668 00669 namespace { 00670 00671 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass { 00672 bool runOnFunction(Function &F); 00673 public: 00674 static char ID; // Pass identification, replacement for typeid 00675 GVNPRE() : FunctionPass(&ID) {} 00676 00677 private: 00678 ValueTable VN; 00679 SmallVector<Instruction*, 8> createdExpressions; 00680 00681 DenseMap<BasicBlock*, ValueNumberedSet> availableOut; 00682 DenseMap<BasicBlock*, ValueNumberedSet> anticipatedIn; 00683 DenseMap<BasicBlock*, ValueNumberedSet> generatedPhis; 00684 00685 // This transformation requires dominator postdominator info 00686 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00687 AU.setPreservesCFG(); 00688 AU.addRequiredID(BreakCriticalEdgesID); 00689 AU.addRequired<UnifyFunctionExitNodes>(); 00690 AU.addRequired<DominatorTree>(); 00691 } 00692 00693 // Helper fuctions 00694 // FIXME: eliminate or document these better 00695 void dump(ValueNumberedSet& s) const ; 00696 void clean(ValueNumberedSet& set) ; 00697 Value* find_leader(ValueNumberedSet& vals, uint32_t v) ; 00698 Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) ; 00699 void phi_translate_set(ValueNumberedSet& anticIn, BasicBlock* pred, 00700 BasicBlock* succ, ValueNumberedSet& out) ; 00701 00702 void topo_sort(ValueNumberedSet& set, 00703 SmallVector<Value*, 8>& vec) ; 00704 00705 void cleanup() ; 00706 bool elimination() ; 00707 00708 void val_insert(ValueNumberedSet& s, Value* v) ; 00709 void val_replace(ValueNumberedSet& s, Value* v) ; 00710 bool dependsOnInvoke(Value* V) ; 00711 void buildsets_availout(BasicBlock::iterator I, 00712 ValueNumberedSet& currAvail, 00713 ValueNumberedSet& currPhis, 00714 ValueNumberedSet& currExps, 00715 SmallPtrSet<Value*, 16>& currTemps); 00716 bool buildsets_anticout(BasicBlock* BB, 00717 ValueNumberedSet& anticOut, 00718 SmallPtrSet<BasicBlock*, 8>& visited); 00719 unsigned buildsets_anticin(BasicBlock* BB, 00720 ValueNumberedSet& anticOut, 00721 ValueNumberedSet& currExps, 00722 SmallPtrSet<Value*, 16>& currTemps, 00723 SmallPtrSet<BasicBlock*, 8>& visited); 00724 void buildsets(Function& F) ; 00725 00726 void insertion_pre(Value* e, BasicBlock* BB, 00727 DenseMap<BasicBlock*, Value*>& avail, 00728 std::map<BasicBlock*,ValueNumberedSet>& new_set); 00729 unsigned insertion_mergepoint(SmallVector<Value*, 8>& workList, 00730 df_iterator<DomTreeNode*>& D, 00731 std::map<BasicBlock*, ValueNumberedSet>& new_set); 00732 bool insertion(Function& F) ; 00733 00734 }; 00735 00736 char GVNPRE::ID = 0; 00737 00738 } 00739 00740 // createGVNPREPass - The public interface to this file... 00741 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); } 00742 00743 static RegisterPass<GVNPRE> X("gvnpre", 00744 "Global Value Numbering/Partial Redundancy Elimination"); 00745 00746 00747 STATISTIC(NumInsertedVals, "Number of values inserted"); 00748 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted"); 00749 STATISTIC(NumEliminated, "Number of redundant instructions eliminated"); 00750 00751 /// find_leader - Given a set and a value number, return the first 00752 /// element of the set with that value number, or 0 if no such element 00753 /// is present 00754 Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) { 00755 if (!vals.test(v)) 00756 return 0; 00757 00758 for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end(); 00759 I != E; ++I) 00760 if (v == VN.lookup(*I)) 00761 return *I; 00762 00763 assert(0 && "No leader found, but present bit is set?"); 00764 return 0; 00765 } 00766 00767 /// val_insert - Insert a value into a set only if there is not a value 00768 /// with the same value number already in the set 00769 void GVNPRE::val_insert(ValueNumberedSet& s, Value* v) { 00770 uint32_t num = VN.lookup(v); 00771 if (!s.test(num)) 00772 s.insert(v); 00773 } 00774 00775 /// val_replace - Insert a value into a set, replacing any values already in 00776 /// the set that have the same value number 00777 void GVNPRE::val_replace(ValueNumberedSet& s, Value* v) { 00778 if (s.count(v)) return; 00779 00780 uint32_t num = VN.lookup(v); 00781 Value* leader = find_leader(s, num); 00782 if (leader != 0) 00783 s.erase(leader); 00784 s.insert(v); 00785 s.set(num); 00786 } 00787 00788 /// phi_translate - Given a value, its parent block, and a predecessor of its 00789 /// parent, translate the value into legal for the predecessor block. This 00790 /// means translating its operands (and recursively, their operands) through 00791 /// any phi nodes in the parent into values available in the predecessor 00792 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) { 00793 if (V == 0) 00794 return 0; 00795 00796 // Unary Operations 00797 if (CastInst* U = dyn_cast<CastInst>(V)) { 00798 Value* newOp1 = 0; 00799 if (isa<Instruction>(U->getOperand(0))) 00800 newOp1 = phi_translate(U->getOperand(0), pred, succ); 00801 else 00802 newOp1 = U->getOperand(0); 00803 00804 if (newOp1 == 0) 00805 return 0; 00806 00807 if (newOp1 != U->getOperand(0)) { 00808 Instruction* newVal = 0; 00809 if (CastInst* C = dyn_cast<CastInst>(U)) 00810 newVal = CastInst::Create(C->getOpcode(), 00811 newOp1, C->getType(), 00812 C->getName()+".expr"); 00813 00814 uint32_t v = VN.lookup_or_add(newVal); 00815 00816 Value* leader = find_leader(availableOut[pred], v); 00817 if (leader == 0) { 00818 createdExpressions.push_back(newVal); 00819 return newVal; 00820 } else { 00821 VN.erase(newVal); 00822 delete newVal; 00823 return leader; 00824 } 00825 } 00826 00827 // Binary Operations 00828 } if (isa<BinaryOperator>(V) || isa<CmpInst>(V) || 00829 isa<ExtractElementInst>(V)) { 00830 User* U = cast<User>(V); 00831 00832 Value* newOp1 = 0; 00833 if (isa<Instruction>(U->getOperand(0))) 00834 newOp1 = phi_translate(U->getOperand(0), pred, succ); 00835 else 00836 newOp1 = U->getOperand(0); 00837 00838 if (newOp1 == 0) 00839 return 0; 00840 00841 Value* newOp2 = 0; 00842 if (isa<Instruction>(U->getOperand(1))) 00843 newOp2 = phi_translate(U->getOperand(1), pred, succ); 00844 else 00845 newOp2 = U->getOperand(1); 00846 00847 if (newOp2 == 0) 00848 return 0; 00849 00850 if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) { 00851 Instruction* newVal = 0; 00852 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U)) 00853 newVal = BinaryOperator::Create(BO->getOpcode(), 00854 newOp1, newOp2, 00855 BO->getName()+".expr"); 00856 else if (CmpInst* C = dyn_cast<CmpInst>(U)) 00857 newVal = CmpInst::Create(C->getOpcode(), 00858 C->getPredicate(), 00859 newOp1, newOp2, 00860 C->getName()+".expr"); 00861 else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U)) 00862 newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr"); 00863 00864 uint32_t v = VN.lookup_or_add(newVal); 00865 00866 Value* leader = find_leader(availableOut[pred], v); 00867 if (leader == 0) { 00868 createdExpressions.push_back(newVal); 00869 return newVal; 00870 } else { 00871 VN.erase(newVal); 00872 delete newVal; 00873 return leader; 00874 } 00875 } 00876 00877 // Ternary Operations 00878 } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) || 00879 isa<SelectInst>(V)) { 00880 User* U = cast<User>(V); 00881 00882 Value* newOp1 = 0; 00883 if (isa<Instruction>(U->getOperand(0))) 00884 newOp1 = phi_translate(U->getOperand(0), pred, succ); 00885 else 00886 newOp1 = U->getOperand(0); 00887 00888 if (newOp1 == 0) 00889 return 0; 00890 00891 Value* newOp2 = 0; 00892 if (isa<Instruction>(U->getOperand(1))) 00893 newOp2 = phi_translate(U->getOperand(1), pred, succ); 00894 else 00895 newOp2 = U->getOperand(1); 00896 00897 if (newOp2 == 0) 00898 return 0; 00899 00900 Value* newOp3 = 0; 00901 if (isa<Instruction>(U->getOperand(2))) 00902 newOp3 = phi_translate(U->getOperand(2), pred, succ); 00903 else 00904 newOp3 = U->getOperand(2); 00905 00906 if (newOp3 == 0) 00907 return 0; 00908 00909 if (newOp1 != U->getOperand(0) || 00910 newOp2 != U->getOperand(1) || 00911 newOp3 != U->getOperand(2)) { 00912 Instruction* newVal = 0; 00913 if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U)) 00914 newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3, 00915 S->getName() + ".expr"); 00916 else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U)) 00917 newVal = InsertElementInst::Create(newOp1, newOp2, newOp3, 00918 I->getName() + ".expr"); 00919 else if (SelectInst* I = dyn_cast<SelectInst>(U)) 00920 newVal = SelectInst::Create(newOp1, newOp2, newOp3, 00921 I->getName() + ".expr"); 00922 00923 uint32_t v = VN.lookup_or_add(newVal); 00924 00925 Value* leader = find_leader(availableOut[pred], v); 00926 if (leader == 0) { 00927 createdExpressions.push_back(newVal); 00928 return newVal; 00929 } else { 00930 VN.erase(newVal); 00931 delete newVal; 00932 return leader; 00933 } 00934 } 00935 00936 // Varargs operators 00937 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) { 00938 Value* newOp1 = 0; 00939 if (isa<Instruction>(U->getPointerOperand())) 00940 newOp1 = phi_translate(U->getPointerOperand(), pred, succ); 00941 else 00942 newOp1 = U->getPointerOperand(); 00943 00944 if (newOp1 == 0) 00945 return 0; 00946 00947 bool changed_idx = false; 00948 SmallVector<Value*, 4> newIdx; 00949 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end(); 00950 I != E; ++I) 00951 if (isa<Instruction>(*I)) { 00952 Value* newVal = phi_translate(*I, pred, succ); 00953 newIdx.push_back(newVal); 00954 if (newVal != *I) 00955 changed_idx = true; 00956 } else { 00957 newIdx.push_back(*I); 00958 } 00959 00960 if (newOp1 != U->getPointerOperand() || changed_idx) { 00961 Instruction* newVal = 00962 GetElementPtrInst::Create(newOp1, 00963 newIdx.begin(), newIdx.end(), 00964 U->getName()+".expr"); 00965 00966 uint32_t v = VN.lookup_or_add(newVal); 00967 00968 Value* leader = find_leader(availableOut[pred], v); 00969 if (leader == 0) { 00970 createdExpressions.push_back(newVal); 00971 return newVal; 00972 } else { 00973 VN.erase(newVal); 00974 delete newVal; 00975 return leader; 00976 } 00977 } 00978 00979 // PHI Nodes 00980 } else if (PHINode* P = dyn_cast<PHINode>(V)) { 00981 if (P->getParent() == succ) 00982 return P->getIncomingValueForBlock(pred); 00983 } 00984 00985 return V; 00986 } 00987 00988 /// phi_translate_set - Perform phi translation on every element of a set 00989 void GVNPRE::phi_translate_set(ValueNumberedSet& anticIn, 00990 BasicBlock* pred, BasicBlock* succ, 00991 ValueNumberedSet& out) { 00992 for (ValueNumberedSet::iterator I = anticIn.begin(), 00993 E = anticIn.end(); I != E; ++I) { 00994 Value* V = phi_translate(*I, pred, succ); 00995 if (V != 0 && !out.test(VN.lookup_or_add(V))) { 00996 out.insert(V); 00997 out.set(VN.lookup(V)); 00998 } 00999 } 01000 } 01001 01002 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of 01003 /// whose inputs is an invoke instruction. If this is true, we cannot safely 01004 /// PRE the instruction or anything that depends on it. 01005 bool GVNPRE::dependsOnInvoke(Value* V) { 01006 if (PHINode* p = dyn_cast<PHINode>(V)) { 01007 for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I) 01008 if (isa<InvokeInst>(*I)) 01009 return true; 01010 return false; 01011 } else { 01012 return false; 01013 } 01014 } 01015 01016 /// clean - Remove all non-opaque values from the set whose operands are not 01017 /// themselves in the set, as well as all values that depend on invokes (see 01018 /// above) 01019 void GVNPRE::clean(ValueNumberedSet& set) { 01020 SmallVector<Value*, 8> worklist; 01021 worklist.reserve(set.size()); 01022 topo_sort(set, worklist); 01023 01024 for (unsigned i = 0; i < worklist.size(); ++i) { 01025 Value* v = worklist[i]; 01026 01027 // Handle unary ops 01028 if (CastInst* U = dyn_cast<CastInst>(v)) { 01029 bool lhsValid = !isa<Instruction>(U->getOperand(0)); 01030 lhsValid |= set.test(VN.lookup(U->getOperand(0))); 01031 if (lhsValid) 01032 lhsValid = !dependsOnInvoke(U->getOperand(0)); 01033 01034 if (!lhsValid) { 01035 set.erase(U); 01036 set.reset(VN.lookup(U)); 01037 } 01038 01039 // Handle binary ops 01040 } else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) || 01041 isa<ExtractElementInst>(v)) { 01042 User* U = cast<User>(v); 01043 01044 bool lhsValid = !isa<Instruction>(U->getOperand(0)); 01045 lhsValid |= set.test(VN.lookup(U->getOperand(0))); 01046 if (lhsValid) 01047 lhsValid = !dependsOnInvoke(U->getOperand(0)); 01048 01049 bool rhsValid = !isa<Instruction>(U->getOperand(1)); 01050 rhsValid |= set.test(VN.lookup(U->getOperand(1))); 01051 if (rhsValid) 01052 rhsValid = !dependsOnInvoke(U->getOperand(1)); 01053 01054 if (!lhsValid || !rhsValid) { 01055 set.erase(U); 01056 set.reset(VN.lookup(U)); 01057 } 01058 01059 // Handle ternary ops 01060 } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) || 01061 isa<SelectInst>(v)) { 01062 User* U = cast<User>(v); 01063 01064 bool lhsValid = !isa<Instruction>(U->getOperand(0)); 01065 lhsValid |= set.test(VN.lookup(U->getOperand(0))); 01066 if (lhsValid) 01067 lhsValid = !dependsOnInvoke(U->getOperand(0)); 01068 01069 bool rhsValid = !isa<Instruction>(U->getOperand(1)); 01070 rhsValid |= set.test(VN.lookup(U->getOperand(1))); 01071 if (rhsValid) 01072 rhsValid = !dependsOnInvoke(U->getOperand(1)); 01073 01074 bool thirdValid = !isa<Instruction>(U->getOperand(2)); 01075 thirdValid |= set.test(VN.lookup(U->getOperand(2))); 01076 if (thirdValid) 01077 thirdValid = !dependsOnInvoke(U->getOperand(2)); 01078 01079 if (!lhsValid || !rhsValid || !thirdValid) { 01080 set.erase(U); 01081 set.reset(VN.lookup(U)); 01082 } 01083 01084 // Handle varargs ops 01085 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) { 01086 bool ptrValid = !isa<Instruction>(U->getPointerOperand()); 01087 ptrValid |= set.test(VN.lookup(U->getPointerOperand())); 01088 if (ptrValid) 01089 ptrValid = !dependsOnInvoke(U->getPointerOperand()); 01090 01091 bool varValid = true; 01092 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end(); 01093 I != E; ++I) 01094 if (varValid) { 01095 varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I)); 01096 varValid &= !dependsOnInvoke(*I); 01097 } 01098 01099 if (!ptrValid || !varValid) { 01100 set.erase(U); 01101 set.reset(VN.lookup(U)); 01102 } 01103 } 01104 } 01105 } 01106 01107 /// topo_sort - Given a set of values, sort them by topological 01108 /// order into the provided vector. 01109 void GVNPRE::topo_sort(ValueNumberedSet& set, SmallVector<Value*, 8>& vec) { 01110 SmallPtrSet<Value*, 16> visited; 01111 SmallVector<Value*, 8> stack; 01112 for (ValueNumberedSet::iterator I = set.begin(), E = set.end(); 01113 I != E; ++I) { 01114 if (visited.count(*I) == 0) 01115 stack.push_back(*I); 01116 01117 while (!stack.empty()) { 01118 Value* e = stack.back(); 01119 01120 // Handle unary ops 01121 if (CastInst* U = dyn_cast<CastInst>(e)) { 01122 Value* l = find_leader(set, VN.lookup(U->getOperand(0))); 01123 01124 if (l != 0 && isa<Instruction>(l) && 01125 visited.count(l) == 0) 01126 stack.push_back(l); 01127 else { 01128 vec.push_back(e); 01129 visited.insert(e); 01130 stack.pop_back(); 01131 } 01132 01133 // Handle binary ops 01134 } else if (isa<BinaryOperator>(e) || isa<CmpInst>(e) || 01135 isa<ExtractElementInst>(e)) { 01136 User* U = cast<User>(e); 01137 Value* l = find_leader(set, VN.lookup(U->getOperand(0))); 01138 Value* r = find_leader(set, VN.lookup(U->getOperand(1))); 01139 01140 if (l != 0 && isa<Instruction>(l) && 01141 visited.count(l) == 0) 01142 stack.push_back(l); 01143 else if (r != 0 && isa<Instruction>(r) && 01144 visited.count(r) == 0) 01145 stack.push_back(r); 01146 else { 01147 vec.push_back(e); 01148 visited.insert(e); 01149 stack.pop_back(); 01150 } 01151 01152 // Handle ternary ops 01153 } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) || 01154 isa<SelectInst>(e)) { 01155 User* U = cast<User>(e); 01156 Value* l = find_leader(set, VN.lookup(U->getOperand(0))); 01157 Value* r = find_leader(set, VN.lookup(U->getOperand(1))); 01158 Value* m = find_leader(set, VN.lookup(U->getOperand(2))); 01159 01160 if (l != 0 && isa<Instruction>(l) && 01161 visited.count(l) == 0) 01162 stack.push_back(l); 01163 else if (r != 0 && isa<Instruction>(r) && 01164 visited.count(r) == 0) 01165 stack.push_back(r); 01166 else if (m != 0 && isa<Instruction>(m) && 01167 visited.count(m) == 0) 01168 stack.push_back(m); 01169 else { 01170 vec.push_back(e); 01171 visited.insert(e); 01172 stack.pop_back(); 01173 } 01174 01175 // Handle vararg ops 01176 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) { 01177 Value* p = find_leader(set, VN.lookup(U->getPointerOperand())); 01178 01179 if (p != 0 && isa<Instruction>(p) && 01180 visited.count(p) == 0) 01181 stack.push_back(p); 01182 else { 01183 bool push_va = false; 01184 for (GetElementPtrInst::op_iterator I = U->idx_begin(), 01185 E = U->idx_end(); I != E; ++I) { 01186 Value * v = find_leader(set, VN.lookup(*I)); 01187 if (v != 0 && isa<Instruction>(v) && visited.count(v) == 0) { 01188 stack.push_back(v); 01189 push_va = true; 01190 } 01191 } 01192 01193 if (!push_va) { 01194 vec.push_back(e); 01195 visited.insert(e); 01196 stack.pop_back(); 01197 } 01198 } 01199 01200 // Handle opaque ops 01201 } else { 01202 visited.insert(e); 01203 vec.push_back(e); 01204 stack.pop_back(); 01205 } 01206 } 01207 01208 stack.clear(); 01209 } 01210 } 01211 01212 /// dump - Dump a set of values to standard error 01213 void GVNPRE::dump(ValueNumberedSet& s) const { 01214 DOUT << "{ "; 01215 for (ValueNumberedSet::iterator I = s.begin(), E = s.end(); 01216 I != E; ++I) { 01217 DOUT << "" << VN.lookup(*I) << ": "; 01218 DEBUG((*I)->dump()); 01219 } 01220 DOUT << "}\n\n"; 01221 } 01222 01223 /// elimination - Phase 3 of the main algorithm. Perform full redundancy 01224 /// elimination by walking the dominator tree and removing any instruction that 01225 /// is dominated by another instruction with the same value number. 01226 bool GVNPRE::elimination() { 01227 bool changed_function = false; 01228 01229 SmallVector<std::pair<Instruction*, Value*>, 8> replace; 01230 SmallVector<Instruction*, 8> erase; 01231 01232 DominatorTree& DT = getAnalysis<DominatorTree>(); 01233 01234 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()), 01235 E = df_end(DT.getRootNode()); DI != E; ++DI) { 01236 BasicBlock* BB = DI->getBlock(); 01237 01238 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); 01239 BI != BE; ++BI) { 01240 01241 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) || 01242 isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) || 01243 isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) || 01244 isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) { 01245 01246 if (availableOut[BB].test(VN.lookup(BI)) && 01247 !availableOut[BB].count(BI)) { 01248 Value *leader = find_leader(availableOut[BB], VN.lookup(BI)); 01249 if (Instruction* Instr = dyn_cast<Instruction>(leader)) 01250 if (Instr->getParent() != 0 && Instr != BI) { 01251 replace.push_back(std::make_pair(BI, leader)); 01252 erase.push_back(BI); 01253 ++NumEliminated; 01254 } 01255 } 01256 } 01257 } 01258 } 01259 01260 while (!replace.empty()) { 01261 std::pair<Instruction*, Value*> rep = replace.back(); 01262 replace.pop_back(); 01263 rep.first->replaceAllUsesWith(rep.second); 01264 changed_function = true; 01265 } 01266 01267 for (SmallVector<Instruction*, 8>::iterator I = erase.begin(), 01268 E = erase.end(); I != E; ++I) 01269 (*I)->eraseFromParent(); 01270 01271 return changed_function; 01272 } 01273 01274 /// cleanup - Delete any extraneous values that were created to represent 01275 /// expressions without leaders. 01276 void GVNPRE::cleanup() { 01277 while (!createdExpressions.empty()) { 01278 Instruction* I = createdExpressions.back(); 01279 createdExpressions.pop_back(); 01280 01281 delete I; 01282 } 01283 } 01284 01285 /// buildsets_availout - When calculating availability, handle an instruction 01286 /// by inserting it into the appropriate sets 01287 void GVNPRE::buildsets_availout(BasicBlock::iterator I, 01288 ValueNumberedSet& currAvail, 01289 ValueNumberedSet& currPhis, 01290 ValueNumberedSet& currExps, 01291 SmallPtrSet<Value*, 16>& currTemps) { 01292 // Handle PHI nodes 01293 if (PHINode* p = dyn_cast<PHINode>(I)) { 01294 unsigned num = VN.lookup_or_add(p); 01295 01296 currPhis.insert(p); 01297 currPhis.set(num); 01298 01299 // Handle unary ops 01300 } else if (CastInst* U = dyn_cast<CastInst>(I)) { 01301 Value* leftValue = U->getOperand(0); 01302 01303 unsigned num = VN.lookup_or_add(U); 01304 01305 if (isa<Instruction>(leftValue)) 01306 if (!currExps.test(VN.lookup(leftValue))) { 01307 currExps.insert(leftValue); 01308 currExps.set(VN.lookup(leftValue)); 01309 } 01310 01311 if (!currExps.test(num)) { 01312 currExps.insert(U); 01313 currExps.set(num); 01314 } 01315 01316 // Handle binary ops 01317 } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || 01318 isa<ExtractElementInst>(I)) { 01319 User* U = cast<User>(I); 01320 Value* leftValue = U->getOperand(0); 01321 Value* rightValue = U->getOperand(1); 01322 01323 unsigned num = VN.lookup_or_add(U); 01324 01325 if (isa<Instruction>(leftValue)) 01326 if (!currExps.test(VN.lookup(leftValue))) { 01327 currExps.insert(leftValue); 01328 currExps.set(VN.lookup(leftValue)); 01329 } 01330 01331 if (isa<Instruction>(rightValue)) 01332 if (!currExps.test(VN.lookup(rightValue))) { 01333 currExps.insert(rightValue); 01334 currExps.set(VN.lookup(rightValue)); 01335 } 01336 01337 if (!currExps.test(num)) { 01338 currExps.insert(U); 01339 currExps.set(num); 01340 } 01341 01342 // Handle ternary ops 01343 } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) || 01344 isa<SelectInst>(I)) { 01345 User* U = cast<User>(I); 01346 Value* leftValue = U->getOperand(0); 01347 Value* rightValue = U->getOperand(1); 01348 Value* thirdValue = U->getOperand(2); 01349 01350 VN.lookup_or_add(U); 01351 01352 unsigned num = VN.lookup_or_add(U); 01353 01354 if (isa<Instruction>(leftValue)) 01355 if (!currExps.test(VN.lookup(leftValue))) { 01356 currExps.insert(leftValue); 01357 currExps.set(VN.lookup(leftValue)); 01358 } 01359 if (isa<Instruction>(rightValue)) 01360 if (!currExps.test(VN.lookup(rightValue))) { 01361 currExps.insert(rightValue); 01362 currExps.set(VN.lookup(rightValue)); 01363 } 01364 if (isa<Instruction>(thirdValue)) 01365 if (!currExps.test(VN.lookup(thirdValue))) { 01366 currExps.insert(thirdValue); 01367 currExps.set(VN.lookup(thirdValue)); 01368 } 01369 01370 if (!currExps.test(num)) { 01371 currExps.insert(U); 01372 currExps.set(num); 01373 } 01374 01375 // Handle vararg ops 01376 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) { 01377 Value* ptrValue = U->getPointerOperand(); 01378 01379 VN.lookup_or_add(U); 01380 01381 unsigned num = VN.lookup_or_add(U); 01382 01383 if (isa<Instruction>(ptrValue)) 01384 if (!currExps.test(VN.lookup(ptrValue))) { 01385 currExps.insert(ptrValue); 01386 currExps.set(VN.lookup(ptrValue)); 01387 } 01388 01389 for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end(); 01390 OI != OE; ++OI) 01391 if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) { 01392 currExps.insert(*OI); 01393 currExps.set(VN.lookup(*OI)); 01394 } 01395 01396 if (!currExps.test(VN.lookup(U))) { 01397 currExps.insert(U); 01398 currExps.set(num); 01399 } 01400 01401 // Handle opaque ops 01402 } else if (!I->isTerminator()){ 01403 VN.lookup_or_add(I); 01404 01405 currTemps.insert(I); 01406 } 01407 01408 if (!I->isTerminator()) 01409 if (!currAvail.test(VN.lookup(I))) { 01410 currAvail.insert(I); 01411 currAvail.set(VN.lookup(I)); 01412 } 01413 } 01414 01415 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT 01416 /// set as a function of the ANTIC_IN set of the block's predecessors 01417 bool GVNPRE::buildsets_anticout(BasicBlock* BB, 01418 ValueNumberedSet& anticOut, 01419 SmallPtrSet<BasicBlock*, 8>& visited) { 01420 if (BB->getTerminator()->getNumSuccessors() == 1) { 01421 if (BB->getTerminator()->getSuccessor(0) != BB && 01422 visited.count(BB->getTerminator()->getSuccessor(0)) == 0) { 01423 return true; 01424 } 01425 else { 01426 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)], 01427 BB, BB->getTerminator()->getSuccessor(0), anticOut); 01428 } 01429 } else if (BB->getTerminator()->getNumSuccessors() > 1) { 01430 BasicBlock* first = BB->getTerminator()->getSuccessor(0); 01431 for (ValueNumberedSet::iterator I = anticipatedIn[first].begin(), 01432 E = anticipatedIn[first].end(); I != E; ++I) { 01433 anticOut.insert(*I); 01434 anticOut.set(VN.lookup(*I)); 01435 } 01436 01437 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) { 01438 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i); 01439 ValueNumberedSet& succAnticIn = anticipatedIn[currSucc]; 01440 01441 SmallVector<Value*, 16> temp; 01442 01443 for (ValueNumberedSet::iterator I = anticOut.begin(), 01444 E = anticOut.end(); I != E; ++I) 01445 if (!succAnticIn.test(VN.lookup(*I))) 01446 temp.push_back(*I); 01447 01448 for (SmallVector<Value*, 16>::iterator I = temp.begin(), E = temp.end(); 01449 I != E; ++I) { 01450 anticOut.erase(*I); 01451 anticOut.reset(VN.lookup(*I)); 01452 } 01453 } 01454 } 01455 01456 return false; 01457 } 01458 01459 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for 01460 /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN 01461 /// sets populated in buildsets_availout 01462 unsigned GVNPRE::buildsets_anticin(BasicBlock* BB, 01463 ValueNumberedSet& anticOut, 01464 ValueNumberedSet& currExps, 01465 SmallPtrSet<Value*, 16>& currTemps, 01466 SmallPtrSet<BasicBlock*, 8>& visited) { 01467 ValueNumberedSet& anticIn = anticipatedIn[BB]; 01468 unsigned old = anticIn.size(); 01469 01470 bool defer = buildsets_anticout(BB, anticOut, visited); 01471 if (defer) 01472 return 0; 01473 01474 anticIn.clear(); 01475 01476 for (ValueNumberedSet::iterator I = anticOut.begin(), 01477 E = anticOut.end(); I != E; ++I) { 01478 anticIn.insert(*I); 01479 anticIn.set(VN.lookup(*I)); 01480 } 01481 for (ValueNumberedSet::iterator I = currExps.begin(), 01482 E = currExps.end(); I != E; ++I) { 01483 if (!anticIn.test(VN.lookup(*I))) { 01484 anticIn.insert(*I); 01485 anticIn.set(VN.lookup(*I)); 01486 } 01487 } 01488 01489 for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(), 01490 E = currTemps.end(); I != E; ++I) { 01491 anticIn.erase(*I); 01492 anticIn.reset(VN.lookup(*I)); 01493 } 01494 01495 clean(anticIn); 01496 anticOut.clear(); 01497 01498 if (old != anticIn.size()) 01499 return 2; 01500 else 01501 return 1; 01502 } 01503 01504 /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT 01505 /// and the ANTIC_IN sets. 01506 void GVNPRE::buildsets(Function& F) { 01507 DenseMap<BasicBlock*, ValueNumberedSet> generatedExpressions; 01508 DenseMap<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries; 01509 01510 DominatorTree &DT = getAnalysis<DominatorTree>(); 01511 01512 // Phase 1, Part 1: calculate AVAIL_OUT 01513 01514 // Top-down walk of the dominator tree 01515 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()), 01516 E = df_end(DT.getRootNode()); DI != E; ++DI) { 01517 01518 // Get the sets to update for this block 01519 ValueNumberedSet& currExps = generatedExpressions[DI->getBlock()]; 01520 ValueNumberedSet& currPhis = generatedPhis[DI->getBlock()]; 01521 SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()]; 01522 ValueNumberedSet& currAvail = availableOut[DI->getBlock()]; 01523 01524 BasicBlock* BB = DI->getBlock(); 01525 01526 // A block inherits AVAIL_OUT from its dominator 01527 if (DI->getIDom() != 0) 01528 currAvail = availableOut[DI->getIDom()->getBlock()]; 01529 01530 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); 01531 BI != BE; ++BI) 01532 buildsets_availout(BI, currAvail, currPhis, currExps, 01533 currTemps); 01534 01535 } 01536 01537 // Phase 1, Part 2: calculate ANTIC_IN 01538 01539 SmallPtrSet<BasicBlock*, 8> visited; 01540 SmallPtrSet<BasicBlock*, 4> block_changed; 01541 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) 01542 block_changed.insert(FI); 01543 01544 bool changed = true; 01545 unsigned iterations = 0; 01546 01547 while (changed) { 01548 changed = false; 01549 ValueNumberedSet anticOut; 01550 01551 // Postorder walk of the CFG 01552 for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()), 01553 BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) { 01554 BasicBlock* BB = *BBI; 01555 01556 if (block_changed.count(BB) != 0) { 01557 unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB], 01558 generatedTemporaries[BB], visited); 01559 01560 if (ret == 0) { 01561 changed = true; 01562 continue; 01563 } else { 01564 visited.insert(BB); 01565 01566 if (ret == 2) 01567 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 01568 PI != PE; ++PI) { 01569 block_changed.insert(*PI); 01570 } 01571 else 01572 block_changed.erase(BB); 01573 01574 changed |= (ret == 2); 01575 } 01576 } 01577 } 01578 01579 iterations++; 01580 } 01581 } 01582 01583 /// insertion_pre - When a partial redundancy has been identified, eliminate it 01584 /// by inserting appropriate values into the predecessors and a phi node in 01585 /// the main block 01586 void GVNPRE::insertion_pre(Value* e, BasicBlock* BB, 01587 DenseMap<BasicBlock*, Value*>& avail, 01588 std::map<BasicBlock*, ValueNumberedSet>& new_sets) { 01589 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { 01590 Value* e2 = avail[*PI]; 01591 if (!availableOut[*PI].test(VN.lookup(e2))) { 01592 User* U = cast<User>(e2); 01593 01594 Value* s1 = 0; 01595 if (isa<BinaryOperator>(U->getOperand(0)) || 01596 isa<CmpInst>(U->getOperand(0)) || 01597 isa<ShuffleVectorInst>(U->getOperand(0)) || 01598 isa<ExtractElementInst>(U->getOperand(0)) || 01599 isa<InsertElementInst>(U->getOperand(0)) || 01600 isa<SelectInst>(U->getOperand(0)) || 01601 isa<CastInst>(U->getOperand(0)) || 01602 isa<GetElementPtrInst>(U->getOperand(0))) 01603 s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0))); 01604 else 01605 s1 = U->getOperand(0); 01606 01607 Value* s2 = 0; 01608 01609 if (isa<BinaryOperator>(U) || 01610 isa<CmpInst>(U) || 01611 isa<ShuffleVectorInst>(U) || 01612 isa<ExtractElementInst>(U) || 01613 isa<InsertElementInst>(U) || 01614 isa<SelectInst>(U)) { 01615 if (isa<BinaryOperator>(U->getOperand(1)) || 01616 isa<CmpInst>(U->getOperand(1)) || 01617 isa<ShuffleVectorInst>(U->getOperand(1)) || 01618 isa<ExtractElementInst>(U->getOperand(1)) || 01619 isa<InsertElementInst>(U->getOperand(1)) || 01620 isa<SelectInst>(U->getOperand(1)) || 01621 isa<CastInst>(U->getOperand(1)) || 01622 isa<GetElementPtrInst>(U->getOperand(1))) { 01623 s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1))); 01624 } else { 01625 s2 = U->getOperand(1); 01626 } 01627 } 01628 01629 // Ternary Operators 01630 Value* s3 = 0; 01631 if (isa<ShuffleVectorInst>(U) || 01632 isa<InsertElementInst>(U) || 01633 isa<SelectInst>(U)) { 01634 if (isa<BinaryOperator>(U->getOperand(2)) || 01635 isa<CmpInst>(U->getOperand(2)) || 01636 isa<ShuffleVectorInst>(U->getOperand(2)) || 01637 isa<ExtractElementInst>(U->getOperand(2)) || 01638 isa<InsertElementInst>(U->getOperand(2)) || 01639 isa<SelectInst>(U->getOperand(2)) || 01640 isa<CastInst>(U->getOperand(2)) || 01641 isa<GetElementPtrInst>(U->getOperand(2))) { 01642 s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2))); 01643 } else { 01644 s3 = U->getOperand(2); 01645 } 01646 } 01647 01648 // Vararg operators 01649 SmallVector<Value*, 4> sVarargs; 01650 if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) { 01651 for (GetElementPtrInst::op_iterator OI = G->idx_begin(), 01652 OE = G->idx_end(); OI != OE; ++OI) { 01653 if (isa<BinaryOperator>(*OI) || 01654 isa<CmpInst>(*OI) || 01655 isa<ShuffleVectorInst>(*OI) || 01656 isa<ExtractElementInst>(*OI) || 01657 isa<InsertElementInst>(*OI) || 01658 isa<SelectInst>(*OI) || 01659 isa<CastInst>(*OI) || 01660 isa<GetElementPtrInst>(*OI)) { 01661 sVarargs.push_back(find_leader(availableOut[*PI], 01662 VN.lookup(*OI))); 01663 } else { 01664 sVarargs.push_back(*OI); 01665 } 01666 } 01667 } 01668 01669 Value* newVal = 0; 01670 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U)) 01671 newVal = BinaryOperator::Create(BO->getOpcode(), s1, s2, 01672 BO->getName()+".gvnpre", 01673 (*PI)->getTerminator()); 01674 else if (CmpInst* C = dyn_cast<CmpInst>(U)) 01675 newVal = CmpInst::Create(C->getOpcode(), C->getPredicate(), s1, s2, 01676 C->getName()+".gvnpre", 01677 (*PI)->getTerminator()); 01678 else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U)) 01679 newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre", 01680 (*PI)->getTerminator()); 01681 else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U)) 01682 newVal = InsertElementInst::Create(s1, s2, s3, S->getName()+".gvnpre", 01683 (*PI)->getTerminator()); 01684 else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U)) 01685 newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre", 01686 (*PI)->getTerminator()); 01687 else if (SelectInst* S = dyn_cast<SelectInst>(U)) 01688 newVal = SelectInst::Create(s1, s2, s3, S->getName()+".gvnpre", 01689 (*PI)->getTerminator()); 01690 else if (CastInst* C = dyn_cast<CastInst>(U)) 01691 newVal = CastInst::Create(C->getOpcode(), s1, C->getType(), 01692 C->getName()+".gvnpre", 01693 (*PI)->getTerminator()); 01694 else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) 01695 newVal = GetElementPtrInst::Create(s1, sVarargs.begin(), sVarargs.end(), 01696 G->getName()+".gvnpre", 01697 (*PI)->getTerminator()); 01698 01699 VN.add(newVal, VN.lookup(U)); 01700 01701 ValueNumberedSet& predAvail = availableOut[*PI]; 01702 val_replace(predAvail, newVal); 01703 val_replace(new_sets[*PI], newVal); 01704 predAvail.set(VN.lookup(newVal)); 01705 01706 DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI); 01707 if (av != avail.end()) 01708 avail.erase(av); 01709 avail.insert(std::make_pair(*PI, newVal)); 01710 01711 ++NumInsertedVals; 01712 } 01713 } 01714 01715 PHINode* p = 0; 01716 01717 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { 01718 if (p == 0) 01719 p = PHINode::Create(avail[*PI]->getType(), "gvnpre-join", BB->begin()); 01720 01721 p->addIncoming(avail[*PI], *PI); 01722 } 01723 01724 VN.add(p, VN.lookup(e)); 01725 val_replace(availableOut[BB], p); 01726 availableOut[BB].set(VN.lookup(e)); 01727 generatedPhis[BB].insert(p); 01728 generatedPhis[BB].set(VN.lookup(e)); 01729 new_sets[BB].insert(p); 01730 new_sets[BB].set(VN.lookup(e)); 01731 01732 ++NumInsertedPhis; 01733 } 01734 01735 /// insertion_mergepoint - When walking the dom tree, check at each merge 01736 /// block for the possibility of a partial redundancy. If present, eliminate it 01737 unsigned GVNPRE::insertion_mergepoint(SmallVector<Value*, 8>& workList, 01738 df_iterator<DomTreeNode*>& D, 01739 std::map<BasicBlock*, ValueNumberedSet >& new_sets) { 01740 bool changed_function = false; 01741 bool new_stuff = false; 01742 01743 BasicBlock* BB = D->getBlock(); 01744 for (unsigned i = 0; i < workList.size(); ++i) { 01745 Value* e = workList[i]; 01746 01747 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) || 01748 isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) || 01749 isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) || 01750 isa<GetElementPtrInst>(e)) { 01751 if (availableOut[D->getIDom()->getBlock()].test(VN.lookup(e))) 01752 continue; 01753 01754 DenseMap<BasicBlock*, Value*> avail; 01755 bool by_some = false; 01756 bool all_same = true; 01757 Value * first_s = 0; 01758 01759 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; 01760 ++PI) { 01761 Value *e2 = phi_translate(e, *PI, BB); 01762 Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2)); 01763 01764 if (e3 == 0) { 01765 DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI); 01766 if (av != avail.end()) 01767 avail.erase(av); 01768 avail.insert(std::make_pair(*PI, e2)); 01769 all_same = false; 01770 } else { 01771 DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI); 01772 if (av != avail.end()) 01773 avail.erase(av); 01774 avail.insert(std::make_pair(*PI, e3)); 01775 01776 by_some = true; 01777 if (first_s == 0) 01778 first_s = e3; 01779 else if (first_s != e3) 01780 all_same = false; 01781 } 01782 } 01783 01784 if (by_some && !all_same && 01785 !generatedPhis[BB].test(VN.lookup(e))) { 01786 insertion_pre(e, BB, avail, new_sets); 01787 01788 changed_function = true; 01789 new_stuff = true; 01790 } 01791 } 01792 } 01793 01794 unsigned retval = 0; 01795 if (changed_function) 01796 retval += 1; 01797 if (new_stuff) 01798 retval += 2; 01799 01800 return retval; 01801 } 01802 01803 /// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for 01804 /// merge points. When one is found, check for a partial redundancy. If one is 01805 /// present, eliminate it. Repeat this walk until no changes are made. 01806 bool GVNPRE::insertion(Function& F) { 01807 bool changed_function = false; 01808 01809 DominatorTree &DT = getAnalysis<DominatorTree>(); 01810 01811 std::map<BasicBlock*, ValueNumberedSet> new_sets; 01812 bool new_stuff = true; 01813 while (new_stuff) { 01814 new_stuff = false; 01815 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()), 01816 E = df_end(DT.getRootNode()); DI != E; ++DI) { 01817 BasicBlock* BB = DI->getBlock(); 01818 01819 if (BB == 0) 01820 continue; 01821 01822 ValueNumberedSet& availOut = availableOut[BB]; 01823 ValueNumberedSet& anticIn = anticipatedIn[BB]; 01824 01825 // Replace leaders with leaders inherited from dominator 01826 if (DI->getIDom() != 0) { 01827 ValueNumberedSet& dom_set = new_sets[DI->getIDom()->getBlock()]; 01828 for (ValueNumberedSet::iterator I = dom_set.begin(), 01829 E = dom_set.end(); I != E; ++I) { 01830 val_replace(new_sets[BB], *I); 01831 val_replace(availOut, *I); 01832 } 01833 } 01834 01835 // If there is more than one predecessor... 01836 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) { 01837 SmallVector<Value*, 8> workList; 01838 workList.reserve(anticIn.size()); 01839 topo_sort(anticIn, workList); 01840 01841 unsigned result = insertion_mergepoint(workList, DI, new_sets); 01842 if (result & 1) 01843 changed_function = true; 01844 if (result & 2) 01845 new_stuff = true; 01846 } 01847 } 01848 } 01849 01850 return changed_function; 01851 } 01852 01853 // GVNPRE::runOnFunction - This is the main transformation entry point for a 01854 // function. 01855 // 01856 bool GVNPRE::runOnFunction(Function &F) { 01857 // Clean out global sets from any previous functions 01858 VN.clear(); 01859 createdExpressions.clear(); 01860 availableOut.clear(); 01861 anticipatedIn.clear(); 01862 generatedPhis.clear(); 01863 01864 bool changed_function = false; 01865 01866 // Phase 1: BuildSets 01867 // This phase calculates the AVAIL_OUT and ANTIC_IN sets 01868 buildsets(F); 01869 01870 // Phase 2: Insert 01871 // This phase inserts values to make partially redundant values 01872 // fully redundant 01873 changed_function |= insertion(F); 01874 01875 // Phase 3: Eliminate 01876 // This phase performs trivial full redundancy elimination 01877 changed_function |= elimination(); 01878 01879 // Phase 4: Cleanup 01880 // This phase cleans up values that were created solely 01881 // as leaders for expressions 01882 cleanup(); 01883 01884 return changed_function; 01885 }