Skip to content

Commit 1839091

Browse files
committed
[BPF] lowering target address leaf nodes tconstpool
Please ref: #73668 for test coverage
1 parent 01091fd commit 1839091

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);
@@ -305,6 +305,8 @@ SDValue BPFTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
305305
return LowerBR_CC(Op, DAG);
306306
case ISD::GlobalAddress:
307307
return LowerGlobalAddress(Op, DAG);
308+
case ISD::ConstantPool:
309+
return LowerConstantPool(Op, DAG);
308310
case ISD::SELECT_CC:
309311
return LowerSELECT_CC(Op, DAG);
310312
case ISD::DYNAMIC_STACKALLOC:
@@ -670,18 +672,41 @@ const char *BPFTargetLowering::getTargetNodeName(unsigned Opcode) const {
670672
return nullptr;
671673
}
672674

675+
static SDValue getTargetNode(GlobalAddressSDNode *N, const SDLoc &DL, EVT Ty,
676+
SelectionDAG &DAG, unsigned Flags) {
677+
return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
678+
}
679+
680+
static SDValue getTargetNode(ConstantPoolSDNode *N, const SDLoc &DL, EVT Ty,
681+
SelectionDAG &DAG, unsigned Flags) {
682+
return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
683+
N->getOffset(), Flags);
684+
}
685+
686+
template <class NodeTy>
687+
SDValue BPFTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
688+
unsigned Flags) const {
689+
SDLoc DL(N);
690+
691+
SDValue GA = getTargetNode(N, DL, MVT::i64, DAG, Flags);
692+
693+
return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
694+
}
695+
673696
SDValue BPFTargetLowering::LowerGlobalAddress(SDValue Op,
674697
SelectionDAG &DAG) const {
675-
auto *N = cast<GlobalAddressSDNode>(Op);
698+
GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
676699
if (N->getOffset() != 0)
677700
report_fatal_error("invalid offset for global address: " +
678701
Twine(N->getOffset()));
702+
return getAddr(N, DAG);
703+
}
679704

680-
SDLoc DL(Op);
681-
const GlobalValue *GV = N->getGlobal();
682-
SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i64);
705+
SDValue BPFTargetLowering::LowerConstantPool(SDValue Op,
706+
SelectionDAG &DAG) const {
707+
ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
683708

684-
return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
709+
return getAddr(N, DAG);
685710
}
686711

687712
unsigned

llvm/lib/Target/BPF/BPFISelLowering.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ 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;
7880
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
7981

82+
template <class NodeTy>
83+
SDValue getAddr(NodeTy *N, SelectionDAG &DAG, unsigned Flags = 0) const;
84+
8085
// Lower the result values of a call, copying them out of physregs into vregs
8186
SDValue LowerCallResult(SDValue Chain, SDValue InGlue,
8287
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)