BPFISelLowering.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===-- BPFISelLowering.h - BPF DAG Lowering Interface ----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the interfaces that BPF uses to lower LLVM code into a
  10. // selection DAG.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_TARGET_BPF_BPFISELLOWERING_H
  14. #define LLVM_LIB_TARGET_BPF_BPFISELLOWERING_H
  15. #include "BPF.h"
  16. #include "llvm/CodeGen/SelectionDAG.h"
  17. #include "llvm/CodeGen/TargetLowering.h"
  18. namespace llvm {
  19. class BPFSubtarget;
  20. namespace BPFISD {
  21. enum NodeType : unsigned {
  22. FIRST_NUMBER = ISD::BUILTIN_OP_END,
  23. RET_FLAG,
  24. CALL,
  25. SELECT_CC,
  26. BR_CC,
  27. Wrapper,
  28. MEMCPY
  29. };
  30. }
  31. class BPFTargetLowering : public TargetLowering {
  32. public:
  33. explicit BPFTargetLowering(const TargetMachine &TM, const BPFSubtarget &STI);
  34. // Provide custom lowering hooks for some operations.
  35. SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
  36. // This method returns the name of a target specific DAG node.
  37. const char *getTargetNodeName(unsigned Opcode) const override;
  38. // This method decides whether folding a constant offset
  39. // with the given GlobalAddress is legal.
  40. bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
  41. BPFTargetLowering::ConstraintType
  42. getConstraintType(StringRef Constraint) const override;
  43. std::pair<unsigned, const TargetRegisterClass *>
  44. getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
  45. StringRef Constraint, MVT VT) const override;
  46. MachineBasicBlock *
  47. EmitInstrWithCustomInserter(MachineInstr &MI,
  48. MachineBasicBlock *BB) const override;
  49. bool getHasAlu32() const { return HasAlu32; }
  50. bool getHasJmp32() const { return HasJmp32; }
  51. bool getHasJmpExt() const { return HasJmpExt; }
  52. EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
  53. EVT VT) const override;
  54. MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override;
  55. private:
  56. // Control Instruction Selection Features
  57. bool HasAlu32;
  58. bool HasJmp32;
  59. bool HasJmpExt;
  60. SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
  61. SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
  62. SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
  63. // Lower the result values of a call, copying them out of physregs into vregs
  64. SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
  65. CallingConv::ID CallConv, bool IsVarArg,
  66. const SmallVectorImpl<ISD::InputArg> &Ins,
  67. const SDLoc &DL, SelectionDAG &DAG,
  68. SmallVectorImpl<SDValue> &InVals) const;
  69. // Maximum number of arguments to a call
  70. static const unsigned MaxArgs;
  71. // Lower a call into CALLSEQ_START - BPFISD:CALL - CALLSEQ_END chain
  72. SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI,
  73. SmallVectorImpl<SDValue> &InVals) const override;
  74. // Lower incoming arguments, copy physregs into vregs
  75. SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
  76. bool IsVarArg,
  77. const SmallVectorImpl<ISD::InputArg> &Ins,
  78. const SDLoc &DL, SelectionDAG &DAG,
  79. SmallVectorImpl<SDValue> &InVals) const override;
  80. SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
  81. const SmallVectorImpl<ISD::OutputArg> &Outs,
  82. const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
  83. SelectionDAG &DAG) const override;
  84. void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
  85. SelectionDAG &DAG) const override;
  86. EVT getOptimalMemOpType(const MemOp &Op,
  87. const AttributeList &FuncAttributes) const override {
  88. return Op.size() >= 8 ? MVT::i64 : MVT::i32;
  89. }
  90. bool isIntDivCheap(EVT VT, AttributeList Attr) const override { return true; }
  91. bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
  92. Type *Ty) const override {
  93. return true;
  94. }
  95. // Prevent reducing load width during SelectionDag phase.
  96. // Otherwise, we may transform the following
  97. // ctx = ctx + reloc_offset
  98. // ... (*(u32 *)ctx) & 0x8000...
  99. // to
  100. // ctx = ctx + reloc_offset
  101. // ... (*(u8 *)(ctx + 1)) & 0x80 ...
  102. // which will be rejected by the verifier.
  103. bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy,
  104. EVT NewVT) const override {
  105. return false;
  106. }
  107. bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM,
  108. Type *Ty, unsigned AS,
  109. Instruction *I = nullptr) const override;
  110. // isTruncateFree - Return true if it's free to truncate a value of
  111. // type Ty1 to type Ty2. e.g. On BPF at alu32 mode, it's free to truncate
  112. // a i64 value in register R1 to i32 by referencing its sub-register W1.
  113. bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
  114. bool isTruncateFree(EVT VT1, EVT VT2) const override;
  115. // For 32bit ALU result zext to 64bit is free.
  116. bool isZExtFree(Type *Ty1, Type *Ty2) const override;
  117. bool isZExtFree(EVT VT1, EVT VT2) const override;
  118. unsigned EmitSubregExt(MachineInstr &MI, MachineBasicBlock *BB, unsigned Reg,
  119. bool isSigned) const;
  120. MachineBasicBlock * EmitInstrWithCustomInserterMemcpy(MachineInstr &MI,
  121. MachineBasicBlock *BB)
  122. const;
  123. };
  124. }
  125. #endif