[llvm] [GlobalIsel] Lower integer constants to constant pool in `LegalizerHelper`. (PR #81957)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 15 17:26:46 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: Mikhail Gudim (mgudim)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/81957.diff
4 Files Affected:
- (modified) llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h (+1)
- (modified) llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp (+34-13)
- (modified) llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp (+1-31)
- (modified) llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h (-2)
``````````diff
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 464ff0864d146f..e5450d36cbae51 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -2987,27 +2987,48 @@ static void getUnmergePieces(SmallVectorImpl<Register> &Pieces,
Pieces.push_back(Unmerge.getReg(I));
}
-LegalizerHelper::LegalizeResult
-LegalizerHelper::lowerFConstant(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();
+
+ emitLoadFromConstantPool(MI.getOperand(0).getReg(), ConstantVal, MIRBuilder);
+ MI.eraseFromParent();
+
+ return Legalized;
+}
+
+LegalizerHelper::LegalizeResult
+LegalizerHelper::lowerFConstant(MachineInstr &MI) {
+ 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 e852052ce5e602..96e74ee486d6af 100644
--- a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
+++ b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
@@ -496,33 +496,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 {
@@ -544,10 +517,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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/81957
More information about the llvm-commits
mailing list