Path: blob/main/contrib/llvm-project/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
35266 views
//===-- MSP430ISelLowering.cpp - MSP430 DAG Lowering Implementation ------===//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 implements the MSP430TargetLowering class.9//10//===----------------------------------------------------------------------===//1112#include "MSP430ISelLowering.h"13#include "MSP430.h"14#include "MSP430MachineFunctionInfo.h"15#include "MSP430Subtarget.h"16#include "MSP430TargetMachine.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/CallingConv.h"25#include "llvm/IR/DerivedTypes.h"26#include "llvm/IR/Function.h"27#include "llvm/IR/GlobalAlias.h"28#include "llvm/IR/GlobalVariable.h"29#include "llvm/IR/Intrinsics.h"30#include "llvm/Support/CommandLine.h"31#include "llvm/Support/Debug.h"32#include "llvm/Support/ErrorHandling.h"33#include "llvm/Support/raw_ostream.h"34using namespace llvm;3536#define DEBUG_TYPE "msp430-lower"3738static cl::opt<bool>MSP430NoLegalImmediate(39"msp430-no-legal-immediate", cl::Hidden,40cl::desc("Enable non legal immediates (for testing purposes only)"),41cl::init(false));4243MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM,44const MSP430Subtarget &STI)45: TargetLowering(TM) {4647// Set up the register classes.48addRegisterClass(MVT::i8, &MSP430::GR8RegClass);49addRegisterClass(MVT::i16, &MSP430::GR16RegClass);5051// Compute derived properties from the register classes52computeRegisterProperties(STI.getRegisterInfo());5354// Provide all sorts of operation actions55setStackPointerRegisterToSaveRestore(MSP430::SP);56setBooleanContents(ZeroOrOneBooleanContent);57setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?5859// We have post-incremented loads / stores.60setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);61setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);6263for (MVT VT : MVT::integer_valuetypes()) {64setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);65setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);66setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);67setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);68setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Expand);69}7071// We don't have any truncstores72setTruncStoreAction(MVT::i16, MVT::i8, Expand);7374setOperationAction(ISD::SRA, MVT::i8, Custom);75setOperationAction(ISD::SHL, MVT::i8, Custom);76setOperationAction(ISD::SRL, MVT::i8, Custom);77setOperationAction(ISD::SRA, MVT::i16, Custom);78setOperationAction(ISD::SHL, MVT::i16, Custom);79setOperationAction(ISD::SRL, MVT::i16, Custom);80setOperationAction(ISD::ROTL, MVT::i8, Expand);81setOperationAction(ISD::ROTR, MVT::i8, Expand);82setOperationAction(ISD::ROTL, MVT::i16, Expand);83setOperationAction(ISD::ROTR, MVT::i16, Expand);84setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);85setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);86setOperationAction(ISD::BlockAddress, MVT::i16, Custom);87setOperationAction(ISD::BR_JT, MVT::Other, Expand);88setOperationAction(ISD::BR_CC, MVT::i8, Custom);89setOperationAction(ISD::BR_CC, MVT::i16, Custom);90setOperationAction(ISD::BRCOND, MVT::Other, Expand);91setOperationAction(ISD::SETCC, MVT::i8, Custom);92setOperationAction(ISD::SETCC, MVT::i16, Custom);93setOperationAction(ISD::SELECT, MVT::i8, Expand);94setOperationAction(ISD::SELECT, MVT::i16, Expand);95setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);96setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);97setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);98setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);99setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);100setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);101setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);102103setOperationAction(ISD::CTTZ, MVT::i8, Expand);104setOperationAction(ISD::CTTZ, MVT::i16, Expand);105setOperationAction(ISD::CTLZ, MVT::i8, Expand);106setOperationAction(ISD::CTLZ, MVT::i16, Expand);107setOperationAction(ISD::CTPOP, MVT::i8, Expand);108setOperationAction(ISD::CTPOP, MVT::i16, Expand);109110setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);111setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);112setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);113setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);114setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);115setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);116117setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);118119// FIXME: Implement efficiently multiplication by a constant120setOperationAction(ISD::MUL, MVT::i8, Promote);121setOperationAction(ISD::MULHS, MVT::i8, Promote);122setOperationAction(ISD::MULHU, MVT::i8, Promote);123setOperationAction(ISD::SMUL_LOHI, MVT::i8, Promote);124setOperationAction(ISD::UMUL_LOHI, MVT::i8, Promote);125setOperationAction(ISD::MUL, MVT::i16, LibCall);126setOperationAction(ISD::MULHS, MVT::i16, Expand);127setOperationAction(ISD::MULHU, MVT::i16, Expand);128setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);129setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);130131setOperationAction(ISD::UDIV, MVT::i8, Promote);132setOperationAction(ISD::UDIVREM, MVT::i8, Promote);133setOperationAction(ISD::UREM, MVT::i8, Promote);134setOperationAction(ISD::SDIV, MVT::i8, Promote);135setOperationAction(ISD::SDIVREM, MVT::i8, Promote);136setOperationAction(ISD::SREM, MVT::i8, Promote);137setOperationAction(ISD::UDIV, MVT::i16, LibCall);138setOperationAction(ISD::UDIVREM, MVT::i16, Expand);139setOperationAction(ISD::UREM, MVT::i16, LibCall);140setOperationAction(ISD::SDIV, MVT::i16, LibCall);141setOperationAction(ISD::SDIVREM, MVT::i16, Expand);142setOperationAction(ISD::SREM, MVT::i16, LibCall);143144// varargs support145setOperationAction(ISD::VASTART, MVT::Other, Custom);146setOperationAction(ISD::VAARG, MVT::Other, Expand);147setOperationAction(ISD::VAEND, MVT::Other, Expand);148setOperationAction(ISD::VACOPY, MVT::Other, Expand);149setOperationAction(ISD::JumpTable, MVT::i16, Custom);150151// EABI Libcalls - EABI Section 6.2152const struct {153const RTLIB::Libcall Op;154const char * const Name;155const ISD::CondCode Cond;156} LibraryCalls[] = {157// Floating point conversions - EABI Table 6158{ RTLIB::FPROUND_F64_F32, "__mspabi_cvtdf", ISD::SETCC_INVALID },159{ RTLIB::FPEXT_F32_F64, "__mspabi_cvtfd", ISD::SETCC_INVALID },160// The following is NOT implemented in libgcc161//{ RTLIB::FPTOSINT_F64_I16, "__mspabi_fixdi", ISD::SETCC_INVALID },162{ RTLIB::FPTOSINT_F64_I32, "__mspabi_fixdli", ISD::SETCC_INVALID },163{ RTLIB::FPTOSINT_F64_I64, "__mspabi_fixdlli", ISD::SETCC_INVALID },164// The following is NOT implemented in libgcc165//{ RTLIB::FPTOUINT_F64_I16, "__mspabi_fixdu", ISD::SETCC_INVALID },166{ RTLIB::FPTOUINT_F64_I32, "__mspabi_fixdul", ISD::SETCC_INVALID },167{ RTLIB::FPTOUINT_F64_I64, "__mspabi_fixdull", ISD::SETCC_INVALID },168// The following is NOT implemented in libgcc169//{ RTLIB::FPTOSINT_F32_I16, "__mspabi_fixfi", ISD::SETCC_INVALID },170{ RTLIB::FPTOSINT_F32_I32, "__mspabi_fixfli", ISD::SETCC_INVALID },171{ RTLIB::FPTOSINT_F32_I64, "__mspabi_fixflli", ISD::SETCC_INVALID },172// The following is NOT implemented in libgcc173//{ RTLIB::FPTOUINT_F32_I16, "__mspabi_fixfu", ISD::SETCC_INVALID },174{ RTLIB::FPTOUINT_F32_I32, "__mspabi_fixful", ISD::SETCC_INVALID },175{ RTLIB::FPTOUINT_F32_I64, "__mspabi_fixfull", ISD::SETCC_INVALID },176// TODO The following IS implemented in libgcc177//{ RTLIB::SINTTOFP_I16_F64, "__mspabi_fltid", ISD::SETCC_INVALID },178{ RTLIB::SINTTOFP_I32_F64, "__mspabi_fltlid", ISD::SETCC_INVALID },179// TODO The following IS implemented in libgcc but is not in the EABI180{ RTLIB::SINTTOFP_I64_F64, "__mspabi_fltllid", ISD::SETCC_INVALID },181// TODO The following IS implemented in libgcc182//{ RTLIB::UINTTOFP_I16_F64, "__mspabi_fltud", ISD::SETCC_INVALID },183{ RTLIB::UINTTOFP_I32_F64, "__mspabi_fltuld", ISD::SETCC_INVALID },184// The following IS implemented in libgcc but is not in the EABI185{ RTLIB::UINTTOFP_I64_F64, "__mspabi_fltulld", ISD::SETCC_INVALID },186// TODO The following IS implemented in libgcc187//{ RTLIB::SINTTOFP_I16_F32, "__mspabi_fltif", ISD::SETCC_INVALID },188{ RTLIB::SINTTOFP_I32_F32, "__mspabi_fltlif", ISD::SETCC_INVALID },189// TODO The following IS implemented in libgcc but is not in the EABI190{ RTLIB::SINTTOFP_I64_F32, "__mspabi_fltllif", ISD::SETCC_INVALID },191// TODO The following IS implemented in libgcc192//{ RTLIB::UINTTOFP_I16_F32, "__mspabi_fltuf", ISD::SETCC_INVALID },193{ RTLIB::UINTTOFP_I32_F32, "__mspabi_fltulf", ISD::SETCC_INVALID },194// The following IS implemented in libgcc but is not in the EABI195{ RTLIB::UINTTOFP_I64_F32, "__mspabi_fltullf", ISD::SETCC_INVALID },196197// Floating point comparisons - EABI Table 7198{ RTLIB::OEQ_F64, "__mspabi_cmpd", ISD::SETEQ },199{ RTLIB::UNE_F64, "__mspabi_cmpd", ISD::SETNE },200{ RTLIB::OGE_F64, "__mspabi_cmpd", ISD::SETGE },201{ RTLIB::OLT_F64, "__mspabi_cmpd", ISD::SETLT },202{ RTLIB::OLE_F64, "__mspabi_cmpd", ISD::SETLE },203{ RTLIB::OGT_F64, "__mspabi_cmpd", ISD::SETGT },204{ RTLIB::OEQ_F32, "__mspabi_cmpf", ISD::SETEQ },205{ RTLIB::UNE_F32, "__mspabi_cmpf", ISD::SETNE },206{ RTLIB::OGE_F32, "__mspabi_cmpf", ISD::SETGE },207{ RTLIB::OLT_F32, "__mspabi_cmpf", ISD::SETLT },208{ RTLIB::OLE_F32, "__mspabi_cmpf", ISD::SETLE },209{ RTLIB::OGT_F32, "__mspabi_cmpf", ISD::SETGT },210211// Floating point arithmetic - EABI Table 8212{ RTLIB::ADD_F64, "__mspabi_addd", ISD::SETCC_INVALID },213{ RTLIB::ADD_F32, "__mspabi_addf", ISD::SETCC_INVALID },214{ RTLIB::DIV_F64, "__mspabi_divd", ISD::SETCC_INVALID },215{ RTLIB::DIV_F32, "__mspabi_divf", ISD::SETCC_INVALID },216{ RTLIB::MUL_F64, "__mspabi_mpyd", ISD::SETCC_INVALID },217{ RTLIB::MUL_F32, "__mspabi_mpyf", ISD::SETCC_INVALID },218{ RTLIB::SUB_F64, "__mspabi_subd", ISD::SETCC_INVALID },219{ RTLIB::SUB_F32, "__mspabi_subf", ISD::SETCC_INVALID },220// The following are NOT implemented in libgcc221// { RTLIB::NEG_F64, "__mspabi_negd", ISD::SETCC_INVALID },222// { RTLIB::NEG_F32, "__mspabi_negf", ISD::SETCC_INVALID },223224// Universal Integer Operations - EABI Table 9225{ RTLIB::SDIV_I16, "__mspabi_divi", ISD::SETCC_INVALID },226{ RTLIB::SDIV_I32, "__mspabi_divli", ISD::SETCC_INVALID },227{ RTLIB::SDIV_I64, "__mspabi_divlli", ISD::SETCC_INVALID },228{ RTLIB::UDIV_I16, "__mspabi_divu", ISD::SETCC_INVALID },229{ RTLIB::UDIV_I32, "__mspabi_divul", ISD::SETCC_INVALID },230{ RTLIB::UDIV_I64, "__mspabi_divull", ISD::SETCC_INVALID },231{ RTLIB::SREM_I16, "__mspabi_remi", ISD::SETCC_INVALID },232{ RTLIB::SREM_I32, "__mspabi_remli", ISD::SETCC_INVALID },233{ RTLIB::SREM_I64, "__mspabi_remlli", ISD::SETCC_INVALID },234{ RTLIB::UREM_I16, "__mspabi_remu", ISD::SETCC_INVALID },235{ RTLIB::UREM_I32, "__mspabi_remul", ISD::SETCC_INVALID },236{ RTLIB::UREM_I64, "__mspabi_remull", ISD::SETCC_INVALID },237238// Bitwise Operations - EABI Table 10239// TODO: __mspabi_[srli/srai/slli] ARE implemented in libgcc240{ RTLIB::SRL_I32, "__mspabi_srll", ISD::SETCC_INVALID },241{ RTLIB::SRA_I32, "__mspabi_sral", ISD::SETCC_INVALID },242{ RTLIB::SHL_I32, "__mspabi_slll", ISD::SETCC_INVALID },243// __mspabi_[srlll/srall/sllll/rlli/rlll] are NOT implemented in libgcc244245};246247for (const auto &LC : LibraryCalls) {248setLibcallName(LC.Op, LC.Name);249if (LC.Cond != ISD::SETCC_INVALID)250setCmpLibcallCC(LC.Op, LC.Cond);251}252253if (STI.hasHWMult16()) {254const struct {255const RTLIB::Libcall Op;256const char * const Name;257} LibraryCalls[] = {258// Integer Multiply - EABI Table 9259{ RTLIB::MUL_I16, "__mspabi_mpyi_hw" },260{ RTLIB::MUL_I32, "__mspabi_mpyl_hw" },261{ RTLIB::MUL_I64, "__mspabi_mpyll_hw" },262// TODO The __mspabi_mpysl*_hw functions ARE implemented in libgcc263// TODO The __mspabi_mpyul*_hw functions ARE implemented in libgcc264};265for (const auto &LC : LibraryCalls) {266setLibcallName(LC.Op, LC.Name);267}268} else if (STI.hasHWMult32()) {269const struct {270const RTLIB::Libcall Op;271const char * const Name;272} LibraryCalls[] = {273// Integer Multiply - EABI Table 9274{ RTLIB::MUL_I16, "__mspabi_mpyi_hw" },275{ RTLIB::MUL_I32, "__mspabi_mpyl_hw32" },276{ RTLIB::MUL_I64, "__mspabi_mpyll_hw32" },277// TODO The __mspabi_mpysl*_hw32 functions ARE implemented in libgcc278// TODO The __mspabi_mpyul*_hw32 functions ARE implemented in libgcc279};280for (const auto &LC : LibraryCalls) {281setLibcallName(LC.Op, LC.Name);282}283} else if (STI.hasHWMultF5()) {284const struct {285const RTLIB::Libcall Op;286const char * const Name;287} LibraryCalls[] = {288// Integer Multiply - EABI Table 9289{ RTLIB::MUL_I16, "__mspabi_mpyi_f5hw" },290{ RTLIB::MUL_I32, "__mspabi_mpyl_f5hw" },291{ RTLIB::MUL_I64, "__mspabi_mpyll_f5hw" },292// TODO The __mspabi_mpysl*_f5hw functions ARE implemented in libgcc293// TODO The __mspabi_mpyul*_f5hw functions ARE implemented in libgcc294};295for (const auto &LC : LibraryCalls) {296setLibcallName(LC.Op, LC.Name);297}298} else { // NoHWMult299const struct {300const RTLIB::Libcall Op;301const char * const Name;302} LibraryCalls[] = {303// Integer Multiply - EABI Table 9304{ RTLIB::MUL_I16, "__mspabi_mpyi" },305{ RTLIB::MUL_I32, "__mspabi_mpyl" },306{ RTLIB::MUL_I64, "__mspabi_mpyll" },307// The __mspabi_mpysl* functions are NOT implemented in libgcc308// The __mspabi_mpyul* functions are NOT implemented in libgcc309};310for (const auto &LC : LibraryCalls) {311setLibcallName(LC.Op, LC.Name);312}313setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::MSP430_BUILTIN);314}315316// Several of the runtime library functions use a special calling conv317setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::MSP430_BUILTIN);318setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::MSP430_BUILTIN);319setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::MSP430_BUILTIN);320setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::MSP430_BUILTIN);321setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::MSP430_BUILTIN);322setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::MSP430_BUILTIN);323setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::MSP430_BUILTIN);324setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::MSP430_BUILTIN);325setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::MSP430_BUILTIN);326setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::MSP430_BUILTIN);327setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::MSP430_BUILTIN);328setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::MSP430_BUILTIN);329setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::MSP430_BUILTIN);330setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::MSP430_BUILTIN);331// TODO: __mspabi_srall, __mspabi_srlll, __mspabi_sllll332333setMinFunctionAlignment(Align(2));334setPrefFunctionAlignment(Align(2));335setMaxAtomicSizeInBitsSupported(0);336}337338SDValue MSP430TargetLowering::LowerOperation(SDValue Op,339SelectionDAG &DAG) const {340switch (Op.getOpcode()) {341case ISD::SHL: // FALLTHROUGH342case ISD::SRL:343case ISD::SRA: return LowerShifts(Op, DAG);344case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);345case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);346case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);347case ISD::SETCC: return LowerSETCC(Op, DAG);348case ISD::BR_CC: return LowerBR_CC(Op, DAG);349case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);350case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);351case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);352case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);353case ISD::VASTART: return LowerVASTART(Op, DAG);354case ISD::JumpTable: return LowerJumpTable(Op, DAG);355default:356llvm_unreachable("unimplemented operand");357}358}359360// Define non profitable transforms into shifts361bool MSP430TargetLowering::shouldAvoidTransformToShift(EVT VT,362unsigned Amount) const {363return !(Amount == 8 || Amount == 9 || Amount<=2);364}365366// Implemented to verify test case assertions in367// tests/codegen/msp430/shift-amount-threshold-b.ll368bool MSP430TargetLowering::isLegalICmpImmediate(int64_t Immed) const {369if (MSP430NoLegalImmediate)370return Immed >= -32 && Immed < 32;371return TargetLowering::isLegalICmpImmediate(Immed);372}373374//===----------------------------------------------------------------------===//375// MSP430 Inline Assembly Support376//===----------------------------------------------------------------------===//377378/// getConstraintType - Given a constraint letter, return the type of379/// constraint it is for this target.380TargetLowering::ConstraintType381MSP430TargetLowering::getConstraintType(StringRef Constraint) const {382if (Constraint.size() == 1) {383switch (Constraint[0]) {384case 'r':385return C_RegisterClass;386default:387break;388}389}390return TargetLowering::getConstraintType(Constraint);391}392393std::pair<unsigned, const TargetRegisterClass *>394MSP430TargetLowering::getRegForInlineAsmConstraint(395const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {396if (Constraint.size() == 1) {397// GCC Constraint Letters398switch (Constraint[0]) {399default: break;400case 'r': // GENERAL_REGS401if (VT == MVT::i8)402return std::make_pair(0U, &MSP430::GR8RegClass);403404return std::make_pair(0U, &MSP430::GR16RegClass);405}406}407408return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);409}410411//===----------------------------------------------------------------------===//412// Calling Convention Implementation413//===----------------------------------------------------------------------===//414415#include "MSP430GenCallingConv.inc"416417/// For each argument in a function store the number of pieces it is composed418/// of.419template<typename ArgT>420static void ParseFunctionArgs(const SmallVectorImpl<ArgT> &Args,421SmallVectorImpl<unsigned> &Out) {422unsigned CurrentArgIndex;423424if (Args.empty())425return;426427CurrentArgIndex = Args[0].OrigArgIndex;428Out.push_back(0);429430for (auto &Arg : Args) {431if (CurrentArgIndex == Arg.OrigArgIndex) {432Out.back() += 1;433} else {434Out.push_back(1);435CurrentArgIndex = Arg.OrigArgIndex;436}437}438}439440static void AnalyzeVarArgs(CCState &State,441const SmallVectorImpl<ISD::OutputArg> &Outs) {442State.AnalyzeCallOperands(Outs, CC_MSP430_AssignStack);443}444445static void AnalyzeVarArgs(CCState &State,446const SmallVectorImpl<ISD::InputArg> &Ins) {447State.AnalyzeFormalArguments(Ins, CC_MSP430_AssignStack);448}449450/// Analyze incoming and outgoing function arguments. We need custom C++ code451/// to handle special constraints in the ABI like reversing the order of the452/// pieces of splitted arguments. In addition, all pieces of a certain argument453/// have to be passed either using registers or the stack but never mixing both.454template<typename ArgT>455static void AnalyzeArguments(CCState &State,456SmallVectorImpl<CCValAssign> &ArgLocs,457const SmallVectorImpl<ArgT> &Args) {458static const MCPhysReg CRegList[] = {459MSP430::R12, MSP430::R13, MSP430::R14, MSP430::R15460};461static const unsigned CNbRegs = std::size(CRegList);462static const MCPhysReg BuiltinRegList[] = {463MSP430::R8, MSP430::R9, MSP430::R10, MSP430::R11,464MSP430::R12, MSP430::R13, MSP430::R14, MSP430::R15465};466static const unsigned BuiltinNbRegs = std::size(BuiltinRegList);467468ArrayRef<MCPhysReg> RegList;469unsigned NbRegs;470471bool Builtin = (State.getCallingConv() == CallingConv::MSP430_BUILTIN);472if (Builtin) {473RegList = BuiltinRegList;474NbRegs = BuiltinNbRegs;475} else {476RegList = CRegList;477NbRegs = CNbRegs;478}479480if (State.isVarArg()) {481AnalyzeVarArgs(State, Args);482return;483}484485SmallVector<unsigned, 4> ArgsParts;486ParseFunctionArgs(Args, ArgsParts);487488if (Builtin) {489assert(ArgsParts.size() == 2 &&490"Builtin calling convention requires two arguments");491}492493unsigned RegsLeft = NbRegs;494bool UsedStack = false;495unsigned ValNo = 0;496497for (unsigned i = 0, e = ArgsParts.size(); i != e; i++) {498MVT ArgVT = Args[ValNo].VT;499ISD::ArgFlagsTy ArgFlags = Args[ValNo].Flags;500MVT LocVT = ArgVT;501CCValAssign::LocInfo LocInfo = CCValAssign::Full;502503// Promote i8 to i16504if (LocVT == MVT::i8) {505LocVT = MVT::i16;506if (ArgFlags.isSExt())507LocInfo = CCValAssign::SExt;508else if (ArgFlags.isZExt())509LocInfo = CCValAssign::ZExt;510else511LocInfo = CCValAssign::AExt;512}513514// Handle byval arguments515if (ArgFlags.isByVal()) {516State.HandleByVal(ValNo++, ArgVT, LocVT, LocInfo, 2, Align(2), ArgFlags);517continue;518}519520unsigned Parts = ArgsParts[i];521522if (Builtin) {523assert(Parts == 4 &&524"Builtin calling convention requires 64-bit arguments");525}526527if (!UsedStack && Parts == 2 && RegsLeft == 1) {528// Special case for 32-bit register split, see EABI section 3.3.3529unsigned Reg = State.AllocateReg(RegList);530State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo));531RegsLeft -= 1;532533UsedStack = true;534CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State);535} else if (Parts <= RegsLeft) {536for (unsigned j = 0; j < Parts; j++) {537unsigned Reg = State.AllocateReg(RegList);538State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo));539RegsLeft--;540}541} else {542UsedStack = true;543for (unsigned j = 0; j < Parts; j++)544CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State);545}546}547}548549static void AnalyzeRetResult(CCState &State,550const SmallVectorImpl<ISD::InputArg> &Ins) {551State.AnalyzeCallResult(Ins, RetCC_MSP430);552}553554static void AnalyzeRetResult(CCState &State,555const SmallVectorImpl<ISD::OutputArg> &Outs) {556State.AnalyzeReturn(Outs, RetCC_MSP430);557}558559template<typename ArgT>560static void AnalyzeReturnValues(CCState &State,561SmallVectorImpl<CCValAssign> &RVLocs,562const SmallVectorImpl<ArgT> &Args) {563AnalyzeRetResult(State, Args);564}565566SDValue MSP430TargetLowering::LowerFormalArguments(567SDValue Chain, CallingConv::ID CallConv, bool isVarArg,568const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,569SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {570571switch (CallConv) {572default:573report_fatal_error("Unsupported calling convention");574case CallingConv::C:575case CallingConv::Fast:576return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);577case CallingConv::MSP430_INTR:578if (Ins.empty())579return Chain;580report_fatal_error("ISRs cannot have arguments");581}582}583584SDValue585MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,586SmallVectorImpl<SDValue> &InVals) const {587SelectionDAG &DAG = CLI.DAG;588SDLoc &dl = CLI.DL;589SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;590SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;591SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;592SDValue Chain = CLI.Chain;593SDValue Callee = CLI.Callee;594bool &isTailCall = CLI.IsTailCall;595CallingConv::ID CallConv = CLI.CallConv;596bool isVarArg = CLI.IsVarArg;597598// MSP430 target does not yet support tail call optimization.599isTailCall = false;600601switch (CallConv) {602default:603report_fatal_error("Unsupported calling convention");604case CallingConv::MSP430_BUILTIN:605case CallingConv::Fast:606case CallingConv::C:607return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,608Outs, OutVals, Ins, dl, DAG, InVals);609case CallingConv::MSP430_INTR:610report_fatal_error("ISRs cannot be called directly");611}612}613614/// LowerCCCArguments - transform physical registers into virtual registers and615/// generate load operations for arguments places on the stack.616// FIXME: struct return stuff617SDValue MSP430TargetLowering::LowerCCCArguments(618SDValue Chain, CallingConv::ID CallConv, bool isVarArg,619const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,620SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {621MachineFunction &MF = DAG.getMachineFunction();622MachineFrameInfo &MFI = MF.getFrameInfo();623MachineRegisterInfo &RegInfo = MF.getRegInfo();624MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();625626// Assign locations to all of the incoming arguments.627SmallVector<CCValAssign, 16> ArgLocs;628CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,629*DAG.getContext());630AnalyzeArguments(CCInfo, ArgLocs, Ins);631632// Create frame index for the start of the first vararg value633if (isVarArg) {634unsigned Offset = CCInfo.getStackSize();635FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, Offset, true));636}637638for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {639CCValAssign &VA = ArgLocs[i];640if (VA.isRegLoc()) {641// Arguments passed in registers642EVT RegVT = VA.getLocVT();643switch (RegVT.getSimpleVT().SimpleTy) {644default:645{646#ifndef NDEBUG647errs() << "LowerFormalArguments Unhandled argument type: "648<< RegVT << "\n";649#endif650llvm_unreachable(nullptr);651}652case MVT::i16:653Register VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);654RegInfo.addLiveIn(VA.getLocReg(), VReg);655SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);656657// If this is an 8-bit value, it is really passed promoted to 16658// bits. Insert an assert[sz]ext to capture this, then truncate to the659// right size.660if (VA.getLocInfo() == CCValAssign::SExt)661ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,662DAG.getValueType(VA.getValVT()));663else if (VA.getLocInfo() == CCValAssign::ZExt)664ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,665DAG.getValueType(VA.getValVT()));666667if (VA.getLocInfo() != CCValAssign::Full)668ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);669670InVals.push_back(ArgValue);671}672} else {673// Only arguments passed on the stack should make it here.674assert(VA.isMemLoc());675676SDValue InVal;677ISD::ArgFlagsTy Flags = Ins[i].Flags;678679if (Flags.isByVal()) {680MVT PtrVT = VA.getLocVT();681int FI = MFI.CreateFixedObject(Flags.getByValSize(),682VA.getLocMemOffset(), true);683InVal = DAG.getFrameIndex(FI, PtrVT);684} else {685// Load the argument to a virtual register686unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;687if (ObjSize > 2) {688errs() << "LowerFormalArguments Unhandled argument type: "689<< VA.getLocVT() << "\n";690}691// Create the frame index object for this incoming parameter...692int FI = MFI.CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);693694// Create the SelectionDAG nodes corresponding to a load695//from this parameter696SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);697InVal = DAG.getLoad(698VA.getLocVT(), dl, Chain, FIN,699MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));700}701702InVals.push_back(InVal);703}704}705706for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {707if (Ins[i].Flags.isSRet()) {708Register Reg = FuncInfo->getSRetReturnReg();709if (!Reg) {710Reg = MF.getRegInfo().createVirtualRegister(711getRegClassFor(MVT::i16));712FuncInfo->setSRetReturnReg(Reg);713}714SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[i]);715Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);716}717}718719return Chain;720}721722bool723MSP430TargetLowering::CanLowerReturn(CallingConv::ID CallConv,724MachineFunction &MF,725bool IsVarArg,726const SmallVectorImpl<ISD::OutputArg> &Outs,727LLVMContext &Context) const {728SmallVector<CCValAssign, 16> RVLocs;729CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);730return CCInfo.CheckReturn(Outs, RetCC_MSP430);731}732733SDValue734MSP430TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,735bool isVarArg,736const SmallVectorImpl<ISD::OutputArg> &Outs,737const SmallVectorImpl<SDValue> &OutVals,738const SDLoc &dl, SelectionDAG &DAG) const {739740MachineFunction &MF = DAG.getMachineFunction();741742// CCValAssign - represent the assignment of the return value to a location743SmallVector<CCValAssign, 16> RVLocs;744745// ISRs cannot return any value.746if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())747report_fatal_error("ISRs cannot return any value");748749// CCState - Info about the registers and stack slot.750CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,751*DAG.getContext());752753// Analize return values.754AnalyzeReturnValues(CCInfo, RVLocs, Outs);755756SDValue Glue;757SmallVector<SDValue, 4> RetOps(1, Chain);758759// Copy the result values into the output registers.760for (unsigned i = 0; i != RVLocs.size(); ++i) {761CCValAssign &VA = RVLocs[i];762assert(VA.isRegLoc() && "Can only return in registers!");763764Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),765OutVals[i], Glue);766767// Guarantee that all emitted copies are stuck together,768// avoiding something bad.769Glue = Chain.getValue(1);770RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));771}772773if (MF.getFunction().hasStructRetAttr()) {774MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();775Register Reg = FuncInfo->getSRetReturnReg();776777if (!Reg)778llvm_unreachable("sret virtual register not created in entry block");779780MVT PtrVT = getFrameIndexTy(DAG.getDataLayout());781SDValue Val =782DAG.getCopyFromReg(Chain, dl, Reg, PtrVT);783unsigned R12 = MSP430::R12;784785Chain = DAG.getCopyToReg(Chain, dl, R12, Val, Glue);786Glue = Chain.getValue(1);787RetOps.push_back(DAG.getRegister(R12, PtrVT));788}789790unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?791MSP430ISD::RETI_GLUE : MSP430ISD::RET_GLUE);792793RetOps[0] = Chain; // Update chain.794795// Add the glue if we have it.796if (Glue.getNode())797RetOps.push_back(Glue);798799return DAG.getNode(Opc, dl, MVT::Other, RetOps);800}801802/// LowerCCCCallTo - functions arguments are copied from virtual regs to803/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.804SDValue MSP430TargetLowering::LowerCCCCallTo(805SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,806bool isTailCall, const SmallVectorImpl<ISD::OutputArg> &Outs,807const SmallVectorImpl<SDValue> &OutVals,808const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,809SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {810// Analyze operands of the call, assigning locations to each operand.811SmallVector<CCValAssign, 16> ArgLocs;812CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,813*DAG.getContext());814AnalyzeArguments(CCInfo, ArgLocs, Outs);815816// Get a count of how many bytes are to be pushed on the stack.817unsigned NumBytes = CCInfo.getStackSize();818MVT PtrVT = getFrameIndexTy(DAG.getDataLayout());819820Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);821822SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;823SmallVector<SDValue, 12> MemOpChains;824SDValue StackPtr;825826// Walk the register/memloc assignments, inserting copies/loads.827for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {828CCValAssign &VA = ArgLocs[i];829830SDValue Arg = OutVals[i];831832// Promote the value if needed.833switch (VA.getLocInfo()) {834default: llvm_unreachable("Unknown loc info!");835case CCValAssign::Full: break;836case CCValAssign::SExt:837Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);838break;839case CCValAssign::ZExt:840Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);841break;842case CCValAssign::AExt:843Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);844break;845}846847// Arguments that can be passed on register must be kept at RegsToPass848// vector849if (VA.isRegLoc()) {850RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));851} else {852assert(VA.isMemLoc());853854if (!StackPtr.getNode())855StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SP, PtrVT);856857SDValue PtrOff =858DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,859DAG.getIntPtrConstant(VA.getLocMemOffset(), dl));860861SDValue MemOp;862ISD::ArgFlagsTy Flags = Outs[i].Flags;863864if (Flags.isByVal()) {865SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i16);866MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,867Flags.getNonZeroByValAlign(),868/*isVolatile*/ false,869/*AlwaysInline=*/true,870/*CI=*/nullptr, std::nullopt,871MachinePointerInfo(), MachinePointerInfo());872} else {873MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());874}875876MemOpChains.push_back(MemOp);877}878}879880// Transform all store nodes into one single node because all store nodes are881// independent of each other.882if (!MemOpChains.empty())883Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);884885// Build a sequence of copy-to-reg nodes chained together with token chain and886// flag operands which copy the outgoing args into registers. The InGlue in887// necessary since all emitted instructions must be stuck together.888SDValue InGlue;889for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {890Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,891RegsToPass[i].second, InGlue);892InGlue = Chain.getValue(1);893}894895// If the callee is a GlobalAddress node (quite common, every direct call is)896// turn it into a TargetGlobalAddress node so that legalize doesn't hack it.897// Likewise ExternalSymbol -> TargetExternalSymbol.898if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))899Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);900else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))901Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);902903// Returns a chain & a flag for retval copy to use.904SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);905SmallVector<SDValue, 8> Ops;906Ops.push_back(Chain);907Ops.push_back(Callee);908909// Add argument registers to the end of the list so that they are910// known live into the call.911for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)912Ops.push_back(DAG.getRegister(RegsToPass[i].first,913RegsToPass[i].second.getValueType()));914915if (InGlue.getNode())916Ops.push_back(InGlue);917918Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, Ops);919InGlue = Chain.getValue(1);920921// Create the CALLSEQ_END node.922Chain = DAG.getCALLSEQ_END(Chain, NumBytes, 0, InGlue, dl);923InGlue = Chain.getValue(1);924925// Handle result values, copying them out of physregs into vregs that we926// return.927return LowerCallResult(Chain, InGlue, CallConv, isVarArg, Ins, dl,928DAG, InVals);929}930931/// LowerCallResult - Lower the result values of a call into the932/// appropriate copies out of appropriate physical registers.933///934SDValue MSP430TargetLowering::LowerCallResult(935SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool isVarArg,936const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,937SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {938939// Assign locations to each value returned by this call.940SmallVector<CCValAssign, 16> RVLocs;941CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,942*DAG.getContext());943944AnalyzeReturnValues(CCInfo, RVLocs, Ins);945946// Copy all of the result registers out of their specified physreg.947for (unsigned i = 0; i != RVLocs.size(); ++i) {948Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),949RVLocs[i].getValVT(), InGlue).getValue(1);950InGlue = Chain.getValue(2);951InVals.push_back(Chain.getValue(0));952}953954return Chain;955}956957SDValue MSP430TargetLowering::LowerShifts(SDValue Op,958SelectionDAG &DAG) const {959unsigned Opc = Op.getOpcode();960SDNode* N = Op.getNode();961EVT VT = Op.getValueType();962SDLoc dl(N);963964// Expand non-constant shifts to loops:965if (!isa<ConstantSDNode>(N->getOperand(1)))966return Op;967968uint64_t ShiftAmount = N->getConstantOperandVal(1);969970// Expand the stuff into sequence of shifts.971SDValue Victim = N->getOperand(0);972973if (ShiftAmount >= 8) {974assert(VT == MVT::i16 && "Can not shift i8 by 8 and more");975switch(Opc) {976default:977llvm_unreachable("Unknown shift");978case ISD::SHL:979// foo << (8 + N) => swpb(zext(foo)) << N980Victim = DAG.getZeroExtendInReg(Victim, dl, MVT::i8);981Victim = DAG.getNode(ISD::BSWAP, dl, VT, Victim);982break;983case ISD::SRA:984case ISD::SRL:985// foo >> (8 + N) => sxt(swpb(foo)) >> N986Victim = DAG.getNode(ISD::BSWAP, dl, VT, Victim);987Victim = (Opc == ISD::SRA)988? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT, Victim,989DAG.getValueType(MVT::i8))990: DAG.getZeroExtendInReg(Victim, dl, MVT::i8);991break;992}993ShiftAmount -= 8;994}995996if (Opc == ISD::SRL && ShiftAmount) {997// Emit a special goodness here:998// srl A, 1 => clrc; rrc A999Victim = DAG.getNode(MSP430ISD::RRCL, dl, VT, Victim);1000ShiftAmount -= 1;1001}10021003while (ShiftAmount--)1004Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),1005dl, VT, Victim);10061007return Victim;1008}10091010SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,1011SelectionDAG &DAG) const {1012const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();1013int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();1014EVT PtrVT = Op.getValueType();10151016// Create the TargetGlobalAddress node, folding in the constant offset.1017SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op), PtrVT, Offset);1018return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op), PtrVT, Result);1019}10201021SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,1022SelectionDAG &DAG) const {1023SDLoc dl(Op);1024const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();1025EVT PtrVT = Op.getValueType();1026SDValue Result = DAG.getTargetExternalSymbol(Sym, PtrVT);10271028return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result);1029}10301031SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,1032SelectionDAG &DAG) const {1033SDLoc dl(Op);1034const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();1035EVT PtrVT = Op.getValueType();1036SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT);10371038return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result);1039}10401041static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,1042ISD::CondCode CC, const SDLoc &dl, SelectionDAG &DAG) {1043// FIXME: Handle bittests someday1044assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");10451046// FIXME: Handle jump negative someday1047MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;1048switch (CC) {1049default: llvm_unreachable("Invalid integer condition!");1050case ISD::SETEQ:1051TCC = MSP430CC::COND_E; // aka COND_Z1052// Minor optimization: if LHS is a constant, swap operands, then the1053// constant can be folded into comparison.1054if (LHS.getOpcode() == ISD::Constant)1055std::swap(LHS, RHS);1056break;1057case ISD::SETNE:1058TCC = MSP430CC::COND_NE; // aka COND_NZ1059// Minor optimization: if LHS is a constant, swap operands, then the1060// constant can be folded into comparison.1061if (LHS.getOpcode() == ISD::Constant)1062std::swap(LHS, RHS);1063break;1064case ISD::SETULE:1065std::swap(LHS, RHS);1066[[fallthrough]];1067case ISD::SETUGE:1068// Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to1069// fold constant into instruction.1070if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {1071LHS = RHS;1072RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));1073TCC = MSP430CC::COND_LO;1074break;1075}1076TCC = MSP430CC::COND_HS; // aka COND_C1077break;1078case ISD::SETUGT:1079std::swap(LHS, RHS);1080[[fallthrough]];1081case ISD::SETULT:1082// Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to1083// fold constant into instruction.1084if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {1085LHS = RHS;1086RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));1087TCC = MSP430CC::COND_HS;1088break;1089}1090TCC = MSP430CC::COND_LO; // aka COND_NC1091break;1092case ISD::SETLE:1093std::swap(LHS, RHS);1094[[fallthrough]];1095case ISD::SETGE:1096// Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to1097// fold constant into instruction.1098if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {1099LHS = RHS;1100RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));1101TCC = MSP430CC::COND_L;1102break;1103}1104TCC = MSP430CC::COND_GE;1105break;1106case ISD::SETGT:1107std::swap(LHS, RHS);1108[[fallthrough]];1109case ISD::SETLT:1110// Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to1111// fold constant into instruction.1112if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {1113LHS = RHS;1114RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));1115TCC = MSP430CC::COND_GE;1116break;1117}1118TCC = MSP430CC::COND_L;1119break;1120}11211122TargetCC = DAG.getConstant(TCC, dl, MVT::i8);1123return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);1124}112511261127SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {1128SDValue Chain = Op.getOperand(0);1129ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();1130SDValue LHS = Op.getOperand(2);1131SDValue RHS = Op.getOperand(3);1132SDValue Dest = Op.getOperand(4);1133SDLoc dl (Op);11341135SDValue TargetCC;1136SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);11371138return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),1139Chain, Dest, TargetCC, Flag);1140}11411142SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {1143SDValue LHS = Op.getOperand(0);1144SDValue RHS = Op.getOperand(1);1145SDLoc dl (Op);11461147// If we are doing an AND and testing against zero, then the CMP1148// will not be generated. The AND (or BIT) will generate the condition codes,1149// but they are different from CMP.1150// FIXME: since we're doing a post-processing, use a pseudoinstr here, so1151// lowering & isel wouldn't diverge.1152bool andCC = isNullConstant(RHS) && LHS.hasOneUse() &&1153(LHS.getOpcode() == ISD::AND ||1154(LHS.getOpcode() == ISD::TRUNCATE &&1155LHS.getOperand(0).getOpcode() == ISD::AND));1156ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();1157SDValue TargetCC;1158SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);11591160// Get the condition codes directly from the status register, if its easy.1161// Otherwise a branch will be generated. Note that the AND and BIT1162// instructions generate different flags than CMP, the carry bit can be used1163// for NE/EQ.1164bool Invert = false;1165bool Shift = false;1166bool Convert = true;1167switch (TargetCC->getAsZExtVal()) {1168default:1169Convert = false;1170break;1171case MSP430CC::COND_HS:1172// Res = SR & 1, no processing is required1173break;1174case MSP430CC::COND_LO:1175// Res = ~(SR & 1)1176Invert = true;1177break;1178case MSP430CC::COND_NE:1179if (andCC) {1180// C = ~Z, thus Res = SR & 1, no processing is required1181} else {1182// Res = ~((SR >> 1) & 1)1183Shift = true;1184Invert = true;1185}1186break;1187case MSP430CC::COND_E:1188Shift = true;1189// C = ~Z for AND instruction, thus we can put Res = ~(SR & 1), however,1190// Res = (SR >> 1) & 1 is 1 word shorter.1191break;1192}1193EVT VT = Op.getValueType();1194SDValue One = DAG.getConstant(1, dl, VT);1195if (Convert) {1196SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SR,1197MVT::i16, Flag);1198if (Shift)1199// FIXME: somewhere this is turned into a SRL, lower it MSP specific?1200SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);1201SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);1202if (Invert)1203SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);1204return SR;1205} else {1206SDValue Zero = DAG.getConstant(0, dl, VT);1207SDValue Ops[] = {One, Zero, TargetCC, Flag};1208return DAG.getNode(MSP430ISD::SELECT_CC, dl, Op.getValueType(), Ops);1209}1210}12111212SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,1213SelectionDAG &DAG) const {1214SDValue LHS = Op.getOperand(0);1215SDValue RHS = Op.getOperand(1);1216SDValue TrueV = Op.getOperand(2);1217SDValue FalseV = Op.getOperand(3);1218ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();1219SDLoc dl (Op);12201221SDValue TargetCC;1222SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);12231224SDValue Ops[] = {TrueV, FalseV, TargetCC, Flag};12251226return DAG.getNode(MSP430ISD::SELECT_CC, dl, Op.getValueType(), Ops);1227}12281229SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,1230SelectionDAG &DAG) const {1231SDValue Val = Op.getOperand(0);1232EVT VT = Op.getValueType();1233SDLoc dl(Op);12341235assert(VT == MVT::i16 && "Only support i16 for now!");12361237return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,1238DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),1239DAG.getValueType(Val.getValueType()));1240}12411242SDValue1243MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {1244MachineFunction &MF = DAG.getMachineFunction();1245MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();1246int ReturnAddrIndex = FuncInfo->getRAIndex();1247MVT PtrVT = getFrameIndexTy(MF.getDataLayout());12481249if (ReturnAddrIndex == 0) {1250// Set up a frame object for the return address.1251uint64_t SlotSize = PtrVT.getStoreSize();1252ReturnAddrIndex = MF.getFrameInfo().CreateFixedObject(SlotSize, -SlotSize,1253true);1254FuncInfo->setRAIndex(ReturnAddrIndex);1255}12561257return DAG.getFrameIndex(ReturnAddrIndex, PtrVT);1258}12591260SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,1261SelectionDAG &DAG) const {1262MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();1263MFI.setReturnAddressIsTaken(true);12641265if (verifyReturnAddressArgumentIsConstant(Op, DAG))1266return SDValue();12671268unsigned Depth = Op.getConstantOperandVal(0);1269SDLoc dl(Op);1270EVT PtrVT = Op.getValueType();12711272if (Depth > 0) {1273SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);1274SDValue Offset =1275DAG.getConstant(PtrVT.getStoreSize(), dl, MVT::i16);1276return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),1277DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset),1278MachinePointerInfo());1279}12801281// Just load the return address.1282SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);1283return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI,1284MachinePointerInfo());1285}12861287SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,1288SelectionDAG &DAG) const {1289MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();1290MFI.setFrameAddressIsTaken(true);12911292EVT VT = Op.getValueType();1293SDLoc dl(Op); // FIXME probably not meaningful1294unsigned Depth = Op.getConstantOperandVal(0);1295SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,1296MSP430::R4, VT);1297while (Depth--)1298FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,1299MachinePointerInfo());1300return FrameAddr;1301}13021303SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,1304SelectionDAG &DAG) const {1305MachineFunction &MF = DAG.getMachineFunction();1306MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();13071308SDValue Ptr = Op.getOperand(1);1309EVT PtrVT = Ptr.getValueType();13101311// Frame index of first vararg argument1312SDValue FrameIndex =1313DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);1314const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();13151316// Create a store of the frame index to the location operand1317return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex, Ptr,1318MachinePointerInfo(SV));1319}13201321SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op,1322SelectionDAG &DAG) const {1323JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);1324EVT PtrVT = Op.getValueType();1325SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);1326return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT), PtrVT, Result);1327}13281329/// getPostIndexedAddressParts - returns true by value, base pointer and1330/// offset pointer and addressing mode by reference if this node can be1331/// combined with a load / store to form a post-indexed load / store.1332bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,1333SDValue &Base,1334SDValue &Offset,1335ISD::MemIndexedMode &AM,1336SelectionDAG &DAG) const {13371338LoadSDNode *LD = cast<LoadSDNode>(N);1339if (LD->getExtensionType() != ISD::NON_EXTLOAD)1340return false;13411342EVT VT = LD->getMemoryVT();1343if (VT != MVT::i8 && VT != MVT::i16)1344return false;13451346if (Op->getOpcode() != ISD::ADD)1347return false;13481349if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {1350uint64_t RHSC = RHS->getZExtValue();1351if ((VT == MVT::i16 && RHSC != 2) ||1352(VT == MVT::i8 && RHSC != 1))1353return false;13541355Base = Op->getOperand(0);1356Offset = DAG.getConstant(RHSC, SDLoc(N), VT);1357AM = ISD::POST_INC;1358return true;1359}13601361return false;1362}136313641365const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {1366switch ((MSP430ISD::NodeType)Opcode) {1367case MSP430ISD::FIRST_NUMBER: break;1368case MSP430ISD::RET_GLUE: return "MSP430ISD::RET_GLUE";1369case MSP430ISD::RETI_GLUE: return "MSP430ISD::RETI_GLUE";1370case MSP430ISD::RRA: return "MSP430ISD::RRA";1371case MSP430ISD::RLA: return "MSP430ISD::RLA";1372case MSP430ISD::RRC: return "MSP430ISD::RRC";1373case MSP430ISD::RRCL: return "MSP430ISD::RRCL";1374case MSP430ISD::CALL: return "MSP430ISD::CALL";1375case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";1376case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";1377case MSP430ISD::CMP: return "MSP430ISD::CMP";1378case MSP430ISD::SETCC: return "MSP430ISD::SETCC";1379case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";1380case MSP430ISD::DADD: return "MSP430ISD::DADD";1381}1382return nullptr;1383}13841385bool MSP430TargetLowering::isTruncateFree(Type *Ty1,1386Type *Ty2) const {1387if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())1388return false;13891390return (Ty1->getPrimitiveSizeInBits().getFixedValue() >1391Ty2->getPrimitiveSizeInBits().getFixedValue());1392}13931394bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {1395if (!VT1.isInteger() || !VT2.isInteger())1396return false;13971398return (VT1.getFixedSizeInBits() > VT2.getFixedSizeInBits());1399}14001401bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {1402// MSP430 implicitly zero-extends 8-bit results in 16-bit registers.1403return false && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);1404}14051406bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {1407// MSP430 implicitly zero-extends 8-bit results in 16-bit registers.1408return false && VT1 == MVT::i8 && VT2 == MVT::i16;1409}14101411//===----------------------------------------------------------------------===//1412// Other Lowering Code1413//===----------------------------------------------------------------------===//14141415MachineBasicBlock *1416MSP430TargetLowering::EmitShiftInstr(MachineInstr &MI,1417MachineBasicBlock *BB) const {1418MachineFunction *F = BB->getParent();1419MachineRegisterInfo &RI = F->getRegInfo();1420DebugLoc dl = MI.getDebugLoc();1421const TargetInstrInfo &TII = *F->getSubtarget().getInstrInfo();14221423unsigned Opc;1424bool ClearCarry = false;1425const TargetRegisterClass * RC;1426switch (MI.getOpcode()) {1427default: llvm_unreachable("Invalid shift opcode!");1428case MSP430::Shl8:1429Opc = MSP430::ADD8rr;1430RC = &MSP430::GR8RegClass;1431break;1432case MSP430::Shl16:1433Opc = MSP430::ADD16rr;1434RC = &MSP430::GR16RegClass;1435break;1436case MSP430::Sra8:1437Opc = MSP430::RRA8r;1438RC = &MSP430::GR8RegClass;1439break;1440case MSP430::Sra16:1441Opc = MSP430::RRA16r;1442RC = &MSP430::GR16RegClass;1443break;1444case MSP430::Srl8:1445ClearCarry = true;1446Opc = MSP430::RRC8r;1447RC = &MSP430::GR8RegClass;1448break;1449case MSP430::Srl16:1450ClearCarry = true;1451Opc = MSP430::RRC16r;1452RC = &MSP430::GR16RegClass;1453break;1454case MSP430::Rrcl8:1455case MSP430::Rrcl16: {1456BuildMI(*BB, MI, dl, TII.get(MSP430::BIC16rc), MSP430::SR)1457.addReg(MSP430::SR).addImm(1);1458Register SrcReg = MI.getOperand(1).getReg();1459Register DstReg = MI.getOperand(0).getReg();1460unsigned RrcOpc = MI.getOpcode() == MSP430::Rrcl161461? MSP430::RRC16r : MSP430::RRC8r;1462BuildMI(*BB, MI, dl, TII.get(RrcOpc), DstReg)1463.addReg(SrcReg);1464MI.eraseFromParent(); // The pseudo instruction is gone now.1465return BB;1466}1467}14681469const BasicBlock *LLVM_BB = BB->getBasicBlock();1470MachineFunction::iterator I = ++BB->getIterator();14711472// Create loop block1473MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);1474MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);14751476F->insert(I, LoopBB);1477F->insert(I, RemBB);14781479// Update machine-CFG edges by transferring all successors of the current1480// block to the block containing instructions after shift.1481RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),1482BB->end());1483RemBB->transferSuccessorsAndUpdatePHIs(BB);14841485// Add edges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB1486BB->addSuccessor(LoopBB);1487BB->addSuccessor(RemBB);1488LoopBB->addSuccessor(RemBB);1489LoopBB->addSuccessor(LoopBB);14901491Register ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);1492Register ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);1493Register ShiftReg = RI.createVirtualRegister(RC);1494Register ShiftReg2 = RI.createVirtualRegister(RC);1495Register ShiftAmtSrcReg = MI.getOperand(2).getReg();1496Register SrcReg = MI.getOperand(1).getReg();1497Register DstReg = MI.getOperand(0).getReg();14981499// BB:1500// cmp 0, N1501// je RemBB1502BuildMI(BB, dl, TII.get(MSP430::CMP8ri))1503.addReg(ShiftAmtSrcReg).addImm(0);1504BuildMI(BB, dl, TII.get(MSP430::JCC))1505.addMBB(RemBB)1506.addImm(MSP430CC::COND_E);15071508// LoopBB:1509// ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]1510// ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]1511// ShiftReg2 = shift ShiftReg1512// ShiftAmt2 = ShiftAmt - 1;1513BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)1514.addReg(SrcReg).addMBB(BB)1515.addReg(ShiftReg2).addMBB(LoopBB);1516BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)1517.addReg(ShiftAmtSrcReg).addMBB(BB)1518.addReg(ShiftAmtReg2).addMBB(LoopBB);1519if (ClearCarry)1520BuildMI(LoopBB, dl, TII.get(MSP430::BIC16rc), MSP430::SR)1521.addReg(MSP430::SR).addImm(1);1522if (Opc == MSP430::ADD8rr || Opc == MSP430::ADD16rr)1523BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)1524.addReg(ShiftReg)1525.addReg(ShiftReg);1526else1527BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)1528.addReg(ShiftReg);1529BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)1530.addReg(ShiftAmtReg).addImm(1);1531BuildMI(LoopBB, dl, TII.get(MSP430::JCC))1532.addMBB(LoopBB)1533.addImm(MSP430CC::COND_NE);15341535// RemBB:1536// DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]1537BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)1538.addReg(SrcReg).addMBB(BB)1539.addReg(ShiftReg2).addMBB(LoopBB);15401541MI.eraseFromParent(); // The pseudo instruction is gone now.1542return RemBB;1543}15441545MachineBasicBlock *1546MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,1547MachineBasicBlock *BB) const {1548unsigned Opc = MI.getOpcode();15491550if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||1551Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||1552Opc == MSP430::Srl8 || Opc == MSP430::Srl16 ||1553Opc == MSP430::Rrcl8 || Opc == MSP430::Rrcl16)1554return EmitShiftInstr(MI, BB);15551556const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();1557DebugLoc dl = MI.getDebugLoc();15581559assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&1560"Unexpected instr type to insert");15611562// To "insert" a SELECT instruction, we actually have to insert the diamond1563// control-flow pattern. The incoming instruction knows the destination vreg1564// to set, the condition code register to branch on, the true/false values to1565// select between, and a branch opcode to use.1566const BasicBlock *LLVM_BB = BB->getBasicBlock();1567MachineFunction::iterator I = ++BB->getIterator();15681569// thisMBB:1570// ...1571// TrueVal = ...1572// cmpTY ccX, r1, r21573// jCC copy1MBB1574// fallthrough --> copy0MBB1575MachineBasicBlock *thisMBB = BB;1576MachineFunction *F = BB->getParent();1577MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);1578MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);1579F->insert(I, copy0MBB);1580F->insert(I, copy1MBB);1581// Update machine-CFG edges by transferring all successors of the current1582// block to the new block which will contain the Phi node for the select.1583copy1MBB->splice(copy1MBB->begin(), BB,1584std::next(MachineBasicBlock::iterator(MI)), BB->end());1585copy1MBB->transferSuccessorsAndUpdatePHIs(BB);1586// Next, add the true and fallthrough blocks as its successors.1587BB->addSuccessor(copy0MBB);1588BB->addSuccessor(copy1MBB);15891590BuildMI(BB, dl, TII.get(MSP430::JCC))1591.addMBB(copy1MBB)1592.addImm(MI.getOperand(3).getImm());15931594// copy0MBB:1595// %FalseValue = ...1596// # fallthrough to copy1MBB1597BB = copy0MBB;15981599// Update machine-CFG edges1600BB->addSuccessor(copy1MBB);16011602// copy1MBB:1603// %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]1604// ...1605BB = copy1MBB;1606BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI), MI.getOperand(0).getReg())1607.addReg(MI.getOperand(2).getReg())1608.addMBB(copy0MBB)1609.addReg(MI.getOperand(1).getReg())1610.addMBB(thisMBB);16111612MI.eraseFromParent(); // The pseudo instruction is gone now.1613return BB;1614}161516161617