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