blob: e0bb0eb42b590fcbecd69030effb83a6a9691a80 [file] [log] [blame]
Nick Lewyckyd01d42e2008-11-02 05:52:50 +00001//===- MergeFunctions.cpp - Merge identical functions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass looks for equivalent functions that are mergable and folds them.
11//
Stepan Dyatkovskiy471eab32014-06-22 00:57:09 +000012// Order relation is defined on set of functions. It was made through
13// special function comparison procedure that returns
14// 0 when functions are equal,
15// -1 when Left function is less than right function, and
16// 1 for opposite case. We need total-ordering, so we need to maintain
17// four properties on the functions set:
18// a <= a (reflexivity)
19// if a <= b and b <= a then a = b (antisymmetry)
20// if a <= b and b <= c then a <= c (transitivity).
21// for all a and b: a <= b or b <= a (totality).
Nick Lewyckyd01d42e2008-11-02 05:52:50 +000022//
Stepan Dyatkovskiy471eab32014-06-22 00:57:09 +000023// Comparison iterates through each instruction in each basic block.
24// Functions are kept on binary tree. For each new function F we perform
25// lookup in binary tree.
26// In practice it works the following way:
27// -- We define Function* container class with custom "operator<" (FunctionPtr).
28// -- "FunctionPtr" instances are stored in std::set collection, so every
29// std::set::insert operation will give you result in log(N) time.
JF Bastien5e4303d2015-08-15 01:18:18 +000030//
31// As an optimization, a hash of the function structure is calculated first, and
32// two functions are only compared if they have the same hash. This hash is
33// cheap to compute, and has the property that if function F == G according to
34// the comparison function, then hash(F) == hash(G). This consistency property
35// is critical to ensuring all possible merging opportunities are exploited.
36// Collisions in the hash affect the speed of the pass but not the correctness
37// or determinism of the resulting transformation.
Nick Lewyckyd01d42e2008-11-02 05:52:50 +000038//
Nick Lewyckyd3c6dfe2010-05-13 05:48:45 +000039// When a match is found the functions are folded. If both functions are
40// overridable, we move the functionality into a new internal function and
41// leave two overridable thunks to it.
Nick Lewyckyd01d42e2008-11-02 05:52:50 +000042//
43//===----------------------------------------------------------------------===//
44//
45// Future work:
46//
Nick Lewyckyd01d42e2008-11-02 05:52:50 +000047// * virtual functions.
48//
49// Many functions have their address taken by the virtual function table for
50// the object they belong to. However, as long as it's only used for a lookup
Nick Lewyckyfbd27572010-08-08 05:04:23 +000051// and call, this is irrelevant, and we'd like to fold such functions.
Nick Lewyckyd01d42e2008-11-02 05:52:50 +000052//
Nick Lewyckyfbd27572010-08-08 05:04:23 +000053// * be smarter about bitcasts.
Nick Lewyckyd3c6dfe2010-05-13 05:48:45 +000054//
55// In order to fold functions, we will sometimes add either bitcast instructions
56// or bitcast constant expressions. Unfortunately, this can confound further
57// analysis since the two functions differ where one has a bitcast and the
Nick Lewyckyfbd27572010-08-08 05:04:23 +000058// other doesn't. We should learn to look through bitcasts.
Nick Lewyckyd3c6dfe2010-05-13 05:48:45 +000059//
Stepan Dyatkovskiy471eab32014-06-22 00:57:09 +000060// * Compare complex types with pointer types inside.
61// * Compare cross-reference cases.
62// * Compare complex expressions.
63//
64// All the three issues above could be described as ability to prove that
65// fA == fB == fC == fE == fF == fG in example below:
66//
67// void fA() {
68// fB();
69// }
70// void fB() {
71// fA();
72// }
73//
74// void fE() {
75// fF();
76// }
77// void fF() {
78// fG();
79// }
80// void fG() {
81// fE();
82// }
83//
84// Simplest cross-reference case (fA <--> fB) was implemented in previous
85// versions of MergeFunctions, though it presented only in two function pairs
86// in test-suite (that counts >50k functions)
87// Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A)
88// could cover much more cases.
89//
Nick Lewyckyd01d42e2008-11-02 05:52:50 +000090//===----------------------------------------------------------------------===//
91
Mehdi Aminib550cb12016-04-18 09:17:29 +000092#include "llvm/ADT/Hashing.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000093#include "llvm/ADT/STLExtras.h"
94#include "llvm/ADT/SmallSet.h"
95#include "llvm/ADT/Statistic.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000096#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000097#include "llvm/IR/Constants.h"
98#include "llvm/IR/DataLayout.h"
99#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000100#include "llvm/IR/Instructions.h"
101#include "llvm/IR/LLVMContext.h"
102#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +0000103#include "llvm/IR/ValueHandle.h"
JF Bastien057292a2015-08-21 23:27:24 +0000104#include "llvm/IR/ValueMap.h"
Nick Lewyckyd01d42e2008-11-02 05:52:50 +0000105#include "llvm/Pass.h"
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000106#include "llvm/Support/CommandLine.h"
Nick Lewyckyd01d42e2008-11-02 05:52:50 +0000107#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +0000108#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000109#include "llvm/Support/raw_ostream.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +0000110#include "llvm/Transforms/IPO.h"
Erik Eckstein4d6fb722016-11-11 21:15:13 +0000111#include "llvm/Transforms/Utils/FunctionComparator.h"
Nick Lewycky68984ed2010-08-31 08:29:37 +0000112#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000113
Nick Lewyckyd01d42e2008-11-02 05:52:50 +0000114using namespace llvm;
115
Chandler Carruth964daaa2014-04-22 02:55:47 +0000116#define DEBUG_TYPE "mergefunc"
117
Nick Lewyckyd01d42e2008-11-02 05:52:50 +0000118STATISTIC(NumFunctionsMerged, "Number of functions merged");
Nick Lewycky71972d42010-09-07 01:42:10 +0000119STATISTIC(NumThunksWritten, "Number of thunks generated");
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000120STATISTIC(NumAliasesWritten, "Number of aliases generated");
Nick Lewycky71972d42010-09-07 01:42:10 +0000121STATISTIC(NumDoubleWeak, "Number of new functions created");
Nick Lewyckyd01d42e2008-11-02 05:52:50 +0000122
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000123static cl::opt<unsigned> NumFunctionsForSanityCheck(
124 "mergefunc-sanity",
125 cl::desc("How many functions in module could be used for "
126 "MergeFunctions pass sanity check. "
127 "'0' disables this check. Works only with '-debug' key."),
128 cl::init(0), cl::Hidden);
129
Nick Lewyckyf3a07ec2010-09-05 09:00:32 +0000130namespace {
Nick Lewycky00959372010-09-05 08:22:49 +0000131
Stepan Dyatkovskiyfe134cd2014-09-10 10:08:25 +0000132class FunctionNode {
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000133 mutable AssertingVH<Function> F;
JF Bastien5e4303d2015-08-15 01:18:18 +0000134 FunctionComparator::FunctionHash Hash;
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000135public:
JF Bastien5e4303d2015-08-15 01:18:18 +0000136 // Note the hash is recalculated potentially multiple times, but it is cheap.
JF Bastien057292a2015-08-21 23:27:24 +0000137 FunctionNode(Function *F)
138 : F(F), Hash(FunctionComparator::functionHash(*F)) {}
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000139 Function *getFunc() const { return F; }
JF Bastien057292a2015-08-21 23:27:24 +0000140 FunctionComparator::FunctionHash getHash() const { return Hash; }
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000141
142 /// Replace the reference to the function F by the function G, assuming their
143 /// implementations are equal.
144 void replaceBy(Function *G) const {
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000145 F = G;
146 }
147
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000148 void release() { F = nullptr; }
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000149};
Nick Lewycky564fcca2011-01-28 07:36:21 +0000150
151/// MergeFunctions finds functions which will generate identical machine code,
152/// by considering all pointer types to be equivalent. Once identified,
153/// MergeFunctions will fold them by replacing a call to one to a call to a
154/// bitcast of the other.
155///
156class MergeFunctions : public ModulePass {
157public:
158 static char ID;
159 MergeFunctions()
JF Bastien3a4ad612015-09-02 23:55:23 +0000160 : ModulePass(ID), FnTree(FunctionNodeCmp(&GlobalNumbers)), FNodesInTree(),
JF Bastien057292a2015-08-21 23:27:24 +0000161 HasGlobalAliases(false) {
Nick Lewycky564fcca2011-01-28 07:36:21 +0000162 initializeMergeFunctionsPass(*PassRegistry::getPassRegistry());
163 }
164
Craig Topper3e4c6972014-03-05 09:10:37 +0000165 bool runOnModule(Module &M) override;
Nick Lewycky564fcca2011-01-28 07:36:21 +0000166
167private:
JF Bastien057292a2015-08-21 23:27:24 +0000168 // The function comparison operator is provided here so that FunctionNodes do
169 // not need to become larger with another pointer.
170 class FunctionNodeCmp {
171 GlobalNumberState* GlobalNumbers;
172 public:
173 FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {}
174 bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const {
175 // Order first by hashes, then full function comparison.
176 if (LHS.getHash() != RHS.getHash())
177 return LHS.getHash() < RHS.getHash();
178 FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers);
179 return FCmp.compare() == -1;
180 }
181 };
182 typedef std::set<FunctionNode, FunctionNodeCmp> FnTreeType;
183
184 GlobalNumberState GlobalNumbers;
Nick Lewycky564fcca2011-01-28 07:36:21 +0000185
186 /// A work queue of functions that may have been modified and should be
187 /// analyzed again.
188 std::vector<WeakVH> Deferred;
189
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000190 /// Checks the rules of order relation introduced among functions set.
191 /// Returns true, if sanity check has been passed, and false if failed.
192 bool doSanityCheck(std::vector<WeakVH> &Worklist);
193
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000194 /// Insert a ComparableFunction into the FnTree, or merge it away if it's
Nick Lewycky564fcca2011-01-28 07:36:21 +0000195 /// equal to one that's already present.
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000196 bool insert(Function *NewFunction);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000197
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000198 /// Remove a Function from the FnTree and queue it up for a second sweep of
Nick Lewycky564fcca2011-01-28 07:36:21 +0000199 /// analysis.
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000200 void remove(Function *F);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000201
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000202 /// Find the functions that use this Value and remove them from FnTree and
Nick Lewycky564fcca2011-01-28 07:36:21 +0000203 /// queue the functions.
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000204 void removeUsers(Value *V);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000205
206 /// Replace all direct calls of Old with calls of New. Will bitcast New if
207 /// necessary to make types match.
208 void replaceDirectCallers(Function *Old, Function *New);
209
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000210 /// Merge two equivalent functions. Upon completion, G may be deleted, or may
211 /// be converted into a thunk. In either case, it should never be visited
212 /// again.
213 void mergeTwoFunctions(Function *F, Function *G);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000214
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000215 /// Replace G with a thunk or an alias to F. Deletes G.
216 void writeThunkOrAlias(Function *F, Function *G);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000217
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000218 /// Replace G with a simple tail call to bitcast(F). Also replace direct uses
219 /// of G with bitcast(F). Deletes G.
220 void writeThunk(Function *F, Function *G);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000221
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000222 /// Replace G with an alias to F. Deletes G.
223 void writeAlias(Function *F, Function *G);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000224
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000225 /// Replace function F with function G in the function tree.
JF Bastien3a4ad612015-09-02 23:55:23 +0000226 void replaceFunctionInTree(const FunctionNode &FN, Function *G);
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000227
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000228 /// The set of all distinct functions. Use the insert() and remove() methods
JF Bastien3a4ad612015-09-02 23:55:23 +0000229 /// to modify it. The map allows efficient lookup and deferring of Functions.
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000230 FnTreeType FnTree;
JF Bastien3a4ad612015-09-02 23:55:23 +0000231 // Map functions to the iterators of the FunctionNode which contains them
232 // in the FnTree. This must be updated carefully whenever the FnTree is
233 // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid
234 // dangling iterators into FnTree. The invariant that preserves this is that
235 // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
236 ValueMap<Function*, FnTreeType::iterator> FNodesInTree;
Nick Lewycky564fcca2011-01-28 07:36:21 +0000237
Nick Lewycky564fcca2011-01-28 07:36:21 +0000238 /// Whether or not the target supports global aliases.
239 bool HasGlobalAliases;
240};
241
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000242} // end anonymous namespace
Nick Lewycky564fcca2011-01-28 07:36:21 +0000243
244char MergeFunctions::ID = 0;
245INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false)
246
247ModulePass *llvm::createMergeFunctionsPass() {
248 return new MergeFunctions();
249}
250
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000251bool MergeFunctions::doSanityCheck(std::vector<WeakVH> &Worklist) {
252 if (const unsigned Max = NumFunctionsForSanityCheck) {
253 unsigned TripleNumber = 0;
254 bool Valid = true;
255
256 dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n";
257
258 unsigned i = 0;
259 for (std::vector<WeakVH>::iterator I = Worklist.begin(), E = Worklist.end();
260 I != E && i < Max; ++I, ++i) {
261 unsigned j = i;
262 for (std::vector<WeakVH>::iterator J = I; J != E && j < Max; ++J, ++j) {
263 Function *F1 = cast<Function>(*I);
264 Function *F2 = cast<Function>(*J);
JF Bastien057292a2015-08-21 23:27:24 +0000265 int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare();
266 int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare();
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000267
268 // If F1 <= F2, then F2 >= F1, otherwise report failure.
269 if (Res1 != -Res2) {
270 dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber
271 << "\n";
272 F1->dump();
273 F2->dump();
274 Valid = false;
275 }
276
277 if (Res1 == 0)
278 continue;
279
280 unsigned k = j;
281 for (std::vector<WeakVH>::iterator K = J; K != E && k < Max;
282 ++k, ++K, ++TripleNumber) {
283 if (K == J)
284 continue;
285
286 Function *F3 = cast<Function>(*K);
JF Bastien057292a2015-08-21 23:27:24 +0000287 int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare();
288 int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare();
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000289
290 bool Transitive = true;
291
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000292 if (Res1 != 0 && Res1 == Res4) {
Stepan Dyatkovskiy0b588012014-06-21 19:07:51 +0000293 // F1 > F2, F2 > F3 => F1 > F3
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000294 Transitive = Res3 == Res1;
Stepan Dyatkovskiy0b588012014-06-21 19:07:51 +0000295 } else if (Res3 != 0 && Res3 == -Res4) {
296 // F1 > F3, F3 > F2 => F1 > F2
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000297 Transitive = Res3 == Res1;
Stepan Dyatkovskiy0b588012014-06-21 19:07:51 +0000298 } else if (Res4 != 0 && -Res3 == Res4) {
299 // F2 > F3, F3 > F1 => F2 > F1
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000300 Transitive = Res4 == -Res1;
301 }
302
303 if (!Transitive) {
304 dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: "
305 << TripleNumber << "\n";
306 dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", "
307 << Res4 << "\n";
308 F1->dump();
309 F2->dump();
310 F3->dump();
311 Valid = false;
312 }
313 }
314 }
315 }
316
317 dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n";
318 return Valid;
319 }
320 return true;
321}
322
Nick Lewycky564fcca2011-01-28 07:36:21 +0000323bool MergeFunctions::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000324 if (skipModule(M))
325 return false;
326
Nick Lewycky564fcca2011-01-28 07:36:21 +0000327 bool Changed = false;
Nick Lewycky564fcca2011-01-28 07:36:21 +0000328
JF Bastien5e4303d2015-08-15 01:18:18 +0000329 // All functions in the module, ordered by hash. Functions with a unique
330 // hash value are easily eliminated.
331 std::vector<std::pair<FunctionComparator::FunctionHash, Function *>>
332 HashedFuncs;
333 for (Function &Func : M) {
334 if (!Func.isDeclaration() && !Func.hasAvailableExternallyLinkage()) {
335 HashedFuncs.push_back({FunctionComparator::functionHash(Func), &Func});
336 }
Nick Lewycky564fcca2011-01-28 07:36:21 +0000337 }
Nick Lewycky564fcca2011-01-28 07:36:21 +0000338
NAKAMURA Takumi51962752015-08-16 02:41:23 +0000339 std::stable_sort(
340 HashedFuncs.begin(), HashedFuncs.end(),
341 [](const std::pair<FunctionComparator::FunctionHash, Function *> &a,
342 const std::pair<FunctionComparator::FunctionHash, Function *> &b) {
343 return a.first < b.first;
344 });
JF Bastien5e4303d2015-08-15 01:18:18 +0000345
346 auto S = HashedFuncs.begin();
347 for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) {
348 // If the hash value matches the previous value or the next one, we must
349 // consider merging it. Otherwise it is dropped and never considered again.
350 if ((I != S && std::prev(I)->first == I->first) ||
351 (std::next(I) != IE && std::next(I)->first == I->first) ) {
352 Deferred.push_back(WeakVH(I->second));
353 }
354 }
355
Nick Lewycky564fcca2011-01-28 07:36:21 +0000356 do {
357 std::vector<WeakVH> Worklist;
358 Deferred.swap(Worklist);
359
Stepan Dyatkovskiya77f3d82014-06-21 18:58:11 +0000360 DEBUG(doSanityCheck(Worklist));
361
Nick Lewycky564fcca2011-01-28 07:36:21 +0000362 DEBUG(dbgs() << "size of module: " << M.size() << '\n');
363 DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n');
364
Erik Eckstein0c48dd82016-05-31 17:20:23 +0000365 // Insert functions and merge them.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000366 for (WeakVH &I : Worklist) {
367 if (!I)
368 continue;
369 Function *F = cast<Function>(I);
Erik Eckstein0c48dd82016-05-31 17:20:23 +0000370 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000371 Changed |= insert(F);
Nick Lewycky564fcca2011-01-28 07:36:21 +0000372 }
373 }
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000374 DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n');
Nick Lewycky564fcca2011-01-28 07:36:21 +0000375 } while (!Deferred.empty());
376
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000377 FnTree.clear();
Arnold Schwaighofer0591c5d2015-10-05 17:26:36 +0000378 GlobalNumbers.clear();
Nick Lewycky564fcca2011-01-28 07:36:21 +0000379
380 return Changed;
381}
382
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000383// Replace direct callers of Old with New.
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000384void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
385 Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType());
Chandler Carruthcdf47882014-03-09 03:16:01 +0000386 for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) {
387 Use *U = &*UI;
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000388 ++UI;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000389 CallSite CS(U->getUser());
390 if (CS && CS.isCallee(U)) {
Arnold Schwaighofer36512332015-07-21 17:07:07 +0000391 // Transfer the called function's attributes to the call site. Due to the
JF Bastienfa946232015-09-10 18:08:35 +0000392 // bitcast we will 'lose' ABI changing attributes because the 'called
Arnold Schwaighofer36512332015-07-21 17:07:07 +0000393 // function' is no longer a Function* but the bitcast. Code that looks up
394 // the attributes from the called function will fail.
JF Bastienfa946232015-09-10 18:08:35 +0000395
396 // FIXME: This is not actually true, at least not anymore. The callsite
397 // will always have the same ABI affecting attributes as the callee,
398 // because otherwise the original input has UB. Note that Old and New
399 // always have matching ABI, so no attributes need to be changed.
400 // Transferring other attributes may help other optimizations, but that
401 // should be done uniformly and not in this ad-hoc way.
Arnold Schwaighofer36512332015-07-21 17:07:07 +0000402 auto &Context = New->getContext();
403 auto NewFuncAttrs = New->getAttributes();
404 auto CallSiteAttrs = CS.getAttributes();
405
406 CallSiteAttrs = CallSiteAttrs.addAttributes(
407 Context, AttributeSet::ReturnIndex, NewFuncAttrs.getRetAttributes());
408
409 for (unsigned argIdx = 0; argIdx < CS.arg_size(); argIdx++) {
410 AttributeSet Attrs = NewFuncAttrs.getParamAttributes(argIdx);
411 if (Attrs.getNumSlots())
412 CallSiteAttrs = CallSiteAttrs.addAttributes(Context, argIdx, Attrs);
413 }
414
415 CS.setAttributes(CallSiteAttrs);
416
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000417 remove(CS.getInstruction()->getParent()->getParent());
Chandler Carruthcdf47882014-03-09 03:16:01 +0000418 U->set(BitcastNew);
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000419 }
420 }
421}
422
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000423// Replace G with an alias to F if possible, or else a thunk to F. Deletes G.
424void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000425 if (HasGlobalAliases && G->hasGlobalUnnamedAddr()) {
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000426 if (G->hasExternalLinkage() || G->hasLocalLinkage() ||
427 G->hasWeakLinkage()) {
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000428 writeAlias(F, G);
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000429 return;
430 }
431 }
432
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000433 writeThunk(F, G);
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000434}
435
Stepan Dyatkovskiydc2c4b42013-09-17 09:36:11 +0000436// Helper for writeThunk,
437// Selects proper bitcast operation,
Alp Tokercb402912014-01-24 17:20:08 +0000438// but a bit simpler then CastInst::getCastOpcode.
Mehdi Aminiba9fba82016-03-13 21:05:13 +0000439static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) {
Stepan Dyatkovskiydc2c4b42013-09-17 09:36:11 +0000440 Type *SrcTy = V->getType();
Carlo Kok307625c2014-04-30 17:53:04 +0000441 if (SrcTy->isStructTy()) {
442 assert(DestTy->isStructTy());
443 assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements());
444 Value *Result = UndefValue::get(DestTy);
445 for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) {
446 Value *Element = createCast(
Craig Toppere1d12942014-08-27 05:25:25 +0000447 Builder, Builder.CreateExtractValue(V, makeArrayRef(I)),
Carlo Kok307625c2014-04-30 17:53:04 +0000448 DestTy->getStructElementType(I));
449
450 Result =
Craig Toppere1d12942014-08-27 05:25:25 +0000451 Builder.CreateInsertValue(Result, Element, makeArrayRef(I));
Carlo Kok307625c2014-04-30 17:53:04 +0000452 }
453 return Result;
454 }
455 assert(!DestTy->isStructTy());
Stepan Dyatkovskiydc2c4b42013-09-17 09:36:11 +0000456 if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
457 return Builder.CreateIntToPtr(V, DestTy);
458 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
459 return Builder.CreatePtrToInt(V, DestTy);
460 else
461 return Builder.CreateBitCast(V, DestTy);
462}
463
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000464// Replace G with a simple tail call to bitcast(F). Also replace direct uses
465// of G with bitcast(F). Deletes G.
466void MergeFunctions::writeThunk(Function *F, Function *G) {
Sanjoy Das5ce32722016-04-08 00:48:30 +0000467 if (!G->isInterposable()) {
Nick Lewyckyd3c6dfe2010-05-13 05:48:45 +0000468 // Redirect direct callers of G to F.
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000469 replaceDirectCallers(G, F);
Nick Lewyckyd3c6dfe2010-05-13 05:48:45 +0000470 }
471
Nick Lewycky71972d42010-09-07 01:42:10 +0000472 // If G was internal then we may have replaced all uses of G with F. If so,
Nick Lewyckyf216f69a2010-08-06 07:21:30 +0000473 // stop here and delete G. There's no need for a thunk.
474 if (G->hasLocalLinkage() && G->use_empty()) {
475 G->eraseFromParent();
476 return;
477 }
478
Nick Lewycky25675ac2009-06-12 15:56:56 +0000479 Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
480 G->getParent());
Owen Anderson55f1c092009-08-13 21:58:54 +0000481 BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
Mehdi Aminiba9fba82016-03-13 21:05:13 +0000482 IRBuilder<> Builder(BB);
Nick Lewyckye04dc222009-06-12 08:04:51 +0000483
Nick Lewyckyd3c6dfe2010-05-13 05:48:45 +0000484 SmallVector<Value *, 16> Args;
Nick Lewyckye04dc222009-06-12 08:04:51 +0000485 unsigned i = 0;
Chris Lattner229907c2011-07-18 04:54:35 +0000486 FunctionType *FFTy = F->getFunctionType();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000487 for (Argument & AI : NewG->args()) {
488 Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i)));
Nick Lewyckye04dc222009-06-12 08:04:51 +0000489 ++i;
490 }
491
Jay Foad5bd375a2011-07-15 08:37:34 +0000492 CallInst *CI = Builder.CreateCall(F, Args);
Nick Lewyckye04dc222009-06-12 08:04:51 +0000493 CI->setTailCall();
Nick Lewyckyd5bf51f2009-06-12 16:04:00 +0000494 CI->setCallingConv(F->getCallingConv());
JF Bastienfa946232015-09-10 18:08:35 +0000495 CI->setAttributes(F->getAttributes());
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000496 if (NewG->getReturnType()->isVoidTy()) {
Nick Lewyckyfbd27572010-08-08 05:04:23 +0000497 Builder.CreateRetVoid();
Nick Lewyckye04dc222009-06-12 08:04:51 +0000498 } else {
Stepan Dyatkovskiydc2c4b42013-09-17 09:36:11 +0000499 Builder.CreateRet(createCast(Builder, CI, NewG->getReturnType()));
Nick Lewyckye04dc222009-06-12 08:04:51 +0000500 }
501
502 NewG->copyAttributesFrom(G);
503 NewG->takeName(G);
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000504 removeUsers(G);
Nick Lewyckye04dc222009-06-12 08:04:51 +0000505 G->replaceAllUsesWith(NewG);
506 G->eraseFromParent();
Nick Lewycky71972d42010-09-07 01:42:10 +0000507
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000508 DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n');
Nick Lewycky71972d42010-09-07 01:42:10 +0000509 ++NumThunksWritten;
Nick Lewyckye04dc222009-06-12 08:04:51 +0000510}
511
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000512// Replace G with an alias to F and delete G.
513void MergeFunctions::writeAlias(Function *F, Function *G) {
David Blaikie6614d8d2015-09-14 20:29:26 +0000514 auto *GA = GlobalAlias::create(G->getLinkage(), "", F);
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000515 F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
516 GA->takeName(G);
517 GA->setVisibility(G->getVisibility());
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000518 removeUsers(G);
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000519 G->replaceAllUsesWith(GA);
520 G->eraseFromParent();
521
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000522 DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n');
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000523 ++NumAliasesWritten;
524}
525
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000526// Merge two equivalent functions. Upon completion, Function G is deleted.
527void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
Sanjoy Das5ce32722016-04-08 00:48:30 +0000528 if (F->isInterposable()) {
529 assert(G->isInterposable());
Nick Lewyckyd3c6dfe2010-05-13 05:48:45 +0000530
Arnold Schwaighofer7e226272015-06-09 18:19:17 +0000531 // Make them both thunks to the same internal function.
532 Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
533 F->getParent());
534 H->copyAttributesFrom(F);
535 H->takeName(F);
536 removeUsers(F);
537 F->replaceAllUsesWith(H);
538
539 unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
540
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000541 if (HasGlobalAliases) {
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000542 writeAlias(F, G);
543 writeAlias(F, H);
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000544 } else {
Arnold Schwaighofer7e226272015-06-09 18:19:17 +0000545 writeThunk(F, G);
546 writeThunk(F, H);
Nick Lewyckyf1cec162011-01-25 08:56:50 +0000547 }
Nick Lewycky71972d42010-09-07 01:42:10 +0000548
Arnold Schwaighofer7e226272015-06-09 18:19:17 +0000549 F->setAlignment(MaxAlignment);
550 F->setLinkage(GlobalValue::PrivateLinkage);
Nick Lewycky71972d42010-09-07 01:42:10 +0000551 ++NumDoubleWeak;
Nick Lewyckyf216f69a2010-08-06 07:21:30 +0000552 } else {
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000553 writeThunkOrAlias(F, G);
Nick Lewycky3c6d34a2008-11-02 16:46:26 +0000554 }
555
Nick Lewyckye04dc222009-06-12 08:04:51 +0000556 ++NumFunctionsMerged;
Nick Lewyckyd01d42e2008-11-02 05:52:50 +0000557}
558
JF Bastien3a4ad612015-09-02 23:55:23 +0000559/// Replace function F by function G.
560void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN,
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000561 Function *G) {
JF Bastien3a4ad612015-09-02 23:55:23 +0000562 Function *F = FN.getFunc();
JF Bastien057292a2015-08-21 23:27:24 +0000563 assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 &&
564 "The two functions must be equal");
JF Bastien3a4ad612015-09-02 23:55:23 +0000565
566 auto I = FNodesInTree.find(F);
567 assert(I != FNodesInTree.end() && "F should be in FNodesInTree");
568 assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G");
569
570 FnTreeType::iterator IterToFNInFnTree = I->second;
571 assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree.");
572 // Remove F -> FN and insert G -> FN
573 FNodesInTree.erase(I);
574 FNodesInTree.insert({G, IterToFNInFnTree});
575 // Replace F with G in FN, which is stored inside the FnTree.
576 FN.replaceBy(G);
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000577}
578
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000579// Insert a ComparableFunction into the FnTree, or merge it away if equal to one
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000580// that was already inserted.
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000581bool MergeFunctions::insert(Function *NewFunction) {
582 std::pair<FnTreeType::iterator, bool> Result =
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000583 FnTree.insert(FunctionNode(NewFunction));
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000584
Nick Lewycky292e78c2011-02-09 06:32:02 +0000585 if (Result.second) {
JF Bastien3a4ad612015-09-02 23:55:23 +0000586 assert(FNodesInTree.count(NewFunction) == 0);
587 FNodesInTree.insert({NewFunction, Result.first});
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000588 DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() << '\n');
Nick Lewycky00959372010-09-05 08:22:49 +0000589 return false;
Nick Lewycky292e78c2011-02-09 06:32:02 +0000590 }
Nick Lewyckyfbd27572010-08-08 05:04:23 +0000591
Stepan Dyatkovskiyfe134cd2014-09-10 10:08:25 +0000592 const FunctionNode &OldF = *Result.first;
Nick Lewycky00959372010-09-05 08:22:49 +0000593
Matt Arsenault517d84e2013-10-01 18:05:30 +0000594 // Don't merge tiny functions, since it can just end up making the function
595 // larger.
596 // FIXME: Should still merge them if they are unnamed_addr and produce an
597 // alias.
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000598 if (NewFunction->size() == 1) {
599 if (NewFunction->front().size() <= 2) {
600 DEBUG(dbgs() << NewFunction->getName()
601 << " is to small to bother merging\n");
Matt Arsenault517d84e2013-10-01 18:05:30 +0000602 return false;
603 }
604 }
605
Arnold Schwaighofer0302da62015-06-09 00:03:29 +0000606 // Impose a total order (by name) on the replacement of functions. This is
607 // important when operating on more than one module independently to prevent
608 // cycles of thunks calling each other when the modules are linked together.
609 //
Erik Eckstein0c48dd82016-05-31 17:20:23 +0000610 // First of all, we process strong functions before weak functions.
611 if ((OldF.getFunc()->isInterposable() && !NewFunction->isInterposable()) ||
612 (OldF.getFunc()->isInterposable() == NewFunction->isInterposable() &&
613 OldF.getFunc()->getName() > NewFunction->getName())) {
614 // Swap the two functions.
615 Function *F = OldF.getFunc();
616 replaceFunctionInTree(*Result.first, NewFunction);
617 NewFunction = F;
618 assert(OldF.getFunc() != F && "Must have swapped the functions.");
619 }
Nick Lewycky00959372010-09-05 08:22:49 +0000620
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000621 DEBUG(dbgs() << " " << OldF.getFunc()->getName()
622 << " == " << NewFunction->getName() << '\n');
Nick Lewycky00959372010-09-05 08:22:49 +0000623
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000624 Function *DeleteF = NewFunction;
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000625 mergeTwoFunctions(OldF.getFunc(), DeleteF);
Nick Lewycky00959372010-09-05 08:22:49 +0000626 return true;
Nick Lewyckyfbd27572010-08-08 05:04:23 +0000627}
Nick Lewyckyd01d42e2008-11-02 05:52:50 +0000628
Stepan Dyatkovskiyf4af8552014-06-21 20:54:36 +0000629// Remove a function from FnTree. If it was already in FnTree, add
630// it to Deferred so that we'll look at it in the next round.
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000631void MergeFunctions::remove(Function *F) {
JF Bastien3a4ad612015-09-02 23:55:23 +0000632 auto I = FNodesInTree.find(F);
633 if (I != FNodesInTree.end()) {
634 DEBUG(dbgs() << "Deferred " << F->getName()<< ".\n");
635 FnTree.erase(I->second);
636 // I->second has been invalidated, remove it from the FNodesInTree map to
637 // preserve the invariant.
638 FNodesInTree.erase(I);
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000639 Deferred.emplace_back(F);
Nick Lewycky0464d1d2010-08-31 05:53:05 +0000640 }
Nick Lewycky4e250c82011-01-02 02:46:33 +0000641}
Nick Lewycky00959372010-09-05 08:22:49 +0000642
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000643// For each instruction used by the value, remove() the function that contains
644// the instruction. This should happen right before a call to RAUW.
645void MergeFunctions::removeUsers(Value *V) {
Nick Lewycky5361b842011-01-02 19:16:44 +0000646 std::vector<Value *> Worklist;
647 Worklist.push_back(V);
JF Bastien7289f732015-07-15 21:51:33 +0000648 SmallSet<Value*, 8> Visited;
649 Visited.insert(V);
Nick Lewycky5361b842011-01-02 19:16:44 +0000650 while (!Worklist.empty()) {
651 Value *V = Worklist.back();
652 Worklist.pop_back();
653
Chandler Carruthcdf47882014-03-09 03:16:01 +0000654 for (User *U : V->users()) {
655 if (Instruction *I = dyn_cast<Instruction>(U)) {
Nick Lewyckycfb284c2011-01-28 08:43:14 +0000656 remove(I->getParent()->getParent());
Chandler Carruthcdf47882014-03-09 03:16:01 +0000657 } else if (isa<GlobalValue>(U)) {
Nick Lewycky540f9532011-01-15 10:16:23 +0000658 // do nothing
Chandler Carruthcdf47882014-03-09 03:16:01 +0000659 } else if (Constant *C = dyn_cast<Constant>(U)) {
JF Bastien7289f732015-07-15 21:51:33 +0000660 for (User *UU : C->users()) {
661 if (!Visited.insert(UU).second)
662 Worklist.push_back(UU);
663 }
Nick Lewycky5361b842011-01-02 19:16:44 +0000664 }
Nick Lewycky00959372010-09-05 08:22:49 +0000665 }
Nick Lewycky0464d1d2010-08-31 05:53:05 +0000666 }
Nick Lewycky00959372010-09-05 08:22:49 +0000667}