[llvm] 5672ae8 - [SCCP] Use constant ranges for select, if cond is overdefined.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 18 02:28:44 PDT 2020


Author: Florian Hahn
Date: 2020-03-18T09:26:02Z
New Revision: 5672ae8d868bbe8ddb33c23c2d2b8eadf8bf2a7f

URL: https://github.com/llvm/llvm-project/commit/5672ae8d868bbe8ddb33c23c2d2b8eadf8bf2a7f
DIFF: https://github.com/llvm/llvm-project/commit/5672ae8d868bbe8ddb33c23c2d2b8eadf8bf2a7f.diff

LOG: [SCCP] Use constant ranges for select, if cond is overdefined.

For selects with an unknown condition, we can approximate the result by
merging the state of both options. This automatically takes care of
the case where on operand is undef.

Reviewers: davide, efriedma, mssimpso

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D71935

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/SCCP.cpp
    llvm/test/Transforms/SCCP/ip-ranges-select.ll
    llvm/test/Transforms/SCCP/select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/SCCP.cpp b/llvm/lib/Transforms/Scalar/SCCP.cpp
index 708f02b2a425..c17c35b7e323 100644
--- a/llvm/lib/Transforms/Scalar/SCCP.cpp
+++ b/llvm/lib/Transforms/Scalar/SCCP.cpp
@@ -939,16 +939,10 @@ void SCCPSolver::visitSelectInst(SelectInst &I) {
   LatticeVal TVal = getValueState(I.getTrueValue());
   LatticeVal FVal = getValueState(I.getFalseValue());
 
-  // select ?, C, C -> C.
-  if (isConstant(TVal) && isConstant(FVal) &&
-      getConstant(TVal) == getConstant(FVal))
-    return (void)markConstant(&I, getConstant(FVal));
-
-  if (TVal.isUnknownOrUndef())   // select ?, undef, X -> X.
-    return (void)mergeInValue(&I, FVal);
-  if (FVal.isUnknownOrUndef())   // select ?, X, undef -> X.
-    return (void)mergeInValue(&I, TVal);
-  markOverdefined(&I);
+  bool Changed = ValueState[&I].mergeIn(TVal, DL);
+  Changed |= ValueState[&I].mergeIn(FVal, DL);
+  if (Changed)
+    pushToWorkListMsg(ValueState[&I], &I);
 }
 
 // Handle Unary Operators.

diff  --git a/llvm/test/Transforms/SCCP/ip-ranges-select.ll b/llvm/test/Transforms/SCCP/ip-ranges-select.ll
index b19dc4335262..f1f2b032ac46 100644
--- a/llvm/test/Transforms/SCCP/ip-ranges-select.ll
+++ b/llvm/test/Transforms/SCCP/ip-ranges-select.ll
@@ -35,3 +35,98 @@ bb12:                                             ; preds = %bb8, %bb3, %bb
 }
 
 declare void @use(i32)
