blob: 21e9d6ad424ba50d8d90720b55e6a578681e2e14 [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
32// and call, this is irrelevant, and we'd like to fold such implementations.
33//
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//
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 Lewycky579a0242008-11-02 05:52:50 +000045//===----------------------------------------------------------------------===//
46
47#define DEBUG_TYPE "mergefunc"
48#include "llvm/Transforms/IPO.h"
49#include "llvm/ADT/DenseMap.h"
Nick Lewycky287de602009-06-12 08:04:51 +000050#include "llvm/ADT/FoldingSet.h"
Nick Lewycky33ab0b12010-05-13 05:48:45 +000051#include "llvm/ADT/SmallSet.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000052#include "llvm/ADT/Statistic.h"
53#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"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000062#include "llvm/Support/raw_ostream.h"
Nick Lewycky33ab0b12010-05-13 05:48:45 +000063#include "llvm/Target/TargetData.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000064#include <map>
65#include <vector>
66using namespace llvm;
67
68STATISTIC(NumFunctionsMerged, "Number of functions merged");
Nick Lewycky579a0242008-11-02 05:52:50 +000069
70namespace {
Nick Lewycky78d43302010-08-02 05:23:03 +000071 /// 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 Lewycky579a0242008-11-02 05:52:50 +000077 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000078 MergeFunctions() : ModulePass(ID) {}
Nick Lewycky579a0242008-11-02 05:52:50 +000079
80 bool runOnModule(Module &M);
81 };
82}
83
84char MergeFunctions::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000085INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false);
Nick Lewycky579a0242008-11-02 05:52:50 +000086
87ModulePass *llvm::createMergeFunctionsPass() {
88 return new MergeFunctions();
89}
90
Nick Lewycky287de602009-06-12 08:04:51 +000091// ===----------------------------------------------------------------------===
92// Comparison of functions
93// ===----------------------------------------------------------------------===
Nick Lewycky78d43302010-08-02 05:23:03 +000094namespace {
95class FunctionComparator {
96public:
97 FunctionComparator(TargetData *TD, Function *F1, Function *F2)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +000098 : F1(F1), F2(F2), TD(TD), IDMap1Count(0), IDMap2Count(0) {}
Nick Lewycky287de602009-06-12 08:04:51 +000099
Nick Lewycky78d43302010-08-02 05:23:03 +0000100 // Compare - test whether the two functions have equivalent behaviour.
101 bool Compare();
102
103private:
104 // Compare - test whether two basic blocks have equivalent behaviour.
105 bool Compare(const BasicBlock *BB1, const BasicBlock *BB2);
106
Nick Lewycky78d43302010-08-02 05:23:03 +0000107 // 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000120
Nick Lewycky78d43302010-08-02 05:23:03 +0000121 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000135 IDMap Map1, Map2;
136 unsigned long IDMap1Count, IDMap2Count;
Nick Lewycky78d43302010-08-02 05:23:03 +0000137};
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.
143static unsigned long ProfileFunction(const Function *F) {
Nick Lewycky287de602009-06-12 08:04:51 +0000144 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000157/// isEquivalentType - any two pointers in the same address space are
158/// equivalent. Otherwise, standard type equivalence rules apply.
Nick Lewycky78d43302010-08-02 05:23:03 +0000159bool FunctionComparator::isEquivalentType(const Type *Ty1,
160 const Type *Ty2) const {
Nick Lewycky287de602009-06-12 08:04:51 +0000161 if (Ty1 == Ty2)
162 return true;
163 if (Ty1->getTypeID() != Ty2->getTypeID())
164 return false;
165
166 switch(Ty1->getTypeID()) {
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000167 default:
168 llvm_unreachable("Unknown type!");
Duncan Sands8246adc2010-07-07 07:48:00 +0000169 // Fall through in Release mode.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000170 case Type::IntegerTyID:
171 case Type::OpaqueTyID:
172 // Ty1 == Ty2 would have returned true earlier.
173 return false;
174
Nick Lewycky287de602009-06-12 08:04:51 +0000175 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 Lewycky287de602009-06-12 08:04:51 +0000185 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 Lewycky33ab0b12010-05-13 05:48:45 +0000207 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 Lewycky287de602009-06-12 08:04:51 +0000222 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 Lewycky394ce412010-07-16 06:31:12 +0000239 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000245
Nick Lewycky287de602009-06-12 08:04:51 +0000246 case Type::VectorTyID: {
Nick Lewycky394ce412010-07-16 06:31:12 +0000247 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 Lewycky287de602009-06-12 08:04:51 +0000251 }
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 Gohman194ae782009-06-12 19:03:05 +0000257/// kept in sync with Instruction::isSameOperationAs.
Nick Lewycky78d43302010-08-02 05:23:03 +0000258bool FunctionComparator::isEquivalentOperation(const Instruction *I1,
259 const Instruction *I2) const {
Nick Lewycky287de602009-06-12 08:04:51 +0000260 if (I1->getOpcode() != I2->getOpcode() ||
261 I1->getNumOperands() != I2->getNumOperands() ||
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000262 !isEquivalentType(I1->getType(), I2->getType()) ||
263 !I1->hasSameSubclassOptionalData(I2))
Nick Lewycky287de602009-06-12 08:04:51 +0000264 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 Lewycky579a0242008-11-02 05:52:50 +0000309}
310
Nick Lewycky78d43302010-08-02 05:23:03 +0000311/// isEquivalentGEP - determine whether two GEP operations perform the same
312/// underlying arithmetic.
313bool 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 Lewycky33ab0b12010-05-13 05:48:45 +0000317 if (TD && GEP1->hasAllConstantIndices() && GEP2->hasAllConstantIndices()) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000318 SmallVector<Value *, 8> Indices1(GEP1->idx_begin(), GEP1->idx_end());
319 SmallVector<Value *, 8> Indices2(GEP2->idx_begin(), GEP2->idx_end());
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000320 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 Lewycky579a0242008-11-02 05:52:50 +0000325 }
326
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000327 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 Lewycky78d43302010-08-02 05:23:03 +0000335 if (!Enumerate(GEP1->getOperand(i), GEP2->getOperand(i)))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000336 return false;
337 }
338
339 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000340}
341
Nick Lewycky78d43302010-08-02 05:23:03 +0000342/// 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.
345bool 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000349 if (V1 == F1 && V2 == F2)
350 return true;
351 if (V1 == F2 && V2 == F1)
352 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000353
Nick Lewycky78d43302010-08-02 05:23:03 +0000354 // TODO: constant expressions with GEP or references to F1 or F2.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000355 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 Lewycky33ab0b12010-05-13 05:48:45 +0000365 unsigned long &ID1 = Map1[V1];
366 if (!ID1)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000367 ID1 = ++IDMap1Count;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000368
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000369 unsigned long &ID2 = Map2[V2];
370 if (!ID2)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000371 ID2 = ++IDMap2Count;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000372
373 return ID1 == ID2;
374}
375
Nick Lewycky78d43302010-08-02 05:23:03 +0000376// Compare - test whether two basic blocks have equivalent behaviour.
377bool 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 Lewycky579a0242008-11-02 05:52:50 +0000380
381 do {
Nick Lewycky78d43302010-08-02 05:23:03 +0000382 if (!Enumerate(F1I, F2I))
Nick Lewycky579a0242008-11-02 05:52:50 +0000383 return false;
384
Nick Lewycky78d43302010-08-02 05:23:03 +0000385 if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) {
386 const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I);
387 if (!GEP2)
388 return false;
Nick Lewyckya142c932009-06-13 19:09:52 +0000389
Nick Lewycky78d43302010-08-02 05:23:03 +0000390 if (!Enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand()))
Nick Lewycky911ae392010-05-13 06:45:13 +0000391 return false;
Nick Lewyckya142c932009-06-13 19:09:52 +0000392
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000393 if (!isEquivalentGEP(GEP1, GEP2))
Nick Lewycky911ae392010-05-13 06:45:13 +0000394 return false;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000395 } else {
Nick Lewycky78d43302010-08-02 05:23:03 +0000396 if (!isEquivalentOperation(F1I, F2I))
Nick Lewycky579a0242008-11-02 05:52:50 +0000397 return false;
398
Nick Lewycky78d43302010-08-02 05:23:03 +0000399 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 Lewycky579a0242008-11-02 05:52:50 +0000403
Nick Lewycky78d43302010-08-02 05:23:03 +0000404 if (!Enumerate(OpF1, OpF2))
Nick Lewycky911ae392010-05-13 06:45:13 +0000405 return false;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000406
Nick Lewycky78d43302010-08-02 05:23:03 +0000407 if (OpF1->getValueID() != OpF2->getValueID() ||
408 !isEquivalentType(OpF1->getType(), OpF2->getType()))
Nick Lewycky579a0242008-11-02 05:52:50 +0000409 return false;
410 }
Nick Lewycky579a0242008-11-02 05:52:50 +0000411 }
412
Nick Lewycky78d43302010-08-02 05:23:03 +0000413 ++F1I, ++F2I;
414 } while (F1I != F1E && F2I != F2E);
Nick Lewycky579a0242008-11-02 05:52:50 +0000415
Nick Lewycky78d43302010-08-02 05:23:03 +0000416 return F1I == F1E && F2I == F2E;
Nick Lewycky579a0242008-11-02 05:52:50 +0000417}
418
Nick Lewycky78d43302010-08-02 05:23:03 +0000419bool FunctionComparator::Compare() {
Nick Lewycky579a0242008-11-02 05:52:50 +0000420 // We need to recheck everything, but check the things that weren't included
421 // in the hash first.
422
Nick Lewycky78d43302010-08-02 05:23:03 +0000423 if (F1->getAttributes() != F2->getAttributes())
Nick Lewycky579a0242008-11-02 05:52:50 +0000424 return false;
425
Nick Lewycky78d43302010-08-02 05:23:03 +0000426 if (F1->hasGC() != F2->hasGC())
Nick Lewycky579a0242008-11-02 05:52:50 +0000427 return false;
428
Nick Lewycky78d43302010-08-02 05:23:03 +0000429 if (F1->hasGC() && F1->getGC() != F2->getGC())
Nick Lewycky579a0242008-11-02 05:52:50 +0000430 return false;
431
Nick Lewycky78d43302010-08-02 05:23:03 +0000432 if (F1->hasSection() != F2->hasSection())
Nick Lewycky579a0242008-11-02 05:52:50 +0000433 return false;
434
Nick Lewycky78d43302010-08-02 05:23:03 +0000435 if (F1->hasSection() && F1->getSection() != F2->getSection())
Nick Lewycky579a0242008-11-02 05:52:50 +0000436 return false;
437
Nick Lewycky78d43302010-08-02 05:23:03 +0000438 if (F1->isVarArg() != F2->isVarArg())
Nick Lewycky287de602009-06-12 08:04:51 +0000439 return false;
440
Nick Lewycky579a0242008-11-02 05:52:50 +0000441 // TODO: if it's internal and only used in direct calls, we could handle this
442 // case too.
Nick Lewycky78d43302010-08-02 05:23:03 +0000443 if (F1->getCallingConv() != F2->getCallingConv())
Nick Lewycky579a0242008-11-02 05:52:50 +0000444 return false;
445
Nick Lewycky78d43302010-08-02 05:23:03 +0000446 if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType()))
Nick Lewycky579a0242008-11-02 05:52:50 +0000447 return false;
448
Nick Lewycky78d43302010-08-02 05:23:03 +0000449 assert(F1->arg_size() == F2->arg_size() &&
Nick Lewycky579a0242008-11-02 05:52:50 +0000450 "Identical functions have a different number of args.");
451
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000452 // Visit the arguments so that they get enumerated in the order they're
453 // passed in.
Nick Lewycky78d43302010-08-02 05:23:03 +0000454 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 Lewycky33ab0b12010-05-13 05:48:45 +0000457 llvm_unreachable("Arguments repeat");
Nick Lewycky579a0242008-11-02 05:52:50 +0000458 }
459
Nick Lewycky78d43302010-08-02 05:23:03 +0000460 // 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000466
Nick Lewycky78d43302010-08-02 05:23:03 +0000467 F1BBs.push_back(&F1->getEntryBlock());
468 F2BBs.push_back(&F2->getEntryBlock());
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000469
Nick Lewycky78d43302010-08-02 05:23:03 +0000470 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000474
Nick Lewycky78d43302010-08-02 05:23:03 +0000475 if (!Enumerate(F1BB, F2BB) || !Compare(F1BB, F2BB))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000476 return false;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000477
Nick Lewycky78d43302010-08-02 05:23:03 +0000478 const TerminatorInst *F1TI = F1BB->getTerminator();
479 const TerminatorInst *F2TI = F2BB->getTerminator();
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000480
Nick Lewycky78d43302010-08-02 05:23:03 +0000481 assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors());
482 for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) {
483 if (!VisitedBBs.insert(F1TI->getSuccessor(i)))
Nick Lewycky911ae392010-05-13 06:45:13 +0000484 continue;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000485
Nick Lewycky78d43302010-08-02 05:23:03 +0000486 F1BBs.push_back(F1TI->getSuccessor(i));
487 F2BBs.push_back(F2TI->getSuccessor(i));
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000488 }
489 }
Nick Lewycky579a0242008-11-02 05:52:50 +0000490 return true;
491}
492
Nick Lewycky287de602009-06-12 08:04:51 +0000493// ===----------------------------------------------------------------------===
494// Folding of functions
495// ===----------------------------------------------------------------------===
Nick Lewycky579a0242008-11-02 05:52:50 +0000496
Nick Lewycky287de602009-06-12 08:04:51 +0000497// Cases:
498// * F is external strong, G is external strong:
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000499// turn G into a thunk to F
Nick Lewycky287de602009-06-12 08:04:51 +0000500// * F is external strong, G is external weak:
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000501// turn G into a thunk to F
Nick Lewycky287de602009-06-12 08:04:51 +0000502// * F is external weak, G is external weak:
503// unfoldable
504// * F is external strong, G is internal:
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000505// turn G into a thunk to F
Nick Lewycky287de602009-06-12 08:04:51 +0000506// * F is internal, G is external weak
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000507// turn G into a thunk to F
Nick Lewycky287de602009-06-12 08:04:51 +0000508// * F is internal, G is internal:
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000509// turn G into a thunk to F
Nick Lewycky287de602009-06-12 08:04:51 +0000510//
Nick Lewycky287de602009-06-12 08:04:51 +0000511// external means 'externally visible' linkage != (internal,private)
512// internal means linkage == (internal,private)
513// weak means linkage mayBeOverridable
Nick Lewycky287de602009-06-12 08:04:51 +0000514
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000515/// ThunkGToF - Replace G with a simple tail call to bitcast(F). Also replace
516/// direct uses of G with bitcast(F).
Nick Lewycky287de602009-06-12 08:04:51 +0000517static void ThunkGToF(Function *F, Function *G) {
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000518 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000531 // 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 Lewycky8728d7a2009-06-12 15:56:56 +0000538 Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
539 G->getParent());
Owen Anderson1d0be152009-08-13 21:58:54 +0000540 BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
Nick Lewycky287de602009-06-12 08:04:51 +0000541
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000542 SmallVector<Value *, 16> Args;
Nick Lewycky287de602009-06-12 08:04:51 +0000543 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 Lewycky33ab0b12010-05-13 05:48:45 +0000547 if (FFTy->getParamType(i) == AI->getType()) {
Nick Lewycky287de602009-06-12 08:04:51 +0000548 Args.push_back(AI);
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000549 } else {
550 Args.push_back(new BitCastInst(AI, FFTy->getParamType(i), "", BB));
Nick Lewycky287de602009-06-12 08:04:51 +0000551 }
552 ++i;
553 }
554
555 CallInst *CI = CallInst::Create(F, Args.begin(), Args.end(), "", BB);
556 CI->setTailCall();
Nick Lewyckyb3c36c92009-06-12 16:04:00 +0000557 CI->setCallingConv(F->getCallingConv());
Benjamin Kramerf0127052010-01-05 13:12:22 +0000558 if (NewG->getReturnType()->isVoidTy()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000559 ReturnInst::Create(F->getContext(), BB);
Nick Lewycky287de602009-06-12 08:04:51 +0000560 } else if (CI->getType() != NewG->getReturnType()) {
561 Value *BCI = new BitCastInst(CI, NewG->getReturnType(), "", BB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000562 ReturnInst::Create(F->getContext(), BCI, BB);
Nick Lewycky287de602009-06-12 08:04:51 +0000563 } else {
Owen Anderson1d0be152009-08-13 21:58:54 +0000564 ReturnInst::Create(F->getContext(), CI, BB);
Nick Lewycky287de602009-06-12 08:04:51 +0000565 }
566
567 NewG->copyAttributesFrom(G);
568 NewG->takeName(G);
569 G->replaceAllUsesWith(NewG);
570 G->eraseFromParent();
Nick Lewycky287de602009-06-12 08:04:51 +0000571}
572
Nick Lewycky287de602009-06-12 08:04:51 +0000573static bool fold(std::vector<Function *> &FnVec, unsigned i, unsigned j) {
Nick Lewycky579a0242008-11-02 05:52:50 +0000574 Function *F = FnVec[i];
575 Function *G = FnVec[j];
576
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000577 if (F->isWeakForLinker() && !G->isWeakForLinker()) {
Nick Lewycky287de602009-06-12 08:04:51 +0000578 std::swap(FnVec[i], FnVec[j]);
579 std::swap(F, G);
Nick Lewycky579a0242008-11-02 05:52:50 +0000580 }
581
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000582 if (F->isWeakForLinker()) {
583 assert(G->isWeakForLinker());
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000584
585 // Make them both thunks to the same internal function.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000586 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 Lewyckyc9dcbed2010-08-06 07:21:30 +0000595 F->setAlignment(std::max(G->getAlignment(), H->getAlignment()));
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000596 F->setLinkage(GlobalValue::InternalLinkage);
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000597 } else {
598 ThunkGToF(F, G);
Nick Lewycky6feb3332008-11-02 16:46:26 +0000599 }
600
Nick Lewycky287de602009-06-12 08:04:51 +0000601 ++NumFunctionsMerged;
602 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000603}
604
Nick Lewycky287de602009-06-12 08:04:51 +0000605// ===----------------------------------------------------------------------===
606// Pass definition
607// ===----------------------------------------------------------------------===
Nick Lewycky579a0242008-11-02 05:52:50 +0000608
609bool MergeFunctions::runOnModule(Module &M) {
610 bool Changed = false;
611
Duncan Sands5baf8ec2008-11-02 09:00:33 +0000612 std::map<unsigned long, std::vector<Function *> > FnMap;
Nick Lewycky579a0242008-11-02 05:52:50 +0000613
614 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000615 if (F->isDeclaration() || F->hasAvailableExternallyLinkage())
Nick Lewycky579a0242008-11-02 05:52:50 +0000616 continue;
617
Nick Lewycky78d43302010-08-02 05:23:03 +0000618 FnMap[ProfileFunction(F)].push_back(F);
Nick Lewycky579a0242008-11-02 05:52:50 +0000619 }
620
Nick Lewycky78d43302010-08-02 05:23:03 +0000621 TargetData *TD = getAnalysisIfAvailable<TargetData>();
Nick Lewycky579a0242008-11-02 05:52:50 +0000622
623 bool LocalChanged;
624 do {
625 LocalChanged = false;
David Greene3a078b52010-01-05 01:28:12 +0000626 DEBUG(dbgs() << "size: " << FnMap.size() << "\n");
Duncan Sands5baf8ec2008-11-02 09:00:33 +0000627 for (std::map<unsigned long, std::vector<Function *> >::iterator
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000628 I = FnMap.begin(), E = FnMap.end(); I != E; ++I) {
Nick Lewycky579a0242008-11-02 05:52:50 +0000629 std::vector<Function *> &FnVec = I->second;
David Greene3a078b52010-01-05 01:28:12 +0000630 DEBUG(dbgs() << "hash (" << I->first << "): " << FnVec.size() << "\n");
Nick Lewycky579a0242008-11-02 05:52:50 +0000631
632 for (int i = 0, e = FnVec.size(); i != e; ++i) {
633 for (int j = i + 1; j != e; ++j) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000634 bool isEqual = FunctionComparator(TD, FnVec[i], FnVec[j]).Compare();
Nick Lewycky579a0242008-11-02 05:52:50 +0000635
David Greene3a078b52010-01-05 01:28:12 +0000636 DEBUG(dbgs() << " " << FnVec[i]->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000637 << (isEqual ? " == " : " != ")
638 << FnVec[j]->getName() << "\n");
Nick Lewycky579a0242008-11-02 05:52:50 +0000639
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}