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 | // |
| 12 | // A Function will not be analyzed if: |
| 13 | // * it is overridable at runtime (except for weak linkage), or |
| 14 | // * it is used by anything other than the callee parameter of a call/invoke |
| 15 | // |
| 16 | // A hash is computed from the function, based on its type and number of |
| 17 | // basic blocks. |
| 18 | // |
| 19 | // Once all hashes are computed, we perform an expensive equality comparison |
| 20 | // on each function pair. This takes n^2/2 comparisons per bucket, so it's |
| 21 | // important that the hash function be high quality. The equality comparison |
| 22 | // iterates through each instruction in each basic block. |
| 23 | // |
| 24 | // When a match is found, the functions are folded. We can only fold two |
| 25 | // functions when we know that the definition of one of them is not |
| 26 | // overridable. |
| 27 | // * fold a function marked internal by replacing all of its users. |
| 28 | // * fold extern or weak functions by replacing them with a global alias |
| 29 | // |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // |
| 32 | // Future work: |
| 33 | // |
| 34 | // * fold vector<T*>::push_back and vector<S*>::push_back. |
| 35 | // |
| 36 | // These two functions have different types, but in a way that doesn't matter |
| 37 | // to us. As long as we never see an S or T itself, using S* and S** is the |
| 38 | // same as using a T* and T**. |
| 39 | // |
| 40 | // * virtual functions. |
| 41 | // |
| 42 | // Many functions have their address taken by the virtual function table for |
| 43 | // the object they belong to. However, as long as it's only used for a lookup |
| 44 | // and call, this is irrelevant, and we'd like to fold such implementations. |
| 45 | // |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | #define DEBUG_TYPE "mergefunc" |
| 49 | #include "llvm/Transforms/IPO.h" |
| 50 | #include "llvm/ADT/DenseMap.h" |
| 51 | #include "llvm/ADT/Statistic.h" |
| 52 | #include "llvm/Constants.h" |
| 53 | #include "llvm/InlineAsm.h" |
| 54 | #include "llvm/Instructions.h" |
| 55 | #include "llvm/Module.h" |
| 56 | #include "llvm/Pass.h" |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 57 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 58 | #include "llvm/Support/Compiler.h" |
| 59 | #include "llvm/Support/Debug.h" |
| 60 | #include <map> |
| 61 | #include <vector> |
| 62 | using namespace llvm; |
| 63 | |
| 64 | STATISTIC(NumFunctionsMerged, "Number of functions merged"); |
| 65 | STATISTIC(NumMergeFails, "Number of identical function pairings not merged"); |
| 66 | |
| 67 | namespace { |
| 68 | struct VISIBILITY_HIDDEN MergeFunctions : public ModulePass { |
| 69 | static char ID; // Pass identification, replacement for typeid |
| 70 | MergeFunctions() : ModulePass((intptr_t)&ID) {} |
| 71 | |
| 72 | bool runOnModule(Module &M); |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | char MergeFunctions::ID = 0; |
| 77 | static RegisterPass<MergeFunctions> |
| 78 | X("mergefunc", "Merge Functions"); |
| 79 | |
| 80 | ModulePass *llvm::createMergeFunctionsPass() { |
| 81 | return new MergeFunctions(); |
| 82 | } |
| 83 | |
Duncan Sands | 5baf8ec | 2008-11-02 09:00:33 +0000 | [diff] [blame] | 84 | static unsigned long hash(const Function *F) { |
| 85 | return F->size() ^ reinterpret_cast<unsigned long>(F->getType()); |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 86 | //return F->size() ^ F->arg_size() ^ F->getReturnType(); |
| 87 | } |
| 88 | |
| 89 | static bool compare(const Value *V, const Value *U) { |
| 90 | assert(!isa<BasicBlock>(V) && !isa<BasicBlock>(U) && |
| 91 | "Must not compare basic blocks."); |
| 92 | |
| 93 | assert(V->getType() == U->getType() && |
| 94 | "Two of the same operation have operands of different type."); |
| 95 | |
| 96 | // TODO: If the constant is an expression of F, we should accept that it's |
| 97 | // equal to the same expression in terms of G. |
| 98 | if (isa<Constant>(V)) |
| 99 | return V == U; |
| 100 | |
| 101 | // The caller has ensured that ValueMap[V] != U. Since Arguments are |
| 102 | // pre-loaded into the ValueMap, and Instructions are added as we go, we know |
| 103 | // that this can only be a mis-match. |
| 104 | if (isa<Instruction>(V) || isa<Argument>(V)) |
| 105 | return false; |
| 106 | |
| 107 | if (isa<InlineAsm>(V) && isa<InlineAsm>(U)) { |
| 108 | const InlineAsm *IAF = cast<InlineAsm>(V); |
| 109 | const InlineAsm *IAG = cast<InlineAsm>(U); |
| 110 | return IAF->getAsmString() == IAG->getAsmString() && |
| 111 | IAF->getConstraintString() == IAG->getConstraintString(); |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | static bool equals(const BasicBlock *BB1, const BasicBlock *BB2, |
| 118 | DenseMap<const Value *, const Value *> &ValueMap, |
| 119 | DenseMap<const Value *, const Value *> &SpeculationMap) { |
| 120 | // Specutively add it anyways. If it's false, we'll notice a difference later, and |
| 121 | // this won't matter. |
| 122 | ValueMap[BB1] = BB2; |
| 123 | |
| 124 | BasicBlock::const_iterator FI = BB1->begin(), FE = BB1->end(); |
| 125 | BasicBlock::const_iterator GI = BB2->begin(), GE = BB2->end(); |
| 126 | |
| 127 | do { |
| 128 | if (!FI->isSameOperationAs(const_cast<Instruction *>(&*GI))) |
| 129 | return false; |
| 130 | |
| 131 | if (FI->getNumOperands() != GI->getNumOperands()) |
| 132 | return false; |
| 133 | |
| 134 | if (ValueMap[FI] == GI) { |
| 135 | ++FI, ++GI; |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | if (ValueMap[FI] != NULL) |
| 140 | return false; |
| 141 | |
| 142 | for (unsigned i = 0, e = FI->getNumOperands(); i != e; ++i) { |
| 143 | Value *OpF = FI->getOperand(i); |
| 144 | Value *OpG = GI->getOperand(i); |
| 145 | |
| 146 | if (ValueMap[OpF] == OpG) |
| 147 | continue; |
| 148 | |
| 149 | if (ValueMap[OpF] != NULL) |
| 150 | return false; |
| 151 | |
| 152 | assert(OpF->getType() == OpG->getType() && |
| 153 | "Two of the same operation has operands of different type."); |
| 154 | |
| 155 | if (OpF->getValueID() != OpG->getValueID()) |
| 156 | return false; |
| 157 | |
| 158 | if (isa<PHINode>(FI)) { |
| 159 | if (SpeculationMap[OpF] == NULL) |
| 160 | SpeculationMap[OpF] = OpG; |
| 161 | else if (SpeculationMap[OpF] != OpG) |
| 162 | return false; |
| 163 | continue; |
| 164 | } else if (isa<BasicBlock>(OpF)) { |
| 165 | assert(isa<TerminatorInst>(FI) && |
| 166 | "BasicBlock referenced by non-Terminator non-PHI"); |
| 167 | // This call changes the ValueMap, hence we can't use |
| 168 | // Value *& = ValueMap[...] |
| 169 | if (!equals(cast<BasicBlock>(OpF), cast<BasicBlock>(OpG), ValueMap, |
| 170 | SpeculationMap)) |
| 171 | return false; |
| 172 | } else { |
| 173 | if (!compare(OpF, OpG)) |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | ValueMap[OpF] = OpG; |
| 178 | } |
| 179 | |
| 180 | ValueMap[FI] = GI; |
| 181 | ++FI, ++GI; |
| 182 | } while (FI != FE && GI != GE); |
| 183 | |
| 184 | return FI == FE && GI == GE; |
| 185 | } |
| 186 | |
| 187 | static bool equals(const Function *F, const Function *G) { |
| 188 | // We need to recheck everything, but check the things that weren't included |
| 189 | // in the hash first. |
| 190 | |
| 191 | if (F->getAttributes() != G->getAttributes()) |
| 192 | return false; |
| 193 | |
| 194 | if (F->hasGC() != G->hasGC()) |
| 195 | return false; |
| 196 | |
| 197 | if (F->hasGC() && F->getGC() != G->getGC()) |
| 198 | return false; |
| 199 | |
| 200 | if (F->hasSection() != G->hasSection()) |
| 201 | return false; |
| 202 | |
| 203 | if (F->hasSection() && F->getSection() != G->getSection()) |
| 204 | return false; |
| 205 | |
| 206 | // TODO: if it's internal and only used in direct calls, we could handle this |
| 207 | // case too. |
| 208 | if (F->getCallingConv() != G->getCallingConv()) |
| 209 | return false; |
| 210 | |
| 211 | // TODO: We want to permit cases where two functions take T* and S* but |
| 212 | // only load or store them into T** and S**. |
| 213 | if (F->getType() != G->getType()) |
| 214 | return false; |
| 215 | |
| 216 | DenseMap<const Value *, const Value *> ValueMap; |
| 217 | DenseMap<const Value *, const Value *> SpeculationMap; |
| 218 | ValueMap[F] = G; |
| 219 | |
| 220 | assert(F->arg_size() == G->arg_size() && |
| 221 | "Identical functions have a different number of args."); |
| 222 | |
| 223 | for (Function::const_arg_iterator fi = F->arg_begin(), gi = G->arg_begin(), |
| 224 | fe = F->arg_end(); fi != fe; ++fi, ++gi) |
| 225 | ValueMap[fi] = gi; |
| 226 | |
| 227 | if (!equals(&F->getEntryBlock(), &G->getEntryBlock(), ValueMap, |
| 228 | SpeculationMap)) |
| 229 | return false; |
| 230 | |
| 231 | for (DenseMap<const Value *, const Value *>::iterator |
| 232 | I = SpeculationMap.begin(), E = SpeculationMap.end(); I != E; ++I) { |
| 233 | if (ValueMap[I->first] != I->second) |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | static bool fold(std::vector<Function *> &FnVec, unsigned i, unsigned j) { |
| 241 | if (FnVec[i]->mayBeOverridden() && !FnVec[j]->mayBeOverridden()) |
| 242 | std::swap(FnVec[i], FnVec[j]); |
| 243 | |
| 244 | Function *F = FnVec[i]; |
| 245 | Function *G = FnVec[j]; |
| 246 | |
| 247 | if (!F->mayBeOverridden()) { |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 248 | if (G->hasLocalLinkage()) { |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 249 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 250 | G->replaceAllUsesWith(F); |
| 251 | G->eraseFromParent(); |
| 252 | ++NumFunctionsMerged; |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | if (G->hasExternalLinkage() || G->hasWeakLinkage()) { |
| 257 | GlobalAlias *GA = new GlobalAlias(G->getType(), G->getLinkage(), "", |
| 258 | F, G->getParent()); |
| 259 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 260 | GA->takeName(G); |
| 261 | GA->setVisibility(G->getVisibility()); |
| 262 | G->replaceAllUsesWith(GA); |
| 263 | G->eraseFromParent(); |
| 264 | ++NumFunctionsMerged; |
| 265 | return true; |
| 266 | } |
| 267 | } |
| 268 | |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 269 | if (F->hasWeakLinkage() && G->hasWeakLinkage()) { |
| 270 | GlobalAlias *GA_F = new GlobalAlias(F->getType(), F->getLinkage(), "", |
| 271 | 0, F->getParent()); |
| 272 | GA_F->takeName(F); |
| 273 | GA_F->setVisibility(F->getVisibility()); |
| 274 | F->setAlignment(std::max(F->getAlignment(), G->getAlignment())); |
| 275 | F->replaceAllUsesWith(GA_F); |
| 276 | F->setName("folded." + GA_F->getName()); |
| 277 | F->setLinkage(GlobalValue::ExternalLinkage); |
| 278 | GA_F->setAliasee(F); |
| 279 | |
| 280 | GlobalAlias *GA_G = new GlobalAlias(G->getType(), G->getLinkage(), "", |
| 281 | F, G->getParent()); |
| 282 | GA_G->takeName(G); |
| 283 | GA_G->setVisibility(G->getVisibility()); |
| 284 | G->replaceAllUsesWith(GA_G); |
| 285 | G->eraseFromParent(); |
| 286 | |
| 287 | ++NumFunctionsMerged; |
| 288 | return true; |
| 289 | } |
| 290 | |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 291 | DOUT << "Failed on " << F->getName() << " and " << G->getName() << "\n"; |
| 292 | |
| 293 | ++NumMergeFails; |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | static bool hasAddressTaken(User *U) { |
| 298 | for (User::use_iterator I = U->use_begin(), E = U->use_end(); I != E; ++I) { |
| 299 | User *Use = *I; |
| 300 | |
| 301 | // 'call (bitcast @F to ...)' happens a lot. |
| 302 | while (isa<ConstantExpr>(Use) && Use->hasOneUse()) { |
| 303 | Use = *Use->use_begin(); |
| 304 | } |
| 305 | |
| 306 | if (isa<ConstantExpr>(Use)) { |
| 307 | if (hasAddressTaken(Use)) |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | if (!isa<CallInst>(Use) && !isa<InvokeInst>(Use)) |
| 312 | return true; |
| 313 | |
| 314 | // Make sure we aren't passing U as a parameter to call instead of the |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 315 | // callee. |
| 316 | if (CallSite(cast<Instruction>(Use)).hasArgument(U)) |
| 317 | return true; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | bool MergeFunctions::runOnModule(Module &M) { |
| 324 | bool Changed = false; |
| 325 | |
Duncan Sands | 5baf8ec | 2008-11-02 09:00:33 +0000 | [diff] [blame] | 326 | std::map<unsigned long, std::vector<Function *> > FnMap; |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 327 | |
| 328 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 329 | if (F->isDeclaration() || F->isIntrinsic()) |
| 330 | continue; |
| 331 | |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 332 | if (!F->hasLocalLinkage() && !F->hasExternalLinkage() && |
Nick Lewycky | 6feb333 | 2008-11-02 16:46:26 +0000 | [diff] [blame] | 333 | !F->hasWeakLinkage()) |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 334 | continue; |
| 335 | |
| 336 | if (hasAddressTaken(F)) |
| 337 | continue; |
| 338 | |
| 339 | FnMap[hash(F)].push_back(F); |
| 340 | } |
| 341 | |
| 342 | // TODO: instead of running in a loop, we could also fold functions in callgraph |
| 343 | // order. Constructing the CFG probably isn't cheaper than just running in a loop. |
| 344 | |
| 345 | bool LocalChanged; |
| 346 | do { |
| 347 | LocalChanged = false; |
Duncan Sands | 5baf8ec | 2008-11-02 09:00:33 +0000 | [diff] [blame] | 348 | for (std::map<unsigned long, std::vector<Function *> >::iterator |
| 349 | I = FnMap.begin(), E = FnMap.end(); I != E; ++I) { |
Nick Lewycky | 579a024 | 2008-11-02 05:52:50 +0000 | [diff] [blame] | 350 | DOUT << "size: " << FnMap.size() << "\n"; |
| 351 | std::vector<Function *> &FnVec = I->second; |
| 352 | DOUT << "hash (" << I->first << "): " << FnVec.size() << "\n"; |
| 353 | |
| 354 | for (int i = 0, e = FnVec.size(); i != e; ++i) { |
| 355 | for (int j = i + 1; j != e; ++j) { |
| 356 | bool isEqual = equals(FnVec[i], FnVec[j]); |
| 357 | |
| 358 | DOUT << " " << FnVec[i]->getName() |
| 359 | << (isEqual ? " == " : " != ") |
| 360 | << FnVec[j]->getName() << "\n"; |
| 361 | |
| 362 | if (isEqual) { |
| 363 | if (fold(FnVec, i, j)) { |
| 364 | LocalChanged = true; |
| 365 | FnVec.erase(FnVec.begin() + j); |
| 366 | --j, --e; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | } |
| 373 | Changed |= LocalChanged; |
| 374 | } while (LocalChanged); |
| 375 | |
| 376 | return Changed; |
| 377 | } |