llvm.org GIT mirror llvm / d2c64e2
Fix really obscure bug in CannotBeNegativeZero() (PR22688) With a diabolically crafted test case, we could recurse through this code and return true instead of false. The larger engineering crime is the use of magic numbers. Added FIXME comments for those. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230515 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel 4 years ago
2 changed file(s) with 33 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
19991999 if (const ConstantFP *CFP = dyn_cast(V))
20002000 return !CFP->getValueAPF().isNegZero();
20012001
2002 // FIXME: Magic number! At the least, this should be given a name because it's
2003 // used similarly in CannotBeOrderedLessThanZero(). A better fix may be to
2004 // expose it as a parameter, so it can be used for testing / experimenting.
20022005 if (Depth == 6)
2003 return 1; // Limit search depth.
2006 return false; // Limit search depth.
20042007
20052008 const Operator *I = dyn_cast(V);
20062009 if (!I) return false;
20472050 if (const ConstantFP *CFP = dyn_cast(V))
20482051 return !CFP->getValueAPF().isNegative() || CFP->getValueAPF().isZero();
20492052
2053 // FIXME: Magic number! At the least, this should be given a name because it's
2054 // used similarly in CannotBeNegativeZero(). A better fix may be to
2055 // expose it as a parameter, so it can be used for testing / experimenting.
20502056 if (Depth == 6)
20512057 return false; // Limit search depth.
20522058
3232 ; CHECK: ret double %a
3333 ret double %b
3434 }
35
36 ; We can't optimize away the fadd in this test because the input
37 ; value to the function and subsequently to the fadd may be -0.0.
38 ; In that one special case, the result of the fadd should be +0.0
39 ; rather than the first parameter of the fadd.
40
41 ; Fragile test warning: We need 6 sqrt calls to trigger the bug
42 ; because the internal logic has a magic recursion limit of 6.
43 ; This is presented without any explanation or ability to customize.
44
45 declare float @sqrtf(float)
46
47 define float @PR22688(float %x) {
48 %1 = call float @sqrtf(float %x)
49 %2 = call float @sqrtf(float %1)
50 %3 = call float @sqrtf(float %2)
51 %4 = call float @sqrtf(float %3)
52 %5 = call float @sqrtf(float %4)
53 %6 = call float @sqrtf(float %5)
54 %7 = fadd float %6, 0.0
55 ret float %7
56
57 ; CHECK-LABEL: @PR22688(
58 ; CHECK: fadd float %6, 0.0
59 }
60