Skip to content

Commit c2658cf

Browse files
committed
[InstCombine] Fix a crash in getSelectCondition if we happen to have two inverse vectors of i1 constants.
We used to try to truncate the constant vector to vXi1, but if it's already i1 this would fail. Instead we now use IRBuilder::getZExtOrTrunc which should check the type and only create a trunc if needed. I believe this should trigger constant folding in the IRBuilder and ultimately do the same thing just with the additional type check. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310639 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 992d9d3 commit c2658cf

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,8 +1494,9 @@ static Value *getSelectCondition(Value *A, Value *B,
14941494
// If both operands are constants, see if the constants are inverse bitmasks.
14951495
Constant *AC, *BC;
14961496
if (match(A, m_Constant(AC)) && match(B, m_Constant(BC)) &&
1497-
areInverseVectorBitmasks(AC, BC))
1498-
return ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1497+
areInverseVectorBitmasks(AC, BC)) {
1498+
return Builder.CreateZExtOrTrunc(AC, CmpInst::makeCmpResultType(Ty));
1499+
}
14991500

15001501
// If both operands are xor'd with constants using the same sexted boolean
15011502
// operand, see if the constants are inverse bitmasks.

test/Transforms/InstCombine/or.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,15 @@ define i1 @orn_and_cmp_4(i32 %a, i32 %b, i32 %c) {
828828
%or = or i1 %and, %x_inv
829829
ret i1 %or
830830
}
831+
832+
; The constant vectors are inverses. Make sure we can turn this into a select without crashing trying to truncate the constant to 16xi1.
833+
define <16 x i1> @test51(<16 x i1> %arg, <16 x i1> %arg1) {
834+
; CHECK-LABEL: @test51(
835+
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <16 x i1> [[ARG:%.*]], <16 x i1> [[ARG1:%.*]], <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 20, i32 5, i32 6, i32 23, i32 24, i32 9, i32 10, i32 27, i32 28, i32 29, i32 30, i32 31>
836+
; CHECK-NEXT: ret <16 x i1> [[TMP1]]
837+
;
838+
%tmp = and <16 x i1> %arg, <i1 true, i1 true, i1 true, i1 true, i1 false, i1 true, i1 true, i1 false, i1 false, i1 true, i1 true, i1 false, i1 false, i1 false, i1 false, i1 false>
839+
%tmp2 = and <16 x i1> %arg1, <i1 false, i1 false, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 true, i1 false, i1 false, i1 true, i1 true, i1 true, i1 true, i1 true>
840+
%tmp3 = or <16 x i1> %tmp, %tmp2
841+
ret <16 x i1> %tmp3
842+
}

0 commit comments

Comments
 (0)