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. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 30 | // |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 92 | #include "llvm/ADT/ArrayRef.h" |
Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 93 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 94 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 95 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 96 | #include "llvm/IR/Argument.h" |
| 97 | #include "llvm/IR/Attributes.h" |
| 98 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 99 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 100 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 101 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 102 | #include "llvm/IR/DebugInfoMetadata.h" |
| 103 | #include "llvm/IR/DebugLoc.h" |
| 104 | #include "llvm/IR/DerivedTypes.h" |
| 105 | #include "llvm/IR/Function.h" |
| 106 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 107 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 108 | #include "llvm/IR/InstrTypes.h" |
| 109 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 110 | #include "llvm/IR/Instructions.h" |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 111 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 112 | #include "llvm/IR/Module.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 113 | #include "llvm/IR/Type.h" |
| 114 | #include "llvm/IR/Use.h" |
| 115 | #include "llvm/IR/User.h" |
| 116 | #include "llvm/IR/Value.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 117 | #include "llvm/IR/ValueHandle.h" |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 118 | #include "llvm/IR/ValueMap.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 119 | #include "llvm/Pass.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 120 | #include "llvm/Support/Casting.h" |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 121 | #include "llvm/Support/CommandLine.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 122 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 123 | #include "llvm/Support/raw_ostream.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 124 | #include "llvm/Transforms/IPO.h" |
Erik Eckstein | 4d6fb72 | 2016-11-11 21:15:13 +0000 | [diff] [blame] | 125 | #include "llvm/Transforms/Utils/FunctionComparator.h" |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 126 | #include <algorithm> |
| 127 | #include <cassert> |
| 128 | #include <iterator> |
| 129 | #include <set> |
| 130 | #include <utility> |
Nick Lewycky | 68984ed | 2010-08-31 08:29:37 +0000 | [diff] [blame] | 131 | #include <vector> |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 132 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 133 | using namespace llvm; |
| 134 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 135 | #define DEBUG_TYPE "mergefunc" |
| 136 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 137 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 138 | STATISTIC(NumThunksWritten, "Number of thunks generated"); |
| 139 | STATISTIC(NumDoubleWeak, "Number of new functions created"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 140 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 141 | static cl::opt<unsigned> NumFunctionsForSanityCheck( |
| 142 | "mergefunc-sanity", |
| 143 | cl::desc("How many functions in module could be used for " |
| 144 | "MergeFunctions pass sanity check. " |
| 145 | "'0' disables this check. Works only with '-debug' key."), |
| 146 | cl::init(0), cl::Hidden); |
| 147 | |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 148 | // Under option -mergefunc-preserve-debug-info we: |
| 149 | // - Do not create a new function for a thunk. |
| 150 | // - Retain the debug info for a thunk's parameters (and associated |
| 151 | // instructions for the debug info) from the entry block. |
| 152 | // Note: -debug will display the algorithm at work. |
| 153 | // - Create debug-info for the call (to the shared implementation) made by |
| 154 | // a thunk and its return value. |
| 155 | // - Erase the rest of the function, retaining the (minimally sized) entry |
| 156 | // block to create a thunk. |
| 157 | // - Preserve a thunk's call site to point to the thunk even when both occur |
| 158 | // within the same translation unit, to aid debugability. Note that this |
| 159 | // behaviour differs from the underlying -mergefunc implementation which |
| 160 | // modifies the thunk's call site to point to the shared implementation |
| 161 | // when both occur within the same translation unit. |
| 162 | static cl::opt<bool> |
| 163 | MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden, |
| 164 | cl::init(false), |
| 165 | cl::desc("Preserve debug info in thunk when mergefunc " |
| 166 | "transformations are made.")); |
| 167 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 168 | namespace { |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 169 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 170 | class FunctionNode { |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 171 | mutable AssertingVH<Function> F; |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 172 | FunctionComparator::FunctionHash Hash; |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 173 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 174 | public: |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 175 | // Note the hash is recalculated potentially multiple times, but it is cheap. |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 176 | FunctionNode(Function *F) |
| 177 | : F(F), Hash(FunctionComparator::functionHash(*F)) {} |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 178 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 179 | Function *getFunc() const { return F; } |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 180 | FunctionComparator::FunctionHash getHash() const { return Hash; } |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 181 | |
| 182 | /// Replace the reference to the function F by the function G, assuming their |
| 183 | /// implementations are equal. |
| 184 | void replaceBy(Function *G) const { |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 185 | F = G; |
| 186 | } |
| 187 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 188 | void release() { F = nullptr; } |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 189 | }; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 190 | |
| 191 | /// MergeFunctions finds functions which will generate identical machine code, |
| 192 | /// by considering all pointer types to be equivalent. Once identified, |
| 193 | /// MergeFunctions will fold them by replacing a call to one to a call to a |
| 194 | /// bitcast of the other. |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 195 | class MergeFunctions : public ModulePass { |
| 196 | public: |
| 197 | static char ID; |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 198 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 199 | MergeFunctions() |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 200 | : ModulePass(ID), FnTree(FunctionNodeCmp(&GlobalNumbers)) { |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 201 | initializeMergeFunctionsPass(*PassRegistry::getPassRegistry()); |
| 202 | } |
| 203 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 204 | bool runOnModule(Module &M) override; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 205 | |
| 206 | private: |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 207 | // The function comparison operator is provided here so that FunctionNodes do |
| 208 | // not need to become larger with another pointer. |
| 209 | class FunctionNodeCmp { |
| 210 | GlobalNumberState* GlobalNumbers; |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 211 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 212 | public: |
| 213 | FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {} |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 214 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 215 | bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const { |
| 216 | // Order first by hashes, then full function comparison. |
| 217 | if (LHS.getHash() != RHS.getHash()) |
| 218 | return LHS.getHash() < RHS.getHash(); |
| 219 | FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers); |
| 220 | return FCmp.compare() == -1; |
| 221 | } |
| 222 | }; |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 223 | using FnTreeType = std::set<FunctionNode, FunctionNodeCmp>; |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 224 | |
| 225 | GlobalNumberState GlobalNumbers; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 226 | |
| 227 | /// A work queue of functions that may have been modified and should be |
| 228 | /// analyzed again. |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 229 | std::vector<WeakTrackingVH> Deferred; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 230 | |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 231 | #ifndef NDEBUG |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 232 | /// Checks the rules of order relation introduced among functions set. |
| 233 | /// Returns true, if sanity check has been passed, and false if failed. |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 234 | bool doSanityCheck(std::vector<WeakTrackingVH> &Worklist); |
Davide Italiano | b6681e2 | 2017-04-28 19:39:45 +0000 | [diff] [blame] | 235 | #endif |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 236 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 237 | /// 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] | 238 | /// equal to one that's already present. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 239 | bool insert(Function *NewFunction); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 240 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 241 | /// 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] | 242 | /// analysis. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 243 | void remove(Function *F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 244 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 245 | /// 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] | 246 | /// queue the functions. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 247 | void removeUsers(Value *V); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 248 | |
| 249 | /// Replace all direct calls of Old with calls of New. Will bitcast New if |
| 250 | /// necessary to make types match. |
| 251 | void replaceDirectCallers(Function *Old, Function *New); |
| 252 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 253 | /// Merge two equivalent functions. Upon completion, G may be deleted, or may |
| 254 | /// be converted into a thunk. In either case, it should never be visited |
| 255 | /// again. |
| 256 | void mergeTwoFunctions(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 257 | |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 258 | /// Fill PDIUnrelatedWL with instructions from the entry block that are |
| 259 | /// unrelated to parameter related debug info. |
| 260 | void filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock, |
| 261 | std::vector<Instruction *> &PDIUnrelatedWL); |
| 262 | |
| 263 | /// Erase the rest of the CFG (i.e. barring the entry block). |
| 264 | void eraseTail(Function *G); |
| 265 | |
| 266 | /// Erase the instructions in PDIUnrelatedWL as they are unrelated to the |
| 267 | /// parameter debug info, from the entry block. |
| 268 | void eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL); |
| 269 | |
| 270 | /// Replace G with a simple tail call to bitcast(F). Also (unless |
| 271 | /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F), |
| 272 | /// delete G. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 273 | void writeThunk(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 274 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 275 | /// Replace function F with function G in the function tree. |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 276 | void replaceFunctionInTree(const FunctionNode &FN, Function *G); |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 277 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 278 | /// The set of all distinct functions. Use the insert() and remove() methods |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 279 | /// to modify it. The map allows efficient lookup and deferring of Functions. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 280 | FnTreeType FnTree; |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 281 | |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 282 | // Map functions to the iterators of the FunctionNode which contains them |
| 283 | // in the FnTree. This must be updated carefully whenever the FnTree is |
| 284 | // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid |
| 285 | // dangling iterators into FnTree. The invariant that preserves this is that |
| 286 | // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree. |
whitequark | 73cb978 | 2018-11-08 03:58:01 +0000 | [diff] [blame^] | 287 | DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 290 | } // end anonymous namespace |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 291 | |
| 292 | char MergeFunctions::ID = 0; |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 293 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 294 | INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false) |
| 295 | |
| 296 | ModulePass *llvm::createMergeFunctionsPass() { |
| 297 | return new MergeFunctions(); |
| 298 | } |
| 299 | |
Davide Italiano | b6681e2 | 2017-04-28 19:39:45 +0000 | [diff] [blame] | 300 | #ifndef NDEBUG |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 301 | bool MergeFunctions::doSanityCheck(std::vector<WeakTrackingVH> &Worklist) { |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 302 | if (const unsigned Max = NumFunctionsForSanityCheck) { |
| 303 | unsigned TripleNumber = 0; |
| 304 | bool Valid = true; |
| 305 | |
| 306 | dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n"; |
| 307 | |
| 308 | unsigned i = 0; |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 309 | for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(), |
| 310 | E = Worklist.end(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 311 | I != E && i < Max; ++I, ++i) { |
| 312 | unsigned j = i; |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 313 | for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max; |
| 314 | ++J, ++j) { |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 315 | Function *F1 = cast<Function>(*I); |
| 316 | Function *F2 = cast<Function>(*J); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 317 | int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare(); |
| 318 | int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 319 | |
| 320 | // If F1 <= F2, then F2 >= F1, otherwise report failure. |
| 321 | if (Res1 != -Res2) { |
| 322 | dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber |
| 323 | << "\n"; |
Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 324 | dbgs() << *F1 << '\n' << *F2 << '\n'; |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 325 | Valid = false; |
| 326 | } |
| 327 | |
| 328 | if (Res1 == 0) |
| 329 | continue; |
| 330 | |
| 331 | unsigned k = j; |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 332 | for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max; |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 333 | ++k, ++K, ++TripleNumber) { |
| 334 | if (K == J) |
| 335 | continue; |
| 336 | |
| 337 | Function *F3 = cast<Function>(*K); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 338 | int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare(); |
| 339 | int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 340 | |
| 341 | bool Transitive = true; |
| 342 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 343 | if (Res1 != 0 && Res1 == Res4) { |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 344 | // F1 > F2, F2 > F3 => F1 > F3 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 345 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 346 | } else if (Res3 != 0 && Res3 == -Res4) { |
| 347 | // F1 > F3, F3 > F2 => F1 > F2 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 348 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 349 | } else if (Res4 != 0 && -Res3 == Res4) { |
| 350 | // F2 > F3, F3 > F1 => F2 > F1 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 351 | Transitive = Res4 == -Res1; |
| 352 | } |
| 353 | |
| 354 | if (!Transitive) { |
| 355 | dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: " |
| 356 | << TripleNumber << "\n"; |
| 357 | dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", " |
| 358 | << Res4 << "\n"; |
Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 359 | dbgs() << *F1 << '\n' << *F2 << '\n' << *F3 << '\n'; |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 360 | Valid = false; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n"; |
| 367 | return Valid; |
| 368 | } |
| 369 | return true; |
| 370 | } |
Davide Italiano | b6681e2 | 2017-04-28 19:39:45 +0000 | [diff] [blame] | 371 | #endif |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 372 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 373 | bool MergeFunctions::runOnModule(Module &M) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 374 | if (skipModule(M)) |
| 375 | return false; |
| 376 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 377 | bool Changed = false; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 378 | |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 379 | // All functions in the module, ordered by hash. Functions with a unique |
| 380 | // hash value are easily eliminated. |
| 381 | std::vector<std::pair<FunctionComparator::FunctionHash, Function *>> |
| 382 | HashedFuncs; |
| 383 | for (Function &Func : M) { |
| 384 | if (!Func.isDeclaration() && !Func.hasAvailableExternallyLinkage()) { |
| 385 | HashedFuncs.push_back({FunctionComparator::functionHash(Func), &Func}); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 386 | } |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 387 | } |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 388 | |
NAKAMURA Takumi | 5196275 | 2015-08-16 02:41:23 +0000 | [diff] [blame] | 389 | std::stable_sort( |
| 390 | HashedFuncs.begin(), HashedFuncs.end(), |
| 391 | [](const std::pair<FunctionComparator::FunctionHash, Function *> &a, |
| 392 | const std::pair<FunctionComparator::FunctionHash, Function *> &b) { |
| 393 | return a.first < b.first; |
| 394 | }); |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 395 | |
| 396 | auto S = HashedFuncs.begin(); |
| 397 | for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) { |
| 398 | // If the hash value matches the previous value or the next one, we must |
| 399 | // consider merging it. Otherwise it is dropped and never considered again. |
| 400 | if ((I != S && std::prev(I)->first == I->first) || |
| 401 | (std::next(I) != IE && std::next(I)->first == I->first) ) { |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 402 | Deferred.push_back(WeakTrackingVH(I->second)); |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 405 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 406 | do { |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 407 | std::vector<WeakTrackingVH> Worklist; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 408 | Deferred.swap(Worklist); |
| 409 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 410 | LLVM_DEBUG(doSanityCheck(Worklist)); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 411 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 412 | LLVM_DEBUG(dbgs() << "size of module: " << M.size() << '\n'); |
| 413 | LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 414 | |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame] | 415 | // Insert functions and merge them. |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 416 | for (WeakTrackingVH &I : Worklist) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 417 | if (!I) |
| 418 | continue; |
| 419 | Function *F = cast<Function>(I); |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame] | 420 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) { |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 421 | Changed |= insert(F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 424 | LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n'); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 425 | } while (!Deferred.empty()); |
| 426 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 427 | FnTree.clear(); |
whitequark | 73cb978 | 2018-11-08 03:58:01 +0000 | [diff] [blame^] | 428 | FNodesInTree.clear(); |
Arnold Schwaighofer | 0591c5d | 2015-10-05 17:26:36 +0000 | [diff] [blame] | 429 | GlobalNumbers.clear(); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 430 | |
| 431 | return Changed; |
| 432 | } |
| 433 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 434 | // Replace direct callers of Old with New. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 435 | void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { |
| 436 | Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 437 | for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) { |
| 438 | Use *U = &*UI; |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 439 | ++UI; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 440 | CallSite CS(U->getUser()); |
| 441 | if (CS && CS.isCallee(U)) { |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 442 | // 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] | 443 | // bitcast we will 'lose' ABI changing attributes because the 'called |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 444 | // function' is no longer a Function* but the bitcast. Code that looks up |
| 445 | // the attributes from the called function will fail. |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 446 | |
| 447 | // FIXME: This is not actually true, at least not anymore. The callsite |
| 448 | // will always have the same ABI affecting attributes as the callee, |
| 449 | // because otherwise the original input has UB. Note that Old and New |
| 450 | // always have matching ABI, so no attributes need to be changed. |
| 451 | // Transferring other attributes may help other optimizations, but that |
| 452 | // should be done uniformly and not in this ad-hoc way. |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 453 | auto &Context = New->getContext(); |
Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 454 | auto NewPAL = New->getAttributes(); |
| 455 | SmallVector<AttributeSet, 4> NewArgAttrs; |
| 456 | for (unsigned argIdx = 0; argIdx < CS.arg_size(); argIdx++) |
| 457 | NewArgAttrs.push_back(NewPAL.getParamAttributes(argIdx)); |
| 458 | // Don't transfer attributes from the function to the callee. Function |
| 459 | // attributes typically aren't relevant to the calling convention or ABI. |
| 460 | CS.setAttributes(AttributeList::get(Context, /*FnAttrs=*/AttributeSet(), |
| 461 | NewPAL.getRetAttributes(), |
| 462 | NewArgAttrs)); |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 463 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 464 | remove(CS.getInstruction()->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 465 | U->set(BitcastNew); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 470 | // Helper for writeThunk, |
| 471 | // Selects proper bitcast operation, |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 472 | // but a bit simpler then CastInst::getCastOpcode. |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 473 | static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 474 | Type *SrcTy = V->getType(); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 475 | if (SrcTy->isStructTy()) { |
| 476 | assert(DestTy->isStructTy()); |
| 477 | assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements()); |
| 478 | Value *Result = UndefValue::get(DestTy); |
| 479 | for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) { |
| 480 | Value *Element = createCast( |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 481 | Builder, Builder.CreateExtractValue(V, makeArrayRef(I)), |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 482 | DestTy->getStructElementType(I)); |
| 483 | |
| 484 | Result = |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 485 | Builder.CreateInsertValue(Result, Element, makeArrayRef(I)); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 486 | } |
| 487 | return Result; |
| 488 | } |
| 489 | assert(!DestTy->isStructTy()); |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 490 | if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) |
| 491 | return Builder.CreateIntToPtr(V, DestTy); |
| 492 | else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) |
| 493 | return Builder.CreatePtrToInt(V, DestTy); |
| 494 | else |
| 495 | return Builder.CreateBitCast(V, DestTy); |
| 496 | } |
| 497 | |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 498 | // Erase the instructions in PDIUnrelatedWL as they are unrelated to the |
| 499 | // parameter debug info, from the entry block. |
| 500 | void MergeFunctions::eraseInstsUnrelatedToPDI( |
| 501 | std::vector<Instruction *> &PDIUnrelatedWL) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 502 | LLVM_DEBUG( |
| 503 | dbgs() << " Erasing instructions (in reverse order of appearance in " |
| 504 | "entry block) unrelated to parameter debug info from entry " |
| 505 | "block: {\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 506 | while (!PDIUnrelatedWL.empty()) { |
| 507 | Instruction *I = PDIUnrelatedWL.back(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 508 | LLVM_DEBUG(dbgs() << " Deleting Instruction: "); |
| 509 | LLVM_DEBUG(I->print(dbgs())); |
| 510 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 511 | I->eraseFromParent(); |
| 512 | PDIUnrelatedWL.pop_back(); |
| 513 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 514 | LLVM_DEBUG(dbgs() << " } // Done erasing instructions unrelated to parameter " |
| 515 | "debug info from entry block. \n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // Reduce G to its entry block. |
| 519 | void MergeFunctions::eraseTail(Function *G) { |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 520 | std::vector<BasicBlock *> WorklistBB; |
| 521 | for (Function::iterator BBI = std::next(G->begin()), BBE = G->end(); |
| 522 | BBI != BBE; ++BBI) { |
| 523 | BBI->dropAllReferences(); |
| 524 | WorklistBB.push_back(&*BBI); |
| 525 | } |
| 526 | while (!WorklistBB.empty()) { |
| 527 | BasicBlock *BB = WorklistBB.back(); |
| 528 | BB->eraseFromParent(); |
| 529 | WorklistBB.pop_back(); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // We are interested in the following instructions from the entry block as being |
| 534 | // related to parameter debug info: |
| 535 | // - @llvm.dbg.declare |
| 536 | // - stores from the incoming parameters to locations on the stack-frame |
| 537 | // - allocas that create these locations on the stack-frame |
| 538 | // - @llvm.dbg.value |
| 539 | // - the entry block's terminator |
| 540 | // The rest are unrelated to debug info for the parameters; fill up |
| 541 | // PDIUnrelatedWL with such instructions. |
| 542 | void MergeFunctions::filterInstsUnrelatedToPDI( |
| 543 | BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL) { |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 544 | std::set<Instruction *> PDIRelated; |
| 545 | for (BasicBlock::iterator BI = GEntryBlock->begin(), BIE = GEntryBlock->end(); |
| 546 | BI != BIE; ++BI) { |
| 547 | if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 548 | LLVM_DEBUG(dbgs() << " Deciding: "); |
| 549 | LLVM_DEBUG(BI->print(dbgs())); |
| 550 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 551 | DILocalVariable *DILocVar = DVI->getVariable(); |
| 552 | if (DILocVar->isParameter()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 553 | LLVM_DEBUG(dbgs() << " Include (parameter): "); |
| 554 | LLVM_DEBUG(BI->print(dbgs())); |
| 555 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 556 | PDIRelated.insert(&*BI); |
| 557 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 558 | LLVM_DEBUG(dbgs() << " Delete (!parameter): "); |
| 559 | LLVM_DEBUG(BI->print(dbgs())); |
| 560 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 561 | } |
| 562 | } else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 563 | LLVM_DEBUG(dbgs() << " Deciding: "); |
| 564 | LLVM_DEBUG(BI->print(dbgs())); |
| 565 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 566 | DILocalVariable *DILocVar = DDI->getVariable(); |
| 567 | if (DILocVar->isParameter()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 568 | LLVM_DEBUG(dbgs() << " Parameter: "); |
| 569 | LLVM_DEBUG(DILocVar->print(dbgs())); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 570 | AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress()); |
| 571 | if (AI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 572 | LLVM_DEBUG(dbgs() << " Processing alloca users: "); |
| 573 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 574 | for (User *U : AI->users()) { |
| 575 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 576 | if (Value *Arg = SI->getValueOperand()) { |
| 577 | if (dyn_cast<Argument>(Arg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 578 | LLVM_DEBUG(dbgs() << " Include: "); |
| 579 | LLVM_DEBUG(AI->print(dbgs())); |
| 580 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 581 | PDIRelated.insert(AI); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 582 | LLVM_DEBUG(dbgs() << " Include (parameter): "); |
| 583 | LLVM_DEBUG(SI->print(dbgs())); |
| 584 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 585 | PDIRelated.insert(SI); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 586 | LLVM_DEBUG(dbgs() << " Include: "); |
| 587 | LLVM_DEBUG(BI->print(dbgs())); |
| 588 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 589 | PDIRelated.insert(&*BI); |
| 590 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 591 | LLVM_DEBUG(dbgs() << " Delete (!parameter): "); |
| 592 | LLVM_DEBUG(SI->print(dbgs())); |
| 593 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 597 | LLVM_DEBUG(dbgs() << " Defer: "); |
| 598 | LLVM_DEBUG(U->print(dbgs())); |
| 599 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 603 | LLVM_DEBUG(dbgs() << " Delete (alloca NULL): "); |
| 604 | LLVM_DEBUG(BI->print(dbgs())); |
| 605 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 606 | } |
| 607 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 608 | LLVM_DEBUG(dbgs() << " Delete (!parameter): "); |
| 609 | LLVM_DEBUG(BI->print(dbgs())); |
| 610 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 611 | } |
Chandler Carruth | 93cf2ea | 2018-10-18 00:37:37 +0000 | [diff] [blame] | 612 | } else if (BI->isTerminator() && &*BI == GEntryBlock->getTerminator()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 613 | LLVM_DEBUG(dbgs() << " Will Include Terminator: "); |
| 614 | LLVM_DEBUG(BI->print(dbgs())); |
| 615 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 616 | PDIRelated.insert(&*BI); |
| 617 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 618 | LLVM_DEBUG(dbgs() << " Defer: "); |
| 619 | LLVM_DEBUG(BI->print(dbgs())); |
| 620 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 623 | LLVM_DEBUG( |
| 624 | dbgs() |
| 625 | << " Report parameter debug info related/related instructions: {\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 626 | for (BasicBlock::iterator BI = GEntryBlock->begin(), BE = GEntryBlock->end(); |
| 627 | BI != BE; ++BI) { |
| 628 | |
| 629 | Instruction *I = &*BI; |
| 630 | if (PDIRelated.find(I) == PDIRelated.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 631 | LLVM_DEBUG(dbgs() << " !PDIRelated: "); |
| 632 | LLVM_DEBUG(I->print(dbgs())); |
| 633 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 634 | PDIUnrelatedWL.push_back(I); |
| 635 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 636 | LLVM_DEBUG(dbgs() << " PDIRelated: "); |
| 637 | LLVM_DEBUG(I->print(dbgs())); |
| 638 | LLVM_DEBUG(dbgs() << "\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 641 | LLVM_DEBUG(dbgs() << " }\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 642 | } |
| 643 | |
whitequark | 8f0ab25 | 2018-05-15 11:31:07 +0000 | [diff] [blame] | 644 | // Don't merge tiny functions using a thunk, since it can just end up |
| 645 | // making the function larger. |
| 646 | static bool isThunkProfitable(Function * F) { |
| 647 | if (F->size() == 1) { |
| 648 | if (F->front().size() <= 2) { |
Nicola Zaghen | 03d0b91 | 2018-05-23 15:09:29 +0000 | [diff] [blame] | 649 | LLVM_DEBUG(dbgs() << "isThunkProfitable: " << F->getName() |
| 650 | << " is too small to bother creating a thunk for\n"); |
whitequark | 8f0ab25 | 2018-05-15 11:31:07 +0000 | [diff] [blame] | 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | return true; |
| 655 | } |
| 656 | |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 657 | // Replace G with a simple tail call to bitcast(F). Also (unless |
| 658 | // MergeFunctionsPDI holds) replace direct uses of G with bitcast(F), |
| 659 | // delete G. Under MergeFunctionsPDI, we use G itself for creating |
| 660 | // the thunk as we preserve the debug info (and associated instructions) |
| 661 | // from G's entry block pertaining to G's incoming arguments which are |
| 662 | // passed on as corresponding arguments in the call that G makes to F. |
| 663 | // For better debugability, under MergeFunctionsPDI, we do not modify G's |
| 664 | // call sites to point to F even when within the same translation unit. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 665 | void MergeFunctions::writeThunk(Function *F, Function *G) { |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 666 | BasicBlock *GEntryBlock = nullptr; |
| 667 | std::vector<Instruction *> PDIUnrelatedWL; |
| 668 | BasicBlock *BB = nullptr; |
| 669 | Function *NewG = nullptr; |
| 670 | if (MergeFunctionsPDI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 671 | LLVM_DEBUG(dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new " |
| 672 | "function as thunk; retain original: " |
| 673 | << G->getName() << "()\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 674 | GEntryBlock = &G->getEntryBlock(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 675 | LLVM_DEBUG( |
| 676 | dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related " |
| 677 | "debug info for " |
| 678 | << G->getName() << "() {\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 679 | filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL); |
| 680 | GEntryBlock->getTerminator()->eraseFromParent(); |
| 681 | BB = GEntryBlock; |
| 682 | } else { |
| 683 | NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", |
| 684 | G->getParent()); |
| 685 | BB = BasicBlock::Create(F->getContext(), "", NewG); |
| 686 | } |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 687 | |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 688 | IRBuilder<> Builder(BB); |
| 689 | Function *H = MergeFunctionsPDI ? G : NewG; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 690 | SmallVector<Value *, 16> Args; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 691 | unsigned i = 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 692 | FunctionType *FFTy = F->getFunctionType(); |
Eugene Zelenko | f27d161 | 2017-10-19 21:21:30 +0000 | [diff] [blame] | 693 | for (Argument &AI : H->args()) { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 694 | Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i))); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 695 | ++i; |
| 696 | } |
| 697 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 698 | CallInst *CI = Builder.CreateCall(F, Args); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 699 | ReturnInst *RI = nullptr; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 700 | CI->setTailCall(); |
Nick Lewycky | d5bf51f | 2009-06-12 16:04:00 +0000 | [diff] [blame] | 701 | CI->setCallingConv(F->getCallingConv()); |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 702 | CI->setAttributes(F->getAttributes()); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 703 | if (H->getReturnType()->isVoidTy()) { |
| 704 | RI = Builder.CreateRetVoid(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 705 | } else { |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 706 | RI = Builder.CreateRet(createCast(Builder, CI, H->getReturnType())); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 709 | if (MergeFunctionsPDI) { |
| 710 | DISubprogram *DIS = G->getSubprogram(); |
| 711 | if (DIS) { |
| 712 | DebugLoc CIDbgLoc = DebugLoc::get(DIS->getScopeLine(), 0, DIS); |
| 713 | DebugLoc RIDbgLoc = DebugLoc::get(DIS->getScopeLine(), 0, DIS); |
| 714 | CI->setDebugLoc(CIDbgLoc); |
| 715 | RI->setDebugLoc(RIDbgLoc); |
| 716 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 717 | LLVM_DEBUG( |
| 718 | dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for " |
| 719 | << G->getName() << "()\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 720 | } |
| 721 | eraseTail(G); |
| 722 | eraseInstsUnrelatedToPDI(PDIUnrelatedWL); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 723 | LLVM_DEBUG( |
| 724 | dbgs() << "} // End of parameter related debug info filtering for: " |
| 725 | << G->getName() << "()\n"); |
Anmol P. Paralkar | 910dc8d | 2017-01-21 02:02:56 +0000 | [diff] [blame] | 726 | } else { |
| 727 | NewG->copyAttributesFrom(G); |
| 728 | NewG->takeName(G); |
| 729 | removeUsers(G); |
| 730 | G->replaceAllUsesWith(NewG); |
| 731 | G->eraseFromParent(); |
| 732 | } |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 733 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 734 | LLVM_DEBUG(dbgs() << "writeThunk: " << H->getName() << '\n'); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 735 | ++NumThunksWritten; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 738 | // Merge two equivalent functions. Upon completion, Function G is deleted. |
| 739 | void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 740 | if (F->isInterposable()) { |
| 741 | assert(G->isInterposable()); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 742 | |
whitequark | 8f0ab25 | 2018-05-15 11:31:07 +0000 | [diff] [blame] | 743 | if (!isThunkProfitable(F)) { |
| 744 | return; |
| 745 | } |
| 746 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 747 | // Make them both thunks to the same internal function. |
| 748 | Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "", |
| 749 | F->getParent()); |
| 750 | H->copyAttributesFrom(F); |
| 751 | H->takeName(F); |
| 752 | removeUsers(F); |
| 753 | F->replaceAllUsesWith(H); |
| 754 | |
| 755 | unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment()); |
| 756 | |
whitequark | 9e8197a | 2017-07-27 19:36:13 +0000 | [diff] [blame] | 757 | writeThunk(F, G); |
| 758 | writeThunk(F, H); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 759 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 760 | F->setAlignment(MaxAlignment); |
| 761 | F->setLinkage(GlobalValue::PrivateLinkage); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 762 | ++NumDoubleWeak; |
whitequark | 8f0ab25 | 2018-05-15 11:31:07 +0000 | [diff] [blame] | 763 | ++NumFunctionsMerged; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 764 | } else { |
whitequark | 8f0ab25 | 2018-05-15 11:31:07 +0000 | [diff] [blame] | 765 | // For better debugability, under MergeFunctionsPDI, we do not modify G's |
| 766 | // call sites to point to F even when within the same translation unit. |
| 767 | if (!G->isInterposable() && !MergeFunctionsPDI) { |
| 768 | if (G->hasGlobalUnnamedAddr()) { |
| 769 | // G might have been a key in our GlobalNumberState, and it's illegal |
| 770 | // to replace a key in ValueMap<GlobalValue *> with a non-global. |
| 771 | GlobalNumbers.erase(G); |
| 772 | // If G's address is not significant, replace it entirely. |
| 773 | Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); |
whitequark | 3580ac6 | 2018-11-08 03:57:55 +0000 | [diff] [blame] | 774 | removeUsers(G); |
whitequark | 8f0ab25 | 2018-05-15 11:31:07 +0000 | [diff] [blame] | 775 | G->replaceAllUsesWith(BitcastF); |
| 776 | } else { |
| 777 | // Redirect direct callers of G to F. (See note on MergeFunctionsPDI |
| 778 | // above). |
| 779 | replaceDirectCallers(G, F); |
| 780 | } |
| 781 | } |
Nick Lewycky | 3c6d34a | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 782 | |
whitequark | 8f0ab25 | 2018-05-15 11:31:07 +0000 | [diff] [blame] | 783 | // If G was internal then we may have replaced all uses of G with F. If so, |
| 784 | // stop here and delete G. There's no need for a thunk. (See note on |
| 785 | // MergeFunctionsPDI above). |
| 786 | if (G->hasLocalLinkage() && G->use_empty() && !MergeFunctionsPDI) { |
| 787 | G->eraseFromParent(); |
| 788 | ++NumFunctionsMerged; |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | if (!isThunkProfitable(F)) { |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | writeThunk(F, G); |
| 797 | ++NumFunctionsMerged; |
| 798 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 799 | } |
| 800 | |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 801 | /// Replace function F by function G. |
| 802 | void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN, |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 803 | Function *G) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 804 | Function *F = FN.getFunc(); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 805 | assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 && |
| 806 | "The two functions must be equal"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 807 | |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 808 | auto I = FNodesInTree.find(F); |
| 809 | assert(I != FNodesInTree.end() && "F should be in FNodesInTree"); |
| 810 | assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 811 | |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 812 | FnTreeType::iterator IterToFNInFnTree = I->second; |
| 813 | assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree."); |
| 814 | // Remove F -> FN and insert G -> FN |
| 815 | FNodesInTree.erase(I); |
| 816 | FNodesInTree.insert({G, IterToFNInFnTree}); |
| 817 | // Replace F with G in FN, which is stored inside the FnTree. |
| 818 | FN.replaceBy(G); |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 819 | } |
| 820 | |
whitequark | 73cb978 | 2018-11-08 03:58:01 +0000 | [diff] [blame^] | 821 | // Ordering for functions that are equal under FunctionComparator |
| 822 | static bool isFuncOrderCorrect(const Function *F, const Function *G) { |
| 823 | if (F->isInterposable() != G->isInterposable()) { |
| 824 | // Strong before weak, because the weak function may call the strong |
| 825 | // one, but not the other way around. |
| 826 | return !F->isInterposable(); |
| 827 | } |
| 828 | if (F->hasLocalLinkage() != G->hasLocalLinkage()) { |
| 829 | // External before local, because we definitely have to keep the external |
| 830 | // function, but may be able to drop the local one. |
| 831 | return !F->hasLocalLinkage(); |
| 832 | } |
| 833 | // Impose a total order (by name) on the replacement of functions. This is |
| 834 | // important when operating on more than one module independently to prevent |
| 835 | // cycles of thunks calling each other when the modules are linked together. |
| 836 | return F->getName() <= G->getName(); |
| 837 | } |
| 838 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 839 | // 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] | 840 | // that was already inserted. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 841 | bool MergeFunctions::insert(Function *NewFunction) { |
| 842 | std::pair<FnTreeType::iterator, bool> Result = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 843 | FnTree.insert(FunctionNode(NewFunction)); |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 844 | |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 845 | if (Result.second) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 846 | assert(FNodesInTree.count(NewFunction) == 0); |
| 847 | FNodesInTree.insert({NewFunction, Result.first}); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 848 | LLVM_DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() |
| 849 | << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 850 | return false; |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 851 | } |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 852 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 853 | const FunctionNode &OldF = *Result.first; |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 854 | |
whitequark | 73cb978 | 2018-11-08 03:58:01 +0000 | [diff] [blame^] | 855 | if (!isFuncOrderCorrect(OldF.getFunc(), NewFunction)) { |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame] | 856 | // Swap the two functions. |
| 857 | Function *F = OldF.getFunc(); |
| 858 | replaceFunctionInTree(*Result.first, NewFunction); |
| 859 | NewFunction = F; |
| 860 | assert(OldF.getFunc() != F && "Must have swapped the functions."); |
| 861 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 862 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 863 | LLVM_DEBUG(dbgs() << " " << OldF.getFunc()->getName() |
| 864 | << " == " << NewFunction->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 865 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 866 | Function *DeleteF = NewFunction; |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 867 | mergeTwoFunctions(OldF.getFunc(), DeleteF); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 868 | return true; |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 869 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 870 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 871 | // Remove a function from FnTree. If it was already in FnTree, add |
| 872 | // 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] | 873 | void MergeFunctions::remove(Function *F) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 874 | auto I = FNodesInTree.find(F); |
| 875 | if (I != FNodesInTree.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 876 | LLVM_DEBUG(dbgs() << "Deferred " << F->getName() << ".\n"); |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 877 | FnTree.erase(I->second); |
| 878 | // I->second has been invalidated, remove it from the FNodesInTree map to |
| 879 | // preserve the invariant. |
| 880 | FNodesInTree.erase(I); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 881 | Deferred.emplace_back(F); |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 882 | } |
Nick Lewycky | 4e250c8 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 883 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 884 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 885 | // For each instruction used by the value, remove() the function that contains |
| 886 | // the instruction. This should happen right before a call to RAUW. |
| 887 | void MergeFunctions::removeUsers(Value *V) { |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 888 | std::vector<Value *> Worklist; |
| 889 | Worklist.push_back(V); |
Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 890 | SmallPtrSet<Value*, 8> Visited; |
JF Bastien | 7289f73 | 2015-07-15 21:51:33 +0000 | [diff] [blame] | 891 | Visited.insert(V); |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 892 | while (!Worklist.empty()) { |
| 893 | Value *V = Worklist.back(); |
| 894 | Worklist.pop_back(); |
| 895 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 896 | for (User *U : V->users()) { |
| 897 | if (Instruction *I = dyn_cast<Instruction>(U)) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 898 | remove(I->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 899 | } else if (isa<GlobalValue>(U)) { |
Nick Lewycky | 540f953 | 2011-01-15 10:16:23 +0000 | [diff] [blame] | 900 | // do nothing |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 901 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
JF Bastien | 7289f73 | 2015-07-15 21:51:33 +0000 | [diff] [blame] | 902 | for (User *UU : C->users()) { |
| 903 | if (!Visited.insert(UU).second) |
| 904 | Worklist.push_back(UU); |
| 905 | } |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 906 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 907 | } |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 908 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 909 | } |