Shrinkify libcalls: use float versions of double libm functions with fast-math (bug 17850)

When a call to a double-precision libm function has fast-math semantics 
(via function attribute for now because there is no IR-level FMF on calls), 
we can avoid fpext/fptrunc operations and use the float version of the call
if the input and output are both float.

We already do this optimization using a command-line option; this patch just
adds the ability for fast-math to use the existing functionality.

I moved the cl::opt from InstructionCombining into SimplifyLibCalls because
it's only ever used internally to that class.

Modified the existing test cases to use the unsafe-fp-math attribute rather
than repeating all tests.

This patch should solve: http://llvm.org/bugs/show_bug.cgi?id=17850

Differential Revision: http://reviews.llvm.org/D5893



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220390 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjay Patel
2014-10-22 15:29:23 +00:00
parent d5b89313e0
commit dc18ebc4b1
4 changed files with 131 additions and 114 deletions

View File

@@ -70,12 +70,6 @@ STATISTIC(NumExpand, "Number of expansions");
STATISTIC(NumFactor , "Number of factorizations");
STATISTIC(NumReassoc , "Number of reassociations");
static cl::opt<bool>
EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
cl::init(false),
cl::desc("Enable unsafe double to float "
"shrinking for math lib calls"));
// Initialization Routines
void llvm::initializeInstCombine(PassRegistry &Registry) {
initializeInstCombinerPass(Registry);
@@ -2945,7 +2939,7 @@ public:
InstCombinerLibCallSimplifier(const DataLayout *DL,
const TargetLibraryInfo *TLI,
InstCombiner *IC)
: LibCallSimplifier(DL, TLI, EnableUnsafeFPShrink) {
: LibCallSimplifier(DL, TLI) {
this->IC = IC;
}

View File

@@ -40,6 +40,13 @@ static cl::opt<bool>
ColdErrorCalls("error-reporting-is-cold", cl::init(true), cl::Hidden,
cl::desc("Treat error-reporting calls as cold"));
static cl::opt<bool>
EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
cl::init(false),
cl::desc("Enable unsafe double to float "
"shrinking for math lib calls"));
//===----------------------------------------------------------------------===//
// Helper Functions
//===----------------------------------------------------------------------===//
@@ -1989,6 +1996,20 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
IRBuilder<> Builder(CI);
bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C;
// Command-line parameter overrides function attribute.
if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
UnsafeFPShrink = EnableUnsafeFPShrink;
else if (Callee->hasFnAttribute("unsafe-fp-math")) {
// FIXME: This is the same problem as described in optimizeSqrt().
// If calls gain access to IR-level FMF, then use that instead of a
// function attribute.
// Check for unsafe-fp-math = true.
Attribute Attr = Callee->getFnAttribute("unsafe-fp-math");
if (Attr.getValueAsString() == "true")
UnsafeFPShrink = true;
}
// Next check for intrinsics.
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
if (!isCallingConvC)
@@ -2182,11 +2203,10 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
}
LibCallSimplifier::LibCallSimplifier(const DataLayout *DL,
const TargetLibraryInfo *TLI,
bool UnsafeFPShrink) :
const TargetLibraryInfo *TLI) :
DL(DL),
TLI(TLI),
UnsafeFPShrink(UnsafeFPShrink) {
UnsafeFPShrink(false) {
}
void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {