Skip to content

Commit adcaed3

Browse files
authored
pulley: Use immediate-taking instructions more (bytecodealliance#9871)
Refactor some existing usage of `pulley_xconst*` instructions to instead use instructions-taking-immediates instead now that they've been added to Pulley.
1 parent 99c5eb8 commit adcaed3

File tree

5 files changed

+75
-95
lines changed

5 files changed

+75
-95
lines changed

cranelift/codegen/src/isa/pulley_shared/lower.isle

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
(decl lower_cond (Value) Cond)
1515
(rule 0 (lower_cond val @ (value_type (fits_in_32 _))) (Cond.If32 (zext32 val)))
1616
(rule 1 (lower_cond val @ (value_type $I64))
17-
(Cond.IfXneq64 val (pulley_xconst8 0)))
17+
(Cond.IfXneq64I32 val 0))
1818

1919
;; Peel away explicit `uextend` values to take a look at the inner value.
2020
(rule 2 (lower_cond (uextend val)) (lower_cond val))
@@ -282,27 +282,33 @@
282282
;;;; Rules for `umulhi` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283283

284284
(rule (lower (has_type $I8 (umulhi a b)))
285-
(pulley_xshr32_u (pulley_xmul32 (zext32 a) (zext32 b)) (pulley_xconst8 8)))
285+
(if-let (u6_from_u8 shift) (u64_as_u8 8))
286+
(pulley_xshr32_u_u6 (pulley_xmul32 (zext32 a) (zext32 b)) shift))
286287

287288
(rule (lower (has_type $I16 (umulhi a b)))
288-
(pulley_xshr32_u (pulley_xmul32 (zext32 a) (zext32 b)) (pulley_xconst8 16)))
289+
(if-let (u6_from_u8 shift) (u64_as_u8 16))
290+
(pulley_xshr32_u_u6 (pulley_xmul32 (zext32 a) (zext32 b)) shift))
289291

290292
(rule (lower (has_type $I32 (umulhi a b)))
291-
(pulley_xshr64_u (pulley_xmul64 (zext64 a) (zext64 b)) (pulley_xconst8 32)))
293+
(if-let (u6_from_u8 shift) (u64_as_u8 32))
294+
(pulley_xshr64_u_u6 (pulley_xmul64 (zext64 a) (zext64 b)) shift))
292295

293296
(rule (lower (has_type $I64 (umulhi a b)))
294297
(pulley_xmulhi64_u a b))
295298

296299
;;;; Rules for `smulhi` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297300

298301
(rule (lower (has_type $I8 (smulhi a b)))
299-
(pulley_xshr32_s (pulley_xmul32 (sext32 a) (sext32 b)) (pulley_xconst8 8)))
302+
(if-let (u6_from_u8 shift) (u64_as_u8 8))
303+
(pulley_xshr32_s_u6 (pulley_xmul32 (sext32 a) (sext32 b)) shift))
300304

301305
(rule (lower (has_type $I16 (smulhi a b)))
302-
(pulley_xshr32_s (pulley_xmul32 (sext32 a) (sext32 b)) (pulley_xconst8 16)))
306+
(if-let (u6_from_u8 shift) (u64_as_u8 16))
307+
(pulley_xshr32_s_u6 (pulley_xmul32 (sext32 a) (sext32 b)) shift))
303308

304309
(rule (lower (has_type $I32 (smulhi a b)))
305-
(pulley_xshr64_s (pulley_xmul64 (sext64 a) (sext64 b)) (pulley_xconst8 32)))
310+
(if-let (u6_from_u8 shift) (u64_as_u8 32))
311+
(pulley_xshr64_s_u6 (pulley_xmul64 (sext64 a) (sext64 b)) shift))
306312

307313
(rule (lower (has_type $I64 (smulhi a b)))
308314
(pulley_xmulhi64_s a b))
@@ -334,10 +340,10 @@
334340
;;;; Rules for `ishl` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335341

