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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274465 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjay Patel 2016-07-03 14:34:39 +00:00
parent b7dea04f9b
commit a9403e9f0e
2 changed files with 19 additions and 13 deletions

View File

@ -918,9 +918,11 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
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::visitSelectInst(SelectInst &SI) {
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);
}

View File

@ -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>