[llvm-commits] [llvm] r58568 - in /llvm/trunk: lib/Transforms/IPO/MergeFunctions.cpp test/Transforms/MergeFunc/fold-weak.ll
Nick Lewycky
nicholas at mxc.ca
Sun Nov 2 08:46:26 PST 2008
Author: nicholas
Date: Sun Nov 2 10:46:26 2008
New Revision: 58568
URL: http://llvm.org/viewvc/llvm-project?rev=58568&view=rev
Log:
Changes from Duncan's review:
* merge two weak functions by making them both alias a third non-weak fn
* don't reimplement CallSite::hasArgument
* whitelist the safe linkage types
Added:
llvm/trunk/test/Transforms/MergeFunc/fold-weak.ll
Modified:
llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp
Modified: llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp?rev=58568&r1=58567&r2=58568&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp Sun Nov 2 10:46:26 2008
@@ -54,6 +54,7 @@
#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
+#include "llvm/Support/CallSite.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include <map>
@@ -265,6 +266,28 @@
}
}
+ if (F->hasWeakLinkage() && G->hasWeakLinkage()) {
+ GlobalAlias *GA_F = new GlobalAlias(F->getType(), F->getLinkage(), "",
+ 0, F->getParent());
+ GA_F->takeName(F);
+ GA_F->setVisibility(F->getVisibility());
+ F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
+ F->replaceAllUsesWith(GA_F);
+ F->setName("folded." + GA_F->getName());
+ F->setLinkage(GlobalValue::ExternalLinkage);
+ GA_F->setAliasee(F);
+
+ GlobalAlias *GA_G = new GlobalAlias(G->getType(), G->getLinkage(), "",
+ F, G->getParent());
+ GA_G->takeName(G);
+ GA_G->setVisibility(G->getVisibility());
+ G->replaceAllUsesWith(GA_G);
+ G->eraseFromParent();
+
+ ++NumFunctionsMerged;
+ return true;
+ }
+
DOUT << "Failed on " << F->getName() << " and " << G->getName() << "\n";
++NumMergeFails;
@@ -289,13 +312,9 @@
return true;
// Make sure we aren't passing U as a parameter to call instead of the
- // callee. getOperand(0) is the callee for both CallInst and InvokeInst.
- // Check the other operands to see if any of them is F.
- for (User::op_iterator OI = I->op_begin() + 1, OE = I->op_end(); OI != OE;
- ++OI) {
- if (*OI == U)
- return true;
- }
+ // callee.
+ if (CallSite(cast<Instruction>(Use)).hasArgument(U))
+ return true;
}
return false;
@@ -310,8 +329,8 @@
if (F->isDeclaration() || F->isIntrinsic())
continue;
- if (F->hasLinkOnceLinkage() || F->hasCommonLinkage() ||
- F->hasDLLImportLinkage() || F->hasDLLExportLinkage())
+ if (!F->hasInternalLinkage() && !F->hasExternalLinkage() &&
+ !F->hasWeakLinkage())
continue;
if (hasAddressTaken(F))
Added: llvm/trunk/test/Transforms/MergeFunc/fold-weak.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/MergeFunc/fold-weak.ll?rev=58568&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/MergeFunc/fold-weak.ll (added)
+++ llvm/trunk/test/Transforms/MergeFunc/fold-weak.ll Sun Nov 2 10:46:26 2008
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | opt -mergefunc | llvm-dis | grep {alias weak} | count 2
+
+define weak i32 @sum(i32 %x, i32 %y) {
+ %sum = add i32 %x, %y
+ ret i32 %sum
+}
+
+define weak i32 @add(i32 %x, i32 %y) {
+ %sum = add i32 %x, %y
+ ret i32 %sum
+}
More information about the llvm-commits
mailing list