Bug Summary

File:src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/IPO/MergeFunctions.cpp
Warning:line 705, column 23
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MergeFunctions.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/IPO/MergeFunctions.cpp
1//===- MergeFunctions.cpp - Merge identical functions ---------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass looks for equivalent functions that are mergable and folds them.
10//
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).
21//
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.
29//
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.
37//
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.
41//
42//===----------------------------------------------------------------------===//
43//
44// Future work:
45//
46// * virtual functions.
47//
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.
51//
52// * be smarter about bitcasts.
53//
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.
58//
59// * Compare complex types with pointer types inside.
60// * Compare cross-reference cases.
61// * Compare complex expressions.
62//
63// All the three issues above could be described as ability to prove that
64// fA == fB == fC == fE == fF == fG in example below:
65//
66// void fA() {
67// fB();
68// }
69// void fB() {
70// fA();
71// }
72//
73// void fE() {
74// fF();
75// }
76// void fF() {
77// fG();
78// }
79// void fG() {
80// fE();
81// }
82//
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.
88//
89//===----------------------------------------------------------------------===//
90
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/Constant.h"
99#include "llvm/IR/Constants.h"
100#include "llvm/IR/DebugInfoMetadata.h"
101#include "llvm/IR/DebugLoc.h"
102#include "llvm/IR/DerivedTypes.h"
103#include "llvm/IR/Function.h"
104#include "llvm/IR/GlobalValue.h"
105#include "llvm/IR/IRBuilder.h"
106#include "llvm/IR/InstrTypes.h"
107#include "llvm/IR/Instruction.h"
108#include "llvm/IR/Instructions.h"
109#include "llvm/IR/IntrinsicInst.h"
110#include "llvm/IR/Module.h"
111#include "llvm/IR/Type.h"
112#include "llvm/IR/Use.h"
113#include "llvm/IR/User.h"
114#include "llvm/IR/Value.h"
115#include "llvm/IR/ValueHandle.h"
116#include "llvm/IR/ValueMap.h"
117#include "llvm/InitializePasses.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/IPO/MergeFunctions.h"
125#include "llvm/Transforms/Utils/FunctionComparator.h"
126#include <algorithm>
127#include <cassert>
128#include <iterator>
129#include <set>
130#include <utility>
131#include <vector>
132
133using namespace llvm;
134
135#define DEBUG_TYPE"mergefunc" "mergefunc"
136
137STATISTIC(NumFunctionsMerged, "Number of functions merged")static llvm::Statistic NumFunctionsMerged = {"mergefunc", "NumFunctionsMerged"
, "Number of functions merged"}
;
138STATISTIC(NumThunksWritten, "Number of thunks generated")static llvm::Statistic NumThunksWritten = {"mergefunc", "NumThunksWritten"
, "Number of thunks generated"}
;
139STATISTIC(NumAliasesWritten, "Number of aliases generated")static llvm::Statistic NumAliasesWritten = {"mergefunc", "NumAliasesWritten"
, "Number of aliases generated"}
;
140STATISTIC(NumDoubleWeak, "Number of new functions created")static llvm::Statistic NumDoubleWeak = {"mergefunc", "NumDoubleWeak"
, "Number of new functions created"}
;
141
142static cl::opt<unsigned> NumFunctionsForSanityCheck(
143 "mergefunc-sanity",
144 cl::desc("How many functions in module could be used for "
145 "MergeFunctions pass sanity check. "
146 "'0' disables this check. Works only with '-debug' key."),
147 cl::init(0), cl::Hidden);
148
149// Under option -mergefunc-preserve-debug-info we:
150// - Do not create a new function for a thunk.
151// - Retain the debug info for a thunk's parameters (and associated
152// instructions for the debug info) from the entry block.
153// Note: -debug will display the algorithm at work.
154// - Create debug-info for the call (to the shared implementation) made by
155// a thunk and its return value.
156// - Erase the rest of the function, retaining the (minimally sized) entry
157// block to create a thunk.
158// - Preserve a thunk's call site to point to the thunk even when both occur
159// within the same translation unit, to aid debugability. Note that this
160// behaviour differs from the underlying -mergefunc implementation which
161// modifies the thunk's call site to point to the shared implementation
162// when both occur within the same translation unit.
163static cl::opt<bool>
164 MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden,
165 cl::init(false),
166 cl::desc("Preserve debug info in thunk when mergefunc "
167 "transformations are made."));
168
169static cl::opt<bool>
170 MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden,
171 cl::init(false),
172 cl::desc("Allow mergefunc to create aliases"));
173
174namespace {
175
176class FunctionNode {
177 mutable AssertingVH<Function> F;
178 FunctionComparator::FunctionHash Hash;
179
180public:
181 // Note the hash is recalculated potentially multiple times, but it is cheap.
182 FunctionNode(Function *F)
183 : F(F), Hash(FunctionComparator::functionHash(*F)) {}
184
185 Function *getFunc() const { return F; }
186 FunctionComparator::FunctionHash getHash() const { return Hash; }
187
188 /// Replace the reference to the function F by the function G, assuming their
189 /// implementations are equal.
190 void replaceBy(Function *G) const {
191 F = G;
192 }
193};
194
195/// MergeFunctions finds functions which will generate identical machine code,
196/// by considering all pointer types to be equivalent. Once identified,
197/// MergeFunctions will fold them by replacing a call to one to a call to a
198/// bitcast of the other.
199class MergeFunctions {
200public:
201 MergeFunctions() : FnTree(FunctionNodeCmp(&GlobalNumbers)) {
202 }
203
204 bool runOnModule(Module &M);
205
206private:
207 // The function comparison operator is provided here so that FunctionNodes do
208 // not need to become larger with another pointer.
209 class FunctionNodeCmp {
210 GlobalNumberState* GlobalNumbers;
211
212 public:
213 FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {}
214
215 bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const {
216 // Order first by hashes, then full function comparison.
217 if (LHS.getHash() != RHS.getHash())
218 return LHS.getHash() < RHS.getHash();
219 FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers);
220 return FCmp.compare() == -1;
221 }
222 };
223 using FnTreeType = std::set<FunctionNode, FunctionNodeCmp>;
224
225 GlobalNumberState GlobalNumbers;
226
227 /// A work queue of functions that may have been modified and should be
228 /// analyzed again.
229 std::vector<WeakTrackingVH> Deferred;
230
231#ifndef NDEBUG1
232 /// Checks the rules of order relation introduced among functions set.
233 /// Returns true, if sanity check has been passed, and false if failed.
234 bool doSanityCheck(std::vector<WeakTrackingVH> &Worklist);
235#endif
236
237 /// Insert a ComparableFunction into the FnTree, or merge it away if it's
238 /// equal to one that's already present.
239 bool insert(Function *NewFunction);
240
241 /// Remove a Function from the FnTree and queue it up for a second sweep of
242 /// analysis.
243 void remove(Function *F);
244
245 /// Find the functions that use this Value and remove them from FnTree and
246 /// queue the functions.
247 void removeUsers(Value *V);
248
249 /// Replace all direct calls of Old with calls of New. Will bitcast New if
250 /// necessary to make types match.
251 void replaceDirectCallers(Function *Old, Function *New);
252
253 /// Merge two equivalent functions. Upon completion, G may be deleted, or may
254 /// be converted into a thunk. In either case, it should never be visited
255 /// again.
256 void mergeTwoFunctions(Function *F, Function *G);
257
258 /// Fill PDIUnrelatedWL with instructions from the entry block that are
259 /// unrelated to parameter related debug info.
260 void filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock,
261 std::vector<Instruction *> &PDIUnrelatedWL);
262
263 /// Erase the rest of the CFG (i.e. barring the entry block).
264 void eraseTail(Function *G);
265
266 /// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
267 /// parameter debug info, from the entry block.
268 void eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL);
269
270 /// Replace G with a simple tail call to bitcast(F). Also (unless
271 /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
272 /// delete G.
273 void writeThunk(Function *F, Function *G);
274
275 // Replace G with an alias to F (deleting function G)
276 void writeAlias(Function *F, Function *G);
277
278 // Replace G with an alias to F if possible, or a thunk to F if possible.
279 // Returns false if neither is the case.
280 bool writeThunkOrAlias(Function *F, Function *G);
281
282 /// Replace function F with function G in the function tree.
283 void replaceFunctionInTree(const FunctionNode &FN, Function *G);
284
285 /// The set of all distinct functions. Use the insert() and remove() methods
286 /// to modify it. The map allows efficient lookup and deferring of Functions.
287 FnTreeType FnTree;
288
289 // Map functions to the iterators of the FunctionNode which contains them
290 // in the FnTree. This must be updated carefully whenever the FnTree is
291 // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid
292 // dangling iterators into FnTree. The invariant that preserves this is that
293 // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
294 DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;
295};
296
297class MergeFunctionsLegacyPass : public ModulePass {
298public:
299 static char ID;
300
301 MergeFunctionsLegacyPass(): ModulePass(ID) {
302 initializeMergeFunctionsLegacyPassPass(*PassRegistry::getPassRegistry());
303 }
304
305 bool runOnModule(Module &M) override {
306 if (skipModule(M))
307 return false;
308
309 MergeFunctions MF;
310 return MF.runOnModule(M);
311 }
312};
313
314} // end anonymous namespace
315
316char MergeFunctionsLegacyPass::ID = 0;
317INITIALIZE_PASS(MergeFunctionsLegacyPass, "mergefunc",static void *initializeMergeFunctionsLegacyPassPassOnce(PassRegistry
&Registry) { PassInfo *PI = new PassInfo( "Merge Functions"
, "mergefunc", &MergeFunctionsLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<MergeFunctionsLegacyPass>), false, false
); Registry.registerPass(*PI, true); return PI; } static llvm
::once_flag InitializeMergeFunctionsLegacyPassPassFlag; void llvm
::initializeMergeFunctionsLegacyPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeMergeFunctionsLegacyPassPassFlag
, initializeMergeFunctionsLegacyPassPassOnce, std::ref(Registry
)); }
318 "Merge Functions", false, false)static void *initializeMergeFunctionsLegacyPassPassOnce(PassRegistry
&Registry) { PassInfo *PI = new PassInfo( "Merge Functions"
, "mergefunc", &MergeFunctionsLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<MergeFunctionsLegacyPass>), false, false
); Registry.registerPass(*PI, true); return PI; } static llvm
::once_flag InitializeMergeFunctionsLegacyPassPassFlag; void llvm
::initializeMergeFunctionsLegacyPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeMergeFunctionsLegacyPassPassFlag
, initializeMergeFunctionsLegacyPassPassOnce, std::ref(Registry
)); }
319
320ModulePass *llvm::createMergeFunctionsPass() {
321 return new MergeFunctionsLegacyPass();
322}
323
324PreservedAnalyses MergeFunctionsPass::run(Module &M,
325 ModuleAnalysisManager &AM) {
326 MergeFunctions MF;
327 if (!MF.runOnModule(M))
328 return PreservedAnalyses::all();
329 return PreservedAnalyses::none();
330}
331
332#ifndef NDEBUG1
333bool MergeFunctions::doSanityCheck(std::vector<WeakTrackingVH> &Worklist) {
334 if (const unsigned Max = NumFunctionsForSanityCheck) {
335 unsigned TripleNumber = 0;
336 bool Valid = true;
337
338 dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n";
339
340 unsigned i = 0;
341 for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(),
342 E = Worklist.end();
343 I != E && i < Max; ++I, ++i) {
344 unsigned j = i;
345 for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max;
346 ++J, ++j) {
347 Function *F1 = cast<Function>(*I);
348 Function *F2 = cast<Function>(*J);
349 int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare();
350 int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare();
351
352 // If F1 <= F2, then F2 >= F1, otherwise report failure.
353 if (Res1 != -Res2) {
354 dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber
355 << "\n";
356 dbgs() << *F1 << '\n' << *F2 << '\n';
357 Valid = false;
358 }
359
360 if (Res1 == 0)
361 continue;
362
363 unsigned k = j;
364 for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max;
365 ++k, ++K, ++TripleNumber) {
366 if (K == J)
367 continue;
368
369 Function *F3 = cast<Function>(*K);
370 int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare();
371 int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare();
372
373 bool Transitive = true;
374
375 if (Res1 != 0 && Res1 == Res4) {
376 // F1 > F2, F2 > F3 => F1 > F3
377 Transitive = Res3 == Res1;
378 } else if (Res3 != 0 && Res3 == -Res4) {
379 // F1 > F3, F3 > F2 => F1 > F2
380 Transitive = Res3 == Res1;
381 } else if (Res4 != 0 && -Res3 == Res4) {
382 // F2 > F3, F3 > F1 => F2 > F1
383 Transitive = Res4 == -Res1;
384 }
385
386 if (!Transitive) {
387 dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: "
388 << TripleNumber << "\n";
389 dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", "
390 << Res4 << "\n";
391 dbgs() << *F1 << '\n' << *F2 << '\n' << *F3 << '\n';
392 Valid = false;
393 }
394 }
395 }
396 }
397
398 dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n";
399 return Valid;
400 }
401 return true;
402}
403#endif
404
405/// Check whether \p F is eligible for function merging.
406static bool isEligibleForMerging(Function &F) {
407 return !F.isDeclaration() && !F.hasAvailableExternallyLinkage();
408}
409
410bool MergeFunctions::runOnModule(Module &M) {
411 bool Changed = false;
412
413 // All functions in the module, ordered by hash. Functions with a unique
414 // hash value are easily eliminated.
415 std::vector<std::pair<FunctionComparator::FunctionHash, Function *>>
416 HashedFuncs;
417 for (Function &Func : M) {
418 if (isEligibleForMerging(Func)) {
419 HashedFuncs.push_back({FunctionComparator::functionHash(Func), &Func});
420 }
421 }
422
423 llvm::stable_sort(HashedFuncs, less_first());
424
425 auto S = HashedFuncs.begin();
426 for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) {
427 // If the hash value matches the previous value or the next one, we must
428 // consider merging it. Otherwise it is dropped and never considered again.
429 if ((I != S && std::prev(I)->first == I->first) ||
430 (std::next(I) != IE && std::next(I)->first == I->first) ) {
431 Deferred.push_back(WeakTrackingVH(I->second));
432 }
433 }
434
435 do {
436 std::vector<WeakTrackingVH> Worklist;
437 Deferred.swap(Worklist);
438
439 LLVM_DEBUG(doSanityCheck(Worklist))do { } while (false);
440
441 LLVM_DEBUG(dbgs() << "size of module: " << M.size() << '\n')do { } while (false);
442 LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n')do { } while (false);
443
444 // Insert functions and merge them.
445 for (WeakTrackingVH &I : Worklist) {
446 if (!I)
447 continue;
448 Function *F = cast<Function>(I);
449 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {
450 Changed |= insert(F);
451 }
452 }
453 LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n')do { } while (false);
454 } while (!Deferred.empty());
455
456 FnTree.clear();
457 FNodesInTree.clear();
458 GlobalNumbers.clear();
459
460 return Changed;
461}
462
463// Replace direct callers of Old with New.
464void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
465 Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType());
466 for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) {
467 Use *U = &*UI;
468 ++UI;
469 CallBase *CB = dyn_cast<CallBase>(U->getUser());
470 if (CB && CB->isCallee(U)) {
471 // Do not copy attributes from the called function to the call-site.
472 // Function comparison ensures that the attributes are the same up to
473 // type congruences in byval(), in which case we need to keep the byval
474 // type of the call-site, not the callee function.
475 remove(CB->getFunction());
476 U->set(BitcastNew);
477 }
478 }
479}
480
481// Helper for writeThunk,
482// Selects proper bitcast operation,
483// but a bit simpler then CastInst::getCastOpcode.
484static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) {
485 Type *SrcTy = V->getType();
486 if (SrcTy->isStructTy()) {
487 assert(DestTy->isStructTy())((void)0);
488 assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements())((void)0);
489 Value *Result = UndefValue::get(DestTy);
490 for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) {
491 Value *Element = createCast(
492 Builder, Builder.CreateExtractValue(V, makeArrayRef(I)),
493 DestTy->getStructElementType(I));
494
495 Result =
496 Builder.CreateInsertValue(Result, Element, makeArrayRef(I));
497 }
498 return Result;
499 }
500 assert(!DestTy->isStructTy())((void)0);
501 if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
502 return Builder.CreateIntToPtr(V, DestTy);
503 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
504 return Builder.CreatePtrToInt(V, DestTy);
505 else
506 return Builder.CreateBitCast(V, DestTy);
507}
508
509// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
510// parameter debug info, from the entry block.
511void MergeFunctions::eraseInstsUnrelatedToPDI(
512 std::vector<Instruction *> &PDIUnrelatedWL) {
513 LLVM_DEBUG(do { } while (false)
514 dbgs() << " Erasing instructions (in reverse order of appearance in "do { } while (false)
515 "entry block) unrelated to parameter debug info from entry "do { } while (false)
516 "block: {\n")do { } while (false);
517 while (!PDIUnrelatedWL.empty()) {
518 Instruction *I = PDIUnrelatedWL.back();
519 LLVM_DEBUG(dbgs() << " Deleting Instruction: ")do { } while (false);
520 LLVM_DEBUG(I->print(dbgs()))do { } while (false);
521 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
522 I->eraseFromParent();
523 PDIUnrelatedWL.pop_back();
524 }
525 LLVM_DEBUG(dbgs() << " } // Done erasing instructions unrelated to parameter "do { } while (false)
526 "debug info from entry block. \n")do { } while (false);
527}
528
529// Reduce G to its entry block.
530void MergeFunctions::eraseTail(Function *G) {
531 std::vector<BasicBlock *> WorklistBB;
532 for (BasicBlock &BB : drop_begin(*G)) {
533 BB.dropAllReferences();
534 WorklistBB.push_back(&BB);
535 }
536 while (!WorklistBB.empty()) {
537 BasicBlock *BB = WorklistBB.back();
538 BB->eraseFromParent();
539 WorklistBB.pop_back();
540 }
541}
542
543// We are interested in the following instructions from the entry block as being
544// related to parameter debug info:
545// - @llvm.dbg.declare
546// - stores from the incoming parameters to locations on the stack-frame
547// - allocas that create these locations on the stack-frame
548// - @llvm.dbg.value
549// - the entry block's terminator
550// The rest are unrelated to debug info for the parameters; fill up
551// PDIUnrelatedWL with such instructions.
552void MergeFunctions::filterInstsUnrelatedToPDI(
553 BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL) {
554 std::set<Instruction *> PDIRelated;
555 for (BasicBlock::iterator BI = GEntryBlock->begin(), BIE = GEntryBlock->end();
556 BI != BIE; ++BI) {
557 if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) {
558 LLVM_DEBUG(dbgs() << " Deciding: ")do { } while (false);
559 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
560 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
561 DILocalVariable *DILocVar = DVI->getVariable();
562 if (DILocVar->isParameter()) {
563 LLVM_DEBUG(dbgs() << " Include (parameter): ")do { } while (false);
564 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
565 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
566 PDIRelated.insert(&*BI);
567 } else {
568 LLVM_DEBUG(dbgs() << " Delete (!parameter): ")do { } while (false);
569 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
570 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
571 }
572 } else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) {
573 LLVM_DEBUG(dbgs() << " Deciding: ")do { } while (false);
574 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
575 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
576 DILocalVariable *DILocVar = DDI->getVariable();
577 if (DILocVar->isParameter()) {
578 LLVM_DEBUG(dbgs() << " Parameter: ")do { } while (false);
579 LLVM_DEBUG(DILocVar->print(dbgs()))do { } while (false);
580 AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
581 if (AI) {
582 LLVM_DEBUG(dbgs() << " Processing alloca users: ")do { } while (false);
583 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
584 for (User *U : AI->users()) {
585 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
586 if (Value *Arg = SI->getValueOperand()) {
587 if (isa<Argument>(Arg)) {
588 LLVM_DEBUG(dbgs() << " Include: ")do { } while (false);
589 LLVM_DEBUG(AI->print(dbgs()))do { } while (false);
590 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
591 PDIRelated.insert(AI);
592 LLVM_DEBUG(dbgs() << " Include (parameter): ")do { } while (false);
593 LLVM_DEBUG(SI->print(dbgs()))do { } while (false);
594 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
595 PDIRelated.insert(SI);
596 LLVM_DEBUG(dbgs() << " Include: ")do { } while (false);
597 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
598 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
599 PDIRelated.insert(&*BI);
600 } else {
601 LLVM_DEBUG(dbgs() << " Delete (!parameter): ")do { } while (false);
602 LLVM_DEBUG(SI->print(dbgs()))do { } while (false);
603 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
604 }
605 }
606 } else {
607 LLVM_DEBUG(dbgs() << " Defer: ")do { } while (false);
608 LLVM_DEBUG(U->print(dbgs()))do { } while (false);
609 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
610 }
611 }
612 } else {
613 LLVM_DEBUG(dbgs() << " Delete (alloca NULL): ")do { } while (false);
614 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
615 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
616 }
617 } else {
618 LLVM_DEBUG(dbgs() << " Delete (!parameter): ")do { } while (false);
619 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
620 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
621 }
622 } else if (BI->isTerminator() && &*BI == GEntryBlock->getTerminator()) {
623 LLVM_DEBUG(dbgs() << " Will Include Terminator: ")do { } while (false);
624 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
625 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
626 PDIRelated.insert(&*BI);
627 } else {
628 LLVM_DEBUG(dbgs() << " Defer: ")do { } while (false);
629 LLVM_DEBUG(BI->print(dbgs()))do { } while (false);
630 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
631 }
632 }
633 LLVM_DEBUG(do { } while (false)
634 dbgs()do { } while (false)
635 << " Report parameter debug info related/related instructions: {\n")do { } while (false);
636 for (Instruction &I : *GEntryBlock) {
637 if (PDIRelated.find(&I) == PDIRelated.end()) {
638 LLVM_DEBUG(dbgs() << " !PDIRelated: ")do { } while (false);
639 LLVM_DEBUG(I.print(dbgs()))do { } while (false);
640 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
641 PDIUnrelatedWL.push_back(&I);
642 } else {
643 LLVM_DEBUG(dbgs() << " PDIRelated: ")do { } while (false);
644 LLVM_DEBUG(I.print(dbgs()))do { } while (false);
645 LLVM_DEBUG(dbgs() << "\n")do { } while (false);
646 }
647 }
648 LLVM_DEBUG(dbgs() << " }\n")do { } while (false);
649}
650
651/// Whether this function may be replaced by a forwarding thunk.
652static bool canCreateThunkFor(Function *F) {
653 if (F->isVarArg())
654 return false;
655
656 // Don't merge tiny functions using a thunk, since it can just end up
657 // making the function larger.
658 if (F->size() == 1) {
659 if (F->front().size() <= 2) {
660 LLVM_DEBUG(dbgs() << "canCreateThunkFor: " << F->getName()do { } while (false)
661 << " is too small to bother creating a thunk for\n")do { } while (false);
662 return false;
663 }
664 }
665 return true;
666}
667
668// Replace G with a simple tail call to bitcast(F). Also (unless
669// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
670// delete G. Under MergeFunctionsPDI, we use G itself for creating
671// the thunk as we preserve the debug info (and associated instructions)
672// from G's entry block pertaining to G's incoming arguments which are
673// passed on as corresponding arguments in the call that G makes to F.
674// For better debugability, under MergeFunctionsPDI, we do not modify G's
675// call sites to point to F even when within the same translation unit.
676void MergeFunctions::writeThunk(Function *F, Function *G) {
677 BasicBlock *GEntryBlock = nullptr;
678 std::vector<Instruction *> PDIUnrelatedWL;
679 BasicBlock *BB = nullptr;
680 Function *NewG = nullptr;
1
'NewG' initialized to a null pointer value
681 if (MergeFunctionsPDI) {
2
Assuming the condition is true
3
Taking true branch
682 LLVM_DEBUG(dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new "do { } while (false)
4
Loop condition is false. Exiting loop
683 "function as thunk; retain original: "do { } while (false)
684 << G->getName() << "()\n")do { } while (false);
685 GEntryBlock = &G->getEntryBlock();
686 LLVM_DEBUG(do { } while (false)
5
Loop condition is false. Exiting loop
687 dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "do { } while (false)
688 "debug info for "do { } while (false)
689 << G->getName() << "() {\n")do { } while (false);
690 filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL);
691 GEntryBlock->getTerminator()->eraseFromParent();
692 BB = GEntryBlock;
693 } else {
694 NewG = Function::Create(G->getFunctionType(), G->getLinkage(),
695 G->getAddressSpace(), "", G->getParent());
696 NewG->setComdat(G->getComdat());
697 BB = BasicBlock::Create(F->getContext(), "", NewG);
698 }
699
700 IRBuilder<> Builder(BB);
701 Function *H = MergeFunctionsPDI ? G : NewG;
6
Assuming the condition is false
7
'?' condition is false
8
'H' initialized to a null pointer value
702 SmallVector<Value *, 16> Args;
703 unsigned i = 0;
704 FunctionType *FFTy = F->getFunctionType();
705 for (Argument &AI : H->args()) {
9
Called C++ object pointer is null
706 Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i)));
707 ++i;
708 }
709
710 CallInst *CI = Builder.CreateCall(F, Args);
711 ReturnInst *RI = nullptr;
712 bool isSwiftTailCall = F->getCallingConv() == CallingConv::SwiftTail &&
713 G->getCallingConv() == CallingConv::SwiftTail;
714 CI->setTailCallKind(isSwiftTailCall ? llvm::CallInst::TCK_MustTail
715 : llvm::CallInst::TCK_Tail);
716 CI->setCallingConv(F->getCallingConv());
717 CI->setAttributes(F->getAttributes());
718 if (H->getReturnType()->isVoidTy()) {
719 RI = Builder.CreateRetVoid();
720 } else {
721 RI = Builder.CreateRet(createCast(Builder, CI, H->getReturnType()));
722 }
723
724 if (MergeFunctionsPDI) {
725 DISubprogram *DIS = G->getSubprogram();
726 if (DIS) {
727 DebugLoc CIDbgLoc =
728 DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS);
729 DebugLoc RIDbgLoc =
730 DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS);
731 CI->setDebugLoc(CIDbgLoc);
732 RI->setDebugLoc(RIDbgLoc);
733 } else {
734 LLVM_DEBUG(do { } while (false)
735 dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for "do { } while (false)
736 << G->getName() << "()\n")do { } while (false);
737 }
738 eraseTail(G);
739 eraseInstsUnrelatedToPDI(PDIUnrelatedWL);
740 LLVM_DEBUG(do { } while (false)
741 dbgs() << "} // End of parameter related debug info filtering for: "do { } while (false)
742 << G->getName() << "()\n")do { } while (false);
743 } else {
744 NewG->copyAttributesFrom(G);
745 NewG->takeName(G);
746 removeUsers(G);
747 G->replaceAllUsesWith(NewG);
748 G->eraseFromParent();
749 }
750
751 LLVM_DEBUG(dbgs() << "writeThunk: " << H->getName() << '\n')do { } while (false);
752 ++NumThunksWritten;
753}
754
755// Whether this function may be replaced by an alias
756static bool canCreateAliasFor(Function *F) {
757 if (!MergeFunctionsAliases || !F->hasGlobalUnnamedAddr())
758 return false;
759
760 // We should only see linkages supported by aliases here
761 assert(F->hasLocalLinkage() || F->hasExternalLinkage()((void)0)
762 || F->hasWeakLinkage() || F->hasLinkOnceLinkage())((void)0);
763 return true;
764}
765
766// Replace G with an alias to F (deleting function G)
767void MergeFunctions::writeAlias(Function *F, Function *G) {
768 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
769 PointerType *PtrType = G->getType();
770 auto *GA = GlobalAlias::create(G->getValueType(), PtrType->getAddressSpace(),
771 G->getLinkage(), "", BitcastF, G->getParent());
772
773 F->setAlignment(MaybeAlign(std::max(F->getAlignment(), G->getAlignment())));
774 GA->takeName(G);
775 GA->setVisibility(G->getVisibility());
776 GA->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
777
778 removeUsers(G);
779 G->replaceAllUsesWith(GA);
780 G->eraseFromParent();
781
782 LLVM_DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n')do { } while (false);
783 ++NumAliasesWritten;
784}
785
786// Replace G with an alias to F if possible, or a thunk to F if
787// profitable. Returns false if neither is the case.
788bool MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {
789 if (canCreateAliasFor(G)) {
790 writeAlias(F, G);
791 return true;
792 }
793 if (canCreateThunkFor(F)) {
794 writeThunk(F, G);
795 return true;
796 }
797 return false;
798}
799
800// Merge two equivalent functions. Upon completion, Function G is deleted.
801void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
802 if (F->isInterposable()) {
803 assert(G->isInterposable())((void)0);
804
805 // Both writeThunkOrAlias() calls below must succeed, either because we can
806 // create aliases for G and NewF, or because a thunk for F is profitable.
807 // F here has the same signature as NewF below, so that's what we check.
808 if (!canCreateThunkFor(F) &&
809 (!canCreateAliasFor(F) || !canCreateAliasFor(G)))
810 return;
811
812 // Make them both thunks to the same internal function.
813 Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(),
814 F->getAddressSpace(), "", F->getParent());
815 NewF->copyAttributesFrom(F);
816 NewF->takeName(F);
817 removeUsers(F);
818 F->replaceAllUsesWith(NewF);
819
820 MaybeAlign MaxAlignment(std::max(G->getAlignment(), NewF->getAlignment()));
821
822 writeThunkOrAlias(F, G);
823 writeThunkOrAlias(F, NewF);
824
825 F->setAlignment(MaxAlignment);
826 F->setLinkage(GlobalValue::PrivateLinkage);
827 ++NumDoubleWeak;
828 ++NumFunctionsMerged;
829 } else {
830 // For better debugability, under MergeFunctionsPDI, we do not modify G's
831 // call sites to point to F even when within the same translation unit.
832 if (!G->isInterposable() && !MergeFunctionsPDI) {
833 if (G->hasGlobalUnnamedAddr()) {
834 // G might have been a key in our GlobalNumberState, and it's illegal
835 // to replace a key in ValueMap<GlobalValue *> with a non-global.
836 GlobalNumbers.erase(G);
837 // If G's address is not significant, replace it entirely.
838 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
839 removeUsers(G);
840 G->replaceAllUsesWith(BitcastF);
841 } else {
842 // Redirect direct callers of G to F. (See note on MergeFunctionsPDI
843 // above).
844 replaceDirectCallers(G, F);
845 }
846 }
847
848 // If G was internal then we may have replaced all uses of G with F. If so,
849 // stop here and delete G. There's no need for a thunk. (See note on
850 // MergeFunctionsPDI above).
851 if (G->isDiscardableIfUnused() && G->use_empty() && !MergeFunctionsPDI) {
852 G->eraseFromParent();
853 ++NumFunctionsMerged;
854 return;
855 }
856
857 if (writeThunkOrAlias(F, G)) {
858 ++NumFunctionsMerged;
859 }
860 }
861}
862
863/// Replace function F by function G.
864void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN,
865 Function *G) {
866 Function *F = FN.getFunc();
867 assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 &&((void)0)
868 "The two functions must be equal")((void)0);
869
870 auto I = FNodesInTree.find(F);
871 assert(I != FNodesInTree.end() && "F should be in FNodesInTree")((void)0);
872 assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G")((void)0);
873
874 FnTreeType::iterator IterToFNInFnTree = I->second;
875 assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree.")((void)0);
876 // Remove F -> FN and insert G -> FN
877 FNodesInTree.erase(I);
878 FNodesInTree.insert({G, IterToFNInFnTree});
879 // Replace F with G in FN, which is stored inside the FnTree.
880 FN.replaceBy(G);
881}
882
883// Ordering for functions that are equal under FunctionComparator
884static bool isFuncOrderCorrect(const Function *F, const Function *G) {
885 if (F->isInterposable() != G->isInterposable()) {
886 // Strong before weak, because the weak function may call the strong
887 // one, but not the other way around.
888 return !F->isInterposable();
889 }
890 if (F->hasLocalLinkage() != G->hasLocalLinkage()) {
891 // External before local, because we definitely have to keep the external
892 // function, but may be able to drop the local one.
893 return !F->hasLocalLinkage();
894 }
895 // Impose a total order (by name) on the replacement of functions. This is
896 // important when operating on more than one module independently to prevent
897 // cycles of thunks calling each other when the modules are linked together.
898 return F->getName() <= G->getName();
899}
900
901// Insert a ComparableFunction into the FnTree, or merge it away if equal to one
902// that was already inserted.
903bool MergeFunctions::insert(Function *NewFunction) {
904 std::pair<FnTreeType::iterator, bool> Result =
905 FnTree.insert(FunctionNode(NewFunction));
906
907 if (Result.second) {
908 assert(FNodesInTree.count(NewFunction) == 0)((void)0);
909 FNodesInTree.insert({NewFunction, Result.first});
910 LLVM_DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName()do { } while (false)
911 << '\n')do { } while (false);
912 return false;
913 }
914
915 const FunctionNode &OldF = *Result.first;
916
917 if (!isFuncOrderCorrect(OldF.getFunc(), NewFunction)) {
918 // Swap the two functions.
919 Function *F = OldF.getFunc();
920 replaceFunctionInTree(*Result.first, NewFunction);
921 NewFunction = F;
922 assert(OldF.getFunc() != F && "Must have swapped the functions.")((void)0);
923 }
924
925 LLVM_DEBUG(dbgs() << " " << OldF.getFunc()->getName()do { } while (false)
926 << " == " << NewFunction->getName() << '\n')do { } while (false);
927
928 Function *DeleteF = NewFunction;
929 mergeTwoFunctions(OldF.getFunc(), DeleteF);
930 return true;
931}
932
933// Remove a function from FnTree. If it was already in FnTree, add
934// it to Deferred so that we'll look at it in the next round.
935void MergeFunctions::remove(Function *F) {
936 auto I = FNodesInTree.find(F);
937 if (I != FNodesInTree.end()) {
938 LLVM_DEBUG(dbgs() << "Deferred " << F->getName() << ".\n")do { } while (false);
939 FnTree.erase(I->second);
940 // I->second has been invalidated, remove it from the FNodesInTree map to
941 // preserve the invariant.
942 FNodesInTree.erase(I);
943 Deferred.emplace_back(F);
944 }
945}
946
947// For each instruction used by the value, remove() the function that contains
948// the instruction. This should happen right before a call to RAUW.
949void MergeFunctions::removeUsers(Value *V) {
950 for (User *U : V->users())
951 if (auto *I = dyn_cast<Instruction>(U))
952 remove(I->getFunction());
953}