Bug Summary

File:llvm/lib/Transforms/IPO/MergeFunctions.cpp
Warning:line 691, column 23
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Transforms/IPO -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Transforms/IPO -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/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/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/InitializePasses.h"
119#include "llvm/Pass.h"
120#include "llvm/Support/Casting.h"
121#include "llvm/Support/CommandLine.h"
122#include "llvm/Support/Debug.h"
123#include "llvm/Support/raw_ostream.h"
124#include "llvm/Transforms/IPO.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 : public ModulePass {
200public:
201 static char ID;
202
203 MergeFunctions()
204 : ModulePass(ID), FnTree(FunctionNodeCmp(&GlobalNumbers)) {
205 initializeMergeFunctionsPass(*PassRegistry::getPassRegistry());
206 }
207
208 bool runOnModule(Module &M) override;
209
210private:
211 // The function comparison operator is provided here so that FunctionNodes do
212 // not need to become larger with another pointer.
213 class FunctionNodeCmp {
214 GlobalNumberState* GlobalNumbers;
215
216 public:
217 FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {}
218
219 bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const {
220 // Order first by hashes, then full function comparison.
221 if (LHS.getHash() != RHS.getHash())
222 return LHS.getHash() < RHS.getHash();
223 FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers);
224 return FCmp.compare() == -1;
225 }
226 };
227 using FnTreeType = std::set<FunctionNode, FunctionNodeCmp>;
228
229 GlobalNumberState GlobalNumbers;
230
231 /// A work queue of functions that may have been modified and should be
232 /// analyzed again.
233 std::vector<WeakTrackingVH> Deferred;
234
235#ifndef NDEBUG
236 /// Checks the rules of order relation introduced among functions set.
237 /// Returns true, if sanity check has been passed, and false if failed.
238 bool doSanityCheck(std::vector<WeakTrackingVH> &Worklist);
239#endif
240
241 /// Insert a ComparableFunction into the FnTree, or merge it away if it's
242 /// equal to one that's already present.
243 bool insert(Function *NewFunction);
244
245 /// Remove a Function from the FnTree and queue it up for a second sweep of
246 /// analysis.
247 void remove(Function *F);
248
249 /// Find the functions that use this Value and remove them from FnTree and
250 /// queue the functions.
251 void removeUsers(Value *V);
252
253 /// Replace all direct calls of Old with calls of New. Will bitcast New if
254 /// necessary to make types match.
255 void replaceDirectCallers(Function *Old, Function *New);
256
257 /// Merge two equivalent functions. Upon completion, G may be deleted, or may
258 /// be converted into a thunk. In either case, it should never be visited
259 /// again.
260 void mergeTwoFunctions(Function *F, Function *G);
261
262 /// Fill PDIUnrelatedWL with instructions from the entry block that are
263 /// unrelated to parameter related debug info.
264 void filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock,
265 std::vector<Instruction *> &PDIUnrelatedWL);
266
267 /// Erase the rest of the CFG (i.e. barring the entry block).
268 void eraseTail(Function *G);
269
270 /// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
271 /// parameter debug info, from the entry block.
272 void eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL);
273
274 /// Replace G with a simple tail call to bitcast(F). Also (unless
275 /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
276 /// delete G.
277 void writeThunk(Function *F, Function *G);
278
279 // Replace G with an alias to F (deleting function G)
280 void writeAlias(Function *F, Function *G);
281
282 // Replace G with an alias to F if possible, or a thunk to F if possible.
283 // Returns false if neither is the case.
284 bool writeThunkOrAlias(Function *F, Function *G);
285
286 /// Replace function F with function G in the function tree.
287 void replaceFunctionInTree(const FunctionNode &FN, Function *G);
288
289 /// The set of all distinct functions. Use the insert() and remove() methods
290 /// to modify it. The map allows efficient lookup and deferring of Functions.
291 FnTreeType FnTree;
292
293 // Map functions to the iterators of the FunctionNode which contains them
294 // in the FnTree. This must be updated carefully whenever the FnTree is
295 // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid
296 // dangling iterators into FnTree. The invariant that preserves this is that
297 // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
298 DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;
299};
300
301} // end anonymous namespace
302
303char MergeFunctions::ID = 0;
304
305INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false)static void *initializeMergeFunctionsPassOnce(PassRegistry &
Registry) { PassInfo *PI = new PassInfo( "Merge Functions", "mergefunc"
, &MergeFunctions::ID, PassInfo::NormalCtor_t(callDefaultCtor
<MergeFunctions>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeMergeFunctionsPassFlag
; void llvm::initializeMergeFunctionsPass(PassRegistry &Registry
) { llvm::call_once(InitializeMergeFunctionsPassFlag, initializeMergeFunctionsPassOnce
, std::ref(Registry)); }
306
307ModulePass *llvm::createMergeFunctionsPass() {
308 return new MergeFunctions();
309}
310
311#ifndef NDEBUG
312bool MergeFunctions::doSanityCheck(std::vector<WeakTrackingVH> &Worklist) {
313 if (const unsigned Max = NumFunctionsForSanityCheck) {
314 unsigned TripleNumber = 0;
315 bool Valid = true;
316
317 dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n";
318
319 unsigned i = 0;
320 for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(),
321 E = Worklist.end();
322 I != E && i < Max; ++I, ++i) {
323 unsigned j = i;
324 for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max;
325 ++J, ++j) {
326 Function *F1 = cast<Function>(*I);
327 Function *F2 = cast<Function>(*J);
328 int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare();
329 int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare();
330
331 // If F1 <= F2, then F2 >= F1, otherwise report failure.
332 if (Res1 != -Res2) {
333 dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber
334 << "\n";
335 dbgs() << *F1 << '\n' << *F2 << '\n';
336 Valid = false;
337 }
338
339 if (Res1 == 0)
340 continue;
341
342 unsigned k = j;
343 for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max;
344 ++k, ++K, ++TripleNumber) {
345 if (K == J)
346 continue;
347
348 Function *F3 = cast<Function>(*K);
349 int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare();
350 int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare();
351
352 bool Transitive = true;
353
354 if (Res1 != 0 && Res1 == Res4) {
355 // F1 > F2, F2 > F3 => F1 > F3
356 Transitive = Res3 == Res1;
357 } else if (Res3 != 0 && Res3 == -Res4) {
358 // F1 > F3, F3 > F2 => F1 > F2
359 Transitive = Res3 == Res1;
360 } else if (Res4 != 0 && -Res3 == Res4) {
361 // F2 > F3, F3 > F1 => F2 > F1
362 Transitive = Res4 == -Res1;
363 }
364
365 if (!Transitive) {
366 dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: "
367 << TripleNumber << "\n";
368 dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", "
369 << Res4 << "\n";
370 dbgs() << *F1 << '\n' << *F2 << '\n' << *F3 << '\n';
371 Valid = false;
372 }
373 }
374 }
375 }
376
377 dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n";
378 return Valid;
379 }
380 return true;
381}
382#endif
383
384/// Check whether \p F is eligible for function merging.
385static bool isEligibleForMerging(Function &F) {
386 return !F.isDeclaration() && !F.hasAvailableExternallyLinkage();
387}
388
389bool MergeFunctions::runOnModule(Module &M) {
390 if (skipModule(M))
1
Assuming the condition is false
2
Taking false branch
391 return false;
392
393 bool Changed = false;
394
395 // All functions in the module, ordered by hash. Functions with a unique
396 // hash value are easily eliminated.
397 std::vector<std::pair<FunctionComparator::FunctionHash, Function *>>
398 HashedFuncs;
399 for (Function &Func : M) {
400 if (isEligibleForMerging(Func)) {
401 HashedFuncs.push_back({FunctionComparator::functionHash(Func), &Func});
402 }
403 }
404
405 llvm::stable_sort(HashedFuncs, less_first());
406
407 auto S = HashedFuncs.begin();
408 for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) {
3
Loop condition is false. Execution continues on line 418
409 // If the hash value matches the previous value or the next one, we must
410 // consider merging it. Otherwise it is dropped and never considered again.
411 if ((I != S && std::prev(I)->first == I->first) ||
412 (std::next(I) != IE && std::next(I)->first == I->first) ) {
413 Deferred.push_back(WeakTrackingVH(I->second));
414 }
415 }
416
417 do {
418 std::vector<WeakTrackingVH> Worklist;
419 Deferred.swap(Worklist);
420
421 LLVM_DEBUG(doSanityCheck(Worklist))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { doSanityCheck(Worklist); } } while (false)
;
4
Assuming 'DebugFlag' is false
5
Loop condition is false. Exiting loop
422
423 LLVM_DEBUG
5.1
'DebugFlag' is false
(dbgs() << "size of module: " << M.size() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "size of module: " << M
.size() << '\n'; } } while (false)
;
6
Loop condition is false. Exiting loop
424 LLVM_DEBUG
6.1
'DebugFlag' is false
(dbgs() << "size of worklist: " << Worklist.size() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "size of worklist: " <<
Worklist.size() << '\n'; } } while (false)
;
7
Loop condition is false. Exiting loop
425
426 // Insert functions and merge them.
427 for (WeakTrackingVH &I : Worklist) {
428 if (!I)
8
Assuming the condition is false
9
Taking false branch
429 continue;
430 Function *F = cast<Function>(I);
431 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {
10
Assuming the condition is true
11
Taking true branch
432 Changed |= insert(F);
12
Calling 'MergeFunctions::insert'
433 }
434 }
435 LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "size of FnTree: " << FnTree
.size() << '\n'; } } while (false)
;
436 } while (!Deferred.empty());
437
438 FnTree.clear();
439 FNodesInTree.clear();
440 GlobalNumbers.clear();
441
442 return Changed;
443}
444
445// Replace direct callers of Old with New.
446void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
447 Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType());
448 for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) {
449 Use *U = &*UI;
450 ++UI;
451 CallSite CS(U->getUser());
452 if (CS && CS.isCallee(U)) {
453 // Do not copy attributes from the called function to the call-site.
454 // Function comparison ensures that the attributes are the same up to
455 // type congruences in byval(), in which case we need to keep the byval
456 // type of the call-site, not the callee function.
457 remove(CS.getInstruction()->getFunction());
458 U->set(BitcastNew);
459 }
460 }
461}
462
463// Helper for writeThunk,
464// Selects proper bitcast operation,
465// but a bit simpler then CastInst::getCastOpcode.
466static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) {
467 Type *SrcTy = V->getType();
468 if (SrcTy->isStructTy()) {
469 assert(DestTy->isStructTy())((DestTy->isStructTy()) ? static_cast<void> (0) : __assert_fail
("DestTy->isStructTy()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 469, __PRETTY_FUNCTION__))
;
470 assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements())((SrcTy->getStructNumElements() == DestTy->getStructNumElements
()) ? static_cast<void> (0) : __assert_fail ("SrcTy->getStructNumElements() == DestTy->getStructNumElements()"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 470, __PRETTY_FUNCTION__))
;
471 Value *Result = UndefValue::get(DestTy);
472 for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) {
473 Value *Element = createCast(
474 Builder, Builder.CreateExtractValue(V, makeArrayRef(I)),
475 DestTy->getStructElementType(I));
476
477 Result =
478 Builder.CreateInsertValue(Result, Element, makeArrayRef(I));
479 }
480 return Result;
481 }
482 assert(!DestTy->isStructTy())((!DestTy->isStructTy()) ? static_cast<void> (0) : __assert_fail
("!DestTy->isStructTy()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 482, __PRETTY_FUNCTION__))
;
483 if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
484 return Builder.CreateIntToPtr(V, DestTy);
485 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
486 return Builder.CreatePtrToInt(V, DestTy);
487 else
488 return Builder.CreateBitCast(V, DestTy);
489}
490
491// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
492// parameter debug info, from the entry block.
493void MergeFunctions::eraseInstsUnrelatedToPDI(
494 std::vector<Instruction *> &PDIUnrelatedWL) {
495 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Erasing instructions (in reverse order of appearance in "
"entry block) unrelated to parameter debug info from entry "
"block: {\n"; } } while (false)
496 dbgs() << " Erasing instructions (in reverse order of appearance in "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Erasing instructions (in reverse order of appearance in "
"entry block) unrelated to parameter debug info from entry "
"block: {\n"; } } while (false)
497 "entry block) unrelated to parameter debug info from entry "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Erasing instructions (in reverse order of appearance in "
"entry block) unrelated to parameter debug info from entry "
"block: {\n"; } } while (false)
498 "block: {\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Erasing instructions (in reverse order of appearance in "
"entry block) unrelated to parameter debug info from entry "
"block: {\n"; } } while (false)
;
499 while (!PDIUnrelatedWL.empty()) {
500 Instruction *I = PDIUnrelatedWL.back();
501 LLVM_DEBUG(dbgs() << " Deleting Instruction: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Deleting Instruction: "; }
} while (false)
;
502 LLVM_DEBUG(I->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { I->print(dbgs()); } } while (false)
;
503 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
504 I->eraseFromParent();
505 PDIUnrelatedWL.pop_back();
506 }
507 LLVM_DEBUG(dbgs() << " } // Done erasing instructions unrelated to parameter "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " } // Done erasing instructions unrelated to parameter "
"debug info from entry block. \n"; } } while (false)
508 "debug info from entry block. \n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " } // Done erasing instructions unrelated to parameter "
"debug info from entry block. \n"; } } while (false)
;
509}
510
511// Reduce G to its entry block.
512void MergeFunctions::eraseTail(Function *G) {
513 std::vector<BasicBlock *> WorklistBB;
514 for (Function::iterator BBI = std::next(G->begin()), BBE = G->end();
515 BBI != BBE; ++BBI) {
516 BBI->dropAllReferences();
517 WorklistBB.push_back(&*BBI);
518 }
519 while (!WorklistBB.empty()) {
520 BasicBlock *BB = WorklistBB.back();
521 BB->eraseFromParent();
522 WorklistBB.pop_back();
523 }
524}
525
526// We are interested in the following instructions from the entry block as being
527// related to parameter debug info:
528// - @llvm.dbg.declare
529// - stores from the incoming parameters to locations on the stack-frame
530// - allocas that create these locations on the stack-frame
531// - @llvm.dbg.value
532// - the entry block's terminator
533// The rest are unrelated to debug info for the parameters; fill up
534// PDIUnrelatedWL with such instructions.
535void MergeFunctions::filterInstsUnrelatedToPDI(
536 BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL) {
537 std::set<Instruction *> PDIRelated;
538 for (BasicBlock::iterator BI = GEntryBlock->begin(), BIE = GEntryBlock->end();
539 BI != BIE; ++BI) {
540 if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) {
541 LLVM_DEBUG(dbgs() << " Deciding: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Deciding: "; } } while (false
)
;
542 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
543 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
544 DILocalVariable *DILocVar = DVI->getVariable();
545 if (DILocVar->isParameter()) {
546 LLVM_DEBUG(dbgs() << " Include (parameter): ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Include (parameter): "; }
} while (false)
;
547 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
548 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
549 PDIRelated.insert(&*BI);
550 } else {
551 LLVM_DEBUG(dbgs() << " Delete (!parameter): ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Delete (!parameter): "; }
} while (false)
;
552 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
553 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
554 }
555 } else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) {
556 LLVM_DEBUG(dbgs() << " Deciding: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Deciding: "; } } while (false
)
;
557 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
558 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
559 DILocalVariable *DILocVar = DDI->getVariable();
560 if (DILocVar->isParameter()) {
561 LLVM_DEBUG(dbgs() << " Parameter: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Parameter: "; } } while (
false)
;
562 LLVM_DEBUG(DILocVar->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { DILocVar->print(dbgs()); } } while (false
)
;
563 AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
564 if (AI) {
565 LLVM_DEBUG(dbgs() << " Processing alloca users: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Processing alloca users: "
; } } while (false)
;
566 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
567 for (User *U : AI->users()) {
568 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
569 if (Value *Arg = SI->getValueOperand()) {
570 if (dyn_cast<Argument>(Arg)) {
571 LLVM_DEBUG(dbgs() << " Include: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Include: "; } } while (false
)
;
572 LLVM_DEBUG(AI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { AI->print(dbgs()); } } while (false)
;
573 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
574 PDIRelated.insert(AI);
575 LLVM_DEBUG(dbgs() << " Include (parameter): ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Include (parameter): "; }
} while (false)
;
576 LLVM_DEBUG(SI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { SI->print(dbgs()); } } while (false)
;
577 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
578 PDIRelated.insert(SI);
579 LLVM_DEBUG(dbgs() << " Include: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Include: "; } } while (false
)
;
580 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
581 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
582 PDIRelated.insert(&*BI);
583 } else {
584 LLVM_DEBUG(dbgs() << " Delete (!parameter): ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Delete (!parameter): "; }
} while (false)
;
585 LLVM_DEBUG(SI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { SI->print(dbgs()); } } while (false)
;
586 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
587 }
588 }
589 } else {
590 LLVM_DEBUG(dbgs() << " Defer: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Defer: "; } } while (false
)
;
591 LLVM_DEBUG(U->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { U->print(dbgs()); } } while (false)
;
592 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
593 }
594 }
595 } else {
596 LLVM_DEBUG(dbgs() << " Delete (alloca NULL): ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Delete (alloca NULL): "; }
} while (false)
;
597 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
598 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
599 }
600 } else {
601 LLVM_DEBUG(dbgs() << " Delete (!parameter): ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Delete (!parameter): "; }
} while (false)
;
602 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
603 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
604 }
605 } else if (BI->isTerminator() && &*BI == GEntryBlock->getTerminator()) {
606 LLVM_DEBUG(dbgs() << " Will Include Terminator: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Will Include Terminator: "
; } } while (false)
;
607 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
608 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
609 PDIRelated.insert(&*BI);
610 } else {
611 LLVM_DEBUG(dbgs() << " Defer: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Defer: "; } } while (false
)
;
612 LLVM_DEBUG(BI->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { BI->print(dbgs()); } } while (false)
;
613 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
614 }
615 }
616 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Report parameter debug info related/related instructions: {\n"
; } } while (false)
617 dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Report parameter debug info related/related instructions: {\n"
; } } while (false)
618 << " Report parameter debug info related/related instructions: {\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " Report parameter debug info related/related instructions: {\n"
; } } while (false)
;
619 for (BasicBlock::iterator BI = GEntryBlock->begin(), BE = GEntryBlock->end();
620 BI != BE; ++BI) {
621
622 Instruction *I = &*BI;
623 if (PDIRelated.find(I) == PDIRelated.end()) {
624 LLVM_DEBUG(dbgs() << " !PDIRelated: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " !PDIRelated: "; } } while
(false)
;
625 LLVM_DEBUG(I->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { I->print(dbgs()); } } while (false)
;
626 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
627 PDIUnrelatedWL.push_back(I);
628 } else {
629 LLVM_DEBUG(dbgs() << " PDIRelated: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " PDIRelated: "; } } while
(false)
;
630 LLVM_DEBUG(I->print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { I->print(dbgs()); } } while (false)
;
631 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "\n"; } } while (false)
;
632 }
633 }
634 LLVM_DEBUG(dbgs() << " }\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " }\n"; } } while (false)
;
635}
636
637/// Whether this function may be replaced by a forwarding thunk.
638static bool canCreateThunkFor(Function *F) {
639 if (F->isVarArg())
640 return false;
641
642 // Don't merge tiny functions using a thunk, since it can just end up
643 // making the function larger.
644 if (F->size() == 1) {
645 if (F->front().size() <= 2) {
646 LLVM_DEBUG(dbgs() << "canCreateThunkFor: " << F->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "canCreateThunkFor: " <<
F->getName() << " is too small to bother creating a thunk for\n"
; } } while (false)
647 << " is too small to bother creating a thunk for\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "canCreateThunkFor: " <<
F->getName() << " is too small to bother creating a thunk for\n"
; } } while (false)
;
648 return false;
649 }
650 }
651 return true;
652}
653
654// Replace G with a simple tail call to bitcast(F). Also (unless
655// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
656// delete G. Under MergeFunctionsPDI, we use G itself for creating
657// the thunk as we preserve the debug info (and associated instructions)
658// from G's entry block pertaining to G's incoming arguments which are
659// passed on as corresponding arguments in the call that G makes to F.
660// For better debugability, under MergeFunctionsPDI, we do not modify G's
661// call sites to point to F even when within the same translation unit.
662void MergeFunctions::writeThunk(Function *F, Function *G) {
663 BasicBlock *GEntryBlock = nullptr;
664 std::vector<Instruction *> PDIUnrelatedWL;
665 BasicBlock *BB = nullptr;
666 Function *NewG = nullptr;
27
'NewG' initialized to a null pointer value
667 if (MergeFunctionsPDI) {
28
Assuming the condition is true
29
Taking true branch
668 LLVM_DEBUG(dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new "
"function as thunk; retain original: " << G->getName
() << "()\n"; } } while (false)
30
Assuming 'DebugFlag' is false
31
Loop condition is false. Exiting loop
669 "function as thunk; retain original: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new "
"function as thunk; retain original: " << G->getName
() << "()\n"; } } while (false)
670 << G->getName() << "()\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new "
"function as thunk; retain original: " << G->getName
() << "()\n"; } } while (false)
;
671 GEntryBlock = &G->getEntryBlock();
672 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "
"debug info for " << G->getName() << "() {\n"
; } } while (false)
32
Assuming 'DebugFlag' is false
33
Loop condition is false. Exiting loop
673 dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "
"debug info for " << G->getName() << "() {\n"
; } } while (false)
674 "debug info for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "
"debug info for " << G->getName() << "() {\n"
; } } while (false)
675 << G->getName() << "() {\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related "
"debug info for " << G->getName() << "() {\n"
; } } while (false)
;
676 filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL);
677 GEntryBlock->getTerminator()->eraseFromParent();
678 BB = GEntryBlock;
679 } else {
680 NewG = Function::Create(G->getFunctionType(), G->getLinkage(),
681 G->getAddressSpace(), "", G->getParent());
682 NewG->setComdat(G->getComdat());
683 BB = BasicBlock::Create(F->getContext(), "", NewG);
684 }
685
686 IRBuilder<> Builder(BB);
687 Function *H = MergeFunctionsPDI ? G : NewG;
34
Assuming the condition is false
35
'?' condition is false
36
'H' initialized to a null pointer value
688 SmallVector<Value *, 16> Args;
689 unsigned i = 0;
690 FunctionType *FFTy = F->getFunctionType();
691 for (Argument &AI : H->args()) {
37
Called C++ object pointer is null
692 Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i)));
693 ++i;
694 }
695
696 CallInst *CI = Builder.CreateCall(F, Args);
697 ReturnInst *RI = nullptr;
698 CI->setTailCall();
699 CI->setCallingConv(F->getCallingConv());
700 CI->setAttributes(F->getAttributes());
701 if (H->getReturnType()->isVoidTy()) {
702 RI = Builder.CreateRetVoid();
703 } else {
704 RI = Builder.CreateRet(createCast(Builder, CI, H->getReturnType()));
705 }
706
707 if (MergeFunctionsPDI) {
708 DISubprogram *DIS = G->getSubprogram();
709 if (DIS) {
710 DebugLoc CIDbgLoc = DebugLoc::get(DIS->getScopeLine(), 0, DIS);
711 DebugLoc RIDbgLoc = DebugLoc::get(DIS->getScopeLine(), 0, DIS);
712 CI->setDebugLoc(CIDbgLoc);
713 RI->setDebugLoc(RIDbgLoc);
714 } else {
715 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for "
<< G->getName() << "()\n"; } } while (false)
716 dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for "
<< G->getName() << "()\n"; } } while (false)
717 << G->getName() << "()\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for "
<< G->getName() << "()\n"; } } while (false)
;
718 }
719 eraseTail(G);
720 eraseInstsUnrelatedToPDI(PDIUnrelatedWL);
721 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "} // End of parameter related debug info filtering for: "
<< G->getName() << "()\n"; } } while (false)
722 dbgs() << "} // End of parameter related debug info filtering for: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "} // End of parameter related debug info filtering for: "
<< G->getName() << "()\n"; } } while (false)
723 << G->getName() << "()\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "} // End of parameter related debug info filtering for: "
<< G->getName() << "()\n"; } } while (false)
;
724 } else {
725 NewG->copyAttributesFrom(G);
726 NewG->takeName(G);
727 removeUsers(G);
728 G->replaceAllUsesWith(NewG);
729 G->eraseFromParent();
730 }
731
732 LLVM_DEBUG(dbgs() << "writeThunk: " << H->getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeThunk: " << H->
getName() << '\n'; } } while (false)
;
733 ++NumThunksWritten;
734}
735
736// Whether this function may be replaced by an alias
737static bool canCreateAliasFor(Function *F) {
738 if (!MergeFunctionsAliases || !F->hasGlobalUnnamedAddr())
739 return false;
740
741 // We should only see linkages supported by aliases here
742 assert(F->hasLocalLinkage() || F->hasExternalLinkage()((F->hasLocalLinkage() || F->hasExternalLinkage() || F->
hasWeakLinkage() || F->hasLinkOnceLinkage()) ? static_cast
<void> (0) : __assert_fail ("F->hasLocalLinkage() || F->hasExternalLinkage() || F->hasWeakLinkage() || F->hasLinkOnceLinkage()"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 743, __PRETTY_FUNCTION__))
743 || F->hasWeakLinkage() || F->hasLinkOnceLinkage())((F->hasLocalLinkage() || F->hasExternalLinkage() || F->
hasWeakLinkage() || F->hasLinkOnceLinkage()) ? static_cast
<void> (0) : __assert_fail ("F->hasLocalLinkage() || F->hasExternalLinkage() || F->hasWeakLinkage() || F->hasLinkOnceLinkage()"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 743, __PRETTY_FUNCTION__))
;
744 return true;
745}
746
747// Replace G with an alias to F (deleting function G)
748void MergeFunctions::writeAlias(Function *F, Function *G) {
749 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
750 PointerType *PtrType = G->getType();
751 auto *GA = GlobalAlias::create(
752 PtrType->getElementType(), PtrType->getAddressSpace(),
753 G->getLinkage(), "", BitcastF, G->getParent());
754
755 F->setAlignment(MaybeAlign(std::max(F->getAlignment(), G->getAlignment())));
756 GA->takeName(G);
757 GA->setVisibility(G->getVisibility());
758 GA->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
759
760 removeUsers(G);
761 G->replaceAllUsesWith(GA);
762 G->eraseFromParent();
763
764 LLVM_DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "writeAlias: " << GA->
getName() << '\n'; } } while (false)
;
765 ++NumAliasesWritten;
766}
767
768// Replace G with an alias to F if possible, or a thunk to F if
769// profitable. Returns false if neither is the case.
770bool MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {
771 if (canCreateAliasFor(G)) {
24
Taking false branch
772 writeAlias(F, G);
773 return true;
774 }
775 if (canCreateThunkFor(F)) {
25
Taking true branch
776 writeThunk(F, G);
26
Calling 'MergeFunctions::writeThunk'
777 return true;
778 }
779 return false;
780}
781
782// Merge two equivalent functions. Upon completion, Function G is deleted.
783void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
784 if (F->isInterposable()) {
19
Taking false branch
785 assert(G->isInterposable())((G->isInterposable()) ? static_cast<void> (0) : __assert_fail
("G->isInterposable()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 785, __PRETTY_FUNCTION__))
;
786
787 // Both writeThunkOrAlias() calls below must succeed, either because we can
788 // create aliases for G and NewF, or because a thunk for F is profitable.
789 // F here has the same signature as NewF below, so that's what we check.
790 if (!canCreateThunkFor(F) &&
791 (!canCreateAliasFor(F) || !canCreateAliasFor(G)))
792 return;
793
794 // Make them both thunks to the same internal function.
795 Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(),
796 F->getAddressSpace(), "", F->getParent());
797 NewF->copyAttributesFrom(F);
798 NewF->takeName(F);
799 removeUsers(F);
800 F->replaceAllUsesWith(NewF);
801
802 MaybeAlign MaxAlignment(std::max(G->getAlignment(), NewF->getAlignment()));
803
804 writeThunkOrAlias(F, G);
805 writeThunkOrAlias(F, NewF);
806
807 F->setAlignment(MaxAlignment);
808 F->setLinkage(GlobalValue::PrivateLinkage);
809 ++NumDoubleWeak;
810 ++NumFunctionsMerged;
811 } else {
812 // For better debugability, under MergeFunctionsPDI, we do not modify G's
813 // call sites to point to F even when within the same translation unit.
814 if (!G->isInterposable() && !MergeFunctionsPDI) {
20
Assuming the condition is false
21
Taking false branch
815 if (G->hasGlobalUnnamedAddr()) {
816 // G might have been a key in our GlobalNumberState, and it's illegal
817 // to replace a key in ValueMap<GlobalValue *> with a non-global.
818 GlobalNumbers.erase(G);
819 // If G's address is not significant, replace it entirely.
820 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
821 removeUsers(G);
822 G->replaceAllUsesWith(BitcastF);
823 } else {
824 // Redirect direct callers of G to F. (See note on MergeFunctionsPDI
825 // above).
826 replaceDirectCallers(G, F);
827 }
828 }
829
830 // If G was internal then we may have replaced all uses of G with F. If so,
831 // stop here and delete G. There's no need for a thunk. (See note on
832 // MergeFunctionsPDI above).
833 if (G->isDiscardableIfUnused() && G->use_empty() && !MergeFunctionsPDI) {
22
Assuming the condition is false
834 G->eraseFromParent();
835 ++NumFunctionsMerged;
836 return;
837 }
838
839 if (writeThunkOrAlias(F, G)) {
23
Calling 'MergeFunctions::writeThunkOrAlias'
840 ++NumFunctionsMerged;
841 }
842 }
843}
844
845/// Replace function F by function G.
846void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN,
847 Function *G) {
848 Function *F = FN.getFunc();
849 assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 &&((FunctionComparator(F, G, &GlobalNumbers).compare() == 0
&& "The two functions must be equal") ? static_cast<
void> (0) : __assert_fail ("FunctionComparator(F, G, &GlobalNumbers).compare() == 0 && \"The two functions must be equal\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 850, __PRETTY_FUNCTION__))
850 "The two functions must be equal")((FunctionComparator(F, G, &GlobalNumbers).compare() == 0
&& "The two functions must be equal") ? static_cast<
void> (0) : __assert_fail ("FunctionComparator(F, G, &GlobalNumbers).compare() == 0 && \"The two functions must be equal\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 850, __PRETTY_FUNCTION__))
;
851
852 auto I = FNodesInTree.find(F);
853 assert(I != FNodesInTree.end() && "F should be in FNodesInTree")((I != FNodesInTree.end() && "F should be in FNodesInTree"
) ? static_cast<void> (0) : __assert_fail ("I != FNodesInTree.end() && \"F should be in FNodesInTree\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 853, __PRETTY_FUNCTION__))
;
854 assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G")((FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G"
) ? static_cast<void> (0) : __assert_fail ("FNodesInTree.count(G) == 0 && \"FNodesInTree should not contain G\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 854, __PRETTY_FUNCTION__))
;
855
856 FnTreeType::iterator IterToFNInFnTree = I->second;
857 assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree.")((&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree."
) ? static_cast<void> (0) : __assert_fail ("&(*IterToFNInFnTree) == &FN && \"F should map to FN in FNodesInTree.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 857, __PRETTY_FUNCTION__))
;
858 // Remove F -> FN and insert G -> FN
859 FNodesInTree.erase(I);
860 FNodesInTree.insert({G, IterToFNInFnTree});
861 // Replace F with G in FN, which is stored inside the FnTree.
862 FN.replaceBy(G);
863}
864
865// Ordering for functions that are equal under FunctionComparator
866static bool isFuncOrderCorrect(const Function *F, const Function *G) {
867 if (F->isInterposable() != G->isInterposable()) {
868 // Strong before weak, because the weak function may call the strong
869 // one, but not the other way around.
870 return !F->isInterposable();
871 }
872 if (F->hasLocalLinkage() != G->hasLocalLinkage()) {
873 // External before local, because we definitely have to keep the external
874 // function, but may be able to drop the local one.
875 return !F->hasLocalLinkage();
876 }
877 // Impose a total order (by name) on the replacement of functions. This is
878 // important when operating on more than one module independently to prevent
879 // cycles of thunks calling each other when the modules are linked together.
880 return F->getName() <= G->getName();
881}
882
883// Insert a ComparableFunction into the FnTree, or merge it away if equal to one
884// that was already inserted.
885bool MergeFunctions::insert(Function *NewFunction) {
886 std::pair<FnTreeType::iterator, bool> Result =
887 FnTree.insert(FunctionNode(NewFunction));
888
889 if (Result.second) {
13
Assuming field 'second' is false
14
Taking false branch
890 assert(FNodesInTree.count(NewFunction) == 0)((FNodesInTree.count(NewFunction) == 0) ? static_cast<void
> (0) : __assert_fail ("FNodesInTree.count(NewFunction) == 0"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 890, __PRETTY_FUNCTION__))
;
891 FNodesInTree.insert({NewFunction, Result.first});
892 LLVM_DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "Inserting as unique: " <<
NewFunction->getName() << '\n'; } } while (false)
893 << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "Inserting as unique: " <<
NewFunction->getName() << '\n'; } } while (false)
;
894 return false;
895 }
896
897 const FunctionNode &OldF = *Result.first;
898
899 if (!isFuncOrderCorrect(OldF.getFunc(), NewFunction)) {
15
Taking false branch
900 // Swap the two functions.
901 Function *F = OldF.getFunc();
902 replaceFunctionInTree(*Result.first, NewFunction);
903 NewFunction = F;
904 assert(OldF.getFunc() != F && "Must have swapped the functions.")((OldF.getFunc() != F && "Must have swapped the functions."
) ? static_cast<void> (0) : __assert_fail ("OldF.getFunc() != F && \"Must have swapped the functions.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/IPO/MergeFunctions.cpp"
, 904, __PRETTY_FUNCTION__))
;
905 }
906
907 LLVM_DEBUG(dbgs() << " " << OldF.getFunc()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " " << OldF.getFunc()
->getName() << " == " << NewFunction->getName
() << '\n'; } } while (false)
16
Assuming 'DebugFlag' is false
17
Loop condition is false. Exiting loop
908 << " == " << NewFunction->getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << " " << OldF.getFunc()
->getName() << " == " << NewFunction->getName
() << '\n'; } } while (false)
;
909
910 Function *DeleteF = NewFunction;
911 mergeTwoFunctions(OldF.getFunc(), DeleteF);
18
Calling 'MergeFunctions::mergeTwoFunctions'
912 return true;
913}
914
915// Remove a function from FnTree. If it was already in FnTree, add
916// it to Deferred so that we'll look at it in the next round.
917void MergeFunctions::remove(Function *F) {
918 auto I = FNodesInTree.find(F);
919 if (I != FNodesInTree.end()) {
920 LLVM_DEBUG(dbgs() << "Deferred " << F->getName() << ".\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mergefunc")) { dbgs() << "Deferred " << F->getName
() << ".\n"; } } while (false)
;
921 FnTree.erase(I->second);
922 // I->second has been invalidated, remove it from the FNodesInTree map to
923 // preserve the invariant.
924 FNodesInTree.erase(I);
925 Deferred.emplace_back(F);
926 }
927}
928
929// For each instruction used by the value, remove() the function that contains
930// the instruction. This should happen right before a call to RAUW.
931void MergeFunctions::removeUsers(Value *V) {
932 for (User *U : V->users())
933 if (auto *I = dyn_cast<Instruction>(U))
934 remove(I->getFunction());
935}