1 //===- MergeFunctions.cpp - Merge identical functions ---------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass looks for equivalent functions that are mergable and folds them.
11 // Order relation is defined on set of functions. It was made through
12 // special function comparison procedure that returns
13 // 0 when functions are equal,
14 // -1 when Left function is less than right function, and
15 // 1 for opposite case. We need total-ordering, so we need to maintain
16 // four properties on the functions set:
17 // a <= a (reflexivity)
18 // if a <= b and b <= a then a = b (antisymmetry)
19 // if a <= b and b <= c then a <= c (transitivity).
20 // for all a and b: a <= b or b <= a (totality).
22 // Comparison iterates through each instruction in each basic block.
23 // Functions are kept on binary tree. For each new function F we perform
24 // lookup in binary tree.
25 // In practice it works the following way:
26 // -- We define Function* container class with custom "operator<" (FunctionPtr).
27 // -- "FunctionPtr" instances are stored in std::set collection, so every
28 // std::set::insert operation will give you result in log(N) time.
30 // As an optimization, a hash of the function structure is calculated first, and
31 // two functions are only compared if they have the same hash. This hash is
32 // cheap to compute, and has the property that if function F == G according to
33 // the comparison function, then hash(F) == hash(G). This consistency property
34 // is critical to ensuring all possible merging opportunities are exploited.
35 // Collisions in the hash affect the speed of the pass but not the correctness
36 // or determinism of the resulting transformation.
38 // When a match is found the functions are folded. If both functions are
39 // overridable, we move the functionality into a new internal function and
40 // leave two overridable thunks to it.
42 //===----------------------------------------------------------------------===//
46 // * virtual functions.
48 // Many functions have their address taken by the virtual function table for
49 // the object they belong to. However, as long as it's only used for a lookup
50 // and call, this is irrelevant, and we'd like to fold such functions.
52 // * be smarter about bitcasts.
54 // In order to fold functions, we will sometimes add either bitcast instructions
55 // or bitcast constant expressions. Unfortunately, this can confound further
56 // analysis since the two functions differ where one has a bitcast and the
57 // other doesn't. We should learn to look through bitcasts.
59 // * Compare complex types with pointer types inside.
60 // * Compare cross-reference cases.
61 // * Compare complex expressions.
63 // All the three issues above could be described as ability to prove that
64 // fA == fB == fC == fE == fF == fG in example below:
83 // Simplest cross-reference case (fA <--> fB) was implemented in previous
84 // versions of MergeFunctions, though it presented only in two function pairs
85 // in test-suite (that counts >50k functions)
86 // Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A)
87 // could cover much more cases.
89 //===----------------------------------------------------------------------===//
91 #include "llvm/ADT/ArrayRef.h"
92 #include "llvm/ADT/SmallPtrSet.h"
93 #include "llvm/ADT/SmallVector.h"
94 #include "llvm/ADT/Statistic.h"
95 #include "llvm/IR/Argument.h"
96 #include "llvm/IR/Attributes.h"
97 #include "llvm/IR/BasicBlock.h"
98 #include "llvm/IR/CallSite.h"
99 #include "llvm/IR/Constant.h"
100 #include "llvm/IR/Constants.h"
101 #include "llvm/IR/DebugInfoMetadata.h"
102 #include "llvm/IR/DebugLoc.h"
103 #include "llvm/IR/DerivedTypes.h"
104 #include "llvm/IR/Function.h"
105 #include "llvm/IR/GlobalValue.h"
106 #include "llvm/IR/IRBuilder.h"
107 #include "llvm/IR/InstrTypes.h"
108 #include "llvm/IR/Instruction.h"
109 #include "llvm/IR/Instructions.h"
110 #include "llvm/IR/IntrinsicInst.h"
111 #include "llvm/IR/Module.h"
112 #include "llvm/IR/Type.h"
113 #include "llvm/IR/Use.h"
114 #include "llvm/IR/User.h"
115 #include "llvm/IR/Value.h"
116 #include "llvm/IR/ValueHandle.h"
117 #include "llvm/IR/ValueMap.h"
118 #include "llvm/Pass.h"
119 #include "llvm/Support/Casting.h"
120 #include "llvm/Support/CommandLine.h"
121 #include "llvm/Support/Debug.h"
122 #include "llvm/Support/raw_ostream.h"
123 #include "llvm/Transforms/IPO.h"
124 #include "llvm/Transforms/Utils/FunctionComparator.h"
132 using namespace llvm
;
134 #define DEBUG_TYPE "mergefunc"
136 STATISTIC(NumFunctionsMerged
, "Number of functions merged");
137 STATISTIC(NumThunksWritten
, "Number of thunks generated");
138 STATISTIC(NumAliasesWritten
, "Number of aliases generated");
139 STATISTIC(NumDoubleWeak
, "Number of new functions created");
141 static cl::opt
<unsigned> NumFunctionsForSanityCheck(
143 cl::desc("How many functions in module could be used for "
144 "MergeFunctions pass sanity check. "
145 "'0' disables this check. Works only with '-debug' key."),
146 cl::init(0), cl::Hidden
);
148 // Under option -mergefunc-preserve-debug-info we:
149 // - Do not create a new function for a thunk.
150 // - Retain the debug info for a thunk's parameters (and associated
151 // instructions for the debug info) from the entry block.
152 // Note: -debug will display the algorithm at work.
153 // - Create debug-info for the call (to the shared implementation) made by
154 // a thunk and its return value.
155 // - Erase the rest of the function, retaining the (minimally sized) entry
156 // block to create a thunk.
157 // - Preserve a thunk's call site to point to the thunk even when both occur
158 // within the same translation unit, to aid debugability. Note that this
159 // behaviour differs from the underlying -mergefunc implementation which
160 // modifies the thunk's call site to point to the shared implementation
161 // when both occur within the same translation unit.
163 MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden
,
165 cl::desc("Preserve debug info in thunk when mergefunc "
166 "transformations are made."));
169 MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden
,
171 cl::desc("Allow mergefunc to create aliases"));
176 mutable AssertingVH
<Function
> F
;
177 FunctionComparator::FunctionHash Hash
;
180 // Note the hash is recalculated potentially multiple times, but it is cheap.
181 FunctionNode(Function
*F
)
182 : F(F
), Hash(FunctionComparator::functionHash(*F
)) {}
184 Function
*getFunc() const { return F
; }
185 FunctionComparator::FunctionHash
getHash() const { return Hash
; }
187 /// Replace the reference to the function F by the function G, assuming their
188 /// implementations are equal.
189 void replaceBy(Function
*G
) const {
193 void release() { F
= nullptr; }
196 /// MergeFunctions finds functions which will generate identical machine code,
197 /// by considering all pointer types to be equivalent. Once identified,
198 /// MergeFunctions will fold them by replacing a call to one to a call to a
199 /// bitcast of the other.
200 class MergeFunctions
: public ModulePass
{
205 : ModulePass(ID
), FnTree(FunctionNodeCmp(&GlobalNumbers
)) {
206 initializeMergeFunctionsPass(*PassRegistry::getPassRegistry());
209 bool runOnModule(Module
&M
) override
;
212 // The function comparison operator is provided here so that FunctionNodes do
213 // not need to become larger with another pointer.
214 class FunctionNodeCmp
{
215 GlobalNumberState
* GlobalNumbers
;
218 FunctionNodeCmp(GlobalNumberState
* GN
) : GlobalNumbers(GN
) {}
220 bool operator()(const FunctionNode
&LHS
, const FunctionNode
&RHS
) const {
221 // Order first by hashes, then full function comparison.
222 if (LHS
.getHash() != RHS
.getHash())
223 return LHS
.getHash() < RHS
.getHash();
224 FunctionComparator
FCmp(LHS
.getFunc(), RHS
.getFunc(), GlobalNumbers
);
225 return FCmp
.compare() == -1;
228 using FnTreeType
= std::set
<FunctionNode
, FunctionNodeCmp
>;
230 GlobalNumberState GlobalNumbers
;
232 /// A work queue of functions that may have been modified and should be
234 std::vector
<WeakTrackingVH
> Deferred
;
237 /// Checks the rules of order relation introduced among functions set.
238 /// Returns true, if sanity check has been passed, and false if failed.
239 bool doSanityCheck(std::vector
<WeakTrackingVH
> &Worklist
);
242 /// Insert a ComparableFunction into the FnTree, or merge it away if it's
243 /// equal to one that's already present.
244 bool insert(Function
*NewFunction
);
246 /// Remove a Function from the FnTree and queue it up for a second sweep of
248 void remove(Function
*F
);
250 /// Find the functions that use this Value and remove them from FnTree and
251 /// queue the functions.
252 void removeUsers(Value
*V
);
254 /// Replace all direct calls of Old with calls of New. Will bitcast New if
255 /// necessary to make types match.
256 void replaceDirectCallers(Function
*Old
, Function
*New
);
258 /// Merge two equivalent functions. Upon completion, G may be deleted, or may
259 /// be converted into a thunk. In either case, it should never be visited
261 void mergeTwoFunctions(Function
*F
, Function
*G
);
263 /// Fill PDIUnrelatedWL with instructions from the entry block that are
264 /// unrelated to parameter related debug info.
265 void filterInstsUnrelatedToPDI(BasicBlock
*GEntryBlock
,
266 std::vector
<Instruction
*> &PDIUnrelatedWL
);
268 /// Erase the rest of the CFG (i.e. barring the entry block).
269 void eraseTail(Function
*G
);
271 /// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
272 /// parameter debug info, from the entry block.
273 void eraseInstsUnrelatedToPDI(std::vector
<Instruction
*> &PDIUnrelatedWL
);
275 /// Replace G with a simple tail call to bitcast(F). Also (unless
276 /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
278 void writeThunk(Function
*F
, Function
*G
);
280 // Replace G with an alias to F (deleting function G)
281 void writeAlias(Function
*F
, Function
*G
);
283 // Replace G with an alias to F if possible, or a thunk to F if possible.
284 // Returns false if neither is the case.
285 bool writeThunkOrAlias(Function
*F
, Function
*G
);
287 /// Replace function F with function G in the function tree.
288 void replaceFunctionInTree(const FunctionNode
&FN
, Function
*G
);
290 /// The set of all distinct functions. Use the insert() and remove() methods
291 /// to modify it. The map allows efficient lookup and deferring of Functions.
294 // Map functions to the iterators of the FunctionNode which contains them
295 // in the FnTree. This must be updated carefully whenever the FnTree is
296 // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid
297 // dangling iterators into FnTree. The invariant that preserves this is that
298 // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
299 DenseMap
<AssertingVH
<Function
>, FnTreeType::iterator
> FNodesInTree
;
302 } // end anonymous namespace
304 char MergeFunctions::ID
= 0;
306 INITIALIZE_PASS(MergeFunctions
, "mergefunc", "Merge Functions", false, false)
308 ModulePass
*llvm::createMergeFunctionsPass() {
309 return new MergeFunctions();
313 bool MergeFunctions::doSanityCheck(std::vector
<WeakTrackingVH
> &Worklist
) {
314 if (const unsigned Max
= NumFunctionsForSanityCheck
) {
315 unsigned TripleNumber
= 0;
318 dbgs() << "MERGEFUNC-SANITY: Started for first " << Max
<< " functions.\n";
321 for (std::vector
<WeakTrackingVH
>::iterator I
= Worklist
.begin(),
323 I
!= E
&& i
< Max
; ++I
, ++i
) {
325 for (std::vector
<WeakTrackingVH
>::iterator J
= I
; J
!= E
&& j
< Max
;
327 Function
*F1
= cast
<Function
>(*I
);
328 Function
*F2
= cast
<Function
>(*J
);
329 int Res1
= FunctionComparator(F1
, F2
, &GlobalNumbers
).compare();
330 int Res2
= FunctionComparator(F2
, F1
, &GlobalNumbers
).compare();
332 // If F1 <= F2, then F2 >= F1, otherwise report failure.
334 dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber
336 dbgs() << *F1
<< '\n' << *F2
<< '\n';
344 for (std::vector
<WeakTrackingVH
>::iterator K
= J
; K
!= E
&& k
< Max
;
345 ++k
, ++K
, ++TripleNumber
) {
349 Function
*F3
= cast
<Function
>(*K
);
350 int Res3
= FunctionComparator(F1
, F3
, &GlobalNumbers
).compare();
351 int Res4
= FunctionComparator(F2
, F3
, &GlobalNumbers
).compare();
353 bool Transitive
= true;
355 if (Res1
!= 0 && Res1
== Res4
) {
356 // F1 > F2, F2 > F3 => F1 > F3
357 Transitive
= Res3
== Res1
;
358 } else if (Res3
!= 0 && Res3
== -Res4
) {
359 // F1 > F3, F3 > F2 => F1 > F2
360 Transitive
= Res3
== Res1
;
361 } else if (Res4
!= 0 && -Res3
== Res4
) {
362 // F2 > F3, F3 > F1 => F2 > F1
363 Transitive
= Res4
== -Res1
;
367 dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: "
368 << TripleNumber
<< "\n";
369 dbgs() << "Res1, Res3, Res4: " << Res1
<< ", " << Res3
<< ", "
371 dbgs() << *F1
<< '\n' << *F2
<< '\n' << *F3
<< '\n';
378 dbgs() << "MERGEFUNC-SANITY: " << (Valid
? "Passed." : "Failed.") << "\n";
385 /// Check whether \p F is eligible for function merging.
386 static bool isEligibleForMerging(Function
&F
) {
387 return !F
.isDeclaration() && !F
.hasAvailableExternallyLinkage();
390 bool MergeFunctions::runOnModule(Module
&M
) {
394 bool Changed
= false;
396 // All functions in the module, ordered by hash. Functions with a unique
397 // hash value are easily eliminated.
398 std::vector
<std::pair
<FunctionComparator::FunctionHash
, Function
*>>
400 for (Function
&Func
: M
) {
401 if (isEligibleForMerging(Func
)) {
402 HashedFuncs
.push_back({FunctionComparator::functionHash(Func
), &Func
});
407 HashedFuncs
.begin(), HashedFuncs
.end(),
408 [](const std::pair
<FunctionComparator::FunctionHash
, Function
*> &a
,
409 const std::pair
<FunctionComparator::FunctionHash
, Function
*> &b
) {
410 return a
.first
< b
.first
;
413 auto S
= HashedFuncs
.begin();
414 for (auto I
= HashedFuncs
.begin(), IE
= HashedFuncs
.end(); I
!= IE
; ++I
) {
415 // If the hash value matches the previous value or the next one, we must
416 // consider merging it. Otherwise it is dropped and never considered again.
417 if ((I
!= S
&& std::prev(I
)->first
== I
->first
) ||
418 (std::next(I
) != IE
&& std::next(I
)->first
== I
->first
) ) {
419 Deferred
.push_back(WeakTrackingVH(I
->second
));
424 std::vector
<WeakTrackingVH
> Worklist
;
425 Deferred
.swap(Worklist
);
427 LLVM_DEBUG(doSanityCheck(Worklist
));
429 LLVM_DEBUG(dbgs() << "size of module: " << M
.size() << '\n');
430 LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist
.size() << '\n');
432 // Insert functions and merge them.
433 for (WeakTrackingVH
&I
: Worklist
) {
436 Function
*F
= cast
<Function
>(I
);
437 if (!F
->isDeclaration() && !F
->hasAvailableExternallyLinkage()) {
438 Changed
|= insert(F
);
441 LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree
.size() << '\n');
442 } while (!Deferred
.empty());
445 FNodesInTree
.clear();
446 GlobalNumbers
.clear();
451 // Replace direct callers of Old with New.
452 void MergeFunctions::replaceDirectCallers(Function
*Old
, Function
*New
) {
453 Constant
*BitcastNew
= ConstantExpr::getBitCast(New
, Old
->getType());
454 for (auto UI
= Old
->use_begin(), UE
= Old
->use_end(); UI
!= UE
;) {
457 CallSite
CS(U
->getUser());
458 if (CS
&& CS
.isCallee(U
)) {
459 // Transfer the called function's attributes to the call site. Due to the
460 // bitcast we will 'lose' ABI changing attributes because the 'called
461 // function' is no longer a Function* but the bitcast. Code that looks up
462 // the attributes from the called function will fail.
464 // FIXME: This is not actually true, at least not anymore. The callsite
465 // will always have the same ABI affecting attributes as the callee,
466 // because otherwise the original input has UB. Note that Old and New
467 // always have matching ABI, so no attributes need to be changed.
468 // Transferring other attributes may help other optimizations, but that
469 // should be done uniformly and not in this ad-hoc way.
470 auto &Context
= New
->getContext();
471 auto NewPAL
= New
->getAttributes();
472 SmallVector
<AttributeSet
, 4> NewArgAttrs
;
473 for (unsigned argIdx
= 0; argIdx
< CS
.arg_size(); argIdx
++)
474 NewArgAttrs
.push_back(NewPAL
.getParamAttributes(argIdx
));
475 // Don't transfer attributes from the function to the callee. Function
476 // attributes typically aren't relevant to the calling convention or ABI.
477 CS
.setAttributes(AttributeList::get(Context
, /*FnAttrs=*/AttributeSet(),
478 NewPAL
.getRetAttributes(),
481 remove(CS
.getInstruction()->getFunction());
487 // Helper for writeThunk,
488 // Selects proper bitcast operation,
489 // but a bit simpler then CastInst::getCastOpcode.
490 static Value
*createCast(IRBuilder
<> &Builder
, Value
*V
, Type
*DestTy
) {
491 Type
*SrcTy
= V
->getType();
492 if (SrcTy
->isStructTy()) {
493 assert(DestTy
->isStructTy());
494 assert(SrcTy
->getStructNumElements() == DestTy
->getStructNumElements());
495 Value
*Result
= UndefValue::get(DestTy
);
496 for (unsigned int I
= 0, E
= SrcTy
->getStructNumElements(); I
< E
; ++I
) {
497 Value
*Element
= createCast(
498 Builder
, Builder
.CreateExtractValue(V
, makeArrayRef(I
)),
499 DestTy
->getStructElementType(I
));
502 Builder
.CreateInsertValue(Result
, Element
, makeArrayRef(I
));
506 assert(!DestTy
->isStructTy());
507 if (SrcTy
->isIntegerTy() && DestTy
->isPointerTy())
508 return Builder
.CreateIntToPtr(V
, DestTy
);
509 else if (SrcTy
->isPointerTy() && DestTy
->isIntegerTy())
510 return Builder
.CreatePtrToInt(V
, DestTy
);
512 return Builder
.CreateBitCast(V
, DestTy
);
515 // Erase the instructions in PDIUnrelatedWL as they are unrelated to the
516 // parameter debug info, from the entry block.
517 void MergeFunctions::eraseInstsUnrelatedToPDI(
518 std::vector
<Instruction
*> &PDIUnrelatedWL
) {
520 dbgs() << " Erasing instructions (in reverse order of appearance in "
521 "entry block) unrelated to parameter debug info from entry "
523 while (!PDIUnrelatedWL
.empty()) {
524 Instruction
*I
= PDIUnrelatedWL
.back();
525 LLVM_DEBUG(dbgs() << " Deleting Instruction: ");
526 LLVM_DEBUG(I
->print(dbgs()));
527 LLVM_DEBUG(dbgs() << "\n");
528 I
->eraseFromParent();
529 PDIUnrelatedWL
.pop_back();
531 LLVM_DEBUG(dbgs() << " } // Done erasing instructions unrelated to parameter "
532 "debug info from entry block. \n");
535 // Reduce G to its entry block.
536 void MergeFunctions::eraseTail(Function
*G
) {
537 std::vector
<BasicBlock
*> WorklistBB
;
538 for (Function::iterator BBI
= std::next(G
->begin()), BBE
= G
->end();
540 BBI
->dropAllReferences();
541 WorklistBB
.push_back(&*BBI
);
543 while (!WorklistBB
.empty()) {
544 BasicBlock
*BB
= WorklistBB
.back();
545 BB
->eraseFromParent();
546 WorklistBB
.pop_back();
550 // We are interested in the following instructions from the entry block as being
551 // related to parameter debug info:
552 // - @llvm.dbg.declare
553 // - stores from the incoming parameters to locations on the stack-frame
554 // - allocas that create these locations on the stack-frame
556 // - the entry block's terminator
557 // The rest are unrelated to debug info for the parameters; fill up
558 // PDIUnrelatedWL with such instructions.
559 void MergeFunctions::filterInstsUnrelatedToPDI(
560 BasicBlock
*GEntryBlock
, std::vector
<Instruction
*> &PDIUnrelatedWL
) {
561 std::set
<Instruction
*> PDIRelated
;
562 for (BasicBlock::iterator BI
= GEntryBlock
->begin(), BIE
= GEntryBlock
->end();
564 if (auto *DVI
= dyn_cast
<DbgValueInst
>(&*BI
)) {
565 LLVM_DEBUG(dbgs() << " Deciding: ");
566 LLVM_DEBUG(BI
->print(dbgs()));
567 LLVM_DEBUG(dbgs() << "\n");
568 DILocalVariable
*DILocVar
= DVI
->getVariable();
569 if (DILocVar
->isParameter()) {
570 LLVM_DEBUG(dbgs() << " Include (parameter): ");
571 LLVM_DEBUG(BI
->print(dbgs()));
572 LLVM_DEBUG(dbgs() << "\n");
573 PDIRelated
.insert(&*BI
);
575 LLVM_DEBUG(dbgs() << " Delete (!parameter): ");
576 LLVM_DEBUG(BI
->print(dbgs()));
577 LLVM_DEBUG(dbgs() << "\n");
579 } else if (auto *DDI
= dyn_cast
<DbgDeclareInst
>(&*BI
)) {
580 LLVM_DEBUG(dbgs() << " Deciding: ");
581 LLVM_DEBUG(BI
->print(dbgs()));
582 LLVM_DEBUG(dbgs() << "\n");
583 DILocalVariable
*DILocVar
= DDI
->getVariable();
584 if (DILocVar
->isParameter()) {
585 LLVM_DEBUG(dbgs() << " Parameter: ");
586 LLVM_DEBUG(DILocVar
->print(dbgs()));
587 AllocaInst
*AI
= dyn_cast_or_null
<AllocaInst
>(DDI
->getAddress());
589 LLVM_DEBUG(dbgs() << " Processing alloca users: ");
590 LLVM_DEBUG(dbgs() << "\n");
591 for (User
*U
: AI
->users()) {
592 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(U
)) {
593 if (Value
*Arg
= SI
->getValueOperand()) {
594 if (dyn_cast
<Argument
>(Arg
)) {
595 LLVM_DEBUG(dbgs() << " Include: ");
596 LLVM_DEBUG(AI
->print(dbgs()));
597 LLVM_DEBUG(dbgs() << "\n");
598 PDIRelated
.insert(AI
);
599 LLVM_DEBUG(dbgs() << " Include (parameter): ");
600 LLVM_DEBUG(SI
->print(dbgs()));
601 LLVM_DEBUG(dbgs() << "\n");
602 PDIRelated
.insert(SI
);
603 LLVM_DEBUG(dbgs() << " Include: ");
604 LLVM_DEBUG(BI
->print(dbgs()));
605 LLVM_DEBUG(dbgs() << "\n");
606 PDIRelated
.insert(&*BI
);
608 LLVM_DEBUG(dbgs() << " Delete (!parameter): ");
609 LLVM_DEBUG(SI
->print(dbgs()));
610 LLVM_DEBUG(dbgs() << "\n");
614 LLVM_DEBUG(dbgs() << " Defer: ");
615 LLVM_DEBUG(U
->print(dbgs()));
616 LLVM_DEBUG(dbgs() << "\n");
620 LLVM_DEBUG(dbgs() << " Delete (alloca NULL): ");
621 LLVM_DEBUG(BI
->print(dbgs()));
622 LLVM_DEBUG(dbgs() << "\n");
625 LLVM_DEBUG(dbgs() << " Delete (!parameter): ");
626 LLVM_DEBUG(BI
->print(dbgs()));
627 LLVM_DEBUG(dbgs() << "\n");
629 } else if (BI
->isTerminator() && &*BI
== GEntryBlock
->getTerminator()) {
630 LLVM_DEBUG(dbgs() << " Will Include Terminator: ");
631 LLVM_DEBUG(BI
->print(dbgs()));
632 LLVM_DEBUG(dbgs() << "\n");
633 PDIRelated
.insert(&*BI
);
635 LLVM_DEBUG(dbgs() << " Defer: ");
636 LLVM_DEBUG(BI
->print(dbgs()));
637 LLVM_DEBUG(dbgs() << "\n");
642 << " Report parameter debug info related/related instructions: {\n");
643 for (BasicBlock::iterator BI
= GEntryBlock
->begin(), BE
= GEntryBlock
->end();
646 Instruction
*I
= &*BI
;
647 if (PDIRelated
.find(I
) == PDIRelated
.end()) {
648 LLVM_DEBUG(dbgs() << " !PDIRelated: ");
649 LLVM_DEBUG(I
->print(dbgs()));
650 LLVM_DEBUG(dbgs() << "\n");
651 PDIUnrelatedWL
.push_back(I
);
653 LLVM_DEBUG(dbgs() << " PDIRelated: ");
654 LLVM_DEBUG(I
->print(dbgs()));
655 LLVM_DEBUG(dbgs() << "\n");
658 LLVM_DEBUG(dbgs() << " }\n");
661 /// Whether this function may be replaced by a forwarding thunk.
662 static bool canCreateThunkFor(Function
*F
) {
666 // Don't merge tiny functions using a thunk, since it can just end up
667 // making the function larger.
668 if (F
->size() == 1) {
669 if (F
->front().size() <= 2) {
670 LLVM_DEBUG(dbgs() << "canCreateThunkFor: " << F
->getName()
671 << " is too small to bother creating a thunk for\n");
678 // Replace G with a simple tail call to bitcast(F). Also (unless
679 // MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
680 // delete G. Under MergeFunctionsPDI, we use G itself for creating
681 // the thunk as we preserve the debug info (and associated instructions)
682 // from G's entry block pertaining to G's incoming arguments which are
683 // passed on as corresponding arguments in the call that G makes to F.
684 // For better debugability, under MergeFunctionsPDI, we do not modify G's
685 // call sites to point to F even when within the same translation unit.
686 void MergeFunctions::writeThunk(Function
*F
, Function
*G
) {
687 BasicBlock
*GEntryBlock
= nullptr;
688 std::vector
<Instruction
*> PDIUnrelatedWL
;
689 BasicBlock
*BB
= nullptr;
690 Function
*NewG
= nullptr;
691 if (MergeFunctionsPDI
) {
692 LLVM_DEBUG(dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new "
693 "function as thunk; retain original: "
694 << G
->getName() << "()\n");
695 GEntryBlock
= &G
->getEntryBlock();
697 dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "
699 << G
->getName() << "() {\n");
700 filterInstsUnrelatedToPDI(GEntryBlock
, PDIUnrelatedWL
);
701 GEntryBlock
->getTerminator()->eraseFromParent();
704 NewG
= Function::Create(G
->getFunctionType(), G
->getLinkage(),
705 G
->getAddressSpace(), "", G
->getParent());
706 BB
= BasicBlock::Create(F
->getContext(), "", NewG
);
709 IRBuilder
<> Builder(BB
);
710 Function
*H
= MergeFunctionsPDI
? G
: NewG
;
711 SmallVector
<Value
*, 16> Args
;
713 FunctionType
*FFTy
= F
->getFunctionType();
714 for (Argument
&AI
: H
->args()) {
715 Args
.push_back(createCast(Builder
, &AI
, FFTy
->getParamType(i
)));
719 CallInst
*CI
= Builder
.CreateCall(F
, Args
);
720 ReturnInst
*RI
= nullptr;
722 CI
->setCallingConv(F
->getCallingConv());
723 CI
->setAttributes(F
->getAttributes());
724 if (H
->getReturnType()->isVoidTy()) {
725 RI
= Builder
.CreateRetVoid();
727 RI
= Builder
.CreateRet(createCast(Builder
, CI
, H
->getReturnType()));
730 if (MergeFunctionsPDI
) {
731 DISubprogram
*DIS
= G
->getSubprogram();
733 DebugLoc CIDbgLoc
= DebugLoc::get(DIS
->getScopeLine(), 0, DIS
);
734 DebugLoc RIDbgLoc
= DebugLoc::get(DIS
->getScopeLine(), 0, DIS
);
735 CI
->setDebugLoc(CIDbgLoc
);
736 RI
->setDebugLoc(RIDbgLoc
);
739 dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for "
740 << G
->getName() << "()\n");
743 eraseInstsUnrelatedToPDI(PDIUnrelatedWL
);
745 dbgs() << "} // End of parameter related debug info filtering for: "
746 << G
->getName() << "()\n");
748 NewG
->copyAttributesFrom(G
);
751 G
->replaceAllUsesWith(NewG
);
752 G
->eraseFromParent();
755 LLVM_DEBUG(dbgs() << "writeThunk: " << H
->getName() << '\n');
759 // Whether this function may be replaced by an alias
760 static bool canCreateAliasFor(Function
*F
) {
761 if (!MergeFunctionsAliases
|| !F
->hasGlobalUnnamedAddr())
764 // We should only see linkages supported by aliases here
765 assert(F
->hasLocalLinkage() || F
->hasExternalLinkage()
766 || F
->hasWeakLinkage() || F
->hasLinkOnceLinkage());
770 // Replace G with an alias to F (deleting function G)
771 void MergeFunctions::writeAlias(Function
*F
, Function
*G
) {
772 Constant
*BitcastF
= ConstantExpr::getBitCast(F
, G
->getType());
773 PointerType
*PtrType
= G
->getType();
774 auto *GA
= GlobalAlias::create(
775 PtrType
->getElementType(), PtrType
->getAddressSpace(),
776 G
->getLinkage(), "", BitcastF
, G
->getParent());
778 F
->setAlignment(std::max(F
->getAlignment(), G
->getAlignment()));
780 GA
->setVisibility(G
->getVisibility());
781 GA
->setUnnamedAddr(GlobalValue::UnnamedAddr::Global
);
784 G
->replaceAllUsesWith(GA
);
785 G
->eraseFromParent();
787 LLVM_DEBUG(dbgs() << "writeAlias: " << GA
->getName() << '\n');
791 // Replace G with an alias to F if possible, or a thunk to F if
792 // profitable. Returns false if neither is the case.
793 bool MergeFunctions::writeThunkOrAlias(Function
*F
, Function
*G
) {
794 if (canCreateAliasFor(G
)) {
798 if (canCreateThunkFor(F
)) {
805 // Merge two equivalent functions. Upon completion, Function G is deleted.
806 void MergeFunctions::mergeTwoFunctions(Function
*F
, Function
*G
) {
807 if (F
->isInterposable()) {
808 assert(G
->isInterposable());
810 // Both writeThunkOrAlias() calls below must succeed, either because we can
811 // create aliases for G and NewF, or because a thunk for F is profitable.
812 // F here has the same signature as NewF below, so that's what we check.
813 if (!canCreateThunkFor(F
) &&
814 (!canCreateAliasFor(F
) || !canCreateAliasFor(G
)))
817 // Make them both thunks to the same internal function.
818 Function
*NewF
= Function::Create(F
->getFunctionType(), F
->getLinkage(),
819 F
->getAddressSpace(), "", F
->getParent());
820 NewF
->copyAttributesFrom(F
);
823 F
->replaceAllUsesWith(NewF
);
825 unsigned MaxAlignment
= std::max(G
->getAlignment(), NewF
->getAlignment());
827 writeThunkOrAlias(F
, G
);
828 writeThunkOrAlias(F
, NewF
);
830 F
->setAlignment(MaxAlignment
);
831 F
->setLinkage(GlobalValue::PrivateLinkage
);
833 ++NumFunctionsMerged
;
835 // For better debugability, under MergeFunctionsPDI, we do not modify G's
836 // call sites to point to F even when within the same translation unit.
837 if (!G
->isInterposable() && !MergeFunctionsPDI
) {
838 if (G
->hasGlobalUnnamedAddr()) {
839 // G might have been a key in our GlobalNumberState, and it's illegal
840 // to replace a key in ValueMap<GlobalValue *> with a non-global.
841 GlobalNumbers
.erase(G
);
842 // If G's address is not significant, replace it entirely.
843 Constant
*BitcastF
= ConstantExpr::getBitCast(F
, G
->getType());
845 G
->replaceAllUsesWith(BitcastF
);
847 // Redirect direct callers of G to F. (See note on MergeFunctionsPDI
849 replaceDirectCallers(G
, F
);
853 // If G was internal then we may have replaced all uses of G with F. If so,
854 // stop here and delete G. There's no need for a thunk. (See note on
855 // MergeFunctionsPDI above).
856 if (G
->isDiscardableIfUnused() && G
->use_empty() && !MergeFunctionsPDI
) {
857 G
->eraseFromParent();
858 ++NumFunctionsMerged
;
862 if (writeThunkOrAlias(F
, G
)) {
863 ++NumFunctionsMerged
;
868 /// Replace function F by function G.
869 void MergeFunctions::replaceFunctionInTree(const FunctionNode
&FN
,
871 Function
*F
= FN
.getFunc();
872 assert(FunctionComparator(F
, G
, &GlobalNumbers
).compare() == 0 &&
873 "The two functions must be equal");
875 auto I
= FNodesInTree
.find(F
);
876 assert(I
!= FNodesInTree
.end() && "F should be in FNodesInTree");
877 assert(FNodesInTree
.count(G
) == 0 && "FNodesInTree should not contain G");
879 FnTreeType::iterator IterToFNInFnTree
= I
->second
;
880 assert(&(*IterToFNInFnTree
) == &FN
&& "F should map to FN in FNodesInTree.");
881 // Remove F -> FN and insert G -> FN
882 FNodesInTree
.erase(I
);
883 FNodesInTree
.insert({G
, IterToFNInFnTree
});
884 // Replace F with G in FN, which is stored inside the FnTree.
888 // Ordering for functions that are equal under FunctionComparator
889 static bool isFuncOrderCorrect(const Function
*F
, const Function
*G
) {
890 if (F
->isInterposable() != G
->isInterposable()) {
891 // Strong before weak, because the weak function may call the strong
892 // one, but not the other way around.
893 return !F
->isInterposable();
895 if (F
->hasLocalLinkage() != G
->hasLocalLinkage()) {
896 // External before local, because we definitely have to keep the external
897 // function, but may be able to drop the local one.
898 return !F
->hasLocalLinkage();
900 // Impose a total order (by name) on the replacement of functions. This is
901 // important when operating on more than one module independently to prevent
902 // cycles of thunks calling each other when the modules are linked together.
903 return F
->getName() <= G
->getName();
906 // Insert a ComparableFunction into the FnTree, or merge it away if equal to one
907 // that was already inserted.
908 bool MergeFunctions::insert(Function
*NewFunction
) {
909 std::pair
<FnTreeType::iterator
, bool> Result
=
910 FnTree
.insert(FunctionNode(NewFunction
));
913 assert(FNodesInTree
.count(NewFunction
) == 0);
914 FNodesInTree
.insert({NewFunction
, Result
.first
});
915 LLVM_DEBUG(dbgs() << "Inserting as unique: " << NewFunction
->getName()
920 const FunctionNode
&OldF
= *Result
.first
;
922 if (!isFuncOrderCorrect(OldF
.getFunc(), NewFunction
)) {
923 // Swap the two functions.
924 Function
*F
= OldF
.getFunc();
925 replaceFunctionInTree(*Result
.first
, NewFunction
);
927 assert(OldF
.getFunc() != F
&& "Must have swapped the functions.");
930 LLVM_DEBUG(dbgs() << " " << OldF
.getFunc()->getName()
931 << " == " << NewFunction
->getName() << '\n');
933 Function
*DeleteF
= NewFunction
;
934 mergeTwoFunctions(OldF
.getFunc(), DeleteF
);
938 // Remove a function from FnTree. If it was already in FnTree, add
939 // it to Deferred so that we'll look at it in the next round.
940 void MergeFunctions::remove(Function
*F
) {
941 auto I
= FNodesInTree
.find(F
);
942 if (I
!= FNodesInTree
.end()) {
943 LLVM_DEBUG(dbgs() << "Deferred " << F
->getName() << ".\n");
944 FnTree
.erase(I
->second
);
945 // I->second has been invalidated, remove it from the FNodesInTree map to
946 // preserve the invariant.
947 FNodesInTree
.erase(I
);
948 Deferred
.emplace_back(F
);
952 // For each instruction used by the value, remove() the function that contains
953 // the instruction. This should happen right before a call to RAUW.
954 void MergeFunctions::removeUsers(Value
*V
) {
955 std::vector
<Value
*> Worklist
;
956 Worklist
.push_back(V
);
957 SmallPtrSet
<Value
*, 8> Visited
;
959 while (!Worklist
.empty()) {
960 Value
*V
= Worklist
.back();
963 for (User
*U
: V
->users()) {
964 if (Instruction
*I
= dyn_cast
<Instruction
>(U
)) {
965 remove(I
->getFunction());
966 } else if (isa
<GlobalValue
>(U
)) {
968 } else if (Constant
*C
= dyn_cast
<Constant
>(U
)) {
969 for (User
*UU
: C
->users()) {
970 if (!Visited
.insert(UU
).second
)
971 Worklist
.push_back(UU
);