Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1 | //===- 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 Dyatkovskiy | 471eab3 | 2014-06-22 00:57:09 +0000 | [diff] [blame] | 12 | // 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 Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 22 | // |
Stepan Dyatkovskiy | 471eab3 | 2014-06-22 00:57:09 +0000 | [diff] [blame] | 23 | // 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 Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 30 | // |
| 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 Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 38 | // |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 39 | // 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 Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 42 | // |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // |
| 45 | // Future work: |
| 46 | // |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 47 | // * 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 Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 51 | // and call, this is irrelevant, and we'd like to fold such functions. |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 52 | // |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 53 | // * be smarter about bitcasts. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 54 | // |
| 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 Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 58 | // other doesn't. We should learn to look through bitcasts. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 59 | // |
Stepan Dyatkovskiy | 471eab3 | 2014-06-22 00:57:09 +0000 | [diff] [blame] | 60 | // * 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 Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 92 | #include "llvm/ADT/Hashing.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 93 | #include "llvm/ADT/STLExtras.h" |
| 94 | #include "llvm/ADT/SmallSet.h" |
| 95 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 96 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 97 | #include "llvm/IR/Constants.h" |
| 98 | #include "llvm/IR/DataLayout.h" |
| 99 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 100 | #include "llvm/IR/Instructions.h" |
| 101 | #include "llvm/IR/LLVMContext.h" |
| 102 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 103 | #include "llvm/IR/ValueHandle.h" |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 104 | #include "llvm/IR/ValueMap.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 105 | #include "llvm/Pass.h" |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 106 | #include "llvm/Support/CommandLine.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 107 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 108 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 109 | #include "llvm/Support/raw_ostream.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 110 | #include "llvm/Transforms/IPO.h" |
Erik Eckstein | 4d6fb72 | 2016-11-11 21:15:13 +0000 | [diff] [blame^] | 111 | #include "llvm/Transforms/Utils/FunctionComparator.h" |
Nick Lewycky | 68984ed | 2010-08-31 08:29:37 +0000 | [diff] [blame] | 112 | #include <vector> |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 113 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 114 | using namespace llvm; |
| 115 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 116 | #define DEBUG_TYPE "mergefunc" |
| 117 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 118 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 119 | STATISTIC(NumThunksWritten, "Number of thunks generated"); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 120 | STATISTIC(NumAliasesWritten, "Number of aliases generated"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 121 | STATISTIC(NumDoubleWeak, "Number of new functions created"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 122 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 123 | static 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 Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 130 | namespace { |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 131 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 132 | class FunctionNode { |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 133 | mutable AssertingVH<Function> F; |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 134 | FunctionComparator::FunctionHash Hash; |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 135 | public: |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 136 | // Note the hash is recalculated potentially multiple times, but it is cheap. |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 137 | FunctionNode(Function *F) |
| 138 | : F(F), Hash(FunctionComparator::functionHash(*F)) {} |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 139 | Function *getFunc() const { return F; } |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 140 | FunctionComparator::FunctionHash getHash() const { return Hash; } |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 141 | |
| 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 Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 145 | F = G; |
| 146 | } |
| 147 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 148 | void release() { F = nullptr; } |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 149 | }; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 150 | |
| 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 | /// |
| 156 | class MergeFunctions : public ModulePass { |
| 157 | public: |
| 158 | static char ID; |
| 159 | MergeFunctions() |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 160 | : ModulePass(ID), FnTree(FunctionNodeCmp(&GlobalNumbers)), FNodesInTree(), |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 161 | HasGlobalAliases(false) { |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 162 | initializeMergeFunctionsPass(*PassRegistry::getPassRegistry()); |
| 163 | } |
| 164 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 165 | bool runOnModule(Module &M) override; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 166 | |
| 167 | private: |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 168 | // 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 Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 185 | |
| 186 | /// A work queue of functions that may have been modified and should be |
| 187 | /// analyzed again. |
| 188 | std::vector<WeakVH> Deferred; |
| 189 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 190 | /// 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 Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 194 | /// Insert a ComparableFunction into the FnTree, or merge it away if it's |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 195 | /// equal to one that's already present. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 196 | bool insert(Function *NewFunction); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 197 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 198 | /// Remove a Function from the FnTree and queue it up for a second sweep of |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 199 | /// analysis. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 200 | void remove(Function *F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 201 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 202 | /// Find the functions that use this Value and remove them from FnTree and |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 203 | /// queue the functions. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 204 | void removeUsers(Value *V); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 205 | |
| 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 Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 210 | /// 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 Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 214 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 215 | /// Replace G with a thunk or an alias to F. Deletes G. |
| 216 | void writeThunkOrAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 217 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 218 | /// 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 Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 221 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 222 | /// Replace G with an alias to F. Deletes G. |
| 223 | void writeAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 224 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 225 | /// Replace function F with function G in the function tree. |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 226 | void replaceFunctionInTree(const FunctionNode &FN, Function *G); |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 227 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 228 | /// The set of all distinct functions. Use the insert() and remove() methods |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 229 | /// to modify it. The map allows efficient lookup and deferring of Functions. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 230 | FnTreeType FnTree; |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 231 | // 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 Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 237 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 238 | /// Whether or not the target supports global aliases. |
| 239 | bool HasGlobalAliases; |
| 240 | }; |
| 241 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 242 | } // end anonymous namespace |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 243 | |
| 244 | char MergeFunctions::ID = 0; |
| 245 | INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false) |
| 246 | |
| 247 | ModulePass *llvm::createMergeFunctionsPass() { |
| 248 | return new MergeFunctions(); |
| 249 | } |
| 250 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 251 | bool 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 Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 265 | int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare(); |
| 266 | int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 267 | |
| 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 Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 287 | int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare(); |
| 288 | int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 289 | |
| 290 | bool Transitive = true; |
| 291 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 292 | if (Res1 != 0 && Res1 == Res4) { |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 293 | // F1 > F2, F2 > F3 => F1 > F3 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 294 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 295 | } else if (Res3 != 0 && Res3 == -Res4) { |
| 296 | // F1 > F3, F3 > F2 => F1 > F2 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 297 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 298 | } else if (Res4 != 0 && -Res3 == Res4) { |
| 299 | // F2 > F3, F3 > F1 => F2 > F1 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 300 | 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 Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 323 | bool MergeFunctions::runOnModule(Module &M) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 324 | if (skipModule(M)) |
| 325 | return false; |
| 326 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 327 | bool Changed = false; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 328 | |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 329 | // 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 Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 337 | } |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 338 | |
NAKAMURA Takumi | 5196275 | 2015-08-16 02:41:23 +0000 | [diff] [blame] | 339 | 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 Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 345 | |
| 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 Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 356 | do { |
| 357 | std::vector<WeakVH> Worklist; |
| 358 | Deferred.swap(Worklist); |
| 359 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 360 | DEBUG(doSanityCheck(Worklist)); |
| 361 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 362 | DEBUG(dbgs() << "size of module: " << M.size() << '\n'); |
| 363 | DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); |
| 364 | |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame] | 365 | // Insert functions and merge them. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 366 | for (WeakVH &I : Worklist) { |
| 367 | if (!I) |
| 368 | continue; |
| 369 | Function *F = cast<Function>(I); |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame] | 370 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) { |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 371 | Changed |= insert(F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 374 | DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n'); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 375 | } while (!Deferred.empty()); |
| 376 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 377 | FnTree.clear(); |
Arnold Schwaighofer | 0591c5d | 2015-10-05 17:26:36 +0000 | [diff] [blame] | 378 | GlobalNumbers.clear(); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 379 | |
| 380 | return Changed; |
| 381 | } |
| 382 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 383 | // Replace direct callers of Old with New. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 384 | void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { |
| 385 | Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 386 | for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) { |
| 387 | Use *U = &*UI; |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 388 | ++UI; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 389 | CallSite CS(U->getUser()); |
| 390 | if (CS && CS.isCallee(U)) { |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 391 | // Transfer the called function's attributes to the call site. Due to the |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 392 | // bitcast we will 'lose' ABI changing attributes because the 'called |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 393 | // function' is no longer a Function* but the bitcast. Code that looks up |
| 394 | // the attributes from the called function will fail. |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 395 | |
| 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 Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 402 | 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 Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 417 | remove(CS.getInstruction()->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 418 | U->set(BitcastNew); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 423 | // Replace G with an alias to F if possible, or else a thunk to F. Deletes G. |
| 424 | void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) { |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 425 | if (HasGlobalAliases && G->hasGlobalUnnamedAddr()) { |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 426 | if (G->hasExternalLinkage() || G->hasLocalLinkage() || |
| 427 | G->hasWeakLinkage()) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 428 | writeAlias(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | } |
| 432 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 433 | writeThunk(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 436 | // Helper for writeThunk, |
| 437 | // Selects proper bitcast operation, |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 438 | // but a bit simpler then CastInst::getCastOpcode. |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 439 | static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 440 | Type *SrcTy = V->getType(); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 441 | 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 Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 447 | Builder, Builder.CreateExtractValue(V, makeArrayRef(I)), |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 448 | DestTy->getStructElementType(I)); |
| 449 | |
| 450 | Result = |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 451 | Builder.CreateInsertValue(Result, Element, makeArrayRef(I)); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 452 | } |
| 453 | return Result; |
| 454 | } |
| 455 | assert(!DestTy->isStructTy()); |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 456 | 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 Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 464 | // Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 465 | // of G with bitcast(F). Deletes G. |
| 466 | void MergeFunctions::writeThunk(Function *F, Function *G) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 467 | if (!G->isInterposable()) { |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 468 | // Redirect direct callers of G to F. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 469 | replaceDirectCallers(G, F); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 472 | // If G was internal then we may have replaced all uses of G with F. If so, |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 473 | // 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 Lewycky | 25675ac | 2009-06-12 15:56:56 +0000 | [diff] [blame] | 479 | Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", |
| 480 | G->getParent()); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 481 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG); |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 482 | IRBuilder<> Builder(BB); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 483 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 484 | SmallVector<Value *, 16> Args; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 485 | unsigned i = 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 486 | FunctionType *FFTy = F->getFunctionType(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 487 | for (Argument & AI : NewG->args()) { |
| 488 | Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i))); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 489 | ++i; |
| 490 | } |
| 491 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 492 | CallInst *CI = Builder.CreateCall(F, Args); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 493 | CI->setTailCall(); |
Nick Lewycky | d5bf51f | 2009-06-12 16:04:00 +0000 | [diff] [blame] | 494 | CI->setCallingConv(F->getCallingConv()); |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 495 | CI->setAttributes(F->getAttributes()); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 496 | if (NewG->getReturnType()->isVoidTy()) { |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 497 | Builder.CreateRetVoid(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 498 | } else { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 499 | Builder.CreateRet(createCast(Builder, CI, NewG->getReturnType())); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | NewG->copyAttributesFrom(G); |
| 503 | NewG->takeName(G); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 504 | removeUsers(G); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 505 | G->replaceAllUsesWith(NewG); |
| 506 | G->eraseFromParent(); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 507 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 508 | DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n'); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 509 | ++NumThunksWritten; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 512 | // Replace G with an alias to F and delete G. |
| 513 | void MergeFunctions::writeAlias(Function *F, Function *G) { |
David Blaikie | 6614d8d | 2015-09-14 20:29:26 +0000 | [diff] [blame] | 514 | auto *GA = GlobalAlias::create(G->getLinkage(), "", F); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 515 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 516 | GA->takeName(G); |
| 517 | GA->setVisibility(G->getVisibility()); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 518 | removeUsers(G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 519 | G->replaceAllUsesWith(GA); |
| 520 | G->eraseFromParent(); |
| 521 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 522 | DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n'); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 523 | ++NumAliasesWritten; |
| 524 | } |
| 525 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 526 | // Merge two equivalent functions. Upon completion, Function G is deleted. |
| 527 | void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 528 | if (F->isInterposable()) { |
| 529 | assert(G->isInterposable()); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 530 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 531 | // 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 Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 541 | if (HasGlobalAliases) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 542 | writeAlias(F, G); |
| 543 | writeAlias(F, H); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 544 | } else { |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 545 | writeThunk(F, G); |
| 546 | writeThunk(F, H); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 547 | } |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 548 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 549 | F->setAlignment(MaxAlignment); |
| 550 | F->setLinkage(GlobalValue::PrivateLinkage); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 551 | ++NumDoubleWeak; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 552 | } else { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 553 | writeThunkOrAlias(F, G); |
Nick Lewycky | 3c6d34a | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 556 | ++NumFunctionsMerged; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 557 | } |
| 558 | |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 559 | /// Replace function F by function G. |
| 560 | void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN, |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 561 | Function *G) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 562 | Function *F = FN.getFunc(); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 563 | assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 && |
| 564 | "The two functions must be equal"); |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 565 | |
| 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 Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 579 | // Insert a ComparableFunction into the FnTree, or merge it away if equal to one |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 580 | // that was already inserted. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 581 | bool MergeFunctions::insert(Function *NewFunction) { |
| 582 | std::pair<FnTreeType::iterator, bool> Result = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 583 | FnTree.insert(FunctionNode(NewFunction)); |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 584 | |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 585 | if (Result.second) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 586 | assert(FNodesInTree.count(NewFunction) == 0); |
| 587 | FNodesInTree.insert({NewFunction, Result.first}); |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 588 | DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 589 | return false; |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 590 | } |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 591 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 592 | const FunctionNode &OldF = *Result.first; |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 593 | |
Matt Arsenault | 517d84e | 2013-10-01 18:05:30 +0000 | [diff] [blame] | 594 | // 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 Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 598 | if (NewFunction->size() == 1) { |
| 599 | if (NewFunction->front().size() <= 2) { |
| 600 | DEBUG(dbgs() << NewFunction->getName() |
| 601 | << " is to small to bother merging\n"); |
Matt Arsenault | 517d84e | 2013-10-01 18:05:30 +0000 | [diff] [blame] | 602 | return false; |
| 603 | } |
| 604 | } |
| 605 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 606 | // 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 Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame] | 610 | // 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 Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 620 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 621 | DEBUG(dbgs() << " " << OldF.getFunc()->getName() |
| 622 | << " == " << NewFunction->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 623 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 624 | Function *DeleteF = NewFunction; |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 625 | mergeTwoFunctions(OldF.getFunc(), DeleteF); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 626 | return true; |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 627 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 628 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 629 | // 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 Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 631 | void MergeFunctions::remove(Function *F) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 632 | 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 Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 639 | Deferred.emplace_back(F); |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 640 | } |
Nick Lewycky | 4e250c8 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 641 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 642 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 643 | // For each instruction used by the value, remove() the function that contains |
| 644 | // the instruction. This should happen right before a call to RAUW. |
| 645 | void MergeFunctions::removeUsers(Value *V) { |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 646 | std::vector<Value *> Worklist; |
| 647 | Worklist.push_back(V); |
JF Bastien | 7289f73 | 2015-07-15 21:51:33 +0000 | [diff] [blame] | 648 | SmallSet<Value*, 8> Visited; |
| 649 | Visited.insert(V); |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 650 | while (!Worklist.empty()) { |
| 651 | Value *V = Worklist.back(); |
| 652 | Worklist.pop_back(); |
| 653 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 654 | for (User *U : V->users()) { |
| 655 | if (Instruction *I = dyn_cast<Instruction>(U)) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 656 | remove(I->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 657 | } else if (isa<GlobalValue>(U)) { |
Nick Lewycky | 540f953 | 2011-01-15 10:16:23 +0000 | [diff] [blame] | 658 | // do nothing |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 659 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
JF Bastien | 7289f73 | 2015-07-15 21:51:33 +0000 | [diff] [blame] | 660 | for (User *UU : C->users()) { |
| 661 | if (!Visited.insert(UU).second) |
| 662 | Worklist.push_back(UU); |
| 663 | } |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 664 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 665 | } |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 666 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 667 | } |