Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1 | //===- MergeFunctions.cpp - Merge identical functions ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass looks for equivalent functions that are mergable and folds them. |
| 11 | // |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 12 | // A hash is computed from the function, based on its type and number of |
| 13 | // basic blocks. |
| 14 | // |
| 15 | // Once all hashes are computed, we perform an expensive equality comparison |
| 16 | // on each function pair. This takes n^2/2 comparisons per bucket, so it's |
| 17 | // important that the hash function be high quality. The equality comparison |
| 18 | // iterates through each instruction in each basic block. |
| 19 | // |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 20 | // When a match is found the functions are folded. If both functions are |
| 21 | // overridable, we move the functionality into a new internal function and |
| 22 | // leave two overridable thunks to it. |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 23 | // |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // |
| 26 | // Future work: |
| 27 | // |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 28 | // * virtual functions. |
| 29 | // |
| 30 | // Many functions have their address taken by the virtual function table for |
| 31 | // the object they belong to. However, as long as it's only used for a lookup |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 32 | // and call, this is irrelevant, and we'd like to fold such functions. |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 33 | // |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 34 | // * switch from n^2 pair-wise comparisons to an n-way comparison for each |
| 35 | // bucket. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 36 | // |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 37 | // * be smarter about bitcasts. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 38 | // |
| 39 | // In order to fold functions, we will sometimes add either bitcast instructions |
| 40 | // or bitcast constant expressions. Unfortunately, this can confound further |
| 41 | // analysis since the two functions differ where one has a bitcast and the |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 42 | // other doesn't. We should learn to look through bitcasts. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 43 | // |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | #define DEBUG_TYPE "mergefunc" |
| 47 | #include "llvm/Transforms/IPO.h" |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/DenseSet.h" |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/FoldingSet.h" |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/SmallSet.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/STLExtras.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 53 | #include "llvm/Constants.h" |
| 54 | #include "llvm/InlineAsm.h" |
| 55 | #include "llvm/Instructions.h" |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 56 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 57 | #include "llvm/Module.h" |
| 58 | #include "llvm/Pass.h" |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 59 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 60 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 61 | #include "llvm/Support/ErrorHandling.h" |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 62 | #include "llvm/Support/IRBuilder.h" |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 63 | #include "llvm/Support/ValueHandle.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 64 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 65 | #include "llvm/Target/TargetData.h" |
Nick Lewycky | 65a0af3 | 2010-08-31 08:29:37 +0000 | [diff] [blame^] | 66 | #include <vector> |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 67 | using namespace llvm; |
| 68 | |
| 69 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 70 | |
| 71 | namespace { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 72 | /// MergeFunctions finds functions which will generate identical machine code, |
| 73 | /// by considering all pointer types to be equivalent. Once identified, |
| 74 | /// MergeFunctions will fold them by replacing a call to one to a call to a |
| 75 | /// bitcast of the other. |
| 76 | /// |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 77 | class MergeFunctions : public ModulePass { |
| 78 | public: |
| 79 | static char ID; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 80 | MergeFunctions() : ModulePass(ID) {} |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 81 | |
| 82 | bool runOnModule(Module &M); |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 85 | /// MergeTwoFunctions - Merge two equivalent functions. Upon completion, G |
Nick Lewycky | 65a0af3 | 2010-08-31 08:29:37 +0000 | [diff] [blame^] | 86 | /// may be deleted, or may be converted into a thunk. In either case, it |
| 87 | /// should never be visited again. |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 88 | void MergeTwoFunctions(Function *F, Function *G) const; |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 89 | |
| 90 | /// WriteThunk - Replace G with a simple tail call to bitcast(F). Also |
| 91 | /// replace direct uses of G with bitcast(F). |
| 92 | void WriteThunk(Function *F, Function *G) const; |
| 93 | |
| 94 | TargetData *TD; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 95 | }; |
| 96 | } |
| 97 | |
| 98 | char MergeFunctions::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 99 | INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 100 | |
| 101 | ModulePass *llvm::createMergeFunctionsPass() { |
| 102 | return new MergeFunctions(); |
| 103 | } |
| 104 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 105 | namespace { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 106 | /// FunctionComparator - Compares two functions to determine whether or not |
| 107 | /// they will generate machine code with the same behaviour. TargetData is |
| 108 | /// used if available. The comparator always fails conservatively (erring on the |
| 109 | /// side of claiming that two functions are different). |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 110 | class FunctionComparator { |
| 111 | public: |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 112 | FunctionComparator(const TargetData *TD, const Function *F1, |
| 113 | const Function *F2) |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 114 | : F1(F1), F2(F2), TD(TD), IDMap1Count(0), IDMap2Count(0) {} |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 115 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 116 | /// Compare - test whether the two functions have equivalent behaviour. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 117 | bool Compare(); |
| 118 | |
| 119 | private: |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 120 | /// Compare - test whether two basic blocks have equivalent behaviour. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 121 | bool Compare(const BasicBlock *BB1, const BasicBlock *BB2); |
| 122 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 123 | /// Enumerate - Assign or look up previously assigned numbers for the two |
| 124 | /// values, and return whether the numbers are equal. Numbers are assigned in |
| 125 | /// the order visited. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 126 | bool Enumerate(const Value *V1, const Value *V2); |
| 127 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 128 | /// isEquivalentOperation - Compare two Instructions for equivalence, similar |
| 129 | /// to Instruction::isSameOperationAs but with modifications to the type |
| 130 | /// comparison. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 131 | bool isEquivalentOperation(const Instruction *I1, |
| 132 | const Instruction *I2) const; |
| 133 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 134 | /// isEquivalentGEP - Compare two GEPs for equivalent pointer arithmetic. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 135 | bool isEquivalentGEP(const GEPOperator *GEP1, const GEPOperator *GEP2); |
| 136 | bool isEquivalentGEP(const GetElementPtrInst *GEP1, |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 137 | const GetElementPtrInst *GEP2) { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 138 | return isEquivalentGEP(cast<GEPOperator>(GEP1), cast<GEPOperator>(GEP2)); |
| 139 | } |
| 140 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 141 | /// isEquivalentType - Compare two Types, treating all pointer types as equal. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 142 | bool isEquivalentType(const Type *Ty1, const Type *Ty2) const; |
| 143 | |
| 144 | // The two functions undergoing comparison. |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 145 | const Function *F1, *F2; |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 146 | |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 147 | const TargetData *TD; |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 148 | |
| 149 | typedef DenseMap<const Value *, unsigned long> IDMap; |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 150 | IDMap Map1, Map2; |
| 151 | unsigned long IDMap1Count, IDMap2Count; |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 152 | }; |
| 153 | } |
| 154 | |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 155 | /// isEquivalentType - any two pointers in the same address space are |
| 156 | /// equivalent. Otherwise, standard type equivalence rules apply. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 157 | bool FunctionComparator::isEquivalentType(const Type *Ty1, |
| 158 | const Type *Ty2) const { |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 159 | if (Ty1 == Ty2) |
| 160 | return true; |
| 161 | if (Ty1->getTypeID() != Ty2->getTypeID()) |
| 162 | return false; |
| 163 | |
| 164 | switch(Ty1->getTypeID()) { |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 165 | default: |
| 166 | llvm_unreachable("Unknown type!"); |
Duncan Sands | 8246adc | 2010-07-07 07:48:00 +0000 | [diff] [blame] | 167 | // Fall through in Release mode. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 168 | case Type::IntegerTyID: |
| 169 | case Type::OpaqueTyID: |
| 170 | // Ty1 == Ty2 would have returned true earlier. |
| 171 | return false; |
| 172 | |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 173 | case Type::VoidTyID: |
| 174 | case Type::FloatTyID: |
| 175 | case Type::DoubleTyID: |
| 176 | case Type::X86_FP80TyID: |
| 177 | case Type::FP128TyID: |
| 178 | case Type::PPC_FP128TyID: |
| 179 | case Type::LabelTyID: |
| 180 | case Type::MetadataTyID: |
| 181 | return true; |
| 182 | |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 183 | case Type::PointerTyID: { |
| 184 | const PointerType *PTy1 = cast<PointerType>(Ty1); |
| 185 | const PointerType *PTy2 = cast<PointerType>(Ty2); |
| 186 | return PTy1->getAddressSpace() == PTy2->getAddressSpace(); |
| 187 | } |
| 188 | |
| 189 | case Type::StructTyID: { |
| 190 | const StructType *STy1 = cast<StructType>(Ty1); |
| 191 | const StructType *STy2 = cast<StructType>(Ty2); |
| 192 | if (STy1->getNumElements() != STy2->getNumElements()) |
| 193 | return false; |
| 194 | |
| 195 | if (STy1->isPacked() != STy2->isPacked()) |
| 196 | return false; |
| 197 | |
| 198 | for (unsigned i = 0, e = STy1->getNumElements(); i != e; ++i) { |
| 199 | if (!isEquivalentType(STy1->getElementType(i), STy2->getElementType(i))) |
| 200 | return false; |
| 201 | } |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | case Type::FunctionTyID: { |
| 206 | const FunctionType *FTy1 = cast<FunctionType>(Ty1); |
| 207 | const FunctionType *FTy2 = cast<FunctionType>(Ty2); |
| 208 | if (FTy1->getNumParams() != FTy2->getNumParams() || |
| 209 | FTy1->isVarArg() != FTy2->isVarArg()) |
| 210 | return false; |
| 211 | |
| 212 | if (!isEquivalentType(FTy1->getReturnType(), FTy2->getReturnType())) |
| 213 | return false; |
| 214 | |
| 215 | for (unsigned i = 0, e = FTy1->getNumParams(); i != e; ++i) { |
| 216 | if (!isEquivalentType(FTy1->getParamType(i), FTy2->getParamType(i))) |
| 217 | return false; |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
Nick Lewycky | 394ce41 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 222 | case Type::ArrayTyID: { |
| 223 | const ArrayType *ATy1 = cast<ArrayType>(Ty1); |
| 224 | const ArrayType *ATy2 = cast<ArrayType>(Ty2); |
| 225 | return ATy1->getNumElements() == ATy2->getNumElements() && |
| 226 | isEquivalentType(ATy1->getElementType(), ATy2->getElementType()); |
| 227 | } |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 228 | |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 229 | case Type::VectorTyID: { |
Nick Lewycky | 394ce41 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 230 | const VectorType *VTy1 = cast<VectorType>(Ty1); |
| 231 | const VectorType *VTy2 = cast<VectorType>(Ty2); |
| 232 | return VTy1->getNumElements() == VTy2->getNumElements() && |
| 233 | isEquivalentType(VTy1->getElementType(), VTy2->getElementType()); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /// isEquivalentOperation - determine whether the two operations are the same |
| 239 | /// except that pointer-to-A and pointer-to-B are equivalent. This should be |
Dan Gohman | 194ae78 | 2009-06-12 19:03:05 +0000 | [diff] [blame] | 240 | /// kept in sync with Instruction::isSameOperationAs. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 241 | bool FunctionComparator::isEquivalentOperation(const Instruction *I1, |
| 242 | const Instruction *I2) const { |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 243 | if (I1->getOpcode() != I2->getOpcode() || |
| 244 | I1->getNumOperands() != I2->getNumOperands() || |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 245 | !isEquivalentType(I1->getType(), I2->getType()) || |
| 246 | !I1->hasSameSubclassOptionalData(I2)) |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 247 | return false; |
| 248 | |
| 249 | // We have two instructions of identical opcode and #operands. Check to see |
| 250 | // if all operands are the same type |
| 251 | for (unsigned i = 0, e = I1->getNumOperands(); i != e; ++i) |
| 252 | if (!isEquivalentType(I1->getOperand(i)->getType(), |
| 253 | I2->getOperand(i)->getType())) |
| 254 | return false; |
| 255 | |
| 256 | // Check special state that is a part of some instructions. |
| 257 | if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) |
| 258 | return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && |
| 259 | LI->getAlignment() == cast<LoadInst>(I2)->getAlignment(); |
| 260 | if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) |
| 261 | return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && |
| 262 | SI->getAlignment() == cast<StoreInst>(I2)->getAlignment(); |
| 263 | if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) |
| 264 | return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); |
| 265 | if (const CallInst *CI = dyn_cast<CallInst>(I1)) |
| 266 | return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && |
| 267 | CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && |
| 268 | CI->getAttributes().getRawPointer() == |
| 269 | cast<CallInst>(I2)->getAttributes().getRawPointer(); |
| 270 | if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) |
| 271 | return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && |
| 272 | CI->getAttributes().getRawPointer() == |
| 273 | cast<InvokeInst>(I2)->getAttributes().getRawPointer(); |
| 274 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) { |
| 275 | if (IVI->getNumIndices() != cast<InsertValueInst>(I2)->getNumIndices()) |
| 276 | return false; |
| 277 | for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i) |
| 278 | if (IVI->idx_begin()[i] != cast<InsertValueInst>(I2)->idx_begin()[i]) |
| 279 | return false; |
| 280 | return true; |
| 281 | } |
| 282 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) { |
| 283 | if (EVI->getNumIndices() != cast<ExtractValueInst>(I2)->getNumIndices()) |
| 284 | return false; |
| 285 | for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i) |
| 286 | if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I2)->idx_begin()[i]) |
| 287 | return false; |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | return true; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 294 | /// isEquivalentGEP - determine whether two GEP operations perform the same |
| 295 | /// underlying arithmetic. |
| 296 | bool FunctionComparator::isEquivalentGEP(const GEPOperator *GEP1, |
| 297 | const GEPOperator *GEP2) { |
| 298 | // When we have target data, we can reduce the GEP down to the value in bytes |
| 299 | // added to the address. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 300 | if (TD && GEP1->hasAllConstantIndices() && GEP2->hasAllConstantIndices()) { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 301 | SmallVector<Value *, 8> Indices1(GEP1->idx_begin(), GEP1->idx_end()); |
| 302 | SmallVector<Value *, 8> Indices2(GEP2->idx_begin(), GEP2->idx_end()); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 303 | uint64_t Offset1 = TD->getIndexedOffset(GEP1->getPointerOperandType(), |
| 304 | Indices1.data(), Indices1.size()); |
| 305 | uint64_t Offset2 = TD->getIndexedOffset(GEP2->getPointerOperandType(), |
| 306 | Indices2.data(), Indices2.size()); |
| 307 | return Offset1 == Offset2; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 310 | if (GEP1->getPointerOperand()->getType() != |
| 311 | GEP2->getPointerOperand()->getType()) |
| 312 | return false; |
| 313 | |
| 314 | if (GEP1->getNumOperands() != GEP2->getNumOperands()) |
| 315 | return false; |
| 316 | |
| 317 | for (unsigned i = 0, e = GEP1->getNumOperands(); i != e; ++i) { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 318 | if (!Enumerate(GEP1->getOperand(i), GEP2->getOperand(i))) |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 319 | return false; |
| 320 | } |
| 321 | |
| 322 | return true; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 325 | /// Enumerate - Compare two values used by the two functions under pair-wise |
| 326 | /// comparison. If this is the first time the values are seen, they're added to |
| 327 | /// the mapping so that we will detect mismatches on next use. |
| 328 | bool FunctionComparator::Enumerate(const Value *V1, const Value *V2) { |
| 329 | // Check for function @f1 referring to itself and function @f2 referring to |
| 330 | // itself, or referring to each other, or both referring to either of them. |
| 331 | // They're all equivalent if the two functions are otherwise equivalent. |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 332 | if (V1 == F1 && V2 == F2) |
| 333 | return true; |
| 334 | if (V1 == F2 && V2 == F1) |
| 335 | return true; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 336 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 337 | // TODO: constant expressions with GEP or references to F1 or F2. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 338 | if (isa<Constant>(V1)) |
| 339 | return V1 == V2; |
| 340 | |
| 341 | if (isa<InlineAsm>(V1) && isa<InlineAsm>(V2)) { |
| 342 | const InlineAsm *IA1 = cast<InlineAsm>(V1); |
| 343 | const InlineAsm *IA2 = cast<InlineAsm>(V2); |
| 344 | return IA1->getAsmString() == IA2->getAsmString() && |
| 345 | IA1->getConstraintString() == IA2->getConstraintString(); |
| 346 | } |
| 347 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 348 | unsigned long &ID1 = Map1[V1]; |
| 349 | if (!ID1) |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 350 | ID1 = ++IDMap1Count; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 351 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 352 | unsigned long &ID2 = Map2[V2]; |
| 353 | if (!ID2) |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 354 | ID2 = ++IDMap2Count; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 355 | |
| 356 | return ID1 == ID2; |
| 357 | } |
| 358 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 359 | /// Compare - test whether two basic blocks have equivalent behaviour. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 360 | bool FunctionComparator::Compare(const BasicBlock *BB1, const BasicBlock *BB2) { |
| 361 | BasicBlock::const_iterator F1I = BB1->begin(), F1E = BB1->end(); |
| 362 | BasicBlock::const_iterator F2I = BB2->begin(), F2E = BB2->end(); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 363 | |
| 364 | do { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 365 | if (!Enumerate(F1I, F2I)) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 366 | return false; |
| 367 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 368 | if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) { |
| 369 | const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I); |
| 370 | if (!GEP2) |
| 371 | return false; |
Nick Lewycky | a142c93 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 372 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 373 | if (!Enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand())) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 374 | return false; |
Nick Lewycky | a142c93 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 375 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 376 | if (!isEquivalentGEP(GEP1, GEP2)) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 377 | return false; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 378 | } else { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 379 | if (!isEquivalentOperation(F1I, F2I)) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 380 | return false; |
| 381 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 382 | assert(F1I->getNumOperands() == F2I->getNumOperands()); |
| 383 | for (unsigned i = 0, e = F1I->getNumOperands(); i != e; ++i) { |
| 384 | Value *OpF1 = F1I->getOperand(i); |
| 385 | Value *OpF2 = F2I->getOperand(i); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 386 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 387 | if (!Enumerate(OpF1, OpF2)) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 388 | return false; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 389 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 390 | if (OpF1->getValueID() != OpF2->getValueID() || |
| 391 | !isEquivalentType(OpF1->getType(), OpF2->getType())) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 392 | return false; |
| 393 | } |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 396 | ++F1I, ++F2I; |
| 397 | } while (F1I != F1E && F2I != F2E); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 398 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 399 | return F1I == F1E && F2I == F2E; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 402 | /// Compare - test whether the two functions have equivalent behaviour. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 403 | bool FunctionComparator::Compare() { |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 404 | // We need to recheck everything, but check the things that weren't included |
| 405 | // in the hash first. |
| 406 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 407 | if (F1->getAttributes() != F2->getAttributes()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 408 | return false; |
| 409 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 410 | if (F1->hasGC() != F2->hasGC()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 411 | return false; |
| 412 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 413 | if (F1->hasGC() && F1->getGC() != F2->getGC()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 414 | return false; |
| 415 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 416 | if (F1->hasSection() != F2->hasSection()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 417 | return false; |
| 418 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 419 | if (F1->hasSection() && F1->getSection() != F2->getSection()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 420 | return false; |
| 421 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 422 | if (F1->isVarArg() != F2->isVarArg()) |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 423 | return false; |
| 424 | |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 425 | // TODO: if it's internal and only used in direct calls, we could handle this |
| 426 | // case too. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 427 | if (F1->getCallingConv() != F2->getCallingConv()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 428 | return false; |
| 429 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 430 | if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType())) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 431 | return false; |
| 432 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 433 | assert(F1->arg_size() == F2->arg_size() && |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 434 | "Identical functions have a different number of args."); |
| 435 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 436 | // Visit the arguments so that they get enumerated in the order they're |
| 437 | // passed in. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 438 | for (Function::const_arg_iterator f1i = F1->arg_begin(), |
| 439 | f2i = F2->arg_begin(), f1e = F1->arg_end(); f1i != f1e; ++f1i, ++f2i) { |
| 440 | if (!Enumerate(f1i, f2i)) |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 441 | llvm_unreachable("Arguments repeat"); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 444 | // We do a CFG-ordered walk since the actual ordering of the blocks in the |
| 445 | // linked list is immaterial. Our walk starts at the entry block for both |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 446 | // functions, then takes each block from each terminator in order. As an |
| 447 | // artifact, this also means that unreachable blocks are ignored. |
| 448 | SmallVector<const BasicBlock *, 8> F1BBs, F2BBs; |
| 449 | SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1. |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 450 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 451 | F1BBs.push_back(&F1->getEntryBlock()); |
| 452 | F2BBs.push_back(&F2->getEntryBlock()); |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 453 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 454 | VisitedBBs.insert(F1BBs[0]); |
| 455 | while (!F1BBs.empty()) { |
| 456 | const BasicBlock *F1BB = F1BBs.pop_back_val(); |
| 457 | const BasicBlock *F2BB = F2BBs.pop_back_val(); |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 458 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 459 | if (!Enumerate(F1BB, F2BB) || !Compare(F1BB, F2BB)) |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 460 | return false; |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 461 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 462 | const TerminatorInst *F1TI = F1BB->getTerminator(); |
| 463 | const TerminatorInst *F2TI = F2BB->getTerminator(); |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 464 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 465 | assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors()); |
| 466 | for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) { |
| 467 | if (!VisitedBBs.insert(F1TI->getSuccessor(i))) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 468 | continue; |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 469 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 470 | F1BBs.push_back(F1TI->getSuccessor(i)); |
| 471 | F2BBs.push_back(F2TI->getSuccessor(i)); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 474 | return true; |
| 475 | } |
| 476 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 477 | /// WriteThunk - Replace G with a simple tail call to bitcast(F). Also replace |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 478 | /// direct uses of G with bitcast(F). |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 479 | void MergeFunctions::WriteThunk(Function *F, Function *G) const { |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 480 | if (!G->mayBeOverridden()) { |
| 481 | // Redirect direct callers of G to F. |
| 482 | Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); |
| 483 | for (Value::use_iterator UI = G->use_begin(), UE = G->use_end(); |
| 484 | UI != UE;) { |
| 485 | Value::use_iterator TheIter = UI; |
| 486 | ++UI; |
| 487 | CallSite CS(*TheIter); |
| 488 | if (CS && CS.isCallee(TheIter)) |
| 489 | TheIter.getUse().set(BitcastF); |
| 490 | } |
| 491 | } |
| 492 | |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 493 | // If G was internal then we may have replaced all uses if G with F. If so, |
| 494 | // stop here and delete G. There's no need for a thunk. |
| 495 | if (G->hasLocalLinkage() && G->use_empty()) { |
| 496 | G->eraseFromParent(); |
| 497 | return; |
| 498 | } |
| 499 | |
Nick Lewycky | 8728d7a | 2009-06-12 15:56:56 +0000 | [diff] [blame] | 500 | Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", |
| 501 | G->getParent()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 502 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG); |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 503 | IRBuilder<false> Builder(BB); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 504 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 505 | SmallVector<Value *, 16> Args; |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 506 | unsigned i = 0; |
| 507 | const FunctionType *FFTy = F->getFunctionType(); |
| 508 | for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end(); |
| 509 | AI != AE; ++AI) { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 510 | Args.push_back(Builder.CreateBitCast(AI, FFTy->getParamType(i))); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 511 | ++i; |
| 512 | } |
| 513 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 514 | CallInst *CI = Builder.CreateCall(F, Args.begin(), Args.end()); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 515 | CI->setTailCall(); |
Nick Lewycky | b3c36c9 | 2009-06-12 16:04:00 +0000 | [diff] [blame] | 516 | CI->setCallingConv(F->getCallingConv()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 517 | if (NewG->getReturnType()->isVoidTy()) { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 518 | Builder.CreateRetVoid(); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 519 | } else { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 520 | Builder.CreateRet(Builder.CreateBitCast(CI, NewG->getReturnType())); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | NewG->copyAttributesFrom(G); |
| 524 | NewG->takeName(G); |
| 525 | G->replaceAllUsesWith(NewG); |
| 526 | G->eraseFromParent(); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 529 | /// MergeTwoFunctions - Merge two equivalent functions. Upon completion, |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 530 | /// Function G is deleted. |
| 531 | void MergeFunctions::MergeTwoFunctions(Function *F, Function *G) const { |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 532 | if (F->isWeakForLinker()) { |
| 533 | assert(G->isWeakForLinker()); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 534 | |
| 535 | // Make them both thunks to the same internal function. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 536 | Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "", |
| 537 | F->getParent()); |
| 538 | H->copyAttributesFrom(F); |
| 539 | H->takeName(F); |
| 540 | F->replaceAllUsesWith(H); |
| 541 | |
Nick Lewycky | 3221834 | 2010-08-09 21:03:28 +0000 | [diff] [blame] | 542 | unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment()); |
| 543 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 544 | WriteThunk(F, G); |
| 545 | WriteThunk(F, H); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 546 | |
Nick Lewycky | 3221834 | 2010-08-09 21:03:28 +0000 | [diff] [blame] | 547 | F->setAlignment(MaxAlignment); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 548 | F->setLinkage(GlobalValue::InternalLinkage); |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 549 | } else { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 550 | WriteThunk(F, G); |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 553 | ++NumFunctionsMerged; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 556 | static unsigned ProfileFunction(const Function *F) { |
| 557 | const FunctionType *FTy = F->getFunctionType(); |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 558 | |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 559 | FoldingSetNodeID ID; |
| 560 | ID.AddInteger(F->size()); |
| 561 | ID.AddInteger(F->getCallingConv()); |
| 562 | ID.AddBoolean(F->hasGC()); |
| 563 | ID.AddBoolean(FTy->isVarArg()); |
| 564 | ID.AddInteger(FTy->getReturnType()->getTypeID()); |
| 565 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 566 | ID.AddInteger(FTy->getParamType(i)->getTypeID()); |
| 567 | return ID.ComputeHash(); |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 568 | } |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 569 | |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 570 | class ComparableFunction { |
| 571 | public: |
| 572 | ComparableFunction(Function *Func, TargetData *TD) |
| 573 | : Func(Func), Hash(ProfileFunction(Func)), TD(TD) {} |
| 574 | |
| 575 | AssertingVH<Function> const Func; |
| 576 | const unsigned Hash; |
| 577 | TargetData * const TD; |
| 578 | }; |
| 579 | |
| 580 | struct MergeFunctionsEqualityInfo { |
| 581 | static ComparableFunction *getEmptyKey() { |
| 582 | return reinterpret_cast<ComparableFunction*>(0); |
| 583 | } |
| 584 | static ComparableFunction *getTombstoneKey() { |
| 585 | return reinterpret_cast<ComparableFunction*>(-1); |
| 586 | } |
| 587 | static unsigned getHashValue(const ComparableFunction *CF) { |
| 588 | return CF->Hash; |
| 589 | } |
| 590 | static bool isEqual(const ComparableFunction *LHS, |
| 591 | const ComparableFunction *RHS) { |
| 592 | if (LHS == RHS) |
| 593 | return true; |
| 594 | if (LHS == getEmptyKey() || LHS == getTombstoneKey() || |
| 595 | RHS == getEmptyKey() || RHS == getTombstoneKey()) |
| 596 | return false; |
| 597 | assert(LHS->TD == RHS->TD && "Comparing functions for different targets"); |
| 598 | return FunctionComparator(LHS->TD, LHS->Func, RHS->Func).Compare(); |
| 599 | } |
| 600 | }; |
| 601 | |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 602 | bool MergeFunctions::runOnModule(Module &M) { |
Nick Lewycky | 65a0af3 | 2010-08-31 08:29:37 +0000 | [diff] [blame^] | 603 | typedef DenseSet<ComparableFunction *, MergeFunctionsEqualityInfo> FnSetType; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 604 | |
Nick Lewycky | 65a0af3 | 2010-08-31 08:29:37 +0000 | [diff] [blame^] | 605 | bool Changed = false; |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 606 | TD = getAnalysisIfAvailable<TargetData>(); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 607 | |
Nick Lewycky | 65a0af3 | 2010-08-31 08:29:37 +0000 | [diff] [blame^] | 608 | std::vector<Function *> Funcs; |
| 609 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 610 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) |
| 611 | Funcs.push_back(F); |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Nick Lewycky | 65a0af3 | 2010-08-31 08:29:37 +0000 | [diff] [blame^] | 614 | bool LocalChanged; |
| 615 | do { |
| 616 | LocalChanged = false; |
| 617 | |
| 618 | FnSetType FnSet; |
| 619 | for (unsigned i = 0, e = Funcs.size(); i != e;) { |
| 620 | Function *F = Funcs[i]; |
| 621 | ComparableFunction *NewF = new ComparableFunction(F, TD); |
| 622 | std::pair<FnSetType::iterator, bool> Result = FnSet.insert(NewF); |
| 623 | if (!Result.second) { |
| 624 | ComparableFunction *&OldF = *Result.first; |
| 625 | assert(OldF && "Expected a hash collision"); |
| 626 | |
| 627 | // NewF will be deleted in favour of OldF unless NewF is strong and |
| 628 | // OldF is weak in which case swap them to keep the strong definition. |
| 629 | |
| 630 | if (OldF->Func->isWeakForLinker() && !NewF->Func->isWeakForLinker()) |
| 631 | std::swap(OldF, NewF); |
| 632 | |
| 633 | DEBUG(dbgs() << " " << OldF->Func->getName() << " == " |
| 634 | << NewF->Func->getName() << '\n'); |
| 635 | |
| 636 | Funcs.erase(Funcs.begin() + i); |
| 637 | --e; |
| 638 | |
| 639 | Function *DeleteF = NewF->Func; |
| 640 | delete NewF; |
| 641 | MergeTwoFunctions(OldF->Func, DeleteF); |
| 642 | LocalChanged = true; |
| 643 | Changed = true; |
| 644 | } else { |
| 645 | ++i; |
| 646 | } |
| 647 | } |
| 648 | DeleteContainerPointers(FnSet); |
| 649 | } while (LocalChanged); |
| 650 | |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 651 | return Changed; |
| 652 | } |