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