[llvm] [GISel][RISCV] Legalize G_CONSTANT_FOLD_BARRIER (PR #89960)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 24 10:44:21 PDT 2024
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/89960
This patch supports `G_CONSTANT_FOLD_BARRIER` on RISCV to generate the following inst seq without crash:
```
define i64 @xor_and_i64(i64 %x) {
entry:
%y = and i64 %x, 16383
%z = xor i64 %y, 16368
ret i64 %z
}
```
I don't know how to avoid creating `G_CONSTANT_FOLD_BARRIER` with s64 on rv32 :(
>From 80a7f64539007c6caf5c20af6dda0d2a5d114cca Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 25 Apr 2024 01:41:44 +0800
Subject: [PATCH] [GISel][RISCV] Legalize G_CONSTANT_FOLD_BARRIER
---
.../Target/RISCV/GISel/RISCVLegalizerInfo.cpp | 2 +-
.../legalizer/legalize-constbarrier-rv32.mir | 31 +++++++++
.../legalizer/legalize-constbarrier-rv64.mir | 64 +++++++++++++++++++
3 files changed, 96 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir
create mode 100644 llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir
diff --git a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
index 38c1f9868d7dbe..adc68e9ee4a89b 100644
--- a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
+++ b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
@@ -227,7 +227,7 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
ConstantActions.widenScalarToNextPow2(0).clampScalar(0, s32, sXLen);
// TODO: transform illegal vector types into legal vector type
- getActionDefinitionsBuilder(G_IMPLICIT_DEF)
+ getActionDefinitionsBuilder({G_IMPLICIT_DEF, G_CONSTANT_FOLD_BARRIER})
.legalFor({s32, sXLen, p0})
.legalIf(typeIsLegalBoolVec(0, BoolVecTys, ST))
.legalIf(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST))
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir
new file mode 100644
index 00000000000000..5d9da732b63177
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir
@@ -0,0 +1,31 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
+# RUN: llc -mtriple=riscv32 -run-pass=legalizer %s -o - | FileCheck %s
+---
+name: xor_and_i32
+body: |
+ bb.1.entry:
+ liveins: $x10
+
+ ; CHECK-LABEL: name: xor_and_i32
+ ; CHECK: liveins: $x10
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 16368
+ ; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 15
+ ; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
+ ; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s32) = G_ADD [[CONSTANT_FOLD_BARRIER]], [[C1]]
+ ; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[ADD]]
+ ; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s32) = G_XOR [[AND]], [[CONSTANT_FOLD_BARRIER]]
+ ; CHECK-NEXT: $x10 = COPY [[XOR]](s32)
+ ; CHECK-NEXT: PseudoRET implicit $x10
+ %0:_(s32) = COPY $x10
+ %1:_(s32) = G_CONSTANT i32 16368
+ %3:_(s32) = G_CONSTANT i32 15
+ %2:_(s32) = G_CONSTANT_FOLD_BARRIER %1
+ %4:_(s32) = G_ADD %2, %3
+ %5:_(s32) = G_AND %0, %4
+ %6:_(s32) = G_XOR %5, %2
+ $x10 = COPY %6(s32)
+ PseudoRET implicit $x10
+
+...
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir
new file mode 100644
index 00000000000000..2b4bb7eec54f8c
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir
@@ -0,0 +1,64 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
+# RUN: llc -mtriple=riscv64 -run-pass=legalizer %s -o - | FileCheck %s
+---
+name: xor_and_i32
+body: |
+ bb.1.entry:
+ liveins: $x10
+
+ ; CHECK-LABEL: name: xor_and_i32
+ ; CHECK: liveins: $x10
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x10
+ ; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[COPY]](s64)
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 16368
+ ; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 15
+ ; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
+ ; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s32) = G_ADD [[CONSTANT_FOLD_BARRIER]], [[C1]]
+ ; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[TRUNC]], [[ADD]]
+ ; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s32) = G_XOR [[AND]], [[CONSTANT_FOLD_BARRIER]]
+ ; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[XOR]](s32)
+ ; CHECK-NEXT: $x10 = COPY [[ANYEXT]](s64)
+ ; CHECK-NEXT: PseudoRET implicit $x10
+ %1:_(s64) = COPY $x10
+ %0:_(s32) = G_TRUNC %1(s64)
+ %2:_(s32) = G_CONSTANT i32 16368
+ %4:_(s32) = G_CONSTANT i32 15
+ %3:_(s32) = G_CONSTANT_FOLD_BARRIER %2
+ %5:_(s32) = G_ADD %3, %4
+ %6:_(s32) = G_AND %0, %5
+ %7:_(s32) = G_XOR %6, %3
+ %8:_(s64) = G_ANYEXT %7(s32)
+ $x10 = COPY %8(s64)
+ PseudoRET implicit $x10
+
+...
+---
+name: xor_and_i64
+body: |
+ bb.1.entry:
+ liveins: $x10
+
+ ; CHECK-LABEL: name: xor_and_i64
+ ; CHECK: liveins: $x10
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x10
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 16368
+ ; CHECK-NEXT: [[C1:%[0-9]+]]:_(s64) = G_CONSTANT i64 15
+ ; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s64) = G_CONSTANT_FOLD_BARRIER [[C]]
+ ; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[CONSTANT_FOLD_BARRIER]], [[C1]]
+ ; CHECK-NEXT: [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY]], [[ADD]]
+ ; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s64) = G_XOR [[AND]], [[CONSTANT_FOLD_BARRIER]]
+ ; CHECK-NEXT: $x10 = COPY [[XOR]](s64)
+ ; CHECK-NEXT: PseudoRET implicit $x10
+ %0:_(s64) = COPY $x10
+ %1:_(s64) = G_CONSTANT i64 16368
+ %3:_(s64) = G_CONSTANT i64 15
+ %2:_(s64) = G_CONSTANT_FOLD_BARRIER %1
+ %4:_(s64) = G_ADD %2, %3
+ %5:_(s64) = G_AND %0, %4
+ %6:_(s64) = G_XOR %5, %2
+ $x10 = COPY %6(s64)
+ PseudoRET implicit $x10
+
+...
More information about the llvm-commits
mailing list