Skip to content

Commit 821bcba

Browse files
authored
[GISel] Add narrowScalar/widenScalar support for G_CONSTANT_FOLD_BARRIER (#93031)
Fixes an error that llc fails to legalize `G_CONSTANT_FOLD_BARRIER` with i16/i128: https://godbolt.org/z/f9n6xM3sv
1 parent 3c67c22 commit 821bcba

File tree

5 files changed

+165
-2
lines changed

5 files changed

+165
-2
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
12961296
MI.eraseFromParent();
12971297
return Legalized;
12981298
}
1299-
1299+
case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
13001300
case TargetOpcode::G_FREEZE: {
13011301
if (TypeIdx != 0)
13021302
return UnableToLegalize;
@@ -1310,7 +1310,8 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
13101310
SmallVector<Register, 8> Parts;
13111311
for (unsigned i = 0; i < Unmerge->getNumDefs(); ++i) {
13121312
Parts.push_back(
1313-
MIRBuilder.buildFreeze(NarrowTy, Unmerge.getReg(i)).getReg(0));
1313+
MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, {Unmerge.getReg(i)})
1314+
.getReg(0));
13141315
}
13151316

13161317
MIRBuilder.buildMergeLikeInstr(MI.getOperand(0).getReg(), Parts);
@@ -2515,6 +2516,7 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
25152516
return Legalized;
25162517
}
25172518
case TargetOpcode::G_FREEZE:
2519+
case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
25182520
Observer.changingInstr(MI);
25192521
widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
25202522
widenScalarDst(MI, WideTy);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple=riscv32 -global-isel -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s --check-prefixes=RV32
4+
5+
define i16 @constant_fold_barrier_i16(i16 %x, i16 %y) {
6+
; RV32-LABEL: constant_fold_barrier_i16:
7+
; RV32: # %bb.0: # %entry
8+
; RV32-NEXT: li a1, 1
9+
; RV32-NEXT: slli a1, a1, 11
10+
; RV32-NEXT: and a0, a0, a1
11+
; RV32-NEXT: addi a1, a1, 289
12+
; RV32-NEXT: or a0, a0, a1
13+
; RV32-NEXT: ret
14+
entry:
15+
%and = and i16 %x, 2048
16+
%or = or i16 %and, 2337
17+
ret i16 %or
18+
}
19+
20+
define void @constant_fold_barrier_i128(ptr %p) {
21+
; RV32-LABEL: constant_fold_barrier_i128:
22+
; RV32: # %bb.0: # %entry
23+
; RV32-NEXT: li a1, 1
24+
; RV32-NEXT: slli a1, a1, 11
25+
; RV32-NEXT: lw a2, 0(a0)
26+
; RV32-NEXT: lw a3, 4(a0)
27+
; RV32-NEXT: lw a4, 8(a0)
28+
; RV32-NEXT: lw a5, 12(a0)
29+
; RV32-NEXT: and a2, a2, a1
30+
; RV32-NEXT: and a3, a3, zero
31+
; RV32-NEXT: and a4, a4, zero
32+
; RV32-NEXT: and a5, a5, zero
33+
; RV32-NEXT: add a2, a2, a1
34+
; RV32-NEXT: sltu a1, a2, a1
35+
; RV32-NEXT: add a6, a3, zero
36+
; RV32-NEXT: sltu a3, a6, a3
37+
; RV32-NEXT: add a6, a6, a1
38+
; RV32-NEXT: seqz a7, a6
39+
; RV32-NEXT: and a1, a7, a1
40+
; RV32-NEXT: or a1, a3, a1
41+
; RV32-NEXT: add a3, a4, zero
42+
; RV32-NEXT: sltu a4, a3, a4
43+
; RV32-NEXT: add a3, a3, a1
44+
; RV32-NEXT: seqz a7, a3
45+
; RV32-NEXT: and a1, a7, a1
46+
; RV32-NEXT: or a1, a4, a1
47+
; RV32-NEXT: add a5, a5, zero
48+
; RV32-NEXT: add a1, a5, a1
49+
; RV32-NEXT: sw a2, 0(a0)
50+
; RV32-NEXT: sw a6, 4(a0)
51+
; RV32-NEXT: sw a3, 8(a0)
52+
; RV32-NEXT: sw a1, 12(a0)
53+
; RV32-NEXT: ret
54+
entry:
55+
%x = load i128, ptr %p
56+
%and = and i128 %x, 2048
57+
%add = add i128 %and, 2048
58+
store i128 %add, ptr %p
59+
ret void
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple=riscv64 -global-isel -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s --check-prefixes=RV64
4+
5+
define i16 @constant_fold_barrier_i16(i16 %x, i16 %y) {
6+
; RV64-LABEL: constant_fold_barrier_i16:
7+
; RV64: # %bb.0: # %entry
8+
; RV64-NEXT: li a1, 1
9+
; RV64-NEXT: slli a1, a1, 11
10+
; RV64-NEXT: and a0, a0, a1
11+
; RV64-NEXT: addiw a1, a1, 289
12+
; RV64-NEXT: or a0, a0, a1
13+
; RV64-NEXT: ret
14+
entry:
15+
%and = and i16 %x, 2048
16+
%or = or i16 %and, 2337
17+
ret i16 %or
18+
}
19+
20+
define i128 @constant_fold_barrier_i128(i128 %x) {
21+
; RV64-LABEL: constant_fold_barrier_i128:
22+
; RV64: # %bb.0: # %entry
23+
; RV64-NEXT: li a2, 1
24+
; RV64-NEXT: slli a2, a2, 11
25+
; RV64-NEXT: and a0, a0, a2
26+
; RV64-NEXT: and a1, a1, zero
27+
; RV64-NEXT: add a0, a0, a2
28+
; RV64-NEXT: sltu a2, a0, a2
29+
; RV64-NEXT: add a1, a1, zero
30+
; RV64-NEXT: add a1, a1, a2
31+
; RV64-NEXT: ret
32+
entry:
33+
%and = and i128 %x, 2048
34+
%add = add i128 %and, 2048
35+
ret i128 %add
36+
}

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@ body: |
1414
$x10 = COPY %2(s32)
1515
PseudoRET implicit $x10
1616
17+
...
18+
---
19+
name: constbarrier_i16
20+
body: |
21+
bb.0.entry:
22+
; CHECK-LABEL: name: constbarrier_i16
23+
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
24+
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
25+
; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s32)
26+
; CHECK-NEXT: PseudoRET implicit $x10
27+
%1:_(s16) = G_CONSTANT i16 2048
28+
%2:_(s16) = G_CONSTANT_FOLD_BARRIER %1
29+
%3:_(s32) = G_ANYEXT %2(s16)
30+
$x10 = COPY %3(s32)
31+
PseudoRET implicit $x10
32+
33+
...
34+
---
35+
name: constbarrier_i128
36+
body: |
37+
bb.0.entry:
38+
; CHECK-LABEL: name: constbarrier_i128
39+
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
40+
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
41+
; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s32)
42+
; CHECK-NEXT: PseudoRET implicit $x10
43+
%1:_(s128) = G_CONSTANT i128 2048
44+
%2:_(s128) = G_CONSTANT_FOLD_BARRIER %1
45+
%3:_(s32) = G_TRUNC %2(s128)
46+
$x10 = COPY %3(s32)
47+
PseudoRET implicit $x10
48+
1749
...
1850
---
1951
name: constbarrier_nxv2i1

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ body: |
3131
$x10 = COPY %2(s64)
3232
PseudoRET implicit $x10
3333
34+
...
35+
---
36+
name: constbarrier_i16
37+
body: |
38+
bb.0.entry:
39+
; CHECK-LABEL: name: constbarrier_i16
40+
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
41+
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
42+
; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[CONSTANT_FOLD_BARRIER]](s32)
43+
; CHECK-NEXT: $x10 = COPY [[ANYEXT]](s64)
44+
; CHECK-NEXT: PseudoRET implicit $x10
45+
%1:_(s16) = G_CONSTANT i16 2048
46+
%2:_(s16) = G_CONSTANT_FOLD_BARRIER %1
47+
%3:_(s64) = G_ANYEXT %2(s16)
48+
$x10 = COPY %3(s64)
49+
PseudoRET implicit $x10
50+
51+
...
52+
---
53+
name: constbarrier_i128
54+
body: |
55+
bb.0.entry:
56+
; CHECK-LABEL: name: constbarrier_i128
57+
; CHECK: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 2048
58+
; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s64) = G_CONSTANT_FOLD_BARRIER [[C]]
59+
; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s64)
60+
; CHECK-NEXT: PseudoRET implicit $x10
61+
%1:_(s128) = G_CONSTANT i128 2048
62+
%2:_(s128) = G_CONSTANT_FOLD_BARRIER %1
63+
%3:_(s64) = G_TRUNC %2(s128)
64+
$x10 = COPY %3(s64)
65+
PseudoRET implicit $x10
66+
3467
...
3568
---
3669
name: constbarrier_nxv2i1

0 commit comments

Comments
 (0)