From d74de3ce79d4ab3495650bfcc4682cab09514b89 Mon Sep 17 00:00:00 2001 From: "Jayanth Krishnamurthy jayanth.krishnamurthy@ibm.com" Date: Mon, 15 Sep 2025 17:33:42 -0500 Subject: cmd/compile: improve uint8/uint16 logical immediates on PPC64 Logical ops on uint8/uint16 (AND/OR/XOR) with constants sometimes materialized the mask via MOVD (often as a negative immediate), even when the value fit in the UI-immediate range. This prevented the backend from selecting andi. / ori / xori forms. This CL makes: UI-immediate truncation is performed only at the use-site of logical-immediate ops, and only when the constant does not fit in the 8- or 16-bit unsigned domain (m != uint8(m) / m != uint16(m)). This avoids negative-mask materialization and enables correct emission of UI-form logical instructions. Arithmetic SI-immediate instructions (addi, subfic, etc.) and other use-patterns are unchanged. Codegen tests are added to ensure the expected andi./ori/xori patterns appear and that MOVD is not emitted for valid 8/16-bit masks. Change-Id: I9fcdf4498c4e984c7587814fb9019a75865c4a0d Cq-Include-Trybots: luci.golang.try:gotip-linux-ppc64_power10,gotip-linux-ppc64_power8,gotip-linux-ppc64le_power8,gotip-linux-ppc64le_power9,gotip-linux-ppc64le_power10 Reviewed-on: https://go-review.googlesource.com/c/go/+/704015 LUCI-TryBot-Result: Go LUCI Reviewed-by: Paul Murphy Reviewed-by: Cherry Mui Reviewed-by: Mark Freeman --- test/codegen/arithmetic.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'test') diff --git a/test/codegen/arithmetic.go b/test/codegen/arithmetic.go index f8a2fc5400..a5a317b2d1 100644 --- a/test/codegen/arithmetic.go +++ b/test/codegen/arithmetic.go @@ -789,3 +789,34 @@ func Uint64Max(a, b uint64) uint64 { // riscv64/rva22u64,riscv64/rva23u64:"MAXU" return max(a, b) } + +// PPC64x: Canonicalization of uint8/uint16 logical immediates +func U16And(v uint16) uint16 { + // ppc64x:"ANDCC [$]32768" -"MOVD" + return v & (1 << 15) +} + +func U16Or(v uint16) uint16 { + // ppc64x:"OR[I]? [$]32768" -"MOVD" + return v | (1 << 15) +} + +func U16Xor(v uint16) uint16 { + // ppc64x:"XOR[I]? [$]32768" -"MOVD" + return v ^ (1 << 15) +} + +func U8And(v uint8) uint8 { + // ppc64x:"ANDCC [$]128" -"MOVD" + return v & (1 << 7) +} + +func U8Or(v uint8) uint8 { + // ppc64x:"OR[I]? [$]128" -"MOVD" + return v | (1 << 7) +} + +func U8Xor(v uint8) uint8 { + // ppc64x:"XOR[I]? [$]128" -"MOVD" + return v ^ (1 << 7) +} -- cgit v1.3