Skip to content

Commit 97e6d08

Browse files
committed
Merging r368517, r368518, r368519, and r368554:
------------------------------------------------------------------------ r368517 | lebedevri | 2019-08-10 21:28:12 +0200 (Sat, 10 Aug 2019) | 1 line [NFC][InstCombine] Tests for shift amount reassociation in bittest with shift of const ------------------------------------------------------------------------ ------------------------------------------------------------------------ r368518 | lebedevri | 2019-08-10 21:28:44 +0200 (Sat, 10 Aug 2019) | 5 lines [InstCombine] Shift amount reassociation in bittest: drop pointless one-use restriction That one-use restriction is not needed for correctness - we have already ensured that one of the shifts will go away, so we know we won't increase the instruction count. So there is no need for that restriction. ------------------------------------------------------------------------ ------------------------------------------------------------------------ r368519 | lebedevri | 2019-08-10 21:28:54 +0200 (Sat, 10 Aug 2019) | 5 lines [InstCombine] Shift amount reassociation in bittest: relax one-use check when shifting constant If one of the values being shifted is a constant, since the new shift amount is known-constant, the new shift will end up being constant-folded so, we don't need that one-use restriction then. ------------------------------------------------------------------------ ------------------------------------------------------------------------ r368554 | lebedevri | 2019-08-12 13:28:02 +0200 (Mon, 12 Aug 2019) | 6 lines [InstCombine] foldShiftIntoShiftInAnotherHandOfAndInICmp(): avoid constantexpr pitfail (PR42962) Instead of matching value and then blindly casting to BinaryOperator just to get the opcode, just match instruction and do no cast. Fixes https://bugs.llvm.org/show_bug.cgi?id=42962 ------------------------------------------------------------------------ llvm-svn: 368673
1 parent 2c69b13 commit 97e6d08

File tree

2 files changed

+100
-24
lines changed

2 files changed

+100
-24
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,26 +3288,35 @@ foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ,
32883288

32893289
// Look for an 'and' of two (opposite) logical shifts.
32903290
// Pick the single-use shift as XShift.
3291-
Value *XShift, *YShift;
3291+
Instruction *XShift, *YShift;
32923292
if (!match(I.getOperand(0),
3293-
m_c_And(m_OneUse(m_CombineAnd(m_AnyLogicalShift, m_Value(XShift))),
3294-
m_CombineAnd(m_AnyLogicalShift, m_Value(YShift)))))
3293+
m_c_And(m_CombineAnd(m_AnyLogicalShift, m_Instruction(XShift)),
3294+
m_CombineAnd(m_AnyLogicalShift, m_Instruction(YShift)))))
32953295
return nullptr;
32963296

3297-
// If YShift is a single-use 'lshr', swap the shifts around.
3298-
if (match(YShift, m_OneUse(m_AnyLShr)))
3297+
// If YShift is a 'lshr', swap the shifts around.
3298+
if (match(YShift, m_AnyLShr))
32993299
std::swap(XShift, YShift);
33003300

33013301
// The shifts must be in opposite directions.
3302-
Instruction::BinaryOps XShiftOpcode =
3303-
cast<BinaryOperator>(XShift)->getOpcode();
3304-
if (XShiftOpcode == cast<BinaryOperator>(YShift)->getOpcode())
3302+
auto XShiftOpcode = XShift->getOpcode();
3303+
if (XShiftOpcode == YShift->getOpcode())
33053304
return nullptr; // Do not care about same-direction shifts here.
33063305

33073306
Value *X, *XShAmt, *Y, *YShAmt;
33083307
match(XShift, m_BinOp(m_Value(X), m_Value(XShAmt)));
33093308
match(YShift, m_BinOp(m_Value(Y), m_Value(YShAmt)));
33103309

3310+
// If one of the values being shifted is a constant, then we will end with
3311+
// and+icmp, and shift instr will be constant-folded. If they are not,
3312+
// however, we will need to ensure that we won't increase instruction count.
3313+
if (!isa<Constant>(X) && !isa<Constant>(Y)) {
3314+
// At least one of the hands of the 'and' should be one-use shift.
3315+
if (!match(I.getOperand(0),
3316+
m_c_And(m_OneUse(m_AnyLogicalShift), m_Value())))
3317+
return nullptr;
3318+
}
3319+
33113320
// Can we fold (XShAmt+YShAmt) ?
33123321
Value *NewShAmt = SimplifyBinOp(Instruction::BinaryOps::Add, XShAmt, YShAmt,
33133322
SQ.getWithInstruction(&I));

