Skip to content

Commit ed23609

Browse files
committed
[PatternMatch] Do not match constant expressions for binops
Currently, m_Mul() style matchers also match constant expressions. This is a regular source of assertion failures (usually by trying to do a match and then cast to Instruction or BinaryOperator) and infinite combine loops. At the same time, I don't think this provides useful optimization capabilities (all of the tests affected here are regression tests for crashes / infinite loops). Long term, all of these constant expressions (apart from possibly add/sub) are slated for removal per https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179 -- but doing those removals can itself expose new crashes and infinite loops due to the current PatternMatch behavior. Differential Revision: https://reviews.llvm.org/D156401
1 parent ff80fc0 commit ed23609

File tree

6 files changed

+15
-25
lines changed

6 files changed

+15
-25
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -972,11 +972,6 @@ struct BinaryOp_match {
972972
(Commutable && L.match(I->getOperand(1)) &&
973973
R.match(I->getOperand(0)));
974974
}
975-
if (auto *CE = dyn_cast<ConstantExpr>(V))
976-
return CE->getOpcode() == Opc &&
977-
((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
978-
(Commutable && L.match(CE->getOperand(1)) &&
979-
R.match(CE->getOperand(0))));
980975
return false;
981976
}
982977

@@ -1258,9 +1253,6 @@ struct BinOpPred_match : Predicate {
12581253
if (auto *I = dyn_cast<Instruction>(V))
12591254
return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
12601255
R.match(I->getOperand(1));
1261-
if (auto *CE = dyn_cast<ConstantExpr>(V))
1262-
return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
1263-
R.match(CE->getOperand(1));
12641256
return false;
12651257
}
12661258
};

llvm/test/Transforms/InstCombine/mul-inseltpoison.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ define i64 @test30(i32 %A, i32 %B) {
570570
@PR22087 = external global i32
571571
define i32 @test31(i32 %V) {
572572
; CHECK-LABEL: @test31(
573-
; CHECK-NEXT: [[MUL1:%.*]] = shl i32 [[V:%.*]], zext (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr @PR22087) to i32)
574-
; CHECK-NEXT: ret i32 [[MUL1]]
573+
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[V:%.*]], shl (i32 1, i32 zext (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr @PR22087) to i32))
574+
; CHECK-NEXT: ret i32 [[MUL]]
575575
;
576576
%mul = mul i32 %V, shl (i32 1, i32 zext (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr @PR22087) to i32))
577577
ret i32 %mul
@@ -686,8 +686,7 @@ define i64 @test_mul_canonicalize_neg_is_not_undone(i64 %L1) {
686686
; Check we do not undo the canonicalization of 0 - (X * Y), if Y is a constant
687687
; expr.
688688
; CHECK-LABEL: @test_mul_canonicalize_neg_is_not_undone(
689-
; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[L1:%.*]], ptrtoint (ptr @X to i64)
690-
; CHECK-NEXT: [[B4:%.*]] = sub i64 0, [[TMP1]]
689+
; CHECK-NEXT: [[B4:%.*]] = mul i64 [[L1:%.*]], sub (i64 0, i64 ptrtoint (ptr @X to i64))
691690
; CHECK-NEXT: ret i64 [[B4]]
692691
;
693692
%v1 = ptrtoint ptr @X to i64

llvm/test/Transforms/InstCombine/mul.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,8 @@ define i64 @test30(i32 %A, i32 %B) {
11311131
@PR22087 = external global i32
11321132
define i32 @test31(i32 %V) {
11331133
; CHECK-LABEL: @test31(
1134-
; CHECK-NEXT: [[MUL1:%.*]] = shl i32 [[V:%.*]], zext (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr @PR22087) to i32)
1135-
; CHECK-NEXT: ret i32 [[MUL1]]
1134+
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[V:%.*]], shl (i32 1, i32 zext (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr @PR22087) to i32))
1135+
; CHECK-NEXT: ret i32 [[MUL]]
11361136
;
11371137
%mul = mul i32 %V, shl (i32 1, i32 zext (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr @PR22087) to i32))
11381138
ret i32 %mul
@@ -1247,8 +1247,7 @@ define i64 @test_mul_canonicalize_neg_is_not_undone(i64 %L1) {
12471247
; Check we do not undo the canonicalization of 0 - (X * Y), if Y is a constant
12481248
; expr.
12491249
; CHECK-LABEL: @test_mul_canonicalize_neg_is_not_undone(
1250-
; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[L1:%.*]], ptrtoint (ptr @X to i64)
1251-
; CHECK-NEXT: [[B4:%.*]] = sub i64 0, [[TMP1]]
1250+
; CHECK-NEXT: [[B4:%.*]] = mul i64 [[L1:%.*]], sub (i64 0, i64 ptrtoint (ptr @X to i64))
12521251
; CHECK-NEXT: ret i64 [[B4]]
12531252
;
12541253
%v1 = ptrtoint ptr @X to i64

llvm/test/Transforms/InstCombine/pr32686.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ define void @tinkywinky() {
88
; CHECK-LABEL: @tinkywinky(
99
; CHECK-NEXT: [[PATATINO:%.*]] = load i8, ptr @a, align 1
1010
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[PATATINO]], 0
11-
; CHECK-NEXT: [[TMP1:%.*]] = zext i1 [[TOBOOL_NOT]] to i32
12-
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], or (i32 zext (i1 icmp ne (ptr @a, ptr @b) to i32), i32 2)
13-
; CHECK-NEXT: store i32 [[OR1]], ptr @b, align 4
11+
; CHECK-NEXT: [[LNOT_EXT:%.*]] = zext i1 [[TOBOOL_NOT]] to i32
12+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[LNOT_EXT]], xor (i32 zext (i1 icmp ne (ptr @a, ptr @b) to i32), i32 2)
13+
; CHECK-NEXT: store i32 [[OR]], ptr @b, align 4
1414
; CHECK-NEXT: ret void
1515
;
1616
%patatino = load i8, ptr @a

