[llvm] [GISel] Teach computeKnownBitsImpl to handle COPY instructions that change bit width. (PR #118924)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 5 21:19:53 PST 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/118924

The selectShiftMask ComplexRenderFn on RISCV calls computeKnownBits.

I encountered a case where we looked through a G_PHI and found a COPY that was created from an already selected G_ANYEXT from s32 to s64. s32 and s64 integers on RISC-V end up in the same register class.a The input to the COPY was an already selected s32 SELECT instruction. We need an s32 SELECT to be legal to support f32 selects. If it isn't used by FP operations, regbank select will assign to GPR.

This patch uses KnownBits::anyextOrTrunc to adjust the width when they mismatch.

I haven't reduced a test case yet, but wanted to make sure this is the right fix.

>From 50563695def13b6b2fa8f11ea467859b2f137db1 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 5 Dec 2024 21:11:31 -0800
Subject: [PATCH] [GISel] Teach computeKnownBitsImpl to handle COPY
 instructions that change bit width.

The selectShiftMask ComplexRenderFn on RISCV calls computeKnownBits.

I encountered a case where we looked through a G_PHI and found a
COPY that was created from an already selected G_ANYEXT from s32 to
s64. s32 and s64 integers on RISC-V end up in the same register class.a
The input to the COPY was an already selected s32 SELECT instruction.
We need an s32 SELECT to be legal to support f32 selects. If it isn't
used by FP operations, regbank select will assign to GPR.

This patch uses KnownBits::anyextOrTrunc to adjust the width when
they mismatch.

I haven't reduced a test case yet, but wanted to make sure this is
the right fix.
---
 llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
index 30cd3ce3baa502..6c15ed3423d3bd 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
@@ -253,6 +253,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
         // For COPYs we don't do anything, don't increase the depth.
         computeKnownBitsImpl(SrcReg, Known2, DemandedElts,
                              Depth + (Opcode != TargetOpcode::COPY));
+        Known2 = Known2.anyextOrTrunc(BitWidth);
         Known = Known.intersectWith(Known2);
         // If we reach a point where we don't know anything
         // just stop looking through the operands.



More information about the llvm-commits mailing list