336342
(rule (lower (has_type $I8 (ishl a b)))
337-
(pulley_xshl32 a (pulley_xband32 b (pulley_xconst8 7))))
343+
(pulley_xshl32 a (pulley_xband32_s8 b 7)))
338344

339345
(rule (lower (has_type $I16 (ishl a b)))
340-
(pulley_xshl32 a (pulley_xband32 b (pulley_xconst8 15))))
346+
(pulley_xshl32 a (pulley_xband32_s8 b 15)))
341347

342348
(rule (lower (has_type $I32 (ishl a b)))
343349
(pulley_xshl32 a b))
@@ -373,10 +379,10 @@
373379
;;;; Rules for `ushr` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374380

375381
(rule (lower (has_type $I8 (ushr a b)))
376-
(pulley_xshr32_u (zext32 a) (pulley_xband32 b (pulley_xconst8 7))))
382+
(pulley_xshr32_u (zext32 a) (pulley_xband32_s8 b 7)))
377383

378384
(rule (lower (has_type $I16 (ushr a b)))
379-
(pulley_xshr32_u (zext32 a) (pulley_xband32 b (pulley_xconst8 15))))
385+
(pulley_xshr32_u (zext32 a) (pulley_xband32_s8 b 15)))
380386

381387
(rule (lower (has_type $I32 (ushr a b)))
382388
(pulley_xshr32_u a b))
@@ -402,10 +408,10 @@
402408
;;;; Rules for `sshr` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
403409

404410
(rule (lower (has_type $I8 (sshr a b)))
405-
(pulley_xshr32_u (sext32 a) (pulley_xband32 b (pulley_xconst8 7))))
411+
(pulley_xshr32_u (sext32 a) (pulley_xband32_s8 b 7)))
406412

407413
(rule (lower (has_type $I16 (sshr a b)))
408-
(pulley_xshr32_u (sext32 a) (pulley_xband32 b (pulley_xconst8 15))))
414+
(pulley_xshr32_u (sext32 a) (pulley_xband32_s8 b 15)))
409415

410416
(rule (lower (has_type $I32 (sshr a b)))
411417
(pulley_xshr32_s a b))
@@ -531,18 +537,18 @@
531537
;;;; Rules for `ctz` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
532538

533539
(rule (lower (has_type $I8 (ctz a)))
534-
(pulley_xctz32 (pulley_xbor32 a (pulley_xconst16 0x100))))
540+
(pulley_xctz32 (pulley_xbor32_s32 a 0x100)))
535541
(rule (lower (has_type $I16 (ctz a)))
536-
(pulley_xctz32 (pulley_xbor32 a (pulley_xconst32 0x10000))))
542+
(pulley_xctz32 (pulley_xbor32_s32 a 0x10000)))
537543
(rule (lower (has_type $I32 (ctz a))) (pulley_xctz32 a))
538544
(rule (lower (has_type $I64 (ctz a))) (pulley_xctz64 a))
539545

540546
;;;; Rules for `clz` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
541547

542548
(rule (lower (has_type $I8 (clz a)))
543-
(pulley_xsub32 (pulley_xclz32 (zext32 a)) (pulley_xconst8 24)))
549+
(pulley_xsub32_u8 (pulley_xclz32 (zext32 a)) 24))
544550
(rule (lower (has_type $I16 (clz a)))
545-
(pulley_xsub32 (pulley_xclz32 (zext32 a)) (pulley_xconst8 16)))
551+
(pulley_xsub32_u8 (pulley_xclz32 (zext32 a)) 16))
546552
(rule (lower (has_type $I32 (clz a))) (pulley_xclz32 a))
547553
(rule (lower (has_type $I64 (clz a))) (pulley_xclz64 a))
548554

@@ -641,13 +647,13 @@
641647

