LLVM API Documentation

MergeFunctions.cpp
Go to the documentation of this file.
00001 //===- MergeFunctions.cpp - Merge identical functions ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass looks for equivalent functions that are mergable and folds them.
00011 //
00012 // A hash is computed from the function, based on its type and number of
00013 // basic blocks.
00014 //
00015 // Once all hashes are computed, we perform an expensive equality comparison
00016 // on each function pair. This takes n^2/2 comparisons per bucket, so it's
00017 // important that the hash function be high quality. The equality comparison
00018 // iterates through each instruction in each basic block.
00019 //
00020 // When a match is found the functions are folded. If both functions are
00021 // overridable, we move the functionality into a new internal function and
00022 // leave two overridable thunks to it.
00023 //
00024 //===----------------------------------------------------------------------===//
00025 //
00026 // Future work:
00027 //
00028 // * virtual functions.
00029 //
00030 // Many functions have their address taken by the virtual function table for
00031 // the object they belong to. However, as long as it's only used for a lookup
00032 // and call, this is irrelevant, and we'd like to fold such functions.
00033 //
00034 // * switch from n^2 pair-wise comparisons to an n-way comparison for each
00035 // bucket.
00036 //
00037 // * be smarter about bitcasts.
00038 //
00039 // In order to fold functions, we will sometimes add either bitcast instructions
00040 // or bitcast constant expressions. Unfortunately, this can confound further
00041 // analysis since the two functions differ where one has a bitcast and the
00042 // other doesn't. We should learn to look through bitcasts.
00043 //
00044 //===----------------------------------------------------------------------===//
00045 
00046 #include "llvm/Transforms/IPO.h"
00047 #include "llvm/ADT/DenseSet.h"
00048 #include "llvm/ADT/FoldingSet.h"
00049 #include "llvm/ADT/STLExtras.h"
00050 #include "llvm/ADT/SmallSet.h"
00051 #include "llvm/ADT/Statistic.h"
00052 #include "llvm/IR/CallSite.h"
00053 #include "llvm/IR/Constants.h"
00054 #include "llvm/IR/DataLayout.h"
00055 #include "llvm/IR/IRBuilder.h"
00056 #include "llvm/IR/InlineAsm.h"
00057 #include "llvm/IR/Instructions.h"
00058 #include "llvm/IR/LLVMContext.h"
00059 #include "llvm/IR/Module.h"
00060 #include "llvm/IR/Operator.h"
00061 #include "llvm/IR/ValueHandle.h"
00062 #include "llvm/Pass.h"
00063 #include "llvm/Support/Debug.h"
00064 #include "llvm/Support/ErrorHandling.h"
00065 #include "llvm/Support/raw_ostream.h"
00066 #include <vector>
00067 using namespace llvm;
00068 
00069 #define DEBUG_TYPE "mergefunc"
00070 
00071 STATISTIC(NumFunctionsMerged, "Number of functions merged");
00072 STATISTIC(NumThunksWritten, "Number of thunks generated");
00073 STATISTIC(NumAliasesWritten, "Number of aliases generated");
00074 STATISTIC(NumDoubleWeak, "Number of new functions created");
00075 
00076 /// Returns the type id for a type to be hashed. We turn pointer types into
00077 /// integers here because the actual compare logic below considers pointers and
00078 /// integers of the same size as equal.
00079 static Type::TypeID getTypeIDForHash(Type *Ty) {
00080   if (Ty->isPointerTy())
00081     return Type::IntegerTyID;
00082   return Ty->getTypeID();
00083 }
00084 
00085 /// Creates a hash-code for the function which is the same for any two
00086 /// functions that will compare equal, without looking at the instructions
00087 /// inside the function.
00088 static unsigned profileFunction(const Function *F) {
00089   FunctionType *FTy = F->getFunctionType();
00090 
00091   FoldingSetNodeID ID;
00092   ID.AddInteger(F->size());
00093   ID.AddInteger(F->getCallingConv());
00094   ID.AddBoolean(F->hasGC());
00095   ID.AddBoolean(FTy->isVarArg());
00096   ID.AddInteger(getTypeIDForHash(FTy->getReturnType()));
00097   for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
00098     ID.AddInteger(getTypeIDForHash(FTy->getParamType(i)));
00099   return ID.ComputeHash();
00100 }
00101 
00102 namespace {
00103 
00104 /// ComparableFunction - A struct that pairs together functions with a
00105 /// DataLayout so that we can keep them together as elements in the DenseSet.
00106 class ComparableFunction {
00107 public:
00108   static const ComparableFunction EmptyKey;
00109   static const ComparableFunction TombstoneKey;
00110   static DataLayout * const LookupOnly;
00111 
00112   ComparableFunction(Function *Func, const DataLayout *DL)
00113     : Func(Func), Hash(profileFunction(Func)), DL(DL) {}
00114 
00115   Function *getFunc() const { return Func; }
00116   unsigned getHash() const { return Hash; }
00117   const DataLayout *getDataLayout() const { return DL; }
00118 
00119   // Drops AssertingVH reference to the function. Outside of debug mode, this
00120   // does nothing.
00121   void release() {
00122     assert(Func &&
00123            "Attempted to release function twice, or release empty/tombstone!");
00124     Func = NULL;
00125   }
00126 
00127 private:
00128   explicit ComparableFunction(unsigned Hash)
00129     : Func(NULL), Hash(Hash), DL(NULL) {}
00130 
00131   AssertingVH<Function> Func;
00132   unsigned Hash;
00133   const DataLayout *DL;
00134 };
00135 
00136 const ComparableFunction ComparableFunction::EmptyKey = ComparableFunction(0);
00137 const ComparableFunction ComparableFunction::TombstoneKey =
00138     ComparableFunction(1);
00139 DataLayout *const ComparableFunction::LookupOnly = (DataLayout*)(-1);
00140 
00141 }
00142 
00143 namespace llvm {
00144   template <>
00145   struct DenseMapInfo<ComparableFunction> {
00146     static ComparableFunction getEmptyKey() {
00147       return ComparableFunction::EmptyKey;
00148     }
00149     static ComparableFunction getTombstoneKey() {
00150       return ComparableFunction::TombstoneKey;
00151     }
00152     static unsigned getHashValue(const ComparableFunction &CF) {
00153       return CF.getHash();
00154     }
00155     static bool isEqual(const ComparableFunction &LHS,
00156                         const ComparableFunction &RHS);
00157   };
00158 }
00159 
00160 namespace {
00161 
00162 /// FunctionComparator - Compares two functions to determine whether or not
00163 /// they will generate machine code with the same behaviour. DataLayout is
00164 /// used if available. The comparator always fails conservatively (erring on the
00165 /// side of claiming that two functions are different).
00166 class FunctionComparator {
00167 public:
00168   FunctionComparator(const DataLayout *DL, const Function *F1,
00169                      const Function *F2)
00170     : F1(F1), F2(F2), DL(DL) {}
00171 
00172   /// Test whether the two functions have equivalent behaviour.
00173   bool compare();
00174 
00175 private:
00176   /// Test whether two basic blocks have equivalent behaviour.
00177   bool compare(const BasicBlock *BB1, const BasicBlock *BB2);
00178 
00179   /// Assign or look up previously assigned numbers for the two values, and
00180   /// return whether the numbers are equal. Numbers are assigned in the order
00181   /// visited.
00182   bool enumerate(const Value *V1, const Value *V2);
00183 
00184   /// Compare two Instructions for equivalence, similar to
00185   /// Instruction::isSameOperationAs but with modifications to the type
00186   /// comparison.
00187   bool isEquivalentOperation(const Instruction *I1,
00188                              const Instruction *I2) const;
00189 
00190   /// Compare two GEPs for equivalent pointer arithmetic.
00191   bool isEquivalentGEP(const GEPOperator *GEP1, const GEPOperator *GEP2);
00192   bool isEquivalentGEP(const GetElementPtrInst *GEP1,
00193                        const GetElementPtrInst *GEP2) {
00194     return isEquivalentGEP(cast<GEPOperator>(GEP1), cast<GEPOperator>(GEP2));
00195   }
00196 
00197   /// cmpType - compares two types,
00198   /// defines total ordering among the types set.
00199   ///
00200   /// Return values:
00201   /// 0 if types are equal,
00202   /// -1 if Left is less than Right,
00203   /// +1 if Left is greater than Right.
00204   ///
00205   /// Description:
00206   /// Comparison is broken onto stages. Like in lexicographical comparison
00207   /// stage coming first has higher priority.
00208   /// On each explanation stage keep in mind total ordering properties.
00209   ///
00210   /// 0. Before comparison we coerce pointer types of 0 address space to
00211   /// integer.
00212   /// We also don't bother with same type at left and right, so
00213   /// just return 0 in this case.
00214   ///
00215   /// 1. If types are of different kind (different type IDs).
00216   ///    Return result of type IDs comparison, treating them as numbers.
00217   /// 2. If types are vectors or integers, compare Type* values as numbers.
00218   /// 3. Types has same ID, so check whether they belongs to the next group:
00219   /// * Void
00220   /// * Float
00221   /// * Double
00222   /// * X86_FP80
00223   /// * FP128
00224   /// * PPC_FP128
00225   /// * Label
00226   /// * Metadata
00227   /// If so - return 0, yes - we can treat these types as equal only because
00228   /// their IDs are same.
00229   /// 4. If Left and Right are pointers, return result of address space
00230   /// comparison (numbers comparison). We can treat pointer types of same
00231   /// address space as equal.
00232   /// 5. If types are complex.
00233   /// Then both Left and Right are to be expanded and their element types will
00234   /// be checked with the same way. If we get Res != 0 on some stage, return it.
00235   /// Otherwise return 0.
00236   /// 6. For all other cases put llvm_unreachable.
00237   int cmpType(Type *TyL, Type *TyR) const;
00238 
00239   bool isEquivalentType(Type *Ty1, Type *Ty2) const {
00240     return cmpType(Ty1, Ty2) == 0;
00241   }
00242 
00243   int cmpNumbers(uint64_t L, uint64_t R) const;
00244 
00245   // The two functions undergoing comparison.
00246   const Function *F1, *F2;
00247 
00248   const DataLayout *DL;
00249 
00250   DenseMap<const Value *, const Value *> id_map;
00251   DenseSet<const Value *> seen_values;
00252 };
00253 
00254 }
00255 
00256 int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const {
00257   if (L < R) return -1;
00258   if (L > R) return 1;
00259   return 0;
00260 }
00261 
00262 /// cmpType - compares two types,
00263 /// defines total ordering among the types set.
00264 /// See method declaration comments for more details.
00265 int FunctionComparator::cmpType(Type *TyL, Type *TyR) const {
00266 
00267   PointerType *PTyL = dyn_cast<PointerType>(TyL);
00268   PointerType *PTyR = dyn_cast<PointerType>(TyR);
00269 
00270   if (DL) {
00271     if (PTyL && PTyL->getAddressSpace() == 0) TyL = DL->getIntPtrType(TyL);
00272     if (PTyR && PTyR->getAddressSpace() == 0) TyR = DL->getIntPtrType(TyR);
00273   }
00274 
00275   if (TyL == TyR)
00276     return 0;
00277 
00278   if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID()))
00279     return Res;
00280 
00281   switch (TyL->getTypeID()) {
00282   default:
00283     llvm_unreachable("Unknown type!");
00284     // Fall through in Release mode.
00285   case Type::IntegerTyID:
00286   case Type::VectorTyID:
00287     // TyL == TyR would have returned true earlier.
00288     return cmpNumbers((uint64_t)TyL, (uint64_t)TyR);
00289 
00290   case Type::VoidTyID:
00291   case Type::FloatTyID:
00292   case Type::DoubleTyID:
00293   case Type::X86_FP80TyID:
00294   case Type::FP128TyID:
00295   case Type::PPC_FP128TyID:
00296   case Type::LabelTyID:
00297   case Type::MetadataTyID:
00298     return 0;
00299 
00300   case Type::PointerTyID: {
00301     assert(PTyL && PTyR && "Both types must be pointers here.");
00302     return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace());
00303   }
00304 
00305   case Type::StructTyID: {
00306     StructType *STyL = cast<StructType>(TyL);
00307     StructType *STyR = cast<StructType>(TyR);
00308     if (STyL->getNumElements() != STyR->getNumElements())
00309       return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
00310 
00311     if (STyL->isPacked() != STyR->isPacked())
00312       return cmpNumbers(STyL->isPacked(), STyR->isPacked());
00313 
00314     for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) {
00315       if (int Res = cmpType(STyL->getElementType(i),
00316                             STyR->getElementType(i)))
00317         return Res;
00318     }
00319     return 0;
00320   }
00321 
00322   case Type::FunctionTyID: {
00323     FunctionType *FTyL = cast<FunctionType>(TyL);
00324     FunctionType *FTyR = cast<FunctionType>(TyR);
00325     if (FTyL->getNumParams() != FTyR->getNumParams())
00326       return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams());
00327 
00328     if (FTyL->isVarArg() != FTyR->isVarArg())
00329       return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg());
00330 
00331     if (int Res = cmpType(FTyL->getReturnType(), FTyR->getReturnType()))
00332       return Res;
00333 
00334     for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) {
00335       if (int Res = cmpType(FTyL->getParamType(i), FTyR->getParamType(i)))
00336         return Res;
00337     }
00338     return 0;
00339   }
00340 
00341   case Type::ArrayTyID: {
00342     ArrayType *ATyL = cast<ArrayType>(TyL);
00343     ArrayType *ATyR = cast<ArrayType>(TyR);
00344     if (ATyL->getNumElements() != ATyR->getNumElements())
00345       return cmpNumbers(ATyL->getNumElements(), ATyR->getNumElements());
00346     return cmpType(ATyL->getElementType(), ATyR->getElementType());
00347   }
00348   }
00349 }
00350 
00351 // Determine whether the two operations are the same except that pointer-to-A
00352 // and pointer-to-B are equivalent. This should be kept in sync with
00353 // Instruction::isSameOperationAs.
00354 bool FunctionComparator::isEquivalentOperation(const Instruction *I1,
00355                                                const Instruction *I2) const {
00356   // Differences from Instruction::isSameOperationAs:
00357   //  * replace type comparison with calls to isEquivalentType.
00358   //  * we test for I->hasSameSubclassOptionalData (nuw/nsw/tail) at the top
00359   //  * because of the above, we don't test for the tail bit on calls later on
00360   if (I1->getOpcode() != I2->getOpcode() ||
00361       I1->getNumOperands() != I2->getNumOperands() ||
00362       !isEquivalentType(I1->getType(), I2->getType()) ||
00363       !I1->hasSameSubclassOptionalData(I2))
00364     return false;
00365 
00366   // We have two instructions of identical opcode and #operands.  Check to see
00367   // if all operands are the same type
00368   for (unsigned i = 0, e = I1->getNumOperands(); i != e; ++i)
00369     if (!isEquivalentType(I1->getOperand(i)->getType(),
00370                           I2->getOperand(i)->getType()))
00371       return false;
00372 
00373   // Check special state that is a part of some instructions.
00374   if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
00375     return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
00376            LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() &&
00377            LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
00378            LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope();
00379   if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
00380     return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
00381            SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() &&
00382            SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
00383            SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope();
00384   if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
00385     return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
00386   if (const CallInst *CI = dyn_cast<CallInst>(I1))
00387     return CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
00388            CI->getAttributes() == cast<CallInst>(I2)->getAttributes();
00389   if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
00390     return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
00391            CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes();
00392   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
00393     return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
00394   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
00395     return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
00396   if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
00397     return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
00398            FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope();
00399   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
00400     return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
00401            CXI->getSuccessOrdering() ==
00402                cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
00403            CXI->getFailureOrdering() ==
00404                cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
00405            CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope();
00406   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
00407     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
00408            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
00409            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
00410            RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope();
00411 
00412   return true;
00413 }
00414 
00415 // Determine whether two GEP operations perform the same underlying arithmetic.
00416 bool FunctionComparator::isEquivalentGEP(const GEPOperator *GEP1,
00417                                          const GEPOperator *GEP2) {
00418   unsigned AS = GEP1->getPointerAddressSpace();
00419   if (AS != GEP2->getPointerAddressSpace())
00420     return false;
00421 
00422   if (DL) {
00423     // When we have target data, we can reduce the GEP down to the value in bytes
00424     // added to the address.
00425     unsigned BitWidth = DL ? DL->getPointerSizeInBits(AS) : 1;
00426     APInt Offset1(BitWidth, 0), Offset2(BitWidth, 0);
00427     if (GEP1->accumulateConstantOffset(*DL, Offset1) &&
00428         GEP2->accumulateConstantOffset(*DL, Offset2)) {
00429       return Offset1 == Offset2;
00430     }
00431   }
00432 
00433   if (GEP1->getPointerOperand()->getType() !=
00434       GEP2->getPointerOperand()->getType())
00435     return false;
00436 
00437   if (GEP1->getNumOperands() != GEP2->getNumOperands())
00438     return false;
00439 
00440   for (unsigned i = 0, e = GEP1->getNumOperands(); i != e; ++i) {
00441     if (!enumerate(GEP1->getOperand(i), GEP2->getOperand(i)))
00442       return false;
00443   }
00444 
00445   return true;
00446 }
00447 
00448 // Compare two values used by the two functions under pair-wise comparison. If
00449 // this is the first time the values are seen, they're added to the mapping so
00450 // that we will detect mismatches on next use.
00451 bool FunctionComparator::enumerate(const Value *V1, const Value *V2) {
00452   // Check for function @f1 referring to itself and function @f2 referring to
00453   // itself, or referring to each other, or both referring to either of them.
00454   // They're all equivalent if the two functions are otherwise equivalent.
00455   if (V1 == F1 && V2 == F2)
00456     return true;
00457   if (V1 == F2 && V2 == F1)
00458     return true;
00459 
00460   if (const Constant *C1 = dyn_cast<Constant>(V1)) {
00461     if (V1 == V2) return true;
00462     const Constant *C2 = dyn_cast<Constant>(V2);
00463     if (!C2) return false;
00464     // TODO: constant expressions with GEP or references to F1 or F2.
00465     if (C1->isNullValue() && C2->isNullValue() &&
00466         isEquivalentType(C1->getType(), C2->getType()))
00467       return true;
00468     // Try bitcasting C2 to C1's type. If the bitcast is legal and returns C1
00469     // then they must have equal bit patterns.
00470     return C1->getType()->canLosslesslyBitCastTo(C2->getType()) &&
00471       C1 == ConstantExpr::getBitCast(const_cast<Constant*>(C2), C1->getType());
00472   }
00473 
00474   if (isa<InlineAsm>(V1) || isa<InlineAsm>(V2))
00475     return V1 == V2;
00476 
00477   // Check that V1 maps to V2. If we find a value that V1 maps to then we simply
00478   // check whether it's equal to V2. When there is no mapping then we need to
00479   // ensure that V2 isn't already equivalent to something else. For this
00480   // purpose, we track the V2 values in a set.
00481 
00482   const Value *&map_elem = id_map[V1];
00483   if (map_elem)
00484     return map_elem == V2;
00485   if (!seen_values.insert(V2).second)
00486     return false;
00487   map_elem = V2;
00488   return true;
00489 }
00490 
00491 // Test whether two basic blocks have equivalent behaviour.
00492 bool FunctionComparator::compare(const BasicBlock *BB1, const BasicBlock *BB2) {
00493   BasicBlock::const_iterator F1I = BB1->begin(), F1E = BB1->end();
00494   BasicBlock::const_iterator F2I = BB2->begin(), F2E = BB2->end();
00495 
00496   do {
00497     if (!enumerate(F1I, F2I))
00498       return false;
00499 
00500     if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) {
00501       const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I);
00502       if (!GEP2)
00503         return false;
00504 
00505       if (!enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand()))
00506         return false;
00507 
00508       if (!isEquivalentGEP(GEP1, GEP2))
00509         return false;
00510     } else {
00511       if (!isEquivalentOperation(F1I, F2I))
00512         return false;
00513 
00514       assert(F1I->getNumOperands() == F2I->getNumOperands());
00515       for (unsigned i = 0, e = F1I->getNumOperands(); i != e; ++i) {
00516         Value *OpF1 = F1I->getOperand(i);
00517         Value *OpF2 = F2I->getOperand(i);
00518 
00519         if (!enumerate(OpF1, OpF2))
00520           return false;
00521 
00522         if (OpF1->getValueID() != OpF2->getValueID() ||
00523             !isEquivalentType(OpF1->getType(), OpF2->getType()))
00524           return false;
00525       }
00526     }
00527 
00528     ++F1I, ++F2I;
00529   } while (F1I != F1E && F2I != F2E);
00530 
00531   return F1I == F1E && F2I == F2E;
00532 }
00533 
00534 // Test whether the two functions have equivalent behaviour.
00535 bool FunctionComparator::compare() {
00536   // We need to recheck everything, but check the things that weren't included
00537   // in the hash first.
00538 
00539   if (F1->getAttributes() != F2->getAttributes())
00540     return false;
00541 
00542   if (F1->hasGC() != F2->hasGC())
00543     return false;
00544 
00545   if (F1->hasGC() && F1->getGC() != F2->getGC())
00546     return false;
00547 
00548   if (F1->hasSection() != F2->hasSection())
00549     return false;
00550 
00551   if (F1->hasSection() && F1->getSection() != F2->getSection())
00552     return false;
00553 
00554   if (F1->isVarArg() != F2->isVarArg())
00555     return false;
00556 
00557   // TODO: if it's internal and only used in direct calls, we could handle this
00558   // case too.
00559   if (F1->getCallingConv() != F2->getCallingConv())
00560     return false;
00561 
00562   if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType()))
00563     return false;
00564 
00565   assert(F1->arg_size() == F2->arg_size() &&
00566          "Identically typed functions have different numbers of args!");
00567 
00568   // Visit the arguments so that they get enumerated in the order they're
00569   // passed in.
00570   for (Function::const_arg_iterator f1i = F1->arg_begin(),
00571          f2i = F2->arg_begin(), f1e = F1->arg_end(); f1i != f1e; ++f1i, ++f2i) {
00572     if (!enumerate(f1i, f2i))
00573       llvm_unreachable("Arguments repeat!");
00574   }
00575 
00576   // We do a CFG-ordered walk since the actual ordering of the blocks in the
00577   // linked list is immaterial. Our walk starts at the entry block for both
00578   // functions, then takes each block from each terminator in order. As an
00579   // artifact, this also means that unreachable blocks are ignored.
00580   SmallVector<const BasicBlock *, 8> F1BBs, F2BBs;
00581   SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1.
00582 
00583   F1BBs.push_back(&F1->getEntryBlock());
00584   F2BBs.push_back(&F2->getEntryBlock());
00585 
00586   VisitedBBs.insert(F1BBs[0]);
00587   while (!F1BBs.empty()) {
00588     const BasicBlock *F1BB = F1BBs.pop_back_val();
00589     const BasicBlock *F2BB = F2BBs.pop_back_val();
00590 
00591     if (!enumerate(F1BB, F2BB) || !compare(F1BB, F2BB))
00592       return false;
00593 
00594     const TerminatorInst *F1TI = F1BB->getTerminator();
00595     const TerminatorInst *F2TI = F2BB->getTerminator();
00596 
00597     assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors());
00598     for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) {
00599       if (!VisitedBBs.insert(F1TI->getSuccessor(i)))
00600         continue;
00601 
00602       F1BBs.push_back(F1TI->getSuccessor(i));
00603       F2BBs.push_back(F2TI->getSuccessor(i));
00604     }
00605   }
00606   return true;
00607 }
00608 
00609 namespace {
00610 
00611 /// MergeFunctions finds functions which will generate identical machine code,
00612 /// by considering all pointer types to be equivalent. Once identified,
00613 /// MergeFunctions will fold them by replacing a call to one to a call to a
00614 /// bitcast of the other.
00615 ///
00616 class MergeFunctions : public ModulePass {
00617 public:
00618   static char ID;
00619   MergeFunctions()
00620     : ModulePass(ID), HasGlobalAliases(false) {
00621     initializeMergeFunctionsPass(*PassRegistry::getPassRegistry());
00622   }
00623 
00624   bool runOnModule(Module &M) override;
00625 
00626 private:
00627   typedef DenseSet<ComparableFunction> FnSetType;
00628 
00629   /// A work queue of functions that may have been modified and should be
00630   /// analyzed again.
00631   std::vector<WeakVH> Deferred;
00632 
00633   /// Insert a ComparableFunction into the FnSet, or merge it away if it's
00634   /// equal to one that's already present.
00635   bool insert(ComparableFunction &NewF);
00636 
00637   /// Remove a Function from the FnSet and queue it up for a second sweep of
00638   /// analysis.
00639   void remove(Function *F);
00640 
00641   /// Find the functions that use this Value and remove them from FnSet and
00642   /// queue the functions.
00643   void removeUsers(Value *V);
00644 
00645   /// Replace all direct calls of Old with calls of New. Will bitcast New if
00646   /// necessary to make types match.
00647   void replaceDirectCallers(Function *Old, Function *New);
00648 
00649   /// Merge two equivalent functions. Upon completion, G may be deleted, or may
00650   /// be converted into a thunk. In either case, it should never be visited
00651   /// again.
00652   void mergeTwoFunctions(Function *F, Function *G);
00653 
00654   /// Replace G with a thunk or an alias to F. Deletes G.
00655   void writeThunkOrAlias(Function *F, Function *G);
00656 
00657   /// Replace G with a simple tail call to bitcast(F). Also replace direct uses
00658   /// of G with bitcast(F). Deletes G.
00659   void writeThunk(Function *F, Function *G);
00660 
00661   /// Replace G with an alias to F. Deletes G.
00662   void writeAlias(Function *F, Function *G);
00663 
00664   /// The set of all distinct functions. Use the insert() and remove() methods
00665   /// to modify it.
00666   FnSetType FnSet;
00667 
00668   /// DataLayout for more accurate GEP comparisons. May be NULL.
00669   const DataLayout *DL;
00670 
00671   /// Whether or not the target supports global aliases.
00672   bool HasGlobalAliases;
00673 };
00674 
00675 }  // end anonymous namespace
00676 
00677 char MergeFunctions::ID = 0;
00678 INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false)
00679 
00680 ModulePass *llvm::createMergeFunctionsPass() {
00681   return new MergeFunctions();
00682 }
00683 
00684 bool MergeFunctions::runOnModule(Module &M) {
00685   bool Changed = false;
00686   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
00687   DL = DLP ? &DLP->getDataLayout() : 0;
00688 
00689   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
00690     if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
00691       Deferred.push_back(WeakVH(I));
00692   }
00693   FnSet.resize(Deferred.size());
00694 
00695   do {
00696     std::vector<WeakVH> Worklist;
00697     Deferred.swap(Worklist);
00698 
00699     DEBUG(dbgs() << "size of module: " << M.size() << '\n');
00700     DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n');
00701 
00702     // Insert only strong functions and merge them. Strong function merging
00703     // always deletes one of them.
00704     for (std::vector<WeakVH>::iterator I = Worklist.begin(),
00705            E = Worklist.end(); I != E; ++I) {
00706       if (!*I) continue;
00707       Function *F = cast<Function>(*I);
00708       if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
00709           !F->mayBeOverridden()) {
00710         ComparableFunction CF = ComparableFunction(F, DL);
00711         Changed |= insert(CF);
00712       }
00713     }
00714 
00715     // Insert only weak functions and merge them. By doing these second we
00716     // create thunks to the strong function when possible. When two weak
00717     // functions are identical, we create a new strong function with two weak
00718     // weak thunks to it which are identical but not mergable.
00719     for (std::vector<WeakVH>::iterator I = Worklist.begin(),
00720            E = Worklist.end(); I != E; ++I) {
00721       if (!*I) continue;
00722       Function *F = cast<Function>(*I);
00723       if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
00724           F->mayBeOverridden()) {
00725         ComparableFunction CF = ComparableFunction(F, DL);
00726         Changed |= insert(CF);
00727       }
00728     }
00729     DEBUG(dbgs() << "size of FnSet: " << FnSet.size() << '\n');
00730   } while (!Deferred.empty());
00731 
00732   FnSet.clear();
00733 
00734   return Changed;
00735 }
00736 
00737 bool DenseMapInfo<ComparableFunction>::isEqual(const ComparableFunction &LHS,
00738                                                const ComparableFunction &RHS) {
00739   if (LHS.getFunc() == RHS.getFunc() &&
00740       LHS.getHash() == RHS.getHash())
00741     return true;
00742   if (!LHS.getFunc() || !RHS.getFunc())
00743     return false;
00744 
00745   // One of these is a special "underlying pointer comparison only" object.
00746   if (LHS.getDataLayout() == ComparableFunction::LookupOnly ||
00747       RHS.getDataLayout() == ComparableFunction::LookupOnly)
00748     return false;
00749 
00750   assert(LHS.getDataLayout() == RHS.getDataLayout() &&
00751          "Comparing functions for different targets");
00752 
00753   return FunctionComparator(LHS.getDataLayout(), LHS.getFunc(),
00754                             RHS.getFunc()).compare();
00755 }
00756 
00757 // Replace direct callers of Old with New.
00758 void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
00759   Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType());
00760   for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) {
00761     Use *U = &*UI;
00762     ++UI;
00763     CallSite CS(U->getUser());
00764     if (CS && CS.isCallee(U)) {
00765       remove(CS.getInstruction()->getParent()->getParent());
00766       U->set(BitcastNew);
00767     }
00768   }
00769 }
00770 
00771 // Replace G with an alias to F if possible, or else a thunk to F. Deletes G.
00772 void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {
00773   if (HasGlobalAliases && G->hasUnnamedAddr()) {
00774     if (G->hasExternalLinkage() || G->hasLocalLinkage() ||
00775         G->hasWeakLinkage()) {
00776       writeAlias(F, G);
00777       return;
00778     }
00779   }
00780 
00781   writeThunk(F, G);
00782 }
00783 
00784 // Helper for writeThunk,
00785 // Selects proper bitcast operation,
00786 // but a bit simpler then CastInst::getCastOpcode.
00787 static Value* createCast(IRBuilder<false> &Builder, Value *V, Type *DestTy) {
00788   Type *SrcTy = V->getType();
00789   if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
00790     return Builder.CreateIntToPtr(V, DestTy);
00791   else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
00792     return Builder.CreatePtrToInt(V, DestTy);
00793   else
00794     return Builder.CreateBitCast(V, DestTy);
00795 }
00796 
00797 // Replace G with a simple tail call to bitcast(F). Also replace direct uses
00798 // of G with bitcast(F). Deletes G.
00799 void MergeFunctions::writeThunk(Function *F, Function *G) {
00800   if (!G->mayBeOverridden()) {
00801     // Redirect direct callers of G to F.
00802     replaceDirectCallers(G, F);
00803   }
00804 
00805   // If G was internal then we may have replaced all uses of G with F. If so,
00806   // stop here and delete G. There's no need for a thunk.
00807   if (G->hasLocalLinkage() && G->use_empty()) {
00808     G->eraseFromParent();
00809     return;
00810   }
00811 
00812   Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
00813                                     G->getParent());
00814   BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
00815   IRBuilder<false> Builder(BB);
00816 
00817   SmallVector<Value *, 16> Args;
00818   unsigned i = 0;
00819   FunctionType *FFTy = F->getFunctionType();
00820   for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end();
00821        AI != AE; ++AI) {
00822     Args.push_back(createCast(Builder, (Value*)AI, FFTy->getParamType(i)));
00823     ++i;
00824   }
00825 
00826   CallInst *CI = Builder.CreateCall(F, Args);
00827   CI->setTailCall();
00828   CI->setCallingConv(F->getCallingConv());
00829   if (NewG->getReturnType()->isVoidTy()) {
00830     Builder.CreateRetVoid();
00831   } else {
00832     Builder.CreateRet(createCast(Builder, CI, NewG->getReturnType()));
00833   }
00834 
00835   NewG->copyAttributesFrom(G);
00836   NewG->takeName(G);
00837   removeUsers(G);
00838   G->replaceAllUsesWith(NewG);
00839   G->eraseFromParent();
00840 
00841   DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n');
00842   ++NumThunksWritten;
00843 }
00844 
00845 // Replace G with an alias to F and delete G.
00846 void MergeFunctions::writeAlias(Function *F, Function *G) {
00847   Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
00848   GlobalAlias *GA = new GlobalAlias(G->getType(), G->getLinkage(), "",
00849                                     BitcastF, G->getParent());
00850   F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
00851   GA->takeName(G);
00852   GA->setVisibility(G->getVisibility());
00853   removeUsers(G);
00854   G->replaceAllUsesWith(GA);
00855   G->eraseFromParent();
00856 
00857   DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n');
00858   ++NumAliasesWritten;
00859 }
00860 
00861 // Merge two equivalent functions. Upon completion, Function G is deleted.
00862 void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
00863   if (F->mayBeOverridden()) {
00864     assert(G->mayBeOverridden());
00865 
00866     if (HasGlobalAliases) {
00867       // Make them both thunks to the same internal function.
00868       Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
00869                                      F->getParent());
00870       H->copyAttributesFrom(F);
00871       H->takeName(F);
00872       removeUsers(F);
00873       F->replaceAllUsesWith(H);
00874 
00875       unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
00876 
00877       writeAlias(F, G);
00878       writeAlias(F, H);
00879 
00880       F->setAlignment(MaxAlignment);
00881       F->setLinkage(GlobalValue::PrivateLinkage);
00882     } else {
00883       // We can't merge them. Instead, pick one and update all direct callers
00884       // to call it and hope that we improve the instruction cache hit rate.
00885       replaceDirectCallers(G, F);
00886     }
00887 
00888     ++NumDoubleWeak;
00889   } else {
00890     writeThunkOrAlias(F, G);
00891   }
00892 
00893   ++NumFunctionsMerged;
00894 }
00895 
00896 // Insert a ComparableFunction into the FnSet, or merge it away if equal to one
00897 // that was already inserted.
00898 bool MergeFunctions::insert(ComparableFunction &NewF) {
00899   std::pair<FnSetType::iterator, bool> Result = FnSet.insert(NewF);
00900   if (Result.second) {
00901     DEBUG(dbgs() << "Inserting as unique: " << NewF.getFunc()->getName() << '\n');
00902     return false;
00903   }
00904 
00905   const ComparableFunction &OldF = *Result.first;
00906 
00907   // Don't merge tiny functions, since it can just end up making the function
00908   // larger.
00909   // FIXME: Should still merge them if they are unnamed_addr and produce an
00910   // alias.
00911   if (NewF.getFunc()->size() == 1) {
00912     if (NewF.getFunc()->front().size() <= 2) {
00913       DEBUG(dbgs() << NewF.getFunc()->getName()
00914             << " is to small to bother merging\n");
00915       return false;
00916     }
00917   }
00918 
00919   // Never thunk a strong function to a weak function.
00920   assert(!OldF.getFunc()->mayBeOverridden() ||
00921          NewF.getFunc()->mayBeOverridden());
00922 
00923   DEBUG(dbgs() << "  " << OldF.getFunc()->getName() << " == "
00924                << NewF.getFunc()->getName() << '\n');
00925 
00926   Function *DeleteF = NewF.getFunc();
00927   NewF.release();
00928   mergeTwoFunctions(OldF.getFunc(), DeleteF);
00929   return true;
00930 }
00931 
00932 // Remove a function from FnSet. If it was already in FnSet, add it to Deferred
00933 // so that we'll look at it in the next round.
00934 void MergeFunctions::remove(Function *F) {
00935   // We need to make sure we remove F, not a function "equal" to F per the
00936   // function equality comparator.
00937   //
00938   // The special "lookup only" ComparableFunction bypasses the expensive
00939   // function comparison in favour of a pointer comparison on the underlying
00940   // Function*'s.
00941   ComparableFunction CF = ComparableFunction(F, ComparableFunction::LookupOnly);
00942   if (FnSet.erase(CF)) {
00943     DEBUG(dbgs() << "Removed " << F->getName() << " from set and deferred it.\n");
00944     Deferred.push_back(F);
00945   }
00946 }
00947 
00948 // For each instruction used by the value, remove() the function that contains
00949 // the instruction. This should happen right before a call to RAUW.
00950 void MergeFunctions::removeUsers(Value *V) {
00951   std::vector<Value *> Worklist;
00952   Worklist.push_back(V);
00953   while (!Worklist.empty()) {
00954     Value *V = Worklist.back();
00955     Worklist.pop_back();
00956 
00957     for (User *U : V->users()) {
00958       if (Instruction *I = dyn_cast<Instruction>(U)) {
00959         remove(I->getParent()->getParent());
00960       } else if (isa<GlobalValue>(U)) {
00961         // do nothing
00962       } else if (Constant *C = dyn_cast<Constant>(U)) {
00963         for (User *UU : C->users())
00964           Worklist.push_back(UU);
00965       }
00966     }
00967   }
00968 }