Skip to content

[GISel] Add narrowScalar/widenScalar support for G_CONSTANT_FOLD_BARRIER #93031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
MI.eraseFromParent();
return Legalized;
}

case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
case TargetOpcode::G_FREEZE: {
if (TypeIdx != 0)
return UnableToLegalize;
Expand All @@ -1310,7 +1310,8 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
SmallVector<Register, 8> Parts;
for (unsigned i = 0; i < Unmerge->getNumDefs(); ++i) {
Parts.push_back(
MIRBuilder.buildFreeze(NarrowTy, Unmerge.getReg(i)).getReg(0));
MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, {Unmerge.getReg(i)})
.getReg(0));
}

MIRBuilder.buildMergeLikeInstr(MI.getOperand(0).getReg(), Parts);
Expand Down Expand Up @@ -2515,6 +2516,7 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
return Legalized;
}
case TargetOpcode::G_FREEZE:
case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
Observer.changingInstr(MI);
widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
widenScalarDst(MI, WideTy);
Expand Down
60 changes: 60 additions & 0 deletions llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=riscv32 -global-isel -verify-machineinstrs < %s \
; RUN: | FileCheck %s --check-prefixes=RV32

define i16 @constant_fold_barrier_i16(i16 %x, i16 %y) {
; RV32-LABEL: constant_fold_barrier_i16:
; RV32: # %bb.0: # %entry
; RV32-NEXT: li a1, 1
; RV32-NEXT: slli a1, a1, 11
; RV32-NEXT: and a0, a0, a1
; RV32-NEXT: addi a1, a1, 289
; RV32-NEXT: or a0, a0, a1
; RV32-NEXT: ret
entry:
%and = and i16 %x, 2048
%or = or i16 %and, 2337
ret i16 %or
}

define void @constant_fold_barrier_i128(ptr %p) {
; RV32-LABEL: constant_fold_barrier_i128:
; RV32: # %bb.0: # %entry
; RV32-NEXT: li a1, 1
; RV32-NEXT: slli a1, a1, 11
; RV32-NEXT: lw a2, 0(a0)
; RV32-NEXT: lw a3, 4(a0)
; RV32-NEXT: lw a4, 8(a0)
; RV32-NEXT: lw a5, 12(a0)
; RV32-NEXT: and a2, a2, a1
; RV32-NEXT: and a3, a3, zero
; RV32-NEXT: and a4, a4, zero
; RV32-NEXT: and a5, a5, zero
; RV32-NEXT: add a2, a2, a1
; RV32-NEXT: sltu a1, a2, a1
; RV32-NEXT: add a6, a3, zero
; RV32-NEXT: sltu a3, a6, a3
; RV32-NEXT: add a6, a6, a1
; RV32-NEXT: seqz a7, a6
; RV32-NEXT: and a1, a7, a1
; RV32-NEXT: or a1, a3, a1
; RV32-NEXT: add a3, a4, zero
; RV32-NEXT: sltu a4, a3, a4
; RV32-NEXT: add a3, a3, a1
; RV32-NEXT: seqz a7, a3
; RV32-NEXT: and a1, a7, a1
; RV32-NEXT: or a1, a4, a1
; RV32-NEXT: add a5, a5, zero
; RV32-NEXT: add a1, a5, a1
; RV32-NEXT: sw a2, 0(a0)
; RV32-NEXT: sw a6, 4(a0)
; RV32-NEXT: sw a3, 8(a0)
; RV32-NEXT: sw a1, 12(a0)
; RV32-NEXT: ret
entry:
%x = load i128, ptr %p
%and = and i128 %x, 2048
%add = add i128 %and, 2048
store i128 %add, ptr %p
ret void
}
36 changes: 36 additions & 0 deletions llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=riscv64 -global-isel -verify-machineinstrs < %s \
; RUN: | FileCheck %s --check-prefixes=RV64

define i16 @constant_fold_barrier_i16(i16 %x, i16 %y) {
; RV64-LABEL: constant_fold_barrier_i16:
; RV64: # %bb.0: # %entry
; RV64-NEXT: li a1, 1
; RV64-NEXT: slli a1, a1, 11
; RV64-NEXT: and a0, a0, a1
; RV64-NEXT: addiw a1, a1, 289
; RV64-NEXT: or a0, a0, a1
; RV64-NEXT: ret
entry:
%and = and i16 %x, 2048
%or = or i16 %and, 2337
ret i16 %or
}

define i128 @constant_fold_barrier_i128(i128 %x) {
; RV64-LABEL: constant_fold_barrier_i128:
; RV64: # %bb.0: # %entry
; RV64-NEXT: li a2, 1
; RV64-NEXT: slli a2, a2, 11
; RV64-NEXT: and a0, a0, a2
; RV64-NEXT: and a1, a1, zero
; RV64-NEXT: add a0, a0, a2
; RV64-NEXT: sltu a2, a0, a2
; RV64-NEXT: add a1, a1, zero
; RV64-NEXT: add a1, a1, a2
; RV64-NEXT: ret
entry:
%and = and i128 %x, 2048
%add = add i128 %and, 2048
ret i128 %add
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,38 @@ body: |
$x10 = COPY %2(s32)
PseudoRET implicit $x10

...
---
name: constbarrier_i16
body: |
bb.0.entry:
; CHECK-LABEL: name: constbarrier_i16
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s32)
; CHECK-NEXT: PseudoRET implicit $x10
%1:_(s16) = G_CONSTANT i16 2048
%2:_(s16) = G_CONSTANT_FOLD_BARRIER %1
%3:_(s32) = G_ANYEXT %2(s16)
$x10 = COPY %3(s32)
PseudoRET implicit $x10

...
---
name: constbarrier_i128
body: |
bb.0.entry:
; CHECK-LABEL: name: constbarrier_i128
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s32)
; CHECK-NEXT: PseudoRET implicit $x10
%1:_(s128) = G_CONSTANT i128 2048
%2:_(s128) = G_CONSTANT_FOLD_BARRIER %1
%3:_(s32) = G_TRUNC %2(s128)
$x10 = COPY %3(s32)
PseudoRET implicit $x10

...
---
name: constbarrier_nxv2i1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ body: |
$x10 = COPY %2(s64)
PseudoRET implicit $x10

...
---
name: constbarrier_i16
body: |
bb.0.entry:
; CHECK-LABEL: name: constbarrier_i16
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[CONSTANT_FOLD_BARRIER]](s32)
; CHECK-NEXT: $x10 = COPY [[ANYEXT]](s64)
; CHECK-NEXT: PseudoRET implicit $x10
%1:_(s16) = G_CONSTANT i16 2048
%2:_(s16) = G_CONSTANT_FOLD_BARRIER %1
%3:_(s64) = G_ANYEXT %2(s16)
$x10 = COPY %3(s64)
PseudoRET implicit $x10

...
---
name: constbarrier_i128
body: |
bb.0.entry:
; CHECK-LABEL: name: constbarrier_i128
; CHECK: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 2048
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s64) = G_CONSTANT_FOLD_BARRIER [[C]]
; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s64)
; CHECK-NEXT: PseudoRET implicit $x10
%1:_(s128) = G_CONSTANT i128 2048
%2:_(s128) = G_CONSTANT_FOLD_BARRIER %1
%3:_(s64) = G_TRUNC %2(s128)
$x10 = COPY %3(s64)
PseudoRET implicit $x10

...
---
name: constbarrier_nxv2i1
Expand Down
Loading