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" |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 63 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 64 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 65 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 68984ed | 2010-08-31 08:29:37 +0000 | [diff] [blame] | 66 | #include <vector> |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 67 | using namespace llvm; |
| 68 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 69 | #define DEBUG_TYPE "mergefunc" |
| 70 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 71 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 72 | STATISTIC(NumThunksWritten, "Number of thunks generated"); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 73 | STATISTIC(NumAliasesWritten, "Number of aliases generated"); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 74 | STATISTIC(NumDoubleWeak, "Number of new functions created"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 75 | |
Benjamin Kramer | 630e6e1 | 2013-04-19 23:06:44 +0000 | [diff] [blame] | 76 | /// Returns the type id for a type to be hashed. We turn pointer types into |
| 77 | /// integers here because the actual compare logic below considers pointers and |
| 78 | /// integers of the same size as equal. |
| 79 | static Type::TypeID getTypeIDForHash(Type *Ty) { |
| 80 | if (Ty->isPointerTy()) |
| 81 | return Type::IntegerTyID; |
| 82 | return Ty->getTypeID(); |
| 83 | } |
| 84 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 85 | /// Creates a hash-code for the function which is the same for any two |
| 86 | /// functions that will compare equal, without looking at the instructions |
| 87 | /// inside the function. |
| 88 | static unsigned profileFunction(const Function *F) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 89 | FunctionType *FTy = F->getFunctionType(); |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 90 | |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 91 | FoldingSetNodeID ID; |
| 92 | ID.AddInteger(F->size()); |
| 93 | ID.AddInteger(F->getCallingConv()); |
| 94 | ID.AddBoolean(F->hasGC()); |
| 95 | ID.AddBoolean(FTy->isVarArg()); |
Benjamin Kramer | 630e6e1 | 2013-04-19 23:06:44 +0000 | [diff] [blame] | 96 | ID.AddInteger(getTypeIDForHash(FTy->getReturnType())); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 97 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Benjamin Kramer | 630e6e1 | 2013-04-19 23:06:44 +0000 | [diff] [blame] | 98 | ID.AddInteger(getTypeIDForHash(FTy->getParamType(i))); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 99 | return ID.ComputeHash(); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 102 | namespace { |
| 103 | |
Nick Lewycky | aaf4012 | 2011-01-28 08:19:00 +0000 | [diff] [blame] | 104 | /// ComparableFunction - A struct that pairs together functions with a |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 105 | /// 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] | 106 | class ComparableFunction { |
| 107 | public: |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 108 | static const ComparableFunction EmptyKey; |
| 109 | static const ComparableFunction TombstoneKey; |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 110 | static DataLayout * const LookupOnly; |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 111 | |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 112 | ComparableFunction(Function *Func, const DataLayout *DL) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 113 | : Func(Func), Hash(profileFunction(Func)), DL(DL) {} |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 114 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 115 | Function *getFunc() const { return Func; } |
| 116 | unsigned getHash() const { return Hash; } |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 117 | const DataLayout *getDataLayout() const { return DL; } |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 118 | |
| 119 | // Drops AssertingVH reference to the function. Outside of debug mode, this |
| 120 | // does nothing. |
| 121 | void release() { |
| 122 | assert(Func && |
| 123 | "Attempted to release function twice, or release empty/tombstone!"); |
| 124 | Func = NULL; |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | explicit ComparableFunction(unsigned Hash) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 129 | : Func(NULL), Hash(Hash), DL(NULL) {} |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 130 | |
| 131 | AssertingVH<Function> Func; |
| 132 | unsigned Hash; |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 133 | const DataLayout *DL; |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 136 | const ComparableFunction ComparableFunction::EmptyKey = ComparableFunction(0); |
| 137 | const ComparableFunction ComparableFunction::TombstoneKey = |
| 138 | ComparableFunction(1); |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 139 | DataLayout *const ComparableFunction::LookupOnly = (DataLayout*)(-1); |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 140 | |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 141 | } |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 142 | |
| 143 | namespace llvm { |
| 144 | template <> |
| 145 | struct DenseMapInfo<ComparableFunction> { |
| 146 | static ComparableFunction getEmptyKey() { |
| 147 | return ComparableFunction::EmptyKey; |
| 148 | } |
| 149 | static ComparableFunction getTombstoneKey() { |
| 150 | return ComparableFunction::TombstoneKey; |
| 151 | } |
| 152 | static unsigned getHashValue(const ComparableFunction &CF) { |
| 153 | return CF.getHash(); |
| 154 | } |
| 155 | static bool isEqual(const ComparableFunction &LHS, |
| 156 | const ComparableFunction &RHS); |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | namespace { |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 161 | |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 162 | /// FunctionComparator - Compares two functions to determine whether or not |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 163 | /// they will generate machine code with the same behaviour. DataLayout is |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 164 | /// used if available. The comparator always fails conservatively (erring on the |
| 165 | /// side of claiming that two functions are different). |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 166 | class FunctionComparator { |
| 167 | public: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 168 | FunctionComparator(const DataLayout *DL, const Function *F1, |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 169 | const Function *F2) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 170 | : F1(F1), F2(F2), DL(DL) {} |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 171 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 172 | /// Test whether the two functions have equivalent behaviour. |
| 173 | bool compare(); |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 174 | |
| 175 | private: |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 176 | /// Test whether two basic blocks have equivalent behaviour. |
| 177 | bool compare(const BasicBlock *BB1, const BasicBlock *BB2); |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 178 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 179 | /// Assign or look up previously assigned numbers for the two values, and |
| 180 | /// return whether the numbers are equal. Numbers are assigned in the order |
| 181 | /// visited. |
| 182 | bool enumerate(const Value *V1, const Value *V2); |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 183 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 184 | /// Compare two Instructions for equivalence, similar to |
| 185 | /// Instruction::isSameOperationAs but with modifications to the type |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 186 | /// comparison. |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 187 | bool isEquivalentOperation(const Instruction *I1, |
| 188 | const Instruction *I2) const; |
| 189 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 190 | /// Compare two GEPs for equivalent pointer arithmetic. |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 191 | bool isEquivalentGEP(const GEPOperator *GEP1, const GEPOperator *GEP2); |
| 192 | bool isEquivalentGEP(const GetElementPtrInst *GEP1, |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 193 | const GetElementPtrInst *GEP2) { |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 194 | return isEquivalentGEP(cast<GEPOperator>(GEP1), cast<GEPOperator>(GEP2)); |
| 195 | } |
| 196 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 197 | /// cmpType - compares two types, |
| 198 | /// defines total ordering among the types set. |
| 199 | /// |
| 200 | /// Return values: |
| 201 | /// 0 if types are equal, |
| 202 | /// -1 if Left is less than Right, |
| 203 | /// +1 if Left is greater than Right. |
| 204 | /// |
| 205 | /// Description: |
| 206 | /// Comparison is broken onto stages. Like in lexicographical comparison |
| 207 | /// stage coming first has higher priority. |
| 208 | /// On each explanation stage keep in mind total ordering properties. |
| 209 | /// |
Stepan Dyatkovskiy | 90c4436 | 2014-03-14 08:17:19 +0000 | [diff] [blame] | 210 | /// 0. Before comparison we coerce pointer types of 0 address space to |
| 211 | /// integer. |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 212 | /// We also don't bother with same type at left and right, so |
| 213 | /// just return 0 in this case. |
| 214 | /// |
| 215 | /// 1. If types are of different kind (different type IDs). |
| 216 | /// Return result of type IDs comparison, treating them as numbers. |
| 217 | /// 2. If types are vectors or integers, compare Type* values as numbers. |
| 218 | /// 3. Types has same ID, so check whether they belongs to the next group: |
| 219 | /// * Void |
| 220 | /// * Float |
| 221 | /// * Double |
| 222 | /// * X86_FP80 |
| 223 | /// * FP128 |
| 224 | /// * PPC_FP128 |
| 225 | /// * Label |
| 226 | /// * Metadata |
| 227 | /// If so - return 0, yes - we can treat these types as equal only because |
| 228 | /// their IDs are same. |
| 229 | /// 4. If Left and Right are pointers, return result of address space |
| 230 | /// comparison (numbers comparison). We can treat pointer types of same |
| 231 | /// address space as equal. |
| 232 | /// 5. If types are complex. |
| 233 | /// Then both Left and Right are to be expanded and their element types will |
| 234 | /// be checked with the same way. If we get Res != 0 on some stage, return it. |
| 235 | /// Otherwise return 0. |
| 236 | /// 6. For all other cases put llvm_unreachable. |
| 237 | int cmpType(Type *TyL, Type *TyR) const; |
| 238 | |
| 239 | bool isEquivalentType(Type *Ty1, Type *Ty2) const { |
| 240 | return cmpType(Ty1, Ty2) == 0; |
| 241 | } |
| 242 | |
| 243 | int cmpNumbers(uint64_t L, uint64_t R) const; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 244 | |
| 245 | // The two functions undergoing comparison. |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 246 | const Function *F1, *F2; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 247 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 248 | const DataLayout *DL; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 249 | |
Nick Lewycky | 080ea93 | 2011-02-20 08:11:03 +0000 | [diff] [blame] | 250 | DenseMap<const Value *, const Value *> id_map; |
| 251 | DenseSet<const Value *> seen_values; |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 252 | }; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 253 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 256 | int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const { |
| 257 | if (L < R) return -1; |
| 258 | if (L > R) return 1; |
| 259 | return 0; |
| 260 | } |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 261 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 262 | /// cmpType - compares two types, |
| 263 | /// defines total ordering among the types set. |
| 264 | /// See method declaration comments for more details. |
| 265 | int FunctionComparator::cmpType(Type *TyL, Type *TyR) const { |
| 266 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 267 | PointerType *PTyL = dyn_cast<PointerType>(TyL); |
| 268 | PointerType *PTyR = dyn_cast<PointerType>(TyR); |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 269 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 270 | if (DL) { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 271 | if (PTyL && PTyL->getAddressSpace() == 0) TyL = DL->getIntPtrType(TyL); |
| 272 | if (PTyR && PTyR->getAddressSpace() == 0) TyR = DL->getIntPtrType(TyR); |
Stepan Dyatkovskiy | abb8505 | 2013-11-26 16:11:03 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 275 | if (TyL == TyR) |
| 276 | return 0; |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 277 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 278 | if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID())) |
| 279 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 280 | |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 281 | switch (TyL->getTypeID()) { |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 282 | default: |
| 283 | llvm_unreachable("Unknown type!"); |
Duncan Sands | 408bb19 | 2010-07-07 07:48:00 +0000 | [diff] [blame] | 284 | // Fall through in Release mode. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 285 | case Type::IntegerTyID: |
Nick Lewycky | fb622f9 | 2011-01-26 08:50:18 +0000 | [diff] [blame] | 286 | case Type::VectorTyID: |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 287 | // TyL == TyR would have returned true earlier. |
| 288 | return cmpNumbers((uint64_t)TyL, (uint64_t)TyR); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 289 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 290 | case Type::VoidTyID: |
| 291 | case Type::FloatTyID: |
| 292 | case Type::DoubleTyID: |
| 293 | case Type::X86_FP80TyID: |
| 294 | case Type::FP128TyID: |
| 295 | case Type::PPC_FP128TyID: |
| 296 | case Type::LabelTyID: |
| 297 | case Type::MetadataTyID: |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 298 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 299 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 300 | case Type::PointerTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 301 | assert(PTyL && PTyR && "Both types must be pointers here."); |
| 302 | return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | case Type::StructTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 306 | StructType *STyL = cast<StructType>(TyL); |
| 307 | StructType *STyR = cast<StructType>(TyR); |
| 308 | if (STyL->getNumElements() != STyR->getNumElements()) |
| 309 | return cmpNumbers(STyL->getNumElements(), STyR->getNumElements()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 310 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 311 | if (STyL->isPacked() != STyR->isPacked()) |
| 312 | return cmpNumbers(STyL->isPacked(), STyR->isPacked()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 313 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 314 | for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) { |
| 315 | if (int Res = cmpType(STyL->getElementType(i), |
| 316 | STyR->getElementType(i))) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 317 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 318 | } |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 319 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | case Type::FunctionTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 323 | FunctionType *FTyL = cast<FunctionType>(TyL); |
| 324 | FunctionType *FTyR = cast<FunctionType>(TyR); |
| 325 | if (FTyL->getNumParams() != FTyR->getNumParams()) |
| 326 | return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams()); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 327 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 328 | if (FTyL->isVarArg() != FTyR->isVarArg()) |
| 329 | return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg()); |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 330 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 331 | if (int Res = cmpType(FTyL->getReturnType(), FTyR->getReturnType())) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 332 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 333 | |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 334 | for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) { |
| 335 | if (int Res = cmpType(FTyL->getParamType(i), FTyR->getParamType(i))) |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 336 | return Res; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 337 | } |
Stepan Dyatkovskiy | d8eb0bc | 2014-03-13 11:54:50 +0000 | [diff] [blame] | 338 | return 0; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Nick Lewycky | 375efe3 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 341 | case Type::ArrayTyID: { |
Stepan Dyatkovskiy | a53cf97 | 2014-03-14 08:48:52 +0000 | [diff] [blame] | 342 | ArrayType *ATyL = cast<ArrayType>(TyL); |
| 343 | ArrayType *ATyR = cast<ArrayType>(TyR); |
| 344 | if (ATyL->getNumElements() != ATyR->getNumElements()) |
| 345 | return cmpNumbers(ATyL->getNumElements(), ATyR->getNumElements()); |
| 346 | return cmpType(ATyL->getElementType(), ATyR->getElementType()); |
Nick Lewycky | 375efe3 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 347 | } |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 351 | // Determine whether the two operations are the same except that pointer-to-A |
| 352 | // and pointer-to-B are equivalent. This should be kept in sync with |
| 353 | // Instruction::isSameOperationAs. |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 354 | bool FunctionComparator::isEquivalentOperation(const Instruction *I1, |
| 355 | const Instruction *I2) const { |
Nick Lewycky | cb1a4c2 | 2011-02-06 05:04:00 +0000 | [diff] [blame] | 356 | // Differences from Instruction::isSameOperationAs: |
| 357 | // * replace type comparison with calls to isEquivalentType. |
| 358 | // * we test for I->hasSameSubclassOptionalData (nuw/nsw/tail) at the top |
| 359 | // * because of the above, we don't test for the tail bit on calls later on |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 360 | if (I1->getOpcode() != I2->getOpcode() || |
| 361 | I1->getNumOperands() != I2->getNumOperands() || |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 362 | !isEquivalentType(I1->getType(), I2->getType()) || |
| 363 | !I1->hasSameSubclassOptionalData(I2)) |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 364 | return false; |
| 365 | |
| 366 | // We have two instructions of identical opcode and #operands. Check to see |
| 367 | // if all operands are the same type |
| 368 | for (unsigned i = 0, e = I1->getNumOperands(); i != e; ++i) |
| 369 | if (!isEquivalentType(I1->getOperand(i)->getType(), |
| 370 | I2->getOperand(i)->getType())) |
| 371 | return false; |
| 372 | |
| 373 | // Check special state that is a part of some instructions. |
| 374 | if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) |
| 375 | return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && |
Eli Friedman | 211e348 | 2011-08-15 22:16:46 +0000 | [diff] [blame] | 376 | LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() && |
| 377 | LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() && |
| 378 | LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 379 | if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) |
| 380 | return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && |
Eli Friedman | 211e348 | 2011-08-15 22:16:46 +0000 | [diff] [blame] | 381 | SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() && |
| 382 | SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() && |
| 383 | SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 384 | if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) |
| 385 | return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); |
| 386 | if (const CallInst *CI = dyn_cast<CallInst>(I1)) |
Nick Lewycky | cb1a4c2 | 2011-02-06 05:04:00 +0000 | [diff] [blame] | 387 | return CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && |
Nick Lewycky | 9154344 | 2011-01-26 09:23:19 +0000 | [diff] [blame] | 388 | CI->getAttributes() == cast<CallInst>(I2)->getAttributes(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 389 | if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) |
| 390 | return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && |
Nick Lewycky | 9154344 | 2011-01-26 09:23:19 +0000 | [diff] [blame] | 391 | CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes(); |
Eli Friedman | adec587 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 392 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) |
| 393 | return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); |
| 394 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) |
| 395 | return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); |
| 396 | if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) |
| 397 | return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() && |
| 398 | FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope(); |
| 399 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1)) |
| 400 | return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 401 | CXI->getSuccessOrdering() == |
| 402 | cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() && |
| 403 | CXI->getFailureOrdering() == |
| 404 | cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() && |
Eli Friedman | adec587 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 405 | CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope(); |
| 406 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1)) |
| 407 | return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() && |
| 408 | RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() && |
| 409 | RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() && |
| 410 | RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 411 | |
| 412 | return true; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 415 | // Determine whether two GEP operations perform the same underlying arithmetic. |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 416 | bool FunctionComparator::isEquivalentGEP(const GEPOperator *GEP1, |
| 417 | const GEPOperator *GEP2) { |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 418 | unsigned AS = GEP1->getPointerAddressSpace(); |
| 419 | if (AS != GEP2->getPointerAddressSpace()) |
| 420 | return false; |
| 421 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 422 | if (DL) { |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 423 | // When we have target data, we can reduce the GEP down to the value in bytes |
| 424 | // added to the address. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 425 | unsigned BitWidth = DL ? DL->getPointerSizeInBits(AS) : 1; |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 426 | APInt Offset1(BitWidth, 0), Offset2(BitWidth, 0); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 427 | if (GEP1->accumulateConstantOffset(*DL, Offset1) && |
| 428 | GEP2->accumulateConstantOffset(*DL, Offset2)) { |
Matt Arsenault | 5bcefab | 2013-11-10 01:44:37 +0000 | [diff] [blame] | 429 | return Offset1 == Offset2; |
| 430 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 433 | if (GEP1->getPointerOperand()->getType() != |
| 434 | GEP2->getPointerOperand()->getType()) |
| 435 | return false; |
| 436 | |
| 437 | if (GEP1->getNumOperands() != GEP2->getNumOperands()) |
| 438 | return false; |
| 439 | |
| 440 | for (unsigned i = 0, e = GEP1->getNumOperands(); i != e; ++i) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 441 | if (!enumerate(GEP1->getOperand(i), GEP2->getOperand(i))) |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
| 445 | return true; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 448 | // Compare two values used by the two functions under pair-wise comparison. If |
| 449 | // this is the first time the values are seen, they're added to the mapping so |
| 450 | // that we will detect mismatches on next use. |
| 451 | bool FunctionComparator::enumerate(const Value *V1, const Value *V2) { |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 452 | // Check for function @f1 referring to itself and function @f2 referring to |
| 453 | // itself, or referring to each other, or both referring to either of them. |
| 454 | // They're all equivalent if the two functions are otherwise equivalent. |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 455 | if (V1 == F1 && V2 == F2) |
| 456 | return true; |
| 457 | if (V1 == F2 && V2 == F1) |
| 458 | return true; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 459 | |
Benjamin Kramer | 57e3d65 | 2011-01-27 20:30:54 +0000 | [diff] [blame] | 460 | if (const Constant *C1 = dyn_cast<Constant>(V1)) { |
Nick Lewycky | 13e04ae | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 461 | if (V1 == V2) return true; |
Nick Lewycky | 13e04ae | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 462 | const Constant *C2 = dyn_cast<Constant>(V2); |
| 463 | if (!C2) return false; |
| 464 | // TODO: constant expressions with GEP or references to F1 or F2. |
| 465 | if (C1->isNullValue() && C2->isNullValue() && |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 466 | isEquivalentType(C1->getType(), C2->getType())) |
Nick Lewycky | 13e04ae | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 467 | return true; |
Nick Lewycky | e2d46d3 | 2011-01-27 19:51:31 +0000 | [diff] [blame] | 468 | // Try bitcasting C2 to C1's type. If the bitcast is legal and returns C1 |
| 469 | // then they must have equal bit patterns. |
Nick Lewycky | 13e04ae | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 470 | return C1->getType()->canLosslesslyBitCastTo(C2->getType()) && |
| 471 | C1 == ConstantExpr::getBitCast(const_cast<Constant*>(C2), C1->getType()); |
| 472 | } |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 473 | |
Nick Lewycky | f8797fd | 2011-02-06 04:33:50 +0000 | [diff] [blame] | 474 | if (isa<InlineAsm>(V1) || isa<InlineAsm>(V2)) |
| 475 | return V1 == V2; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 476 | |
Nick Lewycky | 080ea93 | 2011-02-20 08:11:03 +0000 | [diff] [blame] | 477 | // Check that V1 maps to V2. If we find a value that V1 maps to then we simply |
| 478 | // check whether it's equal to V2. When there is no mapping then we need to |
| 479 | // ensure that V2 isn't already equivalent to something else. For this |
| 480 | // purpose, we track the V2 values in a set. |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 481 | |
Nick Lewycky | 080ea93 | 2011-02-20 08:11:03 +0000 | [diff] [blame] | 482 | const Value *&map_elem = id_map[V1]; |
| 483 | if (map_elem) |
| 484 | return map_elem == V2; |
| 485 | if (!seen_values.insert(V2).second) |
| 486 | return false; |
| 487 | map_elem = V2; |
| 488 | return true; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 491 | // Test whether two basic blocks have equivalent behaviour. |
| 492 | bool FunctionComparator::compare(const BasicBlock *BB1, const BasicBlock *BB2) { |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 493 | BasicBlock::const_iterator F1I = BB1->begin(), F1E = BB1->end(); |
| 494 | BasicBlock::const_iterator F2I = BB2->begin(), F2E = BB2->end(); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 495 | |
| 496 | do { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 497 | if (!enumerate(F1I, F2I)) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 498 | return false; |
| 499 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 500 | if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) { |
| 501 | const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I); |
| 502 | if (!GEP2) |
| 503 | return false; |
Nick Lewycky | 47b71c5 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 504 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 505 | if (!enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand())) |
Nick Lewycky | 2b3cbac | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 506 | return false; |
Nick Lewycky | 47b71c5 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 507 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 508 | if (!isEquivalentGEP(GEP1, GEP2)) |
Nick Lewycky | 2b3cbac | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 509 | return false; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 510 | } else { |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 511 | if (!isEquivalentOperation(F1I, F2I)) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 512 | return false; |
| 513 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 514 | assert(F1I->getNumOperands() == F2I->getNumOperands()); |
| 515 | for (unsigned i = 0, e = F1I->getNumOperands(); i != e; ++i) { |
| 516 | Value *OpF1 = F1I->getOperand(i); |
| 517 | Value *OpF2 = F2I->getOperand(i); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 518 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 519 | if (!enumerate(OpF1, OpF2)) |
Nick Lewycky | 2b3cbac | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 520 | return false; |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 521 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 522 | if (OpF1->getValueID() != OpF2->getValueID() || |
| 523 | !isEquivalentType(OpF1->getType(), OpF2->getType())) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 528 | ++F1I, ++F2I; |
| 529 | } while (F1I != F1E && F2I != F2E); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 530 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 531 | return F1I == F1E && F2I == F2E; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 534 | // Test whether the two functions have equivalent behaviour. |
| 535 | bool FunctionComparator::compare() { |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 536 | // We need to recheck everything, but check the things that weren't included |
| 537 | // in the hash first. |
| 538 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 539 | if (F1->getAttributes() != F2->getAttributes()) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 540 | return false; |
| 541 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 542 | if (F1->hasGC() != F2->hasGC()) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 543 | return false; |
| 544 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 545 | if (F1->hasGC() && F1->getGC() != F2->getGC()) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 546 | return false; |
| 547 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 548 | if (F1->hasSection() != F2->hasSection()) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 549 | return false; |
| 550 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 551 | if (F1->hasSection() && F1->getSection() != F2->getSection()) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 552 | return false; |
| 553 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 554 | if (F1->isVarArg() != F2->isVarArg()) |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 555 | return false; |
| 556 | |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 557 | // TODO: if it's internal and only used in direct calls, we could handle this |
| 558 | // case too. |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 559 | if (F1->getCallingConv() != F2->getCallingConv()) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 560 | return false; |
| 561 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 562 | if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType())) |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 563 | return false; |
| 564 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 565 | assert(F1->arg_size() == F2->arg_size() && |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 566 | "Identically typed functions have different numbers of args!"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 567 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 568 | // Visit the arguments so that they get enumerated in the order they're |
| 569 | // passed in. |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 570 | for (Function::const_arg_iterator f1i = F1->arg_begin(), |
| 571 | f2i = F2->arg_begin(), f1e = F1->arg_end(); f1i != f1e; ++f1i, ++f2i) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 572 | if (!enumerate(f1i, f2i)) |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 573 | llvm_unreachable("Arguments repeat!"); |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 576 | // We do a CFG-ordered walk since the actual ordering of the blocks in the |
| 577 | // 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] | 578 | // functions, then takes each block from each terminator in order. As an |
| 579 | // artifact, this also means that unreachable blocks are ignored. |
| 580 | SmallVector<const BasicBlock *, 8> F1BBs, F2BBs; |
| 581 | SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1. |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 582 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 583 | F1BBs.push_back(&F1->getEntryBlock()); |
| 584 | F2BBs.push_back(&F2->getEntryBlock()); |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 585 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 586 | VisitedBBs.insert(F1BBs[0]); |
| 587 | while (!F1BBs.empty()) { |
| 588 | const BasicBlock *F1BB = F1BBs.pop_back_val(); |
| 589 | const BasicBlock *F2BB = F2BBs.pop_back_val(); |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 590 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 591 | if (!enumerate(F1BB, F2BB) || !compare(F1BB, F2BB)) |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 592 | return false; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 593 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 594 | const TerminatorInst *F1TI = F1BB->getTerminator(); |
| 595 | const TerminatorInst *F2TI = F2BB->getTerminator(); |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 596 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 597 | assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors()); |
| 598 | for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) { |
| 599 | if (!VisitedBBs.insert(F1TI->getSuccessor(i))) |
Nick Lewycky | 2b3cbac | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 600 | continue; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 601 | |
Nick Lewycky | f52bd9c | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 602 | F1BBs.push_back(F1TI->getSuccessor(i)); |
| 603 | F2BBs.push_back(F2TI->getSuccessor(i)); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 606 | return true; |
| 607 | } |
| 608 | |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 609 | namespace { |
| 610 | |
| 611 | /// MergeFunctions finds functions which will generate identical machine code, |
| 612 | /// by considering all pointer types to be equivalent. Once identified, |
| 613 | /// MergeFunctions will fold them by replacing a call to one to a call to a |
| 614 | /// bitcast of the other. |
| 615 | /// |
| 616 | class MergeFunctions : public ModulePass { |
| 617 | public: |
| 618 | static char ID; |
| 619 | MergeFunctions() |
| 620 | : ModulePass(ID), HasGlobalAliases(false) { |
| 621 | initializeMergeFunctionsPass(*PassRegistry::getPassRegistry()); |
| 622 | } |
| 623 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 624 | bool runOnModule(Module &M) override; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 625 | |
| 626 | private: |
| 627 | typedef DenseSet<ComparableFunction> FnSetType; |
| 628 | |
| 629 | /// A work queue of functions that may have been modified and should be |
| 630 | /// analyzed again. |
| 631 | std::vector<WeakVH> Deferred; |
| 632 | |
| 633 | /// Insert a ComparableFunction into the FnSet, or merge it away if it's |
| 634 | /// equal to one that's already present. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 635 | bool insert(ComparableFunction &NewF); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 636 | |
| 637 | /// Remove a Function from the FnSet and queue it up for a second sweep of |
| 638 | /// analysis. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 639 | void remove(Function *F); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 640 | |
| 641 | /// Find the functions that use this Value and remove them from FnSet and |
| 642 | /// queue the functions. |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 643 | void removeUsers(Value *V); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 644 | |
| 645 | /// Replace all direct calls of Old with calls of New. Will bitcast New if |
| 646 | /// necessary to make types match. |
| 647 | void replaceDirectCallers(Function *Old, Function *New); |
| 648 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 649 | /// Merge two equivalent functions. Upon completion, G may be deleted, or may |
| 650 | /// be converted into a thunk. In either case, it should never be visited |
| 651 | /// again. |
| 652 | void mergeTwoFunctions(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 653 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 654 | /// Replace G with a thunk or an alias to F. Deletes G. |
| 655 | void writeThunkOrAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 656 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 657 | /// Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 658 | /// of G with bitcast(F). Deletes G. |
| 659 | void writeThunk(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 660 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 661 | /// Replace G with an alias to F. Deletes G. |
| 662 | void writeAlias(Function *F, Function *G); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 663 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 664 | /// The set of all distinct functions. Use the insert() and remove() methods |
| 665 | /// to modify it. |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 666 | FnSetType FnSet; |
| 667 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 668 | /// DataLayout for more accurate GEP comparisons. May be NULL. |
Rafael Espindola | 43b5a51 | 2014-02-25 14:24:11 +0000 | [diff] [blame] | 669 | const DataLayout *DL; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 670 | |
| 671 | /// Whether or not the target supports global aliases. |
| 672 | bool HasGlobalAliases; |
| 673 | }; |
| 674 | |
| 675 | } // end anonymous namespace |
| 676 | |
| 677 | char MergeFunctions::ID = 0; |
| 678 | INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false) |
| 679 | |
| 680 | ModulePass *llvm::createMergeFunctionsPass() { |
| 681 | return new MergeFunctions(); |
| 682 | } |
| 683 | |
| 684 | bool MergeFunctions::runOnModule(Module &M) { |
| 685 | bool Changed = false; |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 686 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 687 | DL = DLP ? &DLP->getDataLayout() : 0; |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 688 | |
| 689 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 690 | if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) |
| 691 | Deferred.push_back(WeakVH(I)); |
| 692 | } |
| 693 | FnSet.resize(Deferred.size()); |
| 694 | |
| 695 | do { |
| 696 | std::vector<WeakVH> Worklist; |
| 697 | Deferred.swap(Worklist); |
| 698 | |
| 699 | DEBUG(dbgs() << "size of module: " << M.size() << '\n'); |
| 700 | DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); |
| 701 | |
| 702 | // Insert only strong functions and merge them. Strong function merging |
| 703 | // always deletes one of them. |
| 704 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), |
| 705 | E = Worklist.end(); I != E; ++I) { |
| 706 | if (!*I) continue; |
| 707 | Function *F = cast<Function>(*I); |
| 708 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() && |
| 709 | !F->mayBeOverridden()) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 710 | ComparableFunction CF = ComparableFunction(F, DL); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 711 | Changed |= insert(CF); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | // Insert only weak functions and merge them. By doing these second we |
| 716 | // create thunks to the strong function when possible. When two weak |
| 717 | // functions are identical, we create a new strong function with two weak |
| 718 | // weak thunks to it which are identical but not mergable. |
| 719 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), |
| 720 | E = Worklist.end(); I != E; ++I) { |
| 721 | if (!*I) continue; |
| 722 | Function *F = cast<Function>(*I); |
| 723 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() && |
| 724 | F->mayBeOverridden()) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 725 | ComparableFunction CF = ComparableFunction(F, DL); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 726 | Changed |= insert(CF); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | DEBUG(dbgs() << "size of FnSet: " << FnSet.size() << '\n'); |
| 730 | } while (!Deferred.empty()); |
| 731 | |
| 732 | FnSet.clear(); |
| 733 | |
| 734 | return Changed; |
| 735 | } |
| 736 | |
| 737 | bool DenseMapInfo<ComparableFunction>::isEqual(const ComparableFunction &LHS, |
| 738 | const ComparableFunction &RHS) { |
| 739 | if (LHS.getFunc() == RHS.getFunc() && |
| 740 | LHS.getHash() == RHS.getHash()) |
| 741 | return true; |
| 742 | if (!LHS.getFunc() || !RHS.getFunc()) |
| 743 | return false; |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 744 | |
| 745 | // One of these is a special "underlying pointer comparison only" object. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 746 | if (LHS.getDataLayout() == ComparableFunction::LookupOnly || |
| 747 | RHS.getDataLayout() == ComparableFunction::LookupOnly) |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 748 | return false; |
| 749 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 750 | assert(LHS.getDataLayout() == RHS.getDataLayout() && |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 751 | "Comparing functions for different targets"); |
| 752 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 753 | return FunctionComparator(LHS.getDataLayout(), LHS.getFunc(), |
Nick Lewycky | a46c898 | 2011-02-02 05:31:01 +0000 | [diff] [blame] | 754 | RHS.getFunc()).compare(); |
Nick Lewycky | 564fcca | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 757 | // Replace direct callers of Old with New. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 758 | void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { |
| 759 | Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 760 | for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) { |
| 761 | Use *U = &*UI; |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 762 | ++UI; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 763 | CallSite CS(U->getUser()); |
| 764 | if (CS && CS.isCallee(U)) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 765 | remove(CS.getInstruction()->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 766 | U->set(BitcastNew); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 771 | // Replace G with an alias to F if possible, or else a thunk to F. Deletes G. |
| 772 | void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) { |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 773 | if (HasGlobalAliases && G->hasUnnamedAddr()) { |
| 774 | if (G->hasExternalLinkage() || G->hasLocalLinkage() || |
| 775 | G->hasWeakLinkage()) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 776 | writeAlias(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 777 | return; |
| 778 | } |
| 779 | } |
| 780 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 781 | writeThunk(F, G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 784 | // Helper for writeThunk, |
| 785 | // Selects proper bitcast operation, |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 786 | // but a bit simpler then CastInst::getCastOpcode. |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 787 | static Value* createCast(IRBuilder<false> &Builder, Value *V, Type *DestTy) { |
| 788 | Type *SrcTy = V->getType(); |
| 789 | if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) |
| 790 | return Builder.CreateIntToPtr(V, DestTy); |
| 791 | else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) |
| 792 | return Builder.CreatePtrToInt(V, DestTy); |
| 793 | else |
| 794 | return Builder.CreateBitCast(V, DestTy); |
| 795 | } |
| 796 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 797 | // Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 798 | // of G with bitcast(F). Deletes G. |
| 799 | void MergeFunctions::writeThunk(Function *F, Function *G) { |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 800 | if (!G->mayBeOverridden()) { |
| 801 | // Redirect direct callers of G to F. |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 802 | replaceDirectCallers(G, F); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 805 | // 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] | 806 | // stop here and delete G. There's no need for a thunk. |
| 807 | if (G->hasLocalLinkage() && G->use_empty()) { |
| 808 | G->eraseFromParent(); |
| 809 | return; |
| 810 | } |
| 811 | |
Nick Lewycky | 25675ac | 2009-06-12 15:56:56 +0000 | [diff] [blame] | 812 | Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", |
| 813 | G->getParent()); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 814 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG); |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 815 | IRBuilder<false> Builder(BB); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 816 | |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 817 | SmallVector<Value *, 16> Args; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 818 | unsigned i = 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 819 | FunctionType *FFTy = F->getFunctionType(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 820 | for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end(); |
| 821 | AI != AE; ++AI) { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 822 | Args.push_back(createCast(Builder, (Value*)AI, FFTy->getParamType(i))); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 823 | ++i; |
| 824 | } |
| 825 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 826 | CallInst *CI = Builder.CreateCall(F, Args); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 827 | CI->setTailCall(); |
Nick Lewycky | d5bf51f | 2009-06-12 16:04:00 +0000 | [diff] [blame] | 828 | CI->setCallingConv(F->getCallingConv()); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 829 | if (NewG->getReturnType()->isVoidTy()) { |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 830 | Builder.CreateRetVoid(); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 831 | } else { |
Stepan Dyatkovskiy | dc2c4b4 | 2013-09-17 09:36:11 +0000 | [diff] [blame] | 832 | Builder.CreateRet(createCast(Builder, CI, NewG->getReturnType())); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | NewG->copyAttributesFrom(G); |
| 836 | NewG->takeName(G); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 837 | removeUsers(G); |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 838 | G->replaceAllUsesWith(NewG); |
| 839 | G->eraseFromParent(); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 840 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 841 | DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n'); |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 842 | ++NumThunksWritten; |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 845 | // Replace G with an alias to F and delete G. |
| 846 | void MergeFunctions::writeAlias(Function *F, Function *G) { |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 847 | Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); |
| 848 | GlobalAlias *GA = new GlobalAlias(G->getType(), G->getLinkage(), "", |
| 849 | BitcastF, G->getParent()); |
| 850 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 851 | GA->takeName(G); |
| 852 | GA->setVisibility(G->getVisibility()); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 853 | removeUsers(G); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 854 | G->replaceAllUsesWith(GA); |
| 855 | G->eraseFromParent(); |
| 856 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 857 | DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n'); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 858 | ++NumAliasesWritten; |
| 859 | } |
| 860 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 861 | // Merge two equivalent functions. Upon completion, Function G is deleted. |
| 862 | void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 863 | if (F->mayBeOverridden()) { |
| 864 | assert(G->mayBeOverridden()); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 865 | |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 866 | if (HasGlobalAliases) { |
| 867 | // Make them both thunks to the same internal function. |
| 868 | Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "", |
| 869 | F->getParent()); |
| 870 | H->copyAttributesFrom(F); |
| 871 | H->takeName(F); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 872 | removeUsers(F); |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 873 | F->replaceAllUsesWith(H); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 874 | |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 875 | unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment()); |
Nick Lewycky | f0067b6 | 2010-08-09 21:03:28 +0000 | [diff] [blame] | 876 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 877 | writeAlias(F, G); |
| 878 | writeAlias(F, H); |
Nick Lewycky | d3c6dfe | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 879 | |
Nick Lewycky | f1cec16 | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 880 | F->setAlignment(MaxAlignment); |
| 881 | F->setLinkage(GlobalValue::PrivateLinkage); |
| 882 | } else { |
| 883 | // We can't merge them. Instead, pick one and update all direct callers |
| 884 | // to call it and hope that we improve the instruction cache hit rate. |
| 885 | replaceDirectCallers(G, F); |
| 886 | } |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 887 | |
| 888 | ++NumDoubleWeak; |
Nick Lewycky | f216f69a | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 889 | } else { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 890 | writeThunkOrAlias(F, G); |
Nick Lewycky | 3c6d34a | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Nick Lewycky | e04dc22 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 893 | ++NumFunctionsMerged; |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 896 | // Insert a ComparableFunction into the FnSet, or merge it away if equal to one |
| 897 | // that was already inserted. |
| 898 | bool MergeFunctions::insert(ComparableFunction &NewF) { |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 899 | std::pair<FnSetType::iterator, bool> Result = FnSet.insert(NewF); |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 900 | if (Result.second) { |
| 901 | DEBUG(dbgs() << "Inserting as unique: " << NewF.getFunc()->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 902 | return false; |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 903 | } |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 904 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 905 | const ComparableFunction &OldF = *Result.first; |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 906 | |
Matt Arsenault | 517d84e | 2013-10-01 18:05:30 +0000 | [diff] [blame] | 907 | // Don't merge tiny functions, since it can just end up making the function |
| 908 | // larger. |
| 909 | // FIXME: Should still merge them if they are unnamed_addr and produce an |
| 910 | // alias. |
| 911 | if (NewF.getFunc()->size() == 1) { |
| 912 | if (NewF.getFunc()->front().size() <= 2) { |
| 913 | DEBUG(dbgs() << NewF.getFunc()->getName() |
| 914 | << " is to small to bother merging\n"); |
| 915 | return false; |
| 916 | } |
| 917 | } |
| 918 | |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 919 | // Never thunk a strong function to a weak function. |
Nick Lewycky | 71972d4 | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 920 | assert(!OldF.getFunc()->mayBeOverridden() || |
| 921 | NewF.getFunc()->mayBeOverridden()); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 922 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 923 | DEBUG(dbgs() << " " << OldF.getFunc()->getName() << " == " |
| 924 | << NewF.getFunc()->getName() << '\n'); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 925 | |
Nick Lewycky | f3a07ec | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 926 | Function *DeleteF = NewF.getFunc(); |
| 927 | NewF.release(); |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 928 | mergeTwoFunctions(OldF.getFunc(), DeleteF); |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 929 | return true; |
Nick Lewycky | fbd2757 | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 930 | } |
Nick Lewycky | d01d42e | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 931 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 932 | // Remove a function from FnSet. If it was already in FnSet, add it to Deferred |
| 933 | // so that we'll look at it in the next round. |
| 934 | void MergeFunctions::remove(Function *F) { |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 935 | // We need to make sure we remove F, not a function "equal" to F per the |
| 936 | // function equality comparator. |
| 937 | // |
| 938 | // The special "lookup only" ComparableFunction bypasses the expensive |
| 939 | // function comparison in favour of a pointer comparison on the underlying |
| 940 | // Function*'s. |
| 941 | ComparableFunction CF = ComparableFunction(F, ComparableFunction::LookupOnly); |
Nick Lewycky | 4e250c8 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 942 | if (FnSet.erase(CF)) { |
Nick Lewycky | 292e78c | 2011-02-09 06:32:02 +0000 | [diff] [blame] | 943 | DEBUG(dbgs() << "Removed " << F->getName() << " from set and deferred it.\n"); |
Nick Lewycky | 4e250c8 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 944 | Deferred.push_back(F); |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 945 | } |
Nick Lewycky | 4e250c8 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 946 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 947 | |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 948 | // For each instruction used by the value, remove() the function that contains |
| 949 | // the instruction. This should happen right before a call to RAUW. |
| 950 | void MergeFunctions::removeUsers(Value *V) { |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 951 | std::vector<Value *> Worklist; |
| 952 | Worklist.push_back(V); |
| 953 | while (!Worklist.empty()) { |
| 954 | Value *V = Worklist.back(); |
| 955 | Worklist.pop_back(); |
| 956 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 957 | for (User *U : V->users()) { |
| 958 | if (Instruction *I = dyn_cast<Instruction>(U)) { |
Nick Lewycky | cfb284c | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 959 | remove(I->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 960 | } else if (isa<GlobalValue>(U)) { |
Nick Lewycky | 540f953 | 2011-01-15 10:16:23 +0000 | [diff] [blame] | 961 | // do nothing |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 962 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
| 963 | for (User *UU : C->users()) |
| 964 | Worklist.push_back(UU); |
Nick Lewycky | 5361b84 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 965 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 966 | } |
Nick Lewycky | 0464d1d | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 967 | } |
Nick Lewycky | 0095937 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 968 | } |