[llvm] r274465 - [InstCombine] enable vector select of bools -> logic folds

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 3 07:34:39 PDT 2016


Author: spatel
Date: Sun Jul  3 09:34:39 2016
New Revision: 274465

URL: http://llvm.org/viewvc/llvm-project?rev=274465&view=rev
Log:
[InstCombine] enable vector select of bools -> logic folds

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/trunk/test/Transforms/InstCombine/select.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp?rev=274465&r1=274464&r2=274465&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp Sun Jul  3 09:34:39 2016
@@ -918,9 +918,11 @@ Instruction *InstCombiner::visitSelectIn
           SimplifySelectInst(CondVal, TrueVal, FalseVal, DL, TLI, DT, AC))
     return replaceInstUsesWith(SI, V);
 
-  if (SI.getType()->isIntegerTy(1)) {
-    if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
-      if (C->getZExtValue()) {
+  if (SI.getType()->getScalarType()->isIntegerTy(1) &&
+      TrueVal->getType() == CondVal->getType()) {
+    const APInt *TrueC;
+    if (match(TrueVal, m_APInt(TrueC))) {
+      if (TrueC->isAllOnesValue()) {
         // Change: A = select B, true, C --> A = or B, C
         return BinaryOperator::CreateOr(CondVal, FalseVal);
       }
@@ -928,8 +930,9 @@ Instruction *InstCombiner::visitSelectIn
       Value *NotCond = Builder->CreateNot(CondVal, "not." + CondVal->getName());
       return BinaryOperator::CreateAnd(NotCond, FalseVal);
     }
-    if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
-      if (!C->getZExtValue()) {
+    const APInt *FalseC;
+    if (match(FalseVal, m_APInt(FalseC))) {
+      if (*FalseC == 0) {
         // Change: A = select B, C, false --> A = and B, C
         return BinaryOperator::CreateAnd(CondVal, TrueVal);
       }

Modified: llvm/trunk/test/Transforms/InstCombine/select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/select.ll?rev=274465&r1=274464&r2=274465&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/select.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/select.ll Sun Jul  3 09:34:39 2016
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -instcombine -S | FileCheck %s
 
 ; PR1822
@@ -64,7 +65,7 @@ define i1 @test7(i1 %C, i1 %X) {
 
 define <2 x i1> @test7vec(<2 x i1> %C, <2 x i1> %X) {
 ; CHECK-LABEL: @test7vec(
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> %C, <2 x i1> <i1 true, i1 true>, <2 x i1> %X
+; CHECK-NEXT:    [[R:%.*]] = or <2 x i1> %C, %X
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %R = select <2 x i1> %C, <2 x i1> <i1 true, i1 true>, <2 x i1> %X
@@ -82,7 +83,7 @@ define i1 @test8(i1 %C, i1 %X) {
 
 define <2 x i1> @test8vec(<2 x i1> %C, <2 x i1> %X) {
 ; CHECK-LABEL: @test8vec(
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> %C, <2 x i1> %X, <2 x i1> zeroinitializer
+; CHECK-NEXT:    [[R:%.*]] = and <2 x i1> %C, %X
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 false, i1 false>
@@ -101,7 +102,8 @@ define i1 @test9(i1 %C, i1 %X) {
 
 define <2 x i1> @test9vec(<2 x i1> %C, <2 x i1> %X) {
 ; CHECK-LABEL: @test9vec(
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> %C, <2 x i1> zeroinitializer, <2 x i1> %X
+; CHECK-NEXT:    [[NOT_C:%.*]] = xor <2 x i1> %C, <i1 true, i1 true>
+; CHECK-NEXT:    [[R:%.*]] = and <2 x i1> [[NOT_C]], %X
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %R = select <2 x i1> %C, <2 x i1> <i1 false, i1 false>, <2 x i1> %X
@@ -120,7 +122,8 @@ define i1 @test10(i1 %C, i1 %X) {
 
 define <2 x i1> @test10vec(<2 x i1> %C, <2 x i1> %X) {
 ; CHECK-LABEL: @test10vec(
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 true, i1 true>
+; CHECK-NEXT:    [[NOT_C:%.*]] = xor <2 x i1> %C, <i1 true, i1 true>
+; CHECK-NEXT:    [[R:%.*]] = or <2 x i1> [[NOT_C]], %X
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %R = select <2 x i1> %C, <2 x i1> %X, <2 x i1> <i1 true, i1 true>
@@ -138,7 +141,7 @@ define i1 @test23(i1 %a, i1 %b) {
 
 define <2 x i1> @test23vec(<2 x i1> %a, <2 x i1> %b) {
 ; CHECK-LABEL: @test23vec(
-; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> %a, <2 x i1> %b, <2 x i1> %a
+; CHECK-NEXT:    [[C:%.*]] = and <2 x i1> %a, %b
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %c = select <2 x i1> %a, <2 x i1> %b, <2 x i1> %a
@@ -156,7 +159,7 @@ define i1 @test24(i1 %a, i1 %b) {
 
 define <2 x i1> @test24vec(<2 x i1> %a, <2 x i1> %b) {
 ; CHECK-LABEL: @test24vec(
-; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> %a, <2 x i1> %a, <2 x i1> %b
+; CHECK-NEXT:    [[C:%.*]] = or <2 x i1> %a, %b
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %c = select <2 x i1> %a, <2 x i1> %a, <2 x i1> %b
@@ -177,7 +180,7 @@ define i1 @test62(i1 %A, i1 %B) {
 define <2 x i1> @test62vec(<2 x i1> %A, <2 x i1> %B) {
 ; CHECK-LABEL: @test62vec(
 ; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i1> %A, <i1 true, i1 true>
-; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> %A, <2 x i1> [[NOT]], <2 x i1> %B
+; CHECK-NEXT:    [[C:%.*]] = and <2 x i1> [[NOT]], %B
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %not = xor <2 x i1> %A, <i1 true, i1 true>
@@ -199,7 +202,7 @@ define i1 @test63(i1 %A, i1 %B) {
 define <2 x i1> @test63vec(<2 x i1> %A, <2 x i1> %B) {
 ; CHECK-LABEL: @test63vec(
 ; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i1> %A, <i1 true, i1 true>
-; CHECK-NEXT:    [[C:%.*]] = select <2 x i1> %A, <2 x i1> %B, <2 x i1> [[NOT]]
+; CHECK-NEXT:    [[C:%.*]] = or <2 x i1> %B, [[NOT]]
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %not = xor <2 x i1> %A, <i1 true, i1 true>




More information about the llvm-commits mailing list