Path: blob/main/contrib/llvm-project/llvm/lib/Target/MSP430/MSP430ISelDAGToDAG.cpp
35269 views
//===-- MSP430ISelDAGToDAG.cpp - A dag to dag inst selector for MSP430 ----===//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 an instruction selector for the MSP430 target.9//10//===----------------------------------------------------------------------===//1112#include "MSP430.h"13#include "MSP430TargetMachine.h"14#include "llvm/CodeGen/MachineFrameInfo.h"15#include "llvm/CodeGen/MachineFunction.h"16#include "llvm/CodeGen/MachineInstrBuilder.h"17#include "llvm/CodeGen/MachineRegisterInfo.h"18#include "llvm/CodeGen/SelectionDAG.h"19#include "llvm/CodeGen/SelectionDAGISel.h"20#include "llvm/Config/llvm-config.h"21#include "llvm/IR/CallingConv.h"22#include "llvm/IR/Constants.h"23#include "llvm/IR/DerivedTypes.h"24#include "llvm/IR/Function.h"25#include "llvm/IR/Intrinsics.h"26#include "llvm/Support/Debug.h"27#include "llvm/Support/ErrorHandling.h"28#include "llvm/Support/raw_ostream.h"29using namespace llvm;3031#define DEBUG_TYPE "msp430-isel"32#define PASS_NAME "MSP430 DAG->DAG Pattern Instruction Selection"3334namespace {35struct MSP430ISelAddressMode {36enum {37RegBase,38FrameIndexBase39} BaseType = RegBase;4041struct { // This is really a union, discriminated by BaseType!42SDValue Reg;43int FrameIndex = 0;44} Base;4546int16_t Disp = 0;47const GlobalValue *GV = nullptr;48const Constant *CP = nullptr;49const BlockAddress *BlockAddr = nullptr;50const char *ES = nullptr;51int JT = -1;52Align Alignment; // CP alignment.5354MSP430ISelAddressMode() = default;5556bool hasSymbolicDisplacement() const {57return GV != nullptr || CP != nullptr || ES != nullptr || JT != -1;58}5960#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)61LLVM_DUMP_METHOD void dump() {62errs() << "MSP430ISelAddressMode " << this << '\n';63if (BaseType == RegBase && Base.Reg.getNode() != nullptr) {64errs() << "Base.Reg ";65Base.Reg.getNode()->dump();66} else if (BaseType == FrameIndexBase) {67errs() << " Base.FrameIndex " << Base.FrameIndex << '\n';68}69errs() << " Disp " << Disp << '\n';70if (GV) {71errs() << "GV ";72GV->dump();73} else if (CP) {74errs() << " CP ";75CP->dump();76errs() << " Align" << Alignment.value() << '\n';77} else if (ES) {78errs() << "ES ";79errs() << ES << '\n';80} else if (JT != -1)81errs() << " JT" << JT << " Align" << Alignment.value() << '\n';82}83#endif84};85}8687/// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine88/// instructions for SelectionDAG operations.89///90namespace {91class MSP430DAGToDAGISel : public SelectionDAGISel {92public:93MSP430DAGToDAGISel() = delete;9495MSP430DAGToDAGISel(MSP430TargetMachine &TM, CodeGenOptLevel OptLevel)96: SelectionDAGISel(TM, OptLevel) {}9798private:99bool MatchAddress(SDValue N, MSP430ISelAddressMode &AM);100bool MatchWrapper(SDValue N, MSP430ISelAddressMode &AM);101bool MatchAddressBase(SDValue N, MSP430ISelAddressMode &AM);102103bool SelectInlineAsmMemoryOperand(const SDValue &Op,104InlineAsm::ConstraintCode ConstraintID,105std::vector<SDValue> &OutOps) override;106107// Include the pieces autogenerated from the target description.108#include "MSP430GenDAGISel.inc"109110// Main method to transform nodes into machine nodes.111void Select(SDNode *N) override;112113bool tryIndexedLoad(SDNode *Op);114bool tryIndexedBinOp(SDNode *Op, SDValue N1, SDValue N2, unsigned Opc8,115unsigned Opc16);116117bool SelectAddr(SDValue Addr, SDValue &Base, SDValue &Disp);118};119120class MSP430DAGToDAGISelLegacy : public SelectionDAGISelLegacy {121public:122static char ID;123MSP430DAGToDAGISelLegacy(MSP430TargetMachine &TM, CodeGenOptLevel OptLevel)124: SelectionDAGISelLegacy(125ID, std::make_unique<MSP430DAGToDAGISel>(TM, OptLevel)) {}126};127} // end anonymous namespace128129char MSP430DAGToDAGISelLegacy::ID;130131INITIALIZE_PASS(MSP430DAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false, false)132133/// createMSP430ISelDag - This pass converts a legalized DAG into a134/// MSP430-specific DAG, ready for instruction scheduling.135///136FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM,137CodeGenOptLevel OptLevel) {138return new MSP430DAGToDAGISelLegacy(TM, OptLevel);139}140141/// MatchWrapper - Try to match MSP430ISD::Wrapper node into an addressing mode.142/// These wrap things that will resolve down into a symbol reference. If no143/// match is possible, this returns true, otherwise it returns false.144bool MSP430DAGToDAGISel::MatchWrapper(SDValue N, MSP430ISelAddressMode &AM) {145// If the addressing mode already has a symbol as the displacement, we can146// never match another symbol.147if (AM.hasSymbolicDisplacement())148return true;149150SDValue N0 = N.getOperand(0);151152if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {153AM.GV = G->getGlobal();154AM.Disp += G->getOffset();155//AM.SymbolFlags = G->getTargetFlags();156} else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {157AM.CP = CP->getConstVal();158AM.Alignment = CP->getAlign();159AM.Disp += CP->getOffset();160//AM.SymbolFlags = CP->getTargetFlags();161} else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {162AM.ES = S->getSymbol();163//AM.SymbolFlags = S->getTargetFlags();164} else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {165AM.JT = J->getIndex();166//AM.SymbolFlags = J->getTargetFlags();167} else {168AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();169//AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();170}171return false;172}173174/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the175/// specified addressing mode without any further recursion.176bool MSP430DAGToDAGISel::MatchAddressBase(SDValue N, MSP430ISelAddressMode &AM) {177// Is the base register already occupied?178if (AM.BaseType != MSP430ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {179// If so, we cannot select it.180return true;181}182183// Default, generate it as a register.184AM.BaseType = MSP430ISelAddressMode::RegBase;185AM.Base.Reg = N;186return false;187}188189bool MSP430DAGToDAGISel::MatchAddress(SDValue N, MSP430ISelAddressMode &AM) {190LLVM_DEBUG(errs() << "MatchAddress: "; AM.dump());191192switch (N.getOpcode()) {193default: break;194case ISD::Constant: {195uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();196AM.Disp += Val;197return false;198}199200case MSP430ISD::Wrapper:201if (!MatchWrapper(N, AM))202return false;203break;204205case ISD::FrameIndex:206if (AM.BaseType == MSP430ISelAddressMode::RegBase207&& AM.Base.Reg.getNode() == nullptr) {208AM.BaseType = MSP430ISelAddressMode::FrameIndexBase;209AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();210return false;211}212break;213214case ISD::ADD: {215MSP430ISelAddressMode Backup = AM;216if (!MatchAddress(N.getNode()->getOperand(0), AM) &&217!MatchAddress(N.getNode()->getOperand(1), AM))218return false;219AM = Backup;220if (!MatchAddress(N.getNode()->getOperand(1), AM) &&221!MatchAddress(N.getNode()->getOperand(0), AM))222return false;223AM = Backup;224225break;226}227228case ISD::OR:229// Handle "X | C" as "X + C" iff X is known to have C bits clear.230if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {231MSP430ISelAddressMode Backup = AM;232uint64_t Offset = CN->getSExtValue();233// Start with the LHS as an addr mode.234if (!MatchAddress(N.getOperand(0), AM) &&235// Address could not have picked a GV address for the displacement.236AM.GV == nullptr &&237// Check to see if the LHS & C is zero.238CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {239AM.Disp += Offset;240return false;241}242AM = Backup;243}244break;245}246247return MatchAddressBase(N, AM);248}249250/// SelectAddr - returns true if it is able pattern match an addressing mode.251/// It returns the operands which make up the maximal addressing mode it can252/// match by reference.253bool MSP430DAGToDAGISel::SelectAddr(SDValue N,254SDValue &Base, SDValue &Disp) {255MSP430ISelAddressMode AM;256257if (MatchAddress(N, AM))258return false;259260if (AM.BaseType == MSP430ISelAddressMode::RegBase)261if (!AM.Base.Reg.getNode())262AM.Base.Reg = CurDAG->getRegister(MSP430::SR, MVT::i16);263264Base = (AM.BaseType == MSP430ISelAddressMode::FrameIndexBase)265? CurDAG->getTargetFrameIndex(266AM.Base.FrameIndex,267N.getValueType())268: AM.Base.Reg;269270if (AM.GV)271Disp = CurDAG->getTargetGlobalAddress(AM.GV, SDLoc(N),272MVT::i16, AM.Disp,2730/*AM.SymbolFlags*/);274else if (AM.CP)275Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i16, AM.Alignment, AM.Disp,2760 /*AM.SymbolFlags*/);277else if (AM.ES)278Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i16, 0/*AM.SymbolFlags*/);279else if (AM.JT != -1)280Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i16, 0/*AM.SymbolFlags*/);281else if (AM.BlockAddr)282Disp = CurDAG->getTargetBlockAddress(AM.BlockAddr, MVT::i32, 0,2830/*AM.SymbolFlags*/);284else285Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(N), MVT::i16);286287return true;288}289290bool MSP430DAGToDAGISel::SelectInlineAsmMemoryOperand(291const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,292std::vector<SDValue> &OutOps) {293SDValue Op0, Op1;294switch (ConstraintID) {295default: return true;296case InlineAsm::ConstraintCode::m: // memory297if (!SelectAddr(Op, Op0, Op1))298return true;299break;300}301302OutOps.push_back(Op0);303OutOps.push_back(Op1);304return false;305}306307static bool isValidIndexedLoad(const LoadSDNode *LD) {308ISD::MemIndexedMode AM = LD->getAddressingMode();309if (AM != ISD::POST_INC || LD->getExtensionType() != ISD::NON_EXTLOAD)310return false;311312EVT VT = LD->getMemoryVT();313314switch (VT.getSimpleVT().SimpleTy) {315case MVT::i8:316if (LD->getOffset()->getAsZExtVal() != 1)317return false;318319break;320case MVT::i16:321if (LD->getOffset()->getAsZExtVal() != 2)322return false;323324break;325default:326return false;327}328329return true;330}331332bool MSP430DAGToDAGISel::tryIndexedLoad(SDNode *N) {333LoadSDNode *LD = cast<LoadSDNode>(N);334if (!isValidIndexedLoad(LD))335return false;336337MVT VT = LD->getMemoryVT().getSimpleVT();338339unsigned Opcode = 0;340switch (VT.SimpleTy) {341case MVT::i8:342Opcode = MSP430::MOV8rp;343break;344case MVT::i16:345Opcode = MSP430::MOV16rp;346break;347default:348return false;349}350351ReplaceNode(N,352CurDAG->getMachineNode(Opcode, SDLoc(N), VT, MVT::i16, MVT::Other,353LD->getBasePtr(), LD->getChain()));354return true;355}356357bool MSP430DAGToDAGISel::tryIndexedBinOp(SDNode *Op, SDValue N1, SDValue N2,358unsigned Opc8, unsigned Opc16) {359if (N1.getOpcode() == ISD::LOAD &&360N1.hasOneUse() &&361IsLegalToFold(N1, Op, Op, OptLevel)) {362LoadSDNode *LD = cast<LoadSDNode>(N1);363if (!isValidIndexedLoad(LD))364return false;365366MVT VT = LD->getMemoryVT().getSimpleVT();367unsigned Opc = (VT == MVT::i16 ? Opc16 : Opc8);368MachineMemOperand *MemRef = cast<MemSDNode>(N1)->getMemOperand();369SDValue Ops0[] = { N2, LD->getBasePtr(), LD->getChain() };370SDNode *ResNode =371CurDAG->SelectNodeTo(Op, Opc, VT, MVT::i16, MVT::Other, Ops0);372CurDAG->setNodeMemRefs(cast<MachineSDNode>(ResNode), {MemRef});373// Transfer chain.374ReplaceUses(SDValue(N1.getNode(), 2), SDValue(ResNode, 2));375// Transfer writeback.376ReplaceUses(SDValue(N1.getNode(), 1), SDValue(ResNode, 1));377return true;378}379380return false;381}382383384void MSP430DAGToDAGISel::Select(SDNode *Node) {385SDLoc dl(Node);386387// If we have a custom node, we already have selected!388if (Node->isMachineOpcode()) {389LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");390Node->setNodeId(-1);391return;392}393394// Few custom selection stuff.395switch (Node->getOpcode()) {396default: break;397case ISD::FrameIndex: {398assert(Node->getValueType(0) == MVT::i16);399int FI = cast<FrameIndexSDNode>(Node)->getIndex();400SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i16);401if (Node->hasOneUse()) {402CurDAG->SelectNodeTo(Node, MSP430::ADDframe, MVT::i16, TFI,403CurDAG->getTargetConstant(0, dl, MVT::i16));404return;405}406ReplaceNode(Node, CurDAG->getMachineNode(407MSP430::ADDframe, dl, MVT::i16, TFI,408CurDAG->getTargetConstant(0, dl, MVT::i16)));409return;410}411case ISD::LOAD:412if (tryIndexedLoad(Node))413return;414// Other cases are autogenerated.415break;416case ISD::ADD:417if (tryIndexedBinOp(Node, Node->getOperand(0), Node->getOperand(1),418MSP430::ADD8rp, MSP430::ADD16rp))419return;420else if (tryIndexedBinOp(Node, Node->getOperand(1), Node->getOperand(0),421MSP430::ADD8rp, MSP430::ADD16rp))422return;423424// Other cases are autogenerated.425break;426case ISD::SUB:427if (tryIndexedBinOp(Node, Node->getOperand(0), Node->getOperand(1),428MSP430::SUB8rp, MSP430::SUB16rp))429return;430431// Other cases are autogenerated.432break;433case ISD::AND:434if (tryIndexedBinOp(Node, Node->getOperand(0), Node->getOperand(1),435MSP430::AND8rp, MSP430::AND16rp))436return;437else if (tryIndexedBinOp(Node, Node->getOperand(1), Node->getOperand(0),438MSP430::AND8rp, MSP430::AND16rp))439return;440441// Other cases are autogenerated.442break;443case ISD::OR:444if (tryIndexedBinOp(Node, Node->getOperand(0), Node->getOperand(1),445MSP430::BIS8rp, MSP430::BIS16rp))446return;447else if (tryIndexedBinOp(Node, Node->getOperand(1), Node->getOperand(0),448MSP430::BIS8rp, MSP430::BIS16rp))449return;450451// Other cases are autogenerated.452break;453case ISD::XOR:454if (tryIndexedBinOp(Node, Node->getOperand(0), Node->getOperand(1),455MSP430::XOR8rp, MSP430::XOR16rp))456return;457else if (tryIndexedBinOp(Node, Node->getOperand(1), Node->getOperand(0),458MSP430::XOR8rp, MSP430::XOR16rp))459return;460461// Other cases are autogenerated.462break;463}464465// Select the default instruction466SelectCode(Node);467}468469470