[llvm] a1b2f0c - Reland "[GlobalISel] Fix the infinite loop issue in `commute_int_constant_to_rhs`"
Michael Liao via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 5 08:14:29 PDT 2024
Author: Michael Liao
Date: 2024-04-05T10:34:12-04:00
New Revision: a1b2f0cc447736c27ad3899bb0b17704b79e4876
URL: https://github.com/llvm/llvm-project/commit/a1b2f0cc447736c27ad3899bb0b17704b79e4876
DIFF: https://github.com/llvm/llvm-project/commit/a1b2f0cc447736c27ad3899bb0b17704b79e4876.diff
LOG: 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`.
Added:
llvm/test/CodeGen/AArch64/GlobalISel/combine-commute-const-infinite-loop.mir
Modified:
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index e53e35d98ceca7..719209e0edd5fb 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -6273,14 +6273,15 @@ bool CombinerHelper::matchShiftsTooBig(MachineInstr &MI) {
bool CombinerHelper::matchCommuteConstantToRHS(MachineInstr &MI) {
Register LHS = MI.getOperand(1).getReg();
Register RHS = MI.getOperand(2).getReg();
- auto *LHSDef = MRI.getVRegDef(LHS);
- if (getIConstantVRegVal(LHS, MRI).has_value())
- return true;
-
- // LHS may be a G_CONSTANT_FOLD_BARRIER. If so we commute
- // as long as we don't already have a constant on the RHS.
- if (LHSDef->getOpcode() != TargetOpcode::G_CONSTANT_FOLD_BARRIER)
- return false;
+ if (!getIConstantVRegVal(LHS, MRI)) {
+ // Skip commuting if LHS is not a constant. But, LHS may be a
+ // G_CONSTANT_FOLD_BARRIER. If so we commute as long as we don't already
+ // have a constant on the RHS.
+ if (MRI.getVRegDef(LHS)->getOpcode() !=
+ TargetOpcode::G_CONSTANT_FOLD_BARRIER)
+ return false;
+ }
+ // Commute as long as RHS is not a constant or G_CONSTANT_FOLD_BARRIER.
return MRI.getVRegDef(RHS)->getOpcode() !=
TargetOpcode::G_CONSTANT_FOLD_BARRIER &&
!getIConstantVRegVal(RHS, MRI);
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-commute-const-infinite-loop.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-commute-const-infinite-loop.mir
new file mode 100644
index 00000000000000..6efdc5e5e8c36b
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-commute-const-infinite-loop.mir
@@ -0,0 +1,29 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
+# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner %s -o - \
+# RUN: --aarch64prelegalizercombiner-disable-rule=constant_fold_binop | FileCheck %s
+# REQUIRES: asserts
+
+# `constant_fold_binop` is disabled to trigger the infinite loop in `commute_int_constant_to_rhs`.
+
+---
+name: add
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $s0
+
+ ; CHECK-LABEL: name: add
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %c0:_(s32) = G_CONSTANT i32 1
+ ; CHECK-NEXT: %c1:_(s32) = G_CONSTANT i32 2
+ ; CHECK-NEXT: %add:_(s32) = G_ADD %c0, %c1
+ ; CHECK-NEXT: $s0 = COPY %add(s32)
+ ; CHECK-NEXT: RET_ReallyLR
+ %c0:_(s32) = G_CONSTANT i32 1
+ %c1:_(s32) = G_CONSTANT i32 2
+ %add:_(s32) = G_ADD %c0, %c1
+ $s0 = COPY %add
+ RET_ReallyLR
+
+...
More information about the llvm-commits
mailing list