BPFISelLowering.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. //===-- BPFISelLowering.cpp - BPF DAG Lowering Implementation ------------===//
  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. #include "BPFISelLowering.h"
  14. #include "BPF.h"
  15. #include "BPFSubtarget.h"
  16. #include "BPFTargetMachine.h"
  17. #include "llvm/CodeGen/CallingConvLower.h"
  18. #include "llvm/CodeGen/MachineFrameInfo.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
  23. #include "llvm/CodeGen/ValueTypes.h"
  24. #include "llvm/IR/DiagnosticInfo.h"
  25. #include "llvm/IR/DiagnosticPrinter.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "bpf-lower"
  31. static cl::opt<bool> BPFExpandMemcpyInOrder("bpf-expand-memcpy-in-order",
  32. cl::Hidden, cl::init(false),
  33. cl::desc("Expand memcpy into load/store pairs in order"));
  34. static void fail(const SDLoc &DL, SelectionDAG &DAG, const Twine &Msg) {
  35. MachineFunction &MF = DAG.getMachineFunction();
  36. DAG.getContext()->diagnose(
  37. DiagnosticInfoUnsupported(MF.getFunction(), Msg, DL.getDebugLoc()));
  38. }
  39. static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *Msg,
  40. SDValue Val) {
  41. MachineFunction &MF = DAG.getMachineFunction();
  42. std::string Str;
  43. raw_string_ostream OS(Str);
  44. OS << Msg;
  45. Val->print(OS);
  46. OS.flush();
  47. DAG.getContext()->diagnose(
  48. DiagnosticInfoUnsupported(MF.getFunction(), Str, DL.getDebugLoc()));
  49. }
  50. BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM,
  51. const BPFSubtarget &STI)
  52. : TargetLowering(TM) {
  53. // Set up the register classes.
  54. addRegisterClass(MVT::i64, &BPF::GPRRegClass);
  55. if (STI.getHasAlu32())
  56. addRegisterClass(MVT::i32, &BPF::GPR32RegClass);
  57. // Compute derived properties from the register classes
  58. computeRegisterProperties(STI.getRegisterInfo());
  59. setStackPointerRegisterToSaveRestore(BPF::R11);
  60. setOperationAction(ISD::BR_CC, MVT::i64, Custom);
  61. setOperationAction(ISD::BR_JT, MVT::Other, Expand);
  62. setOperationAction(ISD::BRIND, MVT::Other, Expand);
  63. setOperationAction(ISD::BRCOND, MVT::Other, Expand);
  64. setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
  65. setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
  66. setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
  67. setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
  68. // Set unsupported atomic operations as Custom so
  69. // we can emit better error messages than fatal error
  70. // from selectiondag.
  71. for (auto VT : {MVT::i8, MVT::i16, MVT::i32}) {
  72. if (VT == MVT::i32) {
  73. if (STI.getHasAlu32())
  74. continue;
  75. } else {
  76. setOperationAction(ISD::ATOMIC_LOAD_ADD, VT, Custom);
  77. }
  78. setOperationAction(ISD::ATOMIC_LOAD_AND, VT, Custom);
  79. setOperationAction(ISD::ATOMIC_LOAD_OR, VT, Custom);
  80. setOperationAction(ISD::ATOMIC_LOAD_XOR, VT, Custom);
  81. setOperationAction(ISD::ATOMIC_SWAP, VT, Custom);
  82. setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Custom);
  83. }
  84. for (auto VT : { MVT::i32, MVT::i64 }) {
  85. if (VT == MVT::i32 && !STI.getHasAlu32())
  86. continue;
  87. setOperationAction(ISD::SDIVREM, VT, Expand);
  88. setOperationAction(ISD::UDIVREM, VT, Expand);
  89. setOperationAction(ISD::SREM, VT, Expand);
  90. setOperationAction(ISD::UREM, VT, Expand);
  91. setOperationAction(ISD::MULHU, VT, Expand);
  92. setOperationAction(ISD::MULHS, VT, Expand);
  93. setOperationAction(ISD::UMUL_LOHI, VT, Expand);
  94. setOperationAction(ISD::SMUL_LOHI, VT, Expand);
  95. setOperationAction(ISD::ROTR, VT, Expand);
  96. setOperationAction(ISD::ROTL, VT, Expand);
  97. setOperationAction(ISD::SHL_PARTS, VT, Expand);
  98. setOperationAction(ISD::SRL_PARTS, VT, Expand);
  99. setOperationAction(ISD::SRA_PARTS, VT, Expand);
  100. setOperationAction(ISD::CTPOP, VT, Expand);
  101. setOperationAction(ISD::SETCC, VT, Expand);
  102. setOperationAction(ISD::SELECT, VT, Expand);
  103. setOperationAction(ISD::SELECT_CC, VT, Custom);
  104. }
  105. if (STI.getHasAlu32()) {
  106. setOperationAction(ISD::BSWAP, MVT::i32, Promote);
  107. setOperationAction(ISD::BR_CC, MVT::i32,
  108. STI.getHasJmp32() ? Custom : Promote);
  109. }
  110. setOperationAction(ISD::CTTZ, MVT::i64, Custom);
  111. setOperationAction(ISD::CTLZ, MVT::i64, Custom);
  112. setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Custom);
  113. setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
  114. setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
  115. setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
  116. setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
  117. setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
  118. // Extended load operations for i1 types must be promoted
  119. for (MVT VT : MVT::integer_valuetypes()) {
  120. setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
  121. setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
  122. setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
  123. setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
  124. setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Expand);
  125. setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
  126. }
  127. setBooleanContents(ZeroOrOneBooleanContent);
  128. // Function alignments
  129. setMinFunctionAlignment(Align(8));
  130. setPrefFunctionAlignment(Align(8));
  131. if (BPFExpandMemcpyInOrder) {
  132. // LLVM generic code will try to expand memcpy into load/store pairs at this
  133. // stage which is before quite a few IR optimization passes, therefore the
  134. // loads and stores could potentially be moved apart from each other which
  135. // will cause trouble to memcpy pattern matcher inside kernel eBPF JIT
  136. // compilers.
  137. //
  138. // When -bpf-expand-memcpy-in-order specified, we want to defer the expand
  139. // of memcpy to later stage in IR optimization pipeline so those load/store
  140. // pairs won't be touched and could be kept in order. Hence, we set
  141. // MaxStoresPerMem* to zero to disable the generic getMemcpyLoadsAndStores
  142. // code path, and ask LLVM to use target expander EmitTargetCodeForMemcpy.
  143. MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 0;
  144. MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 0;
  145. MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize = 0;
  146. } else {
  147. // inline memcpy() for kernel to see explicit copy
  148. unsigned CommonMaxStores =
  149. STI.getSelectionDAGInfo()->getCommonMaxStoresPerMemFunc();
  150. MaxStoresPerMemset = MaxStoresPerMemsetOptSize = CommonMaxStores;
  151. MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = CommonMaxStores;
  152. MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize = CommonMaxStores;
  153. }
  154. // CPU/Feature control
  155. HasAlu32 = STI.getHasAlu32();
  156. HasJmp32 = STI.getHasJmp32();
  157. HasJmpExt = STI.getHasJmpExt();
  158. }
  159. bool BPFTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
  160. return false;
  161. }
  162. bool BPFTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
  163. if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
  164. return false;
  165. unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
  166. unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
  167. return NumBits1 > NumBits2;
  168. }
  169. bool BPFTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
  170. if (!VT1.isInteger() || !VT2.isInteger())
  171. return false;
  172. unsigned NumBits1 = VT1.getSizeInBits();
  173. unsigned NumBits2 = VT2.getSizeInBits();
  174. return NumBits1 > NumBits2;
  175. }
  176. bool BPFTargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
  177. if (!getHasAlu32() || !Ty1->isIntegerTy() || !Ty2->isIntegerTy())
  178. return false;
  179. unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
  180. unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
  181. return NumBits1 == 32 && NumBits2 == 64;
  182. }
  183. bool BPFTargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
  184. if (!getHasAlu32() || !VT1.isInteger() || !VT2.isInteger())
  185. return false;
  186. unsigned NumBits1 = VT1.getSizeInBits();
  187. unsigned NumBits2 = VT2.getSizeInBits();
  188. return NumBits1 == 32 && NumBits2 == 64;
  189. }
  190. BPFTargetLowering::ConstraintType
  191. BPFTargetLowering::getConstraintType(StringRef Constraint) const {
  192. if (Constraint.size() == 1) {
  193. switch (Constraint[0]) {
  194. default:
  195. break;
  196. case 'w':
  197. return C_RegisterClass;
  198. }
  199. }
  200. return TargetLowering::getConstraintType(Constraint);
  201. }
  202. std::pair<unsigned, const TargetRegisterClass *>
  203. BPFTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
  204. StringRef Constraint,
  205. MVT VT) const {
  206. if (Constraint.size() == 1)
  207. // GCC Constraint Letters
  208. switch (Constraint[0]) {
  209. case 'r': // GENERAL_REGS
  210. return std::make_pair(0U, &BPF::GPRRegClass);
  211. case 'w':
  212. if (HasAlu32)
  213. return std::make_pair(0U, &BPF::GPR32RegClass);
  214. break;
  215. default:
  216. break;
  217. }
  218. return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
  219. }
  220. void BPFTargetLowering::ReplaceNodeResults(
  221. SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const {
  222. const char *err_msg;
  223. uint32_t Opcode = N->getOpcode();
  224. switch (Opcode) {
  225. default:
  226. report_fatal_error("Unhandled custom legalization");
  227. case ISD::ATOMIC_LOAD_ADD:
  228. case ISD::ATOMIC_LOAD_AND:
  229. case ISD::ATOMIC_LOAD_OR:
  230. case ISD::ATOMIC_LOAD_XOR:
  231. case ISD::ATOMIC_SWAP:
  232. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
  233. if (HasAlu32 || Opcode == ISD::ATOMIC_LOAD_ADD)
  234. err_msg = "Unsupported atomic operations, please use 32/64 bit version";
  235. else
  236. err_msg = "Unsupported atomic operations, please use 64 bit version";
  237. break;
  238. }
  239. SDLoc DL(N);
  240. fail(DL, DAG, err_msg);
  241. }
  242. SDValue BPFTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
  243. switch (Op.getOpcode()) {
  244. case ISD::BR_CC:
  245. return LowerBR_CC(Op, DAG);
  246. case ISD::GlobalAddress:
  247. return LowerGlobalAddress(Op, DAG);
  248. case ISD::SELECT_CC:
  249. return LowerSELECT_CC(Op, DAG);
  250. case ISD::DYNAMIC_STACKALLOC:
  251. report_fatal_error("Unsupported dynamic stack allocation");
  252. default:
  253. llvm_unreachable("unimplemented operand");
  254. }
  255. }
  256. // Calling Convention Implementation
  257. #include "BPFGenCallingConv.inc"
  258. SDValue BPFTargetLowering::LowerFormalArguments(
  259. SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
  260. const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
  261. SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
  262. switch (CallConv) {
  263. default:
  264. report_fatal_error("Unsupported calling convention");
  265. case CallingConv::C:
  266. case CallingConv::Fast:
  267. break;
  268. }
  269. MachineFunction &MF = DAG.getMachineFunction();
  270. MachineRegisterInfo &RegInfo = MF.getRegInfo();
  271. // Assign locations to all of the incoming arguments.
  272. SmallVector<CCValAssign, 16> ArgLocs;
  273. CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
  274. CCInfo.AnalyzeFormalArguments(Ins, getHasAlu32() ? CC_BPF32 : CC_BPF64);
  275. for (auto &VA : ArgLocs) {
  276. if (VA.isRegLoc()) {
  277. // Arguments passed in registers
  278. EVT RegVT = VA.getLocVT();
  279. MVT::SimpleValueType SimpleTy = RegVT.getSimpleVT().SimpleTy;
  280. switch (SimpleTy) {
  281. default: {
  282. errs() << "LowerFormalArguments Unhandled argument type: "
  283. << RegVT.getEVTString() << '\n';
  284. llvm_unreachable(nullptr);
  285. }
  286. case MVT::i32:
  287. case MVT::i64:
  288. Register VReg = RegInfo.createVirtualRegister(
  289. SimpleTy == MVT::i64 ? &BPF::GPRRegClass : &BPF::GPR32RegClass);
  290. RegInfo.addLiveIn(VA.getLocReg(), VReg);
  291. SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, RegVT);
  292. // If this is an value that has been promoted to wider types, insert an
  293. // assert[sz]ext to capture this, then truncate to the right size.
  294. if (VA.getLocInfo() == CCValAssign::SExt)
  295. ArgValue = DAG.getNode(ISD::AssertSext, DL, RegVT, ArgValue,
  296. DAG.getValueType(VA.getValVT()));
  297. else if (VA.getLocInfo() == CCValAssign::ZExt)
  298. ArgValue = DAG.getNode(ISD::AssertZext, DL, RegVT, ArgValue,
  299. DAG.getValueType(VA.getValVT()));
  300. if (VA.getLocInfo() != CCValAssign::Full)
  301. ArgValue = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), ArgValue);
  302. InVals.push_back(ArgValue);
  303. break;
  304. }
  305. } else {
  306. fail(DL, DAG, "defined with too many args");
  307. InVals.push_back(DAG.getConstant(0, DL, VA.getLocVT()));
  308. }
  309. }
  310. if (IsVarArg || MF.getFunction().hasStructRetAttr()) {
  311. fail(DL, DAG, "functions with VarArgs or StructRet are not supported");
  312. }
  313. return Chain;
  314. }
  315. const unsigned BPFTargetLowering::MaxArgs = 5;
  316. SDValue BPFTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
  317. SmallVectorImpl<SDValue> &InVals) const {
  318. SelectionDAG &DAG = CLI.DAG;
  319. auto &Outs = CLI.Outs;
  320. auto &OutVals = CLI.OutVals;
  321. auto &Ins = CLI.Ins;
  322. SDValue Chain = CLI.Chain;
  323. SDValue Callee = CLI.Callee;
  324. bool &IsTailCall = CLI.IsTailCall;
  325. CallingConv::ID CallConv = CLI.CallConv;
  326. bool IsVarArg = CLI.IsVarArg;
  327. MachineFunction &MF = DAG.getMachineFunction();
  328. // BPF target does not support tail call optimization.
  329. IsTailCall = false;
  330. switch (CallConv) {
  331. default:
  332. report_fatal_error("Unsupported calling convention");
  333. case CallingConv::Fast:
  334. case CallingConv::C:
  335. break;
  336. }
  337. // Analyze operands of the call, assigning locations to each operand.
  338. SmallVector<CCValAssign, 16> ArgLocs;
  339. CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
  340. CCInfo.AnalyzeCallOperands(Outs, getHasAlu32() ? CC_BPF32 : CC_BPF64);
  341. unsigned NumBytes = CCInfo.getNextStackOffset();
  342. if (Outs.size() > MaxArgs)
  343. fail(CLI.DL, DAG, "too many args to ", Callee);
  344. for (auto &Arg : Outs) {
  345. ISD::ArgFlagsTy Flags = Arg.Flags;
  346. if (!Flags.isByVal())
  347. continue;
  348. fail(CLI.DL, DAG, "pass by value not supported ", Callee);
  349. }
  350. auto PtrVT = getPointerTy(MF.getDataLayout());
  351. Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
  352. SmallVector<std::pair<unsigned, SDValue>, MaxArgs> RegsToPass;
  353. // Walk arg assignments
  354. for (unsigned i = 0,
  355. e = std::min(static_cast<unsigned>(ArgLocs.size()), MaxArgs);
  356. i != e; ++i) {
  357. CCValAssign &VA = ArgLocs[i];
  358. SDValue Arg = OutVals[i];
  359. // Promote the value if needed.
  360. switch (VA.getLocInfo()) {
  361. default:
  362. llvm_unreachable("Unknown loc info");
  363. case CCValAssign::Full:
  364. break;
  365. case CCValAssign::SExt:
  366. Arg = DAG.getNode(ISD::SIGN_EXTEND, CLI.DL, VA.getLocVT(), Arg);
  367. break;
  368. case CCValAssign::ZExt:
  369. Arg = DAG.getNode(ISD::ZERO_EXTEND, CLI.DL, VA.getLocVT(), Arg);
  370. break;
  371. case CCValAssign::AExt:
  372. Arg = DAG.getNode(ISD::ANY_EXTEND, CLI.DL, VA.getLocVT(), Arg);
  373. break;
  374. }
  375. // Push arguments into RegsToPass vector
  376. if (VA.isRegLoc())
  377. RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
  378. else
  379. llvm_unreachable("call arg pass bug");
  380. }
  381. SDValue InFlag;
  382. // Build a sequence of copy-to-reg nodes chained together with token chain and
  383. // flag operands which copy the outgoing args into registers. The InFlag in
  384. // necessary since all emitted instructions must be stuck together.
  385. for (auto &Reg : RegsToPass) {
  386. Chain = DAG.getCopyToReg(Chain, CLI.DL, Reg.first, Reg.second, InFlag);
  387. InFlag = Chain.getValue(1);
  388. }
  389. // If the callee is a GlobalAddress node (quite common, every direct call is)
  390. // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
  391. // Likewise ExternalSymbol -> TargetExternalSymbol.
  392. if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
  393. Callee = DAG.getTargetGlobalAddress(G->getGlobal(), CLI.DL, PtrVT,
  394. G->getOffset(), 0);
  395. } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
  396. Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, 0);
  397. fail(CLI.DL, DAG, Twine("A call to built-in function '"
  398. + StringRef(E->getSymbol())
  399. + "' is not supported."));
  400. }
  401. // Returns a chain & a flag for retval copy to use.
  402. SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
  403. SmallVector<SDValue, 8> Ops;
  404. Ops.push_back(Chain);
  405. Ops.push_back(Callee);
  406. // Add argument registers to the end of the list so that they are
  407. // known live into the call.
  408. for (auto &Reg : RegsToPass)
  409. Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
  410. if (InFlag.getNode())
  411. Ops.push_back(InFlag);
  412. Chain = DAG.getNode(BPFISD::CALL, CLI.DL, NodeTys, Ops);
  413. InFlag = Chain.getValue(1);
  414. // Create the CALLSEQ_END node.
  415. Chain = DAG.getCALLSEQ_END(
  416. Chain, DAG.getConstant(NumBytes, CLI.DL, PtrVT, true),
  417. DAG.getConstant(0, CLI.DL, PtrVT, true), InFlag, CLI.DL);
  418. InFlag = Chain.getValue(1);
  419. // Handle result values, copying them out of physregs into vregs that we
  420. // return.
  421. return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, CLI.DL, DAG,
  422. InVals);
  423. }
  424. SDValue
  425. BPFTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
  426. bool IsVarArg,
  427. const SmallVectorImpl<ISD::OutputArg> &Outs,
  428. const SmallVectorImpl<SDValue> &OutVals,
  429. const SDLoc &DL, SelectionDAG &DAG) const {
  430. unsigned Opc = BPFISD::RET_FLAG;
  431. // CCValAssign - represent the assignment of the return value to a location
  432. SmallVector<CCValAssign, 16> RVLocs;
  433. MachineFunction &MF = DAG.getMachineFunction();
  434. // CCState - Info about the registers and stack slot.
  435. CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
  436. if (MF.getFunction().getReturnType()->isAggregateType()) {
  437. fail(DL, DAG, "only integer returns supported");
  438. return DAG.getNode(Opc, DL, MVT::Other, Chain);
  439. }
  440. // Analize return values.
  441. CCInfo.AnalyzeReturn(Outs, getHasAlu32() ? RetCC_BPF32 : RetCC_BPF64);
  442. SDValue Flag;
  443. SmallVector<SDValue, 4> RetOps(1, Chain);
  444. // Copy the result values into the output registers.
  445. for (unsigned i = 0; i != RVLocs.size(); ++i) {
  446. CCValAssign &VA = RVLocs[i];
  447. assert(VA.isRegLoc() && "Can only return in registers!");
  448. Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVals[i], Flag);
  449. // Guarantee that all emitted copies are stuck together,
  450. // avoiding something bad.
  451. Flag = Chain.getValue(1);
  452. RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
  453. }
  454. RetOps[0] = Chain; // Update chain.
  455. // Add the flag if we have it.
  456. if (Flag.getNode())
  457. RetOps.push_back(Flag);
  458. return DAG.getNode(Opc, DL, MVT::Other, RetOps);
  459. }
  460. SDValue BPFTargetLowering::LowerCallResult(
  461. SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
  462. const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
  463. SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
  464. MachineFunction &MF = DAG.getMachineFunction();
  465. // Assign locations to each value returned by this call.
  466. SmallVector<CCValAssign, 16> RVLocs;
  467. CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
  468. if (Ins.size() >= 2) {
  469. fail(DL, DAG, "only small returns supported");
  470. for (unsigned i = 0, e = Ins.size(); i != e; ++i)
  471. InVals.push_back(DAG.getConstant(0, DL, Ins[i].VT));
  472. return DAG.getCopyFromReg(Chain, DL, 1, Ins[0].VT, InFlag).getValue(1);
  473. }
  474. CCInfo.AnalyzeCallResult(Ins, getHasAlu32() ? RetCC_BPF32 : RetCC_BPF64);
  475. // Copy all of the result registers out of their specified physreg.
  476. for (auto &Val : RVLocs) {
  477. Chain = DAG.getCopyFromReg(Chain, DL, Val.getLocReg(),
  478. Val.getValVT(), InFlag).getValue(1);
  479. InFlag = Chain.getValue(2);
  480. InVals.push_back(Chain.getValue(0));
  481. }
  482. return Chain;
  483. }
  484. static void NegateCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
  485. switch (CC) {
  486. default:
  487. break;
  488. case ISD::SETULT:
  489. case ISD::SETULE:
  490. case ISD::SETLT:
  491. case ISD::SETLE:
  492. CC = ISD::getSetCCSwappedOperands(CC);
  493. std::swap(LHS, RHS);
  494. break;
  495. }
  496. }
  497. SDValue BPFTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
  498. SDValue Chain = Op.getOperand(0);
  499. ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
  500. SDValue LHS = Op.getOperand(2);
  501. SDValue RHS = Op.getOperand(3);
  502. SDValue Dest = Op.getOperand(4);
  503. SDLoc DL(Op);
  504. if (!getHasJmpExt())
  505. NegateCC(LHS, RHS, CC);
  506. return DAG.getNode(BPFISD::BR_CC, DL, Op.getValueType(), Chain, LHS, RHS,
  507. DAG.getConstant(CC, DL, LHS.getValueType()), Dest);
  508. }
  509. SDValue BPFTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
  510. SDValue LHS = Op.getOperand(0);
  511. SDValue RHS = Op.getOperand(1);
  512. SDValue TrueV = Op.getOperand(2);
  513. SDValue FalseV = Op.getOperand(3);
  514. ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
  515. SDLoc DL(Op);
  516. if (!getHasJmpExt())
  517. NegateCC(LHS, RHS, CC);
  518. SDValue TargetCC = DAG.getConstant(CC, DL, LHS.getValueType());
  519. SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
  520. SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
  521. return DAG.getNode(BPFISD::SELECT_CC, DL, VTs, Ops);
  522. }
  523. const char *BPFTargetLowering::getTargetNodeName(unsigned Opcode) const {
  524. switch ((BPFISD::NodeType)Opcode) {
  525. case BPFISD::FIRST_NUMBER:
  526. break;
  527. case BPFISD::RET_FLAG:
  528. return "BPFISD::RET_FLAG";
  529. case BPFISD::CALL:
  530. return "BPFISD::CALL";
  531. case BPFISD::SELECT_CC:
  532. return "BPFISD::SELECT_CC";
  533. case BPFISD::BR_CC:
  534. return "BPFISD::BR_CC";
  535. case BPFISD::Wrapper:
  536. return "BPFISD::Wrapper";
  537. case BPFISD::MEMCPY:
  538. return "BPFISD::MEMCPY";
  539. }
  540. return nullptr;
  541. }
  542. SDValue BPFTargetLowering::LowerGlobalAddress(SDValue Op,
  543. SelectionDAG &DAG) const {
  544. auto N = cast<GlobalAddressSDNode>(Op);
  545. assert(N->getOffset() == 0 && "Invalid offset for global address");
  546. SDLoc DL(Op);
  547. const GlobalValue *GV = N->getGlobal();
  548. SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i64);
  549. return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
  550. }
  551. unsigned
  552. BPFTargetLowering::EmitSubregExt(MachineInstr &MI, MachineBasicBlock *BB,
  553. unsigned Reg, bool isSigned) const {
  554. const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
  555. const TargetRegisterClass *RC = getRegClassFor(MVT::i64);
  556. int RShiftOp = isSigned ? BPF::SRA_ri : BPF::SRL_ri;
  557. MachineFunction *F = BB->getParent();
  558. DebugLoc DL = MI.getDebugLoc();
  559. MachineRegisterInfo &RegInfo = F->getRegInfo();
  560. if (!isSigned) {
  561. Register PromotedReg0 = RegInfo.createVirtualRegister(RC);
  562. BuildMI(BB, DL, TII.get(BPF::MOV_32_64), PromotedReg0).addReg(Reg);
  563. return PromotedReg0;
  564. }
  565. Register PromotedReg0 = RegInfo.createVirtualRegister(RC);
  566. Register PromotedReg1 = RegInfo.createVirtualRegister(RC);
  567. Register PromotedReg2 = RegInfo.createVirtualRegister(RC);
  568. BuildMI(BB, DL, TII.get(BPF::MOV_32_64), PromotedReg0).addReg(Reg);
  569. BuildMI(BB, DL, TII.get(BPF::SLL_ri), PromotedReg1)
  570. .addReg(PromotedReg0).addImm(32);
  571. BuildMI(BB, DL, TII.get(RShiftOp), PromotedReg2)
  572. .addReg(PromotedReg1).addImm(32);
  573. return PromotedReg2;
  574. }
  575. MachineBasicBlock *
  576. BPFTargetLowering::EmitInstrWithCustomInserterMemcpy(MachineInstr &MI,
  577. MachineBasicBlock *BB)
  578. const {
  579. MachineFunction *MF = MI.getParent()->getParent();
  580. MachineRegisterInfo &MRI = MF->getRegInfo();
  581. MachineInstrBuilder MIB(*MF, MI);
  582. unsigned ScratchReg;
  583. // This function does custom insertion during lowering BPFISD::MEMCPY which
  584. // only has two register operands from memcpy semantics, the copy source
  585. // address and the copy destination address.
  586. //
  587. // Because we will expand BPFISD::MEMCPY into load/store pairs, we will need
  588. // a third scratch register to serve as the destination register of load and
  589. // source register of store.
  590. //
  591. // The scratch register here is with the Define | Dead | EarlyClobber flags.
  592. // The EarlyClobber flag has the semantic property that the operand it is
  593. // attached to is clobbered before the rest of the inputs are read. Hence it
  594. // must be unique among the operands to the instruction. The Define flag is
  595. // needed to coerce the machine verifier that an Undef value isn't a problem
  596. // as we anyway is loading memory into it. The Dead flag is needed as the
  597. // value in scratch isn't supposed to be used by any other instruction.
  598. ScratchReg = MRI.createVirtualRegister(&BPF::GPRRegClass);
  599. MIB.addReg(ScratchReg,
  600. RegState::Define | RegState::Dead | RegState::EarlyClobber);
  601. return BB;
  602. }
  603. MachineBasicBlock *
  604. BPFTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
  605. MachineBasicBlock *BB) const {
  606. const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
  607. DebugLoc DL = MI.getDebugLoc();
  608. unsigned Opc = MI.getOpcode();
  609. bool isSelectRROp = (Opc == BPF::Select ||
  610. Opc == BPF::Select_64_32 ||
  611. Opc == BPF::Select_32 ||
  612. Opc == BPF::Select_32_64);
  613. bool isMemcpyOp = Opc == BPF::MEMCPY;
  614. #ifndef NDEBUG
  615. bool isSelectRIOp = (Opc == BPF::Select_Ri ||
  616. Opc == BPF::Select_Ri_64_32 ||
  617. Opc == BPF::Select_Ri_32 ||
  618. Opc == BPF::Select_Ri_32_64);
  619. assert((isSelectRROp || isSelectRIOp || isMemcpyOp) &&
  620. "Unexpected instr type to insert");
  621. #endif
  622. if (isMemcpyOp)
  623. return EmitInstrWithCustomInserterMemcpy(MI, BB);
  624. bool is32BitCmp = (Opc == BPF::Select_32 ||
  625. Opc == BPF::Select_32_64 ||
  626. Opc == BPF::Select_Ri_32 ||
  627. Opc == BPF::Select_Ri_32_64);
  628. // To "insert" a SELECT instruction, we actually have to insert the diamond
  629. // control-flow pattern. The incoming instruction knows the destination vreg
  630. // to set, the condition code register to branch on, the true/false values to
  631. // select between, and a branch opcode to use.
  632. const BasicBlock *LLVM_BB = BB->getBasicBlock();
  633. MachineFunction::iterator I = ++BB->getIterator();
  634. // ThisMBB:
  635. // ...
  636. // TrueVal = ...
  637. // jmp_XX r1, r2 goto Copy1MBB
  638. // fallthrough --> Copy0MBB
  639. MachineBasicBlock *ThisMBB = BB;
  640. MachineFunction *F = BB->getParent();
  641. MachineBasicBlock *Copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
  642. MachineBasicBlock *Copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
  643. F->insert(I, Copy0MBB);
  644. F->insert(I, Copy1MBB);
  645. // Update machine-CFG edges by transferring all successors of the current
  646. // block to the new block which will contain the Phi node for the select.
  647. Copy1MBB->splice(Copy1MBB->begin(), BB,
  648. std::next(MachineBasicBlock::iterator(MI)), BB->end());
  649. Copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
  650. // Next, add the true and fallthrough blocks as its successors.
  651. BB->addSuccessor(Copy0MBB);
  652. BB->addSuccessor(Copy1MBB);
  653. // Insert Branch if Flag
  654. int CC = MI.getOperand(3).getImm();
  655. int NewCC;
  656. switch (CC) {
  657. #define SET_NEWCC(X, Y) \
  658. case ISD::X: \
  659. if (is32BitCmp && HasJmp32) \
  660. NewCC = isSelectRROp ? BPF::Y##_rr_32 : BPF::Y##_ri_32; \
  661. else \
  662. NewCC = isSelectRROp ? BPF::Y##_rr : BPF::Y##_ri; \
  663. break
  664. SET_NEWCC(SETGT, JSGT);
  665. SET_NEWCC(SETUGT, JUGT);
  666. SET_NEWCC(SETGE, JSGE);
  667. SET_NEWCC(SETUGE, JUGE);
  668. SET_NEWCC(SETEQ, JEQ);
  669. SET_NEWCC(SETNE, JNE);
  670. SET_NEWCC(SETLT, JSLT);
  671. SET_NEWCC(SETULT, JULT);
  672. SET_NEWCC(SETLE, JSLE);
  673. SET_NEWCC(SETULE, JULE);
  674. default:
  675. report_fatal_error("unimplemented select CondCode " + Twine(CC));
  676. }
  677. Register LHS = MI.getOperand(1).getReg();
  678. bool isSignedCmp = (CC == ISD::SETGT ||
  679. CC == ISD::SETGE ||
  680. CC == ISD::SETLT ||
  681. CC == ISD::SETLE);
  682. // eBPF at the moment only has 64-bit comparison. Any 32-bit comparison need
  683. // to be promoted, however if the 32-bit comparison operands are destination
  684. // registers then they are implicitly zero-extended already, there is no
  685. // need of explicit zero-extend sequence for them.
  686. //
  687. // We simply do extension for all situations in this method, but we will
  688. // try to remove those unnecessary in BPFMIPeephole pass.
  689. if (is32BitCmp && !HasJmp32)
  690. LHS = EmitSubregExt(MI, BB, LHS, isSignedCmp);
  691. if (isSelectRROp) {
  692. Register RHS = MI.getOperand(2).getReg();
  693. if (is32BitCmp && !HasJmp32)
  694. RHS = EmitSubregExt(MI, BB, RHS, isSignedCmp);
  695. BuildMI(BB, DL, TII.get(NewCC)).addReg(LHS).addReg(RHS).addMBB(Copy1MBB);
  696. } else {
  697. int64_t imm32 = MI.getOperand(2).getImm();
  698. // Check before we build J*_ri instruction.
  699. assert (isInt<32>(imm32));
  700. BuildMI(BB, DL, TII.get(NewCC))
  701. .addReg(LHS).addImm(imm32).addMBB(Copy1MBB);
  702. }
  703. // Copy0MBB:
  704. // %FalseValue = ...
  705. // # fallthrough to Copy1MBB
  706. BB = Copy0MBB;
  707. // Update machine-CFG edges
  708. BB->addSuccessor(Copy1MBB);
  709. // Copy1MBB:
  710. // %Result = phi [ %FalseValue, Copy0MBB ], [ %TrueValue, ThisMBB ]
  711. // ...
  712. BB = Copy1MBB;
  713. BuildMI(*BB, BB->begin(), DL, TII.get(BPF::PHI), MI.getOperand(0).getReg())
  714. .addReg(MI.getOperand(5).getReg())
  715. .addMBB(Copy0MBB)
  716. .addReg(MI.getOperand(4).getReg())
  717. .addMBB(ThisMBB);
  718. MI.eraseFromParent(); // The pseudo instruction is gone now.
  719. return BB;
  720. }
  721. EVT BPFTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
  722. EVT VT) const {
  723. return getHasAlu32() ? MVT::i32 : MVT::i64;
  724. }
  725. MVT BPFTargetLowering::getScalarShiftAmountTy(const DataLayout &DL,
  726. EVT VT) const {
  727. return (getHasAlu32() && VT == MVT::i32) ? MVT::i32 : MVT::i64;
  728. }
  729. bool BPFTargetLowering::isLegalAddressingMode(const DataLayout &DL,
  730. const AddrMode &AM, Type *Ty,
  731. unsigned AS,
  732. Instruction *I) const {
  733. // No global is ever allowed as a base.
  734. if (AM.BaseGV)
  735. return false;
  736. switch (AM.Scale) {
  737. case 0: // "r+i" or just "i", depending on HasBaseReg.
  738. break;
  739. case 1:
  740. if (!AM.HasBaseReg) // allow "r+i".
  741. break;
  742. return false; // disallow "r+r" or "r+r+i".
  743. default:
  744. return false;
  745. }
  746. return true;
  747. }