Path: blob/main/contrib/llvm-project/llvm/lib/Target/BPF/BPFISelDAGToDAG.cpp
35269 views
//===-- BPFISelDAGToDAG.cpp - A dag to dag inst selector for BPF ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines a DAG pattern matching instruction selector for BPF,9// converting from a legalized dag to a BPF dag.10//11//===----------------------------------------------------------------------===//1213#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"3233using namespace llvm;3435#define DEBUG_TYPE "bpf-isel"36#define PASS_NAME "BPF DAG->DAG Pattern Instruction Selection"3738// Instruction Selector Implementation39namespace {4041class BPFDAGToDAGISel : public SelectionDAGISel {4243/// Subtarget - Keep a pointer to the BPFSubtarget around so that we can44/// make the right decision when generating code for different subtargets.45const BPFSubtarget *Subtarget;4647public:48BPFDAGToDAGISel() = delete;4950explicit BPFDAGToDAGISel(BPFTargetMachine &TM)51: SelectionDAGISel(TM), Subtarget(nullptr) {}5253bool runOnMachineFunction(MachineFunction &MF) override {54// Reset the subtarget each time through.55Subtarget = &MF.getSubtarget<BPFSubtarget>();56return SelectionDAGISel::runOnMachineFunction(MF);57}5859void PreprocessISelDAG() override;6061bool SelectInlineAsmMemoryOperand(const SDValue &Op,62InlineAsm::ConstraintCode ConstraintCode,63std::vector<SDValue> &OutOps) override;6465private:66// Include the pieces autogenerated from the target description.67#include "BPFGenDAGISel.inc"6869void Select(SDNode *N) override;7071// Complex Pattern for address selection.72bool SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset);73bool SelectFIAddr(SDValue Addr, SDValue &Base, SDValue &Offset);7475// Node preprocessing cases76void PreprocessLoad(SDNode *Node, SelectionDAG::allnodes_iterator &I);77void PreprocessTrunc(SDNode *Node, SelectionDAG::allnodes_iterator &I);7879// Find constants from a constant structure80typedef std::vector<unsigned char> val_vec_type;81bool fillGenericConstant(const DataLayout &DL, const Constant *CV,82val_vec_type &Vals, uint64_t Offset);83bool fillConstantDataArray(const DataLayout &DL, const ConstantDataArray *CDA,84val_vec_type &Vals, int Offset);85bool fillConstantArray(const DataLayout &DL, const ConstantArray *CA,86val_vec_type &Vals, int Offset);87bool fillConstantStruct(const DataLayout &DL, const ConstantStruct *CS,88val_vec_type &Vals, int Offset);89bool getConstantFieldValue(const GlobalAddressSDNode *Node, uint64_t Offset,90uint64_t Size, unsigned char *ByteSeq);91// Mapping from ConstantStruct global value to corresponding byte-list values92std::map<const void *, val_vec_type> cs_vals_;93};9495class BPFDAGToDAGISelLegacy : public SelectionDAGISelLegacy {96public:97static char ID;98BPFDAGToDAGISelLegacy(BPFTargetMachine &TM)99: SelectionDAGISelLegacy(ID, std::make_unique<BPFDAGToDAGISel>(TM)) {}100};101} // namespace102103char BPFDAGToDAGISelLegacy::ID = 0;104105INITIALIZE_PASS(BPFDAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false, false)106107// ComplexPattern used on BPF Load/Store instructions108bool BPFDAGToDAGISel::SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {109// if Address is FI, get the TargetFrameIndex.110SDLoc DL(Addr);111if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {112Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);113Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);114return true;115}116117if (Addr.getOpcode() == ISD::TargetExternalSymbol ||118Addr.getOpcode() == ISD::TargetGlobalAddress)119return false;120121// Addresses of the form Addr+const or Addr|const122if (CurDAG->isBaseWithConstantOffset(Addr)) {123auto *CN = cast<ConstantSDNode>(Addr.getOperand(1));124if (isInt<16>(CN->getSExtValue())) {125// If the first operand is a FI, get the TargetFI Node126if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))127Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);128else129Base = Addr.getOperand(0);130131Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i64);132return true;133}134}135136Base = Addr;137Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);138return true;139}140141// ComplexPattern used on BPF FI instruction142bool BPFDAGToDAGISel::SelectFIAddr(SDValue Addr, SDValue &Base,143SDValue &Offset) {144SDLoc DL(Addr);145146if (!CurDAG->isBaseWithConstantOffset(Addr))147return false;148149// Addresses of the form Addr+const or Addr|const150auto *CN = cast<ConstantSDNode>(Addr.getOperand(1));151if (isInt<16>(CN->getSExtValue())) {152// If the first operand is a FI, get the TargetFI Node153if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))154Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);155else156return false;157158Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i64);159return true;160}161162return false;163}164165bool BPFDAGToDAGISel::SelectInlineAsmMemoryOperand(166const SDValue &Op, InlineAsm::ConstraintCode ConstraintCode,167std::vector<SDValue> &OutOps) {168SDValue Op0, Op1;169switch (ConstraintCode) {170default:171return true;172case InlineAsm::ConstraintCode::m: // memory173if (!SelectAddr(Op, Op0, Op1))174return true;175break;176}177178SDLoc DL(Op);179SDValue AluOp = CurDAG->getTargetConstant(ISD::ADD, DL, MVT::i32);180OutOps.push_back(Op0);181OutOps.push_back(Op1);182OutOps.push_back(AluOp);183return false;184}185186void BPFDAGToDAGISel::Select(SDNode *Node) {187unsigned Opcode = Node->getOpcode();188189// If we have a custom node, we already have selected!190if (Node->isMachineOpcode()) {191LLVM_DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');192return;193}194195// tablegen selection should be handled here.196switch (Opcode) {197default:198break;199case ISD::INTRINSIC_W_CHAIN: {200unsigned IntNo = Node->getConstantOperandVal(1);201switch (IntNo) {202case Intrinsic::bpf_load_byte:203case Intrinsic::bpf_load_half:204case Intrinsic::bpf_load_word: {205SDLoc DL(Node);206SDValue Chain = Node->getOperand(0);207SDValue N1 = Node->getOperand(1);208SDValue Skb = Node->getOperand(2);209SDValue N3 = Node->getOperand(3);210211SDValue R6Reg = CurDAG->getRegister(BPF::R6, MVT::i64);212Chain = CurDAG->getCopyToReg(Chain, DL, R6Reg, Skb, SDValue());213Node = CurDAG->UpdateNodeOperands(Node, Chain, N1, R6Reg, N3);214break;215}216}217break;218}219220case ISD::FrameIndex: {221int FI = cast<FrameIndexSDNode>(Node)->getIndex();222EVT VT = Node->getValueType(0);223SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);224unsigned Opc = BPF::MOV_rr;225if (Node->hasOneUse()) {226CurDAG->SelectNodeTo(Node, Opc, VT, TFI);227return;228}229ReplaceNode(Node, CurDAG->getMachineNode(Opc, SDLoc(Node), VT, TFI));230return;231}232}233234// Select the default instruction235SelectCode(Node);236}237238void BPFDAGToDAGISel::PreprocessLoad(SDNode *Node,239SelectionDAG::allnodes_iterator &I) {240union {241uint8_t c[8];242uint16_t s;243uint32_t i;244uint64_t d;245} new_val; // hold up the constant values replacing loads.246bool to_replace = false;247SDLoc DL(Node);248const LoadSDNode *LD = cast<LoadSDNode>(Node);249if (!LD->getMemOperand()->getSize().hasValue())250return;251uint64_t size = LD->getMemOperand()->getSize().getValue();252253if (!size || size > 8 || (size & (size - 1)) || !LD->isSimple())254return;255256SDNode *LDAddrNode = LD->getOperand(1).getNode();257// Match LDAddr against either global_addr or (global_addr + offset)258unsigned opcode = LDAddrNode->getOpcode();259if (opcode == ISD::ADD) {260SDValue OP1 = LDAddrNode->getOperand(0);261SDValue OP2 = LDAddrNode->getOperand(1);262263// We want to find the pattern global_addr + offset264SDNode *OP1N = OP1.getNode();265if (OP1N->getOpcode() <= ISD::BUILTIN_OP_END || OP1N->getNumOperands() == 0)266return;267268LLVM_DEBUG(dbgs() << "Check candidate load: "; LD->dump(); dbgs() << '\n');269270const GlobalAddressSDNode *GADN =271dyn_cast<GlobalAddressSDNode>(OP1N->getOperand(0).getNode());272const ConstantSDNode *CDN = dyn_cast<ConstantSDNode>(OP2.getNode());273if (GADN && CDN)274to_replace =275getConstantFieldValue(GADN, CDN->getZExtValue(), size, new_val.c);276} else if (LDAddrNode->getOpcode() > ISD::BUILTIN_OP_END &&277LDAddrNode->getNumOperands() > 0) {278LLVM_DEBUG(dbgs() << "Check candidate load: "; LD->dump(); dbgs() << '\n');279280SDValue OP1 = LDAddrNode->getOperand(0);281if (const GlobalAddressSDNode *GADN =282dyn_cast<GlobalAddressSDNode>(OP1.getNode()))283to_replace = getConstantFieldValue(GADN, 0, size, new_val.c);284}285286if (!to_replace)287return;288289// replacing the old with a new value290uint64_t val;291if (size == 1)292val = new_val.c[0];293else if (size == 2)294val = new_val.s;295else if (size == 4)296val = new_val.i;297else {298val = new_val.d;299}300301LLVM_DEBUG(dbgs() << "Replacing load of size " << size << " with constant "302<< val << '\n');303SDValue NVal = CurDAG->getConstant(val, DL, LD->getValueType(0));304305// After replacement, the current node is dead, we need to306// go backward one step to make iterator still work307I--;308SDValue From[] = {SDValue(Node, 0), SDValue(Node, 1)};309SDValue To[] = {NVal, NVal};310CurDAG->ReplaceAllUsesOfValuesWith(From, To, 2);311I++;312// It is safe to delete node now313CurDAG->DeleteNode(Node);314}315316void BPFDAGToDAGISel::PreprocessISelDAG() {317// Iterate through all nodes, interested in the following case:318//319// . loads from ConstantStruct or ConstantArray of constructs320// which can be turns into constant itself, with this we can321// avoid reading from read-only section at runtime.322//323// . Removing redundant AND for intrinsic narrow loads.324for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),325E = CurDAG->allnodes_end();326I != E;) {327SDNode *Node = &*I++;328unsigned Opcode = Node->getOpcode();329if (Opcode == ISD::LOAD)330PreprocessLoad(Node, I);331else if (Opcode == ISD::AND)332PreprocessTrunc(Node, I);333}334}335336bool BPFDAGToDAGISel::getConstantFieldValue(const GlobalAddressSDNode *Node,337uint64_t Offset, uint64_t Size,338unsigned char *ByteSeq) {339const GlobalVariable *V = dyn_cast<GlobalVariable>(Node->getGlobal());340341if (!V || !V->hasInitializer() || !V->isConstant())342return false;343344const Constant *Init = V->getInitializer();345const DataLayout &DL = CurDAG->getDataLayout();346val_vec_type TmpVal;347348auto it = cs_vals_.find(static_cast<const void *>(Init));349if (it != cs_vals_.end()) {350TmpVal = it->second;351} else {352uint64_t total_size = 0;353if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(Init))354total_size =355DL.getStructLayout(cast<StructType>(CS->getType()))->getSizeInBytes();356else if (const ConstantArray *CA = dyn_cast<ConstantArray>(Init))357total_size = DL.getTypeAllocSize(CA->getType()->getElementType()) *358CA->getNumOperands();359else360return false;361362val_vec_type Vals(total_size, 0);363if (fillGenericConstant(DL, Init, Vals, 0) == false)364return false;365cs_vals_[static_cast<const void *>(Init)] = Vals;366TmpVal = std::move(Vals);367}368369// test whether host endianness matches target370union {371uint8_t c[2];372uint16_t s;373} test_buf;374uint16_t test_val = 0x2345;375if (DL.isLittleEndian())376support::endian::write16le(test_buf.c, test_val);377else378support::endian::write16be(test_buf.c, test_val);379380bool endian_match = test_buf.s == test_val;381for (uint64_t i = Offset, j = 0; i < Offset + Size; i++, j++)382ByteSeq[j] = endian_match ? TmpVal[i] : TmpVal[Offset + Size - 1 - j];383384return true;385}386387bool BPFDAGToDAGISel::fillGenericConstant(const DataLayout &DL,388const Constant *CV,389val_vec_type &Vals, uint64_t Offset) {390uint64_t Size = DL.getTypeAllocSize(CV->getType());391392if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))393return true; // already done394395if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {396uint64_t val = CI->getZExtValue();397LLVM_DEBUG(dbgs() << "Byte array at offset " << Offset << " with value "398<< val << '\n');399400if (Size > 8 || (Size & (Size - 1)))401return false;402403// Store based on target endian404for (uint64_t i = 0; i < Size; ++i) {405Vals[Offset + i] = DL.isLittleEndian()406? ((val >> (i * 8)) & 0xFF)407: ((val >> ((Size - i - 1) * 8)) & 0xFF);408}409return true;410}411412if (const ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(CV))413return fillConstantDataArray(DL, CDA, Vals, Offset);414415if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV))416return fillConstantArray(DL, CA, Vals, Offset);417418if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))419return fillConstantStruct(DL, CVS, Vals, Offset);420421return false;422}423424bool BPFDAGToDAGISel::fillConstantDataArray(const DataLayout &DL,425const ConstantDataArray *CDA,426val_vec_type &Vals, int Offset) {427for (unsigned i = 0, e = CDA->getNumElements(); i != e; ++i) {428if (fillGenericConstant(DL, CDA->getElementAsConstant(i), Vals, Offset) ==429false)430return false;431Offset += DL.getTypeAllocSize(CDA->getElementAsConstant(i)->getType());432}433434return true;435}436437bool BPFDAGToDAGISel::fillConstantArray(const DataLayout &DL,438const ConstantArray *CA,439val_vec_type &Vals, int Offset) {440for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {441if (fillGenericConstant(DL, CA->getOperand(i), Vals, Offset) == false)442return false;443Offset += DL.getTypeAllocSize(CA->getOperand(i)->getType());444}445446return true;447}448449bool BPFDAGToDAGISel::fillConstantStruct(const DataLayout &DL,450const ConstantStruct *CS,451val_vec_type &Vals, int Offset) {452const StructLayout *Layout = DL.getStructLayout(CS->getType());453for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {454const Constant *Field = CS->getOperand(i);455uint64_t SizeSoFar = Layout->getElementOffset(i);456if (fillGenericConstant(DL, Field, Vals, Offset + SizeSoFar) == false)457return false;458}459return true;460}461462void BPFDAGToDAGISel::PreprocessTrunc(SDNode *Node,463SelectionDAG::allnodes_iterator &I) {464ConstantSDNode *MaskN = dyn_cast<ConstantSDNode>(Node->getOperand(1));465if (!MaskN)466return;467468// The Reg operand should be a virtual register, which is defined469// outside the current basic block. DAG combiner has done a pretty470// good job in removing truncating inside a single basic block except471// when the Reg operand comes from bpf_load_[byte | half | word] for472// which the generic optimizer doesn't understand their results are473// zero extended.474SDValue BaseV = Node->getOperand(0);475if (BaseV.getOpcode() != ISD::INTRINSIC_W_CHAIN)476return;477478unsigned IntNo = BaseV->getConstantOperandVal(1);479uint64_t MaskV = MaskN->getZExtValue();480481if (!((IntNo == Intrinsic::bpf_load_byte && MaskV == 0xFF) ||482(IntNo == Intrinsic::bpf_load_half && MaskV == 0xFFFF) ||483(IntNo == Intrinsic::bpf_load_word && MaskV == 0xFFFFFFFF)))484return;485486LLVM_DEBUG(dbgs() << "Remove the redundant AND operation in: ";487Node->dump(); dbgs() << '\n');488489I--;490CurDAG->ReplaceAllUsesWith(SDValue(Node, 0), BaseV);491I++;492CurDAG->DeleteNode(Node);493}494495FunctionPass *llvm::createBPFISelDag(BPFTargetMachine &TM) {496return new BPFDAGToDAGISelLegacy(TM);497}498499500