642648
;; complement `=`-related conditions to get ones that don't use `=`.
643649
(rule 2 (lower_icmp $I128 cc @ (IntCC.SignedLessThanOrEqual) x y)
644-
(pulley_xbxor32 (lower_icmp $I128 (intcc_complement cc) x y) (pulley_xconst8 1)))
650+
(pulley_xbxor32_s8 (lower_icmp $I128 (intcc_complement cc) x y) 1))
645651
(rule 2 (lower_icmp $I128 cc @ (IntCC.SignedGreaterThanOrEqual) x y)
646-
(pulley_xbxor32 (lower_icmp $I128 (intcc_complement cc) x y) (pulley_xconst8 1)))
652+
(pulley_xbxor32_s8 (lower_icmp $I128 (intcc_complement cc) x y) 1))
647653
(rule 2 (lower_icmp $I128 cc @ (IntCC.UnsignedLessThanOrEqual) x y)
648-
(pulley_xbxor32 (lower_icmp $I128 (intcc_complement cc) x y) (pulley_xconst8 1)))
654+
(pulley_xbxor32_s8 (lower_icmp $I128 (intcc_complement cc) x y) 1))
649655
(rule 2 (lower_icmp $I128 cc @ (IntCC.UnsignedGreaterThanOrEqual) x y)
650-
(pulley_xbxor32 (lower_icmp $I128 (intcc_complement cc) x y) (pulley_xconst8 1)))
656+
(pulley_xbxor32_s8 (lower_icmp $I128 (intcc_complement cc) x y) 1))
651657

652658
;; Compare both the bottom and upper halves of the 128-bit values. If
653659
;; the top half is equal use the bottom comparison, otherwise use the upper
@@ -872,8 +878,9 @@
872878
(sext64 val))
873879

874880
(rule 1 (lower (has_type $I128 (sextend val)))
881+
(if-let (u6_from_u8 shift) (u64_as_u8 63))
875882
(let ((lo XReg (sext64 val))
876-
(hi XReg (pulley_xshr64_s lo (pulley_xconst8 63))))
883+
(hi XReg (pulley_xshr64_s_u6 lo shift)))
877884
(value_regs lo hi)))
878885

879886
;;;; Rules for `ireduce` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -1197,7 +1204,8 @@
11971204
;;;; Rules for `bswap` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11981205

11991206
(rule (lower (has_type $I16 (bswap a)))
1200-
(pulley_xshr32_u (pulley_bswap32 a) (pulley_xconst8 16)))
1207+
(if-let (u6_from_u8 shift) (u64_as_u8 16))
1208+
(pulley_xshr32_u_u6 (pulley_bswap32 a) shift))
12011209
(rule (lower (has_type $I32 (bswap a))) (pulley_bswap32 a))
12021210
(rule (lower (has_type $I64 (bswap a))) (pulley_bswap64 a))
12031211

cranelift/filetests/filetests/isa/pulley32/brif.clif

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ block2:
110110

111111
; VCode:
112112
; block0:
113-
; xconst8 x4, 0
114-
; br_if_xneq64 x0, x4, label2; jump label1
113+
; br_if_xneq64_i32 x0, 0, label2; jump label1
115114
; block1:
116115
; xconst8 x0, 0
117116
; ret
@@ -120,8 +119,7 @@ block2:
120119
; ret
121120
;
122121
; Disassembled:
123-
; xconst8 x4, 0
124-
; br_if_xneq64 x0, x4, 0xb // target = 0xe
122+
; br_if_xneq64_i8 x0, 0, 0xb // target = 0xb
125123
; xconst8 x0, 0
126124
; ret
127125
; xconst8 x0, 1

cranelift/filetests/filetests/isa/pulley32/trap.clif

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,23 @@ block2:
102102

