MergeFunctions.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 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. //
  20. // When a match is found, the functions are folded. We can only fold two
  21. // functions when we know that the definition of one of them is not
  22. // overridable.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. //
  26. // Future work:
  27. //
  28. // * fold vector<T*>::push_back and vector<S*>::push_back.
  29. //
  30. // These two functions have different types, but in a way that doesn't matter
  31. // to us. As long as we never see an S or T itself, using S* and S** is the
  32. // same as using a T* and T**.
  33. //
  34. // * virtual functions.
  35. //
  36. // Many functions have their address taken by the virtual function table for
  37. // the object they belong to. However, as long as it's only used for a lookup
  38. // and call, this is irrelevant, and we'd like to fold such implementations.
  39. //
  40. //===----------------------------------------------------------------------===//
  41. #define DEBUG_TYPE "mergefunc"
  42. #include "llvm/Transforms/IPO.h"
  43. #include "llvm/ADT/DenseMap.h"
  44. #include "llvm/ADT/FoldingSet.h"
  45. #include "llvm/ADT/Statistic.h"
  46. #include "llvm/Constants.h"
  47. #include "llvm/InlineAsm.h"
  48. #include "llvm/Instructions.h"
  49. #include "llvm/LLVMContext.h"
  50. #include "llvm/Module.h"
  51. #include "llvm/Pass.h"
  52. #include "llvm/Support/CallSite.h"
  53. #include "llvm/Support/Compiler.h"
  54. #include "llvm/Support/Debug.h"
  55. #include "llvm/Support/ErrorHandling.h"
  56. #include "llvm/Support/raw_ostream.h"
  57. #include <map>
  58. #include <vector>
  59. using namespace llvm;
  60. STATISTIC(NumFunctionsMerged, "Number of functions merged");
  61. namespace {
  62. struct VISIBILITY_HIDDEN MergeFunctions : public ModulePass {
  63. static char ID; // Pass identification, replacement for typeid
  64. MergeFunctions() : ModulePass(&ID) {}
  65. bool runOnModule(Module &M);
  66. };
  67. }
  68. char MergeFunctions::ID = 0;
  69. static RegisterPass<MergeFunctions>
  70. X("mergefunc", "Merge Functions");
  71. ModulePass *llvm::createMergeFunctionsPass() {
  72. return new MergeFunctions();
  73. }
  74. // ===----------------------------------------------------------------------===
  75. // Comparison of functions
  76. // ===----------------------------------------------------------------------===
  77. static unsigned long hash(const Function *F) {
  78. const FunctionType *FTy = F->getFunctionType();
  79. FoldingSetNodeID ID;
  80. ID.AddInteger(F->size());
  81. ID.AddInteger(F->getCallingConv());
  82. ID.AddBoolean(F->hasGC());
  83. ID.AddBoolean(FTy->isVarArg());
  84. ID.AddInteger(FTy->getReturnType()->getTypeID());
  85. for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
  86. ID.AddInteger(FTy->getParamType(i)->getTypeID());
  87. return ID.ComputeHash();
  88. }
  89. /// IgnoreBitcasts - given a bitcast, returns the first non-bitcast found by
  90. /// walking the chain of cast operands. Otherwise, returns the argument.
  91. static Value* IgnoreBitcasts(Value *V) {
  92. while (BitCastInst *BC = dyn_cast<BitCastInst>(V))
  93. V = BC->getOperand(0);
  94. return V;
  95. }
  96. /// isEquivalentType - any two pointers are equivalent. Otherwise, standard
  97. /// type equivalence rules apply.
  98. static bool isEquivalentType(const Type *Ty1, const Type *Ty2) {
  99. if (Ty1 == Ty2)
  100. return true;
  101. if (Ty1->getTypeID() != Ty2->getTypeID())
  102. return false;
  103. switch(Ty1->getTypeID()) {
  104. case Type::VoidTyID:
  105. case Type::FloatTyID:
  106. case Type::DoubleTyID:
  107. case Type::X86_FP80TyID:
  108. case Type::FP128TyID:
  109. case Type::PPC_FP128TyID:
  110. case Type::LabelTyID:
  111. case Type::MetadataTyID:
  112. return true;
  113. case Type::IntegerTyID:
  114. case Type::OpaqueTyID:
  115. // Ty1 == Ty2 would have returned true earlier.
  116. return false;
  117. default:
  118. llvm_unreachable("Unknown type!");
  119. return false;
  120. case Type::PointerTyID: {
  121. const PointerType *PTy1 = cast<PointerType>(Ty1);
  122. const PointerType *PTy2 = cast<PointerType>(Ty2);
  123. return PTy1->getAddressSpace() == PTy2->getAddressSpace();
  124. }
  125. case Type::StructTyID: {
  126. const StructType *STy1 = cast<StructType>(Ty1);
  127. const StructType *STy2 = cast<StructType>(Ty2);
  128. if (STy1->getNumElements() != STy2->getNumElements())
  129. return false;
  130. if (STy1->isPacked() != STy2->isPacked())
  131. return false;
  132. for (unsigned i = 0, e = STy1->getNumElements(); i != e; ++i) {
  133. if (!isEquivalentType(STy1->getElementType(i), STy2->getElementType(i)))
  134. return false;
  135. }
  136. return true;
  137. }
  138. case Type::FunctionTyID: {
  139. const FunctionType *FTy1 = cast<FunctionType>(Ty1);
  140. const FunctionType *FTy2 = cast<FunctionType>(Ty2);
  141. if (FTy1->getNumParams() != FTy2->getNumParams() ||
  142. FTy1->isVarArg() != FTy2->isVarArg())
  143. return false;
  144. if (!isEquivalentType(FTy1->getReturnType(), FTy2->getReturnType()))
  145. return false;
  146. for (unsigned i = 0, e = FTy1->getNumParams(); i != e; ++i) {
  147. if (!isEquivalentType(FTy1->getParamType(i), FTy2->getParamType(i)))
  148. return false;
  149. }
  150. return true;
  151. }
  152. case Type::ArrayTyID:
  153. case Type::VectorTyID: {
  154. const SequentialType *STy1 = cast<SequentialType>(Ty1);
  155. const SequentialType *STy2 = cast<SequentialType>(Ty2);
  156. return isEquivalentType(STy1->getElementType(), STy2->getElementType());
  157. }
  158. }
  159. }
  160. /// isEquivalentOperation - determine whether the two operations are the same
  161. /// except that pointer-to-A and pointer-to-B are equivalent. This should be
  162. /// kept in sync with Instruction::isSameOperationAs.
  163. static bool
  164. isEquivalentOperation(const Instruction *I1, const Instruction *I2) {
  165. if (I1->getOpcode() != I2->getOpcode() ||
  166. I1->getNumOperands() != I2->getNumOperands() ||
  167. !isEquivalentType(I1->getType(), I2->getType()))
  168. return false;
  169. // We have two instructions of identical opcode and #operands. Check to see
  170. // if all operands are the same type
  171. for (unsigned i = 0, e = I1->getNumOperands(); i != e; ++i)
  172. if (!isEquivalentType(I1->getOperand(i)->getType(),
  173. I2->getOperand(i)->getType()))
  174. return false;
  175. // Check special state that is a part of some instructions.
  176. if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
  177. return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
  178. LI->getAlignment() == cast<LoadInst>(I2)->getAlignment();
  179. if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
  180. return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
  181. SI->getAlignment() == cast<StoreInst>(I2)->getAlignment();
  182. if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
  183. return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
  184. if (const CallInst *CI = dyn_cast<CallInst>(I1))
  185. return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
  186. CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
  187. CI->getAttributes().getRawPointer() ==
  188. cast<CallInst>(I2)->getAttributes().getRawPointer();
  189. if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
  190. return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
  191. CI->getAttributes().getRawPointer() ==
  192. cast<InvokeInst>(I2)->getAttributes().getRawPointer();
  193. if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) {
  194. if (IVI->getNumIndices() != cast<InsertValueInst>(I2)->getNumIndices())
  195. return false;
  196. for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
  197. if (IVI->idx_begin()[i] != cast<InsertValueInst>(I2)->idx_begin()[i])
  198. return false;
  199. return true;
  200. }
  201. if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) {
  202. if (EVI->getNumIndices() != cast<ExtractValueInst>(I2)->getNumIndices())
  203. return false;
  204. for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
  205. if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I2)->idx_begin()[i])
  206. return false;
  207. return true;
  208. }
  209. return true;
  210. }
  211. static bool compare(const Value *V, const Value *U) {
  212. assert(!isa<BasicBlock>(V) && !isa<BasicBlock>(U) &&
  213. "Must not compare basic blocks.");
  214. assert(isEquivalentType(V->getType(), U->getType()) &&
  215. "Two of the same operation have operands of different type.");
  216. // TODO: If the constant is an expression of F, we should accept that it's
  217. // equal to the same expression in terms of G.
  218. if (isa<Constant>(V))
  219. return V == U;
  220. // The caller has ensured that ValueMap[V] != U. Since Arguments are
  221. // pre-loaded into the ValueMap, and Instructions are added as we go, we know
  222. // that this can only be a mis-match.
  223. if (isa<Instruction>(V) || isa<Argument>(V))
  224. return false;
  225. if (isa<InlineAsm>(V) && isa<InlineAsm>(U)) {
  226. const InlineAsm *IAF = cast<InlineAsm>(V);
  227. const InlineAsm *IAG = cast<InlineAsm>(U);
  228. return IAF->getAsmString() == IAG->getAsmString() &&
  229. IAF->getConstraintString() == IAG->getConstraintString();
  230. }
  231. return false;
  232. }
  233. static bool equals(const BasicBlock *BB1, const BasicBlock *BB2,
  234. DenseMap<const Value *, const Value *> &ValueMap,
  235. DenseMap<const Value *, const Value *> &SpeculationMap) {
  236. // Speculatively add it anyways. If it's false, we'll notice a difference
  237. // later, and this won't matter.
  238. ValueMap[BB1] = BB2;
  239. BasicBlock::const_iterator FI = BB1->begin(), FE = BB1->end();
  240. BasicBlock::const_iterator GI = BB2->begin(), GE = BB2->end();
  241. do {
  242. if (isa<BitCastInst>(FI)) {
  243. ++FI;
  244. continue;
  245. }
  246. if (isa<BitCastInst>(GI)) {
  247. ++GI;
  248. continue;
  249. }
  250. if (!isEquivalentOperation(FI, GI))
  251. return false;
  252. if (isa<GetElementPtrInst>(FI)) {
  253. const GetElementPtrInst *GEPF = cast<GetElementPtrInst>(FI);
  254. const GetElementPtrInst *GEPG = cast<GetElementPtrInst>(GI);
  255. if (GEPF->hasAllZeroIndices() && GEPG->hasAllZeroIndices()) {
  256. // It's effectively a bitcast.
  257. ++FI, ++GI;
  258. continue;
  259. }
  260. // TODO: we only really care about the elements before the index
  261. if (FI->getOperand(0)->getType() != GI->getOperand(0)->getType())
  262. return false;
  263. }
  264. if (ValueMap[FI] == GI) {
  265. ++FI, ++GI;
  266. continue;
  267. }
  268. if (ValueMap[FI] != NULL)
  269. return false;
  270. for (unsigned i = 0, e = FI->getNumOperands(); i != e; ++i) {
  271. Value *OpF = IgnoreBitcasts(FI->getOperand(i));
  272. Value *OpG = IgnoreBitcasts(GI->getOperand(i));
  273. if (ValueMap[OpF] == OpG)
  274. continue;
  275. if (ValueMap[OpF] != NULL)
  276. return false;
  277. if (OpF->getValueID() != OpG->getValueID() ||
  278. !isEquivalentType(OpF->getType(), OpG->getType()))
  279. return false;
  280. if (isa<PHINode>(FI)) {
  281. if (SpeculationMap[OpF] == NULL)
  282. SpeculationMap[OpF] = OpG;
  283. else if (SpeculationMap[OpF] != OpG)
  284. return false;
  285. continue;
  286. } else if (isa<BasicBlock>(OpF)) {
  287. assert(isa<TerminatorInst>(FI) &&
  288. "BasicBlock referenced by non-Terminator non-PHI");
  289. // This call changes the ValueMap, hence we can't use
  290. // Value *& = ValueMap[...]
  291. if (!equals(cast<BasicBlock>(OpF), cast<BasicBlock>(OpG), ValueMap,
  292. SpeculationMap))
  293. return false;
  294. } else {
  295. if (!compare(OpF, OpG))
  296. return false;
  297. }
  298. ValueMap[OpF] = OpG;
  299. }
  300. ValueMap[FI] = GI;
  301. ++FI, ++GI;
  302. } while (FI != FE && GI != GE);
  303. return FI == FE && GI == GE;
  304. }
  305. static bool equals(const Function *F, const Function *G) {
  306. // We need to recheck everything, but check the things that weren't included
  307. // in the hash first.
  308. if (F->getAttributes() != G->getAttributes())
  309. return false;
  310. if (F->hasGC() != G->hasGC())
  311. return false;
  312. if (F->hasGC() && F->getGC() != G->getGC())
  313. return false;
  314. if (F->hasSection() != G->hasSection())
  315. return false;
  316. if (F->hasSection() && F->getSection() != G->getSection())
  317. return false;
  318. if (F->isVarArg() != G->isVarArg())
  319. return false;
  320. // TODO: if it's internal and only used in direct calls, we could handle this
  321. // case too.
  322. if (F->getCallingConv() != G->getCallingConv())
  323. return false;
  324. if (!isEquivalentType(F->getFunctionType(), G->getFunctionType()))
  325. return false;
  326. DenseMap<const Value *, const Value *> ValueMap;
  327. DenseMap<const Value *, const Value *> SpeculationMap;
  328. ValueMap[F] = G;
  329. assert(F->arg_size() == G->arg_size() &&
  330. "Identical functions have a different number of args.");
  331. for (Function::const_arg_iterator fi = F->arg_begin(), gi = G->arg_begin(),
  332. fe = F->arg_end(); fi != fe; ++fi, ++gi)
  333. ValueMap[fi] = gi;
  334. if (!equals(&F->getEntryBlock(), &G->getEntryBlock(), ValueMap,
  335. SpeculationMap))
  336. return false;
  337. for (DenseMap<const Value *, const Value *>::iterator
  338. I = SpeculationMap.begin(), E = SpeculationMap.end(); I != E; ++I) {
  339. if (ValueMap[I->first] != I->second)
  340. return false;
  341. }
  342. return true;
  343. }
  344. // ===----------------------------------------------------------------------===
  345. // Folding of functions
  346. // ===----------------------------------------------------------------------===
  347. // Cases:
  348. // * F is external strong, G is external strong:
  349. // turn G into a thunk to F (1)
  350. // * F is external strong, G is external weak:
  351. // turn G into a thunk to F (1)
  352. // * F is external weak, G is external weak:
  353. // unfoldable
  354. // * F is external strong, G is internal:
  355. // address of G taken:
  356. // turn G into a thunk to F (1)
  357. // address of G not taken:
  358. // make G an alias to F (2)
  359. // * F is internal, G is external weak
  360. // address of F is taken:
  361. // turn G into a thunk to F (1)
  362. // address of F is not taken:
  363. // make G an alias of F (2)
  364. // * F is internal, G is internal:
  365. // address of F and G are taken:
  366. // turn G into a thunk to F (1)
  367. // address of G is not taken:
  368. // make G an alias to F (2)
  369. //
  370. // alias requires linkage == (external,local,weak) fallback to creating a thunk
  371. // external means 'externally visible' linkage != (internal,private)
  372. // internal means linkage == (internal,private)
  373. // weak means linkage mayBeOverridable
  374. // being external implies that the address is taken
  375. //
  376. // 1. turn G into a thunk to F
  377. // 2. make G an alias to F
  378. enum LinkageCategory {
  379. ExternalStrong,
  380. ExternalWeak,
  381. Internal
  382. };
  383. static LinkageCategory categorize(const Function *F) {
  384. switch (F->getLinkage()) {
  385. case GlobalValue::InternalLinkage:
  386. case GlobalValue::PrivateLinkage:
  387. case GlobalValue::LinkerPrivateLinkage:
  388. return Internal;
  389. case GlobalValue::WeakAnyLinkage:
  390. case GlobalValue::WeakODRLinkage:
  391. case GlobalValue::ExternalWeakLinkage:
  392. return ExternalWeak;
  393. case GlobalValue::ExternalLinkage:
  394. case GlobalValue::AvailableExternallyLinkage:
  395. case GlobalValue::LinkOnceAnyLinkage:
  396. case GlobalValue::LinkOnceODRLinkage:
  397. case GlobalValue::AppendingLinkage:
  398. case GlobalValue::DLLImportLinkage:
  399. case GlobalValue::DLLExportLinkage:
  400. case GlobalValue::GhostLinkage:
  401. case GlobalValue::CommonLinkage:
  402. return ExternalStrong;
  403. }
  404. llvm_unreachable("Unknown LinkageType.");
  405. return ExternalWeak;
  406. }
  407. static void ThunkGToF(Function *F, Function *G) {
  408. Function *NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "",
  409. G->getParent());
  410. BasicBlock *BB = BasicBlock::Create(F->getContext(), "", NewG);
  411. std::vector<Value *> Args;
  412. unsigned i = 0;
  413. const FunctionType *FFTy = F->getFunctionType();
  414. for (Function::arg_iterator AI = NewG->arg_begin(), AE = NewG->arg_end();
  415. AI != AE; ++AI) {
  416. if (FFTy->getParamType(i) == AI->getType())
  417. Args.push_back(AI);
  418. else {
  419. Value *BCI = new BitCastInst(AI, FFTy->getParamType(i), "", BB);
  420. Args.push_back(BCI);
  421. }
  422. ++i;
  423. }
  424. CallInst *CI = CallInst::Create(F, Args.begin(), Args.end(), "", BB);
  425. CI->setTailCall();
  426. CI->setCallingConv(F->getCallingConv());
  427. if (NewG->getReturnType() == Type::getVoidTy(F->getContext())) {
  428. ReturnInst::Create(F->getContext(), BB);
  429. } else if (CI->getType() != NewG->getReturnType()) {
  430. Value *BCI = new BitCastInst(CI, NewG->getReturnType(), "", BB);
  431. ReturnInst::Create(F->getContext(), BCI, BB);
  432. } else {
  433. ReturnInst::Create(F->getContext(), CI, BB);
  434. }
  435. NewG->copyAttributesFrom(G);
  436. NewG->takeName(G);
  437. G->replaceAllUsesWith(NewG);
  438. G->eraseFromParent();
  439. // TODO: look at direct callers to G and make them all direct callers to F.
  440. }
  441. static void AliasGToF(Function *F, Function *G) {
  442. if (!G->hasExternalLinkage() && !G->hasLocalLinkage() && !G->hasWeakLinkage())
  443. return ThunkGToF(F, G);
  444. GlobalAlias *GA = new GlobalAlias(
  445. G->getType(), G->getLinkage(), "",
  446. ConstantExpr::getBitCast(F, G->getType()), G->getParent());
  447. F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
  448. GA->takeName(G);
  449. GA->setVisibility(G->getVisibility());
  450. G->replaceAllUsesWith(GA);
  451. G->eraseFromParent();
  452. }
  453. static bool fold(std::vector<Function *> &FnVec, unsigned i, unsigned j) {
  454. Function *F = FnVec[i];
  455. Function *G = FnVec[j];
  456. LinkageCategory catF = categorize(F);
  457. LinkageCategory catG = categorize(G);
  458. if (catF == ExternalWeak || (catF == Internal && catG == ExternalStrong)) {
  459. std::swap(FnVec[i], FnVec[j]);
  460. std::swap(F, G);
  461. std::swap(catF, catG);
  462. }
  463. switch (catF) {
  464. case ExternalStrong:
  465. switch (catG) {
  466. case ExternalStrong:
  467. case ExternalWeak:
  468. ThunkGToF(F, G);
  469. break;
  470. case Internal:
  471. if (G->hasAddressTaken())
  472. ThunkGToF(F, G);
  473. else
  474. AliasGToF(F, G);
  475. break;
  476. }
  477. break;
  478. case ExternalWeak: {
  479. assert(catG == ExternalWeak);
  480. // Make them both thunks to the same internal function.
  481. F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
  482. Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
  483. F->getParent());
  484. H->copyAttributesFrom(F);
  485. H->takeName(F);
  486. F->replaceAllUsesWith(H);
  487. ThunkGToF(F, G);
  488. ThunkGToF(F, H);
  489. F->setLinkage(GlobalValue::InternalLinkage);
  490. } break;
  491. case Internal:
  492. switch (catG) {
  493. case ExternalStrong:
  494. llvm_unreachable(0);
  495. // fall-through
  496. case ExternalWeak:
  497. if (F->hasAddressTaken())
  498. ThunkGToF(F, G);
  499. else
  500. AliasGToF(F, G);
  501. break;
  502. case Internal: {
  503. bool addrTakenF = F->hasAddressTaken();
  504. bool addrTakenG = G->hasAddressTaken();
  505. if (!addrTakenF && addrTakenG) {
  506. std::swap(FnVec[i], FnVec[j]);
  507. std::swap(F, G);
  508. std::swap(addrTakenF, addrTakenG);
  509. }
  510. if (addrTakenF && addrTakenG) {
  511. ThunkGToF(F, G);
  512. } else {
  513. assert(!addrTakenG);
  514. AliasGToF(F, G);
  515. }
  516. } break;
  517. }
  518. break;
  519. }
  520. ++NumFunctionsMerged;
  521. return true;
  522. }
  523. // ===----------------------------------------------------------------------===
  524. // Pass definition
  525. // ===----------------------------------------------------------------------===
  526. bool MergeFunctions::runOnModule(Module &M) {
  527. bool Changed = false;
  528. std::map<unsigned long, std::vector<Function *> > FnMap;
  529. for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
  530. if (F->isDeclaration() || F->isIntrinsic())
  531. continue;
  532. FnMap[hash(F)].push_back(F);
  533. }
  534. // TODO: instead of running in a loop, we could also fold functions in
  535. // callgraph order. Constructing the CFG probably isn't cheaper than just
  536. // running in a loop, unless it happened to already be available.
  537. bool LocalChanged;
  538. do {
  539. LocalChanged = false;
  540. DOUT << "size: " << FnMap.size() << "\n";
  541. for (std::map<unsigned long, std::vector<Function *> >::iterator
  542. I = FnMap.begin(), E = FnMap.end(); I != E; ++I) {
  543. std::vector<Function *> &FnVec = I->second;
  544. DOUT << "hash (" << I->first << "): " << FnVec.size() << "\n";
  545. for (int i = 0, e = FnVec.size(); i != e; ++i) {
  546. for (int j = i + 1; j != e; ++j) {
  547. bool isEqual = equals(FnVec[i], FnVec[j]);
  548. DEBUG(errs() << " " << FnVec[i]->getName()
  549. << (isEqual ? " == " : " != ")
  550. << FnVec[j]->getName() << "\n");
  551. if (isEqual) {
  552. if (fold(FnVec, i, j)) {
  553. LocalChanged = true;
  554. FnVec.erase(FnVec.begin() + j);
  555. --j, --e;
  556. }
  557. }
  558. }
  559. }
  560. }
  561. Changed |= LocalChanged;
  562. } while (LocalChanged);
  563. return Changed;
  564. }