diff options
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/Scalar/GVNPRE.cpp | 1892 | ||||
-rw-r--r-- | lib/Transforms/Scalar/PredicateSimplifier.cpp | 2721 | ||||
-rw-r--r-- | lib/Transforms/Utils/CloneTrace.cpp | 119 | ||||
-rw-r--r-- | lib/Transforms/Utils/InlineCost.cpp | 315 |
4 files changed, 0 insertions, 5047 deletions
diff --git a/lib/Transforms/Scalar/GVNPRE.cpp b/lib/Transforms/Scalar/GVNPRE.cpp deleted file mode 100644 index 0f3153f..0000000 --- a/lib/Transforms/Scalar/GVNPRE.cpp +++ /dev/null @@ -1,1892 +0,0 @@ -//===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This pass performs a hybrid of global value numbering and partial redundancy -// elimination, known as GVN-PRE. It performs partial redundancy elimination on -// values, rather than lexical expressions, allowing a more comprehensive view -// the optimization. It replaces redundant values with uses of earlier -// occurences of the same value. While this is beneficial in that it eliminates -// unneeded computation, it also increases register pressure by creating large -// live ranges, and should be used with caution on platforms that are very -// sensitive to register pressure. -// -// Note that this pass does the value numbering itself, it does not use the -// ValueNumbering analysis passes. -// -//===----------------------------------------------------------------------===// - -#define DEBUG_TYPE "gvnpre" -#include "llvm/Value.h" -#include "llvm/Transforms/Scalar.h" -#include "llvm/Instructions.h" -#include "llvm/Function.h" -#include "llvm/DerivedTypes.h" -#include "llvm/Analysis/Dominators.h" -#include "llvm/ADT/BitVector.h" -#include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/DepthFirstIterator.h" -#include "llvm/ADT/PostOrderIterator.h" -#include "llvm/ADT/SmallPtrSet.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/Statistic.h" -#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" -#include "llvm/Support/CFG.h" -#include "llvm/Support/Compiler.h" -#include "llvm/Support/Debug.h" -#include <algorithm> -#include <deque> -#include <map> -using namespace llvm; - -//===----------------------------------------------------------------------===// -// ValueTable Class -//===----------------------------------------------------------------------===// - -namespace { - -/// This class holds the mapping between values and value numbers. It is used -/// as an efficient mechanism to determine the expression-wise equivalence of -/// two values. - -struct Expression { - enum ExpressionOpcode { ADD, FADD, SUB, FSUB, MUL, FMUL, - UDIV, SDIV, FDIV, UREM, SREM, - FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ, - ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE, - ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ, - FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE, - FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE, - FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT, - SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI, - FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT, - PTRTOINT, INTTOPTR, BITCAST, GEP, EMPTY, - TOMBSTONE }; - - ExpressionOpcode opcode; - const Type* type; - uint32_t firstVN; - uint32_t secondVN; - uint32_t thirdVN; - SmallVector<uint32_t, 4> varargs; - - Expression() { } - explicit Expression(ExpressionOpcode o) : opcode(o) { } - - bool operator==(const Expression &other) const { - if (opcode != other.opcode) - return false; - else if (opcode == EMPTY || opcode == TOMBSTONE) - return true; - else if (type != other.type) - return false; - else if (firstVN != other.firstVN) - return false; - else if (secondVN != other.secondVN) - return false; - else if (thirdVN != other.thirdVN) - return false; - else { - if (varargs.size() != other.varargs.size()) - return false; - - for (size_t i = 0; i < varargs.size(); ++i) - if (varargs[i] != other.varargs[i]) - return false; - - return true; - } - } - - bool operator!=(const Expression &other) const { - if (opcode != other.opcode) - return true; - else if (opcode == EMPTY || opcode == TOMBSTONE) - return false; - else if (type != other.type) - return true; - else if (firstVN != other.firstVN) - return true; - else if (secondVN != other.secondVN) - return true; - else if (thirdVN != other.thirdVN) - return true; - else { - if (varargs.size() != other.varargs.size()) - return true; - - for (size_t i = 0; i < varargs.size(); ++i) - if (varargs[i] != other.varargs[i]) - return true; - - return false; - } - } -}; - -} - -namespace { - class VISIBILITY_HIDDEN ValueTable { - private: - DenseMap<Value*, uint32_t> valueNumbering; - DenseMap<Expression, uint32_t> expressionNumbering; - - uint32_t nextValueNumber; - - Expression::ExpressionOpcode getOpcode(BinaryOperator* BO); - Expression::ExpressionOpcode getOpcode(CmpInst* C); - Expression::ExpressionOpcode getOpcode(CastInst* C); - Expression create_expression(BinaryOperator* BO); - Expression create_expression(CmpInst* C); - Expression create_expression(ShuffleVectorInst* V); - Expression create_expression(ExtractElementInst* C); - Expression create_expression(InsertElementInst* V); - Expression create_expression(SelectInst* V); - Expression create_expression(CastInst* C); - Expression create_expression(GetElementPtrInst* G); - public: - ValueTable() { nextValueNumber = 1; } - uint32_t lookup_or_add(Value* V); - uint32_t lookup(Value* V) const; - void add(Value* V, uint32_t num); - void clear(); - void erase(Value* v); - unsigned size(); - }; -} - -namespace llvm { -template <> struct DenseMapInfo<Expression> { - static inline Expression getEmptyKey() { - return Expression(Expression::EMPTY); - } - - static inline Expression getTombstoneKey() { - return Expression(Expression::TOMBSTONE); - } - - static unsigned getHashValue(const Expression e) { - unsigned hash = e.opcode; - - hash = e.firstVN + hash * 37; - hash = e.secondVN + hash * 37; - hash = e.thirdVN + hash * 37; - - hash = ((unsigned)((uintptr_t)e.type >> 4) ^ - (unsigned)((uintptr_t)e.type >> 9)) + - hash * 37; - - for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(), - E = e.varargs.end(); I != E; ++I) - hash = *I + hash * 37; - - return hash; - } - static bool isEqual(const Expression &LHS, const Expression &RHS) { - return LHS == RHS; - } - static bool isPod() { return true; } -}; -} - -//===----------------------------------------------------------------------===// -// ValueTable Internal Functions -//===----------------------------------------------------------------------===// -Expression::ExpressionOpcode - ValueTable::getOpcode(BinaryOperator* BO) { - switch(BO->getOpcode()) { - case Instruction::Add: - return Expression::ADD; - case Instruction::FAdd: - return Expression::FADD; - case Instruction::Sub: - return Expression::SUB; - case Instruction::FSub: - return Expression::FSUB; - case Instruction::Mul: - return Expression::MUL; - case Instruction::FMul: - return Expression::FMUL; - case Instruction::UDiv: - return Expression::UDIV; - case Instruction::SDiv: - return Expression::SDIV; - case Instruction::FDiv: - return Expression::FDIV; - case Instruction::URem: - return Expression::UREM; - case Instruction::SRem: - return Expression::SREM; - case Instruction::FRem: - return Expression::FREM; - case Instruction::Shl: - return Expression::SHL; - case Instruction::LShr: - return Expression::LSHR; - case Instruction::AShr: - return Expression::ASHR; - case Instruction::And: - return Expression::AND; - case Instruction::Or: - return Expression::OR; - case Instruction::Xor: - return Expression::XOR; - - // THIS SHOULD NEVER HAPPEN - default: - assert(0 && "Binary operator with unknown opcode?"); - return Expression::ADD; - } -} - -Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) { - if (C->getOpcode() == Instruction::ICmp) { - switch (C->getPredicate()) { - case ICmpInst::ICMP_EQ: - return Expression::ICMPEQ; - case ICmpInst::ICMP_NE: - return Expression::ICMPNE; - case ICmpInst::ICMP_UGT: - return Expression::ICMPUGT; - case ICmpInst::ICMP_UGE: - return Expression::ICMPUGE; - case ICmpInst::ICMP_ULT: - return Expression::ICMPULT; - case ICmpInst::ICMP_ULE: - return Expression::ICMPULE; - case ICmpInst::ICMP_SGT: - return Expression::ICMPSGT; - case ICmpInst::ICMP_SGE: - return Expression::ICMPSGE; - case ICmpInst::ICMP_SLT: - return Expression::ICMPSLT; - case ICmpInst::ICMP_SLE: - return Expression::ICMPSLE; - - // THIS SHOULD NEVER HAPPEN - default: - assert(0 && "Comparison with unknown predicate?"); - return Expression::ICMPEQ; - } - } else { - switch (C->getPredicate()) { - case FCmpInst::FCMP_OEQ: - return Expression::FCMPOEQ; - case FCmpInst::FCMP_OGT: - return Expression::FCMPOGT; - case FCmpInst::FCMP_OGE: - return Expression::FCMPOGE; - case FCmpInst::FCMP_OLT: - return Expression::FCMPOLT; - case FCmpInst::FCMP_OLE: - return Expression::FCMPOLE; - case FCmpInst::FCMP_ONE: - return Expression::FCMPONE; - case FCmpInst::FCMP_ORD: - return Expression::FCMPORD; - case FCmpInst::FCMP_UNO: - return Expression::FCMPUNO; - case FCmpInst::FCMP_UEQ: - return Expression::FCMPUEQ; - case FCmpInst::FCMP_UGT: - return Expression::FCMPUGT; - case FCmpInst::FCMP_UGE: - return Expression::FCMPUGE; - case FCmpInst::FCMP_ULT: - return Expression::FCMPULT; - case FCmpInst::FCMP_ULE: - return Expression::FCMPULE; - case FCmpInst::FCMP_UNE: - return Expression::FCMPUNE; - - // THIS SHOULD NEVER HAPPEN - default: - assert(0 && "Comparison with unknown predicate?"); - return Expression::FCMPOEQ; - } - } -} - -Expression::ExpressionOpcode - ValueTable::getOpcode(CastInst* C) { - switch(C->getOpcode()) { - case Instruction::Trunc: - return Expression::TRUNC; - case Instruction::ZExt: - return Expression::ZEXT; - case Instruction::SExt: - return Expression::SEXT; - case Instruction::FPToUI: - return Expression::FPTOUI; - case Instruction::FPToSI: - return Expression::FPTOSI; - case Instruction::UIToFP: - return Expression::UITOFP; - case Instruction::SIToFP: - return Expression::SITOFP; - case Instruction::FPTrunc: - return Expression::FPTRUNC; - case Instruction::FPExt: - return Expression::FPEXT; - case Instruction::PtrToInt: - return Expression::PTRTOINT; - case Instruction::IntToPtr: - return Expression::INTTOPTR; - case Instruction::BitCast: - return Expression::BITCAST; - - // THIS SHOULD NEVER HAPPEN - default: - assert(0 && "Cast operator with unknown opcode?"); - return Expression::BITCAST; - } -} - -Expression ValueTable::create_expression(BinaryOperator* BO) { - Expression e; - - e.firstVN = lookup_or_add(BO->getOperand(0)); - e.secondVN = lookup_or_add(BO->getOperand(1)); - e.thirdVN = 0; - e.type = BO->getType(); - e.opcode = getOpcode(BO); - - return e; -} - -Expression ValueTable::create_expression(CmpInst* C) { - Expression e; - - e.firstVN = lookup_or_add(C->getOperand(0)); - e.secondVN = lookup_or_add(C->getOperand(1)); - e.thirdVN = 0; - e.type = C->getType(); - e.opcode = getOpcode(C); - - return e; -} - -Expression ValueTable::create_expression(CastInst* C) { - Expression e; - - e.firstVN = lookup_or_add(C->getOperand(0)); - e.secondVN = 0; - e.thirdVN = 0; - e.type = C->getType(); - e.opcode = getOpcode(C); - - return e; -} - -Expression ValueTable::create_expression(ShuffleVectorInst* S) { - Expression e; - - e.firstVN = lookup_or_add(S->getOperand(0)); - e.secondVN = lookup_or_add(S->getOperand(1)); - e.thirdVN = lookup_or_add(S->getOperand(2)); - e.type = S->getType(); - e.opcode = Expression::SHUFFLE; - - return e; -} - -Expression ValueTable::create_expression(ExtractElementInst* E) { - Expression e; - - e.firstVN = lookup_or_add(E->getOperand(0)); - e.secondVN = lookup_or_add(E->getOperand(1)); - e.thirdVN = 0; - e.type = E->getType(); - e.opcode = Expression::EXTRACT; - - return e; -} - -Expression ValueTable::create_expression(InsertElementInst* I) { - Expression e; - - e.firstVN = lookup_or_add(I->getOperand(0)); - e.secondVN = lookup_or_add(I->getOperand(1)); - e.thirdVN = lookup_or_add(I->getOperand(2)); - e.type = I->getType(); - e.opcode = Expression::INSERT; - - return e; -} - -Expression ValueTable::create_expression(SelectInst* I) { - Expression e; - - e.firstVN = lookup_or_add(I->getCondition()); - e.secondVN = lookup_or_add(I->getTrueValue()); - e.thirdVN = lookup_or_add(I->getFalseValue()); - e.type = I->getType(); - e.opcode = Expression::SELECT; - - return e; -} - -Expression ValueTable::create_expression(GetElementPtrInst* G) { - Expression e; - - e.firstVN = lookup_or_add(G->getPointerOperand()); - e.secondVN = 0; - e.thirdVN = 0; - e.type = G->getType(); - e.opcode = Expression::GEP; - - for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end(); - I != E; ++I) - e.varargs.push_back(lookup_or_add(*I)); - - return e; -} - -//===----------------------------------------------------------------------===// -// ValueTable External Functions -//===----------------------------------------------------------------------===// - -/// lookup_or_add - Returns the value number for the specified value, assigning -/// it a new number if it did not have one before. -uint32_t ValueTable::lookup_or_add(Value* V) { - DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); - if (VI != valueNumbering.end()) - return VI->second; - - - if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) { - Expression e = create_expression(BO); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else if (CmpInst* C = dyn_cast<CmpInst>(V)) { - Expression e = create_expression(C); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) { - Expression e = create_expression(U); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) { - Expression e = create_expression(U); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) { - Expression e = create_expression(U); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else if (SelectInst* U = dyn_cast<SelectInst>(V)) { - Expression e = create_expression(U); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else if (CastInst* U = dyn_cast<CastInst>(V)) { - Expression e = create_expression(U); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) { - Expression e = create_expression(U); - - DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e); - if (EI != expressionNumbering.end()) { - valueNumbering.insert(std::make_pair(V, EI->second)); - return EI->second; - } else { - expressionNumbering.insert(std::make_pair(e, nextValueNumber)); - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - - return nextValueNumber++; - } - } else { - valueNumbering.insert(std::make_pair(V, nextValueNumber)); - return nextValueNumber++; - } -} - -/// lookup - Returns the value number of the specified value. Fails if -/// the value has not yet been numbered. -uint32_t ValueTable::lookup(Value* V) const { - DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); - if (VI != valueNumbering.end()) - return VI->second; - else - assert(0 && "Value not numbered?"); - - return 0; -} - -/// add - Add the specified value with the given value number, removing -/// its old number, if any -void ValueTable::add(Value* V, uint32_t num) { - DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); - if (VI != valueNumbering.end()) - valueNumbering.erase(VI); - valueNumbering.insert(std::make_pair(V, num)); -} - -/// clear - Remove all entries from the ValueTable -void ValueTable::clear() { - valueNumbering.clear(); - expressionNumbering.clear(); - nextValueNumber = 1; -} - -/// erase - Remove a value from the value numbering -void ValueTable::erase(Value* V) { - valueNumbering.erase(V); -} - -/// size - Return the number of assigned value numbers -unsigned ValueTable::size() { - // NOTE: zero is never assigned - return nextValueNumber; -} - -namespace { - -//===----------------------------------------------------------------------===// -// ValueNumberedSet Class -//===----------------------------------------------------------------------===// - -class ValueNumberedSet { - private: - SmallPtrSet<Value*, 8> contents; - BitVector numbers; - public: - ValueNumberedSet() { numbers.resize(1); } - ValueNumberedSet(const ValueNumberedSet& other) { - numbers = other.numbers; - contents = other.contents; - } - - typedef SmallPtrSet<Value*, 8>::iterator iterator; - - iterator begin() { return contents.begin(); } - iterator end() { return contents.end(); } - - bool insert(Value* v) { return contents.insert(v); } - void insert(iterator I, iterator E) { contents.insert(I, E); } - void erase(Value* v) { contents.erase(v); } - unsigned count(Value* v) { return contents.count(v); } - size_t size() { return contents.size(); } - - void set(unsigned i) { - if (i >= numbers.size()) - numbers.resize(i+1); - - numbers.set(i); - } - - void operator=(const ValueNumberedSet& other) { - contents = other.contents; - numbers = other.numbers; - } - - void reset(unsigned i) { - if (i < numbers.size()) - numbers.reset(i); - } - - bool test(unsigned i) { - if (i >= numbers.size()) - return false; - - return numbers.test(i); - } - - void clear() { - contents.clear(); - numbers.clear(); - } -}; - -} - -//===----------------------------------------------------------------------===// -// GVNPRE Pass -//===----------------------------------------------------------------------===// - -namespace { - - class VISIBILITY_HIDDEN GVNPRE : public FunctionPass { - bool runOnFunction(Function &F); - public: - static char ID; // Pass identification, replacement for typeid - GVNPRE() : FunctionPass(&ID) {} - - private: - ValueTable VN; - SmallVector<Instruction*, 8> createdExpressions; - - DenseMap<BasicBlock*, ValueNumberedSet> availableOut; - DenseMap<BasicBlock*, ValueNumberedSet> anticipatedIn; - DenseMap<BasicBlock*, ValueNumberedSet> generatedPhis; - - // This transformation requires dominator postdominator info - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesCFG(); - AU.addRequiredID(BreakCriticalEdgesID); - AU.addRequired<UnifyFunctionExitNodes>(); - AU.addRequired<DominatorTree>(); - } - - // Helper fuctions - // FIXME: eliminate or document these better - void dump(ValueNumberedSet& s) const ; - void clean(ValueNumberedSet& set) ; - Value* find_leader(ValueNumberedSet& vals, uint32_t v) ; - Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) ; - void phi_translate_set(ValueNumberedSet& anticIn, BasicBlock* pred, - BasicBlock* succ, ValueNumberedSet& out) ; - - void topo_sort(ValueNumberedSet& set, - SmallVector<Value*, 8>& vec) ; - - void cleanup() ; - bool elimination() ; - - void val_insert(ValueNumberedSet& s, Value* v) ; - void val_replace(ValueNumberedSet& s, Value* v) ; - bool dependsOnInvoke(Value* V) ; - void buildsets_availout(BasicBlock::iterator I, - ValueNumberedSet& currAvail, - ValueNumberedSet& currPhis, - ValueNumberedSet& currExps, - SmallPtrSet<Value*, 16>& currTemps); - bool buildsets_anticout(BasicBlock* BB, - ValueNumberedSet& anticOut, - SmallPtrSet<BasicBlock*, 8>& visited); - unsigned buildsets_anticin(BasicBlock* BB, - ValueNumberedSet& anticOut, - ValueNumberedSet& currExps, - SmallPtrSet<Value*, 16>& currTemps, - SmallPtrSet<BasicBlock*, 8>& visited); - void buildsets(Function& F) ; - - void insertion_pre(Value* e, BasicBlock* BB, - DenseMap<BasicBlock*, Value*>& avail, - std::map<BasicBlock*,ValueNumberedSet>& new_set); - unsigned insertion_mergepoint(SmallVector<Value*, 8>& workList, - df_iterator<DomTreeNode*>& D, - std::map<BasicBlock*, ValueNumberedSet>& new_set); - bool insertion(Function& F) ; - - }; - - char GVNPRE::ID = 0; - -} - -// createGVNPREPass - The public interface to this file... -FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); } - -static RegisterPass<GVNPRE> X("gvnpre", - "Global Value Numbering/Partial Redundancy Elimination"); - - -STATISTIC(NumInsertedVals, "Number of values inserted"); -STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted"); -STATISTIC(NumEliminated, "Number of redundant instructions eliminated"); - -/// find_leader - Given a set and a value number, return the first -/// element of the set with that value number, or 0 if no such element -/// is present -Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) { - if (!vals.test(v)) - return 0; - - for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end(); - I != E; ++I) - if (v == VN.lookup(*I)) - return *I; - - assert(0 && "No leader found, but present bit is set?"); - return 0; -} - -/// val_insert - Insert a value into a set only if there is not a value -/// with the same value number already in the set -void GVNPRE::val_insert(ValueNumberedSet& s, Value* v) { - uint32_t num = VN.lookup(v); - if (!s.test(num)) - s.insert(v); -} - -/// val_replace - Insert a value into a set, replacing any values already in -/// the set that have the same value number -void GVNPRE::val_replace(ValueNumberedSet& s, Value* v) { - if (s.count(v)) return; - - uint32_t num = VN.lookup(v); - Value* leader = find_leader(s, num); - if (leader != 0) - s.erase(leader); - s.insert(v); - s.set(num); -} - -/// phi_translate - Given a value, its parent block, and a predecessor of its -/// parent, translate the value into legal for the predecessor block. This -/// means translating its operands (and recursively, their operands) through -/// any phi nodes in the parent into values available in the predecessor -Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) { - if (V == 0) - return 0; - - // Unary Operations - if (CastInst* U = dyn_cast<CastInst>(V)) { - Value* newOp1 = 0; - if (isa<Instruction>(U->getOperand(0))) - newOp1 = phi_translate(U->getOperand(0), pred, succ); - else - newOp1 = U->getOperand(0); - - if (newOp1 == 0) - return 0; - - if (newOp1 != U->getOperand(0)) { - Instruction* newVal = 0; - if (CastInst* C = dyn_cast<CastInst>(U)) - newVal = CastInst::Create(C->getOpcode(), - newOp1, C->getType(), - C->getName()+".expr"); - - uint32_t v = VN.lookup_or_add(newVal); - - Value* leader = find_leader(availableOut[pred], v); - if (leader == 0) { - createdExpressions.push_back(newVal); - return newVal; - } else { - VN.erase(newVal); - delete newVal; - return leader; - } - } - - // Binary Operations - } if (isa<BinaryOperator>(V) || isa<CmpInst>(V) || - isa<ExtractElementInst>(V)) { - User* U = cast<User>(V); - - Value* newOp1 = 0; - if (isa<Instruction>(U->getOperand(0))) - newOp1 = phi_translate(U->getOperand(0), pred, succ); - else - newOp1 = U->getOperand(0); - - if (newOp1 == 0) - return 0; - - Value* newOp2 = 0; - if (isa<Instruction>(U->getOperand(1))) - newOp2 = phi_translate(U->getOperand(1), pred, succ); - else - newOp2 = U->getOperand(1); - - if (newOp2 == 0) - return 0; - - if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) { - Instruction* newVal = 0; - if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U)) - newVal = BinaryOperator::Create(BO->getOpcode(), - newOp1, newOp2, - BO->getName()+".expr"); - else if (CmpInst* C = dyn_cast<CmpInst>(U)) - newVal = CmpInst::Create(C->getOpcode(), - C->getPredicate(), - newOp1, newOp2, - C->getName()+".expr"); - else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U)) - newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr"); - - uint32_t v = VN.lookup_or_add(newVal); - - Value* leader = find_leader(availableOut[pred], v); - if (leader == 0) { - createdExpressions.push_back(newVal); - return newVal; - } else { - VN.erase(newVal); - delete newVal; - return leader; - } - } - - // Ternary Operations - } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) || - isa<SelectInst>(V)) { - User* U = cast<User>(V); - - Value* newOp1 = 0; - if (isa<Instruction>(U->getOperand(0))) - newOp1 = phi_translate(U->getOperand(0), pred, succ); - else - newOp1 = U->getOperand(0); - - if (newOp1 == 0) - return 0; - - Value* newOp2 = 0; - if (isa<Instruction>(U->getOperand(1))) - newOp2 = phi_translate(U->getOperand(1), pred, succ); - else - newOp2 = U->getOperand(1); - - if (newOp2 == 0) - return 0; - - Value* newOp3 = 0; - if (isa<Instruction>(U->getOperand(2))) - newOp3 = phi_translate(U->getOperand(2), pred, succ); - else - newOp3 = U->getOperand(2); - - if (newOp3 == 0) - return 0; - - if (newOp1 != U->getOperand(0) || - newOp2 != U->getOperand(1) || - newOp3 != U->getOperand(2)) { - Instruction* newVal = 0; - if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U)) - newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3, - S->getName() + ".expr"); - else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U)) - newVal = InsertElementInst::Create(newOp1, newOp2, newOp3, - I->getName() + ".expr"); - else if (SelectInst* I = dyn_cast<SelectInst>(U)) - newVal = SelectInst::Create(newOp1, newOp2, newOp3, - I->getName() + ".expr"); - - uint32_t v = VN.lookup_or_add(newVal); - - Value* leader = find_leader(availableOut[pred], v); - if (leader == 0) { - createdExpressions.push_back(newVal); - return newVal; - } else { - VN.erase(newVal); - delete newVal; - return leader; - } - } - - // Varargs operators - } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) { - Value* newOp1 = 0; - if (isa<Instruction>(U->getPointerOperand())) - newOp1 = phi_translate(U->getPointerOperand(), pred, succ); - else - newOp1 = U->getPointerOperand(); - - if (newOp1 == 0) - return 0; - - bool changed_idx = false; - SmallVector<Value*, 4> newIdx; - for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end(); - I != E; ++I) - if (isa<Instruction>(*I)) { - Value* newVal = phi_translate(*I, pred, succ); - newIdx.push_back(newVal); - if (newVal != *I) - changed_idx = true; - } else { - newIdx.push_back(*I); - } - - if (newOp1 != U->getPointerOperand() || changed_idx) { - Instruction* newVal = - GetElementPtrInst::Create(newOp1, - newIdx.begin(), newIdx.end(), - U->getName()+".expr"); - - uint32_t v = VN.lookup_or_add(newVal); - - Value* leader = find_leader(availableOut[pred], v); - if (leader == 0) { - createdExpressions.push_back(newVal); - return newVal; - } else { - VN.erase(newVal); - delete newVal; - return leader; - } - } - - // PHI Nodes - } else if (PHINode* P = dyn_cast<PHINode>(V)) { - if (P->getParent() == succ) - return P->getIncomingValueForBlock(pred); - } - - return V; -} - -/// phi_translate_set - Perform phi translation on every element of a set -void GVNPRE::phi_translate_set(ValueNumberedSet& anticIn, - BasicBlock* pred, BasicBlock* succ, - ValueNumberedSet& out) { - for (ValueNumberedSet::iterator I = anticIn.begin(), - E = anticIn.end(); I != E; ++I) { - Value* V = phi_translate(*I, pred, succ); - if (V != 0 && !out.test(VN.lookup_or_add(V))) { - out.insert(V); - out.set(VN.lookup(V)); - } - } -} - -/// dependsOnInvoke - Test if a value has an phi node as an operand, any of -/// whose inputs is an invoke instruction. If this is true, we cannot safely -/// PRE the instruction or anything that depends on it. -bool GVNPRE::dependsOnInvoke(Value* V) { - if (PHINode* p = dyn_cast<PHINode>(V)) { - for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I) - if (isa<InvokeInst>(*I)) - return true; - return false; - } else { - return false; - } -} - -/// clean - Remove all non-opaque values from the set whose operands are not -/// themselves in the set, as well as all values that depend on invokes (see -/// above) -void GVNPRE::clean(ValueNumberedSet& set) { - SmallVector<Value*, 8> worklist; - worklist.reserve(set.size()); - topo_sort(set, worklist); - - for (unsigned i = 0; i < worklist.size(); ++i) { - Value* v = worklist[i]; - - // Handle unary ops - if (CastInst* U = dyn_cast<CastInst>(v)) { - bool lhsValid = !isa<Instruction>(U->getOperand(0)); - lhsValid |= set.test(VN.lookup(U->getOperand(0))); - if (lhsValid) - lhsValid = !dependsOnInvoke(U->getOperand(0)); - - if (!lhsValid) { - set.erase(U); - set.reset(VN.lookup(U)); - } - - // Handle binary ops - } else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) || - isa<ExtractElementInst>(v)) { - User* U = cast<User>(v); - - bool lhsValid = !isa<Instruction>(U->getOperand(0)); - lhsValid |= set.test(VN.lookup(U->getOperand(0))); - if (lhsValid) - lhsValid = !dependsOnInvoke(U->getOperand(0)); - - bool rhsValid = !isa<Instruction>(U->getOperand(1)); - rhsValid |= set.test(VN.lookup(U->getOperand(1))); - if (rhsValid) - rhsValid = !dependsOnInvoke(U->getOperand(1)); - - if (!lhsValid || !rhsValid) { - set.erase(U); - set.reset(VN.lookup(U)); - } - - // Handle ternary ops - } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) || - isa<SelectInst>(v)) { - User* U = cast<User>(v); - - bool lhsValid = !isa<Instruction>(U->getOperand(0)); - lhsValid |= set.test(VN.lookup(U->getOperand(0))); - if (lhsValid) - lhsValid = !dependsOnInvoke(U->getOperand(0)); - - bool rhsValid = !isa<Instruction>(U->getOperand(1)); - rhsValid |= set.test(VN.lookup(U->getOperand(1))); - if (rhsValid) - rhsValid = !dependsOnInvoke(U->getOperand(1)); - - bool thirdValid = !isa<Instruction>(U->getOperand(2)); - thirdValid |= set.test(VN.lookup(U->getOperand(2))); - if (thirdValid) - thirdValid = !dependsOnInvoke(U->getOperand(2)); - - if (!lhsValid || !rhsValid || !thirdValid) { - set.erase(U); - set.reset(VN.lookup(U)); - } - - // Handle varargs ops - } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) { - bool ptrValid = !isa<Instruction>(U->getPointerOperand()); - ptrValid |= set.test(VN.lookup(U->getPointerOperand())); - if (ptrValid) - ptrValid = !dependsOnInvoke(U->getPointerOperand()); - - bool varValid = true; - for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end(); - I != E; ++I) - if (varValid) { - varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I)); - varValid &= !dependsOnInvoke(*I); - } - - if (!ptrValid || !varValid) { - set.erase(U); - set.reset(VN.lookup(U)); - } - } - } -} - -/// topo_sort - Given a set of values, sort them by topological -/// order into the provided vector. -void GVNPRE::topo_sort(ValueNumberedSet& set, SmallVector<Value*, 8>& vec) { - SmallPtrSet<Value*, 16> visited; - SmallVector<Value*, 8> stack; - for (ValueNumberedSet::iterator I = set.begin(), E = set.end(); - I != E; ++I) { - if (visited.count(*I) == 0) - stack.push_back(*I); - - while (!stack.empty()) { - Value* e = stack.back(); - - // Handle unary ops - if (CastInst* U = dyn_cast<CastInst>(e)) { - Value* l = find_leader(set, VN.lookup(U->getOperand(0))); - - if (l != 0 && isa<Instruction>(l) && - visited.count(l) == 0) - stack.push_back(l); - else { - vec.push_back(e); - visited.insert(e); - stack.pop_back(); - } - - // Handle binary ops - } else if (isa<BinaryOperator>(e) || isa<CmpInst>(e) || - isa<ExtractElementInst>(e)) { - User* U = cast<User>(e); - Value* l = find_leader(set, VN.lookup(U->getOperand(0))); - Value* r = find_leader(set, VN.lookup(U->getOperand(1))); - - if (l != 0 && isa<Instruction>(l) && - visited.count(l) == 0) - stack.push_back(l); - else if (r != 0 && isa<Instruction>(r) && - visited.count(r) == 0) - stack.push_back(r); - else { - vec.push_back(e); - visited.insert(e); - stack.pop_back(); - } - - // Handle ternary ops - } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) || - isa<SelectInst>(e)) { - User* U = cast<User>(e); - Value* l = find_leader(set, VN.lookup(U->getOperand(0))); - Value* r = find_leader(set, VN.lookup(U->getOperand(1))); - Value* m = find_leader(set, VN.lookup(U->getOperand(2))); - - if (l != 0 && isa<Instruction>(l) && - visited.count(l) == 0) - stack.push_back(l); - else if (r != 0 && isa<Instruction>(r) && - visited.count(r) == 0) - stack.push_back(r); - else if (m != 0 && isa<Instruction>(m) && - visited.count(m) == 0) - stack.push_back(m); - else { - vec.push_back(e); - visited.insert(e); - stack.pop_back(); - } - - // Handle vararg ops - } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) { - Value* p = find_leader(set, VN.lookup(U->getPointerOperand())); - - if (p != 0 && isa<Instruction>(p) && - visited.count(p) == 0) - stack.push_back(p); - else { - bool push_va = false; - for (GetElementPtrInst::op_iterator I = U->idx_begin(), - E = U->idx_end(); I != E; ++I) { - Value * v = find_leader(set, VN.lookup(*I)); - if (v != 0 && isa<Instruction>(v) && visited.count(v) == 0) { - stack.push_back(v); - push_va = true; - } - } - - if (!push_va) { - vec.push_back(e); - visited.insert(e); - stack.pop_back(); - } - } - - // Handle opaque ops - } else { - visited.insert(e); - vec.push_back(e); - stack.pop_back(); - } - } - - stack.clear(); - } -} - -/// dump - Dump a set of values to standard error -void GVNPRE::dump(ValueNumberedSet& s) const { - DOUT << "{ "; - for (ValueNumberedSet::iterator I = s.begin(), E = s.end(); - I != E; ++I) { - DOUT << "" << VN.lookup(*I) << ": "; - DEBUG((*I)->dump()); - } - DOUT << "}\n\n"; -} - -/// elimination - Phase 3 of the main algorithm. Perform full redundancy -/// elimination by walking the dominator tree and removing any instruction that -/// is dominated by another instruction with the same value number. -bool GVNPRE::elimination() { - bool changed_function = false; - - SmallVector<std::pair<Instruction*, Value*>, 8> replace; - SmallVector<Instruction*, 8> erase; - - DominatorTree& DT = getAnalysis<DominatorTree>(); - - for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()), - E = df_end(DT.getRootNode()); DI != E; ++DI) { - BasicBlock* BB = DI->getBlock(); - - for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); - BI != BE; ++BI) { - - if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) || - isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) || - isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) || - isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) { - - if (availableOut[BB].test(VN.lookup(BI)) && - !availableOut[BB].count(BI)) { - Value *leader = find_leader(availableOut[BB], VN.lookup(BI)); - if (Instruction* Instr = dyn_cast<Instruction>(leader)) - if (Instr->getParent() != 0 && Instr != BI) { - replace.push_back(std::make_pair(BI, leader)); - erase.push_back(BI); - ++NumEliminated; - } - } - } - } - } - - while (!replace.empty()) { - std::pair<Instruction*, Value*> rep = replace.back(); - replace.pop_back(); - rep.first->replaceAllUsesWith(rep.second); - changed_function = true; - } - - for (SmallVector<Instruction*, 8>::iterator I = erase.begin(), - E = erase.end(); I != E; ++I) - (*I)->eraseFromParent(); - - return changed_function; -} - -/// cleanup - Delete any extraneous values that were created to represent -/// expressions without leaders. -void GVNPRE::cleanup() { - while (!createdExpressions.empty()) { - Instruction* I = createdExpressions.back(); - createdExpressions.pop_back(); - - delete I; - } -} - -/// buildsets_availout - When calculating availability, handle an instruction -/// by inserting it into the appropriate sets -void GVNPRE::buildsets_availout(BasicBlock::iterator I, - ValueNumberedSet& currAvail, - ValueNumberedSet& currPhis, - ValueNumberedSet& currExps, - SmallPtrSet<Value*, 16>& currTemps) { - // Handle PHI nodes - if (PHINode* p = dyn_cast<PHINode>(I)) { - unsigned num = VN.lookup_or_add(p); - - currPhis.insert(p); - currPhis.set(num); - - // Handle unary ops - } else if (CastInst* U = dyn_cast<CastInst>(I)) { - Value* leftValue = U->getOperand(0); - - unsigned num = VN.lookup_or_add(U); - - if (isa<Instruction>(leftValue)) - if (!currExps.test(VN.lookup(leftValue))) { - currExps.insert(leftValue); - currExps.set(VN.lookup(leftValue)); - } - - if (!currExps.test(num)) { - currExps.insert(U); - currExps.set(num); - } - - // Handle binary ops - } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || - isa<ExtractElementInst>(I)) { - User* U = cast<User>(I); - Value* leftValue = U->getOperand(0); - Value* rightValue = U->getOperand(1); - - unsigned num = VN.lookup_or_add(U); - - if (isa<Instruction>(leftValue)) - if (!currExps.test(VN.lookup(leftValue))) { - currExps.insert(leftValue); - currExps.set(VN.lookup(leftValue)); - } - - if (isa<Instruction>(rightValue)) - if (!currExps.test(VN.lookup(rightValue))) { - currExps.insert(rightValue); - currExps.set(VN.lookup(rightValue)); - } - - if (!currExps.test(num)) { - currExps.insert(U); - currExps.set(num); - } - - // Handle ternary ops - } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) || - isa<SelectInst>(I)) { - User* U = cast<User>(I); - Value* leftValue = U->getOperand(0); - Value* rightValue = U->getOperand(1); - Value* thirdValue = U->getOperand(2); - - VN.lookup_or_add(U); - - unsigned num = VN.lookup_or_add(U); - - if (isa<Instruction>(leftValue)) - if (!currExps.test(VN.lookup(leftValue))) { - currExps.insert(leftValue); - currExps.set(VN.lookup(leftValue)); - } - if (isa<Instruction>(rightValue)) - if (!currExps.test(VN.lookup(rightValue))) { - currExps.insert(rightValue); - currExps.set(VN.lookup(rightValue)); - } - if (isa<Instruction>(thirdValue)) - if (!currExps.test(VN.lookup(thirdValue))) { - currExps.insert(thirdValue); - currExps.set(VN.lookup(thirdValue)); - } - - if (!currExps.test(num)) { - currExps.insert(U); - currExps.set(num); - } - - // Handle vararg ops - } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) { - Value* ptrValue = U->getPointerOperand(); - - VN.lookup_or_add(U); - - unsigned num = VN.lookup_or_add(U); - - if (isa<Instruction>(ptrValue)) - if (!currExps.test(VN.lookup(ptrValue))) { - currExps.insert(ptrValue); - currExps.set(VN.lookup(ptrValue)); - } - - for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end(); - OI != OE; ++OI) - if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) { - currExps.insert(*OI); - currExps.set(VN.lookup(*OI)); - } - - if (!currExps.test(VN.lookup(U))) { - currExps.insert(U); - currExps.set(num); - } - - // Handle opaque ops - } else if (!I->isTerminator()){ - VN.lookup_or_add(I); - - currTemps.insert(I); - } - - if (!I->isTerminator()) - if (!currAvail.test(VN.lookup(I))) { - currAvail.insert(I); - currAvail.set(VN.lookup(I)); - } -} - -/// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT -/// set as a function of the ANTIC_IN set of the block's predecessors -bool GVNPRE::buildsets_anticout(BasicBlock* BB, - ValueNumberedSet& anticOut, - SmallPtrSet<BasicBlock*, 8>& visited) { - if (BB->getTerminator()->getNumSuccessors() == 1) { - if (BB->getTerminator()->getSuccessor(0) != BB && - visited.count(BB->getTerminator()->getSuccessor(0)) == 0) { - return true; - } - else { - phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)], - BB, BB->getTerminator()->getSuccessor(0), anticOut); - } - } else if (BB->getTerminator()->getNumSuccessors() > 1) { - BasicBlock* first = BB->getTerminator()->getSuccessor(0); - for (ValueNumberedSet::iterator I = anticipatedIn[first].begin(), - E = anticipatedIn[first].end(); I != E; ++I) { - anticOut.insert(*I); - anticOut.set(VN.lookup(*I)); - } - - for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) { - BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i); - ValueNumberedSet& succAnticIn = anticipatedIn[currSucc]; - - SmallVector<Value*, 16> temp; - - for (ValueNumberedSet::iterator I = anticOut.begin(), - E = anticOut.end(); I != E; ++I) - if (!succAnticIn.test(VN.lookup(*I))) - temp.push_back(*I); - - for (SmallVector<Value*, 16>::iterator I = temp.begin(), E = temp.end(); - I != E; ++I) { - anticOut.erase(*I); - anticOut.reset(VN.lookup(*I)); - } - } - } - - return false; -} - -/// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for -/// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN -/// sets populated in buildsets_availout -unsigned GVNPRE::buildsets_anticin(BasicBlock* BB, - ValueNumberedSet& anticOut, - ValueNumberedSet& currExps, - SmallPtrSet<Value*, 16>& currTemps, - SmallPtrSet<BasicBlock*, 8>& visited) { - ValueNumberedSet& anticIn = anticipatedIn[BB]; - unsigned old = anticIn.size(); - - bool defer = buildsets_anticout(BB, anticOut, visited); - if (defer) - return 0; - - anticIn.clear(); - - for (ValueNumberedSet::iterator I = anticOut.begin(), - E = anticOut.end(); I != E; ++I) { - anticIn.insert(*I); - anticIn.set(VN.lookup(*I)); - } - for (ValueNumberedSet::iterator I = currExps.begin(), - E = currExps.end(); I != E; ++I) { - if (!anticIn.test(VN.lookup(*I))) { - anticIn.insert(*I); - anticIn.set(VN.lookup(*I)); - } - } - - for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(), - E = currTemps.end(); I != E; ++I) { - anticIn.erase(*I); - anticIn.reset(VN.lookup(*I)); - } - - clean(anticIn); - anticOut.clear(); - - if (old != anticIn.size()) - return 2; - else - return 1; -} - -/// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT -/// and the ANTIC_IN sets. -void GVNPRE::buildsets(Function& F) { - DenseMap<BasicBlock*, ValueNumberedSet> generatedExpressions; - DenseMap<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries; - - DominatorTree &DT = getAnalysis<DominatorTree>(); - - // Phase 1, Part 1: calculate AVAIL_OUT - - // Top-down walk of the dominator tree - for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()), - E = df_end(DT.getRootNode()); DI != E; ++DI) { - - // Get the sets to update for this block - ValueNumberedSet& currExps = generatedExpressions[DI->getBlock()]; - ValueNumberedSet& currPhis = generatedPhis[DI->getBlock()]; - SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()]; - ValueNumberedSet& currAvail = availableOut[DI->getBlock()]; - - BasicBlock* BB = DI->getBlock(); - - // A block inherits AVAIL_OUT from its dominator - if (DI->getIDom() != 0) - currAvail = availableOut[DI->getIDom()->getBlock()]; - - for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); - BI != BE; ++BI) - buildsets_availout(BI, currAvail, currPhis, currExps, - currTemps); - - } - - // Phase 1, Part 2: calculate ANTIC_IN - - SmallPtrSet<BasicBlock*, 8> visited; - SmallPtrSet<BasicBlock*, 4> block_changed; - for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) - block_changed.insert(FI); - - bool changed = true; - unsigned iterations = 0; - - while (changed) { - changed = false; - ValueNumberedSet anticOut; - - // Postorder walk of the CFG - for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()), - BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) { - BasicBlock* BB = *BBI; - - if (block_changed.count(BB) != 0) { - unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB], - generatedTemporaries[BB], visited); - - if (ret == 0) { - changed = true; - continue; - } else { - visited.insert(BB); - - if (ret == 2) - for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); - PI != PE; ++PI) { - block_changed.insert(*PI); - } - else - block_changed.erase(BB); - - changed |= (ret == 2); - } - } - } - - iterations++; - } -} - -/// insertion_pre - When a partial redundancy has been identified, eliminate it -/// by inserting appropriate values into the predecessors and a phi node in -/// the main block -void GVNPRE::insertion_pre(Value* e, BasicBlock* BB, - DenseMap<BasicBlock*, Value*>& avail, - std::map<BasicBlock*, ValueNumberedSet>& new_sets) { - for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { - Value* e2 = avail[*PI]; - if (!availableOut[*PI].test(VN.lookup(e2))) { - User* U = cast<User>(e2); - - Value* s1 = 0; - if (isa<BinaryOperator>(U->getOperand(0)) || - isa<CmpInst>(U->getOperand(0)) || - isa<ShuffleVectorInst>(U->getOperand(0)) || - isa<ExtractElementInst>(U->getOperand(0)) || - isa<InsertElementInst>(U->getOperand(0)) || - isa<SelectInst>(U->getOperand(0)) || - isa<CastInst>(U->getOperand(0)) || - isa<GetElementPtrInst>(U->getOperand(0))) - s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0))); - else - s1 = U->getOperand(0); - - Value* s2 = 0; - - if (isa<BinaryOperator>(U) || - isa<CmpInst>(U) || - isa<ShuffleVectorInst>(U) || - isa<ExtractElementInst>(U) || - isa<InsertElementInst>(U) || - isa<SelectInst>(U)) { - if (isa<BinaryOperator>(U->getOperand(1)) || - isa<CmpInst>(U->getOperand(1)) || - isa<ShuffleVectorInst>(U->getOperand(1)) || - isa<ExtractElementInst>(U->getOperand(1)) || - isa<InsertElementInst>(U->getOperand(1)) || - isa<SelectInst>(U->getOperand(1)) || - isa<CastInst>(U->getOperand(1)) || - isa<GetElementPtrInst>(U->getOperand(1))) { - s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1))); - } else { - s2 = U->getOperand(1); - } - } - - // Ternary Operators - Value* s3 = 0; - if (isa<ShuffleVectorInst>(U) || - isa<InsertElementInst>(U) || - isa<SelectInst>(U)) { - if (isa<BinaryOperator>(U->getOperand(2)) || - isa<CmpInst>(U->getOperand(2)) || - isa<ShuffleVectorInst>(U->getOperand(2)) || - isa<ExtractElementInst>(U->getOperand(2)) || - isa<InsertElementInst>(U->getOperand(2)) || - isa<SelectInst>(U->getOperand(2)) || - isa<CastInst>(U->getOperand(2)) || - isa<GetElementPtrInst>(U->getOperand(2))) { - s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2))); - } else { - s3 = U->getOperand(2); - } - } - - // Vararg operators - SmallVector<Value*, 4> sVarargs; - if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) { - for (GetElementPtrInst::op_iterator OI = G->idx_begin(), - OE = G->idx_end(); OI != OE; ++OI) { - if (isa<BinaryOperator>(*OI) || - isa<CmpInst>(*OI) || - isa<ShuffleVectorInst>(*OI) || - isa<ExtractElementInst>(*OI) || - isa<InsertElementInst>(*OI) || - isa<SelectInst>(*OI) || - isa<CastInst>(*OI) || - isa<GetElementPtrInst>(*OI)) { - sVarargs.push_back(find_leader(availableOut[*PI], - VN.lookup(*OI))); - } else { - sVarargs.push_back(*OI); - } - } - } - - Value* newVal = 0; - if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U)) - newVal = BinaryOperator::Create(BO->getOpcode(), s1, s2, - BO->getName()+".gvnpre", - (*PI)->getTerminator()); - else if (CmpInst* C = dyn_cast<CmpInst>(U)) - newVal = CmpInst::Create(C->getOpcode(), C->getPredicate(), s1, s2, - C->getName()+".gvnpre", - (*PI)->getTerminator()); - else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U)) - newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre", - (*PI)->getTerminator()); - else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U)) - newVal = InsertElementInst::Create(s1, s2, s3, S->getName()+".gvnpre", - (*PI)->getTerminator()); - else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U)) - newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre", - (*PI)->getTerminator()); - else if (SelectInst* S = dyn_cast<SelectInst>(U)) - newVal = SelectInst::Create(s1, s2, s3, S->getName()+".gvnpre", - (*PI)->getTerminator()); - else if (CastInst* C = dyn_cast<CastInst>(U)) - newVal = CastInst::Create(C->getOpcode(), s1, C->getType(), - C->getName()+".gvnpre", - (*PI)->getTerminator()); - else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) - newVal = GetElementPtrInst::Create(s1, sVarargs.begin(), sVarargs.end(), - G->getName()+".gvnpre", - (*PI)->getTerminator()); - - VN.add(newVal, VN.lookup(U)); - - ValueNumberedSet& predAvail = availableOut[*PI]; - val_replace(predAvail, newVal); - val_replace(new_sets[*PI], newVal); - predAvail.set(VN.lookup(newVal)); - - DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI); - if (av != avail.end()) - avail.erase(av); - avail.insert(std::make_pair(*PI, newVal)); - - ++NumInsertedVals; - } - } - - PHINode* p = 0; - - for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { - if (p == 0) - p = PHINode::Create(avail[*PI]->getType(), "gvnpre-join", BB->begin()); - - p->addIncoming(avail[*PI], *PI); - } - - VN.add(p, VN.lookup(e)); - val_replace(availableOut[BB], p); - availableOut[BB].set(VN.lookup(e)); - generatedPhis[BB].insert(p); - generatedPhis[BB].set(VN.lookup(e)); - new_sets[BB].insert(p); - new_sets[BB].set(VN.lookup(e)); - - ++NumInsertedPhis; -} - -/// insertion_mergepoint - When walking the dom tree, check at each merge -/// block for the possibility of a partial redundancy. If present, eliminate it -unsigned GVNPRE::insertion_mergepoint(SmallVector<Value*, 8>& workList, - df_iterator<DomTreeNode*>& D, - std::map<BasicBlock*, ValueNumberedSet >& new_sets) { - bool changed_function = false; - bool new_stuff = false; - - BasicBlock* BB = D->getBlock(); - for (unsigned i = 0; i < workList.size(); ++i) { - Value* e = workList[i]; - - if (isa<BinaryOperator>(e) || isa<CmpInst>(e) || - isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) || - isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) || - isa<GetElementPtrInst>(e)) { - if (availableOut[D->getIDom()->getBlock()].test(VN.lookup(e))) - continue; - - DenseMap<BasicBlock*, Value*> avail; - bool by_some = false; - bool all_same = true; - Value * first_s = 0; - - for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; - ++PI) { - Value *e2 = phi_translate(e, *PI, BB); - Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2)); - - if (e3 == 0) { - DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI); - if (av != avail.end()) - avail.erase(av); - avail.insert(std::make_pair(*PI, e2)); - all_same = false; - } else { - DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI); - if (av != avail.end()) - avail.erase(av); - avail.insert(std::make_pair(*PI, e3)); - - by_some = true; - if (first_s == 0) - first_s = e3; - else if (first_s != e3) - all_same = false; - } - } - - if (by_some && !all_same && - !generatedPhis[BB].test(VN.lookup(e))) { - insertion_pre(e, BB, avail, new_sets); - - changed_function = true; - new_stuff = true; - } - } - } - - unsigned retval = 0; - if (changed_function) - retval += 1; - if (new_stuff) - retval += 2; - - return retval; -} - -/// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for -/// merge points. When one is found, check for a partial redundancy. If one is -/// present, eliminate it. Repeat this walk until no changes are made. -bool GVNPRE::insertion(Function& F) { - bool changed_function = false; - - DominatorTree &DT = getAnalysis<DominatorTree>(); - - std::map<BasicBlock*, ValueNumberedSet> new_sets; - bool new_stuff = true; - while (new_stuff) { - new_stuff = false; - for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()), - E = df_end(DT.getRootNode()); DI != E; ++DI) { - BasicBlock* BB = DI->getBlock(); - - if (BB == 0) - continue; - - ValueNumberedSet& availOut = availableOut[BB]; - ValueNumberedSet& anticIn = anticipatedIn[BB]; - - // Replace leaders with leaders inherited from dominator - if (DI->getIDom() != 0) { - ValueNumberedSet& dom_set = new_sets[DI->getIDom()->getBlock()]; - for (ValueNumberedSet::iterator I = dom_set.begin(), - E = dom_set.end(); I != E; ++I) { - val_replace(new_sets[BB], *I); - val_replace(availOut, *I); - } - } - - // If there is more than one predecessor... - if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) { - SmallVector<Value*, 8> workList; - workList.reserve(anticIn.size()); - topo_sort(anticIn, workList); - - unsigned result = insertion_mergepoint(workList, DI, new_sets); - if (result & 1) - changed_function = true; - if (result & 2) - new_stuff = true; - } - } - } - - return changed_function; -} - -// GVNPRE::runOnFunction - This is the main transformation entry point for a -// function. -// -bool GVNPRE::runOnFunction(Function &F) { - // Clean out global sets from any previous functions - VN.clear(); - createdExpressions.clear(); - availableOut.clear(); - anticipatedIn.clear(); - generatedPhis.clear(); - - bool changed_function = false; - - // Phase 1: BuildSets - // This phase calculates the AVAIL_OUT and ANTIC_IN sets - buildsets(F); - - // Phase 2: Insert - // This phase inserts values to make partially redundant values - // fully redundant - changed_function |= insertion(F); - - // Phase 3: Eliminate - // This phase performs trivial full redundancy elimination - changed_function |= elimination(); - - // Phase 4: Cleanup - // This phase cleans up values that were created solely - // as leaders for expressions - cleanup(); - - return changed_function; -} diff --git a/lib/Transforms/Scalar/PredicateSimplifier.cpp b/lib/Transforms/Scalar/PredicateSimplifier.cpp deleted file mode 100644 index a3cb751..0000000 --- a/lib/Transforms/Scalar/PredicateSimplifier.cpp +++ /dev/null @@ -1,2721 +0,0 @@ -//===-- PredicateSimplifier.cpp - Path Sensitive Simplifier ---------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Path-sensitive optimizer. In a branch where x == y, replace uses of -// x with y. Permits further optimization, such as the elimination of -// the unreachable call: -// -// void test(int *p, int *q) -// { -// if (p != q) -// return; -// -// if (*p != *q) -// foo(); // unreachable -// } -// -//===----------------------------------------------------------------------===// -// -// The InequalityGraph focusses on four properties; equals, not equals, -// less-than and less-than-or-equals-to. The greater-than forms are also held -// just to allow walking from a lesser node to a greater one. These properties -// are stored in a lattice; LE can become LT or EQ, NE can become LT or GT. -// -// These relationships define a graph between values of the same type. Each -// Value is stored in a map table that retrieves the associated Node. This -// is how EQ relationships are stored; the map contains pointers from equal -// Value to the same node. The node contains a most canonical Value* form -// and the list of known relationships with other nodes. -// -// If two nodes are known to be inequal, then they will contain pointers to -// each other with an "NE" relationship. If node getNode(%x) is less than -// getNode(%y), then the %x node will contain <%y, GT> and %y will contain -// <%x, LT>. This allows us to tie nodes together into a graph like this: -// -// %a < %b < %c < %d -// -// with four nodes representing the properties. The InequalityGraph provides -// querying with "isRelatedBy" and mutators "addEquality" and "addInequality". -// To find a relationship, we start with one of the nodes any binary search -// through its list to find where the relationships with the second node start. -// Then we iterate through those to find the first relationship that dominates -// our context node. -// -// To create these properties, we wait until a branch or switch instruction -// implies that a particular value is true (or false). The VRPSolver is -// responsible for analyzing the variable and seeing what new inferences -// can be made from each property. For example: -// -// %P = icmp ne i32* %ptr, null -// %a = and i1 %P, %Q -// br i1 %a label %cond_true, label %cond_false -// -// For the true branch, the VRPSolver will start with %a EQ true and look at -// the definition of %a and find that it can infer that %P and %Q are both -// true. From %P being true, it can infer that %ptr NE null. For the false -// branch it can't infer anything from the "and" instruction. -// -// Besides branches, we can also infer properties from instruction that may -// have undefined behaviour in certain cases. For example, the dividend of -// a division may never be zero. After the division instruction, we may assume -// that the dividend is not equal to zero. -// -//===----------------------------------------------------------------------===// -// -// The ValueRanges class stores the known integer bounds of a Value. When we -// encounter i8 %a u< %b, the ValueRanges stores that %a = [1, 255] and -// %b = [0, 254]. -// -// It never stores an empty range, because that means that the code is -// unreachable. It never stores a single-element range since that's an equality -// relationship and better stored in the InequalityGraph, nor an empty range -// since that is better stored in UnreachableBlocks. -// -//===----------------------------------------------------------------------===// - -#define DEBUG_TYPE "predsimplify" -#include "llvm/Transforms/Scalar.h" -#include "llvm/Constants.h" -#include "llvm/DerivedTypes.h" -#include "llvm/Instructions.h" -#include "llvm/Pass.h" -#include "llvm/ADT/DepthFirstIterator.h" -#include "llvm/ADT/SetOperations.h" -#include "llvm/ADT/SetVector.h" -#include "llvm/ADT/Statistic.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/Analysis/Dominators.h" -#include "llvm/Assembly/Writer.h" -#include "llvm/Support/CFG.h" -#include "llvm/Support/Compiler.h" -#include "llvm/Support/ConstantRange.h" -#include "llvm/Support/Debug.h" -#include "llvm/Support/InstVisitor.h" -#include "llvm/Target/TargetData.h" -#include "llvm/Transforms/Utils/Local.h" -#include <algorithm> -#include <deque> -#include <stack> -using namespace llvm; - -STATISTIC(NumVarsReplaced, "Number of argument substitutions"); -STATISTIC(NumInstruction , "Number of instructions removed"); -STATISTIC(NumSimple , "Number of simple replacements"); -STATISTIC(NumBlocks , "Number of blocks marked unreachable"); -STATISTIC(NumSnuggle , "Number of comparisons snuggled"); - -static const ConstantRange empty(1, false); - -namespace { - class DomTreeDFS { - public: - class Node { - friend class DomTreeDFS; - public: - typedef std::vector<Node *>::iterator iterator; - typedef std::vector<Node *>::const_iterator const_iterator; - - unsigned getDFSNumIn() const { return DFSin; } - unsigned getDFSNumOut() const { return DFSout; } - - BasicBlock *getBlock() const { return BB; } - - iterator begin() { return Children.begin(); } - iterator end() { return Children.end(); } - - const_iterator begin() const { return Children.begin(); } - const_iterator end() const { return Children.end(); } - - bool dominates(const Node *N) const { - return DFSin <= N->DFSin && DFSout >= N->DFSout; - } - - bool DominatedBy(const Node *N) const { - return N->dominates(this); - } - - /// Sorts by the number of descendants. With this, you can iterate - /// through a sorted list and the first matching entry is the most - /// specific match for your basic block. The order provided is stable; - /// DomTreeDFS::Nodes with the same number of descendants are sorted by - /// DFS in number. - bool operator<(const Node &N) const { - unsigned spread = DFSout - DFSin; - unsigned N_spread = N.DFSout - N.DFSin; - if (spread == N_spread) return DFSin < N.DFSin; - return spread < N_spread; - } - bool operator>(const Node &N) const { return N < *this; } - - private: - unsigned DFSin, DFSout; - BasicBlock *BB; - - std::vector<Node *> Children; - }; - - // XXX: this may be slow. Instead of using "new" for each node, consider - // putting them in a vector to keep them contiguous. - explicit DomTreeDFS(DominatorTree *DT) { - std::stack<std::pair<Node *, DomTreeNode *> > S; - - Entry = new Node; - Entry->BB = DT->getRootNode()->getBlock(); - S.push(std::make_pair(Entry, DT->getRootNode())); - - NodeMap[Entry->BB] = Entry; - - while (!S.empty()) { - std::pair<Node *, DomTreeNode *> &Pair = S.top(); - Node *N = Pair.first; - DomTreeNode *DTNode = Pair.second; - S.pop(); - - for (DomTreeNode::iterator I = DTNode->begin(), E = DTNode->end(); - I != E; ++I) { - Node *NewNode = new Node; - NewNode->BB = (*I)->getBlock(); - N->Children.push_back(NewNode); - S.push(std::make_pair(NewNode, *I)); - - NodeMap[NewNode->BB] = NewNode; - } - } - - renumber(); - -#ifndef NDEBUG - DEBUG(dump()); -#endif - } - -#ifndef NDEBUG - virtual -#endif - ~DomTreeDFS() { - std::stack<Node *> S; - - S.push(Entry); - while (!S.empty()) { - Node *N = S.top(); S.pop(); - - for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I) - S.push(*I); - - delete N; - } - } - - /// getRootNode - This returns the entry node for the CFG of the function. - Node *getRootNode() const { return Entry; } - - /// getNodeForBlock - return the node for the specified basic block. - Node *getNodeForBlock(BasicBlock *BB) const { - if (!NodeMap.count(BB)) return 0; - return const_cast<DomTreeDFS*>(this)->NodeMap[BB]; - } - - /// dominates - returns true if the basic block for I1 dominates that of - /// the basic block for I2. If the instructions belong to the same basic - /// block, the instruction first instruction sequentially in the block is - /// considered dominating. - bool dominates(Instruction *I1, Instruction *I2) { - BasicBlock *BB1 = I1->getParent(), - *BB2 = I2->getParent(); - if (BB1 == BB2) { - if (isa<TerminatorInst>(I1)) return false; - if (isa<TerminatorInst>(I2)) return true; - if ( isa<PHINode>(I1) && !isa<PHINode>(I2)) return true; - if (!isa<PHINode>(I1) && isa<PHINode>(I2)) return false; - - for (BasicBlock::const_iterator I = BB2->begin(), E = BB2->end(); - I != E; ++I) { - if (&*I == I1) return true; - else if (&*I == I2) return false; - } - assert(!"Instructions not found in parent BasicBlock?"); - } else { - Node *Node1 = getNodeForBlock(BB1), - *Node2 = getNodeForBlock(BB2); - return Node1 && Node2 && Node1->dominates(Node2); - } - return false; // Not reached - } - - private: - /// renumber - calculates the depth first search numberings and applies - /// them onto the nodes. - void renumber() { - std::stack<std::pair<Node *, Node::iterator> > S; - unsigned n = 0; - - Entry->DFSin = ++n; - S.push(std::make_pair(Entry, Entry->begin())); - - while (!S.empty()) { - std::pair<Node *, Node::iterator> &Pair = S.top(); - Node *N = Pair.first; - Node::iterator &I = Pair.second; - - if (I == N->end()) { - N->DFSout = ++n; - S.pop(); - } else { - Node *Next = *I++; - Next->DFSin = ++n; - S.push(std::make_pair(Next, Next->begin())); - } - } - } - -#ifndef NDEBUG - virtual void dump() const { - dump(*cerr.stream()); - } - - void dump(std::ostream &os) const { - os << "Predicate simplifier DomTreeDFS: \n"; - dump(Entry, 0, os); - os << "\n\n"; - } - - void dump(Node *N, int depth, std::ostream &os) const { - ++depth; - for (int i = 0; i < depth; ++i) { os << " "; } - os << "[" << depth << "] "; - - os << N->getBlock()->getName() << " (" << N->getDFSNumIn() - << ", " << N->getDFSNumOut() << ")\n"; - - for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I) - dump(*I, depth, os); - } -#endif - - Node *Entry; - std::map<BasicBlock *, Node *> NodeMap; - }; - - // SLT SGT ULT UGT EQ - // 0 1 0 1 0 -- GT 10 - // 0 1 0 1 1 -- GE 11 - // 0 1 1 0 0 -- SGTULT 12 - // 0 1 1 0 1 -- SGEULE 13 - // 0 1 1 1 0 -- SGT 14 - // 0 1 1 1 1 -- SGE 15 - // 1 0 0 1 0 -- SLTUGT 18 - // 1 0 0 1 1 -- SLEUGE 19 - // 1 0 1 0 0 -- LT 20 - // 1 0 1 0 1 -- LE 21 - // 1 0 1 1 0 -- SLT 22 - // 1 0 1 1 1 -- SLE 23 - // 1 1 0 1 0 -- UGT 26 - // 1 1 0 1 1 -- UGE 27 - // 1 1 1 0 0 -- ULT 28 - // 1 1 1 0 1 -- ULE 29 - // 1 1 1 1 0 -- NE 30 - enum LatticeBits { - EQ_BIT = 1, UGT_BIT = 2, ULT_BIT = 4, SGT_BIT = 8, SLT_BIT = 16 - }; - enum LatticeVal { - GT = SGT_BIT | UGT_BIT, - GE = GT | EQ_BIT, - LT = SLT_BIT | ULT_BIT, - LE = LT | EQ_BIT, - NE = SLT_BIT | SGT_BIT | ULT_BIT | UGT_BIT, - SGTULT = SGT_BIT | ULT_BIT, - SGEULE = SGTULT | EQ_BIT, - SLTUGT = SLT_BIT | UGT_BIT, - SLEUGE = SLTUGT | EQ_BIT, - ULT = SLT_BIT | SGT_BIT | ULT_BIT, - UGT = SLT_BIT | SGT_BIT | UGT_BIT, - SLT = SLT_BIT | ULT_BIT | UGT_BIT, - SGT = SGT_BIT | ULT_BIT | UGT_BIT, - SLE = SLT | EQ_BIT, - SGE = SGT | EQ_BIT, - ULE = ULT | EQ_BIT, - UGE = UGT | EQ_BIT - }; - -#ifndef NDEBUG - /// validPredicate - determines whether a given value is actually a lattice - /// value. Only used in assertions or debugging. - static bool validPredicate(LatticeVal LV) { - switch (LV) { - case GT: case GE: case LT: case LE: case NE: - case SGTULT: case SGT: case SGEULE: - case SLTUGT: case SLT: case SLEUGE: - case ULT: case UGT: - case SLE: case SGE: case ULE: case UGE: - return true; - default: - return false; - } - } -#endif - - /// reversePredicate - reverse the direction of the inequality - static LatticeVal reversePredicate(LatticeVal LV) { - unsigned reverse = LV ^ (SLT_BIT|SGT_BIT|ULT_BIT|UGT_BIT); //preserve EQ_BIT - - if ((reverse & (SLT_BIT|SGT_BIT)) == 0) - reverse |= (SLT_BIT|SGT_BIT); - - if ((reverse & (ULT_BIT|UGT_BIT)) == 0) - reverse |= (ULT_BIT|UGT_BIT); - - LatticeVal Rev = static_cast<LatticeVal>(reverse); - assert(validPredicate(Rev) && "Failed reversing predicate."); - return Rev; - } - - /// ValueNumbering stores the scope-specific value numbers for a given Value. - class VISIBILITY_HIDDEN ValueNumbering { - - /// VNPair is a tuple of {Value, index number, DomTreeDFS::Node}. It - /// includes the comparison operators necessary to allow you to store it - /// in a sorted vector. - class VISIBILITY_HIDDEN VNPair { - public: - Value *V; - unsigned index; - DomTreeDFS::Node *Subtree; - - VNPair(Value *V, unsigned index, DomTreeDFS::Node *Subtree) - : V(V), index(index), Subtree(Subtree) {} - - bool operator==(const VNPair &RHS) const { - return V == RHS.V && Subtree == RHS.Subtree; - } - - bool operator<(const VNPair &RHS) const { - if (V != RHS.V) return V < RHS.V; - return *Subtree < *RHS.Subtree; - } - - bool operator<(Value *RHS) const { - return V < RHS; - } - - bool operator>(Value *RHS) const { - return V > RHS; - } - - friend bool operator<(Value *RHS, const VNPair &pair) { - return pair.operator>(RHS); - } - }; - - typedef std::vector<VNPair> VNMapType; - VNMapType VNMap; - - /// The canonical choice for value number at index. - std::vector<Value *> Values; - - DomTreeDFS *DTDFS; - - public: -#ifndef NDEBUG - virtual ~ValueNumbering() {} - virtual void dump() { - dump(*cerr.stream()); - } - - void dump(std::ostream &os) { - for (unsigned i = 1; i <= Values.size(); ++i) { - os << i << " = "; - WriteAsOperand(os, Values[i-1]); - os << " {"; - for (unsigned j = 0; j < VNMap.size(); ++j) { - if (VNMap[j].index == i) { - WriteAsOperand(os, VNMap[j].V); - os << " (" << VNMap[j].Subtree->getDFSNumIn() << ") "; - } - } - os << "}\n"; - } - } -#endif - - /// compare - returns true if V1 is a better canonical value than V2. - bool compare(Value *V1, Value *V2) const { - if (isa<Constant>(V1)) - return !isa<Constant>(V2); - else if (isa<Constant>(V2)) - return false; - else if (isa<Argument>(V1)) - return !isa<Argument>(V2); - else if (isa<Argument>(V2)) - return false; - - Instruction *I1 = dyn_cast<Instruction>(V1); - Instruction *I2 = dyn_cast<Instruction>(V2); - - if (!I1 || !I2) - return V1->getNumUses() < V2->getNumUses(); - - return DTDFS->dominates(I1, I2); - } - - ValueNumbering(DomTreeDFS *DTDFS) : DTDFS(DTDFS) {} - - /// valueNumber - finds the value number for V under the Subtree. If - /// there is no value number, returns zero. - unsigned valueNumber(Value *V, DomTreeDFS::Node *Subtree) { - if (!(isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) - || V->getType() == Type::VoidTy) return 0; - - VNMapType::iterator E = VNMap.end(); - VNPair pair(V, 0, Subtree); - VNMapType::iterator I = std::lower_bound(VNMap.begin(), E, pair); - while (I != E && I->V == V) { - if (I->Subtree->dominates(Subtree)) - return I->index; - ++I; - } - return 0; - } - - /// getOrInsertVN - always returns a value number, creating it if necessary. - unsigned getOrInsertVN(Value *V, DomTreeDFS::Node *Subtree) { - if (unsigned n = valueNumber(V, Subtree)) - return n; - else - return newVN(V); - } - - /// newVN - creates a new value number. Value V must not already have a - /// value number assigned. - unsigned newVN(Value *V) { - assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && - "Bad Value for value numbering."); - assert(V->getType() != Type::VoidTy && "Won't value number a void value"); - - Values.push_back(V); - - VNPair pair = VNPair(V, Values.size(), DTDFS->getRootNode()); - VNMapType::iterator I = std::lower_bound(VNMap.begin(), VNMap.end(), pair); - assert((I == VNMap.end() || value(I->index) != V) && - "Attempt to create a duplicate value number."); - VNMap.insert(I, pair); - - return Values.size(); - } - - /// value - returns the Value associated with a value number. - Value *value(unsigned index) const { - assert(index != 0 && "Zero index is reserved for not found."); - assert(index <= Values.size() && "Index out of range."); - return Values[index-1]; - } - - /// canonicalize - return a Value that is equal to V under Subtree. - Value *canonicalize(Value *V, DomTreeDFS::Node *Subtree) { - if (isa<Constant>(V)) return V; - - if (unsigned n = valueNumber(V, Subtree)) - return value(n); - else - return V; - } - - /// addEquality - adds that value V belongs to the set of equivalent - /// values defined by value number n under Subtree. - void addEquality(unsigned n, Value *V, DomTreeDFS::Node *Subtree) { - assert(canonicalize(value(n), Subtree) == value(n) && - "Node's 'canonical' choice isn't best within this subtree."); - - // Suppose that we are given "%x -> node #1 (%y)". The problem is that - // we may already have "%z -> node #2 (%x)" somewhere above us in the - // graph. We need to find those edges and add "%z -> node #1 (%y)" - // to keep the lookups canonical. - - std::vector<Value *> ToRepoint(1, V); - - if (unsigned Conflict = valueNumber(V, Subtree)) { - for (VNMapType::iterator I = VNMap.begin(), E = VNMap.end(); - I != E; ++I) { - if (I->index == Conflict && I->Subtree->dominates(Subtree)) - ToRepoint.push_back(I->V); - } - } - - for (std::vector<Value *>::iterator VI = ToRepoint.begin(), - VE = ToRepoint.end(); VI != VE; ++VI) { - Value *V = *VI; - - VNPair pair(V, n, Subtree); - VNMapType::iterator B = VNMap.begin(), E = VNMap.end(); - VNMapType::iterator I = std::lower_bound(B, E, pair); - if (I != E && I->V == V && I->Subtree == Subtree) - I->index = n; // Update best choice - else - VNMap.insert(I, pair); // New Value - - // XXX: we currently don't have to worry about updating values with - // more specific Subtrees, but we will need to for PHI node support. - -#ifndef NDEBUG - Value *V_n = value(n); - if (isa<Constant>(V) && isa<Constant>(V_n)) { - assert(V == V_n && "Constant equals different constant?"); - } -#endif - } - } - - /// remove - removes all references to value V. - void remove(Value *V) { - VNMapType::iterator B = VNMap.begin(), E = VNMap.end(); - VNPair pair(V, 0, DTDFS->getRootNode()); - VNMapType::iterator J = std::upper_bound(B, E, pair); - VNMapType::iterator I = J; - - while (I != B && (I == E || I->V == V)) --I; - - VNMap.erase(I, J); - } - }; - - /// The InequalityGraph stores the relationships between values. - /// Each Value in the graph is assigned to a Node. Nodes are pointer - /// comparable for equality. The caller is expected to maintain the logical - /// consistency of the system. - /// - /// The InequalityGraph class may invalidate Node*s after any mutator call. - /// @brief The InequalityGraph stores the relationships between values. - class VISIBILITY_HIDDEN InequalityGraph { - ValueNumbering &VN; - DomTreeDFS::Node *TreeRoot; - - InequalityGraph(); // DO NOT IMPLEMENT - InequalityGraph(InequalityGraph &); // DO NOT IMPLEMENT - public: - InequalityGraph(ValueNumbering &VN, DomTreeDFS::Node *TreeRoot) - : VN(VN), TreeRoot(TreeRoot) {} - - class Node; - - /// An Edge is contained inside a Node making one end of the edge implicit - /// and contains a pointer to the other end. The edge contains a lattice - /// value specifying the relationship and an DomTreeDFS::Node specifying - /// the root in the dominator tree to which this edge applies. - class VISIBILITY_HIDDEN Edge { - public: - Edge(unsigned T, LatticeVal V, DomTreeDFS::Node *ST) - : To(T), LV(V), Subtree(ST) {} - - unsigned To; - LatticeVal LV; - DomTreeDFS::Node *Subtree; - - bool operator<(const Edge &edge) const { - if (To != edge.To) return To < edge.To; - return *Subtree < *edge.Subtree; - } - - bool operator<(unsigned to) const { - return To < to; - } - - bool operator>(unsigned to) const { - return To > to; - } - - friend bool operator<(unsigned to, const Edge &edge) { - return edge.operator>(to); - } - }; - - /// A single node in the InequalityGraph. This stores the canonical Value - /// for the node, as well as the relationships with the neighbours. - /// - /// @brief A single node in the InequalityGraph. - class VISIBILITY_HIDDEN Node { - friend class InequalityGraph; - - typedef SmallVector<Edge, 4> RelationsType; - RelationsType Relations; - - // TODO: can this idea improve performance? - //friend class std::vector<Node>; - //Node(Node &N) { RelationsType.swap(N.RelationsType); } - - public: - typedef RelationsType::iterator iterator; - typedef RelationsType::const_iterator const_iterator; - -#ifndef NDEBUG - virtual ~Node() {} - virtual void dump() const { - dump(*cerr.stream()); - } - private: - void dump(std::ostream &os) const { - static const std::string names[32] = - { "000000", "000001", "000002", "000003", "000004", "000005", - "000006", "000007", "000008", "000009", " >", " >=", - " s>u<", "s>=u<=", " s>", " s>=", "000016", "000017", - " s<u>", "s<=u>=", " <", " <=", " s<", " s<=", - "000024", "000025", " u>", " u>=", " u<", " u<=", - " !=", "000031" }; - for (Node::const_iterator NI = begin(), NE = end(); NI != NE; ++NI) { - os << names[NI->LV] << " " << NI->To - << " (" << NI->Subtree->getDFSNumIn() << "), "; - } - } - public: -#endif - - iterator begin() { return Relations.begin(); } - iterator end() { return Relations.end(); } - const_iterator begin() const { return Relations.begin(); } - const_iterator end() const { return Relations.end(); } - - iterator find(unsigned n, DomTreeDFS::Node *Subtree) { - iterator E = end(); - for (iterator I = std::lower_bound(begin(), E, n); - I != E && I->To == n; ++I) { - if (Subtree->DominatedBy(I->Subtree)) - return I; - } - return E; - } - - const_iterator find(unsigned n, DomTreeDFS::Node *Subtree) const { - const_iterator E = end(); - for (const_iterator I = std::lower_bound(begin(), E, n); - I != E && I->To == n; ++I) { - if (Subtree->DominatedBy(I->Subtree)) - return I; - } - return E; - } - - /// update - updates the lattice value for a given node, creating a new - /// entry if one doesn't exist. The new lattice value must not be - /// inconsistent with any previously existing value. - void update(unsigned n, LatticeVal R, DomTreeDFS::Node *Subtree) { - assert(validPredicate(R) && "Invalid predicate."); - - Edge edge(n, R, Subtree); - iterator B = begin(), E = end(); - iterator I = std::lower_bound(B, E, edge); - - iterator J = I; - while (J != E && J->To == n) { - if (Subtree->DominatedBy(J->Subtree)) - break; - ++J; - } - - if (J != E && J->To == n) { - edge.LV = static_cast<LatticeVal>(J->LV & R); - assert(validPredicate(edge.LV) && "Invalid union of lattice values."); - - if (edge.LV == J->LV) - return; // This update adds nothing new. - } - - if (I != B) { - // We also have to tighten any edge beneath our update. - for (iterator K = I - 1; K->To == n; --K) { - if (K->Subtree->DominatedBy(Subtree)) { - LatticeVal LV = static_cast<LatticeVal>(K->LV & edge.LV); - assert(validPredicate(LV) && "Invalid union of lattice values"); - K->LV = LV; - } - if (K == B) break; - } - } - - // Insert new edge at Subtree if it isn't already there. - if (I == E || I->To != n || Subtree != I->Subtree) - Relations.insert(I, edge); - } - }; - - private: - - std::vector<Node> Nodes; - - public: - /// node - returns the node object at a given value number. The pointer - /// returned may be invalidated on the next call to node(). - Node *node(unsigned index) { - assert(VN.value(index)); // This triggers the necessary checks. - if (Nodes.size() < index) Nodes.resize(index); - return &Nodes[index-1]; - } - - /// isRelatedBy - true iff n1 op n2 - bool isRelatedBy(unsigned n1, unsigned n2, DomTreeDFS::Node *Subtree, - LatticeVal LV) { - if (n1 == n2) return LV & EQ_BIT; - - Node *N1 = node(n1); - Node::iterator I = N1->find(n2, Subtree), E = N1->end(); - if (I != E) return (I->LV & LV) == I->LV; - - return false; - } - - // The add* methods assume that your input is logically valid and may - // assertion-fail or infinitely loop if you attempt a contradiction. - - /// addInequality - Sets n1 op n2. - /// It is also an error to call this on an inequality that is already true. - void addInequality(unsigned n1, unsigned n2, DomTreeDFS::Node *Subtree, - LatticeVal LV1) { - assert(n1 != n2 && "A node can't be inequal to itself."); - - if (LV1 != NE) - assert(!isRelatedBy(n1, n2, Subtree, reversePredicate(LV1)) && - "Contradictory inequality."); - - // Suppose we're adding %n1 < %n2. Find all the %a < %n1 and - // add %a < %n2 too. This keeps the graph fully connected. - if (LV1 != NE) { - // Break up the relationship into signed and unsigned comparison parts. - // If the signed parts of %a op1 %n1 match that of %n1 op2 %n2, and - // op1 and op2 aren't NE, then add %a op3 %n2. The new relationship - // should have the EQ_BIT iff it's set for both op1 and op2. - - unsigned LV1_s = LV1 & (SLT_BIT|SGT_BIT); - unsigned LV1_u = LV1 & (ULT_BIT|UGT_BIT); - - for (Node::iterator I = node(n1)->begin(), E = node(n1)->end(); I != E; ++I) { - if (I->LV != NE && I->To != n2) { - - DomTreeDFS::Node *Local_Subtree = NULL; - if (Subtree->DominatedBy(I->Subtree)) - Local_Subtree = Subtree; - else if (I->Subtree->DominatedBy(Subtree)) - Local_Subtree = I->Subtree; - - if (Local_Subtree) { - unsigned new_relationship = 0; - LatticeVal ILV = reversePredicate(I->LV); - unsigned ILV_s = ILV & (SLT_BIT|SGT_BIT); - unsigned ILV_u = ILV & (ULT_BIT|UGT_BIT); - - if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s) - new_relationship |= ILV_s; - if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u) - new_relationship |= ILV_u; - - if (new_relationship) { - if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0) - new_relationship |= (SLT_BIT|SGT_BIT); - if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0) - new_relationship |= (ULT_BIT|UGT_BIT); - if ((LV1 & EQ_BIT) && (ILV & EQ_BIT)) - new_relationship |= EQ_BIT; - - LatticeVal NewLV = static_cast<LatticeVal>(new_relationship); - - node(I->To)->update(n2, NewLV, Local_Subtree); - node(n2)->update(I->To, reversePredicate(NewLV), Local_Subtree); - } - } - } - } - - for (Node::iterator I = node(n2)->begin(), E = node(n2)->end(); I != E; ++I) { - if (I->LV != NE && I->To != n1) { - DomTreeDFS::Node *Local_Subtree = NULL; - if (Subtree->DominatedBy(I->Subtree)) - Local_Subtree = Subtree; - else if (I->Subtree->DominatedBy(Subtree)) - Local_Subtree = I->Subtree; - - if (Local_Subtree) { - unsigned new_relationship = 0; - unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT); - unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT); - - if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s) - new_relationship |= ILV_s; - - if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u) - new_relationship |= ILV_u; - - if (new_relationship) { - if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0) - new_relationship |= (SLT_BIT|SGT_BIT); - if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0) - new_relationship |= (ULT_BIT|UGT_BIT); - if ((LV1 & EQ_BIT) && (I->LV & EQ_BIT)) - new_relationship |= EQ_BIT; - - LatticeVal NewLV = static_cast<LatticeVal>(new_relationship); - - node(n1)->update(I->To, NewLV, Local_Subtree); - node(I->To)->update(n1, reversePredicate(NewLV), Local_Subtree); - } - } - } - } - } - - node(n1)->update(n2, LV1, Subtree); - node(n2)->update(n1, reversePredicate(LV1), Subtree); - } - - /// remove - removes a node from the graph by removing all references to - /// and from it. - void remove(unsigned n) { - Node *N = node(n); - for (Node::iterator NI = N->begin(), NE = N->end(); NI != NE; ++NI) { - Node::iterator Iter = node(NI->To)->find(n, TreeRoot); - do { - node(NI->To)->Relations.erase(Iter); - Iter = node(NI->To)->find(n, TreeRoot); - } while (Iter != node(NI->To)->end()); - } - N->Relations.clear(); - } - -#ifndef NDEBUG - virtual ~InequalityGraph() {} - virtual void dump() { - dump(*cerr.stream()); - } - - void dump(std::ostream &os) { - for (unsigned i = 1; i <= Nodes.size(); ++i) { - os << i << " = {"; - node(i)->dump(os); - os << "}\n"; - } - } -#endif - }; - - class VRPSolver; - - /// ValueRanges tracks the known integer ranges and anti-ranges of the nodes - /// in the InequalityGraph. - class VISIBILITY_HIDDEN ValueRanges { - ValueNumbering &VN; - TargetData *TD; - - class VISIBILITY_HIDDEN ScopedRange { - typedef std::vector<std::pair<DomTreeDFS::Node *, ConstantRange> > - RangeListType; - RangeListType RangeList; - - static bool swo(const std::pair<DomTreeDFS::Node *, ConstantRange> &LHS, - const std::pair<DomTreeDFS::Node *, ConstantRange> &RHS) { - return *LHS.first < *RHS.first; - } - - public: -#ifndef NDEBUG - virtual ~ScopedRange() {} - virtual void dump() const { - dump(*cerr.stream()); - } - - void dump(std::ostream &os) const { - os << "{"; - for (const_iterator I = begin(), E = end(); I != E; ++I) { - os << &I->second << " (" << I->first->getDFSNumIn() << "), "; - } - os << "}"; - } -#endif - - typedef RangeListType::iterator iterator; - typedef RangeListType::const_iterator const_iterator; - - iterator begin() { return RangeList.begin(); } - iterator end() { return RangeList.end(); } - const_iterator begin() const { return RangeList.begin(); } - const_iterator end() const { return RangeList.end(); } - - iterator find(DomTreeDFS::Node *Subtree) { - iterator E = end(); - iterator I = std::lower_bound(begin(), E, - std::make_pair(Subtree, empty), swo); - - while (I != E && !I->first->dominates(Subtree)) ++I; - return I; - } - - const_iterator find(DomTreeDFS::Node *Subtree) const { - const_iterator E = end(); - const_iterator I = std::lower_bound(begin(), E, - std::make_pair(Subtree, empty), swo); - - while (I != E && !I->first->dominates(Subtree)) ++I; - return I; - } - - void update(const ConstantRange &CR, DomTreeDFS::Node *Subtree) { - assert(!CR.isEmptySet() && "Empty ConstantRange."); - assert(!CR.isSingleElement() && "Refusing to store single element."); - - iterator E = end(); - iterator I = - std::lower_bound(begin(), E, std::make_pair(Subtree, empty), swo); - - if (I != end() && I->first == Subtree) { - ConstantRange CR2 = I->second.maximalIntersectWith(CR); - assert(!CR2.isEmptySet() && !CR2.isSingleElement() && - "Invalid union of ranges."); - I->second = CR2; - } else - RangeList.insert(I, std::make_pair(Subtree, CR)); - } - }; - - std::vector<ScopedRange> Ranges; - - void update(unsigned n, const ConstantRange &CR, DomTreeDFS::Node *Subtree){ - if (CR.isFullSet()) return; - if (Ranges.size() < n) Ranges.resize(n); - Ranges[n-1].update(CR, Subtree); - } - - /// create - Creates a ConstantRange that matches the given LatticeVal - /// relation with a given integer. - ConstantRange create(LatticeVal LV, const ConstantRange &CR) { - assert(!CR.isEmptySet() && "Can't deal with empty set."); - - if (LV == NE) - return makeConstantRange(ICmpInst::ICMP_NE, CR); - - unsigned LV_s = LV & (SGT_BIT|SLT_BIT); - unsigned LV_u = LV & (UGT_BIT|ULT_BIT); - bool hasEQ = LV & EQ_BIT; - - ConstantRange Range(CR.getBitWidth()); - - if (LV_s == SGT_BIT) { - Range = Range.maximalIntersectWith(makeConstantRange( - hasEQ ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SGT, CR)); - } else if (LV_s == SLT_BIT) { - Range = Range.maximalIntersectWith(makeConstantRange( - hasEQ ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_SLT, CR)); - } - - if (LV_u == UGT_BIT) { - Range = Range.maximalIntersectWith(makeConstantRange( - hasEQ ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_UGT, CR)); - } else if (LV_u == ULT_BIT) { - Range = Range.maximalIntersectWith(makeConstantRange( - hasEQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT, CR)); - } - - return Range; - } - - /// makeConstantRange - Creates a ConstantRange representing the set of all - /// value that match the ICmpInst::Predicate with any of the values in CR. - ConstantRange makeConstantRange(ICmpInst::Predicate ICmpOpcode, - const ConstantRange &CR) { - uint32_t W = CR.getBitWidth(); - switch (ICmpOpcode) { - default: assert(!"Invalid ICmp opcode to makeConstantRange()"); - case ICmpInst::ICMP_EQ: - return ConstantRange(CR.getLower(), CR.getUpper()); - case ICmpInst::ICMP_NE: - if (CR.isSingleElement()) - return ConstantRange(CR.getUpper(), CR.getLower()); - return ConstantRange(W); - case ICmpInst::ICMP_ULT: - return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax()); - case ICmpInst::ICMP_SLT: - return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax()); - case ICmpInst::ICMP_ULE: { - APInt UMax(CR.getUnsignedMax()); - if (UMax.isMaxValue()) - return ConstantRange(W); - return ConstantRange(APInt::getMinValue(W), UMax + 1); - } - case ICmpInst::ICMP_SLE: { - APInt SMax(CR.getSignedMax()); - if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue()) - return ConstantRange(W); - return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); - } - case ICmpInst::ICMP_UGT: - return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W)); - case ICmpInst::ICMP_SGT: - return ConstantRange(CR.getSignedMin() + 1, - APInt::getSignedMinValue(W)); - case ICmpInst::ICMP_UGE: { - APInt UMin(CR.getUnsignedMin()); - if (UMin.isMinValue()) - return ConstantRange(W); - return ConstantRange(UMin, APInt::getNullValue(W)); - } - case ICmpInst::ICMP_SGE: { - APInt SMin(CR.getSignedMin()); - if (SMin.isMinSignedValue()) - return ConstantRange(W); - return ConstantRange(SMin, APInt::getSignedMinValue(W)); - } - } - } - -#ifndef NDEBUG - bool isCanonical(Value *V, DomTreeDFS::Node *Subtree) { - return V == VN.canonicalize(V, Subtree); - } -#endif - - public: - - ValueRanges(ValueNumbering &VN, TargetData *TD) : VN(VN), TD(TD) {} - -#ifndef NDEBUG - virtual ~ValueRanges() {} - - virtual void dump() const { - dump(*cerr.stream()); - } - - void dump(std::ostream &os) const { - for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { - os << (i+1) << " = "; - Ranges[i].dump(os); - os << "\n"; - } - } -#endif - - /// range - looks up the ConstantRange associated with a value number. - ConstantRange range(unsigned n, DomTreeDFS::Node *Subtree) { - assert(VN.value(n)); // performs range checks - - if (n <= Ranges.size()) { - ScopedRange::iterator I = Ranges[n-1].find(Subtree); - if (I != Ranges[n-1].end()) return I->second; - } - - Value *V = VN.value(n); - ConstantRange CR = range(V); - return CR; - } - - /// range - determine a range from a Value without performing any lookups. - ConstantRange range(Value *V) const { - if (ConstantInt *C = dyn_cast<ConstantInt>(V)) - return ConstantRange(C->getValue()); - else if (isa<ConstantPointerNull>(V)) - return ConstantRange(APInt::getNullValue(typeToWidth(V->getType()))); - else - return ConstantRange(typeToWidth(V->getType())); - } - - // typeToWidth - returns the number of bits necessary to store a value of - // this type, or zero if unknown. - uint32_t typeToWidth(const Type *Ty) const { - if (TD) - return TD->getTypeSizeInBits(Ty); - else - return Ty->getPrimitiveSizeInBits(); - } - - static bool isRelatedBy(const ConstantRange &CR1, const ConstantRange &CR2, - LatticeVal LV) { - switch (LV) { - default: assert(!"Impossible lattice value!"); - case NE: - return CR1.maximalIntersectWith(CR2).isEmptySet(); - case ULT: - return CR1.getUnsignedMax().ult(CR2.getUnsignedMin()); - case ULE: - return CR1.getUnsignedMax().ule(CR2.getUnsignedMin()); - case UGT: - return CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()); - case UGE: - return CR1.getUnsignedMin().uge(CR2.getUnsignedMax()); - case SLT: - return CR1.getSignedMax().slt(CR2.getSignedMin()); - case SLE: - return CR1.getSignedMax().sle(CR2.getSignedMin()); - case SGT: - return CR1.getSignedMin().sgt(CR2.getSignedMax()); - case SGE: - return CR1.getSignedMin().sge(CR2.getSignedMax()); - case LT: - return CR1.getUnsignedMax().ult(CR2.getUnsignedMin()) && - CR1.getSignedMax().slt(CR2.getUnsignedMin()); - case LE: - return CR1.getUnsignedMax().ule(CR2.getUnsignedMin()) && - CR1.getSignedMax().sle(CR2.getUnsignedMin()); - case GT: - return CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()) && - CR1.getSignedMin().sgt(CR2.getSignedMax()); - case GE: - return CR1.getUnsignedMin().uge(CR2.getUnsignedMax()) && - CR1.getSignedMin().sge(CR2.getSignedMax()); - case SLTUGT: - return CR1.getSignedMax().slt(CR2.getSignedMin()) && - CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()); - case SLEUGE: - return CR1.getSignedMax().sle(CR2.getSignedMin()) && - CR1.getUnsignedMin().uge(CR2.getUnsignedMax()); - case SGTULT: - return CR1.getSignedMin().sgt(CR2.getSignedMax()) && - CR1.getUnsignedMax().ult(CR2.getUnsignedMin()); - case SGEULE: - return CR1.getSignedMin().sge(CR2.getSignedMax()) && - CR1.getUnsignedMax().ule(CR2.getUnsignedMin()); - } - } - - bool isRelatedBy(unsigned n1, unsigned n2, DomTreeDFS::Node *Subtree, - LatticeVal LV) { - ConstantRange CR1 = range(n1, Subtree); - ConstantRange CR2 = range(n2, Subtree); - - // True iff all values in CR1 are LV to all values in CR2. - return isRelatedBy(CR1, CR2, LV); - } - - void addToWorklist(Value *V, Constant *C, ICmpInst::Predicate Pred, - VRPSolver *VRP); - void markBlock(VRPSolver *VRP); - - void mergeInto(Value **I, unsigned n, unsigned New, - DomTreeDFS::Node *Subtree, VRPSolver *VRP) { - ConstantRange CR_New = range(New, Subtree); - ConstantRange Merged = CR_New; - - for (; n != 0; ++I, --n) { - unsigned i = VN.valueNumber(*I, Subtree); - ConstantRange CR_Kill = i ? range(i, Subtree) : range(*I); - if (CR_Kill.isFullSet()) continue; - Merged = Merged.maximalIntersectWith(CR_Kill); - } - - if (Merged.isFullSet() || Merged == CR_New) return; - - applyRange(New, Merged, Subtree, VRP); - } - - void applyRange(unsigned n, const ConstantRange &CR, - DomTreeDFS::Node *Subtree, VRPSolver *VRP) { - ConstantRange Merged = CR.maximalIntersectWith(range(n, Subtree)); - if (Merged.isEmptySet()) { - markBlock(VRP); - return; - } - - if (const APInt *I = Merged.getSingleElement()) { - Value *V = VN.value(n); // XXX: redesign worklist. - const Type *Ty = V->getType(); - if (Ty->isInteger()) { - addToWorklist(V, ConstantInt::get(*I), ICmpInst::ICMP_EQ, VRP); - return; - } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { - assert(*I == 0 && "Pointer is null but not zero?"); - addToWorklist(V, ConstantPointerNull::get(PTy), - ICmpInst::ICMP_EQ, VRP); - return; - } - } - - update(n, Merged, Subtree); - } - - void addNotEquals(unsigned n1, unsigned n2, DomTreeDFS::Node *Subtree, - VRPSolver *VRP) { - ConstantRange CR1 = range(n1, Subtree); - ConstantRange CR2 = range(n2, Subtree); - - uint32_t W = CR1.getBitWidth(); - - if (const APInt *I = CR1.getSingleElement()) { - if (CR2.isFullSet()) { - ConstantRange NewCR2(CR1.getUpper(), CR1.getLower()); - applyRange(n2, NewCR2, Subtree, VRP); - } else if (*I == CR2.getLower()) { - APInt NewLower(CR2.getLower() + 1), - NewUpper(CR2.getUpper()); - if (NewLower == NewUpper) - NewLower = NewUpper = APInt::getMinValue(W); - - ConstantRange NewCR2(NewLower, NewUpper); - applyRange(n2, NewCR2, Subtree, VRP); - } else if (*I == CR2.getUpper() - 1) { - APInt NewLower(CR2.getLower()), - NewUpper(CR2.getUpper() - 1); - if (NewLower == NewUpper) - NewLower = NewUpper = APInt::getMinValue(W); - - ConstantRange NewCR2(NewLower, NewUpper); - applyRange(n2, NewCR2, Subtree, VRP); - } - } - - if (const APInt *I = CR2.getSingleElement()) { - if (CR1.isFullSet()) { - ConstantRange NewCR1(CR2.getUpper(), CR2.getLower()); - applyRange(n1, NewCR1, Subtree, VRP); - } else if (*I == CR1.getLower()) { - APInt NewLower(CR1.getLower() + 1), - NewUpper(CR1.getUpper()); - if (NewLower == NewUpper) - NewLower = NewUpper = APInt::getMinValue(W); - - ConstantRange NewCR1(NewLower, NewUpper); - applyRange(n1, NewCR1, Subtree, VRP); - } else if (*I == CR1.getUpper() - 1) { - APInt NewLower(CR1.getLower()), - NewUpper(CR1.getUpper() - 1); - if (NewLower == NewUpper) - NewLower = NewUpper = APInt::getMinValue(W); - - ConstantRange NewCR1(NewLower, NewUpper); - applyRange(n1, NewCR1, Subtree, VRP); - } - } - } - - void addInequality(unsigned n1, unsigned n2, DomTreeDFS::Node *Subtree, - LatticeVal LV, VRPSolver *VRP) { - assert(!isRelatedBy(n1, n2, Subtree, LV) && "Asked to do useless work."); - - if (LV == NE) { - addNotEquals(n1, n2, Subtree, VRP); - return; - } - - ConstantRange CR1 = range(n1, Subtree); - ConstantRange CR2 = range(n2, Subtree); - - if (!CR1.isSingleElement()) { - ConstantRange NewCR1 = CR1.maximalIntersectWith(create(LV, CR2)); - if (NewCR1 != CR1) - applyRange(n1, NewCR1, Subtree, VRP); - } - - if (!CR2.isSingleElement()) { - ConstantRange NewCR2 = CR2.maximalIntersectWith( - create(reversePredicate(LV), CR1)); - if (NewCR2 != CR2) - applyRange(n2, NewCR2, Subtree, VRP); - } - } - }; - - /// UnreachableBlocks keeps tracks of blocks that are for one reason or - /// another discovered to be unreachable. This is used to cull the graph when - /// analyzing instructions, and to mark blocks with the "unreachable" - /// terminator instruction after the function has executed. - class VISIBILITY_HIDDEN UnreachableBlocks { - private: - std::vector<BasicBlock *> DeadBlocks; - - public: - /// mark - mark a block as dead - void mark(BasicBlock *BB) { - std::vector<BasicBlock *>::iterator E = DeadBlocks.end(); - std::vector<BasicBlock *>::iterator I = - std::lower_bound(DeadBlocks.begin(), E, BB); - - if (I == E || *I != BB) DeadBlocks.insert(I, BB); - } - - /// isDead - returns whether a block is known to be dead already - bool isDead(BasicBlock *BB) { - std::vector<BasicBlock *>::iterator E = DeadBlocks.end(); - std::vector<BasicBlock *>::iterator I = - std::lower_bound(DeadBlocks.begin(), E, BB); - - return I != E && *I == BB; - } - - /// kill - replace the dead blocks' terminator with an UnreachableInst. - bool kill() { - bool modified = false; - for (std::vector<BasicBlock *>::iterator I = DeadBlocks.begin(), - E = DeadBlocks.end(); I != E; ++I) { - BasicBlock *BB = *I; - - DOUT << "unreachable block: " << BB->getName() << "\n"; - - for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); - SI != SE; ++SI) { - BasicBlock *Succ = *SI; - Succ->removePredecessor(BB); - } - - TerminatorInst *TI = BB->getTerminator(); - TI->replaceAllUsesWith(UndefValue::get(TI->getType())); - TI->eraseFromParent(); - new UnreachableInst(BB); - ++NumBlocks; - modified = true; - } - DeadBlocks.clear(); - return modified; - } - }; - - /// VRPSolver keeps track of how changes to one variable affect other - /// variables, and forwards changes along to the InequalityGraph. It - /// also maintains the correct choice for "canonical" in the IG. - /// @brief VRPSolver calculates inferences from a new relationship. - class VISIBILITY_HIDDEN VRPSolver { - private: - friend class ValueRanges; - - struct Operation { - Value *LHS, *RHS; - ICmpInst::Predicate Op; - - BasicBlock *ContextBB; // XXX use a DomTreeDFS::Node instead - Instruction *ContextInst; - }; - std::deque<Operation> WorkList; - - ValueNumbering &VN; - InequalityGraph &IG; - UnreachableBlocks &UB; - ValueRanges &VR; - DomTreeDFS *DTDFS; - DomTreeDFS::Node *Top; - BasicBlock *TopBB; - Instruction *TopInst; - bool &modified; - - typedef InequalityGraph::Node Node; - - // below - true if the Instruction is dominated by the current context - // block or instruction - bool below(Instruction *I) { - BasicBlock *BB = I->getParent(); - if (TopInst && TopInst->getParent() == BB) { - if (isa<TerminatorInst>(TopInst)) return false; - if (isa<TerminatorInst>(I)) return true; - if ( isa<PHINode>(TopInst) && !isa<PHINode>(I)) return true; - if (!isa<PHINode>(TopInst) && isa<PHINode>(I)) return false; - - for (BasicBlock::const_iterator Iter = BB->begin(), E = BB->end(); - Iter != E; ++Iter) { - if (&*Iter == TopInst) return true; - else if (&*Iter == I) return false; - } - assert(!"Instructions not found in parent BasicBlock?"); - } else { - DomTreeDFS::Node *Node = DTDFS->getNodeForBlock(BB); - if (!Node) return false; - return Top->dominates(Node); - } - return false; // Not reached - } - - // aboveOrBelow - true if the Instruction either dominates or is dominated - // by the current context block or instruction - bool aboveOrBelow(Instruction *I) { - BasicBlock *BB = I->getParent(); - DomTreeDFS::Node *Node = DTDFS->getNodeForBlock(BB); - if (!Node) return false; - - return Top == Node || Top->dominates(Node) || Node->dominates(Top); - } - - bool makeEqual(Value *V1, Value *V2) { - DOUT << "makeEqual(" << *V1 << ", " << *V2 << ")\n"; - DOUT << "context is "; - if (TopInst) DOUT << "I: " << *TopInst << "\n"; - else DOUT << "BB: " << TopBB->getName() - << "(" << Top->getDFSNumIn() << ")\n"; - - assert(V1->getType() == V2->getType() && - "Can't make two values with different types equal."); - - if (V1 == V2) return true; - - if (isa<Constant>(V1) && isa<Constant>(V2)) - return false; - - unsigned n1 = VN.valueNumber(V1, Top), n2 = VN.valueNumber(V2, Top); - - if (n1 && n2) { - if (n1 == n2) return true; - if (IG.isRelatedBy(n1, n2, Top, NE)) return false; - } - - if (n1) assert(V1 == VN.value(n1) && "Value isn't canonical."); - if (n2) assert(V2 == VN.value(n2) && "Value isn't canonical."); - - assert(!VN.compare(V2, V1) && "Please order parameters to makeEqual."); - - assert(!isa<Constant>(V2) && "Tried to remove a constant."); - - SetVector<unsigned> Remove; - if (n2) Remove.insert(n2); - - if (n1 && n2) { - // Suppose we're being told that %x == %y, and %x <= %z and %y >= %z. - // We can't just merge %x and %y because the relationship with %z would - // be EQ and that's invalid. What we're doing is looking for any nodes - // %z such that %x <= %z and %y >= %z, and vice versa. - - Node::iterator end = IG.node(n2)->end(); - - // Find the intersection between N1 and N2 which is dominated by - // Top. If we find %x where N1 <= %x <= N2 (or >=) then add %x to - // Remove. - for (Node::iterator I = IG.node(n1)->begin(), E = IG.node(n1)->end(); - I != E; ++I) { - if (!(I->LV & EQ_BIT) || !Top->DominatedBy(I->Subtree)) continue; - - unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT); - unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT); - Node::iterator NI = IG.node(n2)->find(I->To, Top); - if (NI != end) { - LatticeVal NILV = reversePredicate(NI->LV); - unsigned NILV_s = NILV & (SLT_BIT|SGT_BIT); - unsigned NILV_u = NILV & (ULT_BIT|UGT_BIT); - - if ((ILV_s != (SLT_BIT|SGT_BIT) && ILV_s == NILV_s) || - (ILV_u != (ULT_BIT|UGT_BIT) && ILV_u == NILV_u)) - Remove.insert(I->To); - } - } - - // See if one of the nodes about to be removed is actually a better - // canonical choice than n1. - unsigned orig_n1 = n1; - SetVector<unsigned>::iterator DontRemove = Remove.end(); - for (SetVector<unsigned>::iterator I = Remove.begin()+1 /* skip n2 */, - E = Remove.end(); I != E; ++I) { - unsigned n = *I; - Value *V = VN.value(n); - if (VN.compare(V, V1)) { - V1 = V; - n1 = n; - DontRemove = I; - } - } - if (DontRemove != Remove.end()) { - unsigned n = *DontRemove; - Remove.remove(n); - Remove.insert(orig_n1); - } - } - - // We'd like to allow makeEqual on two values to perform a simple - // substitution without creating nodes in the IG whenever possible. - // - // The first iteration through this loop operates on V2 before going - // through the Remove list and operating on those too. If all of the - // iterations performed simple replacements then we exit early. - bool mergeIGNode = false; - unsigned i = 0; - for (Value *R = V2; i == 0 || i < Remove.size(); ++i) { - if (i) R = VN.value(Remove[i]); // skip n2. - - // Try to replace the whole instruction. If we can, we're done. - Instruction *I2 = dyn_cast<Instruction>(R); - if (I2 && below(I2)) { - std::vector<Instruction *> ToNotify; - for (Value::use_iterator UI = I2->use_begin(), UE = I2->use_end(); - UI != UE;) { - Use &TheUse = UI.getUse(); - ++UI; - Instruction *I = cast<Instruction>(TheUse.getUser()); - ToNotify.push_back(I); - } - - DOUT << "Simply removing " << *I2 - << ", replacing with " << *V1 << "\n"; - I2->replaceAllUsesWith(V1); - // leave it dead; it'll get erased later. - ++NumInstruction; - modified = true; - - for (std::vector<Instruction *>::iterator II = ToNotify.begin(), - IE = ToNotify.end(); II != IE; ++II) { - opsToDef(*II); - } - - continue; - } - - // Otherwise, replace all dominated uses. - for (Value::use_iterator UI = R->use_begin(), UE = R->use_end(); - UI != UE;) { - Use &TheUse = UI.getUse(); - ++UI; - if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { - if (below(I)) { - TheUse.set(V1); - modified = true; - ++NumVarsReplaced; - opsToDef(I); - } - } - } - - // If that killed the instruction, stop here. - if (I2 && isInstructionTriviallyDead(I2)) { - DOUT << "Killed all uses of " << *I2 - << ", replacing with " << *V1 << "\n"; - continue; - } - - // If we make it to here, then we will need to create a node for N1. - // Otherwise, we can skip out early! - mergeIGNode = true; - } - - if (!isa<Constant>(V1)) { - if (Remove.empty()) { - VR.mergeInto(&V2, 1, VN.getOrInsertVN(V1, Top), Top, this); - } else { - std::vector<Value*> RemoveVals; - RemoveVals.reserve(Remove.size()); - - for (SetVector<unsigned>::iterator I = Remove.begin(), - E = Remove.end(); I != E; ++I) { - Value *V = VN.value(*I); - if (!V->use_empty()) - RemoveVals.push_back(V); - } - VR.mergeInto(&RemoveVals[0], RemoveVals.size(), - VN.getOrInsertVN(V1, Top), Top, this); - } - } - - if (mergeIGNode) { - // Create N1. - if (!n1) n1 = VN.getOrInsertVN(V1, Top); - IG.node(n1); // Ensure that IG.Nodes won't get resized - - // Migrate relationships from removed nodes to N1. - for (SetVector<unsigned>::iterator I = Remove.begin(), E = Remove.end(); - I != E; ++I) { - unsigned n = *I; - for (Node::iterator NI = IG.node(n)->begin(), NE = IG.node(n)->end(); - NI != NE; ++NI) { - if (NI->Subtree->DominatedBy(Top)) { - if (NI->To == n1) { - assert((NI->LV & EQ_BIT) && "Node inequal to itself."); - continue; - } - if (Remove.count(NI->To)) - continue; - - IG.node(NI->To)->update(n1, reversePredicate(NI->LV), Top); - IG.node(n1)->update(NI->To, NI->LV, Top); - } - } - } - - // Point V2 (and all items in Remove) to N1. - if (!n2) - VN.addEquality(n1, V2, Top); - else { - for (SetVector<unsigned>::iterator I = Remove.begin(), - E = Remove.end(); I != E; ++I) { - VN.addEquality(n1, VN.value(*I), Top); - } - } - - // If !Remove.empty() then V2 = Remove[0]->getValue(). - // Even when Remove is empty, we still want to process V2. - i = 0; - for (Value *R = V2; i == 0 || i < Remove.size(); ++i) { - if (i) R = VN.value(Remove[i]); // skip n2. - - if (Instruction *I2 = dyn_cast<Instruction>(R)) { - if (aboveOrBelow(I2)) - defToOps(I2); - } - for (Value::use_iterator UI = V2->use_begin(), UE = V2->use_end(); - UI != UE;) { - Use &TheUse = UI.getUse(); - ++UI; - if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { - if (aboveOrBelow(I)) - opsToDef(I); - } - } - } - } - - // re-opsToDef all dominated users of V1. - if (Instruction *I = dyn_cast<Instruction>(V1)) { - for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); - UI != UE;) { - Use &TheUse = UI.getUse(); - ++UI; - Value *V = TheUse.getUser(); - if (!V->use_empty()) { - Instruction *Inst = cast<Instruction>(V); - if (aboveOrBelow(Inst)) - opsToDef(Inst); - } - } - } - - return true; - } - - /// cmpInstToLattice - converts an CmpInst::Predicate to lattice value - /// Requires that the lattice value be valid; does not accept ICMP_EQ. - static LatticeVal cmpInstToLattice(ICmpInst::Predicate Pred) { - switch (Pred) { - case ICmpInst::ICMP_EQ: - assert(!"No matching lattice value."); - return static_cast<LatticeVal>(EQ_BIT); - default: - assert(!"Invalid 'icmp' predicate."); - case ICmpInst::ICMP_NE: - return NE; - case ICmpInst::ICMP_UGT: - return UGT; - case ICmpInst::ICMP_UGE: - return UGE; - case ICmpInst::ICMP_ULT: - return ULT; - case ICmpInst::ICMP_ULE: - return ULE; - case ICmpInst::ICMP_SGT: - return SGT; - case ICmpInst::ICMP_SGE: - return SGE; - case ICmpInst::ICMP_SLT: - return SLT; - case ICmpInst::ICMP_SLE: - return SLE; - } - } - - public: - VRPSolver(ValueNumbering &VN, InequalityGraph &IG, UnreachableBlocks &UB, - ValueRanges &VR, DomTreeDFS *DTDFS, bool &modified, - BasicBlock *TopBB) - : VN(VN), - IG(IG), - UB(UB), - VR(VR), - DTDFS(DTDFS), - Top(DTDFS->getNodeForBlock(TopBB)), - TopBB(TopBB), - TopInst(NULL), - modified(modified) - { - assert(Top && "VRPSolver created for unreachable basic block."); - } - - VRPSolver(ValueNumbering &VN, InequalityGraph &IG, UnreachableBlocks &UB, - ValueRanges &VR, DomTreeDFS *DTDFS, bool &modified, - Instruction *TopInst) - : VN(VN), - IG(IG), - UB(UB), - VR(VR), - DTDFS(DTDFS), - Top(DTDFS->getNodeForBlock(TopInst->getParent())), - TopBB(TopInst->getParent()), - TopInst(TopInst), - modified(modified) - { - assert(Top && "VRPSolver created for unreachable basic block."); - assert(Top->getBlock() == TopInst->getParent() && "Context mismatch."); - } - - bool isRelatedBy(Value *V1, Value *V2, ICmpInst::Predicate Pred) const { - if (Constant *C1 = dyn_cast<Constant>(V1)) - if (Constant *C2 = dyn_cast<Constant>(V2)) - return ConstantExpr::getCompare(Pred, C1, C2) == - ConstantInt::getTrue(); - - unsigned n1 = VN.valueNumber(V1, Top); - unsigned n2 = VN.valueNumber(V2, Top); - - if (n1 && n2) { - if (n1 == n2) return Pred == ICmpInst::ICMP_EQ || - Pred == ICmpInst::ICMP_ULE || - Pred == ICmpInst::ICMP_UGE || - Pred == ICmpInst::ICMP_SLE || - Pred == ICmpInst::ICMP_SGE; - if (Pred == ICmpInst::ICMP_EQ) return false; - if (IG.isRelatedBy(n1, n2, Top, cmpInstToLattice(Pred))) return true; - if (VR.isRelatedBy(n1, n2, Top, cmpInstToLattice(Pred))) return true; - } - - if ((n1 && !n2 && isa<Constant>(V2)) || - (n2 && !n1 && isa<Constant>(V1))) { - ConstantRange CR1 = n1 ? VR.range(n1, Top) : VR.range(V1); - ConstantRange CR2 = n2 ? VR.range(n2, Top) : VR.range(V2); - - if (Pred == ICmpInst::ICMP_EQ) - return CR1.isSingleElement() && - CR1.getSingleElement() == CR2.getSingleElement(); - - return VR.isRelatedBy(CR1, CR2, cmpInstToLattice(Pred)); - } - if (Pred == ICmpInst::ICMP_EQ) return V1 == V2; - return false; - } - - /// add - adds a new property to the work queue - void add(Value *V1, Value *V2, ICmpInst::Predicate Pred, - Instruction *I = NULL) { - DOUT << "adding " << *V1 << " " << Pred << " " << *V2; - if (I) DOUT << " context: " << *I; - else DOUT << " default context (" << Top->getDFSNumIn() << ")"; - DOUT << "\n"; - - assert(V1->getType() == V2->getType() && - "Can't relate two values with different types."); - - WorkList.push_back(Operation()); - Operation &O = WorkList.back(); - O.LHS = V1, O.RHS = V2, O.Op = Pred, O.ContextInst = I; - O.ContextBB = I ? I->getParent() : TopBB; - } - - /// defToOps - Given an instruction definition that we've learned something - /// new about, find any new relationships between its operands. - void defToOps(Instruction *I) { - Instruction *NewContext = below(I) ? I : TopInst; - Value *Canonical = VN.canonicalize(I, Top); - - if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { - const Type *Ty = BO->getType(); - assert(!Ty->isFPOrFPVector() && "Float in work queue!"); - - Value *Op0 = VN.canonicalize(BO->getOperand(0), Top); - Value *Op1 = VN.canonicalize(BO->getOperand(1), Top); - - // TODO: "and i32 -1, %x" EQ %y then %x EQ %y. - - switch (BO->getOpcode()) { - case Instruction::And: { - // "and i32 %a, %b" EQ -1 then %a EQ -1 and %b EQ -1 - ConstantInt *CI = ConstantInt::getAllOnesValue(Ty); - if (Canonical == CI) { - add(CI, Op0, ICmpInst::ICMP_EQ, NewContext); - add(CI, Op1, ICmpInst::ICMP_EQ, NewContext); - } - } break; - case Instruction::Or: { - // "or i32 %a, %b" EQ 0 then %a EQ 0 and %b EQ 0 - Constant *Zero = Constant::getNullValue(Ty); - if (Canonical == Zero) { - add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext); - add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext); - } - } break; - case Instruction::Xor: { - // "xor i32 %c, %a" EQ %b then %a EQ %c ^ %b - // "xor i32 %c, %a" EQ %c then %a EQ 0 - // "xor i32 %c, %a" NE %c then %a NE 0 - // Repeat the above, with order of operands reversed. - Value *LHS = Op0; - Value *RHS = Op1; - if (!isa<Constant>(LHS)) std::swap(LHS, RHS); - - if (ConstantInt *CI = dyn_cast<ConstantInt>(Canonical)) { - if (ConstantInt *Arg = dyn_cast<ConstantInt>(LHS)) { - add(RHS, ConstantInt::get(CI->getValue() ^ Arg->getValue()), - ICmpInst::ICMP_EQ, NewContext); - } - } - if (Canonical == LHS) { - if (isa<ConstantInt>(Canonical)) - add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ, - NewContext); - } else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) { - add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE, - NewContext); - } - } break; - default: - break; - } - } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) { - // "icmp ult i32 %a, %y" EQ true then %a u< y - // etc. - - if (Canonical == ConstantInt::getTrue()) { - add(IC->getOperand(0), IC->getOperand(1), IC->getPredicate(), - NewContext); - } else if (Canonical == ConstantInt::getFalse()) { - add(IC->getOperand(0), IC->getOperand(1), - ICmpInst::getInversePredicate(IC->getPredicate()), NewContext); - } - } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { - if (I->getType()->isFPOrFPVector()) return; - - // Given: "%a = select i1 %x, i32 %b, i32 %c" - // %a EQ %b and %b NE %c then %x EQ true - // %a EQ %c and %b NE %c then %x EQ false - - Value *True = SI->getTrueValue(); - Value *False = SI->getFalseValue(); - if (isRelatedBy(True, False, ICmpInst::ICMP_NE)) { - if (Canonical == VN.canonicalize(True, Top) || - isRelatedBy(Canonical, False, ICmpInst::ICMP_NE)) - add(SI->getCondition(), ConstantInt::getTrue(), - ICmpInst::ICMP_EQ, NewContext); - else if (Canonical == VN.canonicalize(False, Top) || - isRelatedBy(Canonical, True, ICmpInst::ICMP_NE)) - add(SI->getCondition(), ConstantInt::getFalse(), - ICmpInst::ICMP_EQ, NewContext); - } - } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { - for (GetElementPtrInst::op_iterator OI = GEPI->idx_begin(), - OE = GEPI->idx_end(); OI != OE; ++OI) { - ConstantInt *Op = dyn_cast<ConstantInt>(VN.canonicalize(*OI, Top)); - if (!Op || !Op->isZero()) return; - } - // TODO: The GEPI indices are all zero. Copy from definition to operand, - // jumping the type plane as needed. - if (isRelatedBy(GEPI, Constant::getNullValue(GEPI->getType()), - ICmpInst::ICMP_NE)) { - Value *Ptr = GEPI->getPointerOperand(); - add(Ptr, Constant::getNullValue(Ptr->getType()), ICmpInst::ICMP_NE, - NewContext); - } - } else if (CastInst *CI = dyn_cast<CastInst>(I)) { - const Type *SrcTy = CI->getSrcTy(); - - unsigned ci = VN.getOrInsertVN(CI, Top); - uint32_t W = VR.typeToWidth(SrcTy); - if (!W) return; - ConstantRange CR = VR.range(ci, Top); - - if (CR.isFullSet()) return; - - switch (CI->getOpcode()) { - default: break; - case Instruction::ZExt: - case Instruction::SExt: - VR.applyRange(VN.getOrInsertVN(CI->getOperand(0), Top), - CR.truncate(W), Top, this); - break; - case Instruction::BitCast: - VR.applyRange(VN.getOrInsertVN(CI->getOperand(0), Top), - CR, Top, this); - break; - } - } - } - - /// opsToDef - A new relationship was discovered involving one of this - /// instruction's operands. Find any new relationship involving the - /// definition, or another operand. - void opsToDef(Instruction *I) { - Instruction *NewContext = below(I) ? I : TopInst; - - if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { - Value *Op0 = VN.canonicalize(BO->getOperand(0), Top); - Value *Op1 = VN.canonicalize(BO->getOperand(1), Top); - - if (ConstantInt *CI0 = dyn_cast<ConstantInt>(Op0)) - if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Op1)) { - add(BO, ConstantExpr::get(BO->getOpcode(), CI0, CI1), - ICmpInst::ICMP_EQ, NewContext); - return; - } - - // "%y = and i1 true, %x" then %x EQ %y - // "%y = or i1 false, %x" then %x EQ %y - // "%x = add i32 %y, 0" then %x EQ %y - // "%x = mul i32 %y, 0" then %x EQ 0 - - Instruction::BinaryOps Opcode = BO->getOpcode(); - const Type *Ty = BO->getType(); - assert(!Ty->isFPOrFPVector() && "Float in work queue!"); - - Constant *Zero = Constant::getNullValue(Ty); - Constant *One = ConstantInt::get(Ty, 1); - ConstantInt *AllOnes = ConstantInt::getAllOnesValue(Ty); - - switch (Opcode) { - default: break; - case Instruction::LShr: - case Instruction::AShr: - case Instruction::Shl: - if (Op1 == Zero) { - add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); - return; - } - break; - case Instruction::Sub: - if (Op1 == Zero) { - add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); - return; - } - if (ConstantInt *CI0 = dyn_cast<ConstantInt>(Op0)) { - unsigned n_ci0 = VN.getOrInsertVN(Op1, Top); - ConstantRange CR = VR.range(n_ci0, Top); - if (!CR.isFullSet()) { - CR.subtract(CI0->getValue()); - unsigned n_bo = VN.getOrInsertVN(BO, Top); - VR.applyRange(n_bo, CR, Top, this); - return; - } - } - if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Op1)) { - unsigned n_ci1 = VN.getOrInsertVN(Op0, Top); - ConstantRange CR = VR.range(n_ci1, Top); - if (!CR.isFullSet()) { - CR.subtract(CI1->getValue()); - unsigned n_bo = VN.getOrInsertVN(BO, Top); - VR.applyRange(n_bo, CR, Top, this); - return; - } - } - break; - case Instruction::Or: - if (Op0 == AllOnes || Op1 == AllOnes) { - add(BO, AllOnes, ICmpInst::ICMP_EQ, NewContext); - return; - } - if (Op0 == Zero) { - add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); - return; - } else if (Op1 == Zero) { - add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); - return; - } - break; - case Instruction::Add: - if (ConstantInt *CI0 = dyn_cast<ConstantInt>(Op0)) { - unsigned n_ci0 = VN.getOrInsertVN(Op1, Top); - ConstantRange CR = VR.range(n_ci0, Top); - if (!CR.isFullSet()) { - CR.subtract(-CI0->getValue()); - unsigned n_bo = VN.getOrInsertVN(BO, Top); - VR.applyRange(n_bo, CR, Top, this); - return; - } - } - if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Op1)) { - unsigned n_ci1 = VN.getOrInsertVN(Op0, Top); - ConstantRange CR = VR.range(n_ci1, Top); - if (!CR.isFullSet()) { - CR.subtract(-CI1->getValue()); - unsigned n_bo = VN.getOrInsertVN(BO, Top); - VR.applyRange(n_bo, CR, Top, this); - return; - } - } - // fall-through - case Instruction::Xor: - if (Op0 == Zero) { - add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); - return; - } else if (Op1 == Zero) { - add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); - return; - } - break; - case Instruction::And: - if (Op0 == AllOnes) { - add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); - return; - } else if (Op1 == AllOnes) { - add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); - return; - } - if (Op0 == Zero || Op1 == Zero) { - add(BO, Zero, ICmpInst::ICMP_EQ, NewContext); - return; - } - break; - case Instruction::Mul: - if (Op0 == Zero || Op1 == Zero) { - add(BO, Zero, ICmpInst::ICMP_EQ, NewContext); - return; - } - if (Op0 == One) { - add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); - return; - } else if (Op1 == One) { - add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); - return; - } - break; - } - - // "%x = add i32 %y, %z" and %x EQ %y then %z EQ 0 - // "%x = add i32 %y, %z" and %x EQ %z then %y EQ 0 - // "%x = shl i32 %y, %z" and %x EQ %y and %y NE 0 then %z EQ 0 - // "%x = udiv i32 %y, %z" and %x EQ %y and %y NE 0 then %z EQ 1 - - Value *Known = Op0, *Unknown = Op1, - *TheBO = VN.canonicalize(BO, Top); - if (Known != TheBO) std::swap(Known, Unknown); - if (Known == TheBO) { - switch (Opcode) { - default: break; - case Instruction::LShr: - case Instruction::AShr: - case Instruction::Shl: - if (!isRelatedBy(Known, Zero, ICmpInst::ICMP_NE)) break; - // otherwise, fall-through. - case Instruction::Sub: - if (Unknown == Op0) break; - // otherwise, fall-through. - case Instruction::Xor: - case Instruction::Add: - add(Unknown, Zero, ICmpInst::ICMP_EQ, NewContext); - break; - case Instruction::UDiv: - case Instruction::SDiv: - if (Unknown == Op1) break; - if (isRelatedBy(Known, Zero, ICmpInst::ICMP_NE)) - add(Unknown, One, ICmpInst::ICMP_EQ, NewContext); - break; - } - } - - // TODO: "%a = add i32 %b, 1" and %b > %z then %a >= %z. - - } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) { - // "%a = icmp ult i32 %b, %c" and %b u< %c then %a EQ true - // "%a = icmp ult i32 %b, %c" and %b u>= %c then %a EQ false - // etc. - - Value *Op0 = VN.canonicalize(IC->getOperand(0), Top); - Value *Op1 = VN.canonicalize(IC->getOperand(1), Top); - - ICmpInst::Predicate Pred = IC->getPredicate(); - if (isRelatedBy(Op0, Op1, Pred)) - add(IC, ConstantInt::getTrue(), ICmpInst::ICMP_EQ, NewContext); - else if (isRelatedBy(Op0, Op1, ICmpInst::getInversePredicate(Pred))) - add(IC, ConstantInt::getFalse(), ICmpInst::ICMP_EQ, NewContext); - - } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { - if (I->getType()->isFPOrFPVector()) return; - - // Given: "%a = select i1 %x, i32 %b, i32 %c" - // %x EQ true then %a EQ %b - // %x EQ false then %a EQ %c - // %b EQ %c then %a EQ %b - - Value *Canonical = VN.canonicalize(SI->getCondition(), Top); - if (Canonical == ConstantInt::getTrue()) { - add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext); - } else if (Canonical == ConstantInt::getFalse()) { - add(SI, SI->getFalseValue(), ICmpInst::ICMP_EQ, NewContext); - } else if (VN.canonicalize(SI->getTrueValue(), Top) == - VN.canonicalize(SI->getFalseValue(), Top)) { - add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext); - } - } else if (CastInst *CI = dyn_cast<CastInst>(I)) { - const Type *DestTy = CI->getDestTy(); - if (DestTy->isFPOrFPVector()) return; - - Value *Op = VN.canonicalize(CI->getOperand(0), Top); - Instruction::CastOps Opcode = CI->getOpcode(); - - if (Constant *C = dyn_cast<Constant>(Op)) { - add(CI, ConstantExpr::getCast(Opcode, C, DestTy), - ICmpInst::ICMP_EQ, NewContext); - } - - uint32_t W = VR.typeToWidth(DestTy); - unsigned ci = VN.getOrInsertVN(CI, Top); - ConstantRange CR = VR.range(VN.getOrInsertVN(Op, Top), Top); - - if (!CR.isFullSet()) { - switch (Opcode) { - default: break; - case Instruction::ZExt: - VR.applyRange(ci, CR.zeroExtend(W), Top, this); - break; - case Instruction::SExt: - VR.applyRange(ci, CR.signExtend(W), Top, this); - break; - case Instruction::Trunc: { - ConstantRange Result = CR.truncate(W); - if (!Result.isFullSet()) - VR.applyRange(ci, Result, Top, this); - } break; - case Instruction::BitCast: - VR.applyRange(ci, CR, Top, this); - break; - // TODO: other casts? - } - } - } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { - for (GetElementPtrInst::op_iterator OI = GEPI->idx_begin(), - OE = GEPI->idx_end(); OI != OE; ++OI) { - ConstantInt *Op = dyn_cast<ConstantInt>(VN.canonicalize(*OI, Top)); - if (!Op || !Op->isZero()) return; - } - // TODO: The GEPI indices are all zero. Copy from operand to definition, - // jumping the type plane as needed. - Value *Ptr = GEPI->getPointerOperand(); - if (isRelatedBy(Ptr, Constant::getNullValue(Ptr->getType()), - ICmpInst::ICMP_NE)) { - add(GEPI, Constant::getNullValue(GEPI->getType()), ICmpInst::ICMP_NE, - NewContext); - } - } - } - - /// solve - process the work queue - void solve() { - //DOUT << "WorkList entry, size: " << WorkList.size() << "\n"; - while (!WorkList.empty()) { - //DOUT << "WorkList size: " << WorkList.size() << "\n"; - - Operation &O = WorkList.front(); - TopInst = O.ContextInst; - TopBB = O.ContextBB; - Top = DTDFS->getNodeForBlock(TopBB); // XXX move this into Context - - O.LHS = VN.canonicalize(O.LHS, Top); - O.RHS = VN.canonicalize(O.RHS, Top); - - assert(O.LHS == VN.canonicalize(O.LHS, Top) && "Canonicalize isn't."); - assert(O.RHS == VN.canonicalize(O.RHS, Top) && "Canonicalize isn't."); - - DOUT << "solving " << *O.LHS << " " << O.Op << " " << *O.RHS; - if (O.ContextInst) DOUT << " context inst: " << *O.ContextInst; - else DOUT << " context block: " << O.ContextBB->getName(); - DOUT << "\n"; - - DEBUG(VN.dump()); - DEBUG(IG.dump()); - DEBUG(VR.dump()); - - // If they're both Constant, skip it. Check for contradiction and mark - // the BB as unreachable if so. - if (Constant *CI_L = dyn_cast<Constant>(O.LHS)) { - if (Constant *CI_R = dyn_cast<Constant>(O.RHS)) { - if (ConstantExpr::getCompare(O.Op, CI_L, CI_R) == - ConstantInt::getFalse()) - UB.mark(TopBB); - - WorkList.pop_front(); - continue; - } - } - - if (VN.compare(O.LHS, O.RHS)) { - std::swap(O.LHS, O.RHS); - O.Op = ICmpInst::getSwappedPredicate(O.Op); - } - - if (O.Op == ICmpInst::ICMP_EQ) { - if (!makeEqual(O.RHS, O.LHS)) - UB.mark(TopBB); - } else { - LatticeVal LV = cmpInstToLattice(O.Op); - - if ((LV & EQ_BIT) && - isRelatedBy(O.LHS, O.RHS, ICmpInst::getSwappedPredicate(O.Op))) { - if (!makeEqual(O.RHS, O.LHS)) - UB.mark(TopBB); - } else { - if (isRelatedBy(O.LHS, O.RHS, ICmpInst::getInversePredicate(O.Op))){ - UB.mark(TopBB); - WorkList.pop_front(); - continue; - } - - unsigned n1 = VN.getOrInsertVN(O.LHS, Top); - unsigned n2 = VN.getOrInsertVN(O.RHS, Top); - - if (n1 == n2) { - if (O.Op != ICmpInst::ICMP_UGE && O.Op != ICmpInst::ICMP_ULE && - O.Op != ICmpInst::ICMP_SGE && O.Op != ICmpInst::ICMP_SLE) - UB.mark(TopBB); - - WorkList.pop_front(); - continue; - } - - if (VR.isRelatedBy(n1, n2, Top, LV) || - IG.isRelatedBy(n1, n2, Top, LV)) { - WorkList.pop_front(); - continue; - } - - VR.addInequality(n1, n2, Top, LV, this); - if ((!isa<ConstantInt>(O.RHS) && !isa<ConstantInt>(O.LHS)) || - LV == NE) - IG.addInequality(n1, n2, Top, LV); - - if (Instruction *I1 = dyn_cast<Instruction>(O.LHS)) { - if (aboveOrBelow(I1)) - defToOps(I1); - } - if (isa<Instruction>(O.LHS) || isa<Argument>(O.LHS)) { - for (Value::use_iterator UI = O.LHS->use_begin(), - UE = O.LHS->use_end(); UI != UE;) { - Use &TheUse = UI.getUse(); - ++UI; - Instruction *I = cast<Instruction>(TheUse.getUser()); - if (aboveOrBelow(I)) - opsToDef(I); - } - } - if (Instruction *I2 = dyn_cast<Instruction>(O.RHS)) { - if (aboveOrBelow(I2)) - defToOps(I2); - } - if (isa<Instruction>(O.RHS) || isa<Argument>(O.RHS)) { - for (Value::use_iterator UI = O.RHS->use_begin(), - UE = O.RHS->use_end(); UI != UE;) { - Use &TheUse = UI.getUse(); - ++UI; - Instruction *I = cast<Instruction>(TheUse.getUser()); - if (aboveOrBelow(I)) - opsToDef(I); - } - } - } - } - WorkList.pop_front(); - } - } - }; - - void ValueRanges::addToWorklist(Value *V, Constant *C, - ICmpInst::Predicate Pred, VRPSolver *VRP) { - VRP->add(V, C, Pred, VRP->TopInst); - } - - void ValueRanges::markBlock(VRPSolver *VRP) { - VRP->UB.mark(VRP->TopBB); - } - - /// PredicateSimplifier - This class is a simplifier that replaces - /// one equivalent variable with another. It also tracks what - /// can't be equal and will solve setcc instructions when possible. - /// @brief Root of the predicate simplifier optimization. - class VISIBILITY_HIDDEN PredicateSimplifier : public FunctionPass { - DomTreeDFS *DTDFS; - bool modified; - ValueNumbering *VN; - InequalityGraph *IG; - UnreachableBlocks UB; - ValueRanges *VR; - - std::vector<DomTreeDFS::Node *> WorkList; - - public: - static char ID; // Pass identification, replacement for typeid - PredicateSimplifier() : FunctionPass(&ID) {} - - bool runOnFunction(Function &F); - - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(BreakCriticalEdgesID); - AU.addRequired<DominatorTree>(); - AU.addRequired<TargetData>(); - AU.addPreserved<TargetData>(); - } - - private: - /// Forwards - Adds new properties to VRPSolver and uses them to - /// simplify instructions. Because new properties sometimes apply to - /// a transition from one BasicBlock to another, this will use the - /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the - /// basic block. - /// @brief Performs abstract execution of the program. - class VISIBILITY_HIDDEN Forwards : public InstVisitor<Forwards> { - friend class InstVisitor<Forwards>; - PredicateSimplifier *PS; - DomTreeDFS::Node *DTNode; - - public: - ValueNumbering &VN; - InequalityGraph &IG; - UnreachableBlocks &UB; - ValueRanges &VR; - - Forwards(PredicateSimplifier *PS, DomTreeDFS::Node *DTNode) - : PS(PS), DTNode(DTNode), VN(*PS->VN), IG(*PS->IG), UB(PS->UB), - VR(*PS->VR) {} - - void visitTerminatorInst(TerminatorInst &TI); - void visitBranchInst(BranchInst &BI); - void visitSwitchInst(SwitchInst &SI); - - void visitAllocaInst(AllocaInst &AI); - void visitLoadInst(LoadInst &LI); - void visitStoreInst(StoreInst &SI); - - void visitSExtInst(SExtInst &SI); - void visitZExtInst(ZExtInst &ZI); - - void visitBinaryOperator(BinaryOperator &BO); - void visitICmpInst(ICmpInst &IC); - }; - - // Used by terminator instructions to proceed from the current basic - // block to the next. Verifies that "current" dominates "next", - // then calls visitBasicBlock. - void proceedToSuccessors(DomTreeDFS::Node *Current) { - for (DomTreeDFS::Node::iterator I = Current->begin(), - E = Current->end(); I != E; ++I) { - WorkList.push_back(*I); - } - } - - void proceedToSuccessor(DomTreeDFS::Node *Next) { - WorkList.push_back(Next); - } - - // Visits each instruction in the basic block. - void visitBasicBlock(DomTreeDFS::Node *Node) { - BasicBlock *BB = Node->getBlock(); - DOUT << "Entering Basic Block: " << BB->getName() - << " (" << Node->getDFSNumIn() << ")\n"; - for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { - visitInstruction(I++, Node); - } - } - - // Tries to simplify each Instruction and add new properties. - void visitInstruction(Instruction *I, DomTreeDFS::Node *DT) { - DOUT << "Considering instruction " << *I << "\n"; - DEBUG(VN->dump()); - DEBUG(IG->dump()); - DEBUG(VR->dump()); - - // Sometimes instructions are killed in earlier analysis. - if (isInstructionTriviallyDead(I)) { - ++NumSimple; - modified = true; - if (unsigned n = VN->valueNumber(I, DTDFS->getRootNode())) - if (VN->value(n) == I) IG->remove(n); - VN->remove(I); - I->eraseFromParent(); - return; - } - -#ifndef NDEBUG - // Try to replace the whole instruction. - Value *V = VN->canonicalize(I, DT); - assert(V == I && "Late instruction canonicalization."); - if (V != I) { - modified = true; - ++NumInstruction; - DOUT << "Removing " << *I << ", replacing with " << *V << "\n"; - if (unsigned n = VN->valueNumber(I, DTDFS->getRootNode())) - if (VN->value(n) == I) IG->remove(n); - VN->remove(I); - I->replaceAllUsesWith(V); - I->eraseFromParent(); - return; - } - - // Try to substitute operands. - for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { - Value *Oper = I->getOperand(i); - Value *V = VN->canonicalize(Oper, DT); - assert(V == Oper && "Late operand canonicalization."); - if (V != Oper) { - modified = true; - ++NumVarsReplaced; - DOUT << "Resolving " << *I; - I->setOperand(i, V); - DOUT << " into " << *I; - } - } -#endif - - std::string name = I->getParent()->getName(); - DOUT << "push (%" << name << ")\n"; - Forwards visit(this, DT); - visit.visit(*I); - DOUT << "pop (%" << name << ")\n"; - } - }; - - bool PredicateSimplifier::runOnFunction(Function &F) { - DominatorTree *DT = &getAnalysis<DominatorTree>(); - DTDFS = new DomTreeDFS(DT); - TargetData *TD = &getAnalysis<TargetData>(); - - DOUT << "Entering Function: " << F.getName() << "\n"; - - modified = false; - DomTreeDFS::Node *Root = DTDFS->getRootNode(); - VN = new ValueNumbering(DTDFS); - IG = new InequalityGraph(*VN, Root); - VR = new ValueRanges(*VN, TD); - WorkList.push_back(Root); - - do { - DomTreeDFS::Node *DTNode = WorkList.back(); - WorkList.pop_back(); - if (!UB.isDead(DTNode->getBlock())) visitBasicBlock(DTNode); - } while (!WorkList.empty()); - - delete DTDFS; - delete VR; - delete IG; - delete VN; - - modified |= UB.kill(); - - return modified; - } - - void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) { - PS->proceedToSuccessors(DTNode); - } - - void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) { - if (BI.isUnconditional()) { - PS->proceedToSuccessors(DTNode); - return; - } - - Value *Condition = BI.getCondition(); - BasicBlock *TrueDest = BI.getSuccessor(0); - BasicBlock *FalseDest = BI.getSuccessor(1); - - if (isa<Constant>(Condition) || TrueDest == FalseDest) { - PS->proceedToSuccessors(DTNode); - return; - } - - for (DomTreeDFS::Node::iterator I = DTNode->begin(), E = DTNode->end(); - I != E; ++I) { - BasicBlock *Dest = (*I)->getBlock(); - DOUT << "Branch thinking about %" << Dest->getName() - << "(" << PS->DTDFS->getNodeForBlock(Dest)->getDFSNumIn() << ")\n"; - - if (Dest == TrueDest) { - DOUT << "(" << DTNode->getBlock()->getName() << ") true set:\n"; - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, Dest); - VRP.add(ConstantInt::getTrue(), Condition, ICmpInst::ICMP_EQ); - VRP.solve(); - DEBUG(VN.dump()); - DEBUG(IG.dump()); - DEBUG(VR.dump()); - } else if (Dest == FalseDest) { - DOUT << "(" << DTNode->getBlock()->getName() << ") false set:\n"; - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, Dest); - VRP.add(ConstantInt::getFalse(), Condition, ICmpInst::ICMP_EQ); - VRP.solve(); - DEBUG(VN.dump()); - DEBUG(IG.dump()); - DEBUG(VR.dump()); - } - - PS->proceedToSuccessor(*I); - } - } - - void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) { - Value *Condition = SI.getCondition(); - - // Set the EQProperty in each of the cases BBs, and the NEProperties - // in the default BB. - - for (DomTreeDFS::Node::iterator I = DTNode->begin(), E = DTNode->end(); - I != E; ++I) { - BasicBlock *BB = (*I)->getBlock(); - DOUT << "Switch thinking about BB %" << BB->getName() - << "(" << PS->DTDFS->getNodeForBlock(BB)->getDFSNumIn() << ")\n"; - - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, BB); - if (BB == SI.getDefaultDest()) { - for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i) - if (SI.getSuccessor(i) != BB) - VRP.add(Condition, SI.getCaseValue(i), ICmpInst::ICMP_NE); - VRP.solve(); - } else if (ConstantInt *CI = SI.findCaseDest(BB)) { - VRP.add(Condition, CI, ICmpInst::ICMP_EQ); - VRP.solve(); - } - PS->proceedToSuccessor(*I); - } - } - - void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &AI); - VRP.add(Constant::getNullValue(AI.getType()), &AI, ICmpInst::ICMP_NE); - VRP.solve(); - } - - void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) { - Value *Ptr = LI.getPointerOperand(); - // avoid "load i8* null" -> null NE null. - if (isa<Constant>(Ptr)) return; - - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &LI); - VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE); - VRP.solve(); - } - - void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) { - Value *Ptr = SI.getPointerOperand(); - if (isa<Constant>(Ptr)) return; - - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI); - VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE); - VRP.solve(); - } - - void PredicateSimplifier::Forwards::visitSExtInst(SExtInst &SI) { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI); - uint32_t SrcBitWidth = cast<IntegerType>(SI.getSrcTy())->getBitWidth(); - uint32_t DstBitWidth = cast<IntegerType>(SI.getDestTy())->getBitWidth(); - APInt Min(APInt::getHighBitsSet(DstBitWidth, DstBitWidth-SrcBitWidth+1)); - APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth-1)); - VRP.add(ConstantInt::get(Min), &SI, ICmpInst::ICMP_SLE); - VRP.add(ConstantInt::get(Max), &SI, ICmpInst::ICMP_SGE); - VRP.solve(); - } - - void PredicateSimplifier::Forwards::visitZExtInst(ZExtInst &ZI) { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &ZI); - uint32_t SrcBitWidth = cast<IntegerType>(ZI.getSrcTy())->getBitWidth(); - uint32_t DstBitWidth = cast<IntegerType>(ZI.getDestTy())->getBitWidth(); - APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth)); - VRP.add(ConstantInt::get(Max), &ZI, ICmpInst::ICMP_UGE); - VRP.solve(); - } - - void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) { - Instruction::BinaryOps ops = BO.getOpcode(); - - switch (ops) { - default: break; - case Instruction::URem: - case Instruction::SRem: - case Instruction::UDiv: - case Instruction::SDiv: { - Value *Divisor = BO.getOperand(1); - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO); - VRP.add(Constant::getNullValue(Divisor->getType()), Divisor, - ICmpInst::ICMP_NE); - VRP.solve(); - break; - } - } - - switch (ops) { - default: break; - case Instruction::Shl: { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO); - VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_UGE); - VRP.solve(); - } break; - case Instruction::AShr: { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO); - VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_SLE); - VRP.solve(); - } break; - case Instruction::LShr: - case Instruction::UDiv: { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO); - VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_ULE); - VRP.solve(); - } break; - case Instruction::URem: { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO); - VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_ULE); - VRP.solve(); - } break; - case Instruction::And: { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO); - VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_ULE); - VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_ULE); - VRP.solve(); - } break; - case Instruction::Or: { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO); - VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_UGE); - VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_UGE); - VRP.solve(); - } break; - } - } - - void PredicateSimplifier::Forwards::visitICmpInst(ICmpInst &IC) { - // If possible, squeeze the ICmp predicate into something simpler. - // Eg., if x = [0, 4) and we're being asked icmp uge %x, 3 then change - // the predicate to eq. - - // XXX: once we do full PHI handling, modifying the instruction in the - // Forwards visitor will cause missed optimizations. - - ICmpInst::Predicate Pred = IC.getPredicate(); - - switch (Pred) { - default: break; - case ICmpInst::ICMP_ULE: Pred = ICmpInst::ICMP_ULT; break; - case ICmpInst::ICMP_UGE: Pred = ICmpInst::ICMP_UGT; break; - case ICmpInst::ICMP_SLE: Pred = ICmpInst::ICMP_SLT; break; - case ICmpInst::ICMP_SGE: Pred = ICmpInst::ICMP_SGT; break; - } - if (Pred != IC.getPredicate()) { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &IC); - if (VRP.isRelatedBy(IC.getOperand(1), IC.getOperand(0), - ICmpInst::ICMP_NE)) { - ++NumSnuggle; - PS->modified = true; - IC.setPredicate(Pred); - } - } - - Pred = IC.getPredicate(); - - if (ConstantInt *Op1 = dyn_cast<ConstantInt>(IC.getOperand(1))) { - ConstantInt *NextVal = 0; - switch (Pred) { - default: break; - case ICmpInst::ICMP_SLT: - case ICmpInst::ICMP_ULT: - if (Op1->getValue() != 0) - NextVal = ConstantInt::get(Op1->getValue()-1); - break; - case ICmpInst::ICMP_SGT: - case ICmpInst::ICMP_UGT: - if (!Op1->getValue().isAllOnesValue()) - NextVal = ConstantInt::get(Op1->getValue()+1); - break; - } - - if (NextVal) { - VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &IC); - if (VRP.isRelatedBy(IC.getOperand(0), NextVal, - ICmpInst::getInversePredicate(Pred))) { - ICmpInst *NewIC = new ICmpInst(ICmpInst::ICMP_EQ, IC.getOperand(0), - NextVal, "", &IC); - NewIC->takeName(&IC); - IC.replaceAllUsesWith(NewIC); - - // XXX: prove this isn't necessary - if (unsigned n = VN.valueNumber(&IC, PS->DTDFS->getRootNode())) - if (VN.value(n) == &IC) IG.remove(n); - VN.remove(&IC); - - IC.eraseFromParent(); - ++NumSnuggle; - PS->modified = true; - } - } - } - } -} - -char PredicateSimplifier::ID = 0; -static RegisterPass<PredicateSimplifier> -X("predsimplify", "Predicate Simplifier"); - -FunctionPass *llvm::createPredicateSimplifierPass() { - return new PredicateSimplifier(); -} diff --git a/lib/Transforms/Utils/CloneTrace.cpp b/lib/Transforms/Utils/CloneTrace.cpp deleted file mode 100644 index 0711139..0000000 --- a/lib/Transforms/Utils/CloneTrace.cpp +++ /dev/null @@ -1,119 +0,0 @@ -//===- CloneTrace.cpp - Clone a trace -------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the CloneTrace interface, which is used when writing -// runtime optimizations. It takes a vector of basic blocks clones the basic -// blocks, removes internal phi nodes, adds it to the same function as the -// original (although there is no jump to it) and returns the new vector of -// basic blocks. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Analysis/Trace.h" -#include "llvm/Transforms/Utils/Cloning.h" -#include "llvm/Instructions.h" -#include "llvm/Function.h" -#include "llvm/Transforms/Utils/ValueMapper.h" -using namespace llvm; - -//Clones the trace (a vector of basic blocks) -std::vector<BasicBlock *> -llvm::CloneTrace(const std::vector<BasicBlock*> &origTrace) { - std::vector<BasicBlock *> clonedTrace; - DenseMap<const Value*, Value*> ValueMap; - - //First, loop over all the Basic Blocks in the trace and copy - //them using CloneBasicBlock. Also fix the phi nodes during - //this loop. To fix the phi nodes, we delete incoming branches - //that are not in the trace. - for (std::vector<BasicBlock *>::const_iterator T = origTrace.begin(), - End = origTrace.end(); T != End; ++T) { - - //Clone Basic Block - BasicBlock *clonedBlock = - CloneBasicBlock(*T, ValueMap, ".tr", (*T)->getParent()); - - //Add it to our new trace - clonedTrace.push_back(clonedBlock); - - //Add this new mapping to our Value Map - ValueMap[*T] = clonedBlock; - - //Loop over the phi instructions and delete operands - //that are from blocks not in the trace - //only do this if we are NOT the first block - if (T != origTrace.begin()) { - for (BasicBlock::iterator I = clonedBlock->begin(); - isa<PHINode>(I); ++I) { - PHINode *PN = cast<PHINode>(I); - //get incoming value for the previous BB - Value *V = PN->getIncomingValueForBlock(*(T-1)); - assert(V && "No incoming value from a BasicBlock in our trace!"); - - //remap our phi node to point to incoming value - ValueMap[*&I] = V; - - //remove phi node - clonedBlock->getInstList().erase(PN); - } - } - } - - //Second loop to do the remapping - for (std::vector<BasicBlock *>::const_iterator BB = clonedTrace.begin(), - BE = clonedTrace.end(); BB != BE; ++BB) { - for (BasicBlock::iterator I = (*BB)->begin(); I != (*BB)->end(); ++I) { - //Loop over all the operands of the instruction - for (unsigned op=0, E = I->getNumOperands(); op != E; ++op) { - const Value *Op = I->getOperand(op); - - //Get it out of the value map - Value *V = ValueMap[Op]; - - //If not in the value map, then its outside our trace so ignore - if (V != 0) - I->setOperand(op,V); - } - } - } - - //return new vector of basic blocks - return clonedTrace; -} - -/// CloneTraceInto - Clone T into NewFunc. Original<->clone mapping is -/// saved in ValueMap. -/// -void llvm::CloneTraceInto(Function *NewFunc, Trace &T, - DenseMap<const Value*, Value*> &ValueMap, - const char *NameSuffix) { - assert(NameSuffix && "NameSuffix cannot be null!"); - - // Loop over all of the basic blocks in the trace, cloning them as - // appropriate. - // - for (Trace::const_iterator BI = T.begin(), BE = T.end(); BI != BE; ++BI) { - const BasicBlock *BB = *BI; - - // Create a new basic block and copy instructions into it! - BasicBlock *CBB = CloneBasicBlock(BB, ValueMap, NameSuffix, NewFunc); - ValueMap[BB] = CBB; // Add basic block mapping. - } - - // Loop over all of the instructions in the new function, fixing up operand - // references as we go. This uses ValueMap to do all the hard work. - // - for (Function::iterator BB = - cast<BasicBlock>(ValueMap[T.getEntryBasicBlock()]), - BE = NewFunc->end(); BB != BE; ++BB) - // Loop over all instructions, fixing each one as we find it... - for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) - RemapInstruction(II, ValueMap); -} - diff --git a/lib/Transforms/Utils/InlineCost.cpp b/lib/Transforms/Utils/InlineCost.cpp deleted file mode 100644 index 87aff01..0000000 --- a/lib/Transforms/Utils/InlineCost.cpp +++ /dev/null @@ -1,315 +0,0 @@ -//===- InlineCost.cpp - Cost analysis for inliner -------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements inline cost analysis. -// -//===----------------------------------------------------------------------===// - - -#include "llvm/Transforms/Utils/InlineCost.h" -#include "llvm/Support/CallSite.h" -#include "llvm/CallingConv.h" -#include "llvm/IntrinsicInst.h" - -using namespace llvm; - -// CountCodeReductionForConstant - Figure out an approximation for how many -// instructions will be constant folded if the specified value is constant. -// -unsigned InlineCostAnalyzer::FunctionInfo:: - CountCodeReductionForConstant(Value *V) { - unsigned Reduction = 0; - for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) - if (isa<BranchInst>(*UI)) - Reduction += 40; // Eliminating a conditional branch is a big win - else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI)) - // Eliminating a switch is a big win, proportional to the number of edges - // deleted. - Reduction += (SI->getNumSuccessors()-1) * 40; - else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { - // Turning an indirect call into a direct call is a BIG win - Reduction += CI->getCalledValue() == V ? 500 : 0; - } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { - // Turning an indirect call into a direct call is a BIG win - Reduction += II->getCalledValue() == V ? 500 : 0; - } else { - // Figure out if this instruction will be removed due to simple constant - // propagation. - Instruction &Inst = cast<Instruction>(**UI); - bool AllOperandsConstant = true; - for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) - if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) { - AllOperandsConstant = false; - break; - } - - if (AllOperandsConstant) { - // We will get to remove this instruction... - Reduction += 7; - - // And any other instructions that use it which become constants - // themselves. - Reduction += CountCodeReductionForConstant(&Inst); - } - } - - return Reduction; -} - -// CountCodeReductionForAlloca - Figure out an approximation of how much smaller -// the function will be if it is inlined into a context where an argument -// becomes an alloca. -// -unsigned InlineCostAnalyzer::FunctionInfo:: - CountCodeReductionForAlloca(Value *V) { - if (!isa<PointerType>(V->getType())) return 0; // Not a pointer - unsigned Reduction = 0; - for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ - Instruction *I = cast<Instruction>(*UI); - if (isa<LoadInst>(I) || isa<StoreInst>(I)) - Reduction += 10; - else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { - // If the GEP has variable indices, we won't be able to do much with it. - if (!GEP->hasAllConstantIndices()) - Reduction += CountCodeReductionForAlloca(GEP)+15; - } else { - // If there is some other strange instruction, we're not going to be able - // to do much if we inline this. - return 0; - } - } - - return Reduction; -} - -/// analyzeFunction - Fill in the current structure with information gleaned -/// from the specified function. -void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F) { - unsigned NumInsts = 0, NumBlocks = 0, NumVectorInsts = 0; - - // Look at the size of the callee. Each basic block counts as 20 units, and - // each instruction counts as 5. - for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { - for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); - II != E; ++II) { - if (isa<PHINode>(II)) continue; // PHI nodes don't count. - - // Special handling for calls. - if (isa<CallInst>(II) || isa<InvokeInst>(II)) { - if (isa<DbgInfoIntrinsic>(II)) - continue; // Debug intrinsics don't count as size. - - CallSite CS = CallSite::get(const_cast<Instruction*>(&*II)); - - // If this function contains a call to setjmp or _setjmp, never inline - // it. This is a hack because we depend on the user marking their local - // variables as volatile if they are live across a setjmp call, and they - // probably won't do this in callers. - if (Function *F = CS.getCalledFunction()) - if (F->isDeclaration() && - (F->isName("setjmp") || F->isName("_setjmp"))) { - NeverInline = true; - return; - } - - // Calls often compile into many machine instructions. Bump up their - // cost to reflect this. - if (!isa<IntrinsicInst>(II)) - NumInsts += 5; - } - - if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { - if (!AI->isStaticAlloca()) - this->usesDynamicAlloca = true; - } - - if (isa<ExtractElementInst>(II) || isa<VectorType>(II->getType())) - ++NumVectorInsts; - - // Noop casts, including ptr <-> int, don't count. - if (const CastInst *CI = dyn_cast<CastInst>(II)) { - if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) || - isa<PtrToIntInst>(CI)) - continue; - } else if (const GetElementPtrInst *GEPI = - dyn_cast<GetElementPtrInst>(II)) { - // If a GEP has all constant indices, it will probably be folded with - // a load/store. - if (GEPI->hasAllConstantIndices()) - continue; - } - - ++NumInsts; - } - - ++NumBlocks; - } - - this->NumBlocks = NumBlocks; - this->NumInsts = NumInsts; - this->NumVectorInsts = NumVectorInsts; - - // Check out all of the arguments to the function, figuring out how much - // code can be eliminated if one of the arguments is a constant. - for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) - ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I), - CountCodeReductionForAlloca(I))); -} - - - -// getInlineCost - The heuristic used to determine if we should inline the -// function call or not. -// -InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, - SmallPtrSet<const Function *, 16> &NeverInline) { - Instruction *TheCall = CS.getInstruction(); - Function *Callee = CS.getCalledFunction(); - Function *Caller = TheCall->getParent()->getParent(); - - // Don't inline functions which can be redefined at link-time to mean - // something else. - if (Callee->mayBeOverridden() || - // Don't inline functions marked noinline. - Callee->hasFnAttr(Attribute::NoInline) || NeverInline.count(Callee)) - return llvm::InlineCost::getNever(); - - // InlineCost - This value measures how good of an inline candidate this call - // site is to inline. A lower inline cost make is more likely for the call to - // be inlined. This value may go negative. - // - int InlineCost = 0; - - // If there is only one call of the function, and it has internal linkage, - // make it almost guaranteed to be inlined. - // - if ((Callee->hasLocalLinkage() || Callee->hasAvailableExternallyLinkage()) && - Callee->hasOneUse()) - InlineCost -= 15000; - - // If this function uses the coldcc calling convention, prefer not to inline - // it. - if (Callee->getCallingConv() == CallingConv::Cold) - InlineCost += 2000; - - // If the instruction after the call, or if the normal destination of the - // invoke is an unreachable instruction, the function is noreturn. As such, - // there is little point in inlining this. - if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { - if (isa<UnreachableInst>(II->getNormalDest()->begin())) - InlineCost += 10000; - } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall))) - InlineCost += 10000; - - // Get information about the callee... - FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; - - // If we haven't calculated this information yet, do so now. - if (CalleeFI.NumBlocks == 0) - CalleeFI.analyzeFunction(Callee); - - // If we should never inline this, return a huge cost. - if (CalleeFI.NeverInline) - return InlineCost::getNever(); - - // FIXME: It would be nice to kill off CalleeFI.NeverInline. Then we - // could move this up and avoid computing the FunctionInfo for - // things we are going to just return always inline for. This - // requires handling setjmp somewhere else, however. - if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline)) - return InlineCost::getAlways(); - - if (CalleeFI.usesDynamicAlloca) { - // Get infomation about the caller... - FunctionInfo &CallerFI = CachedFunctionInfo[Caller]; - - // If we haven't calculated this information yet, do so now. - if (CallerFI.NumBlocks == 0) - CallerFI.analyzeFunction(Caller); - - // Don't inline a callee with dynamic alloca into a caller without them. - // Functions containing dynamic alloca's are inefficient in various ways; - // don't create more inefficiency. - if (!CallerFI.usesDynamicAlloca) - return InlineCost::getNever(); - } - - // Add to the inline quality for properties that make the call valuable to - // inline. This includes factors that indicate that the result of inlining - // the function will be optimizable. Currently this just looks at arguments - // passed into the function. - // - unsigned ArgNo = 0; - for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); - I != E; ++I, ++ArgNo) { - // Each argument passed in has a cost at both the caller and the callee - // sides. This favors functions that take many arguments over functions - // that take few arguments. - InlineCost -= 20; - - // If this is a function being passed in, it is very likely that we will be - // able to turn an indirect function call into a direct function call. - if (isa<Function>(I)) - InlineCost -= 100; - - // If an alloca is passed in, inlining this function is likely to allow - // significant future optimization possibilities (like scalar promotion, and - // scalarization), so encourage the inlining of the function. - // - else if (isa<AllocaInst>(I)) { - if (ArgNo < CalleeFI.ArgumentWeights.size()) - InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight; - - // If this is a constant being passed into the function, use the argument - // weights calculated for the callee to determine how much will be folded - // away with this information. - } else if (isa<Constant>(I)) { - if (ArgNo < CalleeFI.ArgumentWeights.size()) - InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight; - } - } - - // Now that we have considered all of the factors that make the call site more - // likely to be inlined, look at factors that make us not want to inline it. - - // Don't inline into something too big, which would make it bigger. - // - InlineCost += Caller->size()/15; - - // Look at the size of the callee. Each instruction counts as 5. - InlineCost += CalleeFI.NumInsts*5; - - return llvm::InlineCost::get(InlineCost); -} - -// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a -// higher threshold to determine if the function call should be inlined. -float InlineCostAnalyzer::getInlineFudgeFactor(CallSite CS) { - Function *Callee = CS.getCalledFunction(); - - // Get information about the callee... - FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; - - // If we haven't calculated this information yet, do so now. - if (CalleeFI.NumBlocks == 0) - CalleeFI.analyzeFunction(Callee); - - float Factor = 1.0f; - // Single BB functions are often written to be inlined. - if (CalleeFI.NumBlocks == 1) - Factor += 0.5f; - - // Be more aggressive if the function contains a good chunk (if it mades up - // at least 10% of the instructions) of vector instructions. - if (CalleeFI.NumVectorInsts > CalleeFI.NumInsts/2) - Factor += 2.0f; - else if (CalleeFI.NumVectorInsts > CalleeFI.NumInsts/10) - Factor += 1.5f; - return Factor; -} |