103103
; VCode:
104104
; block0:
105-
; xconst8 x6, 0
106-
; br_if_xneq64 x0, x6, label2; jump label1
105+
; br_if_xneq64_i32 x0, 0, label2; jump label1
107106
; block1:
108-
; xconst8 x7, 0
109-
; xconst8 x8, 0
110-
; trap_if_xneq64 x7, x8 // code = TrapCode(1)
107+
; xconst8 x4, 0
108+
; trap_if_xneq64_i32 x4, 0 // code = TrapCode(1)
111109
; ret
112110
; block2:
113-
; xconst8 x9, 42
114-
; xconst8 x10, 0
115-
; trap_if_xneq64 x9, x10 // code = TrapCode(1)
111+
; xconst8 x6, 42
112+
; trap_if_xneq64_i32 x6, 0 // code = TrapCode(1)
116113
; ret
117114
;
118115
; Disassembled:
119-
; xconst8 x6, 0
120-
; br_if_xneq64 x0, x6, 0x15 // target = 0x18
121-
; xconst8 x7, 0
122-
; xconst8 x8, 0
123-
; br_if_xneq64 x7, x8, 0x16 // target = 0x26
116+
; br_if_xneq64_i8 x0, 0, 0x12 // target = 0x12
117+
; xconst8 x4, 0
118+
; br_if_xneq64_i8 x4, 0, 0x13 // target = 0x1d
124119
; ret
125-
; xconst8 x9, 42
126-
; xconst8 x10, 0
127-
; br_if_xneq64 x9, x10, 0xb // target = 0x29
120+
; xconst8 x6, 42
121+
; br_if_xneq64_i8 x6, 0, 0xb // target = 0x20
128122
; ret
129123
; trap
130124
; trap
@@ -146,29 +140,23 @@ block2:
146140

147141
; VCode:
148142
; block0:
149-
; xconst8 x6, 0
150-
; br_if_xneq64 x0, x6, label2; jump label1
143+
; br_if_xneq64_i32 x0, 0, label2; jump label1
151144
; block1:
152-
; xconst8 x7, 0
153-
; xconst8 x8, 0
154-
; trap_if_xeq64 x7, x8 // code = TrapCode(1)
145+
; xconst8 x4, 0
146+
; trap_if_xeq64_i32 x4, 0 // code = TrapCode(1)
155147
; ret
156148
; block2:
157-
; xconst8 x9, 42
158-
; xconst8 x10, 0
159-
; trap_if_xeq64 x9, x10 // code = TrapCode(1)
149+
; xconst8 x6, 42
150+
; trap_if_xeq64_i32 x6, 0 // code = TrapCode(1)
160151
; ret
161152
;
162153
; Disassembled:
163-
; xconst8 x6, 0
164-
; br_if_xneq64 x0, x6, 0x15 // target = 0x18
165-
; xconst8 x7, 0
166-
; xconst8 x8, 0
167-
; br_if_xeq64 x7, x8, 0x16 // target = 0x26
154+
; br_if_xneq64_i8 x0, 0, 0x12 // target = 0x12
155+
; xconst8 x4, 0
156+
; br_if_xeq64_i8 x4, 0, 0x13 // target = 0x1d
168157
; ret
169-
; xconst8 x9, 42
170-
; xconst8 x10, 0
171-
; br_if_xeq64 x9, x10, 0xb // target = 0x29
158+
; xconst8 x6, 42
159+
; br_if_xeq64_i8 x6, 0, 0xb // target = 0x20
172160
; ret
173161
; trap
174162
; trap

cranelift/filetests/filetests/isa/pulley64/brif.clif

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ block2:
110110

111111
; VCode:
112112
; block0:
113-
; xconst8 x4, 0
114-
; br_if_xneq64 x0, x4, label2; jump label1
113+
; br_if_xneq64_i32 x0, 0, label2; jump label1
115114
; block1:
116115
; xconst8 x0, 0
117116
; ret
@@ -120,8 +119,7 @@ block2:
120119
; ret
121120
;
122121
; Disassembled:
123-
; xconst8 x4, 0
124-
; br_if_xneq64 x0, x4, 0xb // target = 0xe
122+
; br_if_xneq64_i8 x0, 0, 0xb // target = 0xb
125123
; xconst8 x0, 0
126124
; ret
127125
; xconst8 x0, 1

