[llvm] [GlobalIsel] Lower integer constants to constant pool in `LegalizerHelper`. (PR #81957)
Mikhail Gudim via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 15 22:41:37 PST 2024
https://github.com/mgudim updated https://github.com/llvm/llvm-project/pull/81957
>From 6aa28a8174d8734c628304aad5a70f3843ba575e Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Sat, 10 Feb 2024 00:43:02 -0500
Subject: [PATCH 1/3] [WIP]
---
.../CodeGen/GlobalISel/LegalizerHelper.cpp | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 464ff0864d146f..a44748f5cf124f 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -2987,6 +2987,32 @@ static void getUnmergePieces(SmallVectorImpl<Register> &Pieces,
Pieces.push_back(Unmerge.getReg(I));
}
+LegalizerHelper::LegalizeResult
+LegalizerHelper::lowerConstant(MachineInstr &MI) {
+ Register Dst = MI.getOperand(0).getReg();
+
+ MachineFunction &MF = MIRBuilder.getMF();
+ const DataLayout &DL = MIRBuilder.getDataLayout();
+
+ unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
+ LLT AddrPtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
+ Align Alignment = Align(DL.getABITypeAlign(
+ getFloatTypeForLLT(MF.getFunction().getContext(), MRI.getType(Dst))));
+
+ auto Addr = MIRBuilder.buildConstantPool(
+ AddrPtrTy, MF.getConstantPool()->getConstantPoolIndex(
+ MI.getOperand(1).getFPImm(), Alignment));
+
+ MachineMemOperand *MMO = MF.getMachineMemOperand(
+ MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad,
+ MRI.getType(Dst), Alignment);
+
+ MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, Dst, Addr, *MMO);
+ MI.eraseFromParent();
+
+ return Legalized;
+}
+
LegalizerHelper::LegalizeResult
LegalizerHelper::lowerFConstant(MachineInstr &MI) {
Register Dst = MI.getOperand(0).getReg();
>From c89c3ab9e0ba801f4ad589b710c75b027b465bd0 Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Sat, 10 Feb 2024 03:02:38 -0500
Subject: [PATCH 2/3] [GlobalISel] Lower integer constants to constant pool in
LegalizerHelper.
Extend LegalizerHelper's API to lower integer constants to a load from
constant pool. Previously, this lowering existed only for FP constants.
Apply this change to RISCV.
---
.../llvm/CodeGen/GlobalISel/LegalizerHelper.h | 1 +
.../CodeGen/GlobalISel/LegalizerHelper.cpp | 56 +++++++++----------
.../Target/RISCV/GISel/RISCVLegalizerInfo.cpp | 32 +----------
.../Target/RISCV/GISel/RISCVLegalizerInfo.h | 2 -
4 files changed, 27 insertions(+), 64 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h b/llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h
index a7ecf0dc1ba216..f001d8a1672972 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h
@@ -365,6 +365,7 @@ class LegalizerHelper {
LegalizeResult bitcastInsertVectorElt(MachineInstr &MI, unsigned TypeIdx,
LLT CastTy);
+ LegalizeResult lowerConstant(MachineInstr &MI);
LegalizeResult lowerFConstant(MachineInstr &MI);
LegalizeResult lowerBitcast(MachineInstr &MI);
LegalizeResult lowerLoad(GAnyLoad &MI);
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index a44748f5cf124f..46226867298e6c 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -2987,27 +2987,35 @@ static void getUnmergePieces(SmallVectorImpl<Register> &Pieces,
Pieces.push_back(Unmerge.getReg(I));
}
-LegalizerHelper::LegalizeResult
-LegalizerHelper::lowerConstant(MachineInstr &MI) {
- Register Dst = MI.getOperand(0).getReg();
-
+static void emitLoadFromConstantPool(Register DstReg, const Constant *ConstVal,
+ MachineIRBuilder &MIRBuilder) {
+ MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
MachineFunction &MF = MIRBuilder.getMF();
const DataLayout &DL = MIRBuilder.getDataLayout();
-
unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
LLT AddrPtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
- Align Alignment = Align(DL.getABITypeAlign(
- getFloatTypeForLLT(MF.getFunction().getContext(), MRI.getType(Dst))));
+ LLT DstLLT = MRI.getType(DstReg);
+
+ Align Alignment(DL.getABITypeAlign(ConstVal->getType()));
auto Addr = MIRBuilder.buildConstantPool(
- AddrPtrTy, MF.getConstantPool()->getConstantPoolIndex(
- MI.getOperand(1).getFPImm(), Alignment));
+ AddrPtrTy,
+ MF.getConstantPool()->getConstantPoolIndex(ConstVal, Alignment));
- MachineMemOperand *MMO = MF.getMachineMemOperand(
- MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad,
- MRI.getType(Dst), Alignment);
+ MachineMemOperand *MMO =
+ MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
+ MachineMemOperand::MOLoad, DstLLT, Alignment);
+
+ MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, DstReg, Addr, *MMO);
+}
+
+LegalizerHelper::LegalizeResult
+LegalizerHelper::lowerConstant(MachineInstr &MI) {
+ MachineOperand ConstOperand = MI.getOperand(1);
+ assert(ConstOperand.isCImm());
+ const Constant *ConstantVal = ConstOperand.getCImm();
- MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, Dst, Addr, *MMO);
+ emitLoadFromConstantPool(MI.getOperand(0).getReg(), ConstantVal, MIRBuilder);
MI.eraseFromParent();
return Legalized;
@@ -3015,25 +3023,11 @@ LegalizerHelper::lowerConstant(MachineInstr &MI) {
LegalizerHelper::LegalizeResult
LegalizerHelper::lowerFConstant(MachineInstr &MI) {
- Register Dst = MI.getOperand(0).getReg();
-
- MachineFunction &MF = MIRBuilder.getMF();
- const DataLayout &DL = MIRBuilder.getDataLayout();
-
- unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
- LLT AddrPtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
- Align Alignment = Align(DL.getABITypeAlign(
- getFloatTypeForLLT(MF.getFunction().getContext(), MRI.getType(Dst))));
-
- auto Addr = MIRBuilder.buildConstantPool(
- AddrPtrTy, MF.getConstantPool()->getConstantPoolIndex(
- MI.getOperand(1).getFPImm(), Alignment));
-
- MachineMemOperand *MMO = MF.getMachineMemOperand(
- MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad,
- MRI.getType(Dst), Alignment);
+ MachineOperand ConstOperand = MI.getOperand(1);
+ assert(ConstOperand.isFPImm());
+ const Constant *ConstantVal = ConstOperand.getFPImm();
- MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, Dst, Addr, *MMO);
+ emitLoadFromConstantPool(MI.getOperand(0).getReg(), ConstantVal, MIRBuilder);
MI.eraseFromParent();
return Legalized;
diff --git a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
index 262e8e56d27e78..64ae4e94a8c929 100644
--- a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
+++ b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
@@ -495,33 +495,6 @@ bool RISCVLegalizerInfo::shouldBeInConstantPool(APInt APImm,
return !(!SeqLo.empty() && (SeqLo.size() + 2) <= STI.getMaxBuildIntsCost());
}
-// TODO: This is almost the same as LegalizerHelper::lowerFConstant and is
-// target-independent. Should we move this to LegalizeHelper?
-bool RISCVLegalizerInfo::emitLoadFromConstantPool(
- Register DstReg, const Constant *ConstVal,
- MachineIRBuilder &MIRBuilder) const {
- MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
- MachineFunction &MF = MIRBuilder.getMF();
- const DataLayout &DL = MIRBuilder.getDataLayout();
- LLVMContext &Ctx = MF.getFunction().getContext();
- unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
- LLT AddrPtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
- LLT DstLLT = MRI.getType(DstReg);
-
- Align Alignment(DL.getABITypeAlign(getTypeForLLT(DstLLT, Ctx)));
-
- auto Addr = MIRBuilder.buildConstantPool(
- AddrPtrTy,
- MF.getConstantPool()->getConstantPoolIndex(ConstVal, Alignment));
-
- MachineMemOperand *MMO =
- MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
- MachineMemOperand::MOLoad, DstLLT, Alignment);
-
- MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, DstReg, Addr, *MMO);
- return true;
-}
-
bool RISCVLegalizerInfo::legalizeCustom(
LegalizerHelper &Helper, MachineInstr &MI,
LostDebugLocObserver &LocObserver) const {
@@ -543,10 +516,7 @@ bool RISCVLegalizerInfo::legalizeCustom(
const ConstantInt *ConstVal = MI.getOperand(1).getCImm();
if (!shouldBeInConstantPool(ConstVal->getValue(), ShouldOptForSize))
return true;
- emitLoadFromConstantPool(MI.getOperand(0).getReg(),
- MI.getOperand(1).getCImm(), MIRBuilder);
- MI.eraseFromParent();
- return true;
+ return Helper.lowerConstant(MI);
}
case TargetOpcode::G_SHL:
case TargetOpcode::G_ASHR:
diff --git a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h
index 046555f3a3e9ee..323426034827e4 100644
--- a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h
+++ b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h
@@ -38,8 +38,6 @@ class RISCVLegalizerInfo : public LegalizerInfo {
private:
bool shouldBeInConstantPool(APInt APImm, bool ShouldOptForSize) const;
- bool emitLoadFromConstantPool(Register DstReg, const Constant *CPVal,
- MachineIRBuilder &MIRBuilder) const;
bool legalizeShlAshrLshr(MachineInstr &MI, MachineIRBuilder &MIRBuilder,
GISelChangeObserver &Observer) const;
>From 23826867c257b23d6342ee594d0f99eed27ce6a6 Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Fri, 16 Feb 2024 01:41:13 -0500
Subject: [PATCH 3/3] Removed redundant assert.
---
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp | 2 --
1 file changed, 2 deletions(-)
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 46226867298e6c..13b380118c6e00 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -3012,7 +3012,6 @@ static void emitLoadFromConstantPool(Register DstReg, const Constant *ConstVal,
LegalizerHelper::LegalizeResult
LegalizerHelper::lowerConstant(MachineInstr &MI) {
MachineOperand ConstOperand = MI.getOperand(1);
- assert(ConstOperand.isCImm());
const Constant *ConstantVal = ConstOperand.getCImm();
emitLoadFromConstantPool(MI.getOperand(0).getReg(), ConstantVal, MIRBuilder);
@@ -3024,7 +3023,6 @@ LegalizerHelper::lowerConstant(MachineInstr &MI) {
LegalizerHelper::LegalizeResult
LegalizerHelper::lowerFConstant(MachineInstr &MI) {
MachineOperand ConstOperand = MI.getOperand(1);
- assert(ConstOperand.isFPImm());
const Constant *ConstantVal = ConstOperand.getFPImm();
emitLoadFromConstantPool(MI.getOperand(0).getReg(), ConstantVal, MIRBuilder);
More information about the llvm-commits
mailing list