LLVM API Documentation

ValueNumbering.cpp

Go to the documentation of this file.
00001 //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the non-abstract Value Numbering methods as well as a
00011 // default implementation for the analysis group.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Analysis/Passes.h"
00016 #include "llvm/Analysis/ValueNumbering.h"
00017 #include "llvm/Support/InstVisitor.h"
00018 #include "llvm/BasicBlock.h"
00019 #include "llvm/Instructions.h"
00020 #include "llvm/Pass.h"
00021 #include "llvm/Type.h"
00022 using namespace llvm;
00023 
00024 // Register the ValueNumbering interface, providing a nice name to refer to.
00025 static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
00026 
00027 /// ValueNumbering destructor: DO NOT move this to the header file for
00028 /// ValueNumbering or else clients of the ValueNumbering class may not depend on
00029 /// the ValueNumbering.o file in the current .a file, causing alias analysis
00030 /// support to not be included in the tool correctly!
00031 ///
00032 ValueNumbering::~ValueNumbering() {}
00033 
00034 //===----------------------------------------------------------------------===//
00035 // Basic ValueNumbering Pass Implementation
00036 //===----------------------------------------------------------------------===//
00037 //
00038 // Because of the way .a files work, the implementation of the BasicVN class
00039 // MUST be in the ValueNumbering file itself, or else we run the risk of
00040 // ValueNumbering being used, but the default implementation not being linked
00041 // into the tool that uses it.  As such, we register and implement the class
00042 // here.
00043 //
00044 
00045 namespace {
00046   /// BasicVN - This class is the default implementation of the ValueNumbering
00047   /// interface.  It walks the SSA def-use chains to trivially identify
00048   /// lexically identical expressions.  This does not require any ahead of time
00049   /// analysis, so it is a very fast default implementation.
00050   ///
00051   struct BasicVN : public ImmutablePass, public ValueNumbering {
00052     /// getEqualNumberNodes - Return nodes with the same value number as the
00053     /// specified Value.  This fills in the argument vector with any equal
00054     /// values.
00055     ///
00056     /// This is where our implementation is.
00057     ///
00058     virtual void getEqualNumberNodes(Value *V1,
00059                                      std::vector<Value*> &RetVals) const;
00060   };
00061 
00062   // Register this pass...
00063   RegisterOpt<BasicVN>
00064   X("basicvn", "Basic Value Numbering (default GVN impl)");
00065 
00066   // Declare that we implement the ValueNumbering interface
00067   RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
00068 
00069   /// BVNImpl - Implement BasicVN in terms of a visitor class that
00070   /// handles the different types of instructions as appropriate.
00071   ///
00072   struct BVNImpl : public InstVisitor<BVNImpl> {
00073     std::vector<Value*> &RetVals;
00074     BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
00075 
00076     void handleBinaryInst(Instruction &I);
00077     void visitBinaryOperator(BinaryOperator &I) {
00078       handleBinaryInst((Instruction&)I);
00079     }
00080     void visitGetElementPtrInst(GetElementPtrInst &I);
00081     void visitCastInst(CastInst &I);
00082     void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
00083     void visitSelectInst(SelectInst &I);
00084     void visitInstruction(Instruction &) {
00085       // Cannot value number calls or terminator instructions.
00086     }
00087   };
00088 }
00089 
00090 ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
00091 
00092 // getEqualNumberNodes - Return nodes with the same value number as the
00093 // specified Value.  This fills in the argument vector with any equal values.
00094 //
00095 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
00096   assert(V->getType() != Type::VoidTy &&
00097          "Can only value number non-void values!");
00098   // We can only handle the case where I is an instruction!
00099   if (Instruction *I = dyn_cast<Instruction>(V))
00100     BVNImpl(RetVals).visit(I);
00101 }
00102 
00103 void BVNImpl::visitCastInst(CastInst &CI) {
00104   Instruction &I = (Instruction&)CI;
00105   Value *Op = I.getOperand(0);
00106   Function *F = I.getParent()->getParent();
00107 
00108   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
00109        UI != UE; ++UI)
00110     if (CastInst *Other = dyn_cast<CastInst>(*UI))
00111       // Check that the types are the same, since this code handles casts...
00112       if (Other->getType() == I.getType() &&
00113           // Is it embedded in the same function?  (This could be false if LHS
00114           // is a constant or global!)
00115           Other->getParent()->getParent() == F &&
00116           // Check to see if this new cast is not I.
00117           Other != &I) {
00118         // These instructions are identical.  Add to list...
00119         RetVals.push_back(Other);
00120       }
00121 }
00122 
00123 
00124 // isIdenticalBinaryInst - Return true if the two binary instructions are
00125 // identical.
00126 //
00127 static inline bool isIdenticalBinaryInst(const Instruction &I1,
00128                                          const Instruction *I2) {
00129   // Is it embedded in the same function?  (This could be false if LHS
00130   // is a constant or global!)
00131   if (I1.getOpcode() != I2->getOpcode() ||
00132       I1.getParent()->getParent() != I2->getParent()->getParent())
00133     return false;
00134 
00135   // They are identical if both operands are the same!
00136   if (I1.getOperand(0) == I2->getOperand(0) &&
00137       I1.getOperand(1) == I2->getOperand(1))
00138     return true;
00139 
00140   // If the instruction is commutative, the instruction can match if the
00141   // operands are swapped!
00142   //
00143   if ((I1.getOperand(0) == I2->getOperand(1) &&
00144        I1.getOperand(1) == I2->getOperand(0)) &&
00145       I1.isCommutative())
00146     return true;
00147 
00148   return false;
00149 }
00150 
00151 void BVNImpl::handleBinaryInst(Instruction &I) {
00152   Value *LHS = I.getOperand(0);
00153 
00154   for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
00155        UI != UE; ++UI)
00156     if (Instruction *Other = dyn_cast<Instruction>(*UI))
00157       // Check to see if this new binary operator is not I, but same operand...
00158       if (Other != &I && isIdenticalBinaryInst(I, Other)) {
00159         // These instructions are identical.  Handle the situation.
00160         RetVals.push_back(Other);
00161       }
00162 }
00163 
00164 // IdenticalComplexInst - Return true if the two instructions are the same, by
00165 // using a brute force comparison.  This is useful for instructions with an
00166 // arbitrary number of arguments.
00167 //
00168 static inline bool IdenticalComplexInst(const Instruction *I1,
00169                                         const Instruction *I2) {
00170   assert(I1->getOpcode() == I2->getOpcode());
00171   // Equal if they are in the same function...
00172   return I1->getParent()->getParent() == I2->getParent()->getParent() &&
00173     // And return the same type...
00174     I1->getType() == I2->getType() &&
00175     // And have the same number of operands...
00176     I1->getNumOperands() == I2->getNumOperands() &&
00177     // And all of the operands are equal.
00178     std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
00179 }
00180 
00181 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
00182   Value *Op = I.getOperand(0);
00183 
00184   // Try to pick a local operand if possible instead of a constant or a global
00185   // that might have a lot of uses.
00186   for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
00187     if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
00188       Op = I.getOperand(i);
00189       break;
00190     }
00191 
00192   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
00193        UI != UE; ++UI)
00194     if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
00195       // Check to see if this new getelementptr is not I, but same operand...
00196       if (Other != &I && IdenticalComplexInst(&I, Other)) {
00197         // These instructions are identical.  Handle the situation.
00198         RetVals.push_back(Other);
00199       }
00200 }
00201 
00202 // isIdenticalSelectInst - Return true if the two select instructions are
00203 // identical.
00204 //
00205 static inline bool isIdenticalSelectInst(const SelectInst &I1,
00206                                          const SelectInst *I2) {
00207   // Is it embedded in the same function?  (This could be false if LHS
00208   // is a constant or global!)
00209   if (I1.getParent()->getParent() != I2->getParent()->getParent())
00210     return false;
00211   
00212   // They are identical if both operands are the same!
00213   return I1.getOperand(0) == I2->getOperand(0) &&
00214          I1.getOperand(1) == I2->getOperand(1) &&
00215          I1.getOperand(2) == I2->getOperand(2);
00216     return true;
00217   
00218   return false;
00219 }
00220 
00221 void BVNImpl::visitSelectInst(SelectInst &I) {
00222   Value *Cond = I.getOperand(0);
00223   
00224   for (Value::use_iterator UI = Cond->use_begin(), UE = Cond->use_end();
00225        UI != UE; ++UI)
00226     if (SelectInst *Other = dyn_cast<SelectInst>(*UI))
00227       // Check to see if this new select is not I, but has the same operands.
00228       if (Other != &I && isIdenticalSelectInst(I, Other)) {
00229         // These instructions are identical.  Handle the situation.
00230         RetVals.push_back(Other);
00231       }
00232         
00233 }
00234 
00235 
00236 void llvm::BasicValueNumberingStub() { }