[llvm] c95ed6e - [GlobalISel] Try to commute G_CONSTANT_FOLD_BARRIER LHS operands to RHS.
Amara Emerson via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 30 08:12:17 PDT 2023
Author: Amara Emerson
Date: 2023-08-30T08:07:22-07:00
New Revision: c95ed6e492c7580762c2ee7d5eb907b98b2f3ad6
URL: https://github.com/llvm/llvm-project/commit/c95ed6e492c7580762c2ee7d5eb907b98b2f3ad6
DIFF: https://github.com/llvm/llvm-project/commit/c95ed6e492c7580762c2ee7d5eb907b98b2f3ad6.diff
LOG: [GlobalISel] Try to commute G_CONSTANT_FOLD_BARRIER LHS operands to RHS.
Differential Revision: https://reviews.llvm.org/D159097
Added:
llvm/test/CodeGen/AArch64/GlobalISel/combine-const-fold-barrier-rhs.mir
Modified:
llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
llvm/include/llvm/Target/GlobalISel/Combine.td
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h b/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
index 19c058dacd3ba3..4b10ca3b7eb69a 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
@@ -784,6 +784,9 @@ class CombinerHelper {
/// Match shifts greater or equal to the bitwidth of the operation.
bool matchShiftsTooBig(MachineInstr &MI);
+ /// Match constant LHS ops that should be commuted.
+ bool matchCommuteConstantToRHS(MachineInstr &MI);
+
private:
/// Given a non-indexed load or store instruction \p MI, find an offset that
/// can be usefully and legally folded into it as a post-indexing operation.
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index 2858f3f177f6f5..0f5ef30b8dca5b 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -389,9 +389,8 @@ def select_to_logical : GICombineRule<
// TODO: handle compares (currently not marked as isCommutable)
def commute_constant_to_rhs : GICombineRule<
(defs root:$root),
- (match (wip_match_opcode G_ADD, G_MUL, G_AND, G_OR, G_XOR):$root, [{
- return getIConstantVRegVal(${root}->getOperand(1).getReg(), MRI).has_value();
- }]),
+ (match (wip_match_opcode G_ADD, G_MUL, G_AND, G_OR, G_XOR):$root,
+ [{ return Helper.matchCommuteConstantToRHS(*${root}); }]),
(apply [{
Observer.changingInstr(*${root});
Register LHSReg = ${root}->getOperand(1).getReg();
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index 5faa168a7a35ec..10ea0ce2b23391 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -6013,6 +6013,22 @@ bool CombinerHelper::matchShiftsTooBig(MachineInstr &MI) {
return matchUnaryPredicate(MRI, ShiftReg, IsShiftTooBig);
}
+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;
+ return MRI.getVRegDef(RHS)->getOpcode() !=
+ TargetOpcode::G_CONSTANT_FOLD_BARRIER &&
+ !getIConstantVRegVal(RHS, MRI);
+}
+
bool CombinerHelper::tryCombine(MachineInstr &MI) {
if (tryCombineCopy(MI))
return true;
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-const-fold-barrier-rhs.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-const-fold-barrier-rhs.mir
new file mode 100644
index 00000000000000..01e0dce5a661cb
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-const-fold-barrier-rhs.mir
@@ -0,0 +1,80 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
+# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s
+---
+name: cfb_lhs
+tracksRegLiveness: true
+body: |
+ bb.1:
+ liveins: $w0
+
+ ; CHECK-LABEL: name: cfb_lhs
+ ; CHECK: liveins: $w0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $w0
+ ; CHECK-NEXT: %cst:_(s32) = G_CONSTANT i32 1
+ ; CHECK-NEXT: %cfb:_(s32) = G_CONSTANT_FOLD_BARRIER %cst
+ ; CHECK-NEXT: %add:_(s32) = G_ADD [[COPY]], %cfb
+ ; CHECK-NEXT: $w0 = COPY %add(s32)
+ ; CHECK-NEXT: RET_ReallyLR
+ %0:_(s32) = COPY $w0
+ %cst:_(s32) = G_CONSTANT i32 1
+ %cfb:_(s32) = G_CONSTANT_FOLD_BARRIER %cst
+ %cst2:_(s32) = G_CONSTANT i32 2
+ %add:_(s32) = G_ADD %cfb, %0
+ $w0 = COPY %add
+ RET_ReallyLR
+
+...
+---
+name: cfb_lhs_cfb_already_rhs
+tracksRegLiveness: true
+body: |
+ bb.1:
+ liveins: $w0
+
+ ; CHECK-LABEL: name: cfb_lhs_cfb_already_rhs
+ ; CHECK: liveins: $w0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %cst:_(s32) = G_CONSTANT i32 1
+ ; CHECK-NEXT: %cfb:_(s32) = G_CONSTANT_FOLD_BARRIER %cst
+ ; CHECK-NEXT: %cst2:_(s32) = G_CONSTANT i32 2
+ ; CHECK-NEXT: %cfb2:_(s32) = G_CONSTANT_FOLD_BARRIER %cst2
+ ; CHECK-NEXT: %add:_(s32) = G_ADD %cfb, %cfb2
+ ; CHECK-NEXT: $w0 = COPY %add(s32)
+ ; CHECK-NEXT: RET_ReallyLR
+ %0:_(s32) = COPY $w0
+ %cst:_(s32) = G_CONSTANT i32 1
+ %cfb:_(s32) = G_CONSTANT_FOLD_BARRIER %cst
+ %cst2:_(s32) = G_CONSTANT i32 2
+ %cfb2:_(s32) = G_CONSTANT_FOLD_BARRIER %cst2
+ %add:_(s32) = G_ADD %cfb, %cfb2
+ $w0 = COPY %add
+ RET_ReallyLR
+
+...
+---
+name: cfb_lhs_cst_on_rhs
+alignment: 4
+tracksRegLiveness: true
+body: |
+ bb.1:
+ liveins: $w0
+
+ ; CHECK-LABEL: name: cfb_lhs_cst_on_rhs
+ ; CHECK: liveins: $w0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %cst:_(s32) = G_CONSTANT i32 1
+ ; CHECK-NEXT: %cfb:_(s32) = G_CONSTANT_FOLD_BARRIER %cst
+ ; CHECK-NEXT: %cst2:_(s32) = G_CONSTANT i32 2
+ ; CHECK-NEXT: %add:_(s32) = G_ADD %cfb, %cst2
+ ; CHECK-NEXT: $w0 = COPY %add(s32)
+ ; CHECK-NEXT: RET_ReallyLR
+ %0:_(s32) = COPY $w0
+ %cst:_(s32) = G_CONSTANT i32 1
+ %cfb:_(s32) = G_CONSTANT_FOLD_BARRIER %cst
+ %cst2:_(s32) = G_CONSTANT i32 2
+ %add:_(s32) = G_ADD %cfb, %cst2
+ $w0 = COPY %add
+ RET_ReallyLR
+
+...
More information about the llvm-commits
mailing list