Skip to content

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Sep 5, 2025

SRAI can only increase the number of sign bits. If the input has at least 33 sign bits, the result will to.

I don't have a test case for this currently. It was just an observation I made while thinking about the shifts and extracts.

SRAI can only increase the number of sign bits. If the input has
at least 33 sign bits, the result will to.

I don't have a test case for this currently. It was just an
observation I made while thinking about the shifts and extracts.
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-backend-risc-v

Author: Craig Topper (topperc)

Changes

SRAI can only increase the number of sign bits. If the input has at least 33 sign bits, the result will to.

I don't have a test case for this currently. It was just an observation I made while thinking about the shifts and extracts.


Full diff: https://github.com/llvm/llvm-project/pull/157164.diff

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp (+2)
diff --git a/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp b/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp
index 3b19c3456ad67..e86c49dabfd64 100644
--- a/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp
+++ b/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp
@@ -519,9 +519,11 @@ static bool isSignExtendedW(Register SrcReg, const RISCVSubtarget &ST,
     case RISCV::ANDI:
     case RISCV::ORI:
     case RISCV::XORI:
+    case RISCV::SRAI:
       // |Remainder| is always <= |Dividend|. If D is 32-bit, then so is R.
       // DIV doesn't work because of the edge case 0xf..f 8000 0000 / (long)-1
       // Logical operations use a sign extended 12-bit immediate.
+      // Arithmetic shift right can only increase the number of sign bits.
       if (!AddRegToWorkList(MI->getOperand(1).getReg()))
         return false;
 

Copy link
Collaborator

@preames preames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@topperc topperc merged commit 1ecdbf2 into llvm:main Sep 5, 2025
11 checks passed
@topperc topperc deleted the pr/srai-optw branch September 5, 2025 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants