Skip to content
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

[GlobalISel][ARM] Selection of G_CONSTANT using constant pool #96225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions llvm/lib/Target/ARM/ARMInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class ARMInstructionSelector : public InstructionSelector {
bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
bool selectShift(unsigned ShiftOpc, MachineInstrBuilder &MIB) const;

bool selectConstantUsingPool(MachineInstrBuilder &MIB) const;
void emitLoadFromConstantPool(const Register DefReg, const Constant *CPVal,
MachineInstrBuilder &MIB) const;

// Check if the types match and both operands have the expected size and
// register bank.
bool validOpRegPair(MachineRegisterInfo &MRI, unsigned LHS, unsigned RHS,
Expand Down Expand Up @@ -812,6 +816,57 @@ bool ARMInstructionSelector::selectShift(unsigned ShiftOpc,
return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
}

bool ARMInstructionSelector::selectConstantUsingPool(
MachineInstrBuilder &MIB) const {
MachineInstr &MI = *MIB;
assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");

const Register DefReg = MI.getOperand(0).getReg();
MachineOperand &ConstOperand = MI.getOperand(1);
if (!ConstOperand.isCImm())
return false;
const Constant *ConstVal = ConstOperand.getCImm();
uint64_t ImmVal = ConstOperand.getCImm()->getZExtValue();

if (ConstantMaterializationCost(ImmVal, Subtarget) > 2 &&
!Subtarget->genExecuteOnly()) {
emitLoadFromConstantPool(DefReg, ConstVal, MIB);
MI.eraseFromParent();
return true;
}

return false;
}

void ARMInstructionSelector::emitLoadFromConstantPool(
const Register DefReg, const Constant *ConstVal,
MachineInstrBuilder &MIB) const {
MachineBasicBlock &MBB = *MIB->getParent();
MachineFunction &MF = *MBB.getParent();
const DataLayout &DL = MF.getDataLayout();

auto InsertBefore = std::next(MIB->getIterator());
auto &DbgLoc = MIB->getDebugLoc();

Type *ConstTy = ConstVal->getType();
unsigned Size = DL.getTypeStoreSize(ConstTy);
Align Alignment = DL.getPrefTypeAlign(ConstTy);

MachineConstantPool *Pool = MF.getConstantPool();
unsigned ConstIndex = Pool->getConstantPoolIndex(ConstVal, Alignment);

auto Load = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::LDRcp))
.addDef(DefReg)
.addConstantPoolIndex(ConstIndex)
.addImm(0)
.add(predOps(ARMCC::AL));
MachinePointerInfo PtrInfo = MachinePointerInfo::getConstantPool(MF);
Load->addMemOperand(MF, MF.getMachineMemOperand(PtrInfo,
MachineMemOperand::MOLoad,
Size, Alignment));
constrainSelectedInstRegOperands(*Load, TII, TRI, RBI);
}

void ARMInstructionSelector::renderVFPF32Imm(
MachineInstrBuilder &NewInstBuilder, const MachineInstr &OldInst,
int OpIdx) const {
Expand Down Expand Up @@ -970,6 +1025,9 @@ bool ARMInstructionSelector::select(MachineInstr &I) {
return selectCopy(I, TII, MRI, TRI, RBI);
}
case G_CONSTANT: {
if (selectConstantUsingPool(MIB))
return true;

if (!MRI.getType(I.getOperand(0).getReg()).isPointer()) {
// Non-pointer constants should be handled by TableGen.
LLVM_DEBUG(dbgs() << "Unsupported constant type\n");
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/CodeGen/ARM/GlobalISel/select-const.mir
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ body: |
MOVPCLR 14 /* CC::al */, $noreg, implicit $r0

...
---
name: get_from_constpool
legalized: true
regBankSelected: true
selected: false
tracksRegLiveness: true
registers:
- { id: 0, class: gprb, preferred-register: '' }
body: |
bb.1.entry:
; CHECK-LABEL: name: get_from_constpool
; CHECK: [[LDRcp:%[0-9]+]]:gpr = LDRcp %const.0, 0, 14 /* CC::al */, $noreg :: (load (s32) from constant-pool)
; CHECK-NEXT: $r0 = COPY [[LDRcp]]
; CHECK-NEXT: BX_RET 14 /* CC::al */, $noreg, implicit $r0
%0:gprb(s32) = G_CONSTANT i32 287454020
$r0 = COPY %0(s32)
BX_RET 14 /* CC::al */, $noreg, implicit $r0

...
Loading