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