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