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. |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 30 | // |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 31 | // When a match is found the functions are folded. If both functions are |
| 32 | // overridable, we move the functionality into a new internal function and |
| 33 | // leave two overridable thunks to it. |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 34 | // |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // |
| 37 | // Future work: |
| 38 | // |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 39 | // * virtual functions. |
| 40 | // |
| 41 | // Many functions have their address taken by the virtual function table for |
| 42 | // 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] | 43 | // 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] | 44 | // |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 45 | // * be smarter about bitcasts. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 46 | // |
| 47 | // In order to fold functions, we will sometimes add either bitcast instructions |
| 48 | // or bitcast constant expressions. Unfortunately, this can confound further |
| 49 | // 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] | 50 | // other doesn't. We should learn to look through bitcasts. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 51 | // |
Stepan Dyatkovskiy | 471eab3 | 2014-06-22 00:57:09 +0000 | [diff] [blame] | 52 | // * Compare complex types with pointer types inside. |
| 53 | // * Compare cross-reference cases. |
| 54 | // * Compare complex expressions. |
| 55 | // |
| 56 | // All the three issues above could be described as ability to prove that |
| 57 | // fA == fB == fC == fE == fF == fG in example below: |
| 58 | // |
| 59 | // void fA() { |
| 60 | // fB(); |
| 61 | // } |
| 62 | // void fB() { |
| 63 | // fA(); |
| 64 | // } |
| 65 | // |
| 66 | // void fE() { |
| 67 | // fF(); |
| 68 | // } |
| 69 | // void fF() { |
| 70 | // fG(); |
| 71 | // } |
| 72 | // void fG() { |
| 73 | // fE(); |
| 74 | // } |
| 75 | // |
| 76 | // Simplest cross-reference case (fA <--> fB) was implemented in previous |
| 77 | // versions of MergeFunctions, though it presented only in two function pairs |
| 78 | // in test-suite (that counts >50k functions) |
| 79 | // Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A) |
| 80 | // could cover much more cases. |
| 81 | // |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 84 | #include "llvm/Transforms/IPO.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 85 | #include "llvm/ADT/DenseSet.h" |
| 86 | #include "llvm/ADT/FoldingSet.h" |
| 87 | #include "llvm/ADT/STLExtras.h" |
| 88 | #include "llvm/ADT/SmallSet.h" |
| 89 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 90 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 91 | #include "llvm/IR/Constants.h" |
| 92 | #include "llvm/IR/DataLayout.h" |
| 93 | #include "llvm/IR/IRBuilder.h" |
| 94 | #include "llvm/IR/InlineAsm.h" |
| 95 | #include "llvm/IR/Instructions.h" |
| 96 | #include "llvm/IR/LLVMContext.h" |
| 97 | #include "llvm/IR/Module.h" |
| 98 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 99 | #include "llvm/IR/ValueHandle.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 100 | #include "llvm/Pass.h" |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 101 | #include "llvm/Support/CommandLine.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 102 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 103 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 104 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 68984ed | 2010-08-31 08:29:37 +0000 | [diff] [blame] | 105 | #include <vector> |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 106 | using namespace llvm; |
| 107 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 108 | #define DEBUG_TYPE "mergefunc" |
| 109 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 110 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 111 | STATISTIC(NumThunksWritten, "Number of thunks generated"); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 112 | STATISTIC(NumAliasesWritten, "Number of aliases generated"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 113 | STATISTIC(NumDoubleWeak, "Number of new functions created"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 114 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 115 | static cl::opt<unsigned> NumFunctionsForSanityCheck( |
| 116 | "mergefunc-sanity", |
| 117 | cl::desc("How many functions in module could be used for " |
| 118 | "MergeFunctions pass sanity check. " |
| 119 | "'0' disables this check. Works only with '-debug' key."), |
| 120 | cl::init(0), cl::Hidden); |
| 121 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 122 | namespace { |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 123 | |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 124 | /// FunctionComparator - Compares two functions to determine whether or not |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 125 | /// they will generate machine code with the same behaviour. DataLayout is |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 126 | /// used if available. The comparator always fails conservatively (erring on the |
| 127 | /// side of claiming that two functions are different). |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 128 | class FunctionComparator { |
| 129 | public: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 130 | FunctionComparator(const Function *F1, const Function *F2) |
| 131 | : FnL(F1), FnR(F2) {} |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 132 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 133 | /// Test whether the two functions have equivalent behaviour. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 134 | int compare(); |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 135 | |
| 136 | private: |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 137 | /// Test whether two basic blocks have equivalent behaviour. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 138 | int compare(const BasicBlock *BBL, const BasicBlock *BBR); |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 139 | |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 140 | /// Constants comparison. |
| 141 | /// Its analog to lexicographical comparison between hypothetical numbers |
| 142 | /// of next format: |
| 143 | /// <bitcastability-trait><raw-bit-contents> |
| 144 | /// |
| 145 | /// 1. Bitcastability. |
| 146 | /// Check whether L's type could be losslessly bitcasted to R's type. |
| 147 | /// On this stage method, in case when lossless bitcast is not possible |
| 148 | /// method returns -1 or 1, thus also defining which type is greater in |
| 149 | /// context of bitcastability. |
| 150 | /// Stage 0: If types are equal in terms of cmpTypes, then we can go straight |
| 151 | /// to the contents comparison. |
| 152 | /// If types differ, remember types comparison result and check |
| 153 | /// whether we still can bitcast types. |
| 154 | /// Stage 1: Types that satisfies isFirstClassType conditions are always |
| 155 | /// greater then others. |
| 156 | /// Stage 2: Vector is greater then non-vector. |
| 157 | /// If both types are vectors, then vector with greater bitwidth is |
| 158 | /// greater. |
| 159 | /// If both types are vectors with the same bitwidth, then types |
| 160 | /// are bitcastable, and we can skip other stages, and go to contents |
| 161 | /// comparison. |
| 162 | /// Stage 3: Pointer types are greater than non-pointers. If both types are |
| 163 | /// pointers of the same address space - go to contents comparison. |
| 164 | /// Different address spaces: pointer with greater address space is |
| 165 | /// greater. |
| 166 | /// Stage 4: Types are neither vectors, nor pointers. And they differ. |
| 167 | /// We don't know how to bitcast them. So, we better don't do it, |
| 168 | /// and return types comparison result (so it determines the |
| 169 | /// relationship among constants we don't know how to bitcast). |
| 170 | /// |
| 171 | /// Just for clearance, let's see how the set of constants could look |
| 172 | /// on single dimension axis: |
| 173 | /// |
| 174 | /// [NFCT], [FCT, "others"], [FCT, pointers], [FCT, vectors] |
| 175 | /// Where: NFCT - Not a FirstClassType |
| 176 | /// FCT - FirstClassTyp: |
| 177 | /// |
| 178 | /// 2. Compare raw contents. |
| 179 | /// It ignores types on this stage and only compares bits from L and R. |
| 180 | /// Returns 0, if L and R has equivalent contents. |
| 181 | /// -1 or 1 if values are different. |
| 182 | /// Pretty trivial: |
| 183 | /// 2.1. If contents are numbers, compare numbers. |
| 184 | /// Ints with greater bitwidth are greater. Ints with same bitwidths |
| 185 | /// compared by their contents. |
| 186 | /// 2.2. "And so on". Just to avoid discrepancies with comments |
| 187 | /// perhaps it would be better to read the implementation itself. |
| 188 | /// 3. And again about overall picture. Let's look back at how the ordered set |
| 189 | /// of constants will look like: |
| 190 | /// [NFCT], [FCT, "others"], [FCT, pointers], [FCT, vectors] |
| 191 | /// |
| 192 | /// Now look, what could be inside [FCT, "others"], for example: |
| 193 | /// [FCT, "others"] = |
| 194 | /// [ |
| 195 | /// [double 0.1], [double 1.23], |
| 196 | /// [i32 1], [i32 2], |
| 197 | /// { double 1.0 }, ; StructTyID, NumElements = 1 |
| 198 | /// { i32 1 }, ; StructTyID, NumElements = 1 |
| 199 | /// { double 1, i32 1 }, ; StructTyID, NumElements = 2 |
| 200 | /// { i32 1, double 1 } ; StructTyID, NumElements = 2 |
| 201 | /// ] |
| 202 | /// |
| 203 | /// Let's explain the order. Float numbers will be less than integers, just |
| 204 | /// because of cmpType terms: FloatTyID < IntegerTyID. |
| 205 | /// Floats (with same fltSemantics) are sorted according to their value. |
| 206 | /// Then you can see integers, and they are, like a floats, |
| 207 | /// could be easy sorted among each others. |
| 208 | /// The structures. Structures are grouped at the tail, again because of their |
| 209 | /// TypeID: StructTyID > IntegerTyID > FloatTyID. |
| 210 | /// Structures with greater number of elements are greater. Structures with |
| 211 | /// greater elements going first are greater. |
| 212 | /// The same logic with vectors, arrays and other possible complex types. |
| 213 | /// |
| 214 | /// Bitcastable constants. |
| 215 | /// Let's assume, that some constant, belongs to some group of |
| 216 | /// "so-called-equal" values with different types, and at the same time |
| 217 | /// belongs to another group of constants with equal types |
| 218 | /// and "really" equal values. |
| 219 | /// |
| 220 | /// Now, prove that this is impossible: |
| 221 | /// |
| 222 | /// If constant A with type TyA is bitcastable to B with type TyB, then: |
| 223 | /// 1. All constants with equal types to TyA, are bitcastable to B. Since |
| 224 | /// those should be vectors (if TyA is vector), pointers |
| 225 | /// (if TyA is pointer), or else (if TyA equal to TyB), those types should |
| 226 | /// be equal to TyB. |
| 227 | /// 2. All constants with non-equal, but bitcastable types to TyA, are |
| 228 | /// bitcastable to B. |
| 229 | /// Once again, just because we allow it to vectors and pointers only. |
| 230 | /// This statement could be expanded as below: |
| 231 | /// 2.1. All vectors with equal bitwidth to vector A, has equal bitwidth to |
| 232 | /// vector B, and thus bitcastable to B as well. |
| 233 | /// 2.2. All pointers of the same address space, no matter what they point to, |
| 234 | /// bitcastable. So if C is pointer, it could be bitcasted to A and to B. |
| 235 | /// So any constant equal or bitcastable to A is equal or bitcastable to B. |
| 236 | /// QED. |
| 237 | /// |
| 238 | /// In another words, for pointers and vectors, we ignore top-level type and |
| 239 | /// look at their particular properties (bit-width for vectors, and |
| 240 | /// address space for pointers). |
| 241 | /// If these properties are equal - compare their contents. |
| 242 | int cmpConstants(const Constant *L, const Constant *R); |
| 243 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 244 | /// Assign or look up previously assigned numbers for the two values, and |
| 245 | /// return whether the numbers are equal. Numbers are assigned in the order |
| 246 | /// visited. |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 247 | /// Comparison order: |
| 248 | /// Stage 0: Value that is function itself is always greater then others. |
| 249 | /// If left and right values are references to their functions, then |
| 250 | /// they are equal. |
| 251 | /// Stage 1: Constants are greater than non-constants. |
| 252 | /// If both left and right are constants, then the result of |
| 253 | /// cmpConstants is used as cmpValues result. |
| 254 | /// Stage 2: InlineAsm instances are greater than others. If both left and |
| 255 | /// right are InlineAsm instances, InlineAsm* pointers casted to |
| 256 | /// integers and compared as numbers. |
| 257 | /// Stage 3: For all other cases we compare order we meet these values in |
| 258 | /// their functions. If right value was met first during scanning, |
| 259 | /// then left value is greater. |
| 260 | /// In another words, we compare serial numbers, for more details |
| 261 | /// see comments for sn_mapL and sn_mapR. |
| 262 | int cmpValues(const Value *L, const Value *R); |
| 263 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 264 | /// Compare two Instructions for equivalence, similar to |
| 265 | /// Instruction::isSameOperationAs but with modifications to the type |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 266 | /// comparison. |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 267 | /// Stages are listed in "most significant stage first" order: |
| 268 | /// On each stage below, we do comparison between some left and right |
| 269 | /// operation parts. If parts are non-equal, we assign parts comparison |
| 270 | /// result to the operation comparison result and exit from method. |
| 271 | /// Otherwise we proceed to the next stage. |
| 272 | /// Stages: |
| 273 | /// 1. Operations opcodes. Compared as numbers. |
| 274 | /// 2. Number of operands. |
| 275 | /// 3. Operation types. Compared with cmpType method. |
| 276 | /// 4. Compare operation subclass optional data as stream of bytes: |
| 277 | /// just convert it to integers and call cmpNumbers. |
| 278 | /// 5. Compare in operation operand types with cmpType in |
| 279 | /// most significant operand first order. |
| 280 | /// 6. Last stage. Check operations for some specific attributes. |
| 281 | /// For example, for Load it would be: |
| 282 | /// 6.1.Load: volatile (as boolean flag) |
| 283 | /// 6.2.Load: alignment (as integer numbers) |
| 284 | /// 6.3.Load: synch-scope (as integer numbers) |
Stepan Dyatkovskiy | 6baeb88 | 2014-06-20 19:11:56 +0000 | [diff] [blame] | 285 | /// 6.4.Load: range metadata (as integer numbers) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 286 | /// On this stage its better to see the code, since its not more than 10-15 |
| 287 | /// strings for particular instruction, and could change sometimes. |
Stepan Dyatkovskiy | 87c04618 | 2014-07-31 07:16:59 +0000 | [diff] [blame] | 288 | int cmpOperations(const Instruction *L, const Instruction *R) const; |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 289 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 290 | /// Compare two GEPs for equivalent pointer arithmetic. |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 291 | /// Parts to be compared for each comparison stage, |
| 292 | /// most significant stage first: |
| 293 | /// 1. Address space. As numbers. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 294 | /// 2. Constant offset, (using GEPOperator::accumulateConstantOffset method). |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 295 | /// 3. Pointer operand type (using cmpType method). |
| 296 | /// 4. Number of operands. |
| 297 | /// 5. Compare operands, using cmpValues method. |
Stepan Dyatkovskiy | 016dadd | 2014-08-25 08:12:45 +0000 | [diff] [blame] | 298 | int cmpGEPs(const GEPOperator *GEPL, const GEPOperator *GEPR); |
| 299 | int cmpGEPs(const GetElementPtrInst *GEPL, const GetElementPtrInst *GEPR) { |
| 300 | return cmpGEPs(cast<GEPOperator>(GEPL), cast<GEPOperator>(GEPR)); |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 303 | /// cmpType - compares two types, |
| 304 | /// defines total ordering among the types set. |
| 305 | /// |
| 306 | /// Return values: |
| 307 | /// 0 if types are equal, |
| 308 | /// -1 if Left is less than Right, |
| 309 | /// +1 if Left is greater than Right. |
| 310 | /// |
| 311 | /// Description: |
| 312 | /// Comparison is broken onto stages. Like in lexicographical comparison |
| 313 | /// stage coming first has higher priority. |
| 314 | /// On each explanation stage keep in mind total ordering properties. |
| 315 | /// |
Stepan Dyatkovskiy | 90c4436 | 2014-03-14 08:17:19 +0000 | [diff] [blame] | 316 | /// 0. Before comparison we coerce pointer types of 0 address space to |
| 317 | /// integer. |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 318 | /// We also don't bother with same type at left and right, so |
| 319 | /// just return 0 in this case. |
| 320 | /// |
| 321 | /// 1. If types are of different kind (different type IDs). |
| 322 | /// Return result of type IDs comparison, treating them as numbers. |
| 323 | /// 2. If types are vectors or integers, compare Type* values as numbers. |
| 324 | /// 3. Types has same ID, so check whether they belongs to the next group: |
| 325 | /// * Void |
| 326 | /// * Float |
| 327 | /// * Double |
| 328 | /// * X86_FP80 |
| 329 | /// * FP128 |
| 330 | /// * PPC_FP128 |
| 331 | /// * Label |
| 332 | /// * Metadata |
| 333 | /// If so - return 0, yes - we can treat these types as equal only because |
| 334 | /// their IDs are same. |
| 335 | /// 4. If Left and Right are pointers, return result of address space |
| 336 | /// comparison (numbers comparison). We can treat pointer types of same |
| 337 | /// address space as equal. |
| 338 | /// 5. If types are complex. |
| 339 | /// Then both Left and Right are to be expanded and their element types will |
| 340 | /// be checked with the same way. If we get Res != 0 on some stage, return it. |
| 341 | /// Otherwise return 0. |
| 342 | /// 6. For all other cases put llvm_unreachable. |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 343 | int cmpTypes(Type *TyL, Type *TyR) const; |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 344 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 345 | int cmpNumbers(uint64_t L, uint64_t R) const; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 346 | |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 347 | int cmpAPInts(const APInt &L, const APInt &R) const; |
Stepan Dyatkovskiy | c90308b | 2014-08-25 08:22:46 +0000 | [diff] [blame] | 348 | int cmpAPFloats(const APFloat &L, const APFloat &R) const; |
Stepan Dyatkovskiy | 5c2cc25 | 2014-05-16 08:55:34 +0000 | [diff] [blame] | 349 | int cmpStrings(StringRef L, StringRef R) const; |
| 350 | int cmpAttrs(const AttributeSet L, const AttributeSet R) const; |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 351 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 352 | // The two functions undergoing comparison. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 353 | const Function *FnL, *FnR; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 354 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 355 | /// Assign serial numbers to values from left function, and values from |
| 356 | /// right function. |
| 357 | /// Explanation: |
| 358 | /// Being comparing functions we need to compare values we meet at left and |
| 359 | /// right sides. |
| 360 | /// Its easy to sort things out for external values. It just should be |
| 361 | /// the same value at left and right. |
| 362 | /// But for local values (those were introduced inside function body) |
| 363 | /// we have to ensure they were introduced at exactly the same place, |
| 364 | /// and plays the same role. |
| 365 | /// Let's assign serial number to each value when we meet it first time. |
| 366 | /// Values that were met at same place will be with same serial numbers. |
| 367 | /// In this case it would be good to explain few points about values assigned |
| 368 | /// to BBs and other ways of implementation (see below). |
| 369 | /// |
| 370 | /// 1. Safety of BB reordering. |
| 371 | /// It's safe to change the order of BasicBlocks in function. |
| 372 | /// Relationship with other functions and serial numbering will not be |
| 373 | /// changed in this case. |
| 374 | /// As follows from FunctionComparator::compare(), we do CFG walk: we start |
| 375 | /// from the entry, and then take each terminator. So it doesn't matter how in |
| 376 | /// fact BBs are ordered in function. And since cmpValues are called during |
| 377 | /// this walk, the numbering depends only on how BBs located inside the CFG. |
| 378 | /// So the answer is - yes. We will get the same numbering. |
| 379 | /// |
| 380 | /// 2. Impossibility to use dominance properties of values. |
| 381 | /// If we compare two instruction operands: first is usage of local |
| 382 | /// variable AL from function FL, and second is usage of local variable AR |
| 383 | /// from FR, we could compare their origins and check whether they are |
| 384 | /// defined at the same place. |
| 385 | /// But, we are still not able to compare operands of PHI nodes, since those |
| 386 | /// could be operands from further BBs we didn't scan yet. |
| 387 | /// So it's impossible to use dominance properties in general. |
| 388 | DenseMap<const Value*, int> sn_mapL, sn_mapR; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 389 | }; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 390 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 391 | class FunctionNode { |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 392 | mutable AssertingVH<Function> F; |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 393 | |
| 394 | public: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 395 | FunctionNode(Function *F) : F(F) {} |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 396 | Function *getFunc() const { return F; } |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 397 | |
| 398 | /// Replace the reference to the function F by the function G, assuming their |
| 399 | /// implementations are equal. |
| 400 | void replaceBy(Function *G) const { |
| 401 | assert(!(*this < FunctionNode(G)) && !(FunctionNode(G) < *this) && |
| 402 | "The two functions must be equal"); |
| 403 | |
| 404 | F = G; |
| 405 | } |
| 406 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 407 | void release() { F = 0; } |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 408 | bool operator<(const FunctionNode &RHS) const { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 409 | return (FunctionComparator(F, RHS.getFunc()).compare()) == -1; |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 410 | } |
| 411 | }; |
Alexander Kornienko | 70bc5f1 | 2015-06-19 15:57:42 +0000 | [diff] [blame] | 412 | } // namespace |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 413 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 414 | int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const { |
| 415 | if (L < R) return -1; |
| 416 | if (L > R) return 1; |
| 417 | return 0; |
| 418 | } |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 419 | |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 420 | int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const { |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 421 | if (int Res = cmpNumbers(L.getBitWidth(), R.getBitWidth())) |
| 422 | return Res; |
| 423 | if (L.ugt(R)) return 1; |
| 424 | if (R.ugt(L)) return -1; |
| 425 | return 0; |
| 426 | } |
| 427 | |
Stepan Dyatkovskiy | c90308b | 2014-08-25 08:22:46 +0000 | [diff] [blame] | 428 | int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const { |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 429 | if (int Res = cmpNumbers((uint64_t)&L.getSemantics(), |
| 430 | (uint64_t)&R.getSemantics())) |
| 431 | return Res; |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 432 | return cmpAPInts(L.bitcastToAPInt(), R.bitcastToAPInt()); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Stepan Dyatkovskiy | 5c2cc25 | 2014-05-16 08:55:34 +0000 | [diff] [blame] | 435 | int FunctionComparator::cmpStrings(StringRef L, StringRef R) const { |
| 436 | // Prevent heavy comparison, compare sizes first. |
| 437 | if (int Res = cmpNumbers(L.size(), R.size())) |
| 438 | return Res; |
| 439 | |
| 440 | // Compare strings lexicographically only when it is necessary: only when |
| 441 | // strings are equal in size. |
| 442 | return L.compare(R); |
| 443 | } |
| 444 | |
| 445 | int FunctionComparator::cmpAttrs(const AttributeSet L, |
| 446 | const AttributeSet R) const { |
| 447 | if (int Res = cmpNumbers(L.getNumSlots(), R.getNumSlots())) |
| 448 | return Res; |
| 449 | |
| 450 | for (unsigned i = 0, e = L.getNumSlots(); i != e; ++i) { |
| 451 | AttributeSet::iterator LI = L.begin(i), LE = L.end(i), RI = R.begin(i), |
| 452 | RE = R.end(i); |
| 453 | for (; LI != LE && RI != RE; ++LI, ++RI) { |
| 454 | Attribute LA = *LI; |
| 455 | Attribute RA = *RI; |
| 456 | if (LA < RA) |
| 457 | return -1; |
| 458 | if (RA < LA) |
| 459 | return 1; |
| 460 | } |
| 461 | if (LI != LE) |
| 462 | return 1; |
| 463 | if (RI != RE) |
| 464 | return -1; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 469 | /// Constants comparison: |
| 470 | /// 1. Check whether type of L constant could be losslessly bitcasted to R |
| 471 | /// type. |
| 472 | /// 2. Compare constant contents. |
| 473 | /// For more details see declaration comments. |
| 474 | int FunctionComparator::cmpConstants(const Constant *L, const Constant *R) { |
| 475 | |
| 476 | Type *TyL = L->getType(); |
| 477 | Type *TyR = R->getType(); |
| 478 | |
| 479 | // Check whether types are bitcastable. This part is just re-factored |
| 480 | // Type::canLosslesslyBitCastTo method, but instead of returning true/false, |
| 481 | // we also pack into result which type is "less" for us. |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 482 | int TypesRes = cmpTypes(TyL, TyR); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 483 | if (TypesRes != 0) { |
| 484 | // Types are different, but check whether we can bitcast them. |
| 485 | if (!TyL->isFirstClassType()) { |
| 486 | if (TyR->isFirstClassType()) |
| 487 | return -1; |
| 488 | // Neither TyL nor TyR are values of first class type. Return the result |
| 489 | // of comparing the types |
| 490 | return TypesRes; |
| 491 | } |
| 492 | if (!TyR->isFirstClassType()) { |
| 493 | if (TyL->isFirstClassType()) |
| 494 | return 1; |
| 495 | return TypesRes; |
| 496 | } |
| 497 | |
| 498 | // Vector -> Vector conversions are always lossless if the two vector types |
| 499 | // have the same size, otherwise not. |
| 500 | unsigned TyLWidth = 0; |
| 501 | unsigned TyRWidth = 0; |
| 502 | |
| 503 | if (const VectorType *VecTyL = dyn_cast<VectorType>(TyL)) |
| 504 | TyLWidth = VecTyL->getBitWidth(); |
| 505 | if (const VectorType *VecTyR = dyn_cast<VectorType>(TyR)) |
| 506 | TyRWidth = VecTyR->getBitWidth(); |
| 507 | |
| 508 | if (TyLWidth != TyRWidth) |
| 509 | return cmpNumbers(TyLWidth, TyRWidth); |
| 510 | |
| 511 | // Zero bit-width means neither TyL nor TyR are vectors. |
| 512 | if (!TyLWidth) { |
| 513 | PointerType *PTyL = dyn_cast<PointerType>(TyL); |
| 514 | PointerType *PTyR = dyn_cast<PointerType>(TyR); |
| 515 | if (PTyL && PTyR) { |
| 516 | unsigned AddrSpaceL = PTyL->getAddressSpace(); |
| 517 | unsigned AddrSpaceR = PTyR->getAddressSpace(); |
| 518 | if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR)) |
| 519 | return Res; |
| 520 | } |
| 521 | if (PTyL) |
| 522 | return 1; |
| 523 | if (PTyR) |
| 524 | return -1; |
| 525 | |
| 526 | // TyL and TyR aren't vectors, nor pointers. We don't know how to |
| 527 | // bitcast them. |
| 528 | return TypesRes; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // OK, types are bitcastable, now check constant contents. |
| 533 | |
| 534 | if (L->isNullValue() && R->isNullValue()) |
| 535 | return TypesRes; |
| 536 | if (L->isNullValue() && !R->isNullValue()) |
| 537 | return 1; |
| 538 | if (!L->isNullValue() && R->isNullValue()) |
| 539 | return -1; |
| 540 | |
| 541 | if (int Res = cmpNumbers(L->getValueID(), R->getValueID())) |
| 542 | return Res; |
| 543 | |
| 544 | switch (L->getValueID()) { |
| 545 | case Value::UndefValueVal: return TypesRes; |
| 546 | case Value::ConstantIntVal: { |
| 547 | const APInt &LInt = cast<ConstantInt>(L)->getValue(); |
| 548 | const APInt &RInt = cast<ConstantInt>(R)->getValue(); |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 549 | return cmpAPInts(LInt, RInt); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 550 | } |
| 551 | case Value::ConstantFPVal: { |
| 552 | const APFloat &LAPF = cast<ConstantFP>(L)->getValueAPF(); |
| 553 | const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF(); |
Stepan Dyatkovskiy | c90308b | 2014-08-25 08:22:46 +0000 | [diff] [blame] | 554 | return cmpAPFloats(LAPF, RAPF); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 555 | } |
| 556 | case Value::ConstantArrayVal: { |
| 557 | const ConstantArray *LA = cast<ConstantArray>(L); |
| 558 | const ConstantArray *RA = cast<ConstantArray>(R); |
| 559 | uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements(); |
| 560 | uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements(); |
| 561 | if (int Res = cmpNumbers(NumElementsL, NumElementsR)) |
| 562 | return Res; |
| 563 | for (uint64_t i = 0; i < NumElementsL; ++i) { |
| 564 | if (int Res = cmpConstants(cast<Constant>(LA->getOperand(i)), |
| 565 | cast<Constant>(RA->getOperand(i)))) |
| 566 | return Res; |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | case Value::ConstantStructVal: { |
| 571 | const ConstantStruct *LS = cast<ConstantStruct>(L); |
| 572 | const ConstantStruct *RS = cast<ConstantStruct>(R); |
| 573 | unsigned NumElementsL = cast<StructType>(TyL)->getNumElements(); |
| 574 | unsigned NumElementsR = cast<StructType>(TyR)->getNumElements(); |
| 575 | if (int Res = cmpNumbers(NumElementsL, NumElementsR)) |
| 576 | return Res; |
| 577 | for (unsigned i = 0; i != NumElementsL; ++i) { |
| 578 | if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)), |
| 579 | cast<Constant>(RS->getOperand(i)))) |
| 580 | return Res; |
| 581 | } |
| 582 | return 0; |
| 583 | } |
| 584 | case Value::ConstantVectorVal: { |
| 585 | const ConstantVector *LV = cast<ConstantVector>(L); |
| 586 | const ConstantVector *RV = cast<ConstantVector>(R); |
| 587 | unsigned NumElementsL = cast<VectorType>(TyL)->getNumElements(); |
| 588 | unsigned NumElementsR = cast<VectorType>(TyR)->getNumElements(); |
| 589 | if (int Res = cmpNumbers(NumElementsL, NumElementsR)) |
| 590 | return Res; |
| 591 | for (uint64_t i = 0; i < NumElementsL; ++i) { |
| 592 | if (int Res = cmpConstants(cast<Constant>(LV->getOperand(i)), |
| 593 | cast<Constant>(RV->getOperand(i)))) |
| 594 | return Res; |
| 595 | } |
| 596 | return 0; |
| 597 | } |
| 598 | case Value::ConstantExprVal: { |
| 599 | const ConstantExpr *LE = cast<ConstantExpr>(L); |
| 600 | const ConstantExpr *RE = cast<ConstantExpr>(R); |
| 601 | unsigned NumOperandsL = LE->getNumOperands(); |
| 602 | unsigned NumOperandsR = RE->getNumOperands(); |
| 603 | if (int Res = cmpNumbers(NumOperandsL, NumOperandsR)) |
| 604 | return Res; |
| 605 | for (unsigned i = 0; i < NumOperandsL; ++i) { |
| 606 | if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)), |
| 607 | cast<Constant>(RE->getOperand(i)))) |
| 608 | return Res; |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | case Value::FunctionVal: |
| 613 | case Value::GlobalVariableVal: |
| 614 | case Value::GlobalAliasVal: |
| 615 | default: // Unknown constant, cast L and R pointers to numbers and compare. |
| 616 | return cmpNumbers((uint64_t)L, (uint64_t)R); |
| 617 | } |
| 618 | } |
| 619 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 620 | /// cmpType - compares two types, |
| 621 | /// defines total ordering among the types set. |
| 622 | /// See method declaration comments for more details. |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 623 | int FunctionComparator::cmpTypes(Type *TyL, Type *TyR) const { |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 624 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 625 | PointerType *PTyL = dyn_cast<PointerType>(TyL); |
| 626 | PointerType *PTyR = dyn_cast<PointerType>(TyR); |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 627 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 628 | const DataLayout &DL = FnL->getParent()->getDataLayout(); |
| 629 | if (PTyL && PTyL->getAddressSpace() == 0) |
| 630 | TyL = DL.getIntPtrType(TyL); |
| 631 | if (PTyR && PTyR->getAddressSpace() == 0) |
| 632 | TyR = DL.getIntPtrType(TyR); |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 633 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 634 | if (TyL == TyR) |
| 635 | return 0; |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 636 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 637 | if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID())) |
| 638 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 639 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 640 | switch (TyL->getTypeID()) { |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 641 | default: |
| 642 | llvm_unreachable("Unknown type!"); |
Duncan Sands | 408bb19 | 2010-07-07 07:48:00 +0000 | [diff] [blame] | 643 | // Fall through in Release mode. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 644 | case Type::IntegerTyID: |
Nick Lewycky | fb622f9 | 2011-01-26 08:50:18 +0000 | [diff] [blame] | 645 | case Type::VectorTyID: |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 646 | // TyL == TyR would have returned true earlier. |
| 647 | return cmpNumbers((uint64_t)TyL, (uint64_t)TyR); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 648 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 649 | case Type::VoidTyID: |
| 650 | case Type::FloatTyID: |
| 651 | case Type::DoubleTyID: |
| 652 | case Type::X86_FP80TyID: |
| 653 | case Type::FP128TyID: |
| 654 | case Type::PPC_FP128TyID: |
| 655 | case Type::LabelTyID: |
| 656 | case Type::MetadataTyID: |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 657 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 658 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 659 | case Type::PointerTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 660 | assert(PTyL && PTyR && "Both types must be pointers here."); |
| 661 | return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | case Type::StructTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 665 | StructType *STyL = cast<StructType>(TyL); |
| 666 | StructType *STyR = cast<StructType>(TyR); |
| 667 | if (STyL->getNumElements() != STyR->getNumElements()) |
| 668 | return cmpNumbers(STyL->getNumElements(), STyR->getNumElements()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 669 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 670 | if (STyL->isPacked() != STyR->isPacked()) |
| 671 | return cmpNumbers(STyL->isPacked(), STyR->isPacked()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 672 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 673 | for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) { |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 674 | if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i))) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 675 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 676 | } |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 677 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | case Type::FunctionTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 681 | FunctionType *FTyL = cast<FunctionType>(TyL); |
| 682 | FunctionType *FTyR = cast<FunctionType>(TyR); |
| 683 | if (FTyL->getNumParams() != FTyR->getNumParams()) |
| 684 | return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 685 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 686 | if (FTyL->isVarArg() != FTyR->isVarArg()) |
| 687 | return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg()); |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 688 | |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 689 | if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType())) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 690 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 691 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 692 | for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) { |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 693 | if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i))) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 694 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 695 | } |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 696 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Nick Lewycky | 375efe3 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 699 | case Type::ArrayTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 700 | ArrayType *ATyL = cast<ArrayType>(TyL); |
| 701 | ArrayType *ATyR = cast<ArrayType>(TyR); |
| 702 | if (ATyL->getNumElements() != ATyR->getNumElements()) |
| 703 | return cmpNumbers(ATyL->getNumElements(), ATyR->getNumElements()); |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 704 | return cmpTypes(ATyL->getElementType(), ATyR->getElementType()); |
Nick Lewycky | 375efe3 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 705 | } |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 709 | // Determine whether the two operations are the same except that pointer-to-A |
| 710 | // and pointer-to-B are equivalent. This should be kept in sync with |
| 711 | // Instruction::isSameOperationAs. |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 712 | // Read method declaration comments for more details. |
Stepan Dyatkovskiy | 87c04618 | 2014-07-31 07:16:59 +0000 | [diff] [blame] | 713 | int FunctionComparator::cmpOperations(const Instruction *L, |
| 714 | const Instruction *R) const { |
Nick Lewycky | cb1a4c2 | 2011-02-06 05:04:00 +0000 | [diff] [blame] | 715 | // Differences from Instruction::isSameOperationAs: |
| 716 | // * replace type comparison with calls to isEquivalentType. |
| 717 | // * we test for I->hasSameSubclassOptionalData (nuw/nsw/tail) at the top |
| 718 | // * because of the above, we don't test for the tail bit on calls later on |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 719 | if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode())) |
| 720 | return Res; |
| 721 | |
| 722 | if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands())) |
| 723 | return Res; |
| 724 | |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 725 | if (int Res = cmpTypes(L->getType(), R->getType())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 726 | return Res; |
| 727 | |
| 728 | if (int Res = cmpNumbers(L->getRawSubclassOptionalData(), |
| 729 | R->getRawSubclassOptionalData())) |
| 730 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 731 | |
Arnold Schwaighofer | 6a8c5f6 | 2015-05-12 21:42:22 +0000 | [diff] [blame] | 732 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) { |
| 733 | if (int Res = cmpTypes(AI->getAllocatedType(), |
| 734 | cast<AllocaInst>(R)->getAllocatedType())) |
| 735 | return Res; |
| 736 | if (int Res = |
| 737 | cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment())) |
| 738 | return Res; |
| 739 | } |
| 740 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 741 | // We have two instructions of identical opcode and #operands. Check to see |
| 742 | // if all operands are the same type |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 743 | for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) { |
| 744 | if (int Res = |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 745 | cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 746 | return Res; |
| 747 | } |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 748 | |
| 749 | // Check special state that is a part of some instructions. |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 750 | if (const LoadInst *LI = dyn_cast<LoadInst>(L)) { |
| 751 | if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile())) |
| 752 | return Res; |
| 753 | if (int Res = |
| 754 | cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment())) |
| 755 | return Res; |
| 756 | if (int Res = |
| 757 | cmpNumbers(LI->getOrdering(), cast<LoadInst>(R)->getOrdering())) |
| 758 | return Res; |
Stepan Dyatkovskiy | 6baeb88 | 2014-06-20 19:11:56 +0000 | [diff] [blame] | 759 | if (int Res = |
| 760 | cmpNumbers(LI->getSynchScope(), cast<LoadInst>(R)->getSynchScope())) |
| 761 | return Res; |
| 762 | return cmpNumbers((uint64_t)LI->getMetadata(LLVMContext::MD_range), |
| 763 | (uint64_t)cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range)); |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 764 | } |
| 765 | if (const StoreInst *SI = dyn_cast<StoreInst>(L)) { |
| 766 | if (int Res = |
| 767 | cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile())) |
| 768 | return Res; |
| 769 | if (int Res = |
| 770 | cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment())) |
| 771 | return Res; |
| 772 | if (int Res = |
| 773 | cmpNumbers(SI->getOrdering(), cast<StoreInst>(R)->getOrdering())) |
| 774 | return Res; |
| 775 | return cmpNumbers(SI->getSynchScope(), cast<StoreInst>(R)->getSynchScope()); |
| 776 | } |
| 777 | if (const CmpInst *CI = dyn_cast<CmpInst>(L)) |
| 778 | return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate()); |
| 779 | if (const CallInst *CI = dyn_cast<CallInst>(L)) { |
| 780 | if (int Res = cmpNumbers(CI->getCallingConv(), |
| 781 | cast<CallInst>(R)->getCallingConv())) |
| 782 | return Res; |
Stepan Dyatkovskiy | dee612d | 2014-07-15 10:46:51 +0000 | [diff] [blame] | 783 | if (int Res = |
| 784 | cmpAttrs(CI->getAttributes(), cast<CallInst>(R)->getAttributes())) |
| 785 | return Res; |
| 786 | return cmpNumbers( |
| 787 | (uint64_t)CI->getMetadata(LLVMContext::MD_range), |
| 788 | (uint64_t)cast<CallInst>(R)->getMetadata(LLVMContext::MD_range)); |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 789 | } |
| 790 | if (const InvokeInst *CI = dyn_cast<InvokeInst>(L)) { |
| 791 | if (int Res = cmpNumbers(CI->getCallingConv(), |
| 792 | cast<InvokeInst>(R)->getCallingConv())) |
| 793 | return Res; |
Stepan Dyatkovskiy | dee612d | 2014-07-15 10:46:51 +0000 | [diff] [blame] | 794 | if (int Res = |
| 795 | cmpAttrs(CI->getAttributes(), cast<InvokeInst>(R)->getAttributes())) |
| 796 | return Res; |
| 797 | return cmpNumbers( |
| 798 | (uint64_t)CI->getMetadata(LLVMContext::MD_range), |
| 799 | (uint64_t)cast<InvokeInst>(R)->getMetadata(LLVMContext::MD_range)); |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 800 | } |
| 801 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) { |
| 802 | ArrayRef<unsigned> LIndices = IVI->getIndices(); |
| 803 | ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices(); |
| 804 | if (int Res = cmpNumbers(LIndices.size(), RIndices.size())) |
| 805 | return Res; |
| 806 | for (size_t i = 0, e = LIndices.size(); i != e; ++i) { |
| 807 | if (int Res = cmpNumbers(LIndices[i], RIndices[i])) |
| 808 | return Res; |
| 809 | } |
| 810 | } |
| 811 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) { |
| 812 | ArrayRef<unsigned> LIndices = EVI->getIndices(); |
| 813 | ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices(); |
| 814 | if (int Res = cmpNumbers(LIndices.size(), RIndices.size())) |
| 815 | return Res; |
| 816 | for (size_t i = 0, e = LIndices.size(); i != e; ++i) { |
| 817 | if (int Res = cmpNumbers(LIndices[i], RIndices[i])) |
| 818 | return Res; |
| 819 | } |
| 820 | } |
| 821 | if (const FenceInst *FI = dyn_cast<FenceInst>(L)) { |
| 822 | if (int Res = |
| 823 | cmpNumbers(FI->getOrdering(), cast<FenceInst>(R)->getOrdering())) |
| 824 | return Res; |
| 825 | return cmpNumbers(FI->getSynchScope(), cast<FenceInst>(R)->getSynchScope()); |
| 826 | } |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 827 | |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 828 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) { |
| 829 | if (int Res = cmpNumbers(CXI->isVolatile(), |
| 830 | cast<AtomicCmpXchgInst>(R)->isVolatile())) |
| 831 | return Res; |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 832 | if (int Res = cmpNumbers(CXI->isWeak(), |
| 833 | cast<AtomicCmpXchgInst>(R)->isWeak())) |
| 834 | return Res; |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 835 | if (int Res = cmpNumbers(CXI->getSuccessOrdering(), |
| 836 | cast<AtomicCmpXchgInst>(R)->getSuccessOrdering())) |
| 837 | return Res; |
| 838 | if (int Res = cmpNumbers(CXI->getFailureOrdering(), |
| 839 | cast<AtomicCmpXchgInst>(R)->getFailureOrdering())) |
| 840 | return Res; |
| 841 | return cmpNumbers(CXI->getSynchScope(), |
| 842 | cast<AtomicCmpXchgInst>(R)->getSynchScope()); |
| 843 | } |
| 844 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) { |
| 845 | if (int Res = cmpNumbers(RMWI->getOperation(), |
| 846 | cast<AtomicRMWInst>(R)->getOperation())) |
| 847 | return Res; |
| 848 | if (int Res = cmpNumbers(RMWI->isVolatile(), |
| 849 | cast<AtomicRMWInst>(R)->isVolatile())) |
| 850 | return Res; |
| 851 | if (int Res = cmpNumbers(RMWI->getOrdering(), |
| 852 | cast<AtomicRMWInst>(R)->getOrdering())) |
| 853 | return Res; |
| 854 | return cmpNumbers(RMWI->getSynchScope(), |
| 855 | cast<AtomicRMWInst>(R)->getSynchScope()); |
| 856 | } |
| 857 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 860 | // Determine whether two GEP operations perform the same underlying arithmetic. |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 861 | // Read method declaration comments for more details. |
Stepan Dyatkovskiy | 016dadd | 2014-08-25 08:12:45 +0000 | [diff] [blame] | 862 | int FunctionComparator::cmpGEPs(const GEPOperator *GEPL, |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 863 | const GEPOperator *GEPR) { |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 864 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 865 | unsigned int ASL = GEPL->getPointerAddressSpace(); |
| 866 | unsigned int ASR = GEPR->getPointerAddressSpace(); |
| 867 | |
| 868 | if (int Res = cmpNumbers(ASL, ASR)) |
| 869 | return Res; |
| 870 | |
| 871 | // When we have target data, we can reduce the GEP down to the value in bytes |
| 872 | // added to the address. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 873 | const DataLayout &DL = FnL->getParent()->getDataLayout(); |
| 874 | unsigned BitWidth = DL.getPointerSizeInBits(ASL); |
| 875 | APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0); |
| 876 | if (GEPL->accumulateConstantOffset(DL, OffsetL) && |
| 877 | GEPR->accumulateConstantOffset(DL, OffsetR)) |
| 878 | return cmpAPInts(OffsetL, OffsetR); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 879 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 880 | if (int Res = cmpNumbers((uint64_t)GEPL->getPointerOperand()->getType(), |
| 881 | (uint64_t)GEPR->getPointerOperand()->getType())) |
| 882 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 883 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 884 | if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands())) |
| 885 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 886 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 887 | for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) { |
| 888 | if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i))) |
| 889 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 892 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 895 | /// Compare two values used by the two functions under pair-wise comparison. If |
| 896 | /// this is the first time the values are seen, they're added to the mapping so |
| 897 | /// that we will detect mismatches on next use. |
| 898 | /// See comments in declaration for more details. |
| 899 | int FunctionComparator::cmpValues(const Value *L, const Value *R) { |
| 900 | // Catch self-reference case. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 901 | if (L == FnL) { |
| 902 | if (R == FnR) |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 903 | return 0; |
| 904 | return -1; |
| 905 | } |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 906 | if (R == FnR) { |
| 907 | if (L == FnL) |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 908 | return 0; |
| 909 | return 1; |
Nick Lewycky | 13e04ae | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 910 | } |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 911 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 912 | const Constant *ConstL = dyn_cast<Constant>(L); |
| 913 | const Constant *ConstR = dyn_cast<Constant>(R); |
| 914 | if (ConstL && ConstR) { |
| 915 | if (L == R) |
| 916 | return 0; |
| 917 | return cmpConstants(ConstL, ConstR); |
| 918 | } |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 919 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 920 | if (ConstL) |
| 921 | return 1; |
| 922 | if (ConstR) |
| 923 | return -1; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 924 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 925 | const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L); |
| 926 | const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R); |
| 927 | |
| 928 | if (InlineAsmL && InlineAsmR) |
| 929 | return cmpNumbers((uint64_t)L, (uint64_t)R); |
| 930 | if (InlineAsmL) |
| 931 | return 1; |
| 932 | if (InlineAsmR) |
| 933 | return -1; |
| 934 | |
| 935 | auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())), |
| 936 | RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size())); |
| 937 | |
| 938 | return cmpNumbers(LeftSN.first->second, RightSN.first->second); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 939 | } |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 940 | // Test whether two basic blocks have equivalent behaviour. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 941 | int FunctionComparator::compare(const BasicBlock *BBL, const BasicBlock *BBR) { |
| 942 | BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end(); |
| 943 | BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end(); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 944 | |
| 945 | do { |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 946 | if (int Res = cmpValues(InstL, InstR)) |
| 947 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 948 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 949 | const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(InstL); |
| 950 | const GetElementPtrInst *GEPR = dyn_cast<GetElementPtrInst>(InstR); |
Nick Lewycky | 47b71c5 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 951 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 952 | if (GEPL && !GEPR) |
| 953 | return 1; |
| 954 | if (GEPR && !GEPL) |
| 955 | return -1; |
Nick Lewycky | 47b71c5 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 956 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 957 | if (GEPL && GEPR) { |
| 958 | if (int Res = |
| 959 | cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand())) |
| 960 | return Res; |
Stepan Dyatkovskiy | 016dadd | 2014-08-25 08:12:45 +0000 | [diff] [blame] | 961 | if (int Res = cmpGEPs(GEPL, GEPR)) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 962 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 963 | } else { |
Stepan Dyatkovskiy | 87c04618 | 2014-07-31 07:16:59 +0000 | [diff] [blame] | 964 | if (int Res = cmpOperations(InstL, InstR)) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 965 | return Res; |
| 966 | assert(InstL->getNumOperands() == InstR->getNumOperands()); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 967 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 968 | for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) { |
| 969 | Value *OpL = InstL->getOperand(i); |
| 970 | Value *OpR = InstR->getOperand(i); |
| 971 | if (int Res = cmpValues(OpL, OpR)) |
| 972 | return Res; |
| 973 | if (int Res = cmpNumbers(OpL->getValueID(), OpR->getValueID())) |
| 974 | return Res; |
| 975 | // TODO: Already checked in cmpOperation |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 976 | if (int Res = cmpTypes(OpL->getType(), OpR->getType())) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 977 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 978 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 981 | ++InstL, ++InstR; |
| 982 | } while (InstL != InstLE && InstR != InstRE); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 983 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 984 | if (InstL != InstLE && InstR == InstRE) |
| 985 | return 1; |
| 986 | if (InstL == InstLE && InstR != InstRE) |
| 987 | return -1; |
| 988 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 991 | // Test whether the two functions have equivalent behaviour. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 992 | int FunctionComparator::compare() { |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 993 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 994 | sn_mapL.clear(); |
| 995 | sn_mapR.clear(); |
| 996 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 997 | if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes())) |
| 998 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 999 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1000 | if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC())) |
| 1001 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1002 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1003 | if (FnL->hasGC()) { |
| 1004 | if (int Res = cmpNumbers((uint64_t)FnL->getGC(), (uint64_t)FnR->getGC())) |
| 1005 | return Res; |
| 1006 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1007 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1008 | if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection())) |
| 1009 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1010 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1011 | if (FnL->hasSection()) { |
| 1012 | if (int Res = cmpStrings(FnL->getSection(), FnR->getSection())) |
| 1013 | return Res; |
| 1014 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1015 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1016 | if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg())) |
| 1017 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1018 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1019 | // TODO: if it's internal and only used in direct calls, we could handle this |
| 1020 | // case too. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1021 | if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv())) |
| 1022 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1023 | |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 1024 | if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType())) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1025 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1026 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1027 | assert(FnL->arg_size() == FnR->arg_size() && |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1028 | "Identically typed functions have different numbers of args!"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1029 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1030 | // Visit the arguments so that they get enumerated in the order they're |
| 1031 | // passed in. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1032 | for (Function::const_arg_iterator ArgLI = FnL->arg_begin(), |
| 1033 | ArgRI = FnR->arg_begin(), |
| 1034 | ArgLE = FnL->arg_end(); |
| 1035 | ArgLI != ArgLE; ++ArgLI, ++ArgRI) { |
| 1036 | if (cmpValues(ArgLI, ArgRI) != 0) |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1037 | llvm_unreachable("Arguments repeat!"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1040 | // We do a CFG-ordered walk since the actual ordering of the blocks in the |
| 1041 | // linked list is immaterial. Our walk starts at the entry block for both |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 1042 | // functions, then takes each block from each terminator in order. As an |
| 1043 | // artifact, this also means that unreachable blocks are ignored. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1044 | SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 1045 | SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1. |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1046 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1047 | FnLBBs.push_back(&FnL->getEntryBlock()); |
| 1048 | FnRBBs.push_back(&FnR->getEntryBlock()); |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1049 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1050 | VisitedBBs.insert(FnLBBs[0]); |
| 1051 | while (!FnLBBs.empty()) { |
| 1052 | const BasicBlock *BBL = FnLBBs.pop_back_val(); |
| 1053 | const BasicBlock *BBR = FnRBBs.pop_back_val(); |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1054 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1055 | if (int Res = cmpValues(BBL, BBR)) |
| 1056 | return Res; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1057 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1058 | if (int Res = compare(BBL, BBR)) |
| 1059 | return Res; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1060 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1061 | const TerminatorInst *TermL = BBL->getTerminator(); |
| 1062 | const TerminatorInst *TermR = BBR->getTerminator(); |
| 1063 | |
| 1064 | assert(TermL->getNumSuccessors() == TermR->getNumSuccessors()); |
| 1065 | for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1066 | if (!VisitedBBs.insert(TermL->getSuccessor(i)).second) |
Nick Lewycky | 2b3cbac | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 1067 | continue; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1068 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1069 | FnLBBs.push_back(TermL->getSuccessor(i)); |
| 1070 | FnRBBs.push_back(TermR->getSuccessor(i)); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1073 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1076 | namespace { |
| 1077 | |
| 1078 | /// MergeFunctions finds functions which will generate identical machine code, |
| 1079 | /// by considering all pointer types to be equivalent. Once identified, |
| 1080 | /// MergeFunctions will fold them by replacing a call to one to a call to a |
| 1081 | /// bitcast of the other. |
| 1082 | /// |
| 1083 | class MergeFunctions : public ModulePass { |
| 1084 | public: |
| 1085 | static char ID; |
| 1086 | MergeFunctions() |
| 1087 | : ModulePass(ID), HasGlobalAliases(false) { |
| 1088 | initializeMergeFunctionsPass(*PassRegistry::getPassRegistry()); |
| 1089 | } |
| 1090 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 1091 | bool runOnModule(Module &M) override; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1092 | |
| 1093 | private: |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 1094 | typedef std::set<FunctionNode> FnTreeType; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1095 | |
| 1096 | /// A work queue of functions that may have been modified and should be |
| 1097 | /// analyzed again. |
| 1098 | std::vector<WeakVH> Deferred; |
| 1099 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1100 | /// Checks the rules of order relation introduced among functions set. |
| 1101 | /// Returns true, if sanity check has been passed, and false if failed. |
| 1102 | bool doSanityCheck(std::vector<WeakVH> &Worklist); |
| 1103 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1104 | /// 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] | 1105 | /// equal to one that's already present. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1106 | bool insert(Function *NewFunction); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1107 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1108 | /// 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] | 1109 | /// analysis. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1110 | void remove(Function *F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1111 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1112 | /// 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] | 1113 | /// queue the functions. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1114 | void removeUsers(Value *V); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1115 | |
| 1116 | /// Replace all direct calls of Old with calls of New. Will bitcast New if |
| 1117 | /// necessary to make types match. |
| 1118 | void replaceDirectCallers(Function *Old, Function *New); |
| 1119 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1120 | /// Merge two equivalent functions. Upon completion, G may be deleted, or may |
| 1121 | /// be converted into a thunk. In either case, it should never be visited |
| 1122 | /// again. |
| 1123 | void mergeTwoFunctions(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1124 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1125 | /// Replace G with a thunk or an alias to F. Deletes G. |
| 1126 | void writeThunkOrAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1127 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1128 | /// Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 1129 | /// of G with bitcast(F). Deletes G. |
| 1130 | void writeThunk(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1131 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1132 | /// Replace G with an alias to F. Deletes G. |
| 1133 | void writeAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1134 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1135 | /// Replace function F with function G in the function tree. |
| 1136 | void replaceFunctionInTree(FnTreeType::iterator &IterToF, Function *G); |
| 1137 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1138 | /// The set of all distinct functions. Use the insert() and remove() methods |
| 1139 | /// to modify it. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1140 | FnTreeType FnTree; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1141 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1142 | /// Whether or not the target supports global aliases. |
| 1143 | bool HasGlobalAliases; |
| 1144 | }; |
| 1145 | |
| 1146 | } // end anonymous namespace |
| 1147 | |
| 1148 | char MergeFunctions::ID = 0; |
| 1149 | INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false) |
| 1150 | |
| 1151 | ModulePass *llvm::createMergeFunctionsPass() { |
| 1152 | return new MergeFunctions(); |
| 1153 | } |
| 1154 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1155 | bool MergeFunctions::doSanityCheck(std::vector<WeakVH> &Worklist) { |
| 1156 | if (const unsigned Max = NumFunctionsForSanityCheck) { |
| 1157 | unsigned TripleNumber = 0; |
| 1158 | bool Valid = true; |
| 1159 | |
| 1160 | dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n"; |
| 1161 | |
| 1162 | unsigned i = 0; |
| 1163 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), E = Worklist.end(); |
| 1164 | I != E && i < Max; ++I, ++i) { |
| 1165 | unsigned j = i; |
| 1166 | for (std::vector<WeakVH>::iterator J = I; J != E && j < Max; ++J, ++j) { |
| 1167 | Function *F1 = cast<Function>(*I); |
| 1168 | Function *F2 = cast<Function>(*J); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1169 | int Res1 = FunctionComparator(F1, F2).compare(); |
| 1170 | int Res2 = FunctionComparator(F2, F1).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1171 | |
| 1172 | // If F1 <= F2, then F2 >= F1, otherwise report failure. |
| 1173 | if (Res1 != -Res2) { |
| 1174 | dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber |
| 1175 | << "\n"; |
| 1176 | F1->dump(); |
| 1177 | F2->dump(); |
| 1178 | Valid = false; |
| 1179 | } |
| 1180 | |
| 1181 | if (Res1 == 0) |
| 1182 | continue; |
| 1183 | |
| 1184 | unsigned k = j; |
| 1185 | for (std::vector<WeakVH>::iterator K = J; K != E && k < Max; |
| 1186 | ++k, ++K, ++TripleNumber) { |
| 1187 | if (K == J) |
| 1188 | continue; |
| 1189 | |
| 1190 | Function *F3 = cast<Function>(*K); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1191 | int Res3 = FunctionComparator(F1, F3).compare(); |
| 1192 | int Res4 = FunctionComparator(F2, F3).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1193 | |
| 1194 | bool Transitive = true; |
| 1195 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1196 | if (Res1 != 0 && Res1 == Res4) { |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 1197 | // F1 > F2, F2 > F3 => F1 > F3 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1198 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 1199 | } else if (Res3 != 0 && Res3 == -Res4) { |
| 1200 | // F1 > F3, F3 > F2 => F1 > F2 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1201 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 1202 | } else if (Res4 != 0 && -Res3 == Res4) { |
| 1203 | // F2 > F3, F3 > F1 => F2 > F1 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1204 | Transitive = Res4 == -Res1; |
| 1205 | } |
| 1206 | |
| 1207 | if (!Transitive) { |
| 1208 | dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: " |
| 1209 | << TripleNumber << "\n"; |
| 1210 | dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", " |
| 1211 | << Res4 << "\n"; |
| 1212 | F1->dump(); |
| 1213 | F2->dump(); |
| 1214 | F3->dump(); |
| 1215 | Valid = false; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n"; |
| 1222 | return Valid; |
| 1223 | } |
| 1224 | return true; |
| 1225 | } |
| 1226 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1227 | bool MergeFunctions::runOnModule(Module &M) { |
| 1228 | bool Changed = false; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1229 | |
| 1230 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 1231 | if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) |
| 1232 | Deferred.push_back(WeakVH(I)); |
| 1233 | } |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1234 | |
| 1235 | do { |
| 1236 | std::vector<WeakVH> Worklist; |
| 1237 | Deferred.swap(Worklist); |
| 1238 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1239 | DEBUG(doSanityCheck(Worklist)); |
| 1240 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1241 | DEBUG(dbgs() << "size of module: " << M.size() << '\n'); |
| 1242 | DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); |
| 1243 | |
| 1244 | // Insert only strong functions and merge them. Strong function merging |
| 1245 | // always deletes one of them. |
| 1246 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), |
| 1247 | E = Worklist.end(); I != E; ++I) { |
| 1248 | if (!*I) continue; |
| 1249 | Function *F = cast<Function>(*I); |
| 1250 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() && |
| 1251 | !F->mayBeOverridden()) { |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1252 | Changed |= insert(F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | // Insert only weak functions and merge them. By doing these second we |
| 1257 | // create thunks to the strong function when possible. When two weak |
| 1258 | // functions are identical, we create a new strong function with two weak |
| 1259 | // weak thunks to it which are identical but not mergable. |
| 1260 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), |
| 1261 | E = Worklist.end(); I != E; ++I) { |
| 1262 | if (!*I) continue; |
| 1263 | Function *F = cast<Function>(*I); |
| 1264 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() && |
| 1265 | F->mayBeOverridden()) { |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1266 | Changed |= insert(F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1269 | DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n'); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1270 | } while (!Deferred.empty()); |
| 1271 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1272 | FnTree.clear(); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1273 | |
| 1274 | return Changed; |
| 1275 | } |
| 1276 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1277 | // Replace direct callers of Old with New. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1278 | void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { |
| 1279 | Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1280 | for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) { |
| 1281 | Use *U = &*UI; |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1282 | ++UI; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1283 | CallSite CS(U->getUser()); |
| 1284 | if (CS && CS.isCallee(U)) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1285 | remove(CS.getInstruction()->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1286 | U->set(BitcastNew); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1291 | // Replace G with an alias to F if possible, or else a thunk to F. Deletes G. |
| 1292 | void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) { |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1293 | if (HasGlobalAliases && G->hasUnnamedAddr()) { |
| 1294 | if (G->hasExternalLinkage() || G->hasLocalLinkage() || |
| 1295 | G->hasWeakLinkage()) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1296 | writeAlias(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | } |
| 1300 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1301 | writeThunk(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1304 | // Helper for writeThunk, |
| 1305 | // Selects proper bitcast operation, |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1306 | // but a bit simpler then CastInst::getCastOpcode. |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 1307 | static Value *createCast(IRBuilder<false> &Builder, Value *V, Type *DestTy) { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1308 | Type *SrcTy = V->getType(); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 1309 | if (SrcTy->isStructTy()) { |
| 1310 | assert(DestTy->isStructTy()); |
| 1311 | assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements()); |
| 1312 | Value *Result = UndefValue::get(DestTy); |
| 1313 | for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) { |
| 1314 | Value *Element = createCast( |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1315 | Builder, Builder.CreateExtractValue(V, makeArrayRef(I)), |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 1316 | DestTy->getStructElementType(I)); |
| 1317 | |
| 1318 | Result = |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1319 | Builder.CreateInsertValue(Result, Element, makeArrayRef(I)); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 1320 | } |
| 1321 | return Result; |
| 1322 | } |
| 1323 | assert(!DestTy->isStructTy()); |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1324 | if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) |
| 1325 | return Builder.CreateIntToPtr(V, DestTy); |
| 1326 | else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) |
| 1327 | return Builder.CreatePtrToInt(V, DestTy); |
| 1328 | else |
| 1329 | return Builder.CreateBitCast(V, DestTy); |
| 1330 | } |
| 1331 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1332 | // Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 1333 | // of G with bitcast(F). Deletes G. |
| 1334 | void MergeFunctions::writeThunk(Function *F, Function *G) { |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1335 | if (!G->mayBeOverridden()) { |
| 1336 | // Redirect direct callers of G to F. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1337 | replaceDirectCallers(G, F); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1340 | // 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] | 1341 | // stop here and delete G. There's no need for a thunk. |
| 1342 | if (G->hasLocalLinkage() && G->use_empty()) { |
| 1343 | G->eraseFromParent(); |
| 1344 | return; |
| 1345 | } |
| 1346 | |
Nick Lewycky | 25675ac | 2009-06-12 15:56:56 +0000 | [diff] [blame] | 1347 | Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", |
| 1348 | G->getParent()); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1349 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG); |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1350 | IRBuilder<false> Builder(BB); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1351 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1352 | SmallVector<Value *, 16> Args; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1353 | unsigned i = 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1354 | FunctionType *FFTy = F->getFunctionType(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1355 | for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end(); |
| 1356 | AI != AE; ++AI) { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1357 | Args.push_back(createCast(Builder, (Value*)AI, FFTy->getParamType(i))); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1358 | ++i; |
| 1359 | } |
| 1360 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1361 | CallInst *CI = Builder.CreateCall(F, Args); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1362 | CI->setTailCall(); |
Nick Lewycky | d5bf51f | 2009-06-12 16:04:00 +0000 | [diff] [blame] | 1363 | CI->setCallingConv(F->getCallingConv()); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1364 | if (NewG->getReturnType()->isVoidTy()) { |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1365 | Builder.CreateRetVoid(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1366 | } else { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1367 | Builder.CreateRet(createCast(Builder, CI, NewG->getReturnType())); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | NewG->copyAttributesFrom(G); |
| 1371 | NewG->takeName(G); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1372 | removeUsers(G); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1373 | G->replaceAllUsesWith(NewG); |
| 1374 | G->eraseFromParent(); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1375 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1376 | DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n'); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1377 | ++NumThunksWritten; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1380 | // Replace G with an alias to F and delete G. |
| 1381 | void MergeFunctions::writeAlias(Function *F, Function *G) { |
Rafael Espindola | 4fe0094 | 2014-05-16 13:34:04 +0000 | [diff] [blame] | 1382 | PointerType *PTy = G->getType(); |
David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 1383 | auto *GA = GlobalAlias::create(PTy, G->getLinkage(), "", F); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1384 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 1385 | GA->takeName(G); |
| 1386 | GA->setVisibility(G->getVisibility()); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1387 | removeUsers(G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1388 | G->replaceAllUsesWith(GA); |
| 1389 | G->eraseFromParent(); |
| 1390 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1391 | DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n'); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1392 | ++NumAliasesWritten; |
| 1393 | } |
| 1394 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1395 | // Merge two equivalent functions. Upon completion, Function G is deleted. |
| 1396 | void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1397 | if (F->mayBeOverridden()) { |
| 1398 | assert(G->mayBeOverridden()); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1399 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 1400 | // Make them both thunks to the same internal function. |
| 1401 | Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "", |
| 1402 | F->getParent()); |
| 1403 | H->copyAttributesFrom(F); |
| 1404 | H->takeName(F); |
| 1405 | removeUsers(F); |
| 1406 | F->replaceAllUsesWith(H); |
| 1407 | |
| 1408 | unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment()); |
| 1409 | |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1410 | if (HasGlobalAliases) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1411 | writeAlias(F, G); |
| 1412 | writeAlias(F, H); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1413 | } else { |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 1414 | writeThunk(F, G); |
| 1415 | writeThunk(F, H); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1416 | } |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1417 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 1418 | F->setAlignment(MaxAlignment); |
| 1419 | F->setLinkage(GlobalValue::PrivateLinkage); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1420 | ++NumDoubleWeak; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1421 | } else { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1422 | writeThunkOrAlias(F, G); |
Nick Lewycky | 3c6d34a | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1425 | ++NumFunctionsMerged; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1428 | /// Replace function F for function G in the map. |
| 1429 | void MergeFunctions::replaceFunctionInTree(FnTreeType::iterator &IterToF, |
| 1430 | Function *G) { |
| 1431 | Function *F = IterToF->getFunc(); |
| 1432 | |
| 1433 | // A total order is already guaranteed otherwise because we process strong |
| 1434 | // functions before weak functions. |
Denis Protivensky | c09e376 | 2015-06-09 09:28:37 +0000 | [diff] [blame] | 1435 | assert(((F->mayBeOverridden() && G->mayBeOverridden()) || |
| 1436 | (!F->mayBeOverridden() && !G->mayBeOverridden())) && |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1437 | "Only change functions if both are strong or both are weak"); |
Arnold Schwaighofer | 003c2e9 | 2015-06-09 00:17:40 +0000 | [diff] [blame] | 1438 | (void)F; |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1439 | |
| 1440 | IterToF->replaceBy(G); |
| 1441 | } |
| 1442 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1443 | // 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] | 1444 | // that was already inserted. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1445 | bool MergeFunctions::insert(Function *NewFunction) { |
| 1446 | std::pair<FnTreeType::iterator, bool> Result = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1447 | FnTree.insert(FunctionNode(NewFunction)); |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1448 | |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 1449 | if (Result.second) { |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1450 | DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1451 | return false; |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 1452 | } |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1453 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 1454 | const FunctionNode &OldF = *Result.first; |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1455 | |
Matt Arsenault | 517d84e | 2013-10-01 18:05:30 +0000 | [diff] [blame] | 1456 | // Don't merge tiny functions, since it can just end up making the function |
| 1457 | // larger. |
| 1458 | // FIXME: Should still merge them if they are unnamed_addr and produce an |
| 1459 | // alias. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1460 | if (NewFunction->size() == 1) { |
| 1461 | if (NewFunction->front().size() <= 2) { |
| 1462 | DEBUG(dbgs() << NewFunction->getName() |
| 1463 | << " is to small to bother merging\n"); |
Matt Arsenault | 517d84e | 2013-10-01 18:05:30 +0000 | [diff] [blame] | 1464 | return false; |
| 1465 | } |
| 1466 | } |
| 1467 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1468 | // Impose a total order (by name) on the replacement of functions. This is |
| 1469 | // important when operating on more than one module independently to prevent |
| 1470 | // cycles of thunks calling each other when the modules are linked together. |
| 1471 | // |
| 1472 | // When one function is weak and the other is strong there is an order imposed |
| 1473 | // already. We process strong functions before weak functions. |
| 1474 | if ((OldF.getFunc()->mayBeOverridden() && NewFunction->mayBeOverridden()) || |
| 1475 | (!OldF.getFunc()->mayBeOverridden() && !NewFunction->mayBeOverridden())) |
| 1476 | if (OldF.getFunc()->getName() > NewFunction->getName()) { |
| 1477 | // Swap the two functions. |
| 1478 | Function *F = OldF.getFunc(); |
| 1479 | replaceFunctionInTree(Result.first, NewFunction); |
| 1480 | NewFunction = F; |
| 1481 | assert(OldF.getFunc() != F && "Must have swapped the functions."); |
| 1482 | } |
| 1483 | |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1484 | // Never thunk a strong function to a weak function. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1485 | assert(!OldF.getFunc()->mayBeOverridden() || NewFunction->mayBeOverridden()); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1486 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1487 | DEBUG(dbgs() << " " << OldF.getFunc()->getName() |
| 1488 | << " == " << NewFunction->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1489 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1490 | Function *DeleteF = NewFunction; |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1491 | mergeTwoFunctions(OldF.getFunc(), DeleteF); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1492 | return true; |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1493 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1494 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1495 | // Remove a function from FnTree. If it was already in FnTree, add |
| 1496 | // 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] | 1497 | void MergeFunctions::remove(Function *F) { |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 1498 | // We need to make sure we remove F, not a function "equal" to F per the |
| 1499 | // function equality comparator. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1500 | FnTreeType::iterator found = FnTree.find(FunctionNode(F)); |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1501 | size_t Erased = 0; |
| 1502 | if (found != FnTree.end() && found->getFunc() == F) { |
| 1503 | Erased = 1; |
| 1504 | FnTree.erase(found); |
| 1505 | } |
| 1506 | |
| 1507 | if (Erased) { |
| 1508 | DEBUG(dbgs() << "Removed " << F->getName() |
| 1509 | << " from set and deferred it.\n"); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 1510 | Deferred.emplace_back(F); |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 1511 | } |
Nick Lewycky | 4e250c8 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 1512 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1513 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1514 | // For each instruction used by the value, remove() the function that contains |
| 1515 | // the instruction. This should happen right before a call to RAUW. |
| 1516 | void MergeFunctions::removeUsers(Value *V) { |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 1517 | std::vector<Value *> Worklist; |
| 1518 | Worklist.push_back(V); |
| 1519 | while (!Worklist.empty()) { |
| 1520 | Value *V = Worklist.back(); |
| 1521 | Worklist.pop_back(); |
| 1522 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1523 | for (User *U : V->users()) { |
| 1524 | if (Instruction *I = dyn_cast<Instruction>(U)) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1525 | remove(I->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1526 | } else if (isa<GlobalValue>(U)) { |
Nick Lewycky | 540f953 | 2011-01-15 10:16:23 +0000 | [diff] [blame] | 1527 | // do nothing |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1528 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
| 1529 | for (User *UU : C->users()) |
| 1530 | Worklist.push_back(UU); |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 1531 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1532 | } |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 1533 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1534 | } |