1 //===- MergeFunctions.cpp - Merge identical functions ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass looks for equivalent functions that are mergable and folds them.
12 // A hash is computed from the function, based on its type and number of
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.
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.
24 //===----------------------------------------------------------------------===//
28 // * virtual functions.
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.
34 // * switch from n^2 pair-wise comparisons to an n-way comparison for each
37 // * be smarter about bitcasts.
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.
44 //===----------------------------------------------------------------------===//
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"
69 STATISTIC(NumFunctionsMerged, "Number of functions merged");
73 static unsigned ProfileFunction(const Function *F) {
74 const FunctionType *FTy = F->getFunctionType();
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();
87 class ComparableFunction {
89 ComparableFunction(Function *Func, TargetData *TD)
90 : Func(Func), Hash(ProfileFunction(Func)), TD(TD) {}
92 AssertingVH<Function> const Func;
94 TargetData * const TD;
97 struct MergeFunctionsEqualityInfo {
98 static ComparableFunction *getEmptyKey() {
99 return reinterpret_cast<ComparableFunction*>(0);
101 static ComparableFunction *getTombstoneKey() {
102 return reinterpret_cast<ComparableFunction*>(-1);
104 static unsigned getHashValue(const ComparableFunction *CF) {
107 static bool isEqual(const ComparableFunction *LHS,
108 const ComparableFunction *RHS);
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.
116 class MergeFunctions : public ModulePass {
119 MergeFunctions() : ModulePass(ID) {}
121 bool runOnModule(Module &M);
124 typedef DenseSet<ComparableFunction *, MergeFunctionsEqualityInfo> FnSetType;
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);
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;
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;
143 } // end anonymous namespace
145 char MergeFunctions::ID = 0;
146 INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false);
148 ModulePass *llvm::createMergeFunctionsPass() {
149 return new MergeFunctions();
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 {
159 FunctionComparator(const TargetData *TD, const Function *F1,
161 : F1(F1), F2(F2), TD(TD), IDMap1Count(0), IDMap2Count(0) {}
163 /// Compare - test whether the two functions have equivalent behaviour.
167 /// Compare - test whether two basic blocks have equivalent behaviour.
168 bool Compare(const BasicBlock *BB1, const BasicBlock *BB2);
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);
175 /// isEquivalentOperation - Compare two Instructions for equivalence, similar
176 /// to Instruction::isSameOperationAs but with modifications to the type
178 bool isEquivalentOperation(const Instruction *I1,
179 const Instruction *I2) const;
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));
188 /// isEquivalentType - Compare two Types, treating all pointer types as equal.
189 bool isEquivalentType(const Type *Ty1, const Type *Ty2) const;
191 // The two functions undergoing comparison.
192 const Function *F1, *F2;
194 const TargetData *TD;
196 typedef DenseMap<const Value *, unsigned long> IDMap;
198 unsigned long IDMap1Count, IDMap2Count;
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 {
208 if (Ty1->getTypeID() != Ty2->getTypeID())
211 switch(Ty1->getTypeID()) {
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.
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:
230 case Type::PointerTyID: {
231 const PointerType *PTy1 = cast<PointerType>(Ty1);
232 const PointerType *PTy2 = cast<PointerType>(Ty2);
233 return PTy1->getAddressSpace() == PTy2->getAddressSpace();
236 case Type::StructTyID: {
237 const StructType *STy1 = cast<StructType>(Ty1);
238 const StructType *STy2 = cast<StructType>(Ty2);
239 if (STy1->getNumElements() != STy2->getNumElements())
242 if (STy1->isPacked() != STy2->isPacked())
245 for (unsigned i = 0, e = STy1->getNumElements(); i != e; ++i) {
246 if (!isEquivalentType(STy1->getElementType(i), STy2->getElementType(i)))
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())
259 if (!isEquivalentType(FTy1->getReturnType(), FTy2->getReturnType()))
262 for (unsigned i = 0, e = FTy1->getNumParams(); i != e; ++i) {
263 if (!isEquivalentType(FTy1->getParamType(i), FTy2->getParamType(i)))
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());
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());
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))
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()))
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())
324 for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
325 if (IVI->idx_begin()[i] != cast<InsertValueInst>(I2)->idx_begin()[i])
329 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) {
330 if (EVI->getNumIndices() != cast<ExtractValueInst>(I2)->getNumIndices())
332 for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
333 if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I2)->idx_begin()[i])
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;
357 if (GEP1->getPointerOperand()->getType() !=
358 GEP2->getPointerOperand()->getType())
361 if (GEP1->getNumOperands() != GEP2->getNumOperands())
364 for (unsigned i = 0, e = GEP1->getNumOperands(); i != e; ++i) {
365 if (!Enumerate(GEP1->getOperand(i), GEP2->getOperand(i)))
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)
381 if (V1 == F2 && V2 == F1)
384 // TODO: constant expressions with GEP or references to F1 or F2.
385 if (isa<Constant>(V1))
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();
395 unsigned long &ID1 = Map1[V1];
399 unsigned long &ID2 = Map2[V2];
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();
412 if (!Enumerate(F1I, F2I))
415 if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) {
416 const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I);
420 if (!Enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand()))
423 if (!isEquivalentGEP(GEP1, GEP2))
426 if (!isEquivalentOperation(F1I, F2I))
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);
434 if (!Enumerate(OpF1, OpF2))
437 if (OpF1->getValueID() != OpF2->getValueID() ||
438 !isEquivalentType(OpF1->getType(), OpF2->getType()))
444 } while (F1I != F1E && F2I != F2E);
446 return F1I == F1E && F2I == F2E;
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.
454 if (F1->getAttributes() != F2->getAttributes())
457 if (F1->hasGC() != F2->hasGC())
460 if (F1->hasGC() && F1->getGC() != F2->getGC())
463 if (F1->hasSection() != F2->hasSection())
466 if (F1->hasSection() && F1->getSection() != F2->getSection())
469 if (F1->isVarArg() != F2->isVarArg())
472 // TODO: if it's internal and only used in direct calls, we could handle this
474 if (F1->getCallingConv() != F2->getCallingConv())
477 if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType()))
480 assert(F1->arg_size() == F2->arg_size() &&
481 "Identical functions have a different number of args.");
483 // Visit the arguments so that they get enumerated in the order they're
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");
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.
498 F1BBs.push_back(&F1->getEntryBlock());
499 F2BBs.push_back(&F2->getEntryBlock());
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();
506 if (!Enumerate(F1BB, F2BB) || !Compare(F1BB, F2BB))
509 const TerminatorInst *F1TI = F1BB->getTerminator();
510 const TerminatorInst *F2TI = F2BB->getTerminator();
512 assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors());
513 for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) {
514 if (!VisitedBBs.insert(F1TI->getSuccessor(i)))
517 F1BBs.push_back(F1TI->getSuccessor(i));
518 F2BBs.push_back(F2TI->getSuccessor(i));
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();
532 Value::use_iterator TheIter = UI;
534 CallSite CS(*TheIter);
535 if (CS && CS.isCallee(TheIter))
536 TheIter.getUse().set(BitcastF);
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();
547 Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
549 BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
550 IRBuilder<false> Builder(BB);
552 SmallVector<Value *, 16> Args;
554 const FunctionType *FFTy = F->getFunctionType();
555 for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end();
557 Args.push_back(Builder.CreateBitCast(AI, FFTy->getParamType(i)));
561 CallInst *CI = Builder.CreateCall(F, Args.begin(), Args.end());
563 CI->setCallingConv(F->getCallingConv());
564 if (NewG->getReturnType()->isVoidTy()) {
565 Builder.CreateRetVoid();
567 Builder.CreateRet(Builder.CreateBitCast(CI, NewG->getReturnType()));
570 NewG->copyAttributesFrom(G);
572 G->replaceAllUsesWith(NewG);
573 G->eraseFromParent();
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());
582 // Make them both thunks to the same internal function.
583 Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
585 H->copyAttributesFrom(F);
587 F->replaceAllUsesWith(H);
589 unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
594 F->setAlignment(MaxAlignment);
595 F->setLinkage(GlobalValue::InternalLinkage);
600 ++NumFunctionsMerged;
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);
610 ComparableFunction *OldF = *Result.first;
611 assert(OldF && "Expected a hash collision");
613 // Never thunk a strong function to a weak function.
614 assert(!OldF->Func->isWeakForLinker() || NewF->Func->isWeakForLinker());
616 DEBUG(dbgs() << " " << OldF->Func->getName() << " == "
617 << NewF->Func->getName() << '\n');
619 Function *DeleteF = NewF->Func;
621 MergeTwoFunctions(OldF->Func, DeleteF);
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
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.
636 const BasicBlock *BB = &F->front();
639 // optional-reg tail call @thunkee(args...*)
640 // ret void|optional-reg
641 // where the args are in the same order as the arguments.
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).
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();
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
658 const CallInst *CI = dyn_cast<CallInst>(I++);
659 if (!CI || !CI->isTailCall() ||
660 CI->getNumArgOperands() != F->arg_size()) return false;
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);
666 const BitCastInst *BCI = dyn_cast<BitCastInst>(V);
667 if (!BCI) return false;
668 A = cast<Argument>(BCI->getOperand(0));
670 if (A->getArgNo() != i) return false;
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)) {
678 if (BCI->getOperand(0) != CI) return false;
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;
688 bool MergeFunctions::runOnModule(Module &M) {
689 bool Changed = false;
690 TD = getAnalysisIfAvailable<TargetData>();
694 DEBUG(dbgs() << "size: " << M.size() << '\n');
695 LocalChanged = false;
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;) {
702 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
703 !F->isWeakForLinker() && !IsThunk(F)) {
704 ComparableFunction *CF = new ComparableFunction(F, TD);
705 LocalChanged |= Insert(FnSet, CF);
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;) {
715 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
716 F->isWeakForLinker() && !IsThunk(F)) {
717 ComparableFunction *CF = new ComparableFunction(F, TD);
718 LocalChanged |= Insert(FnSet, CF);
721 DeleteContainerPointers(FnSet);
722 Changed |= LocalChanged;
723 } while (LocalChanged);
728 bool MergeFunctionsEqualityInfo::isEqual(const ComparableFunction *LHS,
729 const ComparableFunction *RHS) {
732 if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||
733 RHS == getEmptyKey() || RHS == getTombstoneKey())
735 assert(LHS->TD == RHS->TD && "Comparing functions for different targets");
736 return FunctionComparator(LHS->TD, LHS->Func, RHS->Func).Compare();