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" |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/DenseSet.h" |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/FoldingSet.h" |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/SmallSet.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/STLExtras.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 53 | #include "llvm/Constants.h" |
| 54 | #include "llvm/InlineAsm.h" |
| 55 | #include "llvm/Instructions.h" |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 56 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 57 | #include "llvm/Module.h" |
| 58 | #include "llvm/Pass.h" |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 59 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 60 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 61 | #include "llvm/Support/ErrorHandling.h" |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 62 | #include "llvm/Support/IRBuilder.h" |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 63 | #include "llvm/Support/ValueHandle.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 64 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 65 | #include "llvm/Target/TargetData.h" |
Nick Lewycky | 65a0af3 | 2010-08-31 08:29:37 +0000 | [diff] [blame] | 66 | #include <vector> |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 67 | using namespace llvm; |
| 68 | |
| 69 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 70 | STATISTIC(NumThunksWritten, "Number of thunks generated"); |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 71 | STATISTIC(NumAliasesWritten, "Number of aliases generated"); |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 72 | STATISTIC(NumDoubleWeak, "Number of new functions created"); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 73 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 74 | /// Creates a hash-code for the function which is the same for any two |
| 75 | /// functions that will compare equal, without looking at the instructions |
| 76 | /// inside the function. |
| 77 | static unsigned profileFunction(const Function *F) { |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 78 | const FunctionType *FTy = F->getFunctionType(); |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 79 | |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 80 | FoldingSetNodeID ID; |
| 81 | ID.AddInteger(F->size()); |
| 82 | ID.AddInteger(F->getCallingConv()); |
| 83 | ID.AddBoolean(F->hasGC()); |
| 84 | ID.AddBoolean(FTy->isVarArg()); |
| 85 | ID.AddInteger(FTy->getReturnType()->getTypeID()); |
| 86 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 87 | ID.AddInteger(FTy->getParamType(i)->getTypeID()); |
| 88 | return ID.ComputeHash(); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 91 | namespace { |
| 92 | |
Nick Lewycky | 8b59643 | 2011-01-28 08:19:00 +0000 | [diff] [blame] | 93 | /// ComparableFunction - A struct that pairs together functions with a |
| 94 | /// TargetData so that we can keep them together as elements in the DenseSet. |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 95 | class ComparableFunction { |
| 96 | public: |
Nick Lewycky | b0e1777 | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 97 | static const ComparableFunction EmptyKey; |
| 98 | static const ComparableFunction TombstoneKey; |
| 99 | |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 100 | ComparableFunction(Function *Func, TargetData *TD) |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 101 | : Func(Func), Hash(profileFunction(Func)), TD(TD) {} |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 102 | |
Nick Lewycky | b0e1777 | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 103 | Function *getFunc() const { return Func; } |
| 104 | unsigned getHash() const { return Hash; } |
| 105 | TargetData *getTD() const { return TD; } |
| 106 | |
| 107 | // Drops AssertingVH reference to the function. Outside of debug mode, this |
| 108 | // does nothing. |
| 109 | void release() { |
| 110 | assert(Func && |
| 111 | "Attempted to release function twice, or release empty/tombstone!"); |
| 112 | Func = NULL; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | explicit ComparableFunction(unsigned Hash) |
| 117 | : Func(NULL), Hash(Hash), TD(NULL) {} |
| 118 | |
| 119 | AssertingVH<Function> Func; |
| 120 | unsigned Hash; |
| 121 | TargetData *TD; |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Nick Lewycky | b0e1777 | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 124 | const ComparableFunction ComparableFunction::EmptyKey = ComparableFunction(0); |
| 125 | const ComparableFunction ComparableFunction::TombstoneKey = |
| 126 | ComparableFunction(1); |
| 127 | |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 128 | } |
Nick Lewycky | b0e1777 | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 129 | |
| 130 | namespace llvm { |
| 131 | template <> |
| 132 | struct DenseMapInfo<ComparableFunction> { |
| 133 | static ComparableFunction getEmptyKey() { |
| 134 | return ComparableFunction::EmptyKey; |
| 135 | } |
| 136 | static ComparableFunction getTombstoneKey() { |
| 137 | return ComparableFunction::TombstoneKey; |
| 138 | } |
| 139 | static unsigned getHashValue(const ComparableFunction &CF) { |
| 140 | return CF.getHash(); |
| 141 | } |
| 142 | static bool isEqual(const ComparableFunction &LHS, |
| 143 | const ComparableFunction &RHS); |
| 144 | }; |
| 145 | } |
| 146 | |
| 147 | namespace { |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 148 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 149 | /// FunctionComparator - Compares two functions to determine whether or not |
| 150 | /// they will generate machine code with the same behaviour. TargetData is |
| 151 | /// used if available. The comparator always fails conservatively (erring on the |
| 152 | /// side of claiming that two functions are different). |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 153 | class FunctionComparator { |
| 154 | public: |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 155 | FunctionComparator(const TargetData *TD, const Function *F1, |
| 156 | const Function *F2) |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 157 | : F1(F1), F2(F2), TD(TD), IDMap1Count(0), IDMap2Count(0) {} |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 158 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 159 | /// Test whether the two functions have equivalent behaviour. |
| 160 | bool compare(); |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 161 | |
| 162 | private: |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 163 | /// Test whether two basic blocks have equivalent behaviour. |
| 164 | bool compare(const BasicBlock *BB1, const BasicBlock *BB2); |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 165 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 166 | /// Assign or look up previously assigned numbers for the two values, and |
| 167 | /// return whether the numbers are equal. Numbers are assigned in the order |
| 168 | /// visited. |
| 169 | bool enumerate(const Value *V1, const Value *V2); |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 170 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 171 | /// Compare two Instructions for equivalence, similar to |
| 172 | /// Instruction::isSameOperationAs but with modifications to the type |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 173 | /// comparison. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 174 | bool isEquivalentOperation(const Instruction *I1, |
| 175 | const Instruction *I2) const; |
| 176 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 177 | /// Compare two GEPs for equivalent pointer arithmetic. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 178 | bool isEquivalentGEP(const GEPOperator *GEP1, const GEPOperator *GEP2); |
| 179 | bool isEquivalentGEP(const GetElementPtrInst *GEP1, |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 180 | const GetElementPtrInst *GEP2) { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 181 | return isEquivalentGEP(cast<GEPOperator>(GEP1), cast<GEPOperator>(GEP2)); |
| 182 | } |
| 183 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 184 | /// Compare two Types, treating all pointer types as equal. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 185 | bool isEquivalentType(const Type *Ty1, const Type *Ty2) const; |
| 186 | |
| 187 | // The two functions undergoing comparison. |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 188 | const Function *F1, *F2; |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 189 | |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 190 | const TargetData *TD; |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 191 | |
| 192 | typedef DenseMap<const Value *, unsigned long> IDMap; |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 193 | IDMap Map1, Map2; |
| 194 | unsigned long IDMap1Count, IDMap2Count; |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 195 | }; |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 196 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 199 | // Any two pointers in the same address space are equivalent, intptr_t and |
| 200 | // pointers are equivalent. Otherwise, standard type equivalence rules apply. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 201 | bool FunctionComparator::isEquivalentType(const Type *Ty1, |
| 202 | const Type *Ty2) const { |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 203 | if (Ty1 == Ty2) |
| 204 | return true; |
Nick Lewycky | 207c193 | 2011-01-26 09:13:58 +0000 | [diff] [blame] | 205 | if (Ty1->getTypeID() != Ty2->getTypeID()) { |
| 206 | if (TD) { |
| 207 | LLVMContext &Ctx = Ty1->getContext(); |
| 208 | if (isa<PointerType>(Ty1) && Ty2 == TD->getIntPtrType(Ctx)) return true; |
| 209 | if (isa<PointerType>(Ty2) && Ty1 == TD->getIntPtrType(Ctx)) return true; |
| 210 | } |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 211 | return false; |
Nick Lewycky | 207c193 | 2011-01-26 09:13:58 +0000 | [diff] [blame] | 212 | } |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 213 | |
| 214 | switch(Ty1->getTypeID()) { |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 215 | default: |
| 216 | llvm_unreachable("Unknown type!"); |
Duncan Sands | 8246adc | 2010-07-07 07:48:00 +0000 | [diff] [blame] | 217 | // Fall through in Release mode. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 218 | case Type::IntegerTyID: |
| 219 | case Type::OpaqueTyID: |
Nick Lewycky | 388f491 | 2011-01-26 08:50:18 +0000 | [diff] [blame] | 220 | case Type::VectorTyID: |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 221 | // Ty1 == Ty2 would have returned true earlier. |
| 222 | return false; |
| 223 | |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 224 | case Type::VoidTyID: |
| 225 | case Type::FloatTyID: |
| 226 | case Type::DoubleTyID: |
| 227 | case Type::X86_FP80TyID: |
| 228 | case Type::FP128TyID: |
| 229 | case Type::PPC_FP128TyID: |
| 230 | case Type::LabelTyID: |
| 231 | case Type::MetadataTyID: |
| 232 | return true; |
| 233 | |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 234 | case Type::PointerTyID: { |
| 235 | const PointerType *PTy1 = cast<PointerType>(Ty1); |
| 236 | const PointerType *PTy2 = cast<PointerType>(Ty2); |
| 237 | return PTy1->getAddressSpace() == PTy2->getAddressSpace(); |
| 238 | } |
| 239 | |
| 240 | case Type::StructTyID: { |
| 241 | const StructType *STy1 = cast<StructType>(Ty1); |
| 242 | const StructType *STy2 = cast<StructType>(Ty2); |
| 243 | if (STy1->getNumElements() != STy2->getNumElements()) |
| 244 | return false; |
| 245 | |
| 246 | if (STy1->isPacked() != STy2->isPacked()) |
| 247 | return false; |
| 248 | |
| 249 | for (unsigned i = 0, e = STy1->getNumElements(); i != e; ++i) { |
| 250 | if (!isEquivalentType(STy1->getElementType(i), STy2->getElementType(i))) |
| 251 | return false; |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | case Type::FunctionTyID: { |
| 257 | const FunctionType *FTy1 = cast<FunctionType>(Ty1); |
| 258 | const FunctionType *FTy2 = cast<FunctionType>(Ty2); |
| 259 | if (FTy1->getNumParams() != FTy2->getNumParams() || |
| 260 | FTy1->isVarArg() != FTy2->isVarArg()) |
| 261 | return false; |
| 262 | |
| 263 | if (!isEquivalentType(FTy1->getReturnType(), FTy2->getReturnType())) |
| 264 | return false; |
| 265 | |
| 266 | for (unsigned i = 0, e = FTy1->getNumParams(); i != e; ++i) { |
| 267 | if (!isEquivalentType(FTy1->getParamType(i), FTy2->getParamType(i))) |
| 268 | return false; |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | |
Nick Lewycky | 394ce41 | 2010-07-16 06:31:12 +0000 | [diff] [blame] | 273 | case Type::ArrayTyID: { |
| 274 | const ArrayType *ATy1 = cast<ArrayType>(Ty1); |
| 275 | const ArrayType *ATy2 = cast<ArrayType>(Ty2); |
| 276 | return ATy1->getNumElements() == ATy2->getNumElements() && |
| 277 | isEquivalentType(ATy1->getElementType(), ATy2->getElementType()); |
| 278 | } |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 282 | // Determine whether the two operations are the same except that pointer-to-A |
| 283 | // and pointer-to-B are equivalent. This should be kept in sync with |
| 284 | // Instruction::isSameOperationAs. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 285 | bool FunctionComparator::isEquivalentOperation(const Instruction *I1, |
| 286 | const Instruction *I2) const { |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 287 | if (I1->getOpcode() != I2->getOpcode() || |
| 288 | I1->getNumOperands() != I2->getNumOperands() || |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 289 | !isEquivalentType(I1->getType(), I2->getType()) || |
| 290 | !I1->hasSameSubclassOptionalData(I2)) |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 291 | return false; |
| 292 | |
| 293 | // We have two instructions of identical opcode and #operands. Check to see |
| 294 | // if all operands are the same type |
| 295 | for (unsigned i = 0, e = I1->getNumOperands(); i != e; ++i) |
| 296 | if (!isEquivalentType(I1->getOperand(i)->getType(), |
| 297 | I2->getOperand(i)->getType())) |
| 298 | return false; |
| 299 | |
| 300 | // Check special state that is a part of some instructions. |
| 301 | if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) |
| 302 | return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && |
| 303 | LI->getAlignment() == cast<LoadInst>(I2)->getAlignment(); |
| 304 | if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) |
| 305 | return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && |
| 306 | SI->getAlignment() == cast<StoreInst>(I2)->getAlignment(); |
| 307 | if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) |
| 308 | return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); |
| 309 | if (const CallInst *CI = dyn_cast<CallInst>(I1)) |
| 310 | return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && |
| 311 | CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && |
Nick Lewycky | f6c63c2 | 2011-01-26 09:23:19 +0000 | [diff] [blame] | 312 | CI->getAttributes() == cast<CallInst>(I2)->getAttributes(); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 313 | if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) |
| 314 | return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && |
Nick Lewycky | f6c63c2 | 2011-01-26 09:23:19 +0000 | [diff] [blame] | 315 | CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes(); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 316 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) { |
| 317 | if (IVI->getNumIndices() != cast<InsertValueInst>(I2)->getNumIndices()) |
| 318 | return false; |
| 319 | for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i) |
| 320 | if (IVI->idx_begin()[i] != cast<InsertValueInst>(I2)->idx_begin()[i]) |
| 321 | return false; |
| 322 | return true; |
| 323 | } |
| 324 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) { |
| 325 | if (EVI->getNumIndices() != cast<ExtractValueInst>(I2)->getNumIndices()) |
| 326 | return false; |
| 327 | for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i) |
| 328 | if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I2)->idx_begin()[i]) |
| 329 | return false; |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | return true; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 336 | // Determine whether two GEP operations perform the same underlying arithmetic. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 337 | bool FunctionComparator::isEquivalentGEP(const GEPOperator *GEP1, |
| 338 | const GEPOperator *GEP2) { |
| 339 | // When we have target data, we can reduce the GEP down to the value in bytes |
| 340 | // added to the address. |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 341 | if (TD && GEP1->hasAllConstantIndices() && GEP2->hasAllConstantIndices()) { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 342 | SmallVector<Value *, 8> Indices1(GEP1->idx_begin(), GEP1->idx_end()); |
| 343 | SmallVector<Value *, 8> Indices2(GEP2->idx_begin(), GEP2->idx_end()); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 344 | uint64_t Offset1 = TD->getIndexedOffset(GEP1->getPointerOperandType(), |
| 345 | Indices1.data(), Indices1.size()); |
| 346 | uint64_t Offset2 = TD->getIndexedOffset(GEP2->getPointerOperandType(), |
| 347 | Indices2.data(), Indices2.size()); |
| 348 | return Offset1 == Offset2; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 351 | if (GEP1->getPointerOperand()->getType() != |
| 352 | GEP2->getPointerOperand()->getType()) |
| 353 | return false; |
| 354 | |
| 355 | if (GEP1->getNumOperands() != GEP2->getNumOperands()) |
| 356 | return false; |
| 357 | |
| 358 | for (unsigned i = 0, e = GEP1->getNumOperands(); i != e; ++i) { |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 359 | if (!enumerate(GEP1->getOperand(i), GEP2->getOperand(i))) |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
| 363 | return true; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 366 | // Compare two values used by the two functions under pair-wise comparison. If |
| 367 | // this is the first time the values are seen, they're added to the mapping so |
| 368 | // that we will detect mismatches on next use. |
| 369 | bool FunctionComparator::enumerate(const Value *V1, const Value *V2) { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 370 | // Check for function @f1 referring to itself and function @f2 referring to |
| 371 | // itself, or referring to each other, or both referring to either of them. |
| 372 | // They're all equivalent if the two functions are otherwise equivalent. |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 373 | if (V1 == F1 && V2 == F2) |
| 374 | return true; |
| 375 | if (V1 == F2 && V2 == F1) |
| 376 | return true; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 377 | |
Benjamin Kramer | 9c1858c | 2011-01-27 20:30:54 +0000 | [diff] [blame] | 378 | if (const Constant *C1 = dyn_cast<Constant>(V1)) { |
Nick Lewycky | 25296e2 | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 379 | if (V1 == V2) return true; |
Nick Lewycky | 25296e2 | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 380 | const Constant *C2 = dyn_cast<Constant>(V2); |
| 381 | if (!C2) return false; |
| 382 | // TODO: constant expressions with GEP or references to F1 or F2. |
| 383 | if (C1->isNullValue() && C2->isNullValue() && |
| 384 | isEquivalentType(C1->getType(), C2->getType())) |
| 385 | return true; |
Nick Lewycky | c9d6948 | 2011-01-27 19:51:31 +0000 | [diff] [blame] | 386 | // Try bitcasting C2 to C1's type. If the bitcast is legal and returns C1 |
| 387 | // then they must have equal bit patterns. |
Nick Lewycky | 25296e2 | 2011-01-27 08:38:19 +0000 | [diff] [blame] | 388 | return C1->getType()->canLosslesslyBitCastTo(C2->getType()) && |
| 389 | C1 == ConstantExpr::getBitCast(const_cast<Constant*>(C2), C1->getType()); |
| 390 | } |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 391 | |
| 392 | if (isa<InlineAsm>(V1) && isa<InlineAsm>(V2)) { |
| 393 | const InlineAsm *IA1 = cast<InlineAsm>(V1); |
| 394 | const InlineAsm *IA2 = cast<InlineAsm>(V2); |
| 395 | return IA1->getAsmString() == IA2->getAsmString() && |
| 396 | IA1->getConstraintString() == IA2->getConstraintString(); |
| 397 | } |
| 398 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 399 | unsigned long &ID1 = Map1[V1]; |
| 400 | if (!ID1) |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 401 | ID1 = ++IDMap1Count; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 402 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 403 | unsigned long &ID2 = Map2[V2]; |
| 404 | if (!ID2) |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 405 | ID2 = ++IDMap2Count; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 406 | |
| 407 | return ID1 == ID2; |
| 408 | } |
| 409 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 410 | // Test whether two basic blocks have equivalent behaviour. |
| 411 | bool FunctionComparator::compare(const BasicBlock *BB1, const BasicBlock *BB2) { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 412 | BasicBlock::const_iterator F1I = BB1->begin(), F1E = BB1->end(); |
| 413 | BasicBlock::const_iterator F2I = BB2->begin(), F2E = BB2->end(); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 414 | |
| 415 | do { |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 416 | if (!enumerate(F1I, F2I)) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 417 | return false; |
| 418 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 419 | if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(F1I)) { |
| 420 | const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(F2I); |
| 421 | if (!GEP2) |
| 422 | return false; |
Nick Lewycky | a142c93 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 423 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 424 | if (!enumerate(GEP1->getPointerOperand(), GEP2->getPointerOperand())) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 425 | return false; |
Nick Lewycky | a142c93 | 2009-06-13 19:09:52 +0000 | [diff] [blame] | 426 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 427 | if (!isEquivalentGEP(GEP1, GEP2)) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 428 | return false; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 429 | } else { |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 430 | if (!isEquivalentOperation(F1I, F2I)) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 431 | return false; |
| 432 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 433 | assert(F1I->getNumOperands() == F2I->getNumOperands()); |
| 434 | for (unsigned i = 0, e = F1I->getNumOperands(); i != e; ++i) { |
| 435 | Value *OpF1 = F1I->getOperand(i); |
| 436 | Value *OpF2 = F2I->getOperand(i); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 437 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 438 | if (!enumerate(OpF1, OpF2)) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 439 | return false; |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 440 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 441 | if (OpF1->getValueID() != OpF2->getValueID() || |
| 442 | !isEquivalentType(OpF1->getType(), OpF2->getType())) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 443 | return false; |
| 444 | } |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 447 | ++F1I, ++F2I; |
| 448 | } while (F1I != F1E && F2I != F2E); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 449 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 450 | return F1I == F1E && F2I == F2E; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 453 | // Test whether the two functions have equivalent behaviour. |
| 454 | bool FunctionComparator::compare() { |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 455 | // We need to recheck everything, but check the things that weren't included |
| 456 | // in the hash first. |
| 457 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 458 | if (F1->getAttributes() != F2->getAttributes()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 459 | return false; |
| 460 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 461 | if (F1->hasGC() != F2->hasGC()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 462 | return false; |
| 463 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 464 | if (F1->hasGC() && F1->getGC() != F2->getGC()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 465 | return false; |
| 466 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 467 | if (F1->hasSection() != F2->hasSection()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 468 | return false; |
| 469 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 470 | if (F1->hasSection() && F1->getSection() != F2->getSection()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 471 | return false; |
| 472 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 473 | if (F1->isVarArg() != F2->isVarArg()) |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 474 | return false; |
| 475 | |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 476 | // TODO: if it's internal and only used in direct calls, we could handle this |
| 477 | // case too. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 478 | if (F1->getCallingConv() != F2->getCallingConv()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 479 | return false; |
| 480 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 481 | if (!isEquivalentType(F1->getFunctionType(), F2->getFunctionType())) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 482 | return false; |
| 483 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 484 | assert(F1->arg_size() == F2->arg_size() && |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 485 | "Identically typed functions have different numbers of args!"); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 486 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 487 | // Visit the arguments so that they get enumerated in the order they're |
| 488 | // passed in. |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 489 | for (Function::const_arg_iterator f1i = F1->arg_begin(), |
| 490 | f2i = F2->arg_begin(), f1e = F1->arg_end(); f1i != f1e; ++f1i, ++f2i) { |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 491 | if (!enumerate(f1i, f2i)) |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 492 | llvm_unreachable("Arguments repeat!"); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 495 | // We do a CFG-ordered walk since the actual ordering of the blocks in the |
| 496 | // 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] | 497 | // functions, then takes each block from each terminator in order. As an |
| 498 | // artifact, this also means that unreachable blocks are ignored. |
| 499 | SmallVector<const BasicBlock *, 8> F1BBs, F2BBs; |
| 500 | SmallSet<const BasicBlock *, 128> VisitedBBs; // in terms of F1. |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 501 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 502 | F1BBs.push_back(&F1->getEntryBlock()); |
| 503 | F2BBs.push_back(&F2->getEntryBlock()); |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 504 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 505 | VisitedBBs.insert(F1BBs[0]); |
| 506 | while (!F1BBs.empty()) { |
| 507 | const BasicBlock *F1BB = F1BBs.pop_back_val(); |
| 508 | const BasicBlock *F2BB = F2BBs.pop_back_val(); |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 509 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 510 | if (!enumerate(F1BB, F2BB) || !compare(F1BB, F2BB)) |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 511 | return false; |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 512 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 513 | const TerminatorInst *F1TI = F1BB->getTerminator(); |
| 514 | const TerminatorInst *F2TI = F2BB->getTerminator(); |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 515 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 516 | assert(F1TI->getNumSuccessors() == F2TI->getNumSuccessors()); |
| 517 | for (unsigned i = 0, e = F1TI->getNumSuccessors(); i != e; ++i) { |
| 518 | if (!VisitedBBs.insert(F1TI->getSuccessor(i))) |
Nick Lewycky | 911ae39 | 2010-05-13 06:45:13 +0000 | [diff] [blame] | 519 | continue; |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 520 | |
Nick Lewycky | 78d4330 | 2010-08-02 05:23:03 +0000 | [diff] [blame] | 521 | F1BBs.push_back(F1TI->getSuccessor(i)); |
| 522 | F2BBs.push_back(F2TI->getSuccessor(i)); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 525 | return true; |
| 526 | } |
| 527 | |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 528 | namespace { |
| 529 | |
| 530 | /// MergeFunctions finds functions which will generate identical machine code, |
| 531 | /// by considering all pointer types to be equivalent. Once identified, |
| 532 | /// MergeFunctions will fold them by replacing a call to one to a call to a |
| 533 | /// bitcast of the other. |
| 534 | /// |
| 535 | class MergeFunctions : public ModulePass { |
| 536 | public: |
| 537 | static char ID; |
| 538 | MergeFunctions() |
| 539 | : ModulePass(ID), HasGlobalAliases(false) { |
| 540 | initializeMergeFunctionsPass(*PassRegistry::getPassRegistry()); |
| 541 | } |
| 542 | |
| 543 | bool runOnModule(Module &M); |
| 544 | |
| 545 | private: |
| 546 | typedef DenseSet<ComparableFunction> FnSetType; |
| 547 | |
| 548 | /// A work queue of functions that may have been modified and should be |
| 549 | /// analyzed again. |
| 550 | std::vector<WeakVH> Deferred; |
| 551 | |
| 552 | /// Insert a ComparableFunction into the FnSet, or merge it away if it's |
| 553 | /// equal to one that's already present. |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 554 | bool insert(ComparableFunction &NewF); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 555 | |
| 556 | /// Remove a Function from the FnSet and queue it up for a second sweep of |
| 557 | /// analysis. |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 558 | void remove(Function *F); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 559 | |
| 560 | /// Find the functions that use this Value and remove them from FnSet and |
| 561 | /// queue the functions. |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 562 | void removeUsers(Value *V); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 563 | |
| 564 | /// Replace all direct calls of Old with calls of New. Will bitcast New if |
| 565 | /// necessary to make types match. |
| 566 | void replaceDirectCallers(Function *Old, Function *New); |
| 567 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 568 | /// Merge two equivalent functions. Upon completion, G may be deleted, or may |
| 569 | /// be converted into a thunk. In either case, it should never be visited |
| 570 | /// again. |
| 571 | void mergeTwoFunctions(Function *F, Function *G); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 572 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 573 | /// Replace G with a thunk or an alias to F. Deletes G. |
| 574 | void writeThunkOrAlias(Function *F, Function *G); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 575 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 576 | /// Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 577 | /// of G with bitcast(F). Deletes G. |
| 578 | void writeThunk(Function *F, Function *G); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 579 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 580 | /// Replace G with an alias to F. Deletes G. |
| 581 | void writeAlias(Function *F, Function *G); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 582 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 583 | /// The set of all distinct functions. Use the insert() and remove() methods |
| 584 | /// to modify it. |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 585 | FnSetType FnSet; |
| 586 | |
| 587 | /// TargetData for more accurate GEP comparisons. May be NULL. |
| 588 | TargetData *TD; |
| 589 | |
| 590 | /// Whether or not the target supports global aliases. |
| 591 | bool HasGlobalAliases; |
| 592 | }; |
| 593 | |
| 594 | } // end anonymous namespace |
| 595 | |
| 596 | char MergeFunctions::ID = 0; |
| 597 | INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false) |
| 598 | |
| 599 | ModulePass *llvm::createMergeFunctionsPass() { |
| 600 | return new MergeFunctions(); |
| 601 | } |
| 602 | |
| 603 | bool MergeFunctions::runOnModule(Module &M) { |
| 604 | bool Changed = false; |
| 605 | TD = getAnalysisIfAvailable<TargetData>(); |
| 606 | |
| 607 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 608 | if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) |
| 609 | Deferred.push_back(WeakVH(I)); |
| 610 | } |
| 611 | FnSet.resize(Deferred.size()); |
| 612 | |
| 613 | do { |
| 614 | std::vector<WeakVH> Worklist; |
| 615 | Deferred.swap(Worklist); |
| 616 | |
| 617 | DEBUG(dbgs() << "size of module: " << M.size() << '\n'); |
| 618 | DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); |
| 619 | |
| 620 | // Insert only strong functions and merge them. Strong function merging |
| 621 | // always deletes one of them. |
| 622 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), |
| 623 | E = Worklist.end(); I != E; ++I) { |
| 624 | if (!*I) continue; |
| 625 | Function *F = cast<Function>(*I); |
| 626 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() && |
| 627 | !F->mayBeOverridden()) { |
| 628 | ComparableFunction CF = ComparableFunction(F, TD); |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 629 | Changed |= insert(CF); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | // Insert only weak functions and merge them. By doing these second we |
| 634 | // create thunks to the strong function when possible. When two weak |
| 635 | // functions are identical, we create a new strong function with two weak |
| 636 | // weak thunks to it which are identical but not mergable. |
| 637 | for (std::vector<WeakVH>::iterator I = Worklist.begin(), |
| 638 | E = Worklist.end(); I != E; ++I) { |
| 639 | if (!*I) continue; |
| 640 | Function *F = cast<Function>(*I); |
| 641 | if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() && |
| 642 | F->mayBeOverridden()) { |
| 643 | ComparableFunction CF = ComparableFunction(F, TD); |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 644 | Changed |= insert(CF); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | DEBUG(dbgs() << "size of FnSet: " << FnSet.size() << '\n'); |
| 648 | } while (!Deferred.empty()); |
| 649 | |
| 650 | FnSet.clear(); |
| 651 | |
| 652 | return Changed; |
| 653 | } |
| 654 | |
| 655 | bool DenseMapInfo<ComparableFunction>::isEqual(const ComparableFunction &LHS, |
| 656 | const ComparableFunction &RHS) { |
| 657 | if (LHS.getFunc() == RHS.getFunc() && |
| 658 | LHS.getHash() == RHS.getHash()) |
| 659 | return true; |
| 660 | if (!LHS.getFunc() || !RHS.getFunc()) |
| 661 | return false; |
| 662 | assert(LHS.getTD() == RHS.getTD() && |
| 663 | "Comparing functions for different targets"); |
| 664 | |
Nick Lewycky | 8eb3e54 | 2011-02-02 05:31:01 +0000 | [diff] [blame] | 665 | return FunctionComparator(LHS.getTD(), LHS.getFunc(), |
| 666 | RHS.getFunc()).compare(); |
Nick Lewycky | 285cf80 | 2011-01-28 07:36:21 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 669 | // Replace direct callers of Old with New. |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 670 | void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { |
| 671 | Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); |
| 672 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 673 | UI != UE;) { |
| 674 | Value::use_iterator TheIter = UI; |
| 675 | ++UI; |
| 676 | CallSite CS(*TheIter); |
| 677 | if (CS && CS.isCallee(TheIter)) { |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 678 | remove(CS.getInstruction()->getParent()->getParent()); |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 679 | TheIter.getUse().set(BitcastNew); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 684 | // Replace G with an alias to F if possible, or else a thunk to F. Deletes G. |
| 685 | void MergeFunctions::writeThunkOrAlias(Function *F, Function *G) { |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 686 | if (HasGlobalAliases && G->hasUnnamedAddr()) { |
| 687 | if (G->hasExternalLinkage() || G->hasLocalLinkage() || |
| 688 | G->hasWeakLinkage()) { |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 689 | writeAlias(F, G); |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | } |
| 693 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 694 | writeThunk(F, G); |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 697 | // Replace G with a simple tail call to bitcast(F). Also replace direct uses |
| 698 | // of G with bitcast(F). Deletes G. |
| 699 | void MergeFunctions::writeThunk(Function *F, Function *G) { |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 700 | if (!G->mayBeOverridden()) { |
| 701 | // Redirect direct callers of G to F. |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 702 | replaceDirectCallers(G, F); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 705 | // 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] | 706 | // stop here and delete G. There's no need for a thunk. |
| 707 | if (G->hasLocalLinkage() && G->use_empty()) { |
| 708 | G->eraseFromParent(); |
| 709 | return; |
| 710 | } |
| 711 | |
Nick Lewycky | 8728d7a | 2009-06-12 15:56:56 +0000 | [diff] [blame] | 712 | Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", |
| 713 | G->getParent()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 714 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG); |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 715 | IRBuilder<false> Builder(BB); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 716 | |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 717 | SmallVector<Value *, 16> Args; |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 718 | unsigned i = 0; |
| 719 | const FunctionType *FFTy = F->getFunctionType(); |
| 720 | for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end(); |
| 721 | AI != AE; ++AI) { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 722 | Args.push_back(Builder.CreateBitCast(AI, FFTy->getParamType(i))); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 723 | ++i; |
| 724 | } |
| 725 | |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 726 | CallInst *CI = Builder.CreateCall(F, Args.begin(), Args.end()); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 727 | CI->setTailCall(); |
Nick Lewycky | b3c36c9 | 2009-06-12 16:04:00 +0000 | [diff] [blame] | 728 | CI->setCallingConv(F->getCallingConv()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 729 | if (NewG->getReturnType()->isVoidTy()) { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 730 | Builder.CreateRetVoid(); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 731 | } else { |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 732 | Builder.CreateRet(Builder.CreateBitCast(CI, NewG->getReturnType())); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | NewG->copyAttributesFrom(G); |
| 736 | NewG->takeName(G); |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 737 | removeUsers(G); |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 738 | G->replaceAllUsesWith(NewG); |
| 739 | G->eraseFromParent(); |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 740 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 741 | DEBUG(dbgs() << "writeThunk: " << NewG->getName() << '\n'); |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 742 | ++NumThunksWritten; |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 745 | // Replace G with an alias to F and delete G. |
| 746 | void MergeFunctions::writeAlias(Function *F, Function *G) { |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 747 | Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); |
| 748 | GlobalAlias *GA = new GlobalAlias(G->getType(), G->getLinkage(), "", |
| 749 | BitcastF, G->getParent()); |
| 750 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 751 | GA->takeName(G); |
| 752 | GA->setVisibility(G->getVisibility()); |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 753 | removeUsers(G); |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 754 | G->replaceAllUsesWith(GA); |
| 755 | G->eraseFromParent(); |
| 756 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 757 | DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n'); |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 758 | ++NumAliasesWritten; |
| 759 | } |
| 760 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 761 | // Merge two equivalent functions. Upon completion, Function G is deleted. |
| 762 | void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 763 | if (F->mayBeOverridden()) { |
| 764 | assert(G->mayBeOverridden()); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 765 | |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 766 | if (HasGlobalAliases) { |
| 767 | // Make them both thunks to the same internal function. |
| 768 | Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "", |
| 769 | F->getParent()); |
| 770 | H->copyAttributesFrom(F); |
| 771 | H->takeName(F); |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 772 | removeUsers(F); |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 773 | F->replaceAllUsesWith(H); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 774 | |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 775 | unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment()); |
Nick Lewycky | 3221834 | 2010-08-09 21:03:28 +0000 | [diff] [blame] | 776 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 777 | writeAlias(F, G); |
| 778 | writeAlias(F, H); |
Nick Lewycky | 33ab0b1 | 2010-05-13 05:48:45 +0000 | [diff] [blame] | 779 | |
Nick Lewycky | b38824f | 2011-01-25 08:56:50 +0000 | [diff] [blame] | 780 | F->setAlignment(MaxAlignment); |
| 781 | F->setLinkage(GlobalValue::PrivateLinkage); |
| 782 | } else { |
| 783 | // We can't merge them. Instead, pick one and update all direct callers |
| 784 | // to call it and hope that we improve the instruction cache hit rate. |
| 785 | replaceDirectCallers(G, F); |
| 786 | } |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 787 | |
| 788 | ++NumDoubleWeak; |
Nick Lewycky | c9dcbed | 2010-08-06 07:21:30 +0000 | [diff] [blame] | 789 | } else { |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 790 | writeThunkOrAlias(F, G); |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Nick Lewycky | 287de60 | 2009-06-12 08:04:51 +0000 | [diff] [blame] | 793 | ++NumFunctionsMerged; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 796 | // Insert a ComparableFunction into the FnSet, or merge it away if equal to one |
| 797 | // that was already inserted. |
| 798 | bool MergeFunctions::insert(ComparableFunction &NewF) { |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 799 | std::pair<FnSetType::iterator, bool> Result = FnSet.insert(NewF); |
| 800 | if (Result.second) |
| 801 | return false; |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 802 | |
Nick Lewycky | b0e1777 | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 803 | const ComparableFunction &OldF = *Result.first; |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 804 | |
| 805 | // Never thunk a strong function to a weak function. |
Nick Lewycky | 2b6c01b | 2010-09-07 01:42:10 +0000 | [diff] [blame] | 806 | assert(!OldF.getFunc()->mayBeOverridden() || |
| 807 | NewF.getFunc()->mayBeOverridden()); |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 808 | |
Nick Lewycky | b0e1777 | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 809 | DEBUG(dbgs() << " " << OldF.getFunc()->getName() << " == " |
| 810 | << NewF.getFunc()->getName() << '\n'); |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 811 | |
Nick Lewycky | b0e1777 | 2010-09-05 09:00:32 +0000 | [diff] [blame] | 812 | Function *DeleteF = NewF.getFunc(); |
| 813 | NewF.release(); |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 814 | mergeTwoFunctions(OldF.getFunc(), DeleteF); |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 815 | return true; |
Nick Lewycky | be04fde | 2010-08-08 05:04:23 +0000 | [diff] [blame] | 816 | } |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 817 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 818 | // Remove a function from FnSet. If it was already in FnSet, add it to Deferred |
| 819 | // so that we'll look at it in the next round. |
| 820 | void MergeFunctions::remove(Function *F) { |
Nick Lewycky | abd6c75 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 821 | ComparableFunction CF = ComparableFunction(F, TD); |
| 822 | if (FnSet.erase(CF)) { |
| 823 | Deferred.push_back(F); |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 824 | } |
Nick Lewycky | abd6c75 | 2011-01-02 02:46:33 +0000 | [diff] [blame] | 825 | } |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 826 | |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 827 | // For each instruction used by the value, remove() the function that contains |
| 828 | // the instruction. This should happen right before a call to RAUW. |
| 829 | void MergeFunctions::removeUsers(Value *V) { |
Nick Lewycky | d081b04 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 830 | std::vector<Value *> Worklist; |
| 831 | Worklist.push_back(V); |
| 832 | while (!Worklist.empty()) { |
| 833 | Value *V = Worklist.back(); |
| 834 | Worklist.pop_back(); |
| 835 | |
| 836 | for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); |
| 837 | UI != UE; ++UI) { |
| 838 | Use &U = UI.getUse(); |
| 839 | if (Instruction *I = dyn_cast<Instruction>(U.getUser())) { |
Nick Lewycky | 468ee0a | 2011-01-28 08:43:14 +0000 | [diff] [blame] | 840 | remove(I->getParent()->getParent()); |
Nick Lewycky | d081b04 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 841 | } else if (isa<GlobalValue>(U.getUser())) { |
Nick Lewycky | e8f8139 | 2011-01-15 10:16:23 +0000 | [diff] [blame] | 842 | // do nothing |
Nick Lewycky | d081b04 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 843 | } else if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Nick Lewycky | e8f8139 | 2011-01-15 10:16:23 +0000 | [diff] [blame] | 844 | for (Value::use_iterator CUI = C->use_begin(), CUE = C->use_end(); |
| 845 | CUI != CUE; ++CUI) |
Nick Lewycky | d081b04 | 2011-01-02 19:16:44 +0000 | [diff] [blame] | 846 | Worklist.push_back(*CUI); |
| 847 | } |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 848 | } |
Nick Lewycky | f53de86 | 2010-08-31 05:53:05 +0000 | [diff] [blame] | 849 | } |
Nick Lewycky | b0104e1 | 2010-09-05 08:22:49 +0000 | [diff] [blame] | 850 | } |