llvm/test/Transforms/InstCombine/shift-amount-reassociation-in-bittest.ll

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ define i1 @t18_const_oneuse0(i32 %x, i32 %y) {
279279
; CHECK-LABEL: @t18_const_oneuse0(
280280
; CHECK-NEXT: [[T0:%.*]] = lshr i32 [[X:%.*]], 1
281281
; CHECK-NEXT: call void @use32(i32 [[T0]])
282-
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[Y:%.*]], 2
283-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[X]]
282+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X]], 2
283+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[Y:%.*]]
284284
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0
285285
; CHECK-NEXT: ret i1 [[TMP3]]
286286
;
@@ -521,15 +521,70 @@ define i1 @t31_var_oneuse6(i32 %x, i32 %y, i32 %shamt0, i32 %shamt1) {
521521
ret i1 %t3
522522
}
523523

524+
; Shift-of-const
525+
526+
; Ok, non-truncated shift is of constant;
527+
define i1 @t32_shift_of_const_oneuse0(i32 %x, i32 %y, i32 %len) {
528+
; CHECK-LABEL: @t32_shift_of_const_oneuse0(
529+
; CHECK-NEXT: [[T0:%.*]] = sub i32 32, [[LEN:%.*]]
530+
; CHECK-NEXT: call void @use32(i32 [[T0]])
531+
; CHECK-NEXT: [[T1:%.*]] = lshr i32 -52543054, [[T0]]
532+
; CHECK-NEXT: call void @use32(i32 [[T1]])
533+
; CHECK-NEXT: [[T2:%.*]] = add i32 [[LEN]], -1
534+
; CHECK-NEXT: call void @use32(i32 [[T2]])
535+
; CHECK-NEXT: [[T3:%.*]] = shl i32 [[Y:%.*]], [[T2]]
536+
; CHECK-NEXT: call void @use32(i32 [[T3]])
537+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[Y]], 1
538+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 0
539+
; CHECK-NEXT: ret i1 [[TMP2]]
540+
;
541+
%t0 = sub i32 32, %len
542+
call void @use32(i32 %t0)
543+
%t1 = lshr i32 4242424242, %t0 ; shift-of-constant
544+
call void @use32(i32 %t1)
545+
%t2 = add i32 %len, -1
546+
call void @use32(i32 %t2)
547+
%t3 = shl i32 %y, %t2
548+
call void @use32(i32 %t3)
549+
%t4 = and i32 %t1, %t3 ; no extra uses
550+
%t5 = icmp ne i32 %t4, 0
551+
ret i1 %t5
552+
}
553+
; Ok, truncated shift is of constant;
554+
define i1 @t33_shift_of_const_oneuse1(i32 %x, i32 %y, i32 %len) {
555+
; CHECK-LABEL: @t33_shift_of_const_oneuse1(
556+
; CHECK-NEXT: [[T0:%.*]] = sub i32 32, [[LEN:%.*]]
557+
; CHECK-NEXT: call void @use32(i32 [[T0]])
558+
; CHECK-NEXT: [[T1:%.*]] = lshr i32 [[X:%.*]], [[T0]]
559+
; CHECK-NEXT: call void @use32(i32 [[T1]])
560+
; CHECK-NEXT: [[T2:%.*]] = add i32 [[LEN]], -1
561+
; CHECK-NEXT: call void @use32(i32 [[T2]])
562+
; CHECK-NEXT: [[T3:%.*]] = shl i32 -52543054, [[T2]]
563+
; CHECK-NEXT: call void @use32(i32 [[T3]])
564+
; CHECK-NEXT: ret i1 false
565+
;
566+
%t0 = sub i32 32, %len
567+
call void @use32(i32 %t0)
568+
%t1 = lshr i32 %x, %t0 ; shift-of-constant
569+
call void @use32(i32 %t1)
570+
%t2 = add i32 %len, -1
571+
call void @use32(i32 %t2)
572+
%t3 = shl i32 4242424242, %t2
573+
call void @use32(i32 %t3)
574+
%t4 = and i32 %t1, %t3 ; no extra uses
575+
%t5 = icmp ne i32 %t4, 0
576+
ret i1 %t5
577+
}
578+
524579
; Commutativity with extra uses
525580

526-
define i1 @t32_commutativity0_oneuse0(i32 %x) {
527-
; CHECK-LABEL: @t32_commutativity0_oneuse0(
581+
define i1 @t34_commutativity0_oneuse0(i32 %x) {
582+
; CHECK-LABEL: @t34_commutativity0_oneuse0(
528583
; CHECK-NEXT: [[Y:%.*]] = call i32 @gen32()
529584
; CHECK-NEXT: [[T0:%.*]] = lshr i32 [[X:%.*]], 1
530585
; CHECK-NEXT: call void @use32(i32 [[T0]])
531-
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[Y]], 2
532-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[X]]
586+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X]], 2
587+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[Y]]
533588
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0
534589
; CHECK-NEXT: ret i1 [[TMP3]]
535590
;
@@ -541,8 +596,8 @@ define i1 @t32_commutativity0_oneuse0(i32 %x) {
541596
%t3 = icmp ne i32 %t2, 0
542597
ret i1 %t3
543598
}
544-
define i1 @t33_commutativity0_oneuse1(i32 %x) {
545-
; CHECK-LABEL: @t33_commutativity0_oneuse1(
599+
define i1 @t35_commutativity0_oneuse1(i32 %x) {
600+
; CHECK-LABEL: @t35_commutativity0_oneuse1(
546601
; CHECK-NEXT: [[Y:%.*]] = call i32 @gen32()
547602
; CHECK-NEXT: [[T1:%.*]] = shl i32 [[Y]], 1
548603
; CHECK-NEXT: call void @use32(i32 [[T1]])
@@ -560,13 +615,13 @@ define i1 @t33_commutativity0_oneuse1(i32 %x) {
560615
ret i1 %t3
561616
}
562617

563-
define i1 @t34_commutativity1_oneuse0(i32 %y) {
564-
; CHECK-LABEL: @t34_commutativity1_oneuse0(
618+
define i1 @t36_commutativity1_oneuse0(i32 %y) {
619+
; CHECK-LABEL: @t36_commutativity1_oneuse0(
565620
; CHECK-NEXT: [[X:%.*]] = call i32 @gen32()
566621
; CHECK-NEXT: [[T0:%.*]] = lshr i32 [[X]], 1
567622
; CHECK-NEXT: call void @use32(i32 [[T0]])
568-
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[Y:%.*]], 2
569-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[X]]
623+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X]], 2
624+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[Y:%.*]]
570625
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0
571626
; CHECK-NEXT: ret i1 [[TMP3]]
572627
;
@@ -578,8 +633,8 @@ define i1 @t34_commutativity1_oneuse0(i32 %y) {
578633
%t3 = icmp ne i32 %t2, 0
579634
ret i1 %t3
580635
}
581-
define i1 @t35_commutativity1_oneuse1(i32 %y) {
582-
; CHECK-LABEL: @t35_commutativity1_oneuse1(
636+
define i1 @t37_commutativity1_oneuse1(i32 %y) {
637+
; CHECK-LABEL: @t37_commutativity1_oneuse1(
583638
; CHECK-NEXT: [[X:%.*]] = call i32 @gen32()
584639
; CHECK-NEXT: [[T1:%.*]] = shl i32 [[Y:%.*]], 1
585640
; CHECK-NEXT: call void @use32(i32 [[T1]])
@@ -598,8 +653,8 @@ define i1 @t35_commutativity1_oneuse1(i32 %y) {
598653
}
599654

600655
; Negative tests
601-
define <2 x i1> @n36_overshift(<2 x i32> %x, <2 x i32> %y) {
602-
; CHECK-LABEL: @n36_overshift(
656+
define <2 x i1> @n38_overshift(<2 x i32> %x, <2 x i32> %y) {
657+
; CHECK-LABEL: @n38_overshift(
603658
; CHECK-NEXT: [[T0:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 15, i32 1>
604659
; CHECK-NEXT: [[T1:%.*]] = shl <2 x i32> [[Y:%.*]], <i32 17, i32 1>
605660
; CHECK-NEXT: [[T2:%.*]] = and <2 x i32> [[T1]], [[T0]]
@@ -612,3 +667,15 @@ define <2 x i1> @n36_overshift(<2 x i32> %x, <2 x i32> %y) {
612667
%t3 = icmp ne <2 x i32> %t2, <i32 0, i32 0>
613668
ret <2 x i1> %t3
614669
}
670+
671+
; As usual, don't crash given constantexpr's :/
672+
@f.a = internal global i16 0
673+
define i1 @constantexpr() {
674+
entry:
675+
%0 = load i16, i16* @f.a
676+
%shr = ashr i16 %0, 1
677+
%shr1 = ashr i16 %shr, zext (i1 icmp ne (i16 ptrtoint (i16* @f.a to i16), i16 1) to i16)
678+
%and = and i16 %shr1, 1
679+
%tobool = icmp ne i16 %and, 0
680+
ret i1 %tobool
681+
}

0 commit comments

Comments
 (0)