Skip to content

Commit a1b2f0c

Browse files
committed
Reland "[GlobalISel] Fix the infinite loop issue in commute_int_constant_to_rhs"
- That test needs to disable combine rules by name and hence requires `asserts`.
1 parent f905935 commit a1b2f0c

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6273,14 +6273,15 @@ bool CombinerHelper::matchShiftsTooBig(MachineInstr &MI) {
62736273
bool CombinerHelper::matchCommuteConstantToRHS(MachineInstr &MI) {
62746274
Register LHS = MI.getOperand(1).getReg();
62756275
Register RHS = MI.getOperand(2).getReg();
6276-
auto *LHSDef = MRI.getVRegDef(LHS);
6277-
if (getIConstantVRegVal(LHS, MRI).has_value())
6278-
return true;
6279-
6280-
// LHS may be a G_CONSTANT_FOLD_BARRIER. If so we commute
6281-
// as long as we don't already have a constant on the RHS.
6282-
if (LHSDef->getOpcode() != TargetOpcode::G_CONSTANT_FOLD_BARRIER)
6283-
return false;
6276+
if (!getIConstantVRegVal(LHS, MRI)) {
6277+
// Skip commuting if LHS is not a constant. But, LHS may be a
6278+
// G_CONSTANT_FOLD_BARRIER. If so we commute as long as we don't already
6279+
// have a constant on the RHS.
6280+
if (MRI.getVRegDef(LHS)->getOpcode() !=
6281+
TargetOpcode::G_CONSTANT_FOLD_BARRIER)
6282+
return false;
6283+
}
6284+
// Commute as long as RHS is not a constant or G_CONSTANT_FOLD_BARRIER.
62846285
return MRI.getVRegDef(RHS)->getOpcode() !=
62856286
TargetOpcode::G_CONSTANT_FOLD_BARRIER &&
62866287
!getIConstantVRegVal(RHS, MRI);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
2+
# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner %s -o - \
3+
# RUN: --aarch64prelegalizercombiner-disable-rule=constant_fold_binop | FileCheck %s
4+
# REQUIRES: asserts
5+
6+
# `constant_fold_binop` is disabled to trigger the infinite loop in `commute_int_constant_to_rhs`.
7+
8+
---
9+
name: add
10+
tracksRegLiveness: true
11+
body: |
12+
bb.0:
13+
liveins: $s0
14+
15+
; CHECK-LABEL: name: add
16+
; CHECK: liveins: $s0
17+
; CHECK-NEXT: {{ $}}
18+
; CHECK-NEXT: %c0:_(s32) = G_CONSTANT i32 1
19+
; CHECK-NEXT: %c1:_(s32) = G_CONSTANT i32 2
20+
; CHECK-NEXT: %add:_(s32) = G_ADD %c0, %c1
21+
; CHECK-NEXT: $s0 = COPY %add(s32)
22+
; CHECK-NEXT: RET_ReallyLR
23+
%c0:_(s32) = G_CONSTANT i32 1
24+
%c1:_(s32) = G_CONSTANT i32 2
25+
%add:_(s32) = G_ADD %c0, %c1
26+
$s0 = COPY %add
27+
RET_ReallyLR
28+
29+
...

0 commit comments

Comments
 (0)