Skip to content

Commit 1f01c58

Browse files
authored
[GlobalISel] Fix the infinite loop issue in commute_int_constant_to_rhs
- When both operands are constant, the matcher runs into an infinite loop as the commutation should be applied only when LHS is a constant and RHS is not. Reviewers: arsenm Reviewed By: arsenm Pull Request: #87426
1 parent abd05eb commit 1f01c58

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6276,14 +6276,15 @@ bool CombinerHelper::matchShiftsTooBig(MachineInstr &MI) {
62766276
bool CombinerHelper::matchCommuteConstantToRHS(MachineInstr &MI) {
62776277
Register LHS = MI.getOperand(1).getReg();
62786278
Register RHS = MI.getOperand(2).getReg();
6279-
auto *LHSDef = MRI.getVRegDef(LHS);
6280-
if (getIConstantVRegVal(LHS, MRI).has_value())
6281-
return true;
6282-
6283-
// LHS may be a G_CONSTANT_FOLD_BARRIER. If so we commute
6284-
// as long as we don't already have a constant on the RHS.
6285-
if (LHSDef->getOpcode() != TargetOpcode::G_CONSTANT_FOLD_BARRIER)
6286-
return false;
6279+
if (!getIConstantVRegVal(LHS, MRI)) {
6280+
// Skip commuting if LHS is not a constant. But, LHS may be a
6281+
// G_CONSTANT_FOLD_BARRIER. If so we commute as long as we don't already
6282+
// have a constant on the RHS.
6283+
if (MRI.getVRegDef(LHS)->getOpcode() !=
6284+
TargetOpcode::G_CONSTANT_FOLD_BARRIER)
6285+
return false;
6286+
}
6287+
// Commute as long as RHS is not a constant or G_CONSTANT_FOLD_BARRIER.
62876288
return MRI.getVRegDef(RHS)->getOpcode() !=
62886289
TargetOpcode::G_CONSTANT_FOLD_BARRIER &&
62896290
!getIConstantVRegVal(RHS, MRI);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
5+
# `constant_fold_binop` is disabled to trigger the infinite loop in `commute_int_constant_to_rhs`.
6+
7+
---
8+
name: add
9+
tracksRegLiveness: true
10+
body: |
11+
bb.0:
12+
liveins: $s0
13+
14+
; CHECK-LABEL: name: add
15+
; CHECK: liveins: $s0
16+
; CHECK-NEXT: {{ $}}
17+
; CHECK-NEXT: %c0:_(s32) = G_CONSTANT i32 1
18+
; CHECK-NEXT: %c1:_(s32) = G_CONSTANT i32 2
19+
; CHECK-NEXT: %add:_(s32) = G_ADD %c0, %c1
20+
; CHECK-NEXT: $s0 = COPY %add(s32)
21+
; CHECK-NEXT: RET_ReallyLR
22+
%c0:_(s32) = G_CONSTANT i32 1
23+
%c1:_(s32) = G_CONSTANT i32 2
24+
%add:_(s32) = G_ADD %c0, %c1
25+
$s0 = COPY %add
26+
RET_ReallyLR
27+
28+
...

0 commit comments

Comments
 (0)