Fix many bugs when merging weak-strong and weak-weak pairs. We now merge all
[oota-llvm.git] / lib / Transforms / IPO / MergeFunctions.cpp
1 //===- MergeFunctions.cpp - Merge identical functions ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass looks for equivalent functions that are mergable and folds them.
11 //
12 // A hash is computed from the function, based on its type and number of
13 // basic blocks.
14 //
15 // Once all hashes are computed, we perform an expensive equality comparison
16 // on each function pair. This takes n^2/2 comparisons per bucket, so it's
17 // important that the hash function be high quality. The equality comparison
18 // iterates through each instruction in each basic block.
19 //
20 // When a match is found the functions are folded. If both functions are
21 // overridable, we move the functionality into a new internal function and
22 // leave two overridable thunks to it.
23 //
24 //===----------------------------------------------------------------------===//
25 //
26 // Future work:
27 //
28 // * virtual functions.
29 //
30 // Many functions have their address taken by the virtual function table for
31 // the object they belong to. However, as long as it's only used for a lookup
32 // and call, this is irrelevant, and we'd like to fold such functions.
33 //
34 // * switch from n^2 pair-wise comparisons to an n-way comparison for each
35 // bucket.
36 //
37 // * be smarter about bitcasts.
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 look through bitcasts.
43 //
44 //===----------------------------------------------------------------------===//
45
46 #define DEBUG_TYPE "mergefunc"
47 #include "llvm/Transforms/IPO.h"
48 #include "llvm/ADT/DenseSet.h"
49 #include "llvm/ADT/FoldingSet.h"
50 #include "llvm/ADT/SmallSet.h"
51 #include "llvm/ADT/Statistic.h"
52 #include "llvm/ADT/STLExtras.h"
53 #include "llvm/Constants.h"
54 #include "llvm/InlineAsm.h"
55 #include "llvm/Instructions.h"
56 #include "llvm/LLVMContext.h"
57 #include "llvm/Module.h"
58 #include "llvm/Pass.h"
59 #include "llvm/Support/CallSite.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/ErrorHandling.h"
62 #include "llvm/Support/IRBuilder.h"
63 #include "llvm/Support/ValueHandle.h"
64 #include "llvm/Support/raw_ostream.h"
65 #include "llvm/Target/TargetData.h"
66 #include <vector>
67 using namespace llvm;
68
69 STATISTIC(NumFunctionsMerged, "Number of functions merged");
70
71 namespace {
72
73 static unsigned ProfileFunction(const Function *F) {
74   const FunctionType *FTy = F->getFunctionType();
75
76   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();
85 }
86
87 class ComparableFunction {
88 public:
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
97 struct 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 ///
116 class MergeFunctions : public ModulePass {
117 public:
118   static char ID;
119   MergeFunctions() : ModulePass(ID) {}
120
121   bool runOnModule(Module &M);
122
123 private:
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
145 char MergeFunctions::ID = 0;
146 INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false);
147
148 ModulePass *llvm::createMergeFunctionsPass() {
149   return new MergeFunctions();
150 }
151
152 namespace {
153 /// 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).
157 class FunctionComparator {
158 public:
159   FunctionComparator(const TargetData *TD, const Function *F1,
160                      const Function *F2)
161     : F1(F1), F2(F2), TD(TD), IDMap1Count(0), IDMap2Count(0) {}
162
163   /// Compare - test whether the two functions have equivalent behaviour.
164   bool Compare();
165
166 private:
167   /// Compare - test whether two basic blocks have equivalent behaviour.
168   bool Compare(const BasicBlock *BB1, const BasicBlock *BB2);
169
170   /// 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.
173   bool Enumerate(const Value *V1, const Value *V2);
174
175   /// isEquivalentOperation - Compare two Instructions for equivalence, similar
176   /// to Instruction::isSameOperationAs but with modifications to the type
177   /// comparison.
178   bool isEquivalentOperation(const Instruction *I1,
179                              const Instruction *I2) const;
180
181   /// isEquivalentGEP - Compare two GEPs for equivalent pointer arithmetic.
182   bool isEquivalentGEP(const GEPOperator *GEP1, const GEPOperator *GEP2);
183   bool isEquivalentGEP(const GetElementPtrInst *GEP1,
184                        const GetElementPtrInst *GEP2) {
185     return isEquivalentGEP(cast<GEPOperator>(GEP1), cast<GEPOperator>(GEP2));
186   }
187
188   /// isEquivalentType - Compare two Types, treating all pointer types as equal.
189   bool isEquivalentType(const Type *Ty1, const Type *Ty2) const;
190
191   // The two functions undergoing comparison.
192   const Function *F1, *F2;
193
194   const TargetData *TD;
195
196   typedef DenseMap<const Value *, unsigned long> IDMap;
197   IDMap Map1, Map2;
198   unsigned long IDMap1Count, IDMap2Count;
199 };
200 }
201
202 /// isEquivalentType - any two pointers in the same address space are
203 /// equivalent. Otherwise, standard type equivalence rules apply.
204 bool FunctionComparator::isEquivalentType(const Type *Ty1,
205                                           const Type *Ty2) const {
206   if (Ty1 == Ty2)
207     return true;
208   if (Ty1->getTypeID() != Ty2->getTypeID())
209     return false;
210
211   switch(Ty1->getTypeID()) {
212   default:
213     llvm_unreachable("Unknown type!");
214     // Fall through in Release mode.
215   case Type::IntegerTyID:
216   case Type::OpaqueTyID:
217     // Ty1 == Ty2 would have returned true earlier.
218     return false;
219
220   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
230   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
269   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   }
275
276   case Type::VectorTyID: {
277     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());
281   }
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
287 /// kept in sync with Instruction::isSameOperationAs.
288 bool FunctionComparator::isEquivalentOperation(const Instruction *I1,
289                                                const Instruction *I2) const {
290   if (I1->getOpcode() != I2->getOpcode() ||
291       I1->getNumOperands() != I2->getNumOperands() ||
292       !isEquivalentType(I1->getType(), I2->getType()) ||
293       !I1->hasSameSubclassOptionalData(I2))
294     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;
339 }
340
341 /// isEquivalentGEP - determine whether two GEP operations perform the same
342 /// underlying arithmetic.
343 bool 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.
347   if (TD && GEP1->hasAllConstantIndices() && GEP2->hasAllConstantIndices()) {
348     SmallVector<Value *, 8> Indices1(GEP1->idx_begin(), GEP1->idx_end());
349     SmallVector<Value *, 8> Indices2(GEP2->idx_begin(), GEP2->idx_end());
350     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;
355   }
356
357   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) {
365     if (!Enumerate(GEP1->getOperand(i), GEP2->getOperand(i)))
366       return false;
367   }
368
369   return true;
370 }
371
372 /// 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.
375 bool 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.
379   if (V1 == F1 && V2 == F2)
380     return true;
381   if (V1 == F2 && V2 == F1)
382     return true;
383
384   // TODO: constant expressions with GEP or references to F1 or F2.
385   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
395   unsigned long &ID1 = Map1[V1];
396   if (!ID1)
397     ID1 = ++IDMap1Count;
398
399   unsigned long &ID2 = Map2[V2];
400   if (!ID2)
401     ID2 = ++IDMap2Count;
402
403   return ID1 == ID2;
404 }
405
406 /// Compare - test whether two basic blocks have equivalent behaviour.
407 bool 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();
410
411   do {
412     if (!Enumerate(F1I, F2I))
413       return false;
414
415     if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) {
416       const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I);
417       if (!GEP2)
418         return false;
419
420       if (!Enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand()))
421         return false;
422
423       if (!isEquivalentGEP(GEP1, GEP2))
424         return false;
425     } else {
426       if (!isEquivalentOperation(F1I, F2I))
427         return false;
428
429       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);
433
434         if (!Enumerate(OpF1, OpF2))
435           return false;
436
437         if (OpF1->getValueID() != OpF2->getValueID() ||
438             !isEquivalentType(OpF1->getType(), OpF2->getType()))
439           return false;
440       }
441     }
442
443     ++F1I, ++F2I;
444   } while (F1I != F1E && F2I != F2E);
445
446   return F1I == F1E && F2I == F2E;
447 }
448
449 /// Compare - test whether the two functions have equivalent behaviour.
450 bool FunctionComparator::Compare() {
451   // We need to recheck everything, but check the things that weren't included
452   // in the hash first.
453
454   if (F1->getAttributes() != F2->getAttributes())
455     return false;
456
457   if (F1->hasGC() != F2->hasGC())
458     return false;
459
460   if (F1->hasGC() && F1->getGC() != F2->getGC())
461     return false;
462
463   if (F1->hasSection() != F2->hasSection())
464     return false;
465
466   if (F1->hasSection() && F1->getSection() != F2->getSection())
467     return false;
468
469   if (F1->isVarArg() != F2->isVarArg())
470     return false;
471
472   // TODO: if it's internal and only used in direct calls, we could handle this
473   // case too.
474   if (F1->getCallingConv() != F2->getCallingConv())
475     return false;
476
477   if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType()))
478     return false;
479
480   assert(F1->arg_size() == F2->arg_size() &&
481          "Identical functions have a different number of args.");
482
483   // Visit the arguments so that they get enumerated in the order they're
484   // passed in.
485   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))
488       llvm_unreachable("Arguments repeat");
489   }
490
491   // 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
493   // 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.
497
498   F1BBs.push_back(&F1->getEntryBlock());
499   F2BBs.push_back(&F2->getEntryBlock());
500
501   VisitedBBs.insert(F1BBs[0]);
502   while (!F1BBs.empty()) {
503     const BasicBlock *F1BB = F1BBs.pop_back_val();
504     const BasicBlock *F2BB = F2BBs.pop_back_val();
505
506     if (!Enumerate(F1BB, F2BB) || !Compare(F1BB, F2BB))
507       return false;
508
509     const TerminatorInst *F1TI = F1BB->getTerminator();
510     const TerminatorInst *F2TI = F2BB->getTerminator();
511
512     assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors());
513     for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) {
514       if (!VisitedBBs.insert(F1TI->getSuccessor(i)))
515         continue;
516
517       F1BBs.push_back(F1TI->getSuccessor(i));
518       F2BBs.push_back(F2TI->getSuccessor(i));
519     }
520   }
521   return true;
522 }
523
524 /// WriteThunk - Replace G with a simple tail call to bitcast(F). Also replace
525 /// direct uses of G with bitcast(F). Deletes G.
526 void MergeFunctions::WriteThunk(Function *F, Function *G) const {
527   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
540   // 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
547   Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
548                                     G->getParent());
549   BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
550   IRBuilder<false> Builder(BB);
551
552   SmallVector<Value *, 16> Args;
553   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) {
557     Args.push_back(Builder.CreateBitCast(AI, FFTy->getParamType(i)));
558     ++i;
559   }
560
561   CallInst *CI = Builder.CreateCall(F, Args.begin(), Args.end());
562   CI->setTailCall();
563   CI->setCallingConv(F->getCallingConv());
564   if (NewG->getReturnType()->isVoidTy()) {
565     Builder.CreateRetVoid();
566   } else {
567     Builder.CreateRet(Builder.CreateBitCast(CI, NewG->getReturnType()));
568   }
569
570   NewG->copyAttributesFrom(G);
571   NewG->takeName(G);
572   G->replaceAllUsesWith(NewG);
573   G->eraseFromParent();
574 }
575
576 /// MergeTwoFunctions - Merge two equivalent functions. Upon completion,
577 /// Function G is deleted.
578 void MergeFunctions::MergeTwoFunctions(Function *F, Function *G) const {
579   if (F->isWeakForLinker()) {
580     assert(G->isWeakForLinker());
581
582     // Make them both thunks to the same internal function.
583     Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
584                                    F->getParent());
585     H->copyAttributesFrom(F);
586     H->takeName(F);
587     F->replaceAllUsesWith(H);
588
589     unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
590
591     WriteThunk(F, G);
592     WriteThunk(F, H);
593
594     F->setAlignment(MaxAlignment);
595     F->setLinkage(GlobalValue::InternalLinkage);
596   } else {
597     WriteThunk(F, G);
598   }
599
600   ++NumFunctionsMerged;
601 }
602
603 // Insert - Insert a ComparableFunction into the FnSet, or merge it away if
604 // equal to one that's already inserted.
605 bool MergeFunctions::Insert(FnSetType &FnSet, ComparableFunction *NewF) {
606   std::pair<FnSetType::iterator, bool> Result = FnSet.insert(NewF);
607   if (Result.second)
608     return false;
609
610   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;
623 }
624
625 // 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.
627 static 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.
632
633   if (F->size() != 1)
634     return false;
635
636   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;
653   }
654
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;
671   }
672
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;
680   }
681   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 }
687
688 bool MergeFunctions::runOnModule(Module &M) {
689   bool Changed = false;
690   TD = getAnalysisIfAvailable<TargetData>();
691
692   bool LocalChanged;
693   do {
694     DEBUG(dbgs() << "size: " << M.size() << '\n');
695     LocalChanged = false;
696     FnSetType FnSet;
697
698     // 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     }
708
709     // 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);
719       }
720     }
721     DeleteContainerPointers(FnSet);
722     Changed |= LocalChanged;
723   } while (LocalChanged);
724
725   return Changed;
726 }
727
728 bool 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 }