[ValueTracking] Add computeKnownBits DemandedElts support to EXTRACTELEMENT/OR/BSWAP/BITREVERSE instructions (PR36319)

These are all covered by the bswap/bitreverse vector tests.
This commit is contained in:
Simon Pilgrim 2020-03-18 18:49:45 +00:00
parent 6f79f80e6e
commit 1010c44b4c
3 changed files with 24 additions and 48 deletions

View File

@ -1128,8 +1128,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
break;
}
case Instruction::Or:
computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
// Output known-0 bits are only known if clear in both the LHS & RHS.
Known.Zero &= Known2.Zero;
@ -1605,12 +1605,12 @@ static void computeKnownBitsFromOperator(const Operator *I,
switch (II->getIntrinsicID()) {
default: break;
case Intrinsic::bitreverse:
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
Known.Zero |= Known2.Zero.reverseBits();
Known.One |= Known2.One.reverseBits();
break;
case Intrinsic::bswap:
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
Known.Zero |= Known2.Zero.byteSwap();
Known.One |= Known2.One.byteSwap();
break;
@ -1762,13 +1762,20 @@ static void computeKnownBitsFromOperator(const Operator *I,
}
break;
}
case Instruction::ExtractElement:
// Look through extract element. At the moment we keep this simple and skip
// tracking the specific element. But at least we might find information
// valid for all elements of the vector (for example if vector is sign
// extended, shifted, etc).
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
case Instruction::ExtractElement: {
// Look through extract element. If the index is non-constant or
// out-of-range demand all elements, otherwise just the extracted element.
auto* EEI = cast<ExtractElementInst>(I);
const Value* Vec = EEI->getVectorOperand();
const Value* Idx = EEI->getIndexOperand();
auto *CIdx = dyn_cast<ConstantInt>(Idx);
unsigned NumElts = Vec->getType()->getVectorNumElements();
APInt DemandedVecElts = APInt::getAllOnesValue(NumElts);
if (CIdx && CIdx->getValue().ult(NumElts))
DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
break;
}
case Instruction::ExtractValue:
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
const ExtractValueInst *EVI = cast<ExtractValueInst>(I);

View File

@ -16,11 +16,7 @@ define i1 @test1(i32 %arg) {
define i1 @test1v(<2 x i32> %arg) {
; CHECK-LABEL: @test1v(
; CHECK-NEXT: [[A:%.*]] = or <2 x i32> [[ARG:%.*]], <i32 1, i32 0>
; CHECK-NEXT: [[B:%.*]] = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> [[A]])
; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i32> [[B]], i32 0
; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[C]], 0
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 false
;
%a = or <2 x i32> %arg, <i32 1, i32 0>
%b = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> %a)
@ -41,11 +37,7 @@ define i1 @test2(i32 %arg) {
define i1 @test2v(<2 x i32> %arg) {
; CHECK-LABEL: @test2v(
; CHECK-NEXT: [[A:%.*]] = or <2 x i32> [[ARG:%.*]], <i32 0, i32 1024>
; CHECK-NEXT: [[B:%.*]] = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> [[A]])
; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i32> [[B]], i32 1
; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[C]], 0
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 false
;
%a = or <2 x i32> %arg, <i32 0, i32 1024>
%b = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> %a)
@ -67,12 +59,7 @@ define i1 @test3(i32 %arg) {
define i1 @test3v(<2 x i32> %arg) {
; CHECK-LABEL: @test3v(
; CHECK-NEXT: [[A:%.*]] = and <2 x i32> [[ARG:%.*]], <i32 1, i32 -1>
; CHECK-NEXT: [[B:%.*]] = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> [[A]])
; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[B]], <i32 1, i32 -1>
; CHECK-NEXT: [[EXT:%.*]] = extractelement <2 x i32> [[AND]], i32 0
; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[EXT]], 1
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 false
;
%a = and <2 x i32> %arg, <i32 1, i32 -1>
%b = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> %a)

View File

@ -16,11 +16,7 @@ define i1 @test1(i16 %arg) {
define i1 @test1v(<2 x i16> %arg) {
; CHECK-LABEL: @test1v(
; CHECK-NEXT: [[A:%.*]] = or <2 x i16> [[ARG:%.*]], <i16 1, i16 0>
; CHECK-NEXT: [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i16> [[B]], i32 0
; CHECK-NEXT: [[RES:%.*]] = icmp eq i16 [[C]], 0
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 false
;
%a = or <2 x i16> %arg, <i16 1, i16 0>
%b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)
@ -41,11 +37,7 @@ define i1 @test2(i16 %arg) {
define i1 @test2v(<2 x i16> %arg) {
; CHECK-LABEL: @test2v(
; CHECK-NEXT: [[A:%.*]] = or <2 x i16> [[ARG:%.*]], <i16 0, i16 1024>
; CHECK-NEXT: [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i16> [[B]], i32 1
; CHECK-NEXT: [[RES:%.*]] = icmp eq i16 [[C]], 0
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 false
;
%a = or <2 x i16> %arg, <i16 0, i16 1024>
%b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)
@ -67,12 +59,7 @@ define i1 @test3(i16 %arg) {
define i1 @test3v(<2 x i16> %arg) {
; CHECK-LABEL: @test3v(
; CHECK-NEXT: [[A:%.*]] = and <2 x i16> [[ARG:%.*]], <i16 1, i16 -1>
; CHECK-NEXT: [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i16> [[B]], i32 0
; CHECK-NEXT: [[AND:%.*]] = and i16 [[C]], 1
; CHECK-NEXT: [[RES:%.*]] = icmp eq i16 [[AND]], 1
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 false
;
%a = and <2 x i16> %arg, <i16 1, i16 -1>
%b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)
@ -95,12 +82,7 @@ define i1 @test4(i16 %arg) {
define i1 @test4v(<2 x i16> %arg) {
; CHECK-LABEL: @test4v(
; CHECK-NEXT: [[A:%.*]] = and <2 x i16> [[ARG:%.*]], <i16 511, i16 511>
; CHECK-NEXT: [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
; CHECK-NEXT: [[AND:%.*]] = and <2 x i16> [[B]], <i16 255, i16 256>
; CHECK-NEXT: [[EXT:%.*]] = extractelement <2 x i16> [[AND]], i32 1
; CHECK-NEXT: [[RES:%.*]] = icmp eq i16 [[EXT]], 1
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 false
;
%a = and <2 x i16> %arg, <i16 511, i16 511>
%b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)