llvm/test/Transforms/InstCombine/switch-constant-expr.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
; PR30486
77
define i32 @single_case() {
88
; CHECK-LABEL: @single_case(
9-
; CHECK-NEXT: switch i32 ptrtoint (ptr @g to i32), label [[X:%.*]] [
9+
; CHECK-NEXT: switch i32 add (i32 ptrtoint (ptr @g to i32), i32 -1), label [[X:%.*]] [
1010
; CHECK-NEXT: ]
1111
; CHECK: x:
1212
; CHECK-NEXT: ret i32 0
@@ -18,9 +18,9 @@ x:
1818

1919
define i32 @multiple_cases() {
2020
; CHECK-LABEL: @multiple_cases(
21-
; CHECK-NEXT: switch i32 ptrtoint (ptr @g to i32), label [[X:%.*]] [
22-
; CHECK-NEXT: i32 2, label [[ONE:%.*]]
23-
; CHECK-NEXT: i32 3, label [[TWO:%.*]]
21+
; CHECK-NEXT: switch i32 add (i32 ptrtoint (ptr @g to i32), i32 -1), label [[X:%.*]] [
22+
; CHECK-NEXT: i32 1, label [[ONE:%.*]]
23+
; CHECK-NEXT: i32 2, label [[TWO:%.*]]
2424
; CHECK-NEXT: ]
2525
; CHECK: x:
2626
; CHECK-NEXT: ret i32 0

llvm/test/Transforms/InstCombine/udiv-simplify.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ define i64 @test2_PR2274(i32 %x, i32 %v) nounwind {
5555
define i32 @PR30366(i1 %a) {
5656
; CHECK-LABEL: @PR30366(
5757
; CHECK-NEXT: [[Z:%.*]] = zext i1 [[A:%.*]] to i32
58-
; CHECK-NEXT: [[D1:%.*]] = lshr i32 [[Z]], zext (i16 ptrtoint (ptr @b to i16) to i32)
59-
; CHECK-NEXT: ret i32 [[D1]]
58+
; CHECK-NEXT: [[D:%.*]] = udiv i32 [[Z]], zext (i16 shl (i16 1, i16 ptrtoint (ptr @b to i16)) to i32)
59+
; CHECK-NEXT: ret i32 [[D]]
6060
;
6161
%z = zext i1 %a to i32
6262
%d = udiv i32 %z, zext (i16 shl (i16 1, i16 ptrtoint (ptr @b to i16)) to i32)

0 commit comments

Comments
 (0)