[llvm] r347896 - [InstSimplify] fold select with implied condition

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 29 10:44:39 PST 2018


Author: spatel
Date: Thu Nov 29 10:44:39 2018
New Revision: 347896

URL: http://llvm.org/viewvc/llvm-project?rev=347896&view=rev
Log:
[InstSimplify] fold select with implied condition

This is an almost direct move of the functionality from InstCombine to 
InstSimplify. There's no reason not to do this in InstSimplify because 
we never create a new value with this transform.

(There's a question of whether any dominance-based transform belongs in
either of these passes, but that's a separate issue.)

I've changed 1 of the conditions for the fold (1 of the blocks for the 
branch must be the block we started with) into an assert because I'm not 
sure how that could ever be false.

We need 1 extra check to make sure that the instruction itself is in a
basic block because passes other than InstCombine may be using InstSimplify
as an analysis on values that are not wired up yet.

The 3-way compare changes show that InstCombine has some kind of 
phase-ordering hole. Otherwise, we would have already gotten the intended
final result that we now show here.

Added:
    llvm/trunk/test/Transforms/InstSimplify/select-implied.ll
      - copied, changed from r347895, llvm/trunk/test/Transforms/InstCombine/select-implied.ll
Removed:
    llvm/trunk/test/Transforms/InstCombine/select-implied.ll
Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/trunk/test/Transforms/InstCombine/unrecognized_three-way-comparison.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=347896&r1=347895&r2=347896&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Thu Nov 29 10:44:39 2018
@@ -3924,6 +3924,42 @@ static Value *simplifySelectWithFCmp(Val
   return nullptr;
 }
 
+/// Try to determine the result of a select based on a dominating condition.
+static Value *foldSelectWithDominatingCond(Value *Cond, Value *TV, Value *FV,
+                                           const SimplifyQuery &Q) {
+  // First, make sure that we have a select in a basic block.
+  // We don't know if we are called from some incomplete state.
+  if (!Q.CxtI || !Q.CxtI->getParent())
+    return nullptr;
+
+  // TODO: This is a poor/cheap way to determine dominance. Should we use the
+  // dominator tree in the SimplifyQuery instead?
+  const BasicBlock *SelectBB = Q.CxtI->getParent();
+  const BasicBlock *PredBB = SelectBB->getSinglePredecessor();
+  if (!PredBB)
+    return nullptr;
+
+  // We need a conditional branch in the predecessor.
+  Value *PredCond;
+  BasicBlock *TrueBB, *FalseBB;
+  if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
+    return nullptr;
+
+  // The branch should get simplified. Don't bother simplifying the select.
+  if (TrueBB == FalseBB)
+    return nullptr;
+
+  assert((TrueBB == SelectBB || FalseBB == SelectBB) &&
+         "Predecessor block does not point to successor?");
+
+  // Is the select condition implied by the predecessor condition?
+  bool CondIsTrue = TrueBB == SelectBB;
+  Optional<bool> Implied = isImpliedCondition(PredCond, Cond, Q.DL, CondIsTrue);
+  if (!Implied)
+    return nullptr;
+  return *Implied ? TV : FV;
+}
+
 /// Given operands for a SelectInst, see if we can fold the result.
 /// If not, this returns null.
 static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
@@ -3966,6 +4002,9 @@ static Value *SimplifySelectInst(Value *
   if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal))
     return V;
 
+  if (Value *V = foldSelectWithDominatingCond(Cond, TrueVal, FalseVal, Q))
+    return V;
+
   return nullptr;
 }
 

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp?rev=347896&r1=347895&r2=347896&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp Thu Nov 29 10:44:39 2018
@@ -2021,24 +2021,6 @@ Instruction *InstCombiner::visitSelectIn
     }
   }
 
-  // See if we can determine the result of this select based on a dominating
-  // condition.
-  BasicBlock *Parent = SI.getParent();
-  if (BasicBlock *Dom = Parent->getSinglePredecessor()) {
-    auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator());
-    if (PBI && PBI->isConditional() &&
-        PBI->getSuccessor(0) != PBI->getSuccessor(1) &&
-        (PBI->getSuccessor(0) == Parent || PBI->getSuccessor(1) == Parent)) {
-      bool CondIsTrue = PBI->getSuccessor(0) == Parent;
-      Optional<bool> Implication = isImpliedCondition(
-          PBI->getCondition(), SI.getCondition(), DL, CondIsTrue);
-      if (Implication) {
-        Value *V = *Implication ? TrueVal : FalseVal;
-        return replaceInstUsesWith(SI, V);
-      }
-    }
-  }
-
   // If we can compute the condition, there's no need for a select.
   // Like the above fold, we are attempting to reduce compile-time cost by
   // putting this fold here with limitations rather than in InstSimplify.

