Skip to content

Commit 35cfaec

Browse files
authored
[GlobalIsel] Lower integer constants to constant pool in LegalizerHelper. (#81957)
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.
1 parent 5a82daa commit 35cfaec

File tree

4 files changed

+33
-46
lines changed

4 files changed

+33
-46
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ class LegalizerHelper {
365365
LegalizeResult bitcastInsertVectorElt(MachineInstr &MI, unsigned TypeIdx,
366366
LLT CastTy);
367367

368+
LegalizeResult lowerConstant(MachineInstr &MI);
368369
LegalizeResult lowerFConstant(MachineInstr &MI);
369370
LegalizeResult lowerBitcast(MachineInstr &MI);
370371
LegalizeResult lowerLoad(GAnyLoad &MI);

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,27 +2986,45 @@ static void getUnmergePieces(SmallVectorImpl<Register> &Pieces,
29862986
Pieces.push_back(Unmerge.getReg(I));
29872987
}
29882988

2989-
LegalizerHelper::LegalizeResult
2990-
LegalizerHelper::lowerFConstant(MachineInstr &MI) {
2991-
Register Dst = MI.getOperand(0).getReg();
2992-
2989+
static void emitLoadFromConstantPool(Register DstReg, const Constant *ConstVal,
2990+
MachineIRBuilder &MIRBuilder) {
2991+
MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
29932992
MachineFunction &MF = MIRBuilder.getMF();
29942993
const DataLayout &DL = MIRBuilder.getDataLayout();
2995-
29962994
unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
29972995
LLT AddrPtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
2998-
Align Alignment = Align(DL.getABITypeAlign(
2999-
getFloatTypeForLLT(MF.getFunction().getContext(), MRI.getType(Dst))));
2996+
LLT DstLLT = MRI.getType(DstReg);
2997+
2998+
Align Alignment(DL.getABITypeAlign(ConstVal->getType()));
30002999

30013000
auto Addr = MIRBuilder.buildConstantPool(
3002-
AddrPtrTy, MF.getConstantPool()->getConstantPoolIndex(
3003-
MI.getOperand(1).getFPImm(), Alignment));
3001+
AddrPtrTy,
3002+
MF.getConstantPool()->getConstantPoolIndex(ConstVal, Alignment));
30043003

3005-
MachineMemOperand *MMO = MF.getMachineMemOperand(
3006-
MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad,
3007-
MRI.getType(Dst), Alignment);
3004+
MachineMemOperand *MMO =
3005+
MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
3006+
MachineMemOperand::MOLoad, DstLLT, Alignment);
3007+
3008+
MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, DstReg, Addr, *MMO);
3009+
}
3010+
3011+
LegalizerHelper::LegalizeResult
3012+
LegalizerHelper::lowerConstant(MachineInstr &MI) {
3013+
const MachineOperand &ConstOperand = MI.getOperand(1);
3014+
const Constant *ConstantVal = ConstOperand.getCImm();
3015+
3016+
emitLoadFromConstantPool(MI.getOperand(0).getReg(), ConstantVal, MIRBuilder);
3017+
MI.eraseFromParent();
3018+
3019+
return Legalized;
3020+
}
3021+
3022+
LegalizerHelper::LegalizeResult
3023+
LegalizerHelper::lowerFConstant(MachineInstr &MI) {
3024+
const MachineOperand &ConstOperand = MI.getOperand(1);
3025+
const Constant *ConstantVal = ConstOperand.getFPImm();
30083026

3009-
MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, Dst, Addr, *MMO);
3027+
emitLoadFromConstantPool(MI.getOperand(0).getReg(), ConstantVal, MIRBuilder);
30103028
MI.eraseFromParent();
30113029

30123030
return Legalized;

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -495,33 +495,6 @@ bool RISCVLegalizerInfo::shouldBeInConstantPool(APInt APImm,
495495
return !(!SeqLo.empty() && (SeqLo.size() + 2) <= STI.getMaxBuildIntsCost());
496496
}
497497

498-
// TODO: This is almost the same as LegalizerHelper::lowerFConstant and is
499-
// target-independent. Should we move this to LegalizeHelper?
500-
bool RISCVLegalizerInfo::emitLoadFromConstantPool(
501-
Register DstReg, const Constant *ConstVal,
502-
MachineIRBuilder &MIRBuilder) const {
503-
MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
504-
MachineFunction &MF = MIRBuilder.getMF();
505-
const DataLayout &DL = MIRBuilder.getDataLayout();
506-
LLVMContext &Ctx = MF.getFunction().getContext();
507-
unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
508-
LLT AddrPtrTy = LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
509-
LLT DstLLT = MRI.getType(DstReg);
510-
511-
Align Alignment(DL.getABITypeAlign(getTypeForLLT(DstLLT, Ctx)));
512-
513-
auto Addr = MIRBuilder.buildConstantPool(
514-
AddrPtrTy,
515-
MF.getConstantPool()->getConstantPoolIndex(ConstVal, Alignment));
516-
517-
MachineMemOperand *MMO =
518-
MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
519-
MachineMemOperand::MOLoad, DstLLT, Alignment);
520-
521-
MIRBuilder.buildLoadInstr(TargetOpcode::G_LOAD, DstReg, Addr, *MMO);
522-
return true;
523-
}
524-
525498
bool RISCVLegalizerInfo::legalizeCustom(
526499
LegalizerHelper &Helper, MachineInstr &MI,
527500
LostDebugLocObserver &LocObserver) const {
@@ -543,10 +516,7 @@ bool RISCVLegalizerInfo::legalizeCustom(
543516
const ConstantInt *ConstVal = MI.getOperand(1).getCImm();
544517
if (!shouldBeInConstantPool(ConstVal->getValue(), ShouldOptForSize))
545518
return true;
546-
emitLoadFromConstantPool(MI.getOperand(0).getReg(),
547-
MI.getOperand(1).getCImm(), MIRBuilder);
548-
MI.eraseFromParent();
549-
return true;
519+
return Helper.lowerConstant(MI);
550520
}
551521
case TargetOpcode::G_SHL:
552522
case TargetOpcode::G_ASHR:

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class RISCVLegalizerInfo : public LegalizerInfo {
3838

3939
private:
4040
bool shouldBeInConstantPool(APInt APImm, bool ShouldOptForSize) const;
41-
bool emitLoadFromConstantPool(Register DstReg, const Constant *CPVal,
42-
MachineIRBuilder &MIRBuilder) const;
4341
bool legalizeShlAshrLshr(MachineInstr &MI, MachineIRBuilder &MIRBuilder,
4442
GISelChangeObserver &Observer) const;
4543

0 commit comments

Comments
 (0)