cranelift/filetests/filetests/isa/pulley64/trap.clif

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,23 @@ block2:
102102

103103
; VCode:
104104
; block0:
105-
; xconst8 x6, 0
106-
; br_if_xneq64 x0, x6, label2; jump label1
105+
; br_if_xneq64_i32 x0, 0, label2; jump label1
107106
; block1:
108-
; xconst8 x7, 0
109-
; xconst8 x8, 0
110-
; trap_if_xneq64 x7, x8 // code = TrapCode(1)
107+
; xconst8 x4, 0
108+
; trap_if_xneq64_i32 x4, 0 // code = TrapCode(1)
111109
; ret
112110
; block2:
113-
; xconst8 x9, 42
114-
; xconst8 x10, 0
115-
; trap_if_xneq64 x9, x10 // code = TrapCode(1)
111+
; xconst8 x6, 42
112+
; trap_if_xneq64_i32 x6, 0 // code = TrapCode(1)
116113
; ret
117114
;
118115
; Disassembled:
119-
; xconst8 x6, 0
120-
; br_if_xneq64 x0, x6, 0x15 // target = 0x18
121-
; xconst8 x7, 0
122-
; xconst8 x8, 0
123-
; br_if_xneq64 x7, x8, 0x16 // target = 0x26
116+
; br_if_xneq64_i8 x0, 0, 0x12 // target = 0x12
117+
; xconst8 x4, 0
118+
; br_if_xneq64_i8 x4, 0, 0x13 // target = 0x1d
124119
; ret
125-
; xconst8 x9, 42
126-
; xconst8 x10, 0
127-
; br_if_xneq64 x9, x10, 0xb // target = 0x29
120+
; xconst8 x6, 42
121+
; br_if_xneq64_i8 x6, 0, 0xb // target = 0x20
128122
; ret
129123
; trap
130124
; trap
@@ -146,29 +140,23 @@ block2:
146140

147141
; VCode:
148142
; block0:
149-
; xconst8 x6, 0
150-
; br_if_xneq64 x0, x6, label2; jump label1
143+
; br_if_xneq64_i32 x0, 0, label2; jump label1
151144
; block1:
152-
; xconst8 x7, 0
153-
; xconst8 x8, 0
154-
; trap_if_xeq64 x7, x8 // code = TrapCode(1)
145+
; xconst8 x4, 0
146+
; trap_if_xeq64_i32 x4, 0 // code = TrapCode(1)
155147
; ret
156148
; block2:
157-
; xconst8 x9, 42
158-
; xconst8 x10, 0
159-
; trap_if_xeq64 x9, x10 // code = TrapCode(1)
149+
; xconst8 x6, 42
150+
; trap_if_xeq64_i32 x6, 0 // code = TrapCode(1)
160151
; ret
161152
;
162153
; Disassembled:
163-
; xconst8 x6, 0
164-
; br_if_xneq64 x0, x6, 0x15 // target = 0x18
165-
; xconst8 x7, 0
166-
; xconst8 x8, 0
167-
; br_if_xeq64 x7, x8, 0x16 // target = 0x26
154+
; br_if_xneq64_i8 x0, 0, 0x12 // target = 0x12
155+
; xconst8 x4, 0
156+
; br_if_xeq64_i8 x4, 0, 0x13 // target = 0x1d
168157
; ret
169-
; xconst8 x9, 42
170-
; xconst8 x10, 0
171-
; br_if_xeq64 x9, x10, 0xb // target = 0x29
158+
; xconst8 x6, 42
159+
; br_if_xeq64_i8 x6, 0, 0xb // target = 0x20
172160
; ret
173161
; trap
174162
; trap

0 commit comments

Comments
 (0)