+
+define internal i1 @f1(i32 %x, i32 %y, i1 %cmp) {
+; CHECK-LABEL: define internal i1 @f1(i32 %x, i32 %y, i1 %cmp) {
+; CHECK-NEXT:    %sel.1 = select i1 %cmp, i32 %x, i32 %y
+; CHECK-NEXT:    %c.2 = icmp sgt i32 %sel.1, 100
+; CHECK-NEXT:    %c.3 = icmp eq i32 %sel.1, 50
+; CHECK-NEXT:    %res.1 = add i1 false, %c.2
+; CHECK-NEXT:    %res.2 = add i1 %res.1, %c.3
+; CHECK-NEXT:    %res.3 = add i1 %res.2, false
+; CHECK-NEXT:    ret i1 %res.3
+;
+  %sel.1 = select i1 %cmp, i32 %x, i32 %y
+  %c.1 = icmp sgt i32 %sel.1, 300
+  %c.2 = icmp sgt i32 %sel.1, 100
+  %c.3 = icmp eq i32 %sel.1, 50
+  %c.4 = icmp slt i32 %sel.1, 9
+  %res.1 = add i1 %c.1, %c.2
+  %res.2 = add i1 %res.1, %c.3
+  %res.3 = add i1 %res.2, %c.4
+  ret i1 %res.3
+}
+
+define i1 @caller1(i1 %cmp) {
+; CHECK-LABEL:  define i1 @caller1(i1 %cmp) {
+; CHECK-NEXT:    %call.1 = tail call i1 @f1(i32 10, i32 100, i1 %cmp)
+; CHECK-NEXT:    %call.2 = tail call i1 @f1(i32 20, i32 200, i1 %cmp)
+; CHECK-NEXT:    %res = and i1 %call.1, %call.2
+; CHECK-NEXT:    ret i1 %res
+;
+  %call.1 = tail call i1 @f1(i32 10, i32 100, i1 %cmp)
+  %call.2 = tail call i1 @f1(i32 20, i32 200, i1 %cmp)
+  %res = and i1 %call.1, %call.2
+  ret i1 %res
+}
+
+
+define i1 @f2(i32 %x, i32 %y, i1 %cmp) {
+; CHECK-LABEL: define i1 @f2(i32 %x, i32 %y, i1 %cmp) {
+; CHECK-NEXT:    %sel.1 = select i1 %cmp, i32 %x, i32 %y
+; CHECK-NEXT:    %c.1 = icmp sgt i32 %sel.1, 300
+; CHECK-NEXT:    %c.2 = icmp sgt i32 %sel.1, 100
+; CHECK-NEXT:    %c.3 = icmp eq i32 %sel.1, 50
+; CHECK-NEXT:    %c.4 = icmp slt i32 %sel.1, 9
+; CHECK-NEXT:    %res.1 = add i1 %c.1, %c.2
+; CHECK-NEXT:    %res.2 = add i1 %res.1, %c.3
+; CHECK-NEXT:    %res.3 = add i1 %res.2, %c.4
+; CHECK-NEXT:    ret i1 %res.3
+;
+  %sel.1 = select i1 %cmp, i32 %x, i32 %y
+  %c.1 = icmp sgt i32 %sel.1, 300
+  %c.2 = icmp sgt i32 %sel.1, 100
+  %c.3 = icmp eq i32 %sel.1, 50
+  %c.4 = icmp slt i32 %sel.1, 9
+  %res.1 = add i1 %c.1, %c.2
+  %res.2 = add i1 %res.1, %c.3
+  %res.3 = add i1 %res.2, %c.4
+  ret i1 %res.3
+}
+
+define i1 @caller2(i32 %y, i1 %cmp) {
+; CHECK-LABEL:  define i1 @caller2(i32 %y, i1 %cmp) {
+; CHECK-NEXT:    %call.1 = tail call i1 @f2(i32 10, i32 %y, i1 %cmp)
+; CHECK-NEXT:    %call.2 = tail call i1 @f2(i32 20, i32 %y, i1 %cmp)
+; CHECK-NEXT:    %res = and i1 %call.1, %call.2
+; CHECK-NEXT:    ret i1 %res
+;
+  %call.1 = tail call i1 @f2(i32 10, i32 %y, i1 %cmp)
+  %call.2 = tail call i1 @f2(i32 20, i32 %y, i1 %cmp)
+  %res = and i1 %call.1, %call.2
+  ret i1 %res
+}
+
+ at GV = external global i32
+
+define i32 @f3_constantexpr_cond(i32 %x, i32 %y) {
+; CHECK-LABEL: define i32 @f3_constantexpr_cond(i32 %x, i32 %y)
+; CHECK-NEXT:   %sel.1 = select i1 icmp eq (i32* bitcast (i32 (i32, i32)* @f3_constantexpr_cond to i32*), i32* @GV), i32 %x, i32 %y
+; CHECK-NEXT:   ret i32 %sel.1
+;
+  %sel.1 = select i1 icmp eq (i32* bitcast (i32 (i32, i32)* @f3_constantexpr_cond to i32*), i32* @GV), i32 %x, i32 %y
+  ret i32 %sel.1
+}
+
+define i32 @caller3(i32 %y) {
+; CHECK-LABEL:  define i32 @caller3(i32 %y) {
+; CHECK-NEXT:    %call.1 = tail call i32 @f3_constantexpr_cond(i32 10, i32 %y)
+; CHECK-NEXT:    %call.2 = tail call i32 @f3_constantexpr_cond(i32 20, i32 %y)
+; CHECK-NEXT:    %res = and i32 %call.1, %call.2
+; CHECK-NEXT:    ret i32 %res
+;
+  %call.1 = tail call i32 @f3_constantexpr_cond(i32 10, i32 %y)
+  %call.2 = tail call i32 @f3_constantexpr_cond(i32 20, i32 %y)
+  %res = and i32 %call.1, %call.2
+  ret i32 %res
+}

diff  --git a/llvm/test/Transforms/SCCP/select.ll b/llvm/test/Transforms/SCCP/select.ll
index b2f1dd2d0f23..9cd700760ec7 100644
--- a/llvm/test/Transforms/SCCP/select.ll
+++ b/llvm/test/Transforms/SCCP/select.ll
@@ -1,12 +1,40 @@
-; RUN: opt < %s -sccp -S | not grep select
+; RUN: opt < %s -sccp -S | FileCheck %s
 
 define i32 @test1(i1 %C) {
+; CHECK-LABEL: define i32 @test1(
+; CHECK-NEXT:   ret i32 0
+;
 	%X = select i1 %C, i32 0, i32 0		; <i32> [#uses=1]
 	ret i32 %X
 }
 
 define i32 @test2(i1 %C) {
+; CHECK-LABEL: define i32 @test2(
+; CHECK-NEXT:   ret i32 0
+;
 	%X = select i1 %C, i32 0, i32 undef		; <i32> [#uses=1]
 	ret i32 %X
 }
 
+define i1 @f2(i32 %x, i1 %cmp) {
+; CHECK-LABEL: define i1 @f2(i32 %x, i1 %cmp) {
+; CHECK-NEXT:    %sel.1 = select i1 %cmp, i32 %x, i32 10
+; CHECK-NEXT:    %c.1 = icmp sgt i32 %sel.1, 300
+; CHECK-NEXT:    %c.2 = icmp sgt i32 %sel.1, 100
+; CHECK-NEXT:    %c.3 = icmp eq i32 %sel.1, 50
+; CHECK-NEXT:    %c.4 = icmp slt i32 %sel.1, 9
+; CHECK-NEXT:    %res.1 = add i1 %c.1, %c.2
+; CHECK-NEXT:    %res.2 = add i1 %res.1, %c.3
+; CHECK-NEXT:    %res.3 = add i1 %res.2, %c.4
+; CHECK-NEXT:    ret i1 %res.3
+;
+  %sel.1 = select i1 %cmp, i32 %x, i32 10
+  %c.1 = icmp sgt i32 %sel.1, 300
+  %c.2 = icmp sgt i32 %sel.1, 100
+  %c.3 = icmp eq i32 %sel.1, 50
+  %c.4 = icmp slt i32 %sel.1, 9
+  %res.1 = add i1 %c.1, %c.2
+  %res.2 = add i1 %res.1, %c.3
+  %res.3 = add i1 %res.2, %c.4
+  ret i1 %res.3
+}


        


More information about the llvm-commits mailing list