BPFISelDAGToDAG.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. //===-- BPFISelDAGToDAG.cpp - A dag to dag inst selector for BPF ----------===//
  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 a DAG pattern matching instruction selector for BPF,
  10. // converting from a legalized dag to a BPF dag.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "BPF.h"
  14. #include "BPFRegisterInfo.h"
  15. #include "BPFSubtarget.h"
  16. #include "BPFTargetMachine.h"
  17. #include "llvm/CodeGen/FunctionLoweringInfo.h"
  18. #include "llvm/CodeGen/MachineConstantPool.h"
  19. #include "llvm/CodeGen/MachineFrameInfo.h"
  20. #include "llvm/CodeGen/MachineFunction.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/CodeGen/SelectionDAGISel.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/IntrinsicInst.h"
  26. #include "llvm/IR/IntrinsicsBPF.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/Endian.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "llvm/Target/TargetMachine.h"
  32. using namespace llvm;
  33. #define DEBUG_TYPE "bpf-isel"
  34. #define PASS_NAME "BPF DAG->DAG Pattern Instruction Selection"
  35. // Instruction Selector Implementation
  36. namespace {
  37. class BPFDAGToDAGISel : public SelectionDAGISel {
  38. /// Subtarget - Keep a pointer to the BPFSubtarget around so that we can
  39. /// make the right decision when generating code for different subtargets.
  40. const BPFSubtarget *Subtarget;
  41. public:
  42. static char ID;
  43. BPFDAGToDAGISel() = delete;
  44. explicit BPFDAGToDAGISel(BPFTargetMachine &TM)
  45. : SelectionDAGISel(ID, TM), Subtarget(nullptr) {}
  46. bool runOnMachineFunction(MachineFunction &MF) override {
  47. // Reset the subtarget each time through.
  48. Subtarget = &MF.getSubtarget<BPFSubtarget>();
  49. return SelectionDAGISel::runOnMachineFunction(MF);
  50. }
  51. void PreprocessISelDAG() override;
  52. bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintCode,
  53. std::vector<SDValue> &OutOps) override;
  54. private:
  55. // Include the pieces autogenerated from the target description.
  56. #include "BPFGenDAGISel.inc"
  57. void Select(SDNode *N) override;
  58. // Complex Pattern for address selection.
  59. bool SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset);
  60. bool SelectFIAddr(SDValue Addr, SDValue &Base, SDValue &Offset);
  61. // Node preprocessing cases
  62. void PreprocessLoad(SDNode *Node, SelectionDAG::allnodes_iterator &I);
  63. void PreprocessCopyToReg(SDNode *Node);
  64. void PreprocessTrunc(SDNode *Node, SelectionDAG::allnodes_iterator &I);
  65. // Find constants from a constant structure
  66. typedef std::vector<unsigned char> val_vec_type;
  67. bool fillGenericConstant(const DataLayout &DL, const Constant *CV,
  68. val_vec_type &Vals, uint64_t Offset);
  69. bool fillConstantDataArray(const DataLayout &DL, const ConstantDataArray *CDA,
  70. val_vec_type &Vals, int Offset);
  71. bool fillConstantArray(const DataLayout &DL, const ConstantArray *CA,
  72. val_vec_type &Vals, int Offset);
  73. bool fillConstantStruct(const DataLayout &DL, const ConstantStruct *CS,
  74. val_vec_type &Vals, int Offset);
  75. bool getConstantFieldValue(const GlobalAddressSDNode *Node, uint64_t Offset,
  76. uint64_t Size, unsigned char *ByteSeq);
  77. // Mapping from ConstantStruct global value to corresponding byte-list values
  78. std::map<const void *, val_vec_type> cs_vals_;
  79. };
  80. } // namespace
  81. char BPFDAGToDAGISel::ID = 0;
  82. INITIALIZE_PASS(BPFDAGToDAGISel, DEBUG_TYPE, PASS_NAME, false, false)
  83. // ComplexPattern used on BPF Load/Store instructions
  84. bool BPFDAGToDAGISel::SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {
  85. // if Address is FI, get the TargetFrameIndex.
  86. SDLoc DL(Addr);
  87. if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
  88. Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
  89. Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);
  90. return true;
  91. }
  92. if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
  93. Addr.getOpcode() == ISD::TargetGlobalAddress)
  94. return false;
  95. // Addresses of the form Addr+const or Addr|const
  96. if (CurDAG->isBaseWithConstantOffset(Addr)) {
  97. auto *CN = cast<ConstantSDNode>(Addr.getOperand(1));
  98. if (isInt<16>(CN->getSExtValue())) {
  99. // If the first operand is a FI, get the TargetFI Node
  100. if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
  101. Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
  102. else
  103. Base = Addr.getOperand(0);
  104. Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i64);
  105. return true;
  106. }
  107. }
  108. Base = Addr;
  109. Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);
  110. return true;
  111. }
  112. // ComplexPattern used on BPF FI instruction
  113. bool BPFDAGToDAGISel::SelectFIAddr(SDValue Addr, SDValue &Base,
  114. SDValue &Offset) {
  115. SDLoc DL(Addr);
  116. if (!CurDAG->isBaseWithConstantOffset(Addr))
  117. return false;
  118. // Addresses of the form Addr+const or Addr|const
  119. auto *CN = cast<ConstantSDNode>(Addr.getOperand(1));
  120. if (isInt<16>(CN->getSExtValue())) {
  121. // If the first operand is a FI, get the TargetFI Node
  122. if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
  123. Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
  124. else
  125. return false;
  126. Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i64);
  127. return true;
  128. }
  129. return false;
  130. }
  131. bool BPFDAGToDAGISel::SelectInlineAsmMemoryOperand(
  132. const SDValue &Op, unsigned ConstraintCode, std::vector<SDValue> &OutOps) {
  133. SDValue Op0, Op1;
  134. switch (ConstraintCode) {
  135. default:
  136. return true;
  137. case InlineAsm::Constraint_m: // memory
  138. if (!SelectAddr(Op, Op0, Op1))
  139. return true;
  140. break;
  141. }
  142. SDLoc DL(Op);
  143. SDValue AluOp = CurDAG->getTargetConstant(ISD::ADD, DL, MVT::i32);;
  144. OutOps.push_back(Op0);
  145. OutOps.push_back(Op1);
  146. OutOps.push_back(AluOp);
  147. return false;
  148. }
  149. void BPFDAGToDAGISel::Select(SDNode *Node) {
  150. unsigned Opcode = Node->getOpcode();
  151. // If we have a custom node, we already have selected!
  152. if (Node->isMachineOpcode()) {
  153. LLVM_DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
  154. return;
  155. }
  156. // tablegen selection should be handled here.
  157. switch (Opcode) {
  158. default:
  159. break;
  160. case ISD::SDIV: {
  161. DebugLoc Empty;
  162. const DebugLoc &DL = Node->getDebugLoc();
  163. if (DL != Empty)
  164. errs() << "Error at line " << DL.getLine() << ": ";
  165. else
  166. errs() << "Error: ";
  167. errs() << "Unsupport signed division for DAG: ";
  168. Node->print(errs(), CurDAG);
  169. errs() << "Please convert to unsigned div/mod.\n";
  170. break;
  171. }
  172. case ISD::INTRINSIC_W_CHAIN: {
  173. unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
  174. switch (IntNo) {
  175. case Intrinsic::bpf_load_byte:
  176. case Intrinsic::bpf_load_half:
  177. case Intrinsic::bpf_load_word: {
  178. SDLoc DL(Node);
  179. SDValue Chain = Node->getOperand(0);
  180. SDValue N1 = Node->getOperand(1);
  181. SDValue Skb = Node->getOperand(2);
  182. SDValue N3 = Node->getOperand(3);
  183. SDValue R6Reg = CurDAG->getRegister(BPF::R6, MVT::i64);
  184. Chain = CurDAG->getCopyToReg(Chain, DL, R6Reg, Skb, SDValue());
  185. Node = CurDAG->UpdateNodeOperands(Node, Chain, N1, R6Reg, N3);
  186. break;
  187. }
  188. }
  189. break;
  190. }
  191. case ISD::FrameIndex: {
  192. int FI = cast<FrameIndexSDNode>(Node)->getIndex();
  193. EVT VT = Node->getValueType(0);
  194. SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
  195. unsigned Opc = BPF::MOV_rr;
  196. if (Node->hasOneUse()) {
  197. CurDAG->SelectNodeTo(Node, Opc, VT, TFI);
  198. return;
  199. }
  200. ReplaceNode(Node, CurDAG->getMachineNode(Opc, SDLoc(Node), VT, TFI));
  201. return;
  202. }
  203. }
  204. // Select the default instruction
  205. SelectCode(Node);
  206. }
  207. void BPFDAGToDAGISel::PreprocessLoad(SDNode *Node,
  208. SelectionDAG::allnodes_iterator &I) {
  209. union {
  210. uint8_t c[8];
  211. uint16_t s;
  212. uint32_t i;
  213. uint64_t d;
  214. } new_val; // hold up the constant values replacing loads.
  215. bool to_replace = false;
  216. SDLoc DL(Node);
  217. const LoadSDNode *LD = cast<LoadSDNode>(Node);
  218. uint64_t size = LD->getMemOperand()->getSize();
  219. if (!size || size > 8 || (size & (size - 1)) || !LD->isSimple())
  220. return;
  221. SDNode *LDAddrNode = LD->getOperand(1).getNode();
  222. // Match LDAddr against either global_addr or (global_addr + offset)
  223. unsigned opcode = LDAddrNode->getOpcode();
  224. if (opcode == ISD::ADD) {
  225. SDValue OP1 = LDAddrNode->getOperand(0);
  226. SDValue OP2 = LDAddrNode->getOperand(1);
  227. // We want to find the pattern global_addr + offset
  228. SDNode *OP1N = OP1.getNode();
  229. if (OP1N->getOpcode() <= ISD::BUILTIN_OP_END || OP1N->getNumOperands() == 0)
  230. return;
  231. LLVM_DEBUG(dbgs() << "Check candidate load: "; LD->dump(); dbgs() << '\n');
  232. const GlobalAddressSDNode *GADN =
  233. dyn_cast<GlobalAddressSDNode>(OP1N->getOperand(0).getNode());
  234. const ConstantSDNode *CDN = dyn_cast<ConstantSDNode>(OP2.getNode());
  235. if (GADN && CDN)
  236. to_replace =
  237. getConstantFieldValue(GADN, CDN->getZExtValue(), size, new_val.c);
  238. } else if (LDAddrNode->getOpcode() > ISD::BUILTIN_OP_END &&
  239. LDAddrNode->getNumOperands() > 0) {
  240. LLVM_DEBUG(dbgs() << "Check candidate load: "; LD->dump(); dbgs() << '\n');
  241. SDValue OP1 = LDAddrNode->getOperand(0);
  242. if (const GlobalAddressSDNode *GADN =
  243. dyn_cast<GlobalAddressSDNode>(OP1.getNode()))
  244. to_replace = getConstantFieldValue(GADN, 0, size, new_val.c);
  245. }
  246. if (!to_replace)
  247. return;
  248. // replacing the old with a new value
  249. uint64_t val;
  250. if (size == 1)
  251. val = new_val.c[0];
  252. else if (size == 2)
  253. val = new_val.s;
  254. else if (size == 4)
  255. val = new_val.i;
  256. else {
  257. val = new_val.d;
  258. }
  259. LLVM_DEBUG(dbgs() << "Replacing load of size " << size << " with constant "
  260. << val << '\n');
  261. SDValue NVal = CurDAG->getConstant(val, DL, LD->getValueType(0));
  262. // After replacement, the current node is dead, we need to
  263. // go backward one step to make iterator still work
  264. I--;
  265. SDValue From[] = {SDValue(Node, 0), SDValue(Node, 1)};
  266. SDValue To[] = {NVal, NVal};
  267. CurDAG->ReplaceAllUsesOfValuesWith(From, To, 2);
  268. I++;
  269. // It is safe to delete node now
  270. CurDAG->DeleteNode(Node);
  271. }
  272. void BPFDAGToDAGISel::PreprocessISelDAG() {
  273. // Iterate through all nodes, interested in the following case:
  274. //
  275. // . loads from ConstantStruct or ConstantArray of constructs
  276. // which can be turns into constant itself, with this we can
  277. // avoid reading from read-only section at runtime.
  278. //
  279. // . Removing redundant AND for intrinsic narrow loads.
  280. for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
  281. E = CurDAG->allnodes_end();
  282. I != E;) {
  283. SDNode *Node = &*I++;
  284. unsigned Opcode = Node->getOpcode();
  285. if (Opcode == ISD::LOAD)
  286. PreprocessLoad(Node, I);
  287. else if (Opcode == ISD::AND)
  288. PreprocessTrunc(Node, I);
  289. }
  290. }
  291. bool BPFDAGToDAGISel::getConstantFieldValue(const GlobalAddressSDNode *Node,
  292. uint64_t Offset, uint64_t Size,
  293. unsigned char *ByteSeq) {
  294. const GlobalVariable *V = dyn_cast<GlobalVariable>(Node->getGlobal());
  295. if (!V || !V->hasInitializer() || !V->isConstant())
  296. return false;
  297. const Constant *Init = V->getInitializer();
  298. const DataLayout &DL = CurDAG->getDataLayout();
  299. val_vec_type TmpVal;
  300. auto it = cs_vals_.find(static_cast<const void *>(Init));
  301. if (it != cs_vals_.end()) {
  302. TmpVal = it->second;
  303. } else {
  304. uint64_t total_size = 0;
  305. if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(Init))
  306. total_size =
  307. DL.getStructLayout(cast<StructType>(CS->getType()))->getSizeInBytes();
  308. else if (const ConstantArray *CA = dyn_cast<ConstantArray>(Init))
  309. total_size = DL.getTypeAllocSize(CA->getType()->getElementType()) *
  310. CA->getNumOperands();
  311. else
  312. return false;
  313. val_vec_type Vals(total_size, 0);
  314. if (fillGenericConstant(DL, Init, Vals, 0) == false)
  315. return false;
  316. cs_vals_[static_cast<const void *>(Init)] = Vals;
  317. TmpVal = std::move(Vals);
  318. }
  319. // test whether host endianness matches target
  320. union {
  321. uint8_t c[2];
  322. uint16_t s;
  323. } test_buf;
  324. uint16_t test_val = 0x2345;
  325. if (DL.isLittleEndian())
  326. support::endian::write16le(test_buf.c, test_val);
  327. else
  328. support::endian::write16be(test_buf.c, test_val);
  329. bool endian_match = test_buf.s == test_val;
  330. for (uint64_t i = Offset, j = 0; i < Offset + Size; i++, j++)
  331. ByteSeq[j] = endian_match ? TmpVal[i] : TmpVal[Offset + Size - 1 - j];
  332. return true;
  333. }
  334. bool BPFDAGToDAGISel::fillGenericConstant(const DataLayout &DL,
  335. const Constant *CV,
  336. val_vec_type &Vals, uint64_t Offset) {
  337. uint64_t Size = DL.getTypeAllocSize(CV->getType());
  338. if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
  339. return true; // already done
  340. if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
  341. uint64_t val = CI->getZExtValue();
  342. LLVM_DEBUG(dbgs() << "Byte array at offset " << Offset << " with value "
  343. << val << '\n');
  344. if (Size > 8 || (Size & (Size - 1)))
  345. return false;
  346. // Store based on target endian
  347. for (uint64_t i = 0; i < Size; ++i) {
  348. Vals[Offset + i] = DL.isLittleEndian()
  349. ? ((val >> (i * 8)) & 0xFF)
  350. : ((val >> ((Size - i - 1) * 8)) & 0xFF);
  351. }
  352. return true;
  353. }
  354. if (const ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(CV))
  355. return fillConstantDataArray(DL, CDA, Vals, Offset);
  356. if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV))
  357. return fillConstantArray(DL, CA, Vals, Offset);
  358. if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
  359. return fillConstantStruct(DL, CVS, Vals, Offset);
  360. return false;
  361. }
  362. bool BPFDAGToDAGISel::fillConstantDataArray(const DataLayout &DL,
  363. const ConstantDataArray *CDA,
  364. val_vec_type &Vals, int Offset) {
  365. for (unsigned i = 0, e = CDA->getNumElements(); i != e; ++i) {
  366. if (fillGenericConstant(DL, CDA->getElementAsConstant(i), Vals, Offset) ==
  367. false)
  368. return false;
  369. Offset += DL.getTypeAllocSize(CDA->getElementAsConstant(i)->getType());
  370. }
  371. return true;
  372. }
  373. bool BPFDAGToDAGISel::fillConstantArray(const DataLayout &DL,
  374. const ConstantArray *CA,
  375. val_vec_type &Vals, int Offset) {
  376. for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
  377. if (fillGenericConstant(DL, CA->getOperand(i), Vals, Offset) == false)
  378. return false;
  379. Offset += DL.getTypeAllocSize(CA->getOperand(i)->getType());
  380. }
  381. return true;
  382. }
  383. bool BPFDAGToDAGISel::fillConstantStruct(const DataLayout &DL,
  384. const ConstantStruct *CS,
  385. val_vec_type &Vals, int Offset) {
  386. const StructLayout *Layout = DL.getStructLayout(CS->getType());
  387. for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
  388. const Constant *Field = CS->getOperand(i);
  389. uint64_t SizeSoFar = Layout->getElementOffset(i);
  390. if (fillGenericConstant(DL, Field, Vals, Offset + SizeSoFar) == false)
  391. return false;
  392. }
  393. return true;
  394. }
  395. void BPFDAGToDAGISel::PreprocessTrunc(SDNode *Node,
  396. SelectionDAG::allnodes_iterator &I) {
  397. ConstantSDNode *MaskN = dyn_cast<ConstantSDNode>(Node->getOperand(1));
  398. if (!MaskN)
  399. return;
  400. // The Reg operand should be a virtual register, which is defined
  401. // outside the current basic block. DAG combiner has done a pretty
  402. // good job in removing truncating inside a single basic block except
  403. // when the Reg operand comes from bpf_load_[byte | half | word] for
  404. // which the generic optimizer doesn't understand their results are
  405. // zero extended.
  406. SDValue BaseV = Node->getOperand(0);
  407. if (BaseV.getOpcode() != ISD::INTRINSIC_W_CHAIN)
  408. return;
  409. unsigned IntNo = cast<ConstantSDNode>(BaseV->getOperand(1))->getZExtValue();
  410. uint64_t MaskV = MaskN->getZExtValue();
  411. if (!((IntNo == Intrinsic::bpf_load_byte && MaskV == 0xFF) ||
  412. (IntNo == Intrinsic::bpf_load_half && MaskV == 0xFFFF) ||
  413. (IntNo == Intrinsic::bpf_load_word && MaskV == 0xFFFFFFFF)))
  414. return;
  415. LLVM_DEBUG(dbgs() << "Remove the redundant AND operation in: ";
  416. Node->dump(); dbgs() << '\n');
  417. I--;
  418. CurDAG->ReplaceAllUsesWith(SDValue(Node, 0), BaseV);
  419. I++;
  420. CurDAG->DeleteNode(Node);
  421. }
  422. FunctionPass *llvm::createBPFISelDag(BPFTargetMachine &TM) {
  423. return new BPFDAGToDAGISel(TM);
  424. }