blob: d7075b9277de0e2f8581358e8e4ddc849c423650 [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"
48#include "llvm/ADT/DenseMap.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"
52#include "llvm/Constants.h"
53#include "llvm/InlineAsm.h"
54#include "llvm/Instructions.h"
Owen Anderson14ce9ef2009-07-06 01:34:54 +000055#include "llvm/LLVMContext.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000056#include "llvm/Module.h"
57#include "llvm/Pass.h"
Nick Lewycky6feb3332008-11-02 16:46:26 +000058#include "llvm/Support/CallSite.h"
Nick Lewycky579a0242008-11-02 05:52:50 +000059#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000060#include "llvm/Support/ErrorHandling.h"
Nick Lewyckybe04fde2010-08-08 05:04:23 +000061#include "llvm/Support/IRBuilder.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 ///
Nick Lewyckybe04fde2010-08-08 05:04:23 +000076 class MergeFunctions : public ModulePass {
77 public:
78 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000079 MergeFunctions() : ModulePass(ID) {}
Nick Lewycky579a0242008-11-02 05:52:50 +000080
81 bool runOnModule(Module &M);
Nick Lewyckybe04fde2010-08-08 05:04:23 +000082
83 private:
84 /// PairwiseCompareAndMerge - Given a list of functions, compare each pair
85 /// and merge the pairs of equivalent functions.
86 bool PairwiseCompareAndMerge(std::vector<Function *> &FnVec);
87
88 /// MergeTwoFunctions - Merge two equivalent functions. Upon completion,
89 /// FnVec[j] should never be visited again.
90 void MergeTwoFunctions(std::vector<Function *> &FnVec,
91 unsigned i, unsigned j) const;
92
93 /// WriteThunk - Replace G with a simple tail call to bitcast(F). Also
94 /// replace direct uses of G with bitcast(F).
95 void WriteThunk(Function *F, Function *G) const;
96
97 TargetData *TD;
Nick Lewycky579a0242008-11-02 05:52:50 +000098 };
99}
100
101char MergeFunctions::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000102INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false);
Nick Lewycky579a0242008-11-02 05:52:50 +0000103
104ModulePass *llvm::createMergeFunctionsPass() {
105 return new MergeFunctions();
106}
107
Nick Lewycky78d43302010-08-02 05:23:03 +0000108namespace {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000109/// FunctionComparator - Compares two functions to determine whether or not
110/// they will generate machine code with the same behaviour. TargetData is
111/// used if available. The comparator always fails conservatively (erring on the
112/// side of claiming that two functions are different).
Nick Lewycky78d43302010-08-02 05:23:03 +0000113class FunctionComparator {
114public:
115 FunctionComparator(TargetData *TD, Function *F1, Function *F2)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000116 : F1(F1), F2(F2), TD(TD), IDMap1Count(0), IDMap2Count(0) {}
Nick Lewycky287de602009-06-12 08:04:51 +0000117
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000118 /// Compare - test whether the two functions have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000119 bool Compare();
120
121private:
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000122 /// Compare - test whether two basic blocks have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000123 bool Compare(const BasicBlock *BB1, const BasicBlock *BB2);
124
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000125 /// Enumerate - Assign or look up previously assigned numbers for the two
126 /// values, and return whether the numbers are equal. Numbers are assigned in
127 /// the order visited.
Nick Lewycky78d43302010-08-02 05:23:03 +0000128 bool Enumerate(const Value *V1, const Value *V2);
129
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000130 /// isEquivalentOperation - Compare two Instructions for equivalence, similar
131 /// to Instruction::isSameOperationAs but with modifications to the type
132 /// comparison.
Nick Lewycky78d43302010-08-02 05:23:03 +0000133 bool isEquivalentOperation(const Instruction *I1,
134 const Instruction *I2) const;
135
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000136 /// isEquivalentGEP - Compare two GEPs for equivalent pointer arithmetic.
Nick Lewycky78d43302010-08-02 05:23:03 +0000137 bool isEquivalentGEP(const GEPOperator *GEP1, const GEPOperator *GEP2);
138 bool isEquivalentGEP(const GetElementPtrInst *GEP1,
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000139 const GetElementPtrInst *GEP2) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000140 return isEquivalentGEP(cast<GEPOperator>(GEP1), cast<GEPOperator>(GEP2));
141 }
142
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000143 /// isEquivalentType - Compare two Types, treating all pointer types as equal.
Nick Lewycky78d43302010-08-02 05:23:03 +0000144 bool isEquivalentType(const Type *Ty1, const Type *Ty2) const;
145
146 // The two functions undergoing comparison.
147 Function *F1, *F2;
148
149 TargetData *TD;
150
151 typedef DenseMap<const Value *, unsigned long> IDMap;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000152 IDMap Map1, Map2;
153 unsigned long IDMap1Count, IDMap2Count;
Nick Lewycky78d43302010-08-02 05:23:03 +0000154};
155}
156
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000157/// Compute a hash guaranteed to be equal for two equivalent functions, but
158/// very likely to be different for different functions.
Nick Lewycky78d43302010-08-02 05:23:03 +0000159static unsigned long ProfileFunction(const Function *F) {
Nick Lewycky287de602009-06-12 08:04:51 +0000160 const FunctionType *FTy = F->getFunctionType();
161
162 FoldingSetNodeID ID;
163 ID.AddInteger(F->size());
164 ID.AddInteger(F->getCallingConv());
165 ID.AddBoolean(F->hasGC());
166 ID.AddBoolean(FTy->isVarArg());
167 ID.AddInteger(FTy->getReturnType()->getTypeID());
168 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
169 ID.AddInteger(FTy->getParamType(i)->getTypeID());
170 return ID.ComputeHash();
171}
172
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000173/// isEquivalentType - any two pointers in the same address space are
174/// equivalent. Otherwise, standard type equivalence rules apply.
Nick Lewycky78d43302010-08-02 05:23:03 +0000175bool FunctionComparator::isEquivalentType(const Type *Ty1,
176 const Type *Ty2) const {
Nick Lewycky287de602009-06-12 08:04:51 +0000177 if (Ty1 == Ty2)
178 return true;
179 if (Ty1->getTypeID() != Ty2->getTypeID())
180 return false;
181
182 switch(Ty1->getTypeID()) {
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000183 default:
184 llvm_unreachable("Unknown type!");
Duncan Sands8246adc2010-07-07 07:48:00 +0000185 // Fall through in Release mode.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000186 case Type::IntegerTyID:
187 case Type::OpaqueTyID:
188 // Ty1 == Ty2 would have returned true earlier.
189 return false;
190
Nick Lewycky287de602009-06-12 08:04:51 +0000191 case Type::VoidTyID:
192 case Type::FloatTyID:
193 case Type::DoubleTyID:
194 case Type::X86_FP80TyID:
195 case Type::FP128TyID:
196 case Type::PPC_FP128TyID:
197 case Type::LabelTyID:
198 case Type::MetadataTyID:
199 return true;
200
Nick Lewycky287de602009-06-12 08:04:51 +0000201 case Type::PointerTyID: {
202 const PointerType *PTy1 = cast<PointerType>(Ty1);
203 const PointerType *PTy2 = cast<PointerType>(Ty2);
204 return PTy1->getAddressSpace() == PTy2->getAddressSpace();
205 }
206
207 case Type::StructTyID: {
208 const StructType *STy1 = cast<StructType>(Ty1);
209 const StructType *STy2 = cast<StructType>(Ty2);
210 if (STy1->getNumElements() != STy2->getNumElements())
211 return false;
212
213 if (STy1->isPacked() != STy2->isPacked())
214 return false;
215
216 for (unsigned i = 0, e = STy1->getNumElements(); i != e; ++i) {
217 if (!isEquivalentType(STy1->getElementType(i), STy2->getElementType(i)))
218 return false;
219 }
220 return true;
221 }
222
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000223 case Type::UnionTyID: {
224 const UnionType *UTy1 = cast<UnionType>(Ty1);
225 const UnionType *UTy2 = cast<UnionType>(Ty2);
226
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000227 if (UTy1->getNumElements() != UTy2->getNumElements())
228 return false;
229
230 for (unsigned i = 0, e = UTy1->getNumElements(); i != e; ++i) {
231 if (!isEquivalentType(UTy1->getElementType(i), UTy2->getElementType(i)))
232 return false;
233 }
234 return true;
235 }
236
Nick Lewycky287de602009-06-12 08:04:51 +0000237 case Type::FunctionTyID: {
238 const FunctionType *FTy1 = cast<FunctionType>(Ty1);
239 const FunctionType *FTy2 = cast<FunctionType>(Ty2);
240 if (FTy1->getNumParams() != FTy2->getNumParams() ||
241 FTy1->isVarArg() != FTy2->isVarArg())
242 return false;
243
244 if (!isEquivalentType(FTy1->getReturnType(), FTy2->getReturnType()))
245 return false;
246
247 for (unsigned i = 0, e = FTy1->getNumParams(); i != e; ++i) {
248 if (!isEquivalentType(FTy1->getParamType(i), FTy2->getParamType(i)))
249 return false;
250 }
251 return true;
252 }
253
Nick Lewycky394ce412010-07-16 06:31:12 +0000254 case Type::ArrayTyID: {
255 const ArrayType *ATy1 = cast<ArrayType>(Ty1);
256 const ArrayType *ATy2 = cast<ArrayType>(Ty2);
257 return ATy1->getNumElements() == ATy2->getNumElements() &&
258 isEquivalentType(ATy1->getElementType(), ATy2->getElementType());
259 }
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000260
Nick Lewycky287de602009-06-12 08:04:51 +0000261 case Type::VectorTyID: {
Nick Lewycky394ce412010-07-16 06:31:12 +0000262 const VectorType *VTy1 = cast<VectorType>(Ty1);
263 const VectorType *VTy2 = cast<VectorType>(Ty2);
264 return VTy1->getNumElements() == VTy2->getNumElements() &&
265 isEquivalentType(VTy1->getElementType(), VTy2->getElementType());
Nick Lewycky287de602009-06-12 08:04:51 +0000266 }
267 }
268}
269
270/// isEquivalentOperation - determine whether the two operations are the same
271/// except that pointer-to-A and pointer-to-B are equivalent. This should be
Dan Gohman194ae782009-06-12 19:03:05 +0000272/// kept in sync with Instruction::isSameOperationAs.
Nick Lewycky78d43302010-08-02 05:23:03 +0000273bool FunctionComparator::isEquivalentOperation(const Instruction *I1,
274 const Instruction *I2) const {
Nick Lewycky287de602009-06-12 08:04:51 +0000275 if (I1->getOpcode() != I2->getOpcode() ||
276 I1->getNumOperands() != I2->getNumOperands() ||
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000277 !isEquivalentType(I1->getType(), I2->getType()) ||
278 !I1->hasSameSubclassOptionalData(I2))
Nick Lewycky287de602009-06-12 08:04:51 +0000279 return false;
280
281 // We have two instructions of identical opcode and #operands. Check to see
282 // if all operands are the same type
283 for (unsigned i = 0, e = I1->getNumOperands(); i != e; ++i)
284 if (!isEquivalentType(I1->getOperand(i)->getType(),
285 I2->getOperand(i)->getType()))
286 return false;
287
288 // Check special state that is a part of some instructions.
289 if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
290 return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
291 LI->getAlignment() == cast<LoadInst>(I2)->getAlignment();
292 if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
293 return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
294 SI->getAlignment() == cast<StoreInst>(I2)->getAlignment();
295 if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
296 return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
297 if (const CallInst *CI = dyn_cast<CallInst>(I1))
298 return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
299 CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
300 CI->getAttributes().getRawPointer() ==
301 cast<CallInst>(I2)->getAttributes().getRawPointer();
302 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
303 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
304 CI->getAttributes().getRawPointer() ==
305 cast<InvokeInst>(I2)->getAttributes().getRawPointer();
306 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) {
307 if (IVI->getNumIndices() != cast<InsertValueInst>(I2)->getNumIndices())
308 return false;
309 for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
310 if (IVI->idx_begin()[i] != cast<InsertValueInst>(I2)->idx_begin()[i])
311 return false;
312 return true;
313 }
314 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) {
315 if (EVI->getNumIndices() != cast<ExtractValueInst>(I2)->getNumIndices())
316 return false;
317 for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
318 if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I2)->idx_begin()[i])
319 return false;
320 return true;
321 }
322
323 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000324}
325
Nick Lewycky78d43302010-08-02 05:23:03 +0000326/// isEquivalentGEP - determine whether two GEP operations perform the same
327/// underlying arithmetic.
328bool FunctionComparator::isEquivalentGEP(const GEPOperator *GEP1,
329 const GEPOperator *GEP2) {
330 // When we have target data, we can reduce the GEP down to the value in bytes
331 // added to the address.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000332 if (TD && GEP1->hasAllConstantIndices() && GEP2->hasAllConstantIndices()) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000333 SmallVector<Value *, 8> Indices1(GEP1->idx_begin(), GEP1->idx_end());
334 SmallVector<Value *, 8> Indices2(GEP2->idx_begin(), GEP2->idx_end());
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000335 uint64_t Offset1 = TD->getIndexedOffset(GEP1->getPointerOperandType(),
336 Indices1.data(), Indices1.size());
337 uint64_t Offset2 = TD->getIndexedOffset(GEP2->getPointerOperandType(),
338 Indices2.data(), Indices2.size());
339 return Offset1 == Offset2;
Nick Lewycky579a0242008-11-02 05:52:50 +0000340 }
341
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000342 if (GEP1->getPointerOperand()->getType() !=
343 GEP2->getPointerOperand()->getType())
344 return false;
345
346 if (GEP1->getNumOperands() != GEP2->getNumOperands())
347 return false;
348
349 for (unsigned i = 0, e = GEP1->getNumOperands(); i != e; ++i) {
Nick Lewycky78d43302010-08-02 05:23:03 +0000350 if (!Enumerate(GEP1->getOperand(i), GEP2->getOperand(i)))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000351 return false;
352 }
353
354 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000355}
356
Nick Lewycky78d43302010-08-02 05:23:03 +0000357/// Enumerate - Compare two values used by the two functions under pair-wise
358/// comparison. If this is the first time the values are seen, they're added to
359/// the mapping so that we will detect mismatches on next use.
360bool FunctionComparator::Enumerate(const Value *V1, const Value *V2) {
361 // Check for function @f1 referring to itself and function @f2 referring to
362 // itself, or referring to each other, or both referring to either of them.
363 // They're all equivalent if the two functions are otherwise equivalent.
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000364 if (V1 == F1 && V2 == F2)
365 return true;
366 if (V1 == F2 && V2 == F1)
367 return true;
Nick Lewycky579a0242008-11-02 05:52:50 +0000368
Nick Lewycky78d43302010-08-02 05:23:03 +0000369 // TODO: constant expressions with GEP or references to F1 or F2.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000370 if (isa<Constant>(V1))
371 return V1 == V2;
372
373 if (isa<InlineAsm>(V1) && isa<InlineAsm>(V2)) {
374 const InlineAsm *IA1 = cast<InlineAsm>(V1);
375 const InlineAsm *IA2 = cast<InlineAsm>(V2);
376 return IA1->getAsmString() == IA2->getAsmString() &&
377 IA1->getConstraintString() == IA2->getConstraintString();
378 }
379
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000380 unsigned long &ID1 = Map1[V1];
381 if (!ID1)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000382 ID1 = ++IDMap1Count;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000383
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000384 unsigned long &ID2 = Map2[V2];
385 if (!ID2)
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000386 ID2 = ++IDMap2Count;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000387
388 return ID1 == ID2;
389}
390
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000391/// Compare - test whether two basic blocks have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000392bool FunctionComparator::Compare(const BasicBlock *BB1, const BasicBlock *BB2) {
393 BasicBlock::const_iterator F1I = BB1->begin(), F1E = BB1->end();
394 BasicBlock::const_iterator F2I = BB2->begin(), F2E = BB2->end();
Nick Lewycky579a0242008-11-02 05:52:50 +0000395
396 do {
Nick Lewycky78d43302010-08-02 05:23:03 +0000397 if (!Enumerate(F1I, F2I))
Nick Lewycky579a0242008-11-02 05:52:50 +0000398 return false;
399
Nick Lewycky78d43302010-08-02 05:23:03 +0000400 if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) {
401 const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I);
402 if (!GEP2)
403 return false;
Nick Lewyckya142c932009-06-13 19:09:52 +0000404
Nick Lewycky78d43302010-08-02 05:23:03 +0000405 if (!Enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand()))
Nick Lewycky911ae392010-05-13 06:45:13 +0000406 return false;
Nick Lewyckya142c932009-06-13 19:09:52 +0000407
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000408 if (!isEquivalentGEP(GEP1, GEP2))
Nick Lewycky911ae392010-05-13 06:45:13 +0000409 return false;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000410 } else {
Nick Lewycky78d43302010-08-02 05:23:03 +0000411 if (!isEquivalentOperation(F1I, F2I))
Nick Lewycky579a0242008-11-02 05:52:50 +0000412 return false;
413
Nick Lewycky78d43302010-08-02 05:23:03 +0000414 assert(F1I->getNumOperands() == F2I->getNumOperands());
415 for (unsigned i = 0, e = F1I->getNumOperands(); i != e; ++i) {
416 Value *OpF1 = F1I->getOperand(i);
417 Value *OpF2 = F2I->getOperand(i);
Nick Lewycky579a0242008-11-02 05:52:50 +0000418
Nick Lewycky78d43302010-08-02 05:23:03 +0000419 if (!Enumerate(OpF1, OpF2))
Nick Lewycky911ae392010-05-13 06:45:13 +0000420 return false;
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000421
Nick Lewycky78d43302010-08-02 05:23:03 +0000422 if (OpF1->getValueID() != OpF2->getValueID() ||
423 !isEquivalentType(OpF1->getType(), OpF2->getType()))
Nick Lewycky579a0242008-11-02 05:52:50 +0000424 return false;
425 }
Nick Lewycky579a0242008-11-02 05:52:50 +0000426 }
427
Nick Lewycky78d43302010-08-02 05:23:03 +0000428 ++F1I, ++F2I;
429 } while (F1I != F1E && F2I != F2E);
Nick Lewycky579a0242008-11-02 05:52:50 +0000430
Nick Lewycky78d43302010-08-02 05:23:03 +0000431 return F1I == F1E && F2I == F2E;
Nick Lewycky579a0242008-11-02 05:52:50 +0000432}
433
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000434/// Compare - test whether the two functions have equivalent behaviour.
Nick Lewycky78d43302010-08-02 05:23:03 +0000435bool FunctionComparator::Compare() {
Nick Lewycky579a0242008-11-02 05:52:50 +0000436 // We need to recheck everything, but check the things that weren't included
437 // in the hash first.
438
Nick Lewycky78d43302010-08-02 05:23:03 +0000439 if (F1->getAttributes() != F2->getAttributes())
Nick Lewycky579a0242008-11-02 05:52:50 +0000440 return false;
441
Nick Lewycky78d43302010-08-02 05:23:03 +0000442 if (F1->hasGC() != F2->hasGC())
Nick Lewycky579a0242008-11-02 05:52:50 +0000443 return false;
444
Nick Lewycky78d43302010-08-02 05:23:03 +0000445 if (F1->hasGC() && F1->getGC() != F2->getGC())
Nick Lewycky579a0242008-11-02 05:52:50 +0000446 return false;
447
Nick Lewycky78d43302010-08-02 05:23:03 +0000448 if (F1->hasSection() != F2->hasSection())
Nick Lewycky579a0242008-11-02 05:52:50 +0000449 return false;
450
Nick Lewycky78d43302010-08-02 05:23:03 +0000451 if (F1->hasSection() && F1->getSection() != F2->getSection())
Nick Lewycky579a0242008-11-02 05:52:50 +0000452 return false;
453
Nick Lewycky78d43302010-08-02 05:23:03 +0000454 if (F1->isVarArg() != F2->isVarArg())
Nick Lewycky287de602009-06-12 08:04:51 +0000455 return false;
456
Nick Lewycky579a0242008-11-02 05:52:50 +0000457 // TODO: if it's internal and only used in direct calls, we could handle this
458 // case too.
Nick Lewycky78d43302010-08-02 05:23:03 +0000459 if (F1->getCallingConv() != F2->getCallingConv())
Nick Lewycky579a0242008-11-02 05:52:50 +0000460 return false;
461
Nick Lewycky78d43302010-08-02 05:23:03 +0000462 if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType()))
Nick Lewycky579a0242008-11-02 05:52:50 +0000463 return false;
464
Nick Lewycky78d43302010-08-02 05:23:03 +0000465 assert(F1->arg_size() == F2->arg_size() &&
Nick Lewycky579a0242008-11-02 05:52:50 +0000466 "Identical functions have a different number of args.");
467
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000468 // Visit the arguments so that they get enumerated in the order they're
469 // passed in.
Nick Lewycky78d43302010-08-02 05:23:03 +0000470 for (Function::const_arg_iterator f1i = F1->arg_begin(),
471 f2i = F2->arg_begin(), f1e = F1->arg_end(); f1i != f1e; ++f1i, ++f2i) {
472 if (!Enumerate(f1i, f2i))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000473 llvm_unreachable("Arguments repeat");
Nick Lewycky579a0242008-11-02 05:52:50 +0000474 }
475
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000476 // We do a CFG-ordered walk since the actual ordering of the blocks in the
477 // linked list is immaterial. Our walk starts at the entry block for both
Nick Lewycky78d43302010-08-02 05:23:03 +0000478 // functions, then takes each block from each terminator in order. As an
479 // artifact, this also means that unreachable blocks are ignored.
480 SmallVector<const BasicBlock *, 8> F1BBs, F2BBs;
481 SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1.
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000482
Nick Lewycky78d43302010-08-02 05:23:03 +0000483 F1BBs.push_back(&F1->getEntryBlock());
484 F2BBs.push_back(&F2->getEntryBlock());
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000485
Nick Lewycky78d43302010-08-02 05:23:03 +0000486 VisitedBBs.insert(F1BBs[0]);
487 while (!F1BBs.empty()) {
488 const BasicBlock *F1BB = F1BBs.pop_back_val();
489 const BasicBlock *F2BB = F2BBs.pop_back_val();
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000490
Nick Lewycky78d43302010-08-02 05:23:03 +0000491 if (!Enumerate(F1BB, F2BB) || !Compare(F1BB, F2BB))
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000492 return false;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000493
Nick Lewycky78d43302010-08-02 05:23:03 +0000494 const TerminatorInst *F1TI = F1BB->getTerminator();
495 const TerminatorInst *F2TI = F2BB->getTerminator();
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000496
Nick Lewycky78d43302010-08-02 05:23:03 +0000497 assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors());
498 for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) {
499 if (!VisitedBBs.insert(F1TI->getSuccessor(i)))
Nick Lewycky911ae392010-05-13 06:45:13 +0000500 continue;
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000501
Nick Lewycky78d43302010-08-02 05:23:03 +0000502 F1BBs.push_back(F1TI->getSuccessor(i));
503 F2BBs.push_back(F2TI->getSuccessor(i));
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000504 }
505 }
Nick Lewycky579a0242008-11-02 05:52:50 +0000506 return true;
507}
508
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000509/// WriteThunk - Replace G with a simple tail call to bitcast(F). Also replace
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000510/// direct uses of G with bitcast(F).
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000511void MergeFunctions::WriteThunk(Function *F, Function *G) const {
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000512 if (!G->mayBeOverridden()) {
513 // Redirect direct callers of G to F.
514 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
515 for (Value::use_iterator UI = G->use_begin(), UE = G->use_end();
516 UI != UE;) {
517 Value::use_iterator TheIter = UI;
518 ++UI;
519 CallSite CS(*TheIter);
520 if (CS && CS.isCallee(TheIter))
521 TheIter.getUse().set(BitcastF);
522 }
523 }
524
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000525 // If G was internal then we may have replaced all uses if G with F. If so,
526 // stop here and delete G. There's no need for a thunk.
527 if (G->hasLocalLinkage() && G->use_empty()) {
528 G->eraseFromParent();
529 return;
530 }
531
Nick Lewycky8728d7a2009-06-12 15:56:56 +0000532 Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
533 G->getParent());
Owen Anderson1d0be152009-08-13 21:58:54 +0000534 BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000535 IRBuilder<false> Builder(BB);
Nick Lewycky287de602009-06-12 08:04:51 +0000536
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000537 SmallVector<Value *, 16> Args;
Nick Lewycky287de602009-06-12 08:04:51 +0000538 unsigned i = 0;
539 const FunctionType *FFTy = F->getFunctionType();
540 for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end();
541 AI != AE; ++AI) {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000542 Args.push_back(Builder.CreateBitCast(AI, FFTy->getParamType(i)));
Nick Lewycky287de602009-06-12 08:04:51 +0000543 ++i;
544 }
545
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000546 CallInst *CI = Builder.CreateCall(F, Args.begin(), Args.end());
Nick Lewycky287de602009-06-12 08:04:51 +0000547 CI->setTailCall();
Nick Lewyckyb3c36c92009-06-12 16:04:00 +0000548 CI->setCallingConv(F->getCallingConv());
Benjamin Kramerf0127052010-01-05 13:12:22 +0000549 if (NewG->getReturnType()->isVoidTy()) {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000550 Builder.CreateRetVoid();
Nick Lewycky287de602009-06-12 08:04:51 +0000551 } else {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000552 Builder.CreateRet(Builder.CreateBitCast(CI, NewG->getReturnType()));
Nick Lewycky287de602009-06-12 08:04:51 +0000553 }
554
555 NewG->copyAttributesFrom(G);
556 NewG->takeName(G);
557 G->replaceAllUsesWith(NewG);
558 G->eraseFromParent();
Nick Lewycky287de602009-06-12 08:04:51 +0000559}
560
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000561/// MergeTwoFunctions - Merge two equivalent functions. Upon completion,
Nick Lewycky32218342010-08-09 21:03:28 +0000562/// FnVec[j] is deleted but not removed from the vector.
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000563void MergeFunctions::MergeTwoFunctions(std::vector<Function *> &FnVec,
564 unsigned i, unsigned j) const {
Nick Lewycky579a0242008-11-02 05:52:50 +0000565 Function *F = FnVec[i];
566 Function *G = FnVec[j];
567
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000568 if (F->isWeakForLinker() && !G->isWeakForLinker()) {
Nick Lewycky287de602009-06-12 08:04:51 +0000569 std::swap(FnVec[i], FnVec[j]);
570 std::swap(F, G);
Nick Lewycky579a0242008-11-02 05:52:50 +0000571 }
572
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000573 if (F->isWeakForLinker()) {
574 assert(G->isWeakForLinker());
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000575
576 // Make them both thunks to the same internal function.
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000577 Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
578 F->getParent());
579 H->copyAttributesFrom(F);
580 H->takeName(F);
581 F->replaceAllUsesWith(H);
582
Nick Lewycky32218342010-08-09 21:03:28 +0000583 unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
584
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000585 WriteThunk(F, G);
586 WriteThunk(F, H);
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000587
Nick Lewycky32218342010-08-09 21:03:28 +0000588 F->setAlignment(MaxAlignment);
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000589 F->setLinkage(GlobalValue::InternalLinkage);
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000590 } else {
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000591 WriteThunk(F, G);
Nick Lewycky6feb3332008-11-02 16:46:26 +0000592 }
593
Nick Lewycky287de602009-06-12 08:04:51 +0000594 ++NumFunctionsMerged;
Nick Lewycky579a0242008-11-02 05:52:50 +0000595}
596
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000597/// PairwiseCompareAndMerge - Given a list of functions, compare each pair and
598/// merge the pairs of equivalent functions.
599bool MergeFunctions::PairwiseCompareAndMerge(std::vector<Function *> &FnVec) {
600 bool Changed = false;
601 for (int i = 0, e = FnVec.size(); i != e; ++i) {
602 for (int j = i + 1; j != e; ++j) {
603 bool isEqual = FunctionComparator(TD, FnVec[i], FnVec[j]).Compare();
604
605 DEBUG(dbgs() << " " << FnVec[i]->getName()
606 << (isEqual ? " == " : " != ") << FnVec[j]->getName() << "\n");
607
608 if (isEqual) {
609 MergeTwoFunctions(FnVec, i, j);
610 Changed = true;
611 FnVec.erase(FnVec.begin() + j);
612 --j, --e;
613 }
614 }
615 }
616 return Changed;
617}
Nick Lewycky579a0242008-11-02 05:52:50 +0000618
619bool MergeFunctions::runOnModule(Module &M) {
620 bool Changed = false;
621
Duncan Sands5baf8ec2008-11-02 09:00:33 +0000622 std::map<unsigned long, std::vector<Function *> > FnMap;
Nick Lewycky579a0242008-11-02 05:52:50 +0000623
624 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Nick Lewyckyc9dcbed2010-08-06 07:21:30 +0000625 if (F->isDeclaration() || F->hasAvailableExternallyLinkage())
Nick Lewycky579a0242008-11-02 05:52:50 +0000626 continue;
627
Nick Lewycky78d43302010-08-02 05:23:03 +0000628 FnMap[ProfileFunction(F)].push_back(F);
Nick Lewycky579a0242008-11-02 05:52:50 +0000629 }
630
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000631 TD = getAnalysisIfAvailable<TargetData>();
Nick Lewycky579a0242008-11-02 05:52:50 +0000632
633 bool LocalChanged;
634 do {
635 LocalChanged = false;
David Greene3a078b52010-01-05 01:28:12 +0000636 DEBUG(dbgs() << "size: " << FnMap.size() << "\n");
Duncan Sands5baf8ec2008-11-02 09:00:33 +0000637 for (std::map<unsigned long, std::vector<Function *> >::iterator
Nick Lewycky33ab0b12010-05-13 05:48:45 +0000638 I = FnMap.begin(), E = FnMap.end(); I != E; ++I) {
Nick Lewycky579a0242008-11-02 05:52:50 +0000639 std::vector<Function *> &FnVec = I->second;
David Greene3a078b52010-01-05 01:28:12 +0000640 DEBUG(dbgs() << "hash (" << I->first << "): " << FnVec.size() << "\n");
Nick Lewyckybe04fde2010-08-08 05:04:23 +0000641 LocalChanged |= PairwiseCompareAndMerge(FnVec);
Nick Lewycky579a0242008-11-02 05:52:50 +0000642 }
643 Changed |= LocalChanged;
644 } while (LocalChanged);
645
646 return Changed;
647}