Skip to content

Commit c41b11a

Browse files
committed
[BPF] lowering target address leaf nodes (tconstpool, tblockaddr, tjumptable)
1 parent 01091fd commit c41b11a

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

llvm/lib/Target/BPF/BPFISelLowering.cpp

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ 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::BlockAddress, ISD::ConstantPool,
73+
ISD::JumpTable},
74+
MVT::i64, Custom);
7375

7476
setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
7577
setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
@@ -305,6 +307,12 @@ SDValue BPFTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
305307
return LowerBR_CC(Op, DAG);
306308
case ISD::GlobalAddress:
307309
return LowerGlobalAddress(Op, DAG);
310+
case ISD::BlockAddress:
311+
return LowerBlockAddress(Op, DAG);
312+
case ISD::ConstantPool:
313+
return LowerConstantPool(Op, DAG);
314+
case ISD::JumpTable:
315+
return LowerJumpTable(Op, DAG);
308316
case ISD::SELECT_CC:
309317
return LowerSELECT_CC(Op, DAG);
310318
case ISD::DYNAMIC_STACKALLOC:
@@ -670,18 +678,65 @@ const char *BPFTargetLowering::getTargetNodeName(unsigned Opcode) const {
670678
return nullptr;
671679
}
672680

681+
static SDValue getTargetNode(GlobalAddressSDNode *N, const SDLoc &DL, EVT Ty,
682+
SelectionDAG &DAG, unsigned Flags) {
683+
return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
684+
}
685+
686+
static SDValue getTargetNode(BlockAddressSDNode *N, const SDLoc &DL, EVT Ty,
687+
SelectionDAG &DAG, unsigned Flags) {
688+
return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, N->getOffset(),
689+
Flags);
690+
}
691+
692+
static SDValue getTargetNode(ConstantPoolSDNode *N, const SDLoc &DL, EVT Ty,
693+
SelectionDAG &DAG, unsigned Flags) {
694+
return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
695+
N->getOffset(), Flags);
696+
}
697+
698+
static SDValue getTargetNode(JumpTableSDNode *N, const SDLoc &DL, EVT Ty,
699+
SelectionDAG &DAG, unsigned Flags) {
700+
return DAG.getTargetJumpTable(N->getIndex(), Ty, Flags);
701+
}
702+
703+
template <class NodeTy>
704+
SDValue BPFTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
705+
unsigned Flags) const {
706+
SDLoc DL(N);
707+
708+
SDValue GA = getTargetNode(N, DL, MVT::i64, DAG, Flags);
709+
710+
return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
711+
}
712+
673713
SDValue BPFTargetLowering::LowerGlobalAddress(SDValue Op,
674714
SelectionDAG &DAG) const {
675-
auto *N = cast<GlobalAddressSDNode>(Op);
715+
GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
676716
if (N->getOffset() != 0)
677717
report_fatal_error("invalid offset for global address: " +
678718
Twine(N->getOffset()));
719+
return getAddr(N, DAG);
720+
}
679721

680-
SDLoc DL(Op);
681-
const GlobalValue *GV = N->getGlobal();
682-
SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i64);
722+
SDValue BPFTargetLowering::LowerBlockAddress(SDValue Op,
723+
SelectionDAG &DAG) const {
724+
BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
683725

684-
return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
726+
return getAddr(N, DAG);
727+
}
728+
729+
SDValue BPFTargetLowering::LowerConstantPool(SDValue Op,
730+
SelectionDAG &DAG) const {
731+
ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
732+
733+
return getAddr(N, DAG);
734+
}
735+
736+
SDValue BPFTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
737+
JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
738+
739+
return getAddr(N, DAG);
685740
}
686741

687742
unsigned

llvm/lib/Target/BPF/BPFISelLowering.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@ class BPFTargetLowering : public TargetLowering {
7575

7676
SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
7777
SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
78+
79+
SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
80+
SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
7881
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
82+
SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
83+
84+
template <class NodeTy>
85+
SDValue getAddr(NodeTy *N, SelectionDAG &DAG, unsigned Flags = 0) const;
7986

8087
// Lower the result values of a call, copying them out of physregs into vregs
8188
SDValue LowerCallResult(SDValue Chain, SDValue InGlue,

llvm/lib/Target/BPF/BPFInstrInfo.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ 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)>;
731+
def : Pat<(BPFWrapper tblockaddress:$in), (LD_imm64 tblockaddress:$in)>;
732+
def : Pat<(BPFWrapper tjumptable:$in), (LD_imm64 tjumptable:$in)>;
730733

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

llvm/lib/Target/BPF/BPFMCInstLower.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ 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_JumpTableIndex:
78+
MCOp = LowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
79+
break;
80+
case MachineOperand::MO_ConstantPoolIndex:
81+
MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
82+
break;
83+
case MachineOperand::MO_BlockAddress:
84+
MCOp = LowerSymbolOperand(
85+
MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
86+
break;
7787
}
7888

7989
OutMI.addOperand(MCOp);

0 commit comments

Comments
 (0)