-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[RISCV][GISel] Fold G_FCONSTANT 0.0 store into G_CONSTANT x0 #163008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sunshaoce
wants to merge
7
commits into
llvm:main
Choose a base branch
from
sunshaoce:pr-sw-0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+381
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-globalisel @llvm/pr-subscribers-backend-risc-v Author: Shaoce SUN (sunshaoce) ChangesFull diff: https://github.com/llvm/llvm-project/pull/163008.diff 2 Files Affected:
diff --git a/llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp b/llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
index 53633eac3d2c3..54050242b1854 100644
--- a/llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
+++ b/llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
@@ -1049,6 +1049,19 @@ void RISCVInstructionSelector::preISelLower(MachineInstr &MI,
MRI->setType(DstReg, sXLen);
break;
}
+ case TargetOpcode::G_STORE: {
+ Register SrcReg = MI.getOperand(0).getReg();
+ MachineInstr *Def = MRI->getVRegDef(SrcReg);
+ if (Def && Def->getOpcode() == TargetOpcode::G_FCONSTANT) {
+ if (Def->getOperand(1).getFPImm()->getValueAPF().isPosZero()) {
+ MI.getOperand(0).setReg(RISCV::X0);
+
+ if (MRI->use_nodbg_empty(SrcReg))
+ Def->eraseFromParent();
+ }
+ }
+ break;
+ }
}
}
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/store-fp-zero-to-x0.ll b/llvm/test/CodeGen/RISCV/GlobalISel/store-fp-zero-to-x0.ll
new file mode 100644
index 0000000000000..d9a6e4b5ec53a
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/store-fp-zero-to-x0.ll
@@ -0,0 +1,41 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv32 -global-isel -mattr=+f -verify-machineinstrs < %s \
+; RUN: | FileCheck %s --check-prefix=RV32
+; RUN: llc -mtriple=riscv64 -global-isel -mattr=+d -verify-machineinstrs < %s \
+; RUN: | FileCheck %s --check-prefix=RV64
+
+define void @zero_f32(ptr %i) {
+; RV32-LABEL: zero_f32:
+; RV32: # %bb.0: # %entry
+; RV32-NEXT: sw zero, 0(a0)
+; RV32-NEXT: ret
+;
+; RV64-LABEL: zero_f32:
+; RV64: # %bb.0: # %entry
+; RV64-NEXT: sw zero, 0(a0)
+; RV64-NEXT: ret
+entry:
+ store float 0.000000e+00, ptr %i, align 4
+ ret void
+}
+
+
+define void @zero_f64(ptr %i) {
+; RV32-LABEL: zero_f64:
+; RV32: # %bb.0: # %entry
+; RV32-NEXT: lui a1, %hi(.LCPI1_0)
+; RV32-NEXT: addi a1, a1, %lo(.LCPI1_0)
+; RV32-NEXT: lw a2, 0(a1)
+; RV32-NEXT: lw a1, 4(a1)
+; RV32-NEXT: sw a2, 0(a0)
+; RV32-NEXT: sw a1, 4(a0)
+; RV32-NEXT: ret
+;
+; RV64-LABEL: zero_f64:
+; RV64: # %bb.0: # %entry
+; RV64-NEXT: sd zero, 0(a0)
+; RV64-NEXT: ret
+entry:
+ store double 0.000000e+00, ptr %i, align 8
+ ret void
+}
|
arsenm
reviewed
Oct 11, 2025
G_FCONSTANT
0.0 store into sw x0
topperc
reviewed
Oct 13, 2025
topperc
reviewed
Oct 14, 2025
topperc
reviewed
Oct 14, 2025
topperc
reviewed
Oct 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.