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 visitCastInst(CastInst &I);
00077     void visitGetElementPtrInst(GetElementPtrInst &I);
00078 
00079     void handleBinaryInst(Instruction &I);
00080     void visitBinaryOperator(Instruction &I)     { handleBinaryInst(I); }
00081     void visitShiftInst(Instruction &I)          { handleBinaryInst(I); }
00082     void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
00083 
00084     void handleTernaryInst(Instruction &I);
00085     void visitSelectInst(Instruction &I)         { handleTernaryInst(I); }
00086     void visitInsertElementInst(Instruction &I)  { handleTernaryInst(I); }
00087     void visitShuffleVectorInst(Instruction &I)  { handleTernaryInst(I); }
00088     void visitInstruction(Instruction &) {
00089       // Cannot value number calls or terminator instructions.
00090     }
00091   };
00092 }
00093 
00094 ImmutablePass *llvm::createBasicVNPass() { return new BasicVN(); }
00095 
00096 // getEqualNumberNodes - Return nodes with the same value number as the
00097 // specified Value.  This fills in the argument vector with any equal values.
00098 //
00099 void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
00100   assert(V->getType() != Type::VoidTy &&
00101          "Can only value number non-void values!");
00102   // We can only handle the case where I is an instruction!
00103   if (Instruction *I = dyn_cast<Instruction>(V))
00104     BVNImpl(RetVals).visit(I);
00105 }
00106 
00107 void BVNImpl::visitCastInst(CastInst &CI) {
00108   Instruction &I = (Instruction&)CI;
00109   Value *Op = I.getOperand(0);
00110   Function *F = I.getParent()->getParent();
00111 
00112   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
00113        UI != UE; ++UI)
00114     if (CastInst *Other = dyn_cast<CastInst>(*UI))
00115       // Check that the types are the same, since this code handles casts...
00116       if (Other->getType() == I.getType() &&
00117           // Is it embedded in the same function?  (This could be false if LHS
00118           // is a constant or global!)
00119           Other->getParent()->getParent() == F &&
00120           // Check to see if this new cast is not I.
00121           Other != &I) {
00122         // These instructions are identical.  Add to list...
00123         RetVals.push_back(Other);
00124       }
00125 }
00126 
00127 
00128 // isIdenticalBinaryInst - Return true if the two binary instructions are
00129 // identical.
00130 //
00131 static inline bool isIdenticalBinaryInst(const Instruction &I1,
00132                                          const Instruction *I2) {
00133   // Is it embedded in the same function?  (This could be false if LHS
00134   // is a constant or global!)
00135   if (I1.getOpcode() != I2->getOpcode() ||
00136       I1.getParent()->getParent() != I2->getParent()->getParent())
00137     return false;
00138 
00139   // They are identical if both operands are the same!
00140   if (I1.getOperand(0) == I2->getOperand(0) &&
00141       I1.getOperand(1) == I2->getOperand(1))
00142     return true;
00143 
00144   // If the instruction is commutative, the instruction can match if the
00145   // operands are swapped!
00146   //
00147   if ((I1.getOperand(0) == I2->getOperand(1) &&
00148        I1.getOperand(1) == I2->getOperand(0)) &&
00149       I1.isCommutative())
00150     return true;
00151 
00152   return false;
00153 }
00154 
00155 // isIdenticalTernaryInst - Return true if the two ternary instructions are
00156 // identical.
00157 //
00158 static inline bool isIdenticalTernaryInst(const Instruction &I1,
00159                                           const Instruction *I2) {
00160   // Is it embedded in the same function?  (This could be false if LHS
00161   // is a constant or global!)
00162   if (I1.getParent()->getParent() != I2->getParent()->getParent())
00163     return false;
00164   
00165   // They are identical if all operands are the same!
00166   return I1.getOperand(0) == I2->getOperand(0) &&
00167          I1.getOperand(1) == I2->getOperand(1) &&
00168          I1.getOperand(2) == I2->getOperand(2);
00169 }
00170 
00171 
00172 
00173 void BVNImpl::handleBinaryInst(Instruction &I) {
00174   Value *LHS = I.getOperand(0);
00175 
00176   for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
00177        UI != UE; ++UI)
00178     if (Instruction *Other = dyn_cast<Instruction>(*UI))
00179       // Check to see if this new binary operator is not I, but same operand...
00180       if (Other != &I && isIdenticalBinaryInst(I, Other)) {
00181         // These instructions are identical.  Handle the situation.
00182         RetVals.push_back(Other);
00183       }
00184 }
00185 
00186 // IdenticalComplexInst - Return true if the two instructions are the same, by
00187 // using a brute force comparison.  This is useful for instructions with an
00188 // arbitrary number of arguments.
00189 //
00190 static inline bool IdenticalComplexInst(const Instruction *I1,
00191                                         const Instruction *I2) {
00192   assert(I1->getOpcode() == I2->getOpcode());
00193   // Equal if they are in the same function...
00194   return I1->getParent()->getParent() == I2->getParent()->getParent() &&
00195     // And return the same type...
00196     I1->getType() == I2->getType() &&
00197     // And have the same number of operands...
00198     I1->getNumOperands() == I2->getNumOperands() &&
00199     // And all of the operands are equal.
00200     std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
00201 }
00202 
00203 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
00204   Value *Op = I.getOperand(0);
00205 
00206   // Try to pick a local operand if possible instead of a constant or a global
00207   // that might have a lot of uses.
00208   for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
00209     if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
00210       Op = I.getOperand(i);
00211       break;
00212     }
00213 
00214   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
00215        UI != UE; ++UI)
00216     if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
00217       // Check to see if this new getelementptr is not I, but same operand...
00218       if (Other != &I && IdenticalComplexInst(&I, Other)) {
00219         // These instructions are identical.  Handle the situation.
00220         RetVals.push_back(Other);
00221       }
00222 }
00223 
00224 void BVNImpl::handleTernaryInst(Instruction &I) {
00225   Value *Op0 = I.getOperand(0);
00226   Instruction *OtherInst;
00227   
00228   for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
00229        UI != UE; ++UI)
00230     if ((OtherInst = dyn_cast<Instruction>(*UI)) && 
00231         OtherInst->getOpcode() == I.getOpcode()) {
00232       // Check to see if this new select is not I, but has the same operands.
00233       if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
00234         // These instructions are identical.  Handle the situation.
00235         RetVals.push_back(OtherInst);
00236       }
00237         
00238     }
00239 }
00240 
00241 
00242 // Ensure that users of ValueNumbering.h will link with this file
00243 DEFINING_FILE_FOR(BasicValueNumbering)