xref: /NextBSD/contrib/llvm/patches/patch-06-llvm-r248439-fdiv-hoisting.diff (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
1Pull in r248439 from upstream llvm trunk (by Sanjay Patel):
2
3  set div/rem default values to 'expensive' in TargetTransformInfo's
4  cost model
5
6  ...because that's what the cost model was intended to do.
7
8  As discussed in D12882, this fix has a temporary unintended
9  consequence for SimplifyCFG: it causes us to not speculate an fdiv.
10  However, two wrongs make PR24818 right, and two wrongs make PR24343
11  act right even though it's really still wrong.
12
13  I intend to correct SimplifyCFG and add to CodeGenPrepare to account
14  for this cost model change and preserve the righteousness for the bug
15  report cases.
16
17  https://llvm.org/bugs/show_bug.cgi?id=24818
18  https://llvm.org/bugs/show_bug.cgi?id=24343
19
20  Differential Revision: http://reviews.llvm.org/D12882
21
22This fixes the too-eager fdiv hoisting in pow(), which could lead to
23unexpected floating point exceptions.
24
25Introduced here: http://svnweb.freebsd.org/changeset/base/288195
26
27Index: include/llvm/Analysis/TargetTransformInfoImpl.h
28===================================================================
29--- include/llvm/Analysis/TargetTransformInfoImpl.h
30+++ include/llvm/Analysis/TargetTransformInfoImpl.h
31@@ -60,6 +60,14 @@ class TargetTransformInfoImplBase {
32       // Otherwise, the default basic cost is used.
33       return TTI::TCC_Basic;
34
35+    case Instruction::FDiv:
36+    case Instruction::FRem:
37+    case Instruction::SDiv:
38+    case Instruction::SRem:
39+    case Instruction::UDiv:
40+    case Instruction::URem:
41+      return TTI::TCC_Expensive;
42+
43     case Instruction::IntToPtr: {
44       // An inttoptr cast is free so long as the input is a legal integer type
45       // which doesn't contain values outside the range of a pointer.
46Index: test/Transforms/SimplifyCFG/speculate-math.ll
47===================================================================
48--- test/Transforms/SimplifyCFG/speculate-math.ll
49+++ test/Transforms/SimplifyCFG/speculate-math.ll
50@@ -7,6 +7,33 @@ declare float @llvm.fabs.f32(float) nounwind reado
51 declare float @llvm.minnum.f32(float, float) nounwind readonly
52 declare float @llvm.maxnum.f32(float, float) nounwind readonly
53
54+; FIXME: This is intended to be a temporary test. As discussed in
55+; D12882, we actually do want to speculate even expensive operations
56+; in SimplifyCFG because it can expose more optimizations for other
57+; passes. Therefore, we either need to adjust SimplifyCFG's
58+; calculations that use the TTI cost model or use a different cost
59+; model for deciding which ops should be speculated in SimplifyCFG.
60+; We should also be using the TTI cost model later - for example in
61+; CodeGenPrepare - to potentially undo this speculation.
62+
63+; Do not speculate fdiv by default because it is generally expensive.
64+
65+; CHECK-LABEL: @fdiv_test(
66+; CHECK-NOT: select
67+define double @fdiv_test(double %a, double %b) {
68+entry:
69+  %cmp = fcmp ogt double %a, 0.0
70+  br i1 %cmp, label %cond.true, label %cond.end
71+
72+cond.true:
73+  %div = fdiv double %b, %a
74+  br label %cond.end
75+
76+cond.end:
77+  %cond = phi double [ %div, %cond.true ], [ 0.0, %entry ]
78+  ret double %cond
79+}
80+
81 ; CHECK-LABEL: @sqrt_test(
82 ; CHECK: select
83 define void @sqrt_test(float addrspace(1)* noalias nocapture %out, float %a) nounwind {
84