Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1 | //===- MergeFunctions.cpp - Merge identical functions ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass looks for equivalent functions that are mergable and folds them. |
| 11 | // |
Stepan Dyatkovskiy | 471eab3 | 2014-06-22 00:57:09 +0000 | [diff] [blame] | 12 | // Order relation is defined on set of functions. It was made through |
| 13 | // special function comparison procedure that returns |
| 14 | // 0 when functions are equal, |
| 15 | // -1 when Left function is less than right function, and |
| 16 | // 1 for opposite case. We need total-ordering, so we need to maintain |
| 17 | // four properties on the functions set: |
| 18 | // a <= a (reflexivity) |
| 19 | // if a <= b and b <= a then a = b (antisymmetry) |
| 20 | // if a <= b and b <= c then a <= c (transitivity). |
| 21 | // for all a and b: a <= b or b <= a (totality). |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 22 | // |
Stepan Dyatkovskiy | 471eab3 | 2014-06-22 00:57:09 +0000 | [diff] [blame] | 23 | // Comparison iterates through each instruction in each basic block. |
| 24 | // Functions are kept on binary tree. For each new function F we perform |
| 25 | // lookup in binary tree. |
| 26 | // In practice it works the following way: |
| 27 | // -- We define Function* container class with custom "operator<" (FunctionPtr). |
| 28 | // -- "FunctionPtr" instances are stored in std::set collection, so every |
| 29 | // std::set::insert operation will give you result in log(N) time. |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 30 | // |
| 31 | // As an optimization, a hash of the function structure is calculated first, and |
| 32 | // two functions are only compared if they have the same hash. This hash is |
| 33 | // cheap to compute, and has the property that if function F == G according to |
| 34 | // the comparison function, then hash(F) == hash(G). This consistency property |
| 35 | // is critical to ensuring all possible merging opportunities are exploited. |
| 36 | // Collisions in the hash affect the speed of the pass but not the correctness |
| 37 | // or determinism of the resulting transformation. |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 38 | // |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 39 | // When a match is found the functions are folded. If both functions are |
| 40 | // overridable, we move the functionality into a new internal function and |
| 41 | // leave two overridable thunks to it. |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 42 | // |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // |
| 45 | // Future work: |
| 46 | // |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 47 | // * virtual functions. |
| 48 | // |
| 49 | // Many functions have their address taken by the virtual function table for |
| 50 | // the object they belong to. However, as long as it's only used for a lookup |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 51 | // and call, this is irrelevant, and we'd like to fold such functions. |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 52 | // |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 53 | // * be smarter about bitcasts. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 54 | // |
| 55 | // In order to fold functions, we will sometimes add either bitcast instructions |
| 56 | // or bitcast constant expressions. Unfortunately, this can confound further |
| 57 | // analysis since the two functions differ where one has a bitcast and the |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 58 | // other doesn't. We should learn to look through bitcasts. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 59 | // |
Stepan Dyatkovskiy | 471eab3 | 2014-06-22 00:57:09 +0000 | [diff] [blame] | 60 | // * Compare complex types with pointer types inside. |
| 61 | // * Compare cross-reference cases. |
| 62 | // * Compare complex expressions. |
| 63 | // |
| 64 | // All the three issues above could be described as ability to prove that |
| 65 | // fA == fB == fC == fE == fF == fG in example below: |
| 66 | // |
| 67 | // void fA() { |
| 68 | // fB(); |
| 69 | // } |
| 70 | // void fB() { |
| 71 | // fA(); |
| 72 | // } |
| 73 | // |
| 74 | // void fE() { |
| 75 | // fF(); |
| 76 | // } |
| 77 | // void fF() { |
| 78 | // fG(); |
| 79 | // } |
| 80 | // void fG() { |
| 81 | // fE(); |
| 82 | // } |
| 83 | // |
| 84 | // Simplest cross-reference case (fA <--> fB) was implemented in previous |
| 85 | // versions of MergeFunctions, though it presented only in two function pairs |
| 86 | // in test-suite (that counts >50k functions) |
| 87 | // Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A) |
| 88 | // could cover much more cases. |
| 89 | // |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 92 | #include "llvm/ADT/Hashing.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 93 | #include "llvm/ADT/STLExtras.h" |
| 94 | #include "llvm/ADT/SmallSet.h" |
| 95 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 96 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 97 | #include "llvm/IR/Constants.h" |
| 98 | #include "llvm/IR/DataLayout.h" |
| 99 | #include "llvm/IR/IRBuilder.h" |
| 100 | #include "llvm/IR/InlineAsm.h" |
| 101 | #include "llvm/IR/Instructions.h" |
| 102 | #include "llvm/IR/LLVMContext.h" |
| 103 | #include "llvm/IR/Module.h" |
| 104 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 105 | #include "llvm/IR/ValueHandle.h" |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 106 | #include "llvm/IR/ValueMap.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 107 | #include "llvm/Pass.h" |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 108 | #include "llvm/Support/CommandLine.h" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 109 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 110 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 111 | #include "llvm/Support/raw_ostream.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 112 | #include "llvm/Transforms/IPO.h" |
Nick Lewycky | 68984ed | 2010-08-31 08:29:37 +0000 | [diff] [blame] | 113 | #include <vector> |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 114 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 115 | using namespace llvm; |
| 116 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 117 | #define DEBUG_TYPE "mergefunc" |
| 118 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 119 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 120 | STATISTIC(NumThunksWritten, "Number of thunks generated"); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 121 | STATISTIC(NumAliasesWritten, "Number of aliases generated"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 122 | STATISTIC(NumDoubleWeak, "Number of new functions created"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 123 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 124 | static cl::opt<unsigned> NumFunctionsForSanityCheck( |
| 125 | "mergefunc-sanity", |
| 126 | cl::desc("How many functions in module could be used for " |
| 127 | "MergeFunctions pass sanity check. " |
| 128 | "'0' disables this check. Works only with '-debug' key."), |
| 129 | cl::init(0), cl::Hidden); |
| 130 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 131 | namespace { |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 132 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 133 | /// GlobalNumberState assigns an integer to each global value in the program, |
| 134 | /// which is used by the comparison routine to order references to globals. This |
| 135 | /// state must be preserved throughout the pass, because Functions and other |
| 136 | /// globals need to maintain their relative order. Globals are assigned a number |
| 137 | /// when they are first visited. This order is deterministic, and so the |
| 138 | /// assigned numbers are as well. When two functions are merged, neither number |
| 139 | /// is updated. If the symbols are weak, this would be incorrect. If they are |
| 140 | /// strong, then one will be replaced at all references to the other, and so |
| 141 | /// direct callsites will now see one or the other symbol, and no update is |
| 142 | /// necessary. Note that if we were guaranteed unique names, we could just |
| 143 | /// compare those, but this would not work for stripped bitcodes or for those |
| 144 | /// few symbols without a name. |
| 145 | class GlobalNumberState { |
| 146 | struct Config : ValueMapConfig<GlobalValue*> { |
| 147 | enum { FollowRAUW = false }; |
| 148 | }; |
| 149 | // Each GlobalValue is mapped to an identifier. The Config ensures when RAUW |
| 150 | // occurs, the mapping does not change. Tracking changes is unnecessary, and |
| 151 | // also problematic for weak symbols (which may be overwritten). |
| 152 | typedef ValueMap<GlobalValue *, uint64_t, Config> ValueNumberMap; |
| 153 | ValueNumberMap GlobalNumbers; |
| 154 | // The next unused serial number to assign to a global. |
| 155 | uint64_t NextNumber; |
| 156 | public: |
| 157 | GlobalNumberState() : GlobalNumbers(), NextNumber(0) {} |
| 158 | uint64_t getNumber(GlobalValue* Global) { |
| 159 | ValueNumberMap::iterator MapIter; |
| 160 | bool Inserted; |
| 161 | std::tie(MapIter, Inserted) = GlobalNumbers.insert({Global, NextNumber}); |
| 162 | if (Inserted) |
| 163 | NextNumber++; |
| 164 | return MapIter->second; |
| 165 | } |
Arnold Schwaighofer | 0591c5d | 2015-10-05 17:26:36 +0000 | [diff] [blame] | 166 | void clear() { |
| 167 | GlobalNumbers.clear(); |
| 168 | } |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 171 | /// FunctionComparator - Compares two functions to determine whether or not |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 172 | /// they will generate machine code with the same behaviour. DataLayout is |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 173 | /// used if available. The comparator always fails conservatively (erring on the |
| 174 | /// side of claiming that two functions are different). |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 175 | class FunctionComparator { |
| 176 | public: |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 177 | FunctionComparator(const Function *F1, const Function *F2, |
| 178 | GlobalNumberState* GN) |
| 179 | : FnL(F1), FnR(F2), GlobalNumbers(GN) {} |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 180 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 181 | /// Test whether the two functions have equivalent behaviour. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 182 | int compare(); |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 183 | /// Hash a function. Equivalent functions will have the same hash, and unequal |
| 184 | /// functions will have different hashes with high probability. |
| 185 | typedef uint64_t FunctionHash; |
| 186 | static FunctionHash functionHash(Function &); |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 187 | |
| 188 | private: |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 189 | /// Test whether two basic blocks have equivalent behaviour. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 190 | int cmpBasicBlocks(const BasicBlock *BBL, const BasicBlock *BBR) const; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 191 | |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 192 | /// Constants comparison. |
| 193 | /// Its analog to lexicographical comparison between hypothetical numbers |
| 194 | /// of next format: |
| 195 | /// <bitcastability-trait><raw-bit-contents> |
| 196 | /// |
| 197 | /// 1. Bitcastability. |
| 198 | /// Check whether L's type could be losslessly bitcasted to R's type. |
| 199 | /// On this stage method, in case when lossless bitcast is not possible |
| 200 | /// method returns -1 or 1, thus also defining which type is greater in |
| 201 | /// context of bitcastability. |
| 202 | /// Stage 0: If types are equal in terms of cmpTypes, then we can go straight |
| 203 | /// to the contents comparison. |
| 204 | /// If types differ, remember types comparison result and check |
| 205 | /// whether we still can bitcast types. |
| 206 | /// Stage 1: Types that satisfies isFirstClassType conditions are always |
| 207 | /// greater then others. |
| 208 | /// Stage 2: Vector is greater then non-vector. |
| 209 | /// If both types are vectors, then vector with greater bitwidth is |
| 210 | /// greater. |
| 211 | /// If both types are vectors with the same bitwidth, then types |
| 212 | /// are bitcastable, and we can skip other stages, and go to contents |
| 213 | /// comparison. |
| 214 | /// Stage 3: Pointer types are greater than non-pointers. If both types are |
| 215 | /// pointers of the same address space - go to contents comparison. |
| 216 | /// Different address spaces: pointer with greater address space is |
| 217 | /// greater. |
| 218 | /// Stage 4: Types are neither vectors, nor pointers. And they differ. |
| 219 | /// We don't know how to bitcast them. So, we better don't do it, |
| 220 | /// and return types comparison result (so it determines the |
| 221 | /// relationship among constants we don't know how to bitcast). |
| 222 | /// |
| 223 | /// Just for clearance, let's see how the set of constants could look |
| 224 | /// on single dimension axis: |
| 225 | /// |
| 226 | /// [NFCT], [FCT, "others"], [FCT, pointers], [FCT, vectors] |
| 227 | /// Where: NFCT - Not a FirstClassType |
| 228 | /// FCT - FirstClassTyp: |
| 229 | /// |
| 230 | /// 2. Compare raw contents. |
| 231 | /// It ignores types on this stage and only compares bits from L and R. |
| 232 | /// Returns 0, if L and R has equivalent contents. |
| 233 | /// -1 or 1 if values are different. |
| 234 | /// Pretty trivial: |
| 235 | /// 2.1. If contents are numbers, compare numbers. |
| 236 | /// Ints with greater bitwidth are greater. Ints with same bitwidths |
| 237 | /// compared by their contents. |
| 238 | /// 2.2. "And so on". Just to avoid discrepancies with comments |
| 239 | /// perhaps it would be better to read the implementation itself. |
| 240 | /// 3. And again about overall picture. Let's look back at how the ordered set |
| 241 | /// of constants will look like: |
| 242 | /// [NFCT], [FCT, "others"], [FCT, pointers], [FCT, vectors] |
| 243 | /// |
| 244 | /// Now look, what could be inside [FCT, "others"], for example: |
| 245 | /// [FCT, "others"] = |
| 246 | /// [ |
| 247 | /// [double 0.1], [double 1.23], |
| 248 | /// [i32 1], [i32 2], |
| 249 | /// { double 1.0 }, ; StructTyID, NumElements = 1 |
| 250 | /// { i32 1 }, ; StructTyID, NumElements = 1 |
| 251 | /// { double 1, i32 1 }, ; StructTyID, NumElements = 2 |
| 252 | /// { i32 1, double 1 } ; StructTyID, NumElements = 2 |
| 253 | /// ] |
| 254 | /// |
| 255 | /// Let's explain the order. Float numbers will be less than integers, just |
| 256 | /// because of cmpType terms: FloatTyID < IntegerTyID. |
| 257 | /// Floats (with same fltSemantics) are sorted according to their value. |
| 258 | /// Then you can see integers, and they are, like a floats, |
| 259 | /// could be easy sorted among each others. |
| 260 | /// The structures. Structures are grouped at the tail, again because of their |
| 261 | /// TypeID: StructTyID > IntegerTyID > FloatTyID. |
| 262 | /// Structures with greater number of elements are greater. Structures with |
| 263 | /// greater elements going first are greater. |
| 264 | /// The same logic with vectors, arrays and other possible complex types. |
| 265 | /// |
| 266 | /// Bitcastable constants. |
| 267 | /// Let's assume, that some constant, belongs to some group of |
| 268 | /// "so-called-equal" values with different types, and at the same time |
| 269 | /// belongs to another group of constants with equal types |
| 270 | /// and "really" equal values. |
| 271 | /// |
| 272 | /// Now, prove that this is impossible: |
| 273 | /// |
| 274 | /// If constant A with type TyA is bitcastable to B with type TyB, then: |
| 275 | /// 1. All constants with equal types to TyA, are bitcastable to B. Since |
| 276 | /// those should be vectors (if TyA is vector), pointers |
| 277 | /// (if TyA is pointer), or else (if TyA equal to TyB), those types should |
| 278 | /// be equal to TyB. |
| 279 | /// 2. All constants with non-equal, but bitcastable types to TyA, are |
| 280 | /// bitcastable to B. |
| 281 | /// Once again, just because we allow it to vectors and pointers only. |
| 282 | /// This statement could be expanded as below: |
| 283 | /// 2.1. All vectors with equal bitwidth to vector A, has equal bitwidth to |
| 284 | /// vector B, and thus bitcastable to B as well. |
| 285 | /// 2.2. All pointers of the same address space, no matter what they point to, |
| 286 | /// bitcastable. So if C is pointer, it could be bitcasted to A and to B. |
| 287 | /// So any constant equal or bitcastable to A is equal or bitcastable to B. |
| 288 | /// QED. |
| 289 | /// |
| 290 | /// In another words, for pointers and vectors, we ignore top-level type and |
| 291 | /// look at their particular properties (bit-width for vectors, and |
| 292 | /// address space for pointers). |
| 293 | /// If these properties are equal - compare their contents. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 294 | int cmpConstants(const Constant *L, const Constant *R) const; |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 295 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 296 | /// Compares two global values by number. Uses the GlobalNumbersState to |
| 297 | /// identify the same gobals across function calls. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 298 | int cmpGlobalValues(GlobalValue *L, GlobalValue *R) const; |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 299 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 300 | /// Assign or look up previously assigned numbers for the two values, and |
| 301 | /// return whether the numbers are equal. Numbers are assigned in the order |
| 302 | /// visited. |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 303 | /// Comparison order: |
| 304 | /// Stage 0: Value that is function itself is always greater then others. |
| 305 | /// If left and right values are references to their functions, then |
| 306 | /// they are equal. |
| 307 | /// Stage 1: Constants are greater than non-constants. |
| 308 | /// If both left and right are constants, then the result of |
| 309 | /// cmpConstants is used as cmpValues result. |
| 310 | /// Stage 2: InlineAsm instances are greater than others. If both left and |
| 311 | /// right are InlineAsm instances, InlineAsm* pointers casted to |
| 312 | /// integers and compared as numbers. |
| 313 | /// Stage 3: For all other cases we compare order we meet these values in |
| 314 | /// their functions. If right value was met first during scanning, |
| 315 | /// then left value is greater. |
| 316 | /// In another words, we compare serial numbers, for more details |
| 317 | /// see comments for sn_mapL and sn_mapR. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 318 | int cmpValues(const Value *L, const Value *R) const; |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 319 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 320 | /// Compare two Instructions for equivalence, similar to |
JF Bastien | 1bb32ac | 2016-04-12 21:13:01 +0000 | [diff] [blame] | 321 | /// Instruction::isSameOperationAs. |
| 322 | /// |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 323 | /// Stages are listed in "most significant stage first" order: |
| 324 | /// On each stage below, we do comparison between some left and right |
| 325 | /// operation parts. If parts are non-equal, we assign parts comparison |
| 326 | /// result to the operation comparison result and exit from method. |
| 327 | /// Otherwise we proceed to the next stage. |
| 328 | /// Stages: |
| 329 | /// 1. Operations opcodes. Compared as numbers. |
| 330 | /// 2. Number of operands. |
| 331 | /// 3. Operation types. Compared with cmpType method. |
| 332 | /// 4. Compare operation subclass optional data as stream of bytes: |
| 333 | /// just convert it to integers and call cmpNumbers. |
| 334 | /// 5. Compare in operation operand types with cmpType in |
| 335 | /// most significant operand first order. |
| 336 | /// 6. Last stage. Check operations for some specific attributes. |
| 337 | /// For example, for Load it would be: |
| 338 | /// 6.1.Load: volatile (as boolean flag) |
| 339 | /// 6.2.Load: alignment (as integer numbers) |
JF Bastien | 1bb32ac | 2016-04-12 21:13:01 +0000 | [diff] [blame] | 340 | /// 6.3.Load: ordering (as underlying enum class value) |
| 341 | /// 6.4.Load: synch-scope (as integer numbers) |
| 342 | /// 6.5.Load: range metadata (as integer ranges) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 343 | /// On this stage its better to see the code, since its not more than 10-15 |
| 344 | /// strings for particular instruction, and could change sometimes. |
Stepan Dyatkovskiy | 87c04618 | 2014-07-31 07:16:59 +0000 | [diff] [blame] | 345 | int cmpOperations(const Instruction *L, const Instruction *R) const; |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 346 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 347 | /// Compare two GEPs for equivalent pointer arithmetic. |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 348 | /// Parts to be compared for each comparison stage, |
| 349 | /// most significant stage first: |
| 350 | /// 1. Address space. As numbers. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 351 | /// 2. Constant offset, (using GEPOperator::accumulateConstantOffset method). |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 352 | /// 3. Pointer operand type (using cmpType method). |
| 353 | /// 4. Number of operands. |
| 354 | /// 5. Compare operands, using cmpValues method. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 355 | int cmpGEPs(const GEPOperator *GEPL, const GEPOperator *GEPR) const; |
| 356 | int cmpGEPs(const GetElementPtrInst *GEPL, |
| 357 | const GetElementPtrInst *GEPR) const { |
Stepan Dyatkovskiy | 016dadd | 2014-08-25 08:12:45 +0000 | [diff] [blame] | 358 | return cmpGEPs(cast<GEPOperator>(GEPL), cast<GEPOperator>(GEPR)); |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 361 | /// cmpType - compares two types, |
| 362 | /// defines total ordering among the types set. |
| 363 | /// |
| 364 | /// Return values: |
| 365 | /// 0 if types are equal, |
| 366 | /// -1 if Left is less than Right, |
| 367 | /// +1 if Left is greater than Right. |
| 368 | /// |
| 369 | /// Description: |
| 370 | /// Comparison is broken onto stages. Like in lexicographical comparison |
| 371 | /// stage coming first has higher priority. |
| 372 | /// On each explanation stage keep in mind total ordering properties. |
| 373 | /// |
Stepan Dyatkovskiy | 90c4436 | 2014-03-14 08:17:19 +0000 | [diff] [blame] | 374 | /// 0. Before comparison we coerce pointer types of 0 address space to |
| 375 | /// integer. |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 376 | /// We also don't bother with same type at left and right, so |
| 377 | /// just return 0 in this case. |
| 378 | /// |
| 379 | /// 1. If types are of different kind (different type IDs). |
| 380 | /// Return result of type IDs comparison, treating them as numbers. |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 381 | /// 2. If types are integers, check that they have the same width. If they |
| 382 | /// are vectors, check that they have the same count and subtype. |
| 383 | /// 3. Types have the same ID, so check whether they are one of: |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 384 | /// * Void |
| 385 | /// * Float |
| 386 | /// * Double |
| 387 | /// * X86_FP80 |
| 388 | /// * FP128 |
| 389 | /// * PPC_FP128 |
| 390 | /// * Label |
| 391 | /// * Metadata |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 392 | /// We can treat these types as equal whenever their IDs are same. |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 393 | /// 4. If Left and Right are pointers, return result of address space |
| 394 | /// comparison (numbers comparison). We can treat pointer types of same |
| 395 | /// address space as equal. |
| 396 | /// 5. If types are complex. |
| 397 | /// Then both Left and Right are to be expanded and their element types will |
| 398 | /// be checked with the same way. If we get Res != 0 on some stage, return it. |
| 399 | /// Otherwise return 0. |
| 400 | /// 6. For all other cases put llvm_unreachable. |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 401 | int cmpTypes(Type *TyL, Type *TyR) const; |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 402 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 403 | int cmpNumbers(uint64_t L, uint64_t R) const; |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 404 | int cmpOrderings(AtomicOrdering L, AtomicOrdering R) const; |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 405 | int cmpAPInts(const APInt &L, const APInt &R) const; |
Stepan Dyatkovskiy | c90308b | 2014-08-25 08:22:46 +0000 | [diff] [blame] | 406 | int cmpAPFloats(const APFloat &L, const APFloat &R) const; |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 407 | int cmpInlineAsm(const InlineAsm *L, const InlineAsm *R) const; |
| 408 | int cmpMem(StringRef L, StringRef R) const; |
Stepan Dyatkovskiy | 5c2cc25 | 2014-05-16 08:55:34 +0000 | [diff] [blame] | 409 | int cmpAttrs(const AttributeSet L, const AttributeSet R) const; |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 410 | int cmpRangeMetadata(const MDNode *L, const MDNode *R) const; |
Sanjoy Das | 2a74eb0 | 2015-12-14 19:11:40 +0000 | [diff] [blame] | 411 | int cmpOperandBundlesSchema(const Instruction *L, const Instruction *R) const; |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 412 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 413 | // The two functions undergoing comparison. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 414 | const Function *FnL, *FnR; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 415 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 416 | /// Assign serial numbers to values from left function, and values from |
| 417 | /// right function. |
| 418 | /// Explanation: |
| 419 | /// Being comparing functions we need to compare values we meet at left and |
| 420 | /// right sides. |
| 421 | /// Its easy to sort things out for external values. It just should be |
| 422 | /// the same value at left and right. |
| 423 | /// But for local values (those were introduced inside function body) |
| 424 | /// we have to ensure they were introduced at exactly the same place, |
| 425 | /// and plays the same role. |
| 426 | /// Let's assign serial number to each value when we meet it first time. |
| 427 | /// Values that were met at same place will be with same serial numbers. |
| 428 | /// In this case it would be good to explain few points about values assigned |
| 429 | /// to BBs and other ways of implementation (see below). |
| 430 | /// |
| 431 | /// 1. Safety of BB reordering. |
| 432 | /// It's safe to change the order of BasicBlocks in function. |
| 433 | /// Relationship with other functions and serial numbering will not be |
| 434 | /// changed in this case. |
| 435 | /// As follows from FunctionComparator::compare(), we do CFG walk: we start |
| 436 | /// from the entry, and then take each terminator. So it doesn't matter how in |
| 437 | /// fact BBs are ordered in function. And since cmpValues are called during |
| 438 | /// this walk, the numbering depends only on how BBs located inside the CFG. |
| 439 | /// So the answer is - yes. We will get the same numbering. |
| 440 | /// |
| 441 | /// 2. Impossibility to use dominance properties of values. |
| 442 | /// If we compare two instruction operands: first is usage of local |
| 443 | /// variable AL from function FL, and second is usage of local variable AR |
| 444 | /// from FR, we could compare their origins and check whether they are |
| 445 | /// defined at the same place. |
| 446 | /// But, we are still not able to compare operands of PHI nodes, since those |
| 447 | /// could be operands from further BBs we didn't scan yet. |
| 448 | /// So it's impossible to use dominance properties in general. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 449 | mutable DenseMap<const Value*, int> sn_mapL, sn_mapR; |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 450 | |
| 451 | // The global state we will use |
| 452 | GlobalNumberState* GlobalNumbers; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 453 | }; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 454 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 455 | class FunctionNode { |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 456 | mutable AssertingVH<Function> F; |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 457 | FunctionComparator::FunctionHash Hash; |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 458 | public: |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 459 | // Note the hash is recalculated potentially multiple times, but it is cheap. |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 460 | FunctionNode(Function *F) |
| 461 | : F(F), Hash(FunctionComparator::functionHash(*F)) {} |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 462 | Function *getFunc() const { return F; } |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 463 | FunctionComparator::FunctionHash getHash() const { return Hash; } |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 464 | |
| 465 | /// Replace the reference to the function F by the function G, assuming their |
| 466 | /// implementations are equal. |
| 467 | void replaceBy(Function *G) const { |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 468 | F = G; |
| 469 | } |
| 470 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 471 | void release() { F = nullptr; } |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 472 | }; |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 473 | } // end anonymous namespace |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 474 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 475 | int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const { |
| 476 | if (L < R) return -1; |
| 477 | if (L > R) return 1; |
| 478 | return 0; |
| 479 | } |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 480 | |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 481 | int FunctionComparator::cmpOrderings(AtomicOrdering L, AtomicOrdering R) const { |
| 482 | if ((int)L < (int)R) return -1; |
| 483 | if ((int)L > (int)R) return 1; |
| 484 | return 0; |
| 485 | } |
| 486 | |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 487 | int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const { |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 488 | if (int Res = cmpNumbers(L.getBitWidth(), R.getBitWidth())) |
| 489 | return Res; |
| 490 | if (L.ugt(R)) return 1; |
| 491 | if (R.ugt(L)) return -1; |
| 492 | return 0; |
| 493 | } |
| 494 | |
Stepan Dyatkovskiy | c90308b | 2014-08-25 08:22:46 +0000 | [diff] [blame] | 495 | int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const { |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 496 | // Floats are ordered first by semantics (i.e. float, double, half, etc.), |
| 497 | // then by value interpreted as a bitstring (aka APInt). |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 498 | const fltSemantics &SL = L.getSemantics(), &SR = R.getSemantics(); |
| 499 | if (int Res = cmpNumbers(APFloat::semanticsPrecision(SL), |
| 500 | APFloat::semanticsPrecision(SR))) |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 501 | return Res; |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 502 | if (int Res = cmpNumbers(APFloat::semanticsMaxExponent(SL), |
| 503 | APFloat::semanticsMaxExponent(SR))) |
| 504 | return Res; |
| 505 | if (int Res = cmpNumbers(APFloat::semanticsMinExponent(SL), |
| 506 | APFloat::semanticsMinExponent(SR))) |
| 507 | return Res; |
| 508 | if (int Res = cmpNumbers(APFloat::semanticsSizeInBits(SL), |
| 509 | APFloat::semanticsSizeInBits(SR))) |
| 510 | return Res; |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 511 | return cmpAPInts(L.bitcastToAPInt(), R.bitcastToAPInt()); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 512 | } |
| 513 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 514 | int FunctionComparator::cmpMem(StringRef L, StringRef R) const { |
Stepan Dyatkovskiy | 5c2cc25 | 2014-05-16 08:55:34 +0000 | [diff] [blame] | 515 | // Prevent heavy comparison, compare sizes first. |
| 516 | if (int Res = cmpNumbers(L.size(), R.size())) |
| 517 | return Res; |
| 518 | |
| 519 | // Compare strings lexicographically only when it is necessary: only when |
| 520 | // strings are equal in size. |
| 521 | return L.compare(R); |
| 522 | } |
| 523 | |
| 524 | int FunctionComparator::cmpAttrs(const AttributeSet L, |
| 525 | const AttributeSet R) const { |
| 526 | if (int Res = cmpNumbers(L.getNumSlots(), R.getNumSlots())) |
| 527 | return Res; |
| 528 | |
| 529 | for (unsigned i = 0, e = L.getNumSlots(); i != e; ++i) { |
| 530 | AttributeSet::iterator LI = L.begin(i), LE = L.end(i), RI = R.begin(i), |
| 531 | RE = R.end(i); |
| 532 | for (; LI != LE && RI != RE; ++LI, ++RI) { |
| 533 | Attribute LA = *LI; |
| 534 | Attribute RA = *RI; |
| 535 | if (LA < RA) |
| 536 | return -1; |
| 537 | if (RA < LA) |
| 538 | return 1; |
| 539 | } |
| 540 | if (LI != LE) |
| 541 | return 1; |
| 542 | if (RI != RE) |
| 543 | return -1; |
| 544 | } |
| 545 | return 0; |
| 546 | } |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 547 | |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 548 | int FunctionComparator::cmpRangeMetadata(const MDNode *L, |
| 549 | const MDNode *R) const { |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 550 | if (L == R) |
| 551 | return 0; |
| 552 | if (!L) |
| 553 | return -1; |
| 554 | if (!R) |
| 555 | return 1; |
| 556 | // Range metadata is a sequence of numbers. Make sure they are the same |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 557 | // sequence. |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 558 | // TODO: Note that as this is metadata, it is possible to drop and/or merge |
| 559 | // this data when considering functions to merge. Thus this comparison would |
| 560 | // return 0 (i.e. equivalent), but merging would become more complicated |
| 561 | // because the ranges would need to be unioned. It is not likely that |
| 562 | // functions differ ONLY in this metadata if they are actually the same |
| 563 | // function semantically. |
| 564 | if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands())) |
| 565 | return Res; |
| 566 | for (size_t I = 0; I < L->getNumOperands(); ++I) { |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 567 | ConstantInt *LLow = mdconst::extract<ConstantInt>(L->getOperand(I)); |
| 568 | ConstantInt *RLow = mdconst::extract<ConstantInt>(R->getOperand(I)); |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 569 | if (int Res = cmpAPInts(LLow->getValue(), RLow->getValue())) |
| 570 | return Res; |
| 571 | } |
| 572 | return 0; |
| 573 | } |
Stepan Dyatkovskiy | 5c2cc25 | 2014-05-16 08:55:34 +0000 | [diff] [blame] | 574 | |
Sanjoy Das | 2a74eb0 | 2015-12-14 19:11:40 +0000 | [diff] [blame] | 575 | int FunctionComparator::cmpOperandBundlesSchema(const Instruction *L, |
| 576 | const Instruction *R) const { |
| 577 | ImmutableCallSite LCS(L); |
| 578 | ImmutableCallSite RCS(R); |
| 579 | |
| 580 | assert(LCS && RCS && "Must be calls or invokes!"); |
| 581 | assert(LCS.isCall() == RCS.isCall() && "Can't compare otherwise!"); |
| 582 | |
| 583 | if (int Res = |
| 584 | cmpNumbers(LCS.getNumOperandBundles(), RCS.getNumOperandBundles())) |
| 585 | return Res; |
| 586 | |
| 587 | for (unsigned i = 0, e = LCS.getNumOperandBundles(); i != e; ++i) { |
| 588 | auto OBL = LCS.getOperandBundleAt(i); |
| 589 | auto OBR = RCS.getOperandBundleAt(i); |
| 590 | |
| 591 | if (int Res = OBL.getTagName().compare(OBR.getTagName())) |
| 592 | return Res; |
| 593 | |
| 594 | if (int Res = cmpNumbers(OBL.Inputs.size(), OBR.Inputs.size())) |
| 595 | return Res; |
| 596 | } |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 601 | /// Constants comparison: |
| 602 | /// 1. Check whether type of L constant could be losslessly bitcasted to R |
| 603 | /// type. |
| 604 | /// 2. Compare constant contents. |
| 605 | /// For more details see declaration comments. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 606 | int FunctionComparator::cmpConstants(const Constant *L, |
| 607 | const Constant *R) const { |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 608 | |
| 609 | Type *TyL = L->getType(); |
| 610 | Type *TyR = R->getType(); |
| 611 | |
| 612 | // Check whether types are bitcastable. This part is just re-factored |
| 613 | // Type::canLosslesslyBitCastTo method, but instead of returning true/false, |
| 614 | // we also pack into result which type is "less" for us. |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 615 | int TypesRes = cmpTypes(TyL, TyR); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 616 | if (TypesRes != 0) { |
| 617 | // Types are different, but check whether we can bitcast them. |
| 618 | if (!TyL->isFirstClassType()) { |
| 619 | if (TyR->isFirstClassType()) |
| 620 | return -1; |
| 621 | // Neither TyL nor TyR are values of first class type. Return the result |
| 622 | // of comparing the types |
| 623 | return TypesRes; |
| 624 | } |
| 625 | if (!TyR->isFirstClassType()) { |
| 626 | if (TyL->isFirstClassType()) |
| 627 | return 1; |
| 628 | return TypesRes; |
| 629 | } |
| 630 | |
| 631 | // Vector -> Vector conversions are always lossless if the two vector types |
| 632 | // have the same size, otherwise not. |
| 633 | unsigned TyLWidth = 0; |
| 634 | unsigned TyRWidth = 0; |
| 635 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 636 | if (auto *VecTyL = dyn_cast<VectorType>(TyL)) |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 637 | TyLWidth = VecTyL->getBitWidth(); |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 638 | if (auto *VecTyR = dyn_cast<VectorType>(TyR)) |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 639 | TyRWidth = VecTyR->getBitWidth(); |
| 640 | |
| 641 | if (TyLWidth != TyRWidth) |
| 642 | return cmpNumbers(TyLWidth, TyRWidth); |
| 643 | |
| 644 | // Zero bit-width means neither TyL nor TyR are vectors. |
| 645 | if (!TyLWidth) { |
| 646 | PointerType *PTyL = dyn_cast<PointerType>(TyL); |
| 647 | PointerType *PTyR = dyn_cast<PointerType>(TyR); |
| 648 | if (PTyL && PTyR) { |
| 649 | unsigned AddrSpaceL = PTyL->getAddressSpace(); |
| 650 | unsigned AddrSpaceR = PTyR->getAddressSpace(); |
| 651 | if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR)) |
| 652 | return Res; |
| 653 | } |
| 654 | if (PTyL) |
| 655 | return 1; |
| 656 | if (PTyR) |
| 657 | return -1; |
| 658 | |
| 659 | // TyL and TyR aren't vectors, nor pointers. We don't know how to |
| 660 | // bitcast them. |
| 661 | return TypesRes; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // OK, types are bitcastable, now check constant contents. |
| 666 | |
| 667 | if (L->isNullValue() && R->isNullValue()) |
| 668 | return TypesRes; |
| 669 | if (L->isNullValue() && !R->isNullValue()) |
| 670 | return 1; |
| 671 | if (!L->isNullValue() && R->isNullValue()) |
| 672 | return -1; |
| 673 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 674 | auto GlobalValueL = const_cast<GlobalValue*>(dyn_cast<GlobalValue>(L)); |
| 675 | auto GlobalValueR = const_cast<GlobalValue*>(dyn_cast<GlobalValue>(R)); |
| 676 | if (GlobalValueL && GlobalValueR) { |
| 677 | return cmpGlobalValues(GlobalValueL, GlobalValueR); |
| 678 | } |
| 679 | |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 680 | if (int Res = cmpNumbers(L->getValueID(), R->getValueID())) |
| 681 | return Res; |
| 682 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 683 | if (const auto *SeqL = dyn_cast<ConstantDataSequential>(L)) { |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 684 | const auto *SeqR = cast<ConstantDataSequential>(R); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 685 | // This handles ConstantDataArray and ConstantDataVector. Note that we |
| 686 | // compare the two raw data arrays, which might differ depending on the host |
| 687 | // endianness. This isn't a problem though, because the endiness of a module |
| 688 | // will affect the order of the constants, but this order is the same |
| 689 | // for a given input module and host platform. |
| 690 | return cmpMem(SeqL->getRawDataValues(), SeqR->getRawDataValues()); |
| 691 | } |
| 692 | |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 693 | switch (L->getValueID()) { |
David Majnemer | f0f224d | 2015-11-11 21:57:16 +0000 | [diff] [blame] | 694 | case Value::UndefValueVal: |
| 695 | case Value::ConstantTokenNoneVal: |
| 696 | return TypesRes; |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 697 | case Value::ConstantIntVal: { |
| 698 | const APInt &LInt = cast<ConstantInt>(L)->getValue(); |
| 699 | const APInt &RInt = cast<ConstantInt>(R)->getValue(); |
Stepan Dyatkovskiy | 7f895c1 | 2014-08-25 08:19:50 +0000 | [diff] [blame] | 700 | return cmpAPInts(LInt, RInt); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 701 | } |
| 702 | case Value::ConstantFPVal: { |
| 703 | const APFloat &LAPF = cast<ConstantFP>(L)->getValueAPF(); |
| 704 | const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF(); |
Stepan Dyatkovskiy | c90308b | 2014-08-25 08:22:46 +0000 | [diff] [blame] | 705 | return cmpAPFloats(LAPF, RAPF); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 706 | } |
| 707 | case Value::ConstantArrayVal: { |
| 708 | const ConstantArray *LA = cast<ConstantArray>(L); |
| 709 | const ConstantArray *RA = cast<ConstantArray>(R); |
| 710 | uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements(); |
| 711 | uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements(); |
| 712 | if (int Res = cmpNumbers(NumElementsL, NumElementsR)) |
| 713 | return Res; |
| 714 | for (uint64_t i = 0; i < NumElementsL; ++i) { |
| 715 | if (int Res = cmpConstants(cast<Constant>(LA->getOperand(i)), |
| 716 | cast<Constant>(RA->getOperand(i)))) |
| 717 | return Res; |
| 718 | } |
| 719 | return 0; |
| 720 | } |
| 721 | case Value::ConstantStructVal: { |
| 722 | const ConstantStruct *LS = cast<ConstantStruct>(L); |
| 723 | const ConstantStruct *RS = cast<ConstantStruct>(R); |
| 724 | unsigned NumElementsL = cast<StructType>(TyL)->getNumElements(); |
| 725 | unsigned NumElementsR = cast<StructType>(TyR)->getNumElements(); |
| 726 | if (int Res = cmpNumbers(NumElementsL, NumElementsR)) |
| 727 | return Res; |
| 728 | for (unsigned i = 0; i != NumElementsL; ++i) { |
| 729 | if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)), |
| 730 | cast<Constant>(RS->getOperand(i)))) |
| 731 | return Res; |
| 732 | } |
| 733 | return 0; |
| 734 | } |
| 735 | case Value::ConstantVectorVal: { |
| 736 | const ConstantVector *LV = cast<ConstantVector>(L); |
| 737 | const ConstantVector *RV = cast<ConstantVector>(R); |
| 738 | unsigned NumElementsL = cast<VectorType>(TyL)->getNumElements(); |
| 739 | unsigned NumElementsR = cast<VectorType>(TyR)->getNumElements(); |
| 740 | if (int Res = cmpNumbers(NumElementsL, NumElementsR)) |
| 741 | return Res; |
| 742 | for (uint64_t i = 0; i < NumElementsL; ++i) { |
| 743 | if (int Res = cmpConstants(cast<Constant>(LV->getOperand(i)), |
| 744 | cast<Constant>(RV->getOperand(i)))) |
| 745 | return Res; |
| 746 | } |
| 747 | return 0; |
| 748 | } |
| 749 | case Value::ConstantExprVal: { |
| 750 | const ConstantExpr *LE = cast<ConstantExpr>(L); |
| 751 | const ConstantExpr *RE = cast<ConstantExpr>(R); |
| 752 | unsigned NumOperandsL = LE->getNumOperands(); |
| 753 | unsigned NumOperandsR = RE->getNumOperands(); |
| 754 | if (int Res = cmpNumbers(NumOperandsL, NumOperandsR)) |
| 755 | return Res; |
| 756 | for (unsigned i = 0; i < NumOperandsL; ++i) { |
| 757 | if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)), |
| 758 | cast<Constant>(RE->getOperand(i)))) |
| 759 | return Res; |
| 760 | } |
| 761 | return 0; |
| 762 | } |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 763 | case Value::BlockAddressVal: { |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 764 | const BlockAddress *LBA = cast<BlockAddress>(L); |
| 765 | const BlockAddress *RBA = cast<BlockAddress>(R); |
| 766 | if (int Res = cmpValues(LBA->getFunction(), RBA->getFunction())) |
| 767 | return Res; |
| 768 | if (LBA->getFunction() == RBA->getFunction()) { |
| 769 | // They are BBs in the same function. Order by which comes first in the |
| 770 | // BB order of the function. This order is deterministic. |
| 771 | Function* F = LBA->getFunction(); |
| 772 | BasicBlock *LBB = LBA->getBasicBlock(); |
| 773 | BasicBlock *RBB = RBA->getBasicBlock(); |
| 774 | if (LBB == RBB) |
| 775 | return 0; |
| 776 | for(BasicBlock &BB : F->getBasicBlockList()) { |
| 777 | if (&BB == LBB) { |
| 778 | assert(&BB != RBB); |
| 779 | return -1; |
| 780 | } |
| 781 | if (&BB == RBB) |
| 782 | return 1; |
| 783 | } |
| 784 | llvm_unreachable("Basic Block Address does not point to a basic block in " |
| 785 | "its function."); |
| 786 | return -1; |
| 787 | } else { |
| 788 | // cmpValues said the functions are the same. So because they aren't |
| 789 | // literally the same pointer, they must respectively be the left and |
| 790 | // right functions. |
| 791 | assert(LBA->getFunction() == FnL && RBA->getFunction() == FnR); |
| 792 | // cmpValues will tell us if these are equivalent BasicBlocks, in the |
| 793 | // context of their respective functions. |
| 794 | return cmpValues(LBA->getBasicBlock(), RBA->getBasicBlock()); |
| 795 | } |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 796 | } |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 797 | default: // Unknown constant, abort. |
| 798 | DEBUG(dbgs() << "Looking at valueID " << L->getValueID() << "\n"); |
| 799 | llvm_unreachable("Constant ValueID not recognized."); |
| 800 | return -1; |
| 801 | } |
| 802 | } |
| 803 | |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 804 | int FunctionComparator::cmpGlobalValues(GlobalValue *L, GlobalValue *R) const { |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 805 | return cmpNumbers(GlobalNumbers->getNumber(L), GlobalNumbers->getNumber(R)); |
Stepan Dyatkovskiy | d103130 | 2014-05-07 09:05:10 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 808 | /// cmpType - compares two types, |
| 809 | /// defines total ordering among the types set. |
| 810 | /// See method declaration comments for more details. |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 811 | int FunctionComparator::cmpTypes(Type *TyL, Type *TyR) const { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 812 | PointerType *PTyL = dyn_cast<PointerType>(TyL); |
| 813 | PointerType *PTyR = dyn_cast<PointerType>(TyR); |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 814 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 815 | const DataLayout &DL = FnL->getParent()->getDataLayout(); |
| 816 | if (PTyL && PTyL->getAddressSpace() == 0) |
| 817 | TyL = DL.getIntPtrType(TyL); |
| 818 | if (PTyR && PTyR->getAddressSpace() == 0) |
| 819 | TyR = DL.getIntPtrType(TyR); |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 820 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 821 | if (TyL == TyR) |
| 822 | return 0; |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 823 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 824 | if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID())) |
| 825 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 826 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 827 | switch (TyL->getTypeID()) { |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 828 | default: |
| 829 | llvm_unreachable("Unknown type!"); |
Duncan Sands | 408bb19 | 2010-07-07 07:48:00 +0000 | [diff] [blame] | 830 | // Fall through in Release mode. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 831 | case Type::IntegerTyID: |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 832 | return cmpNumbers(cast<IntegerType>(TyL)->getBitWidth(), |
| 833 | cast<IntegerType>(TyR)->getBitWidth()); |
| 834 | case Type::VectorTyID: { |
| 835 | VectorType *VTyL = cast<VectorType>(TyL), *VTyR = cast<VectorType>(TyR); |
| 836 | if (int Res = cmpNumbers(VTyL->getNumElements(), VTyR->getNumElements())) |
| 837 | return Res; |
| 838 | return cmpTypes(VTyL->getElementType(), VTyR->getElementType()); |
| 839 | } |
| 840 | // TyL == TyR would have returned true earlier, because types are uniqued. |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 841 | case Type::VoidTyID: |
| 842 | case Type::FloatTyID: |
| 843 | case Type::DoubleTyID: |
| 844 | case Type::X86_FP80TyID: |
| 845 | case Type::FP128TyID: |
| 846 | case Type::PPC_FP128TyID: |
| 847 | case Type::LabelTyID: |
| 848 | case Type::MetadataTyID: |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 849 | case Type::TokenTyID: |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 850 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 851 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 852 | case Type::PointerTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 853 | assert(PTyL && PTyR && "Both types must be pointers here."); |
| 854 | return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | case Type::StructTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 858 | StructType *STyL = cast<StructType>(TyL); |
| 859 | StructType *STyR = cast<StructType>(TyR); |
| 860 | if (STyL->getNumElements() != STyR->getNumElements()) |
| 861 | return cmpNumbers(STyL->getNumElements(), STyR->getNumElements()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 862 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 863 | if (STyL->isPacked() != STyR->isPacked()) |
| 864 | return cmpNumbers(STyL->isPacked(), STyR->isPacked()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 865 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 866 | for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) { |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 867 | if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i))) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 868 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 869 | } |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 870 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | case Type::FunctionTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 874 | FunctionType *FTyL = cast<FunctionType>(TyL); |
| 875 | FunctionType *FTyR = cast<FunctionType>(TyR); |
| 876 | if (FTyL->getNumParams() != FTyR->getNumParams()) |
| 877 | return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 878 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 879 | if (FTyL->isVarArg() != FTyR->isVarArg()) |
| 880 | return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg()); |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 881 | |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 882 | if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType())) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 883 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 884 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 885 | for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) { |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 886 | if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i))) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 887 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 888 | } |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 889 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Nick Lewycky | 375efe3 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 892 | case Type::ArrayTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 893 | ArrayType *ATyL = cast<ArrayType>(TyL); |
| 894 | ArrayType *ATyR = cast<ArrayType>(TyR); |
| 895 | if (ATyL->getNumElements() != ATyR->getNumElements()) |
| 896 | return cmpNumbers(ATyL->getNumElements(), ATyR->getNumElements()); |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 897 | return cmpTypes(ATyL->getElementType(), ATyR->getElementType()); |
Nick Lewycky | 375efe3 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 898 | } |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 902 | // Determine whether the two operations are the same except that pointer-to-A |
| 903 | // and pointer-to-B are equivalent. This should be kept in sync with |
| 904 | // Instruction::isSameOperationAs. |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 905 | // Read method declaration comments for more details. |
Stepan Dyatkovskiy | 87c04618 | 2014-07-31 07:16:59 +0000 | [diff] [blame] | 906 | int FunctionComparator::cmpOperations(const Instruction *L, |
| 907 | const Instruction *R) const { |
Nick Lewycky | cb1a4c2 | 2011-02-06 05:04:00 +0000 | [diff] [blame] | 908 | // Differences from Instruction::isSameOperationAs: |
JF Bastien | 1bb32ac | 2016-04-12 21:13:01 +0000 | [diff] [blame] | 909 | // * replace type comparison with calls to cmpTypes. |
| 910 | // * we test for I->getRawSubclassOptionalData (nuw/nsw/tail) at the top. |
| 911 | // * 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] | 912 | if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode())) |
| 913 | return Res; |
| 914 | |
| 915 | if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands())) |
| 916 | return Res; |
| 917 | |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 918 | if (int Res = cmpTypes(L->getType(), R->getType())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 919 | return Res; |
| 920 | |
| 921 | if (int Res = cmpNumbers(L->getRawSubclassOptionalData(), |
| 922 | R->getRawSubclassOptionalData())) |
| 923 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 924 | |
| 925 | // We have two instructions of identical opcode and #operands. Check to see |
| 926 | // if all operands are the same type |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 927 | for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) { |
| 928 | if (int Res = |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 929 | cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 930 | return Res; |
| 931 | } |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 932 | |
| 933 | // Check special state that is a part of some instructions. |
JF Bastien | 4f43cfd | 2016-04-12 00:03:26 +0000 | [diff] [blame] | 934 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) { |
| 935 | if (int Res = cmpTypes(AI->getAllocatedType(), |
| 936 | cast<AllocaInst>(R)->getAllocatedType())) |
| 937 | return Res; |
| 938 | return cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment()); |
| 939 | } |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 940 | if (const LoadInst *LI = dyn_cast<LoadInst>(L)) { |
| 941 | if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile())) |
| 942 | return Res; |
| 943 | if (int Res = |
| 944 | cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment())) |
| 945 | return Res; |
| 946 | if (int Res = |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 947 | cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 948 | return Res; |
Stepan Dyatkovskiy | 6baeb88 | 2014-06-20 19:11:56 +0000 | [diff] [blame] | 949 | if (int Res = |
| 950 | cmpNumbers(LI->getSynchScope(), cast<LoadInst>(R)->getSynchScope())) |
| 951 | return Res; |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 952 | return cmpRangeMetadata(LI->getMetadata(LLVMContext::MD_range), |
| 953 | cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range)); |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 954 | } |
| 955 | if (const StoreInst *SI = dyn_cast<StoreInst>(L)) { |
| 956 | if (int Res = |
| 957 | cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile())) |
| 958 | return Res; |
| 959 | if (int Res = |
| 960 | cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment())) |
| 961 | return Res; |
| 962 | if (int Res = |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 963 | cmpOrderings(SI->getOrdering(), cast<StoreInst>(R)->getOrdering())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 964 | return Res; |
| 965 | return cmpNumbers(SI->getSynchScope(), cast<StoreInst>(R)->getSynchScope()); |
| 966 | } |
| 967 | if (const CmpInst *CI = dyn_cast<CmpInst>(L)) |
| 968 | return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate()); |
| 969 | if (const CallInst *CI = dyn_cast<CallInst>(L)) { |
| 970 | if (int Res = cmpNumbers(CI->getCallingConv(), |
| 971 | cast<CallInst>(R)->getCallingConv())) |
| 972 | return Res; |
Stepan Dyatkovskiy | dee612d | 2014-07-15 10:46:51 +0000 | [diff] [blame] | 973 | if (int Res = |
| 974 | cmpAttrs(CI->getAttributes(), cast<CallInst>(R)->getAttributes())) |
| 975 | return Res; |
Sanjoy Das | 2a74eb0 | 2015-12-14 19:11:40 +0000 | [diff] [blame] | 976 | if (int Res = cmpOperandBundlesSchema(CI, R)) |
| 977 | return Res; |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 978 | return cmpRangeMetadata( |
| 979 | CI->getMetadata(LLVMContext::MD_range), |
| 980 | cast<CallInst>(R)->getMetadata(LLVMContext::MD_range)); |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 981 | } |
Sanjoy Das | adfec01 | 2015-12-14 19:11:45 +0000 | [diff] [blame] | 982 | if (const InvokeInst *II = dyn_cast<InvokeInst>(L)) { |
| 983 | if (int Res = cmpNumbers(II->getCallingConv(), |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 984 | cast<InvokeInst>(R)->getCallingConv())) |
| 985 | return Res; |
Stepan Dyatkovskiy | dee612d | 2014-07-15 10:46:51 +0000 | [diff] [blame] | 986 | if (int Res = |
Sanjoy Das | adfec01 | 2015-12-14 19:11:45 +0000 | [diff] [blame] | 987 | cmpAttrs(II->getAttributes(), cast<InvokeInst>(R)->getAttributes())) |
Stepan Dyatkovskiy | dee612d | 2014-07-15 10:46:51 +0000 | [diff] [blame] | 988 | return Res; |
Sanjoy Das | adfec01 | 2015-12-14 19:11:45 +0000 | [diff] [blame] | 989 | if (int Res = cmpOperandBundlesSchema(II, R)) |
Sanjoy Das | 2a74eb0 | 2015-12-14 19:11:40 +0000 | [diff] [blame] | 990 | return Res; |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 991 | return cmpRangeMetadata( |
Sanjoy Das | adfec01 | 2015-12-14 19:11:45 +0000 | [diff] [blame] | 992 | II->getMetadata(LLVMContext::MD_range), |
JF Bastien | f5aa1ca | 2015-08-28 16:49:09 +0000 | [diff] [blame] | 993 | cast<InvokeInst>(R)->getMetadata(LLVMContext::MD_range)); |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 994 | } |
| 995 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) { |
| 996 | ArrayRef<unsigned> LIndices = IVI->getIndices(); |
| 997 | ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices(); |
| 998 | if (int Res = cmpNumbers(LIndices.size(), RIndices.size())) |
| 999 | return Res; |
| 1000 | for (size_t i = 0, e = LIndices.size(); i != e; ++i) { |
| 1001 | if (int Res = cmpNumbers(LIndices[i], RIndices[i])) |
| 1002 | return Res; |
| 1003 | } |
JF Bastien | f90029b | 2016-04-12 21:23:05 +0000 | [diff] [blame] | 1004 | return 0; |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 1005 | } |
| 1006 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) { |
| 1007 | ArrayRef<unsigned> LIndices = EVI->getIndices(); |
| 1008 | ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices(); |
| 1009 | if (int Res = cmpNumbers(LIndices.size(), RIndices.size())) |
| 1010 | return Res; |
| 1011 | for (size_t i = 0, e = LIndices.size(); i != e; ++i) { |
| 1012 | if (int Res = cmpNumbers(LIndices[i], RIndices[i])) |
| 1013 | return Res; |
| 1014 | } |
| 1015 | } |
| 1016 | if (const FenceInst *FI = dyn_cast<FenceInst>(L)) { |
| 1017 | if (int Res = |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1018 | cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 1019 | return Res; |
| 1020 | return cmpNumbers(FI->getSynchScope(), cast<FenceInst>(R)->getSynchScope()); |
| 1021 | } |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 1022 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) { |
| 1023 | if (int Res = cmpNumbers(CXI->isVolatile(), |
| 1024 | cast<AtomicCmpXchgInst>(R)->isVolatile())) |
| 1025 | return Res; |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1026 | if (int Res = cmpNumbers(CXI->isWeak(), |
| 1027 | cast<AtomicCmpXchgInst>(R)->isWeak())) |
| 1028 | return Res; |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1029 | if (int Res = |
| 1030 | cmpOrderings(CXI->getSuccessOrdering(), |
| 1031 | cast<AtomicCmpXchgInst>(R)->getSuccessOrdering())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 1032 | return Res; |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1033 | if (int Res = |
| 1034 | cmpOrderings(CXI->getFailureOrdering(), |
| 1035 | cast<AtomicCmpXchgInst>(R)->getFailureOrdering())) |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 1036 | return Res; |
| 1037 | return cmpNumbers(CXI->getSynchScope(), |
| 1038 | cast<AtomicCmpXchgInst>(R)->getSynchScope()); |
| 1039 | } |
| 1040 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) { |
| 1041 | if (int Res = cmpNumbers(RMWI->getOperation(), |
| 1042 | cast<AtomicRMWInst>(R)->getOperation())) |
| 1043 | return Res; |
| 1044 | if (int Res = cmpNumbers(RMWI->isVolatile(), |
| 1045 | cast<AtomicRMWInst>(R)->isVolatile())) |
| 1046 | return Res; |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1047 | if (int Res = cmpOrderings(RMWI->getOrdering(), |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 1048 | cast<AtomicRMWInst>(R)->getOrdering())) |
| 1049 | return Res; |
| 1050 | return cmpNumbers(RMWI->getSynchScope(), |
| 1051 | cast<AtomicRMWInst>(R)->getSynchScope()); |
| 1052 | } |
Mark Lacey | 9b5fcf6 | 2016-05-20 18:39:11 +0000 | [diff] [blame] | 1053 | if (const PHINode *PNL = dyn_cast<PHINode>(L)) { |
| 1054 | const PHINode *PNR = cast<PHINode>(R); |
| 1055 | // Ensure that in addition to the incoming values being identical |
| 1056 | // (checked by the caller of this function), the incoming blocks |
| 1057 | // are also identical. |
| 1058 | for (unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) { |
| 1059 | if (int Res = |
| 1060 | cmpValues(PNL->getIncomingBlock(i), PNR->getIncomingBlock(i))) |
| 1061 | return Res; |
| 1062 | } |
| 1063 | } |
Stepan Dyatkovskiy | fa6820a | 2014-05-16 11:02:22 +0000 | [diff] [blame] | 1064 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1067 | // Determine whether two GEP operations perform the same underlying arithmetic. |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 1068 | // Read method declaration comments for more details. |
Stepan Dyatkovskiy | 016dadd | 2014-08-25 08:12:45 +0000 | [diff] [blame] | 1069 | int FunctionComparator::cmpGEPs(const GEPOperator *GEPL, |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 1070 | const GEPOperator *GEPR) const { |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 1071 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 1072 | unsigned int ASL = GEPL->getPointerAddressSpace(); |
| 1073 | unsigned int ASR = GEPR->getPointerAddressSpace(); |
| 1074 | |
| 1075 | if (int Res = cmpNumbers(ASL, ASR)) |
| 1076 | return Res; |
| 1077 | |
| 1078 | // When we have target data, we can reduce the GEP down to the value in bytes |
| 1079 | // added to the address. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1080 | const DataLayout &DL = FnL->getParent()->getDataLayout(); |
| 1081 | unsigned BitWidth = DL.getPointerSizeInBits(ASL); |
| 1082 | APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0); |
| 1083 | if (GEPL->accumulateConstantOffset(DL, OffsetL) && |
| 1084 | GEPR->accumulateConstantOffset(DL, OffsetR)) |
| 1085 | return cmpAPInts(OffsetL, OffsetR); |
JF Bastien | 26aca14 | 2015-09-14 15:37:48 +0000 | [diff] [blame] | 1086 | if (int Res = cmpTypes(GEPL->getSourceElementType(), |
| 1087 | GEPR->getSourceElementType())) |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 1088 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1089 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 1090 | if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands())) |
| 1091 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1092 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 1093 | for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) { |
| 1094 | if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i))) |
| 1095 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Stepan Dyatkovskiy | 948366a | 2014-05-16 11:55:02 +0000 | [diff] [blame] | 1098 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1101 | int FunctionComparator::cmpInlineAsm(const InlineAsm *L, |
| 1102 | const InlineAsm *R) const { |
| 1103 | // InlineAsm's are uniqued. If they are the same pointer, obviously they are |
| 1104 | // the same, otherwise compare the fields. |
| 1105 | if (L == R) |
| 1106 | return 0; |
| 1107 | if (int Res = cmpTypes(L->getFunctionType(), R->getFunctionType())) |
| 1108 | return Res; |
| 1109 | if (int Res = cmpMem(L->getAsmString(), R->getAsmString())) |
| 1110 | return Res; |
| 1111 | if (int Res = cmpMem(L->getConstraintString(), R->getConstraintString())) |
| 1112 | return Res; |
| 1113 | if (int Res = cmpNumbers(L->hasSideEffects(), R->hasSideEffects())) |
| 1114 | return Res; |
| 1115 | if (int Res = cmpNumbers(L->isAlignStack(), R->isAlignStack())) |
| 1116 | return Res; |
| 1117 | if (int Res = cmpNumbers(L->getDialect(), R->getDialect())) |
| 1118 | return Res; |
| 1119 | llvm_unreachable("InlineAsm blocks were not uniqued."); |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1123 | /// Compare two values used by the two functions under pair-wise comparison. If |
| 1124 | /// this is the first time the values are seen, they're added to the mapping so |
| 1125 | /// that we will detect mismatches on next use. |
| 1126 | /// See comments in declaration for more details. |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 1127 | int FunctionComparator::cmpValues(const Value *L, const Value *R) const { |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1128 | // Catch self-reference case. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1129 | if (L == FnL) { |
| 1130 | if (R == FnR) |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1131 | return 0; |
| 1132 | return -1; |
| 1133 | } |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1134 | if (R == FnR) { |
| 1135 | if (L == FnL) |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1136 | return 0; |
| 1137 | return 1; |
Nick Lewycky | 13e04ae | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 1138 | } |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1139 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1140 | const Constant *ConstL = dyn_cast<Constant>(L); |
| 1141 | const Constant *ConstR = dyn_cast<Constant>(R); |
| 1142 | if (ConstL && ConstR) { |
| 1143 | if (L == R) |
| 1144 | return 0; |
| 1145 | return cmpConstants(ConstL, ConstR); |
| 1146 | } |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1147 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1148 | if (ConstL) |
| 1149 | return 1; |
| 1150 | if (ConstR) |
| 1151 | return -1; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1152 | |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1153 | const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L); |
| 1154 | const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R); |
| 1155 | |
| 1156 | if (InlineAsmL && InlineAsmR) |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1157 | return cmpInlineAsm(InlineAsmL, InlineAsmR); |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1158 | if (InlineAsmL) |
| 1159 | return 1; |
| 1160 | if (InlineAsmR) |
| 1161 | return -1; |
| 1162 | |
| 1163 | auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())), |
| 1164 | RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size())); |
| 1165 | |
| 1166 | return cmpNumbers(LeftSN.first->second, RightSN.first->second); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1167 | } |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1168 | // Test whether two basic blocks have equivalent behaviour. |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1169 | int FunctionComparator::cmpBasicBlocks(const BasicBlock *BBL, |
JF Bastien | 8331458 | 2016-04-13 21:12:21 +0000 | [diff] [blame] | 1170 | const BasicBlock *BBR) const { |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1171 | BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end(); |
| 1172 | BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end(); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1173 | |
| 1174 | do { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 1175 | if (int Res = cmpValues(&*InstL, &*InstR)) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1176 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1177 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1178 | const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(InstL); |
| 1179 | const GetElementPtrInst *GEPR = dyn_cast<GetElementPtrInst>(InstR); |
Nick Lewycky | 47b71c5 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 1180 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1181 | if (GEPL && !GEPR) |
| 1182 | return 1; |
| 1183 | if (GEPR && !GEPL) |
| 1184 | return -1; |
Nick Lewycky | 47b71c5 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 1185 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1186 | if (GEPL && GEPR) { |
| 1187 | if (int Res = |
| 1188 | cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand())) |
| 1189 | return Res; |
Stepan Dyatkovskiy | 016dadd | 2014-08-25 08:12:45 +0000 | [diff] [blame] | 1190 | if (int Res = cmpGEPs(GEPL, GEPR)) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1191 | return Res; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1192 | } else { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 1193 | if (int Res = cmpOperations(&*InstL, &*InstR)) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1194 | return Res; |
| 1195 | assert(InstL->getNumOperands() == InstR->getNumOperands()); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1196 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1197 | for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) { |
| 1198 | Value *OpL = InstL->getOperand(i); |
| 1199 | Value *OpR = InstR->getOperand(i); |
| 1200 | if (int Res = cmpValues(OpL, OpR)) |
| 1201 | return Res; |
JF Bastien | 9dc042a | 2015-08-26 03:02:58 +0000 | [diff] [blame] | 1202 | // cmpValues should ensure this is true. |
| 1203 | assert(cmpTypes(OpL->getType(), OpR->getType()) == 0); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1204 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1207 | ++InstL; |
| 1208 | ++InstR; |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1209 | } while (InstL != InstLE && InstR != InstRE); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1210 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1211 | if (InstL != InstLE && InstR == InstRE) |
| 1212 | return 1; |
| 1213 | if (InstL == InstLE && InstR != InstRE) |
| 1214 | return -1; |
| 1215 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1218 | // Test whether the two functions have equivalent behaviour. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1219 | int FunctionComparator::compare() { |
Stepan Dyatkovskiy | cfd641f | 2014-05-07 11:11:39 +0000 | [diff] [blame] | 1220 | sn_mapL.clear(); |
| 1221 | sn_mapR.clear(); |
| 1222 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1223 | if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes())) |
| 1224 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1225 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1226 | if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC())) |
| 1227 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1228 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1229 | if (FnL->hasGC()) { |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1230 | if (int Res = cmpMem(FnL->getGC(), FnR->getGC())) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1231 | return Res; |
| 1232 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1233 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1234 | if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection())) |
| 1235 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1236 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1237 | if (FnL->hasSection()) { |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1238 | if (int Res = cmpMem(FnL->getSection(), FnR->getSection())) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1239 | return Res; |
| 1240 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1241 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1242 | if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg())) |
| 1243 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1244 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1245 | // TODO: if it's internal and only used in direct calls, we could handle this |
| 1246 | // case too. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1247 | if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv())) |
| 1248 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1249 | |
Stepan Dyatkovskiy | 0b765de | 2014-08-25 08:16:39 +0000 | [diff] [blame] | 1250 | if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType())) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1251 | return Res; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1252 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1253 | assert(FnL->arg_size() == FnR->arg_size() && |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1254 | "Identically typed functions have different numbers of args!"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1255 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1256 | // Visit the arguments so that they get enumerated in the order they're |
| 1257 | // passed in. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1258 | for (Function::const_arg_iterator ArgLI = FnL->arg_begin(), |
| 1259 | ArgRI = FnR->arg_begin(), |
| 1260 | ArgLE = FnL->arg_end(); |
| 1261 | ArgLI != ArgLE; ++ArgLI, ++ArgRI) { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 1262 | if (cmpValues(&*ArgLI, &*ArgRI) != 0) |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1263 | llvm_unreachable("Arguments repeat!"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1266 | // We do a CFG-ordered walk since the actual ordering of the blocks in the |
| 1267 | // 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] | 1268 | // functions, then takes each block from each terminator in order. As an |
| 1269 | // artifact, this also means that unreachable blocks are ignored. |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1270 | SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs; |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 1271 | SmallPtrSet<const BasicBlock *, 32> VisitedBBs; // in terms of F1. |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1272 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1273 | FnLBBs.push_back(&FnL->getEntryBlock()); |
| 1274 | FnRBBs.push_back(&FnR->getEntryBlock()); |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1275 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1276 | VisitedBBs.insert(FnLBBs[0]); |
| 1277 | while (!FnLBBs.empty()) { |
| 1278 | const BasicBlock *BBL = FnLBBs.pop_back_val(); |
| 1279 | const BasicBlock *BBR = FnRBBs.pop_back_val(); |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1280 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1281 | if (int Res = cmpValues(BBL, BBR)) |
| 1282 | return Res; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1283 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1284 | if (int Res = cmpBasicBlocks(BBL, BBR)) |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1285 | return Res; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1286 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1287 | const TerminatorInst *TermL = BBL->getTerminator(); |
| 1288 | const TerminatorInst *TermR = BBR->getTerminator(); |
| 1289 | |
| 1290 | assert(TermL->getNumSuccessors() == TermR->getNumSuccessors()); |
| 1291 | for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1292 | if (!VisitedBBs.insert(TermL->getSuccessor(i)).second) |
Nick Lewycky | 2b3cbac | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 1293 | continue; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1294 | |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1295 | FnLBBs.push_back(TermL->getSuccessor(i)); |
| 1296 | FnRBBs.push_back(TermR->getSuccessor(i)); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
Stepan Dyatkovskiy | 17ee5ac | 2014-06-21 17:55:51 +0000 | [diff] [blame] | 1299 | return 0; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Benjamin Kramer | 83709b1 | 2015-11-16 09:01:28 +0000 | [diff] [blame] | 1302 | namespace { |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 1303 | // Accumulate the hash of a sequence of 64-bit integers. This is similar to a |
| 1304 | // hash of a sequence of 64bit ints, but the entire input does not need to be |
| 1305 | // available at once. This interface is necessary for functionHash because it |
| 1306 | // needs to accumulate the hash as the structure of the function is traversed |
| 1307 | // without saving these values to an intermediate buffer. This form of hashing |
| 1308 | // is not often needed, as usually the object to hash is just read from a |
| 1309 | // buffer. |
| 1310 | class HashAccumulator64 { |
| 1311 | uint64_t Hash; |
| 1312 | public: |
| 1313 | // Initialize to random constant, so the state isn't zero. |
| 1314 | HashAccumulator64() { Hash = 0x6acaa36bef8325c5ULL; } |
| 1315 | void add(uint64_t V) { |
| 1316 | Hash = llvm::hashing::detail::hash_16_bytes(Hash, V); |
| 1317 | } |
| 1318 | // No finishing is required, because the entire hash value is used. |
| 1319 | uint64_t getHash() { return Hash; } |
| 1320 | }; |
Benjamin Kramer | 83709b1 | 2015-11-16 09:01:28 +0000 | [diff] [blame] | 1321 | } // end anonymous namespace |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 1322 | |
| 1323 | // A function hash is calculated by considering only the number of arguments and |
| 1324 | // whether a function is varargs, the order of basic blocks (given by the |
| 1325 | // successors of each basic block in depth first order), and the order of |
| 1326 | // opcodes of each instruction within each of these basic blocks. This mirrors |
| 1327 | // the strategy compare() uses to compare functions by walking the BBs in depth |
| 1328 | // first order and comparing each instruction in sequence. Because this hash |
| 1329 | // does not look at the operands, it is insensitive to things such as the |
| 1330 | // target of calls and the constants used in the function, which makes it useful |
| 1331 | // when possibly merging functions which are the same modulo constants and call |
| 1332 | // targets. |
| 1333 | FunctionComparator::FunctionHash FunctionComparator::functionHash(Function &F) { |
| 1334 | HashAccumulator64 H; |
| 1335 | H.add(F.isVarArg()); |
| 1336 | H.add(F.arg_size()); |
| 1337 | |
| 1338 | SmallVector<const BasicBlock *, 8> BBs; |
| 1339 | SmallSet<const BasicBlock *, 16> VisitedBBs; |
| 1340 | |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1341 | // Walk the blocks in the same order as FunctionComparator::cmpBasicBlocks(), |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 1342 | // accumulating the hash of the function "structure." (BB and opcode sequence) |
| 1343 | BBs.push_back(&F.getEntryBlock()); |
| 1344 | VisitedBBs.insert(BBs[0]); |
| 1345 | while (!BBs.empty()) { |
| 1346 | const BasicBlock *BB = BBs.pop_back_val(); |
| 1347 | // This random value acts as a block header, as otherwise the partition of |
| 1348 | // opcodes into BBs wouldn't affect the hash, only the order of the opcodes |
| 1349 | H.add(45798); |
| 1350 | for (auto &Inst : *BB) { |
| 1351 | H.add(Inst.getOpcode()); |
| 1352 | } |
| 1353 | const TerminatorInst *Term = BB->getTerminator(); |
| 1354 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 1355 | if (!VisitedBBs.insert(Term->getSuccessor(i)).second) |
| 1356 | continue; |
| 1357 | BBs.push_back(Term->getSuccessor(i)); |
| 1358 | } |
| 1359 | } |
| 1360 | return H.getHash(); |
| 1361 | } |
| 1362 | |
| 1363 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1364 | namespace { |
| 1365 | |
| 1366 | /// MergeFunctions finds functions which will generate identical machine code, |
| 1367 | /// by considering all pointer types to be equivalent. Once identified, |
| 1368 | /// MergeFunctions will fold them by replacing a call to one to a call to a |
| 1369 | /// bitcast of the other. |
| 1370 | /// |
| 1371 | class MergeFunctions : public ModulePass { |
| 1372 | public: |
| 1373 | static char ID; |
| 1374 | MergeFunctions() |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1375 | : ModulePass(ID), FnTree(FunctionNodeCmp(&GlobalNumbers)), FNodesInTree(), |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1376 | HasGlobalAliases(false) { |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1377 | initializeMergeFunctionsPass(*PassRegistry::getPassRegistry()); |
| 1378 | } |
| 1379 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 1380 | bool runOnModule(Module &M) override; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1381 | |
| 1382 | private: |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1383 | // The function comparison operator is provided here so that FunctionNodes do |
| 1384 | // not need to become larger with another pointer. |
| 1385 | class FunctionNodeCmp { |
| 1386 | GlobalNumberState* GlobalNumbers; |
| 1387 | public: |
| 1388 | FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {} |
| 1389 | bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const { |
| 1390 | // Order first by hashes, then full function comparison. |
| 1391 | if (LHS.getHash() != RHS.getHash()) |
| 1392 | return LHS.getHash() < RHS.getHash(); |
| 1393 | FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers); |
| 1394 | return FCmp.compare() == -1; |
| 1395 | } |
| 1396 | }; |
| 1397 | typedef std::set<FunctionNode, FunctionNodeCmp> FnTreeType; |
| 1398 | |
| 1399 | GlobalNumberState GlobalNumbers; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1400 | |
| 1401 | /// A work queue of functions that may have been modified and should be |
| 1402 | /// analyzed again. |
| 1403 | std::vector<WeakVH> Deferred; |
| 1404 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1405 | /// Checks the rules of order relation introduced among functions set. |
| 1406 | /// Returns true, if sanity check has been passed, and false if failed. |
| 1407 | bool doSanityCheck(std::vector<WeakVH> &Worklist); |
| 1408 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1409 | /// 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] | 1410 | /// equal to one that's already present. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1411 | bool insert(Function *NewFunction); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1412 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1413 | /// 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] | 1414 | /// analysis. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1415 | void remove(Function *F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1416 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1417 | /// 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] | 1418 | /// queue the functions. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1419 | void removeUsers(Value *V); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1420 | |
| 1421 | /// Replace all direct calls of Old with calls of New. Will bitcast New if |
| 1422 | /// necessary to make types match. |
| 1423 | void replaceDirectCallers(Function *Old, Function *New); |
| 1424 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1425 | /// Merge two equivalent functions. Upon completion, G may be deleted, or may |
| 1426 | /// be converted into a thunk. In either case, it should never be visited |
| 1427 | /// again. |
| 1428 | void mergeTwoFunctions(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1429 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1430 | /// Replace G with a thunk or an alias to F. Deletes G. |
| 1431 | void writeThunkOrAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1432 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1433 | /// Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 1434 | /// of G with bitcast(F). Deletes G. |
| 1435 | void writeThunk(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1436 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1437 | /// Replace G with an alias to F. Deletes G. |
| 1438 | void writeAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1439 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1440 | /// Replace function F with function G in the function tree. |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1441 | void replaceFunctionInTree(const FunctionNode &FN, Function *G); |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1442 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1443 | /// The set of all distinct functions. Use the insert() and remove() methods |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1444 | /// to modify it. The map allows efficient lookup and deferring of Functions. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1445 | FnTreeType FnTree; |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1446 | // Map functions to the iterators of the FunctionNode which contains them |
| 1447 | // in the FnTree. This must be updated carefully whenever the FnTree is |
| 1448 | // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid |
| 1449 | // dangling iterators into FnTree. The invariant that preserves this is that |
| 1450 | // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree. |
| 1451 | ValueMap<Function*, FnTreeType::iterator> FNodesInTree; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1452 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1453 | /// Whether or not the target supports global aliases. |
| 1454 | bool HasGlobalAliases; |
| 1455 | }; |
| 1456 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 1457 | } // end anonymous namespace |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1458 | |
| 1459 | char MergeFunctions::ID = 0; |
| 1460 | INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false) |
| 1461 | |
| 1462 | ModulePass *llvm::createMergeFunctionsPass() { |
| 1463 | return new MergeFunctions(); |
| 1464 | } |
| 1465 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1466 | bool MergeFunctions::doSanityCheck(std::vector<WeakVH> &Worklist) { |
| 1467 | if (const unsigned Max = NumFunctionsForSanityCheck) { |
| 1468 | unsigned TripleNumber = 0; |
| 1469 | bool Valid = true; |
| 1470 | |
| 1471 | dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n"; |
| 1472 | |
| 1473 | unsigned i = 0; |
| 1474 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), E = Worklist.end(); |
| 1475 | I != E && i < Max; ++I, ++i) { |
| 1476 | unsigned j = i; |
| 1477 | for (std::vector<WeakVH>::iterator J = I; J != E && j < Max; ++J, ++j) { |
| 1478 | Function *F1 = cast<Function>(*I); |
| 1479 | Function *F2 = cast<Function>(*J); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1480 | int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare(); |
| 1481 | int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1482 | |
| 1483 | // If F1 <= F2, then F2 >= F1, otherwise report failure. |
| 1484 | if (Res1 != -Res2) { |
| 1485 | dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber |
| 1486 | << "\n"; |
| 1487 | F1->dump(); |
| 1488 | F2->dump(); |
| 1489 | Valid = false; |
| 1490 | } |
| 1491 | |
| 1492 | if (Res1 == 0) |
| 1493 | continue; |
| 1494 | |
| 1495 | unsigned k = j; |
| 1496 | for (std::vector<WeakVH>::iterator K = J; K != E && k < Max; |
| 1497 | ++k, ++K, ++TripleNumber) { |
| 1498 | if (K == J) |
| 1499 | continue; |
| 1500 | |
| 1501 | Function *F3 = cast<Function>(*K); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1502 | int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare(); |
| 1503 | int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare(); |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1504 | |
| 1505 | bool Transitive = true; |
| 1506 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1507 | if (Res1 != 0 && Res1 == Res4) { |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 1508 | // F1 > F2, F2 > F3 => F1 > F3 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1509 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 1510 | } else if (Res3 != 0 && Res3 == -Res4) { |
| 1511 | // F1 > F3, F3 > F2 => F1 > F2 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1512 | Transitive = Res3 == Res1; |
Stepan Dyatkovskiy | 0b58801 | 2014-06-21 19:07:51 +0000 | [diff] [blame] | 1513 | } else if (Res4 != 0 && -Res3 == Res4) { |
| 1514 | // F2 > F3, F3 > F1 => F2 > F1 |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1515 | Transitive = Res4 == -Res1; |
| 1516 | } |
| 1517 | |
| 1518 | if (!Transitive) { |
| 1519 | dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: " |
| 1520 | << TripleNumber << "\n"; |
| 1521 | dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", " |
| 1522 | << Res4 << "\n"; |
| 1523 | F1->dump(); |
| 1524 | F2->dump(); |
| 1525 | F3->dump(); |
| 1526 | Valid = false; |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n"; |
| 1533 | return Valid; |
| 1534 | } |
| 1535 | return true; |
| 1536 | } |
| 1537 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1538 | bool MergeFunctions::runOnModule(Module &M) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 1539 | if (skipModule(M)) |
| 1540 | return false; |
| 1541 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1542 | bool Changed = false; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1543 | |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 1544 | // All functions in the module, ordered by hash. Functions with a unique |
| 1545 | // hash value are easily eliminated. |
| 1546 | std::vector<std::pair<FunctionComparator::FunctionHash, Function *>> |
| 1547 | HashedFuncs; |
| 1548 | for (Function &Func : M) { |
| 1549 | if (!Func.isDeclaration() && !Func.hasAvailableExternallyLinkage()) { |
| 1550 | HashedFuncs.push_back({FunctionComparator::functionHash(Func), &Func}); |
| 1551 | } |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1552 | } |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1553 | |
NAKAMURA Takumi | 5196275 | 2015-08-16 02:41:23 +0000 | [diff] [blame] | 1554 | std::stable_sort( |
| 1555 | HashedFuncs.begin(), HashedFuncs.end(), |
| 1556 | [](const std::pair<FunctionComparator::FunctionHash, Function *> &a, |
| 1557 | const std::pair<FunctionComparator::FunctionHash, Function *> &b) { |
| 1558 | return a.first < b.first; |
| 1559 | }); |
JF Bastien | 5e4303d | 2015-08-15 01:18:18 +0000 | [diff] [blame] | 1560 | |
| 1561 | auto S = HashedFuncs.begin(); |
| 1562 | for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) { |
| 1563 | // If the hash value matches the previous value or the next one, we must |
| 1564 | // consider merging it. Otherwise it is dropped and never considered again. |
| 1565 | if ((I != S && std::prev(I)->first == I->first) || |
| 1566 | (std::next(I) != IE && std::next(I)->first == I->first) ) { |
| 1567 | Deferred.push_back(WeakVH(I->second)); |
| 1568 | } |
| 1569 | } |
| 1570 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1571 | do { |
| 1572 | std::vector<WeakVH> Worklist; |
| 1573 | Deferred.swap(Worklist); |
| 1574 | |
Stepan Dyatkovskiy | a77f3d8 | 2014-06-21 18:58:11 +0000 | [diff] [blame] | 1575 | DEBUG(doSanityCheck(Worklist)); |
| 1576 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1577 | DEBUG(dbgs() << "size of module: " << M.size() << '\n'); |
| 1578 | DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); |
| 1579 | |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame^] | 1580 | // Insert functions and merge them. |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1581 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), |
| 1582 | E = Worklist.end(); I != E; ++I) { |
| 1583 | if (!*I) continue; |
| 1584 | Function *F = cast<Function>(*I); |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame^] | 1585 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) { |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1586 | Changed |= insert(F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1589 | DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n'); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1590 | } while (!Deferred.empty()); |
| 1591 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1592 | FnTree.clear(); |
Arnold Schwaighofer | 0591c5d | 2015-10-05 17:26:36 +0000 | [diff] [blame] | 1593 | GlobalNumbers.clear(); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 1594 | |
| 1595 | return Changed; |
| 1596 | } |
| 1597 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1598 | // Replace direct callers of Old with New. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1599 | void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { |
| 1600 | Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1601 | for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) { |
| 1602 | Use *U = &*UI; |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1603 | ++UI; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1604 | CallSite CS(U->getUser()); |
| 1605 | if (CS && CS.isCallee(U)) { |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 1606 | // Transfer the called function's attributes to the call site. Due to the |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 1607 | // bitcast we will 'lose' ABI changing attributes because the 'called |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 1608 | // function' is no longer a Function* but the bitcast. Code that looks up |
| 1609 | // the attributes from the called function will fail. |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 1610 | |
| 1611 | // FIXME: This is not actually true, at least not anymore. The callsite |
| 1612 | // will always have the same ABI affecting attributes as the callee, |
| 1613 | // because otherwise the original input has UB. Note that Old and New |
| 1614 | // always have matching ABI, so no attributes need to be changed. |
| 1615 | // Transferring other attributes may help other optimizations, but that |
| 1616 | // should be done uniformly and not in this ad-hoc way. |
Arnold Schwaighofer | 3651233 | 2015-07-21 17:07:07 +0000 | [diff] [blame] | 1617 | auto &Context = New->getContext(); |
| 1618 | auto NewFuncAttrs = New->getAttributes(); |
| 1619 | auto CallSiteAttrs = CS.getAttributes(); |
| 1620 | |
| 1621 | CallSiteAttrs = CallSiteAttrs.addAttributes( |
| 1622 | Context, AttributeSet::ReturnIndex, NewFuncAttrs.getRetAttributes()); |
| 1623 | |
| 1624 | for (unsigned argIdx = 0; argIdx < CS.arg_size(); argIdx++) { |
| 1625 | AttributeSet Attrs = NewFuncAttrs.getParamAttributes(argIdx); |
| 1626 | if (Attrs.getNumSlots()) |
| 1627 | CallSiteAttrs = CallSiteAttrs.addAttributes(Context, argIdx, Attrs); |
| 1628 | } |
| 1629 | |
| 1630 | CS.setAttributes(CallSiteAttrs); |
| 1631 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1632 | remove(CS.getInstruction()->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1633 | U->set(BitcastNew); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1638 | // Replace G with an alias to F if possible, or else a thunk to F. Deletes G. |
| 1639 | void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) { |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1640 | if (HasGlobalAliases && G->hasUnnamedAddr()) { |
| 1641 | if (G->hasExternalLinkage() || G->hasLocalLinkage() || |
| 1642 | G->hasWeakLinkage()) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1643 | writeAlias(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1644 | return; |
| 1645 | } |
| 1646 | } |
| 1647 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1648 | writeThunk(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1651 | // Helper for writeThunk, |
| 1652 | // Selects proper bitcast operation, |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1653 | // but a bit simpler then CastInst::getCastOpcode. |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 1654 | static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1655 | Type *SrcTy = V->getType(); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 1656 | if (SrcTy->isStructTy()) { |
| 1657 | assert(DestTy->isStructTy()); |
| 1658 | assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements()); |
| 1659 | Value *Result = UndefValue::get(DestTy); |
| 1660 | for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) { |
| 1661 | Value *Element = createCast( |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1662 | Builder, Builder.CreateExtractValue(V, makeArrayRef(I)), |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 1663 | DestTy->getStructElementType(I)); |
| 1664 | |
| 1665 | Result = |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1666 | Builder.CreateInsertValue(Result, Element, makeArrayRef(I)); |
Carlo Kok | 307625c | 2014-04-30 17:53:04 +0000 | [diff] [blame] | 1667 | } |
| 1668 | return Result; |
| 1669 | } |
| 1670 | assert(!DestTy->isStructTy()); |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1671 | if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) |
| 1672 | return Builder.CreateIntToPtr(V, DestTy); |
| 1673 | else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) |
| 1674 | return Builder.CreatePtrToInt(V, DestTy); |
| 1675 | else |
| 1676 | return Builder.CreateBitCast(V, DestTy); |
| 1677 | } |
| 1678 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1679 | // Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 1680 | // of G with bitcast(F). Deletes G. |
| 1681 | void MergeFunctions::writeThunk(Function *F, Function *G) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 1682 | if (!G->isInterposable()) { |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1683 | // Redirect direct callers of G to F. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1684 | replaceDirectCallers(G, F); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1687 | // 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] | 1688 | // stop here and delete G. There's no need for a thunk. |
| 1689 | if (G->hasLocalLinkage() && G->use_empty()) { |
| 1690 | G->eraseFromParent(); |
| 1691 | return; |
| 1692 | } |
| 1693 | |
Nick Lewycky | 25675ac | 2009-06-12 15:56:56 +0000 | [diff] [blame] | 1694 | Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", |
| 1695 | G->getParent()); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1696 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG); |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 1697 | IRBuilder<> Builder(BB); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1698 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1699 | SmallVector<Value *, 16> Args; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1700 | unsigned i = 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1701 | FunctionType *FFTy = F->getFunctionType(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 1702 | for (Argument & AI : NewG->args()) { |
| 1703 | Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i))); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1704 | ++i; |
| 1705 | } |
| 1706 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1707 | CallInst *CI = Builder.CreateCall(F, Args); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1708 | CI->setTailCall(); |
Nick Lewycky | d5bf51f | 2009-06-12 16:04:00 +0000 | [diff] [blame] | 1709 | CI->setCallingConv(F->getCallingConv()); |
JF Bastien | fa94623 | 2015-09-10 18:08:35 +0000 | [diff] [blame] | 1710 | CI->setAttributes(F->getAttributes()); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1711 | if (NewG->getReturnType()->isVoidTy()) { |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1712 | Builder.CreateRetVoid(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1713 | } else { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 1714 | Builder.CreateRet(createCast(Builder, CI, NewG->getReturnType())); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | NewG->copyAttributesFrom(G); |
| 1718 | NewG->takeName(G); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1719 | removeUsers(G); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1720 | G->replaceAllUsesWith(NewG); |
| 1721 | G->eraseFromParent(); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1722 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1723 | DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n'); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1724 | ++NumThunksWritten; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1727 | // Replace G with an alias to F and delete G. |
| 1728 | void MergeFunctions::writeAlias(Function *F, Function *G) { |
David Blaikie | 6614d8d | 2015-09-14 20:29:26 +0000 | [diff] [blame] | 1729 | auto *GA = GlobalAlias::create(G->getLinkage(), "", F); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1730 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 1731 | GA->takeName(G); |
| 1732 | GA->setVisibility(G->getVisibility()); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1733 | removeUsers(G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1734 | G->replaceAllUsesWith(GA); |
| 1735 | G->eraseFromParent(); |
| 1736 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1737 | DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n'); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1738 | ++NumAliasesWritten; |
| 1739 | } |
| 1740 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1741 | // Merge two equivalent functions. Upon completion, Function G is deleted. |
| 1742 | void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 1743 | if (F->isInterposable()) { |
| 1744 | assert(G->isInterposable()); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 1745 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 1746 | // Make them both thunks to the same internal function. |
| 1747 | Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "", |
| 1748 | F->getParent()); |
| 1749 | H->copyAttributesFrom(F); |
| 1750 | H->takeName(F); |
| 1751 | removeUsers(F); |
| 1752 | F->replaceAllUsesWith(H); |
| 1753 | |
| 1754 | unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment()); |
| 1755 | |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1756 | if (HasGlobalAliases) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1757 | writeAlias(F, G); |
| 1758 | writeAlias(F, H); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1759 | } else { |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 1760 | writeThunk(F, G); |
| 1761 | writeThunk(F, H); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 1762 | } |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1763 | |
Arnold Schwaighofer | 7e22627 | 2015-06-09 18:19:17 +0000 | [diff] [blame] | 1764 | F->setAlignment(MaxAlignment); |
| 1765 | F->setLinkage(GlobalValue::PrivateLinkage); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 1766 | ++NumDoubleWeak; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 1767 | } else { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1768 | writeThunkOrAlias(F, G); |
Nick Lewycky | 3c6d34a | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 1771 | ++NumFunctionsMerged; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1774 | /// Replace function F by function G. |
| 1775 | void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN, |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1776 | Function *G) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1777 | Function *F = FN.getFunc(); |
JF Bastien | 057292a | 2015-08-21 23:27:24 +0000 | [diff] [blame] | 1778 | assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 && |
| 1779 | "The two functions must be equal"); |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1780 | |
| 1781 | auto I = FNodesInTree.find(F); |
| 1782 | assert(I != FNodesInTree.end() && "F should be in FNodesInTree"); |
| 1783 | assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G"); |
| 1784 | |
| 1785 | FnTreeType::iterator IterToFNInFnTree = I->second; |
| 1786 | assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree."); |
| 1787 | // Remove F -> FN and insert G -> FN |
| 1788 | FNodesInTree.erase(I); |
| 1789 | FNodesInTree.insert({G, IterToFNInFnTree}); |
| 1790 | // Replace F with G in FN, which is stored inside the FnTree. |
| 1791 | FN.replaceBy(G); |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1794 | // 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] | 1795 | // that was already inserted. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1796 | bool MergeFunctions::insert(Function *NewFunction) { |
| 1797 | std::pair<FnTreeType::iterator, bool> Result = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1798 | FnTree.insert(FunctionNode(NewFunction)); |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1799 | |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 1800 | if (Result.second) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1801 | assert(FNodesInTree.count(NewFunction) == 0); |
| 1802 | FNodesInTree.insert({NewFunction, Result.first}); |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1803 | DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1804 | return false; |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 1805 | } |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1806 | |
Stepan Dyatkovskiy | fe134cd | 2014-09-10 10:08:25 +0000 | [diff] [blame] | 1807 | const FunctionNode &OldF = *Result.first; |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1808 | |
Matt Arsenault | 517d84e | 2013-10-01 18:05:30 +0000 | [diff] [blame] | 1809 | // Don't merge tiny functions, since it can just end up making the function |
| 1810 | // larger. |
| 1811 | // FIXME: Should still merge them if they are unnamed_addr and produce an |
| 1812 | // alias. |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1813 | if (NewFunction->size() == 1) { |
| 1814 | if (NewFunction->front().size() <= 2) { |
| 1815 | DEBUG(dbgs() << NewFunction->getName() |
| 1816 | << " is to small to bother merging\n"); |
Matt Arsenault | 517d84e | 2013-10-01 18:05:30 +0000 | [diff] [blame] | 1817 | return false; |
| 1818 | } |
| 1819 | } |
| 1820 | |
Arnold Schwaighofer | 0302da6 | 2015-06-09 00:03:29 +0000 | [diff] [blame] | 1821 | // Impose a total order (by name) on the replacement of functions. This is |
| 1822 | // important when operating on more than one module independently to prevent |
| 1823 | // cycles of thunks calling each other when the modules are linked together. |
| 1824 | // |
Erik Eckstein | 0c48dd8 | 2016-05-31 17:20:23 +0000 | [diff] [blame^] | 1825 | // First of all, we process strong functions before weak functions. |
| 1826 | if ((OldF.getFunc()->isInterposable() && !NewFunction->isInterposable()) || |
| 1827 | (OldF.getFunc()->isInterposable() == NewFunction->isInterposable() && |
| 1828 | OldF.getFunc()->getName() > NewFunction->getName())) { |
| 1829 | // Swap the two functions. |
| 1830 | Function *F = OldF.getFunc(); |
| 1831 | replaceFunctionInTree(*Result.first, NewFunction); |
| 1832 | NewFunction = F; |
| 1833 | assert(OldF.getFunc() != F && "Must have swapped the functions."); |
| 1834 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1835 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1836 | DEBUG(dbgs() << " " << OldF.getFunc()->getName() |
| 1837 | << " == " << NewFunction->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1838 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1839 | Function *DeleteF = NewFunction; |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1840 | mergeTwoFunctions(OldF.getFunc(), DeleteF); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1841 | return true; |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 1842 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 1843 | |
Stepan Dyatkovskiy | f4af855 | 2014-06-21 20:54:36 +0000 | [diff] [blame] | 1844 | // Remove a function from FnTree. If it was already in FnTree, add |
| 1845 | // 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] | 1846 | void MergeFunctions::remove(Function *F) { |
JF Bastien | 3a4ad61 | 2015-09-02 23:55:23 +0000 | [diff] [blame] | 1847 | auto I = FNodesInTree.find(F); |
| 1848 | if (I != FNodesInTree.end()) { |
| 1849 | DEBUG(dbgs() << "Deferred " << F->getName()<< ".\n"); |
| 1850 | FnTree.erase(I->second); |
| 1851 | // I->second has been invalidated, remove it from the FNodesInTree map to |
| 1852 | // preserve the invariant. |
| 1853 | FNodesInTree.erase(I); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 1854 | Deferred.emplace_back(F); |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 1855 | } |
Nick Lewycky | 4e250c8 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 1856 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1857 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1858 | // For each instruction used by the value, remove() the function that contains |
| 1859 | // the instruction. This should happen right before a call to RAUW. |
| 1860 | void MergeFunctions::removeUsers(Value *V) { |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 1861 | std::vector<Value *> Worklist; |
| 1862 | Worklist.push_back(V); |
JF Bastien | 7289f73 | 2015-07-15 21:51:33 +0000 | [diff] [blame] | 1863 | SmallSet<Value*, 8> Visited; |
| 1864 | Visited.insert(V); |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 1865 | while (!Worklist.empty()) { |
| 1866 | Value *V = Worklist.back(); |
| 1867 | Worklist.pop_back(); |
| 1868 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1869 | for (User *U : V->users()) { |
| 1870 | if (Instruction *I = dyn_cast<Instruction>(U)) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 1871 | remove(I->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1872 | } else if (isa<GlobalValue>(U)) { |
Nick Lewycky | 540f953 | 2011-01-15 10:16:23 +0000 | [diff] [blame] | 1873 | // do nothing |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1874 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
JF Bastien | 7289f73 | 2015-07-15 21:51:33 +0000 | [diff] [blame] | 1875 | for (User *UU : C->users()) { |
| 1876 | if (!Visited.insert(UU).second) |
| 1877 | Worklist.push_back(UU); |
| 1878 | } |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 1879 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1880 | } |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 1881 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 1882 | } |