Skip to content

Commit cf922e5

Browse files
authored
[BPF] lowering target address leaf nodes tconstpool (#73667)
Adds custom lowering for tconstpool. Please ref: #73668 for test coverage
1 parent 5ddc5b8 commit cf922e5

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

llvm/lib/Target/BPF/BPFISelLowering.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM,
6969
setOperationAction(ISD::BRIND, MVT::Other, Expand);
7070
setOperationAction(ISD::BRCOND, MVT::Other, Expand);
7171

72-
setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
72+
setOperationAction({ISD::GlobalAddress, ISD::ConstantPool}, MVT::i64, Custom);
7373

7474
setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
7575
setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
@@ -308,6 +308,8 @@ SDValue BPFTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
308308
return LowerBR_CC(Op, DAG);
309309
case ISD::GlobalAddress:
310310
return LowerGlobalAddress(Op, DAG);
311+
case ISD::ConstantPool:
312+
return LowerConstantPool(Op, DAG);
311313
case ISD::SELECT_CC:
312314
return LowerSELECT_CC(Op, DAG);
313315
case ISD::SDIV:
@@ -691,18 +693,41 @@ const char *BPFTargetLowering::getTargetNodeName(unsigned Opcode) const {
691693
return nullptr;
692694
}
693695

696+
static SDValue getTargetNode(GlobalAddressSDNode *N, const SDLoc &DL, EVT Ty,
697+
SelectionDAG &DAG, unsigned Flags) {
698+
return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
699+
}
700+
701+
static SDValue getTargetNode(ConstantPoolSDNode *N, const SDLoc &DL, EVT Ty,
702+
SelectionDAG &DAG, unsigned Flags) {
703+
return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
704+
N->getOffset(), Flags);
705+
}
706+
707+
template <class NodeTy>
708+
SDValue BPFTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
709+
unsigned Flags) const {
710+
SDLoc DL(N);
711+
712+
SDValue GA = getTargetNode(N, DL, MVT::i64, DAG, Flags);
713+
714+
return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
715+
}
716+
694717
SDValue BPFTargetLowering::LowerGlobalAddress(SDValue Op,
695718
SelectionDAG &DAG) const {
696-
auto *N = cast<GlobalAddressSDNode>(Op);
719+
GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
697720
if (N->getOffset() != 0)
698721
report_fatal_error("invalid offset for global address: " +
699722
Twine(N->getOffset()));
723+
return getAddr(N, DAG);
724+
}
700725

701-
SDLoc DL(Op);
702-
const GlobalValue *GV = N->getGlobal();
703-
SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i64);
726+
SDValue BPFTargetLowering::LowerConstantPool(SDValue Op,
727+
SelectionDAG &DAG) const {
728+
ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
704729

705-
return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
730+
return getAddr(N, DAG);
706731
}
707732

708733
unsigned

llvm/lib/Target/BPF/BPFISelLowering.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ class BPFTargetLowering : public TargetLowering {
7777
SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
7878
SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
7979
SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
80+
81+
SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
8082
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
8183

84+
template <class NodeTy>
85+
SDValue getAddr(NodeTy *N, SelectionDAG &DAG, unsigned Flags = 0) const;
86+
8287
// Lower the result values of a call, copying them out of physregs into vregs
8388
SDValue LowerCallResult(SDValue Chain, SDValue InGlue,
8489
CallingConv::ID CallConv, bool IsVarArg,

llvm/lib/Target/BPF/BPFInstrInfo.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ let usesCustomInserter = 1, isCodeGenOnly = 1 in {
727727

728728
// load 64-bit global addr into register
729729
def : Pat<(BPFWrapper tglobaladdr:$in), (LD_imm64 tglobaladdr:$in)>;
730+
def : Pat<(BPFWrapper tconstpool:$in), (LD_imm64 tconstpool:$in)>;
730731

731732
// 0xffffFFFF doesn't fit into simm32, optimize common case
732733
def : Pat<(i64 (and (i64 GPR:$src), 0xffffFFFF)),

llvm/lib/Target/BPF/BPFMCInstLower.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
7474
case MachineOperand::MO_GlobalAddress:
7575
MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
7676
break;
77+
case MachineOperand::MO_ConstantPoolIndex:
78+
MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
79+
break;
7780
}
7881

7982
OutMI.addOperand(MCOp);

0 commit comments

Comments
 (0)