Do not drop fast-math flags in SimplifyBinOp

Michael Zolotukhin mzolotukhin at apple.com
Thu Jan 29 16:12:25 PST 2015


Hi,

Currently InlineCost pass fails to use fast-math flags when analyzing profitability of inlining. Here is a small example to demonstrate that:

float foo(float *a, float b) {
  float r;
  if (a[1] * b)
    r = a[1]+a[2]/a[3]-a[4]*a[5]+a[6]-a[7]+a[8]*a[9]+
            a[11]+a[12]/a[13]-a[14]*a[15]+a[16]-a[17]+a[18]*a[19]+
            a[21]+a[22]/a[23]-a[24]*a[25]+a[26]-a[27]+a[28]*a[29]+
            a[31]+a[32]/a[33]-a[34]*a[35]+a[36]-a[37]+a[38]*a[39]+
            a[41]+a[42]/a[43]-a[44]*a[45]+a[46]-a[47]+a[48]*a[49]+
            a[51]+a[52]/a[53]-a[54]*a[55]+a[56]-a[57]+a[58]*a[59]+
            a[61]+a[62]/a[63]-a[64]*a[65]+a[66]-a[67]+a[68]*a[69]+
            a[0]*a[0]+b;
  else
    r = 1;
  return r;
}
float boo(float *a) {
  return foo(a, 0.0);
}

Here inliner can’t figure out that a[1]*b can be folded to 0 if the call is inlined, and decides not to inline.

That happens because inliner drops fast-math flags in SimplifyBinOp. The attached patch fixes it by looking into the instruction math flags. Does it look good?

Without patch:
> clang inline.c -O3 -S -Rpass=inline -ffast-math
// Empty output

With patch:
> clang inline.c -O3 -S -Rpass=inline -ffast-math
inline.c:17:10: remark: foo inlined into boo [-Rpass=inline]
  return foo(a, 0.0);
         ^

-------------- next part --------------
A non-text attachment was scrubbed...
Name: inst-simplify-fast-math.patch
Type: application/octet-stream
Size: 2490 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150129/54f695d9/attachment.obj>
-------------- next part --------------


Thanks,
Michael


More information about the llvm-commits mailing list