blob: 726466ec81d896b02998107d996a87e2ae8e0d0f [file] [log] [blame]
Nick Lewycky579a0242008-11-02 05:52:50 +00001//===- 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 Lewycky579a0242008-11-02 05:52:50 +000012// 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 Lewycky33ab0b12010-05-13 05:48:45 +000020// 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 Lewycky579a0242008-11-02 05:52:50 +000023//
24//===----------------------------------------------------------------------===//
25//
26// Future work:
27//
Nick Lewycky579a0242008-11-02 05:52:50 +000028// * 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 Lewyckybe04fde2010-08-08 05:04:23 +000032// and call, this is irrelevant, and we'd like to fold such functions.
Nick Lewycky579a0242008-11-02 05:52:50 +000033//
Nick Lewycky78d43302010-08-02 05:23:03 +000034// * switch from n^2 pair-wise comparisons to an n-way comparison for each
35// bucket.
Nick Lewycky33ab0b12010-05-13 05:48:45 +000036//
Nick Lewyckybe04fde2010-08-08 05:04:23 +000037// * be smarter about bitcasts.
Nick Lewycky33ab0b12010-05-13 05:48:45 +000038//
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 Lewyckybe04fde2010-08-08 05:04:23 +000042// other doesn't. We should learn to look through bitcasts.
Nick Lewycky33ab0b12010-05-13 05:48:45 +000043//
Nick Lewycky579a0242008-11-02 05:52:50 +000044//===----------------------------------------------------------------------===//
45
46#define DEBUG_TYPE "mergefunc"
47#include "llvm/Transforms/IPO.h"
Nick Lewyckyf53de862010-08-31 05:53:05 +000048#include "llvm/ADT/DenseSet.h"
Nick Lewycky287de602009-06-12 08:04:51 +000049#include "llvm/ADT/FoldingSet.h"
Nick Lewycky33ab0b12010-05-13 05:48:45 +000050#include "llvm/ADT/SmallSet.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000051#include "llvm/ADT/Statistic.h"
Nick Lewyckyf53de862010-08-31 05:53:05 +000052#include "llvm/ADT/STLExtras.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000053#include "llvm/Constants.h"
54#include "llvm/InlineAsm.h"
55#include "llvm/Instructions.h"
Owen Anderson14ce9ef2009-07-06 01:34:54 +000056#include "llvm/LLVMContext.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000057#include "llvm/Module.h"
58#include "llvm/Pass.h"
Nick Lewycky6feb3332008-11-02 16:46:26 +000059#include "llvm/Support/CallSite.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000060#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000061#include "llvm/Support/ErrorHandling.h"
Nick Lewyckybe04fde2010-08-08 05:04:23 +000062#include "llvm/Support/IRBuilder.h"
Nick Lewyckyf53de862010-08-31 05:53:05 +000063#include "llvm/Support/ValueHandle.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000064#include "llvm/Support/raw_ostream.h"
Nick Lewycky33ab0b12010-05-13 05:48:45 +000065#include "llvm/Target/TargetData.h"
Nick Lewycky65a0af32010-08-31 08:29:37 +000066#include <vector>
Nick Lewycky579a0242008-11-02 05:52:50 +000067using namespace llvm;
68
69STATISTIC(NumFunctionsMerged, "Number of functions merged");
Nick Lewycky579a0242008-11-02 05:52:50 +000070
71namespace {
Nick Lewycky579a0242008-11-02 05:52:50 +000072
Nick Lewyckyb0104e12010-09-05 08:22:49 +000073static unsigned ProfileFunction(const Function *F) {
74 const FunctionType *FTy = F->getFunctionType();
Nick Lewyckybe04fde2010-08-08 05:04:23 +000075
Nick Lewyckyb0104e12010-09-05 08:22:49 +000076 FoldingSetNodeID ID;
77 ID.AddInteger(F->size());
78 ID.AddInteger(F->getCallingConv());
79 ID.AddBoolean(F->hasGC());
80 ID.AddBoolean(FTy->isVarArg());
81 ID.AddInteger(FTy->getReturnType()->getTypeID());
82 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
83 ID.AddInteger(FTy->getParamType(i)->getTypeID());
84 return ID.ComputeHash();
Nick Lewycky579a0242008-11-02 05:52:50 +000085}
86
Nick Lewyckyb0104e12010-09-05 08:22:49 +000087class ComparableFunction {
88public:
89 ComparableFunction(Function *Func, TargetData *TD)
90 : Func(Func), Hash(ProfileFunction(Func)), TD(TD) {}
91
92 AssertingVH<Function> const Func;
93 const unsigned Hash;
94 TargetData * const TD;
95};
96
97struct MergeFunctionsEqualityInfo {
98 static ComparableFunction *getEmptyKey() {
99 return reinterpret_cast<ComparableFunction*>(0);
100 }
101 static ComparableFunction *getTombstoneKey() {
102 return reinterpret_cast<ComparableFunction*>(-1);
103 }
104 static unsigned getHashValue(const ComparableFunction *CF) {
105 return CF->Hash;
106 }
107 static bool isEqual(const ComparableFunction *LHS,
108 const ComparableFunction *RHS);
109};
110
111/// MergeFunctions finds functions which will generate identical machine code,
112/// by considering all pointer types to be equivalent. Once identified,
113/// MergeFunctions will fold them by replacing a call to one to a call to a
114/// bitcast of the other.
115///
116class MergeFunctions : public ModulePass {
117public:
118 static char ID;
119 MergeFunctions() : ModulePass(ID) {}
120
121 bool runOnModule(Module &M);
122
123private:
124 typedef DenseSet<ComparableFunction *, MergeFunctionsEqualityInfo> FnSetType;
125
126
127 /// Insert a ComparableFunction into the FnSet, or merge it away if it's
128 /// equal to one that's already present.
129 bool Insert(FnSetType &FnSet, ComparableFunction *NewF);
130
131 /// MergeTwoFunctions - Merge two equivalent functions. Upon completion, G
132 /// may be deleted, or may be converted into a thunk. In either case, it
133 /// should never be visited again.
134 void MergeTwoFunctions(Function *F, Function *G) const;
135
136 /// WriteThunk - Replace G with a simple tail call to bitcast(F). Also
137 /// replace direct uses of G with bitcast(F). Deletes G.
138 void WriteThunk(Function *F, Function *G) const;
139
140 TargetData *TD;
141};
142
143} // end anonymous namespace
144
Nick Lewycky579a0242008-11-02 05:52:50 +0000145char MergeFunctions::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000146INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false);
Nick Lewycky579a0242008-11-02 05:52:50 +0000147
148ModulePass *llvm::createMergeFunctionsPass() {
149 return new MergeFunctions();
150}
151
Nick Lewycky78d43302010-08-02 05:23:03 +0000152namespace {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000153/// FunctionComparator - Compares two functions to determine whether or not
154/// they will generate machine code with the same behaviour. TargetData is
155/// used if available. The comparator always fails conservatively (erring on the
156/// side of claiming that two functions are different).
Nick Lewycky78d43302010-08-02 05:23:03 +0000157class FunctionComparator {
158public:
Nick Lewyckyf53de862010-08-31 05:53:05 +0000159 FunctionComparator(const TargetData *TD, const Function *F1,
160 const Function *F2)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000161 : F1(F1), F2(F2), TD(TD), IDMap1Count(0), IDMap2Count(0) {}
Nick Lewycky287de602009-06-12 08:04:51 +0000162
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000163 /// Compare - test whether the two functions have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000164 bool Compare();
165
166private:
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000167 /// Compare - test whether two basic blocks have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000168 bool Compare(const BasicBlock *BB1, const BasicBlock *BB2);
169
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000170 /// Enumerate - Assign or look up previously assigned numbers for the two
171 /// values, and return whether the numbers are equal. Numbers are assigned in
172 /// the order visited.
Nick Lewycky78d43302010-08-02 05:23:03 +0000173 bool Enumerate(const Value *V1, const Value *V2);
174
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000175 /// isEquivalentOperation - Compare two Instructions for equivalence, similar
176 /// to Instruction::isSameOperationAs but with modifications to the type
177 /// comparison.
Nick Lewycky78d43302010-08-02 05:23:03 +0000178 bool isEquivalentOperation(const Instruction *I1,
179 const Instruction *I2) const;
180
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000181 /// isEquivalentGEP - Compare two GEPs for equivalent pointer arithmetic.
Nick Lewycky78d43302010-08-02 05:23:03 +0000182 bool isEquivalentGEP(const GEPOperator *GEP1, const GEPOperator *GEP2);
183 bool isEquivalentGEP(const GetElementPtrInst *GEP1,
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000184 const GetElementPtrInst *GEP2) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000185 return isEquivalentGEP(cast<GEPOperator>(GEP1), cast<GEPOperator>(GEP2));
186 }
187
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000188 /// isEquivalentType - Compare two Types, treating all pointer types as equal.
Nick Lewycky78d43302010-08-02 05:23:03 +0000189 bool isEquivalentType(const Type *Ty1, const Type *Ty2) const;
190
191 // The two functions undergoing comparison.
Nick Lewyckyf53de862010-08-31 05:53:05 +0000192 const Function *F1, *F2;
Nick Lewycky78d43302010-08-02 05:23:03 +0000193
Nick Lewyckyf53de862010-08-31 05:53:05 +0000194 const TargetData *TD;
Nick Lewycky78d43302010-08-02 05:23:03 +0000195
196 typedef DenseMap<const Value *, unsigned long> IDMap;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000197 IDMap Map1, Map2;
198 unsigned long IDMap1Count, IDMap2Count;
Nick Lewycky78d43302010-08-02 05:23:03 +0000199};
200}
201
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000202/// isEquivalentType - any two pointers in the same address space are
203/// equivalent. Otherwise, standard type equivalence rules apply.
Nick Lewycky78d43302010-08-02 05:23:03 +0000204bool FunctionComparator::isEquivalentType(const Type *Ty1,
205 const Type *Ty2) const {
Nick Lewycky287de602009-06-12 08:04:51 +0000206 if (Ty1 == Ty2)
207 return true;
208 if (Ty1->getTypeID() != Ty2->getTypeID())
209 return false;
210
211 switch(Ty1->getTypeID()) {
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000212 default:
213 llvm_unreachable("Unknown type!");
Duncan Sands8246adc2010-07-07 07:48:00 +0000214 // Fall through in Release mode.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000215 case Type::IntegerTyID:
216 case Type::OpaqueTyID:
217 // Ty1 == Ty2 would have returned true earlier.
218 return false;
219
Nick Lewycky287de602009-06-12 08:04:51 +0000220 case Type::VoidTyID:
221 case Type::FloatTyID:
222 case Type::DoubleTyID:
223 case Type::X86_FP80TyID:
224 case Type::FP128TyID:
225 case Type::PPC_FP128TyID:
226 case Type::LabelTyID:
227 case Type::MetadataTyID:
228 return true;
229
Nick Lewycky287de602009-06-12 08:04:51 +0000230 case Type::PointerTyID: {
231 const PointerType *PTy1 = cast<PointerType>(Ty1);
232 const PointerType *PTy2 = cast<PointerType>(Ty2);
233 return PTy1->getAddressSpace() == PTy2->getAddressSpace();
234 }
235
236 case Type::StructTyID: {
237 const StructType *STy1 = cast<StructType>(Ty1);
238 const StructType *STy2 = cast<StructType>(Ty2);
239 if (STy1->getNumElements() != STy2->getNumElements())
240 return false;
241
242 if (STy1->isPacked() != STy2->isPacked())
243 return false;
244
245 for (unsigned i = 0, e = STy1->getNumElements(); i != e; ++i) {
246 if (!isEquivalentType(STy1->getElementType(i), STy2->getElementType(i)))
247 return false;
248 }
249 return true;
250 }
251
252 case Type::FunctionTyID: {
253 const FunctionType *FTy1 = cast<FunctionType>(Ty1);
254 const FunctionType *FTy2 = cast<FunctionType>(Ty2);
255 if (FTy1->getNumParams() != FTy2->getNumParams() ||
256 FTy1->isVarArg() != FTy2->isVarArg())
257 return false;
258
259 if (!isEquivalentType(FTy1->getReturnType(), FTy2->getReturnType()))
260 return false;
261
262 for (unsigned i = 0, e = FTy1->getNumParams(); i != e; ++i) {
263 if (!isEquivalentType(FTy1->getParamType(i), FTy2->getParamType(i)))
264 return false;
265 }
266 return true;
267 }
268
Nick Lewycky394ce412010-07-16 06:31:12 +0000269 case Type::ArrayTyID: {
270 const ArrayType *ATy1 = cast<ArrayType>(Ty1);
271 const ArrayType *ATy2 = cast<ArrayType>(Ty2);
272 return ATy1->getNumElements() == ATy2->getNumElements() &&
273 isEquivalentType(ATy1->getElementType(), ATy2->getElementType());
274 }
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000275
Nick Lewycky287de602009-06-12 08:04:51 +0000276 case Type::VectorTyID: {
Nick Lewycky394ce412010-07-16 06:31:12 +0000277 const VectorType *VTy1 = cast<VectorType>(Ty1);
278 const VectorType *VTy2 = cast<VectorType>(Ty2);
279 return VTy1->getNumElements() == VTy2->getNumElements() &&
280 isEquivalentType(VTy1->getElementType(), VTy2->getElementType());
Nick Lewycky287de602009-06-12 08:04:51 +0000281 }
282 }
283}
284
285/// isEquivalentOperation - determine whether the two operations are the same
286/// except that pointer-to-A and pointer-to-B are equivalent. This should be
Dan Gohman194ae782009-06-12 19:03:05 +0000287/// kept in sync with Instruction::isSameOperationAs.
Nick Lewycky78d43302010-08-02 05:23:03 +0000288bool FunctionComparator::isEquivalentOperation(const Instruction *I1,
289 const Instruction *I2) const {
Nick Lewycky287de602009-06-12 08:04:51 +0000290 if (I1->getOpcode() != I2->getOpcode() ||
291 I1->getNumOperands() != I2->getNumOperands() ||
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000292 !isEquivalentType(I1->getType(), I2->getType()) ||
293 !I1->hasSameSubclassOptionalData(I2))
Nick Lewycky287de602009-06-12 08:04:51 +0000294 return false;
295
296 // We have two instructions of identical opcode and #operands. Check to see
297 // if all operands are the same type
298 for (unsigned i = 0, e = I1->getNumOperands(); i != e; ++i)
299 if (!isEquivalentType(I1->getOperand(i)->getType(),
300 I2->getOperand(i)->getType()))
301 return false;
302
303 // Check special state that is a part of some instructions.
304 if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
305 return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
306 LI->getAlignment() == cast<LoadInst>(I2)->getAlignment();
307 if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
308 return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
309 SI->getAlignment() == cast<StoreInst>(I2)->getAlignment();
310 if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
311 return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
312 if (const CallInst *CI = dyn_cast<CallInst>(I1))
313 return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
314 CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
315 CI->getAttributes().getRawPointer() ==
316 cast<CallInst>(I2)->getAttributes().getRawPointer();
317 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
318 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
319 CI->getAttributes().getRawPointer() ==
320 cast<InvokeInst>(I2)->getAttributes().getRawPointer();
321 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) {
322 if (IVI->getNumIndices() != cast<InsertValueInst>(I2)->getNumIndices())
323 return false;
324 for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
325 if (IVI->idx_begin()[i] != cast<InsertValueInst>(I2)->idx_begin()[i])
326 return false;
327 return true;
328 }
329 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) {
330 if (EVI->getNumIndices() != cast<ExtractValueInst>(I2)->getNumIndices())
331 return false;
332 for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
333 if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I2)->idx_begin()[i])
334 return false;
335 return true;
336 }
337
338 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000339}
340
Nick Lewycky78d43302010-08-02 05:23:03 +0000341/// isEquivalentGEP - determine whether two GEP operations perform the same
342/// underlying arithmetic.
343bool FunctionComparator::isEquivalentGEP(const GEPOperator *GEP1,
344 const GEPOperator *GEP2) {
345 // When we have target data, we can reduce the GEP down to the value in bytes
346 // added to the address.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000347 if (TD && GEP1->hasAllConstantIndices() && GEP2->hasAllConstantIndices()) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000348 SmallVector<Value *, 8> Indices1(GEP1->idx_begin(), GEP1->idx_end());
349 SmallVector<Value *, 8> Indices2(GEP2->idx_begin(), GEP2->idx_end());
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000350 uint64_t Offset1 = TD->getIndexedOffset(GEP1->getPointerOperandType(),
351 Indices1.data(), Indices1.size());
352 uint64_t Offset2 = TD->getIndexedOffset(GEP2->getPointerOperandType(),
353 Indices2.data(), Indices2.size());
354 return Offset1 == Offset2;
Nick Lewycky579a0242008-11-02 05:52:50 +0000355 }
356
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000357 if (GEP1->getPointerOperand()->getType() !=
358 GEP2->getPointerOperand()->getType())
359 return false;
360
361 if (GEP1->getNumOperands() != GEP2->getNumOperands())
362 return false;
363
364 for (unsigned i = 0, e = GEP1->getNumOperands(); i != e; ++i) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000365 if (!Enumerate(GEP1->getOperand(i), GEP2->getOperand(i)))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000366 return false;
367 }
368
369 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000370}
371
Nick Lewycky78d43302010-08-02 05:23:03 +0000372/// Enumerate - Compare two values used by the two functions under pair-wise
373/// comparison. If this is the first time the values are seen, they're added to
374/// the mapping so that we will detect mismatches on next use.
375bool FunctionComparator::Enumerate(const Value *V1, const Value *V2) {
376 // Check for function @f1 referring to itself and function @f2 referring to
377 // itself, or referring to each other, or both referring to either of them.
378 // They're all equivalent if the two functions are otherwise equivalent.
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000379 if (V1 == F1 && V2 == F2)
380 return true;
381 if (V1 == F2 && V2 == F1)
382 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000383
Nick Lewycky78d43302010-08-02 05:23:03 +0000384 // TODO: constant expressions with GEP or references to F1 or F2.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000385 if (isa<Constant>(V1))
386 return V1 == V2;
387
388 if (isa<InlineAsm>(V1) && isa<InlineAsm>(V2)) {
389 const InlineAsm *IA1 = cast<InlineAsm>(V1);
390 const InlineAsm *IA2 = cast<InlineAsm>(V2);
391 return IA1->getAsmString() == IA2->getAsmString() &&
392 IA1->getConstraintString() == IA2->getConstraintString();
393 }
394
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000395 unsigned long &ID1 = Map1[V1];
396 if (!ID1)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000397 ID1 = ++IDMap1Count;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000398
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000399 unsigned long &ID2 = Map2[V2];
400 if (!ID2)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000401 ID2 = ++IDMap2Count;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000402
403 return ID1 == ID2;
404}
405
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000406/// Compare - test whether two basic blocks have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000407bool FunctionComparator::Compare(const BasicBlock *BB1, const BasicBlock *BB2) {
408 BasicBlock::const_iterator F1I = BB1->begin(), F1E = BB1->end();
409 BasicBlock::const_iterator F2I = BB2->begin(), F2E = BB2->end();
Nick Lewycky579a0242008-11-02 05:52:50 +0000410
411 do {
Nick Lewycky78d43302010-08-02 05:23:03 +0000412 if (!Enumerate(F1I, F2I))
Nick Lewycky579a0242008-11-02 05:52:50 +0000413 return false;
414
Nick Lewycky78d43302010-08-02 05:23:03 +0000415 if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) {
416 const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I);
417 if (!GEP2)
418 return false;
Nick Lewyckya142c932009-06-13 19:09:52 +0000419
Nick Lewycky78d43302010-08-02 05:23:03 +0000420 if (!Enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand()))
Nick Lewycky911ae392010-05-13 06:45:13 +0000421 return false;
Nick Lewyckya142c932009-06-13 19:09:52 +0000422
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000423 if (!isEquivalentGEP(GEP1, GEP2))
Nick Lewycky911ae392010-05-13 06:45:13 +0000424 return false;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000425 } else {
Nick Lewycky78d43302010-08-02 05:23:03 +0000426 if (!isEquivalentOperation(F1I, F2I))
Nick Lewycky579a0242008-11-02 05:52:50 +0000427 return false;
428
Nick Lewycky78d43302010-08-02 05:23:03 +0000429 assert(F1I->getNumOperands() == F2I->getNumOperands());
430 for (unsigned i = 0, e = F1I->getNumOperands(); i != e; ++i) {
431 Value *OpF1 = F1I->getOperand(i);
432 Value *OpF2 = F2I->getOperand(i);
Nick Lewycky579a0242008-11-02 05:52:50 +0000433
Nick Lewycky78d43302010-08-02 05:23:03 +0000434 if (!Enumerate(OpF1, OpF2))
Nick Lewycky911ae392010-05-13 06:45:13 +0000435 return false;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000436
Nick Lewycky78d43302010-08-02 05:23:03 +0000437 if (OpF1->getValueID() != OpF2->getValueID() ||
438 !isEquivalentType(OpF1->getType(), OpF2->getType()))
Nick Lewycky579a0242008-11-02 05:52:50 +0000439 return false;
440 }
Nick Lewycky579a0242008-11-02 05:52:50 +0000441 }
442
Nick Lewycky78d43302010-08-02 05:23:03 +0000443 ++F1I, ++F2I;
444 } while (F1I != F1E && F2I != F2E);
Nick Lewycky579a0242008-11-02 05:52:50 +0000445
Nick Lewycky78d43302010-08-02 05:23:03 +0000446 return F1I == F1E && F2I == F2E;
Nick Lewycky579a0242008-11-02 05:52:50 +0000447}
448
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000449/// Compare - test whether the two functions have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000450bool FunctionComparator::Compare() {
Nick Lewycky579a0242008-11-02 05:52:50 +0000451 // We need to recheck everything, but check the things that weren't included
452 // in the hash first.
453
Nick Lewycky78d43302010-08-02 05:23:03 +0000454 if (F1->getAttributes() != F2->getAttributes())
Nick Lewycky579a0242008-11-02 05:52:50 +0000455 return false;
456
Nick Lewycky78d43302010-08-02 05:23:03 +0000457 if (F1->hasGC() != F2->hasGC())
Nick Lewycky579a0242008-11-02 05:52:50 +0000458 return false;
459
Nick Lewycky78d43302010-08-02 05:23:03 +0000460 if (F1->hasGC() && F1->getGC() != F2->getGC())
Nick Lewycky579a0242008-11-02 05:52:50 +0000461 return false;
462
Nick Lewycky78d43302010-08-02 05:23:03 +0000463 if (F1->hasSection() != F2->hasSection())
Nick Lewycky579a0242008-11-02 05:52:50 +0000464 return false;
465
Nick Lewycky78d43302010-08-02 05:23:03 +0000466 if (F1->hasSection() && F1->getSection() != F2->getSection())
Nick Lewycky579a0242008-11-02 05:52:50 +0000467 return false;
468
Nick Lewycky78d43302010-08-02 05:23:03 +0000469 if (F1->isVarArg() != F2->isVarArg())
Nick Lewycky287de602009-06-12 08:04:51 +0000470 return false;
471
Nick Lewycky579a0242008-11-02 05:52:50 +0000472 // TODO: if it's internal and only used in direct calls, we could handle this
473 // case too.
Nick Lewycky78d43302010-08-02 05:23:03 +0000474 if (F1->getCallingConv() != F2->getCallingConv())
Nick Lewycky579a0242008-11-02 05:52:50 +0000475 return false;
476
Nick Lewycky78d43302010-08-02 05:23:03 +0000477 if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType()))
Nick Lewycky579a0242008-11-02 05:52:50 +0000478 return false;
479
Nick Lewycky78d43302010-08-02 05:23:03 +0000480 assert(F1->arg_size() == F2->arg_size() &&
Nick Lewycky579a0242008-11-02 05:52:50 +0000481 "Identical functions have a different number of args.");
482
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000483 // Visit the arguments so that they get enumerated in the order they're
484 // passed in.
Nick Lewycky78d43302010-08-02 05:23:03 +0000485 for (Function::const_arg_iterator f1i = F1->arg_begin(),
486 f2i = F2->arg_begin(), f1e = F1->arg_end(); f1i != f1e; ++f1i, ++f2i) {
487 if (!Enumerate(f1i, f2i))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000488 llvm_unreachable("Arguments repeat");
Nick Lewycky579a0242008-11-02 05:52:50 +0000489 }
490
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000491 // We do a CFG-ordered walk since the actual ordering of the blocks in the
492 // linked list is immaterial. Our walk starts at the entry block for both
Nick Lewycky78d43302010-08-02 05:23:03 +0000493 // functions, then takes each block from each terminator in order. As an
494 // artifact, this also means that unreachable blocks are ignored.
495 SmallVector<const BasicBlock *, 8> F1BBs, F2BBs;
496 SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1.
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000497
Nick Lewycky78d43302010-08-02 05:23:03 +0000498 F1BBs.push_back(&F1->getEntryBlock());
499 F2BBs.push_back(&F2->getEntryBlock());
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000500
Nick Lewycky78d43302010-08-02 05:23:03 +0000501 VisitedBBs.insert(F1BBs[0]);
502 while (!F1BBs.empty()) {
503 const BasicBlock *F1BB = F1BBs.pop_back_val();
504 const BasicBlock *F2BB = F2BBs.pop_back_val();
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000505
Nick Lewycky78d43302010-08-02 05:23:03 +0000506 if (!Enumerate(F1BB, F2BB) || !Compare(F1BB, F2BB))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000507 return false;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000508
Nick Lewycky78d43302010-08-02 05:23:03 +0000509 const TerminatorInst *F1TI = F1BB->getTerminator();
510 const TerminatorInst *F2TI = F2BB->getTerminator();
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000511
Nick Lewycky78d43302010-08-02 05:23:03 +0000512 assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors());
513 for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) {
514 if (!VisitedBBs.insert(F1TI->getSuccessor(i)))
Nick Lewycky911ae392010-05-13 06:45:13 +0000515 continue;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000516
Nick Lewycky78d43302010-08-02 05:23:03 +0000517 F1BBs.push_back(F1TI->getSuccessor(i));
518 F2BBs.push_back(F2TI->getSuccessor(i));
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000519 }
520 }
Nick Lewycky579a0242008-11-02 05:52:50 +0000521 return true;
522}
523
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000524/// WriteThunk - Replace G with a simple tail call to bitcast(F). Also replace
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000525/// direct uses of G with bitcast(F). Deletes G.
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000526void MergeFunctions::WriteThunk(Function *F, Function *G) const {
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000527 if (!G->mayBeOverridden()) {
528 // Redirect direct callers of G to F.
529 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
530 for (Value::use_iterator UI = G->use_begin(), UE = G->use_end();
531 UI != UE;) {
532 Value::use_iterator TheIter = UI;
533 ++UI;
534 CallSite CS(*TheIter);
535 if (CS && CS.isCallee(TheIter))
536 TheIter.getUse().set(BitcastF);
537 }
538 }
539
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000540 // If G was internal then we may have replaced all uses if G with F. If so,
541 // stop here and delete G. There's no need for a thunk.
542 if (G->hasLocalLinkage() && G->use_empty()) {
543 G->eraseFromParent();
544 return;
545 }
546
Nick Lewycky8728d7a2009-06-12 15:56:56 +0000547 Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
548 G->getParent());
Owen Anderson1d0be152009-08-13 21:58:54 +0000549 BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000550 IRBuilder<false> Builder(BB);
Nick Lewycky287de602009-06-12 08:04:51 +0000551
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000552 SmallVector<Value *, 16> Args;
Nick Lewycky287de602009-06-12 08:04:51 +0000553 unsigned i = 0;
554 const FunctionType *FFTy = F->getFunctionType();
555 for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end();
556 AI != AE; ++AI) {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000557 Args.push_back(Builder.CreateBitCast(AI, FFTy->getParamType(i)));
Nick Lewycky287de602009-06-12 08:04:51 +0000558 ++i;
559 }
560
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000561 CallInst *CI = Builder.CreateCall(F, Args.begin(), Args.end());
Nick Lewycky287de602009-06-12 08:04:51 +0000562 CI->setTailCall();
Nick Lewyckyb3c36c92009-06-12 16:04:00 +0000563 CI->setCallingConv(F->getCallingConv());
Benjamin Kramerf0127052010-01-05 13:12:22 +0000564 if (NewG->getReturnType()->isVoidTy()) {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000565 Builder.CreateRetVoid();
Nick Lewycky287de602009-06-12 08:04:51 +0000566 } else {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000567 Builder.CreateRet(Builder.CreateBitCast(CI, NewG->getReturnType()));
Nick Lewycky287de602009-06-12 08:04:51 +0000568 }
569
570 NewG->copyAttributesFrom(G);
571 NewG->takeName(G);
572 G->replaceAllUsesWith(NewG);
573 G->eraseFromParent();
Nick Lewycky287de602009-06-12 08:04:51 +0000574}
575
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000576/// MergeTwoFunctions - Merge two equivalent functions. Upon completion,
Nick Lewyckyf53de862010-08-31 05:53:05 +0000577/// Function G is deleted.
578void MergeFunctions::MergeTwoFunctions(Function *F, Function *G) const {
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000579 if (F->isWeakForLinker()) {
580 assert(G->isWeakForLinker());
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000581
582 // Make them both thunks to the same internal function.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000583 Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
584 F->getParent());
585 H->copyAttributesFrom(F);
586 H->takeName(F);
587 F->replaceAllUsesWith(H);
588
Nick Lewycky32218342010-08-09 21:03:28 +0000589 unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
590
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000591 WriteThunk(F, G);
592 WriteThunk(F, H);
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000593
Nick Lewycky32218342010-08-09 21:03:28 +0000594 F->setAlignment(MaxAlignment);
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000595 F->setLinkage(GlobalValue::InternalLinkage);
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000596 } else {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000597 WriteThunk(F, G);
Nick Lewycky6feb3332008-11-02 16:46:26 +0000598 }
599
Nick Lewycky287de602009-06-12 08:04:51 +0000600 ++NumFunctionsMerged;
Nick Lewycky579a0242008-11-02 05:52:50 +0000601}
602
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000603// Insert - Insert a ComparableFunction into the FnSet, or merge it away if
604// equal to one that's already inserted.
605bool MergeFunctions::Insert(FnSetType &FnSet, ComparableFunction *NewF) {
606 std::pair<FnSetType::iterator, bool> Result = FnSet.insert(NewF);
607 if (Result.second)
608 return false;
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000609
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000610 ComparableFunction *OldF = *Result.first;
611 assert(OldF && "Expected a hash collision");
612
613 // Never thunk a strong function to a weak function.
614 assert(!OldF->Func->isWeakForLinker() || NewF->Func->isWeakForLinker());
615
616 DEBUG(dbgs() << " " << OldF->Func->getName() << " == "
617 << NewF->Func->getName() << '\n');
618
619 Function *DeleteF = NewF->Func;
620 delete NewF;
621 MergeTwoFunctions(OldF->Func, DeleteF);
622 return true;
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000623}
Nick Lewycky579a0242008-11-02 05:52:50 +0000624
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000625// IsThunk - This method determines whether or not a given Function is a thunk\// like the ones emitted by this pass and therefore not subject to further
626// merging.
627static bool IsThunk(const Function *F) {
628 // The safe direction to fail is to return true. In that case, the function
629 // will be removed from merging analysis. If we failed to including functions
630 // then we may try to merge unmergable thing (ie., identical weak functions)
631 // which will push us into an infinite loop.
Nick Lewyckyf53de862010-08-31 05:53:05 +0000632
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000633 if (F->size() != 1)
634 return false;
Nick Lewyckyf53de862010-08-31 05:53:05 +0000635
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000636 const BasicBlock *BB = &F->front();
637 // A thunk is:
638 // bitcast-inst*
639 // optional-reg tail call @thunkee(args...*)
640 // ret void|optional-reg
641 // where the args are in the same order as the arguments.
642
643 // Verify that the sequence of bitcast-inst's are all casts of arguments and
644 // that there aren't any extras (ie. no repeated casts).
645 int LastArgNo = -1;
646 BasicBlock::const_iterator I = BB->begin();
647 while (const BitCastInst *BCI = dyn_cast<BitCastInst>(I)) {
648 const Argument *A = dyn_cast<Argument>(BCI->getOperand(0));
649 if (!A) return false;
650 if ((int)A->getArgNo() >= LastArgNo) return false;
651 LastArgNo = A->getArgNo();
652 ++I;
Nick Lewyckyf53de862010-08-31 05:53:05 +0000653 }
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000654
655 // Verify that the call instruction has the same arguments as this function
656 // and that they're all either the incoming argument or a cast of the right
657 // argument.
658 const CallInst *CI = dyn_cast<CallInst>(I++);
659 if (!CI || !CI->isTailCall() ||
660 CI->getNumArgOperands() != F->arg_size()) return false;
661
662 for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i) {
663 const Value *V = CI->getArgOperand(i);
664 const Argument *A = dyn_cast<Argument>(V);
665 if (!A) {
666 const BitCastInst *BCI = dyn_cast<BitCastInst>(V);
667 if (!BCI) return false;
668 A = cast<Argument>(BCI->getOperand(0));
669 }
670 if (A->getArgNo() != i) return false;
Nick Lewyckyf53de862010-08-31 05:53:05 +0000671 }
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000672
673 // Verify that the terminator is a ret void (if we're void) or a ret of the
674 // call's return, or a ret of a bitcast of the call's return.
675 const Value *RetOp = CI;
676 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(I)) {
677 ++I;
678 if (BCI->getOperand(0) != CI) return false;
679 RetOp = BCI;
Nick Lewyckyf53de862010-08-31 05:53:05 +0000680 }
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000681 const ReturnInst *RI = dyn_cast<ReturnInst>(I);
682 if (!RI) return false;
683 if (RI->getNumOperands() == 0)
684 return CI->getType()->isVoidTy();
685 return RI->getReturnValue() == CI;
686}
Nick Lewyckyf53de862010-08-31 05:53:05 +0000687
Nick Lewycky579a0242008-11-02 05:52:50 +0000688bool MergeFunctions::runOnModule(Module &M) {
Nick Lewycky65a0af32010-08-31 08:29:37 +0000689 bool Changed = false;
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000690 TD = getAnalysisIfAvailable<TargetData>();
Nick Lewycky579a0242008-11-02 05:52:50 +0000691
Nick Lewycky65a0af32010-08-31 08:29:37 +0000692 bool LocalChanged;
693 do {
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000694 DEBUG(dbgs() << "size: " << M.size() << '\n');
Nick Lewycky65a0af32010-08-31 08:29:37 +0000695 LocalChanged = false;
Nick Lewycky65a0af32010-08-31 08:29:37 +0000696 FnSetType FnSet;
Nick Lewycky65a0af32010-08-31 08:29:37 +0000697
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000698 // Insert only strong functions and merge them. Strong function merging
699 // always deletes one of them.
700 for (Module::iterator I = M.begin(), E = M.end(); I != E;) {
701 Function *F = I++;
702 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
703 !F->isWeakForLinker() && !IsThunk(F)) {
704 ComparableFunction *CF = new ComparableFunction(F, TD);
705 LocalChanged |= Insert(FnSet, CF);
706 }
707 }
Nick Lewycky65a0af32010-08-31 08:29:37 +0000708
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000709 // Insert only weak functions and merge them. By doing these second we
710 // create thunks to the strong function when possible. When two weak
711 // functions are identical, we create a new strong function with two weak
712 // weak thunks to it which are identical but not mergable.
713 for (Module::iterator I = M.begin(), E = M.end(); I != E;) {
714 Function *F = I++;
715 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
716 F->isWeakForLinker() && !IsThunk(F)) {
717 ComparableFunction *CF = new ComparableFunction(F, TD);
718 LocalChanged |= Insert(FnSet, CF);
Nick Lewycky65a0af32010-08-31 08:29:37 +0000719 }
720 }
721 DeleteContainerPointers(FnSet);
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000722 Changed |= LocalChanged;
Nick Lewycky65a0af32010-08-31 08:29:37 +0000723 } while (LocalChanged);
724
Nick Lewycky579a0242008-11-02 05:52:50 +0000725 return Changed;
726}
Nick Lewyckyb0104e12010-09-05 08:22:49 +0000727
728bool MergeFunctionsEqualityInfo::isEqual(const ComparableFunction *LHS,
729 const ComparableFunction *RHS) {
730 if (LHS == RHS)
731 return true;
732 if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||
733 RHS == getEmptyKey() || RHS == getTombstoneKey())
734 return false;
735 assert(LHS->TD == RHS->TD && "Comparing functions for different targets");
736 return FunctionComparator(LHS->TD, LHS->Func, RHS->Func).Compare();
737}