Removed: llvm/trunk/test/Transforms/InstCombine/select-implied.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/select-implied.ll?rev=347895&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/select-implied.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/select-implied.ll (removed)
@@ -1,274 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -instcombine -S | FileCheck %s
-
-; A == B implies A >u B is false.
-
-define void @test1(i32 %a, i32 %b) {
-; CHECK-LABEL: @test1(
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
-; CHECK:       taken:
-; CHECK-NEXT:    call void @foo(i32 10)
-; CHECK-NEXT:    br label [[END]]
-; CHECK:       end:
-; CHECK-NEXT:    ret void
-;
-  %cmp1 = icmp eq i32 %a, %b
-  br i1 %cmp1, label %taken, label %end
-
-taken:
-  %cmp2 = icmp ugt i32 %a, %b
-  %c = select i1 %cmp2, i32 0, i32 10
-  call void @foo(i32 %c)
-  br label %end
-
-end:
-  ret void
-}
-
-; If A == B is false then A != B is true.
-
-define void @test2(i32 %a, i32 %b) {
-; CHECK-LABEL: @test2(
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
-; CHECK:       taken:
-; CHECK-NEXT:    call void @foo(i32 20)
-; CHECK-NEXT:    br label [[END]]
-; CHECK:       end:
-; CHECK-NEXT:    ret void
-;
-  %cmp1 = icmp eq i32 %a, %b
-  br i1 %cmp1, label %end, label %taken
-
-taken:
-  %cmp2 = icmp ne i32 %a, %b
-  %c = select i1 %cmp2, i32 20, i32 0
-  call void @foo(i32 %c)
-  br label %end
-
-end:
-  ret void
-}
-
-; A >u 10 implies A >u 10 is true.
-
-define void @test3(i32 %a) {
-; CHECK-LABEL: @test3(
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 10
-; CHECK-NEXT:    br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
-; CHECK:       taken:
-; CHECK-NEXT:    call void @foo(i32 30)
-; CHECK-NEXT:    br label [[END]]
-; CHECK:       end:
-; CHECK-NEXT:    ret void
-;
-  %cmp1 = icmp ugt i32 %a, 10
-  br i1 %cmp1, label %taken, label %end
-
-taken:
-  %cmp2 = icmp ugt i32 %a, 10
-  %c = select i1 %cmp2, i32 30, i32 0
-  call void @foo(i32 %c)
-  br label %end
-
-end:
-  ret void
-}
-
-define i8 @PR23333(i8 addrspace(1)* %ptr) {
-; CHECK-LABEL: @PR23333(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 addrspace(1)* [[PTR:%.*]], null
-; CHECK-NEXT:    br i1 [[CMP]], label [[TAKEN:%.*]], label [[END:%.*]]
-; CHECK:       taken:
-; CHECK-NEXT:    ret i8 1
-; CHECK:       end:
-; CHECK-NEXT:    ret i8 0
-;
-  %cmp = icmp eq i8 addrspace(1)* %ptr, null
-  br i1 %cmp, label %taken, label %end
-
-taken:
-  %cmp2 = icmp ne i8 addrspace(1)* %ptr, null
-  %res = select i1 %cmp2, i8 2, i8 1
-  ret i8 %res
-
-end:
-  ret i8 0
-}
-
-; We know the condition of the select is true based on a dominating condition.
-; Therefore, we can replace %cond with %len. However, now the inner icmp is
-; always false and can be elided.
-
-define void @test4(i32 %len) {
-; CHECK-LABEL: @test4(
-; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @bar(i32 [[LEN:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[LEN]], 4
-; CHECK-NEXT:    br i1 [[CMP]], label [[BB:%.*]], label [[B1:%.*]]
-; CHECK:       bb:
-; CHECK-NEXT:    br i1 false, label [[B0:%.*]], label [[B1]]
-; CHECK:       b0:
-; CHECK-NEXT:    br label [[B1]]
-; CHECK:       b1:
-; CHECK-NEXT:    [[TMP1:%.*]] = phi i32 [ [[LEN]], [[BB]] ], [ undef, [[B0]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
-; CHECK-NEXT:    br label [[RET:%.*]]
-; CHECK:       ret:
-; CHECK-NEXT:    call void @foo(i32 [[TMP1]])
-; CHECK-NEXT:    ret void
-;
-entry:
-  %0 = call i32 @bar(i32 %len);
-  %cmp = icmp ult i32 %len, 4
-  br i1 %cmp, label %bb, label %b1
-bb:
-  %cond = select i1 %cmp, i32 %len, i32 8
-  %cmp11 = icmp eq i32 %cond, 8
-  br i1 %cmp11, label %b0, label %b1
-
-b0:
-  call void @foo(i32 %len)
-  br label %b1
-
-b1:
-  %1 = phi i32 [ %cond, %bb ], [ undef, %b0 ], [ %0, %entry ]
-  br label %ret
-
-ret:
-  call void @foo(i32 %1)
-  ret void
-}
-
-; A >u 10 implies A >u 9 is true.
-
-define void @test5(i32 %a) {
-; CHECK-LABEL: @test5(
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 10
-; CHECK-NEXT:    br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
-; CHECK:       taken:
-; CHECK-NEXT:    call void @foo(i32 30)
-; CHECK-NEXT:    br label [[END]]
-; CHECK:       end:
-; CHECK-NEXT:    ret void
-;
-  %cmp1 = icmp ugt i32 %a, 10
-  br i1 %cmp1, label %taken, label %end
-
-taken:
-  %cmp2 = icmp ugt i32 %a, 9
-  %c = select i1 %cmp2, i32 30, i32 0
-  call void @foo(i32 %c)
-  br label %end
-
-end:
-  ret void
-}
-
-declare void @foo(i32)
-declare i32 @bar(i32)
-
-define i32 @test_and(i32 %a, i32 %b) {
-; CHECK-LABEL: @test_and(
-; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
-; CHECK-NEXT:    [[CMP2:%.*]] = icmp ne i32 [[B:%.*]], 0
-; CHECK-NEXT:    [[AND:%.*]] = and i1 [[CMP1]], [[CMP2]]
-; CHECK-NEXT:    br i1 [[AND]], label [[TPATH:%.*]], label [[END:%.*]]
-; CHECK:       tpath:
-; CHECK-NEXT:    ret i32 313
-; CHECK:       end:
-; CHECK-NEXT:    ret i32 0
-;
-entry:
-  %cmp1 = icmp ne i32 %a, 0
-  %cmp2 = icmp ne i32 %b, 0
-  %and = and i1 %cmp1, %cmp2
-  br i1 %and, label %tpath, label %end
-
-tpath:
-  %cmp3 = icmp eq i32 %a, 0 ;; <-- implied false
-  %c = select i1 %cmp3, i32 0, i32 313
-  ret i32 %c
-
-end:
-  ret i32 0
-}
-
-; cmp1 and cmp2 are false on the 'fpath' path and thus cmp3 is true.
-
-define i32 @test_or1(i32 %a, i32 %b) {
-; CHECK-LABEL: @test_or1(
-; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], 0
-; CHECK-NEXT:    [[CMP2:%.*]] = icmp eq i32 [[B:%.*]], 0
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[CMP1]], [[CMP2]]
-; CHECK-NEXT:    br i1 [[OR]], label [[END:%.*]], label [[FPATH:%.*]]
-; CHECK:       fpath:
-; CHECK-NEXT:    ret i32 37
-; CHECK:       end:
-; CHECK-NEXT:    ret i32 0
-;
-entry:
-  %cmp1 = icmp eq i32 %a, 0
-  %cmp2 = icmp eq i32 %b, 0
-  %or = or i1 %cmp1, %cmp2
-  br i1 %or, label %end, label %fpath
-
-fpath:
-  %cmp3 = icmp ne i32 %a, 0  ;; <-- implied true
-  %c = select i1 %cmp3, i32 37, i32 0
-  ret i32 %c
-
-end:
-  ret i32 0
-}
-
-; LHS ==> RHS by definition (true -> true)
-
-define void @test6(i32 %a, i32 %b) {
-; CHECK-LABEL: @test6(
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
-; CHECK:       taken:
-; CHECK-NEXT:    call void @foo(i32 10)
-; CHECK-NEXT:    br label [[END]]
-; CHECK:       end:
-; CHECK-NEXT:    ret void
-;
-  %cmp1 = icmp eq i32 %a, %b
-  br i1 %cmp1, label %taken, label %end
-
-taken:
-  %c = select i1 %cmp1, i32 10, i32 0
-  call void @foo(i32 %c)
-  br label %end
-
-end:
-  ret void
-}
-
-; LHS ==> RHS by definition (false -> false)
-
-define void @test7(i32 %a, i32 %b) {
-; CHECK-LABEL: @test7(
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
-; CHECK:       taken:
-; CHECK-NEXT:    call void @foo(i32 11)
-; CHECK-NEXT:    br label [[END]]
-; CHECK:       end:
-; CHECK-NEXT:    ret void
-;
-  %cmp1 = icmp eq i32 %a, %b
-  br i1 %cmp1, label %end, label %taken
-
-taken:
-  %c = select i1 %cmp1, i32 0, i32 11
-  call void @foo(i32 %c)
-  br label %end
-
-end:
-  ret void
-}
-

Modified: llvm/trunk/test/Transforms/InstCombine/unrecognized_three-way-comparison.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/unrecognized_three-way-comparison.ll?rev=347896&r1=347895&r2=347896&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/unrecognized_three-way-comparison.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/unrecognized_three-way-comparison.ll Thu Nov 29 10:44:39 2018
@@ -1,6 +1,4 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; Various patterns of three-ways comparison that are not currently recognized.
-
 ; RUN: opt < %s -instcombine -S | FileCheck %s
 
 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
@@ -8,17 +6,12 @@ target datalayout = "e-p:32:32:32-i1:8:8
 declare void @foo(i32 %x)
 
 define i32 @compare_against_arbitrary_value(i32 %x, i32 %c) {
-; TODO: We can prove that if %x s> %c then %x != c, so there should be no actual
-;       calculations in callfoo block. @foo can be invoked with 1. We only do it
-;       for constants that are not 0 currently while it could be generalized.
 ; CHECK-LABEL: @compare_against_arbitrary_value(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], [[C:%.*]]
 ; CHECK-NEXT:    br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]]
 ; CHECK:       callfoo:
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i32 [[X]], [[C]]
-; CHECK-NEXT:    [[SELECT2:%.*]] = zext i1 [[CMP1]] to i32
-; CHECK-NEXT:    call void @foo(i32 [[SELECT2]])
+; CHECK-NEXT:    call void @foo(i32 1)
 ; CHECK-NEXT:    br label [[EXIT]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret i32 42
@@ -353,17 +346,12 @@ exit:
 }
 
 define i32 @compare_against_arbitrary_value_type_mismatch(i64 %x, i64 %c) {
-; TODO: We can prove that if %x s> %c then %x != c, so there should be no actual
-;       calculations in callfoo block. @foo can be invoked with 1. We only do it
-;       for constants that are not 0 currently while it could be generalized.
 ; CHECK-LABEL: @compare_against_arbitrary_value_type_mismatch(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TMP0:%.*]] = icmp sgt i64 [[X:%.*]], [[C:%.*]]
 ; CHECK-NEXT:    br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]]
 ; CHECK:       callfoo:
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i64 [[X]], [[C]]
-; CHECK-NEXT:    [[SELECT2:%.*]] = zext i1 [[CMP1]] to i32
-; CHECK-NEXT:    call void @foo(i32 [[SELECT2]])
+; CHECK-NEXT:    call void @foo(i32 1)
 ; CHECK-NEXT:    br label [[EXIT]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret i32 42

Copied: llvm/trunk/test/Transforms/InstSimplify/select-implied.ll (from r347895, llvm/trunk/test/Transforms/InstCombine/select-implied.ll)
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/select-implied.ll?p2=llvm/trunk/test/Transforms/InstSimplify/select-implied.ll&p1=llvm/trunk/test/Transforms/InstCombine/select-implied.ll&r1=347895&r2=347896&rev=347896&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/select-implied.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/select-implied.ll Thu Nov 29 10:44:39 2018
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -instcombine -S | FileCheck %s
+; RUN: opt < %s -instsimplify -S | FileCheck %s
 
 ; A == B implies A >u B is false.
 
@@ -98,8 +98,8 @@ end:
 }
 
 ; We know the condition of the select is true based on a dominating condition.
-; Therefore, we can replace %cond with %len. However, now the inner icmp is
-; always false and can be elided.
+; Therefore, we can replace %cond with %len. 
+; TODO: len == 8 is known false in bb. This is handled by other passes, but should it be handled here? 
 
 define void @test4(i32 %len) {
 ; CHECK-LABEL: @test4(
@@ -108,8 +108,10 @@ define void @test4(i32 %len) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[LEN]], 4
 ; CHECK-NEXT:    br i1 [[CMP]], label [[BB:%.*]], label [[B1:%.*]]
 ; CHECK:       bb:
-; CHECK-NEXT:    br i1 false, label [[B0:%.*]], label [[B1]]
+; CHECK-NEXT:    [[CMP11:%.*]] = icmp eq i32 [[LEN]], 8
+; CHECK-NEXT:    br i1 [[CMP11]], label [[B0:%.*]], label [[B1]]
 ; CHECK:       b0:
+; CHECK-NEXT:    call void @foo(i32 [[LEN]])
 ; CHECK-NEXT:    br label [[B1]]
 ; CHECK:       b1:
 ; CHECK-NEXT:    [[TMP1:%.*]] = phi i32 [ [[LEN]], [[BB]] ], [ undef, [[B0]] ], [ [[TMP0]], [[ENTRY:%.*]] ]




More information about the llvm-commits mailing list