[llvm] r267504 - [CodeGenPrepare] don't convert an unpredictable select into control flow
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 25 17:47:39 PDT 2016
Author: spatel
Date: Mon Apr 25 19:47:39 2016
New Revision: 267504
URL: http://llvm.org/viewvc/llvm-project?rev=267504&view=rev
Log:
[CodeGenPrepare] don't convert an unpredictable select into control flow
Suggested in the review of D19488:
http://reviews.llvm.org/D19488
Modified:
llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
llvm/trunk/test/Transforms/CodeGenPrepare/X86/select.ll
Modified: llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp?rev=267504&r1=267503&r2=267504&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp (original)
+++ llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp Mon Apr 25 19:47:39 2016
@@ -4568,7 +4568,8 @@ bool CodeGenPrepare::optimizeSelectInst(
bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1);
// Can we convert the 'select' to CF ?
- if (DisableSelectToBranch || OptSize || !TLI || VectorCond)
+ if (DisableSelectToBranch || OptSize || !TLI || VectorCond ||
+ SI->getMetadata(LLVMContext::MD_unpredictable))
return false;
TargetLowering::SelectSupportKind SelectKind;
Modified: llvm/trunk/test/Transforms/CodeGenPrepare/X86/select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/CodeGenPrepare/X86/select.ll?rev=267504&r1=267503&r2=267504&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/CodeGenPrepare/X86/select.ll (original)
+++ llvm/trunk/test/Transforms/CodeGenPrepare/X86/select.ll Mon Apr 25 19:47:39 2016
@@ -53,7 +53,7 @@ entry:
; CHECK: %div = fdiv float %a, %b
; CHECK: br label %select.end
; CHECK: select.end:
-; CHECK: %sel = phi float [ 4.000000e+00, %entry ], [ %div, %select.false.sink ]
+; CHECK: %sel = phi float [ 4.000000e+00, %entry ], [ %div, %select.false.sink ]
; CHECK: ret float %sel
}
@@ -75,20 +75,39 @@ entry:
; CHECK: %div2 = fdiv float %b, %a
; CHECK: br label %select.end
; CHECK: select.end:
-; CHECK: %sel = phi float [ %div1, %select.true.sink ], [ %div2, %select.false.sink ]
+; CHECK: %sel = phi float [ %div1, %select.true.sink ], [ %div2, %select.false.sink ]
; CHECK: ret float %sel
}
+; But if the select is marked unpredictable, then don't turn it into a branch.
+
+define float @unpredictable_select(float %a, float %b) {
+; CHECK-LABEL: @unpredictable_select(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[DIV:%.*]] = fdiv float %a, %b
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float %a, 1.000000e+00
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[DIV]], float 2.000000e+00, !unpredictable !0
+; CHECK-NEXT: ret float [[SEL]]
+;
+entry:
+ %div = fdiv float %a, %b
+ %cmp = fcmp ogt float %a, 1.0
+ %sel = select i1 %cmp, float %div, float 2.0, !unpredictable !0
+ ret float %sel
+}
+
+!0 = !{}
+
; An 'fadd' is not too expensive, so it's ok to speculate.
define float @fadd_no_sink(float %a, float %b) {
%add = fadd float %a, %b
%cmp = fcmp ogt float 6.0, %a
- %sel = select i1 %cmp, float %add, float 7.0
+ %sel = select i1 %cmp, float %add, float 7.0
ret float %sel
; CHECK-LABEL: @fadd_no_sink(
-; CHECK: %sel = select i1 %cmp, float %add, float 7.0
+; CHECK: %sel = select i1 %cmp, float %add, float 7.0
}
; Possible enhancement: sinkability is only calculated with the direct
@@ -104,7 +123,7 @@ entry:
ret float %sel
; CHECK-LABEL: @fdiv_no_sink(
-; CHECK: %sel = select i1 %cmp, float %add, float 8.0
+; CHECK: %sel = select i1 %cmp, float %add, float 8.0
}
; Do not transform the CFG if the select operands may have side effects.
More information about the llvm-commits
mailing list