xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- MSP430ISelLowering.cpp - MSP430 DAG Lowering Implementation  ------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the MSP430TargetLowering class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "MSP430ISelLowering.h"
140b57cec5SDimitry Andric #include "MSP430.h"
150b57cec5SDimitry Andric #include "MSP430MachineFunctionInfo.h"
160b57cec5SDimitry Andric #include "MSP430Subtarget.h"
170b57cec5SDimitry Andric #include "MSP430TargetMachine.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/CallingConvLower.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
250b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
260b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
270b57cec5SDimitry Andric #include "llvm/IR/Function.h"
280b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h"
290b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
300b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
310b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
320b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
330b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
340b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
350b57cec5SDimitry Andric using namespace llvm;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #define DEBUG_TYPE "msp430-lower"
380b57cec5SDimitry Andric 
39480093f4SDimitry Andric static cl::opt<bool>MSP430NoLegalImmediate(
40480093f4SDimitry Andric   "msp430-no-legal-immediate", cl::Hidden,
41480093f4SDimitry Andric   cl::desc("Enable non legal immediates (for testing purposes only)"),
42480093f4SDimitry Andric   cl::init(false));
43480093f4SDimitry Andric 
440b57cec5SDimitry Andric MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM,
450b57cec5SDimitry Andric                                            const MSP430Subtarget &STI)
460b57cec5SDimitry Andric     : TargetLowering(TM) {
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   // Set up the register classes.
490b57cec5SDimitry Andric   addRegisterClass(MVT::i8,  &MSP430::GR8RegClass);
500b57cec5SDimitry Andric   addRegisterClass(MVT::i16, &MSP430::GR16RegClass);
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   // Compute derived properties from the register classes
530b57cec5SDimitry Andric   computeRegisterProperties(STI.getRegisterInfo());
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   // Provide all sorts of operation actions
560b57cec5SDimitry Andric   setStackPointerRegisterToSaveRestore(MSP430::SP);
570b57cec5SDimitry Andric   setBooleanContents(ZeroOrOneBooleanContent);
580b57cec5SDimitry Andric   setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   // We have post-incremented loads / stores.
610b57cec5SDimitry Andric   setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
620b57cec5SDimitry Andric   setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   for (MVT VT : MVT::integer_valuetypes()) {
650b57cec5SDimitry Andric     setLoadExtAction(ISD::EXTLOAD,  VT, MVT::i1,  Promote);
660b57cec5SDimitry Andric     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1,  Promote);
670b57cec5SDimitry Andric     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1,  Promote);
680b57cec5SDimitry Andric     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8,  Expand);
690b57cec5SDimitry Andric     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Expand);
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   // We don't have any truncstores
730b57cec5SDimitry Andric   setTruncStoreAction(MVT::i16, MVT::i8, Expand);
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   setOperationAction(ISD::SRA,              MVT::i8,    Custom);
760b57cec5SDimitry Andric   setOperationAction(ISD::SHL,              MVT::i8,    Custom);
770b57cec5SDimitry Andric   setOperationAction(ISD::SRL,              MVT::i8,    Custom);
780b57cec5SDimitry Andric   setOperationAction(ISD::SRA,              MVT::i16,   Custom);
790b57cec5SDimitry Andric   setOperationAction(ISD::SHL,              MVT::i16,   Custom);
800b57cec5SDimitry Andric   setOperationAction(ISD::SRL,              MVT::i16,   Custom);
810b57cec5SDimitry Andric   setOperationAction(ISD::ROTL,             MVT::i8,    Expand);
820b57cec5SDimitry Andric   setOperationAction(ISD::ROTR,             MVT::i8,    Expand);
830b57cec5SDimitry Andric   setOperationAction(ISD::ROTL,             MVT::i16,   Expand);
840b57cec5SDimitry Andric   setOperationAction(ISD::ROTR,             MVT::i16,   Expand);
850b57cec5SDimitry Andric   setOperationAction(ISD::GlobalAddress,    MVT::i16,   Custom);
860b57cec5SDimitry Andric   setOperationAction(ISD::ExternalSymbol,   MVT::i16,   Custom);
870b57cec5SDimitry Andric   setOperationAction(ISD::BlockAddress,     MVT::i16,   Custom);
880b57cec5SDimitry Andric   setOperationAction(ISD::BR_JT,            MVT::Other, Expand);
890b57cec5SDimitry Andric   setOperationAction(ISD::BR_CC,            MVT::i8,    Custom);
900b57cec5SDimitry Andric   setOperationAction(ISD::BR_CC,            MVT::i16,   Custom);
910b57cec5SDimitry Andric   setOperationAction(ISD::BRCOND,           MVT::Other, Expand);
920b57cec5SDimitry Andric   setOperationAction(ISD::SETCC,            MVT::i8,    Custom);
930b57cec5SDimitry Andric   setOperationAction(ISD::SETCC,            MVT::i16,   Custom);
940b57cec5SDimitry Andric   setOperationAction(ISD::SELECT,           MVT::i8,    Expand);
950b57cec5SDimitry Andric   setOperationAction(ISD::SELECT,           MVT::i16,   Expand);
960b57cec5SDimitry Andric   setOperationAction(ISD::SELECT_CC,        MVT::i8,    Custom);
970b57cec5SDimitry Andric   setOperationAction(ISD::SELECT_CC,        MVT::i16,   Custom);
980b57cec5SDimitry Andric   setOperationAction(ISD::SIGN_EXTEND,      MVT::i16,   Custom);
990b57cec5SDimitry Andric   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
1000b57cec5SDimitry Andric   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
1010b57cec5SDimitry Andric   setOperationAction(ISD::STACKSAVE,        MVT::Other, Expand);
1020b57cec5SDimitry Andric   setOperationAction(ISD::STACKRESTORE,     MVT::Other, Expand);
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   setOperationAction(ISD::CTTZ,             MVT::i8,    Expand);
1050b57cec5SDimitry Andric   setOperationAction(ISD::CTTZ,             MVT::i16,   Expand);
1060b57cec5SDimitry Andric   setOperationAction(ISD::CTLZ,             MVT::i8,    Expand);
1070b57cec5SDimitry Andric   setOperationAction(ISD::CTLZ,             MVT::i16,   Expand);
1080b57cec5SDimitry Andric   setOperationAction(ISD::CTPOP,            MVT::i8,    Expand);
1090b57cec5SDimitry Andric   setOperationAction(ISD::CTPOP,            MVT::i16,   Expand);
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   setOperationAction(ISD::SHL_PARTS,        MVT::i8,    Expand);
1120b57cec5SDimitry Andric   setOperationAction(ISD::SHL_PARTS,        MVT::i16,   Expand);
1130b57cec5SDimitry Andric   setOperationAction(ISD::SRL_PARTS,        MVT::i8,    Expand);
1140b57cec5SDimitry Andric   setOperationAction(ISD::SRL_PARTS,        MVT::i16,   Expand);
1150b57cec5SDimitry Andric   setOperationAction(ISD::SRA_PARTS,        MVT::i8,    Expand);
1160b57cec5SDimitry Andric   setOperationAction(ISD::SRA_PARTS,        MVT::i16,   Expand);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1,   Expand);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   // FIXME: Implement efficiently multiplication by a constant
1210b57cec5SDimitry Andric   setOperationAction(ISD::MUL,              MVT::i8,    Promote);
1220b57cec5SDimitry Andric   setOperationAction(ISD::MULHS,            MVT::i8,    Promote);
1230b57cec5SDimitry Andric   setOperationAction(ISD::MULHU,            MVT::i8,    Promote);
1240b57cec5SDimitry Andric   setOperationAction(ISD::SMUL_LOHI,        MVT::i8,    Promote);
1250b57cec5SDimitry Andric   setOperationAction(ISD::UMUL_LOHI,        MVT::i8,    Promote);
1260b57cec5SDimitry Andric   setOperationAction(ISD::MUL,              MVT::i16,   LibCall);
1270b57cec5SDimitry Andric   setOperationAction(ISD::MULHS,            MVT::i16,   Expand);
1280b57cec5SDimitry Andric   setOperationAction(ISD::MULHU,            MVT::i16,   Expand);
1290b57cec5SDimitry Andric   setOperationAction(ISD::SMUL_LOHI,        MVT::i16,   Expand);
1300b57cec5SDimitry Andric   setOperationAction(ISD::UMUL_LOHI,        MVT::i16,   Expand);
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   setOperationAction(ISD::UDIV,             MVT::i8,    Promote);
1330b57cec5SDimitry Andric   setOperationAction(ISD::UDIVREM,          MVT::i8,    Promote);
1340b57cec5SDimitry Andric   setOperationAction(ISD::UREM,             MVT::i8,    Promote);
1350b57cec5SDimitry Andric   setOperationAction(ISD::SDIV,             MVT::i8,    Promote);
1360b57cec5SDimitry Andric   setOperationAction(ISD::SDIVREM,          MVT::i8,    Promote);
1370b57cec5SDimitry Andric   setOperationAction(ISD::SREM,             MVT::i8,    Promote);
1380b57cec5SDimitry Andric   setOperationAction(ISD::UDIV,             MVT::i16,   LibCall);
1390b57cec5SDimitry Andric   setOperationAction(ISD::UDIVREM,          MVT::i16,   Expand);
1400b57cec5SDimitry Andric   setOperationAction(ISD::UREM,             MVT::i16,   LibCall);
1410b57cec5SDimitry Andric   setOperationAction(ISD::SDIV,             MVT::i16,   LibCall);
1420b57cec5SDimitry Andric   setOperationAction(ISD::SDIVREM,          MVT::i16,   Expand);
1430b57cec5SDimitry Andric   setOperationAction(ISD::SREM,             MVT::i16,   LibCall);
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   // varargs support
1460b57cec5SDimitry Andric   setOperationAction(ISD::VASTART,          MVT::Other, Custom);
1470b57cec5SDimitry Andric   setOperationAction(ISD::VAARG,            MVT::Other, Expand);
1480b57cec5SDimitry Andric   setOperationAction(ISD::VAEND,            MVT::Other, Expand);
1490b57cec5SDimitry Andric   setOperationAction(ISD::VACOPY,           MVT::Other, Expand);
1500b57cec5SDimitry Andric   setOperationAction(ISD::JumpTable,        MVT::i16,   Custom);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   // EABI Libcalls - EABI Section 6.2
1530b57cec5SDimitry Andric   const struct {
1540b57cec5SDimitry Andric     const RTLIB::Libcall Op;
1550b57cec5SDimitry Andric     const char * const Name;
1560b57cec5SDimitry Andric     const ISD::CondCode Cond;
1570b57cec5SDimitry Andric   } LibraryCalls[] = {
1580b57cec5SDimitry Andric     // Floating point conversions - EABI Table 6
1590b57cec5SDimitry Andric     { RTLIB::FPROUND_F64_F32,   "__mspabi_cvtdf",   ISD::SETCC_INVALID },
1600b57cec5SDimitry Andric     { RTLIB::FPEXT_F32_F64,     "__mspabi_cvtfd",   ISD::SETCC_INVALID },
1610b57cec5SDimitry Andric     // The following is NOT implemented in libgcc
1620b57cec5SDimitry Andric     //{ RTLIB::FPTOSINT_F64_I16,  "__mspabi_fixdi", ISD::SETCC_INVALID },
1630b57cec5SDimitry Andric     { RTLIB::FPTOSINT_F64_I32,  "__mspabi_fixdli",  ISD::SETCC_INVALID },
1640b57cec5SDimitry Andric     { RTLIB::FPTOSINT_F64_I64,  "__mspabi_fixdlli", ISD::SETCC_INVALID },
1650b57cec5SDimitry Andric     // The following is NOT implemented in libgcc
1660b57cec5SDimitry Andric     //{ RTLIB::FPTOUINT_F64_I16,  "__mspabi_fixdu", ISD::SETCC_INVALID },
1670b57cec5SDimitry Andric     { RTLIB::FPTOUINT_F64_I32,  "__mspabi_fixdul",  ISD::SETCC_INVALID },
1680b57cec5SDimitry Andric     { RTLIB::FPTOUINT_F64_I64,  "__mspabi_fixdull", ISD::SETCC_INVALID },
1690b57cec5SDimitry Andric     // The following is NOT implemented in libgcc
1700b57cec5SDimitry Andric     //{ RTLIB::FPTOSINT_F32_I16,  "__mspabi_fixfi", ISD::SETCC_INVALID },
1710b57cec5SDimitry Andric     { RTLIB::FPTOSINT_F32_I32,  "__mspabi_fixfli",  ISD::SETCC_INVALID },
1720b57cec5SDimitry Andric     { RTLIB::FPTOSINT_F32_I64,  "__mspabi_fixflli", ISD::SETCC_INVALID },
1730b57cec5SDimitry Andric     // The following is NOT implemented in libgcc
1740b57cec5SDimitry Andric     //{ RTLIB::FPTOUINT_F32_I16,  "__mspabi_fixfu", ISD::SETCC_INVALID },
1750b57cec5SDimitry Andric     { RTLIB::FPTOUINT_F32_I32,  "__mspabi_fixful",  ISD::SETCC_INVALID },
1760b57cec5SDimitry Andric     { RTLIB::FPTOUINT_F32_I64,  "__mspabi_fixfull", ISD::SETCC_INVALID },
1770b57cec5SDimitry Andric     // TODO The following IS implemented in libgcc
1780b57cec5SDimitry Andric     //{ RTLIB::SINTTOFP_I16_F64,  "__mspabi_fltid", ISD::SETCC_INVALID },
1790b57cec5SDimitry Andric     { RTLIB::SINTTOFP_I32_F64,  "__mspabi_fltlid",  ISD::SETCC_INVALID },
1800b57cec5SDimitry Andric     // TODO The following IS implemented in libgcc but is not in the EABI
1810b57cec5SDimitry Andric     { RTLIB::SINTTOFP_I64_F64,  "__mspabi_fltllid", ISD::SETCC_INVALID },
1820b57cec5SDimitry Andric     // TODO The following IS implemented in libgcc
1830b57cec5SDimitry Andric     //{ RTLIB::UINTTOFP_I16_F64,  "__mspabi_fltud", ISD::SETCC_INVALID },
1840b57cec5SDimitry Andric     { RTLIB::UINTTOFP_I32_F64,  "__mspabi_fltuld",  ISD::SETCC_INVALID },
1850b57cec5SDimitry Andric     // The following IS implemented in libgcc but is not in the EABI
1860b57cec5SDimitry Andric     { RTLIB::UINTTOFP_I64_F64,  "__mspabi_fltulld", ISD::SETCC_INVALID },
1870b57cec5SDimitry Andric     // TODO The following IS implemented in libgcc
1880b57cec5SDimitry Andric     //{ RTLIB::SINTTOFP_I16_F32,  "__mspabi_fltif", ISD::SETCC_INVALID },
1890b57cec5SDimitry Andric     { RTLIB::SINTTOFP_I32_F32,  "__mspabi_fltlif",  ISD::SETCC_INVALID },
1900b57cec5SDimitry Andric     // TODO The following IS implemented in libgcc but is not in the EABI
1910b57cec5SDimitry Andric     { RTLIB::SINTTOFP_I64_F32,  "__mspabi_fltllif", ISD::SETCC_INVALID },
1920b57cec5SDimitry Andric     // TODO The following IS implemented in libgcc
1930b57cec5SDimitry Andric     //{ RTLIB::UINTTOFP_I16_F32,  "__mspabi_fltuf", ISD::SETCC_INVALID },
1940b57cec5SDimitry Andric     { RTLIB::UINTTOFP_I32_F32,  "__mspabi_fltulf",  ISD::SETCC_INVALID },
1950b57cec5SDimitry Andric     // The following IS implemented in libgcc but is not in the EABI
1960b57cec5SDimitry Andric     { RTLIB::UINTTOFP_I64_F32,  "__mspabi_fltullf", ISD::SETCC_INVALID },
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric     // Floating point comparisons - EABI Table 7
1990b57cec5SDimitry Andric     { RTLIB::OEQ_F64, "__mspabi_cmpd", ISD::SETEQ },
2000b57cec5SDimitry Andric     { RTLIB::UNE_F64, "__mspabi_cmpd", ISD::SETNE },
2010b57cec5SDimitry Andric     { RTLIB::OGE_F64, "__mspabi_cmpd", ISD::SETGE },
2020b57cec5SDimitry Andric     { RTLIB::OLT_F64, "__mspabi_cmpd", ISD::SETLT },
2030b57cec5SDimitry Andric     { RTLIB::OLE_F64, "__mspabi_cmpd", ISD::SETLE },
2040b57cec5SDimitry Andric     { RTLIB::OGT_F64, "__mspabi_cmpd", ISD::SETGT },
2050b57cec5SDimitry Andric     { RTLIB::OEQ_F32, "__mspabi_cmpf", ISD::SETEQ },
2060b57cec5SDimitry Andric     { RTLIB::UNE_F32, "__mspabi_cmpf", ISD::SETNE },
2070b57cec5SDimitry Andric     { RTLIB::OGE_F32, "__mspabi_cmpf", ISD::SETGE },
2080b57cec5SDimitry Andric     { RTLIB::OLT_F32, "__mspabi_cmpf", ISD::SETLT },
2090b57cec5SDimitry Andric     { RTLIB::OLE_F32, "__mspabi_cmpf", ISD::SETLE },
2100b57cec5SDimitry Andric     { RTLIB::OGT_F32, "__mspabi_cmpf", ISD::SETGT },
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric     // Floating point arithmetic - EABI Table 8
2130b57cec5SDimitry Andric     { RTLIB::ADD_F64,  "__mspabi_addd", ISD::SETCC_INVALID },
2140b57cec5SDimitry Andric     { RTLIB::ADD_F32,  "__mspabi_addf", ISD::SETCC_INVALID },
2150b57cec5SDimitry Andric     { RTLIB::DIV_F64,  "__mspabi_divd", ISD::SETCC_INVALID },
2160b57cec5SDimitry Andric     { RTLIB::DIV_F32,  "__mspabi_divf", ISD::SETCC_INVALID },
2170b57cec5SDimitry Andric     { RTLIB::MUL_F64,  "__mspabi_mpyd", ISD::SETCC_INVALID },
2180b57cec5SDimitry Andric     { RTLIB::MUL_F32,  "__mspabi_mpyf", ISD::SETCC_INVALID },
2190b57cec5SDimitry Andric     { RTLIB::SUB_F64,  "__mspabi_subd", ISD::SETCC_INVALID },
2200b57cec5SDimitry Andric     { RTLIB::SUB_F32,  "__mspabi_subf", ISD::SETCC_INVALID },
2210b57cec5SDimitry Andric     // The following are NOT implemented in libgcc
2220b57cec5SDimitry Andric     // { RTLIB::NEG_F64,  "__mspabi_negd", ISD::SETCC_INVALID },
2230b57cec5SDimitry Andric     // { RTLIB::NEG_F32,  "__mspabi_negf", ISD::SETCC_INVALID },
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     // Universal Integer Operations - EABI Table 9
2260b57cec5SDimitry Andric     { RTLIB::SDIV_I16,   "__mspabi_divi", ISD::SETCC_INVALID },
2270b57cec5SDimitry Andric     { RTLIB::SDIV_I32,   "__mspabi_divli", ISD::SETCC_INVALID },
2280b57cec5SDimitry Andric     { RTLIB::SDIV_I64,   "__mspabi_divlli", ISD::SETCC_INVALID },
2290b57cec5SDimitry Andric     { RTLIB::UDIV_I16,   "__mspabi_divu", ISD::SETCC_INVALID },
2300b57cec5SDimitry Andric     { RTLIB::UDIV_I32,   "__mspabi_divul", ISD::SETCC_INVALID },
2310b57cec5SDimitry Andric     { RTLIB::UDIV_I64,   "__mspabi_divull", ISD::SETCC_INVALID },
2320b57cec5SDimitry Andric     { RTLIB::SREM_I16,   "__mspabi_remi", ISD::SETCC_INVALID },
2330b57cec5SDimitry Andric     { RTLIB::SREM_I32,   "__mspabi_remli", ISD::SETCC_INVALID },
2340b57cec5SDimitry Andric     { RTLIB::SREM_I64,   "__mspabi_remlli", ISD::SETCC_INVALID },
2350b57cec5SDimitry Andric     { RTLIB::UREM_I16,   "__mspabi_remu", ISD::SETCC_INVALID },
2360b57cec5SDimitry Andric     { RTLIB::UREM_I32,   "__mspabi_remul", ISD::SETCC_INVALID },
2370b57cec5SDimitry Andric     { RTLIB::UREM_I64,   "__mspabi_remull", ISD::SETCC_INVALID },
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric     // Bitwise Operations - EABI Table 10
2400b57cec5SDimitry Andric     // TODO: __mspabi_[srli/srai/slli] ARE implemented in libgcc
2410b57cec5SDimitry Andric     { RTLIB::SRL_I32,    "__mspabi_srll", ISD::SETCC_INVALID },
2420b57cec5SDimitry Andric     { RTLIB::SRA_I32,    "__mspabi_sral", ISD::SETCC_INVALID },
2430b57cec5SDimitry Andric     { RTLIB::SHL_I32,    "__mspabi_slll", ISD::SETCC_INVALID },
2440b57cec5SDimitry Andric     // __mspabi_[srlll/srall/sllll/rlli/rlll] are NOT implemented in libgcc
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   };
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   for (const auto &LC : LibraryCalls) {
2490b57cec5SDimitry Andric     setLibcallName(LC.Op, LC.Name);
2500b57cec5SDimitry Andric     if (LC.Cond != ISD::SETCC_INVALID)
2510b57cec5SDimitry Andric       setCmpLibcallCC(LC.Op, LC.Cond);
2520b57cec5SDimitry Andric   }
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   if (STI.hasHWMult16()) {
2550b57cec5SDimitry Andric     const struct {
2560b57cec5SDimitry Andric       const RTLIB::Libcall Op;
2570b57cec5SDimitry Andric       const char * const Name;
2580b57cec5SDimitry Andric     } LibraryCalls[] = {
2590b57cec5SDimitry Andric       // Integer Multiply - EABI Table 9
2600b57cec5SDimitry Andric       { RTLIB::MUL_I16,   "__mspabi_mpyi_hw" },
2610b57cec5SDimitry Andric       { RTLIB::MUL_I32,   "__mspabi_mpyl_hw" },
2620b57cec5SDimitry Andric       { RTLIB::MUL_I64,   "__mspabi_mpyll_hw" },
2630b57cec5SDimitry Andric       // TODO The __mspabi_mpysl*_hw functions ARE implemented in libgcc
2640b57cec5SDimitry Andric       // TODO The __mspabi_mpyul*_hw functions ARE implemented in libgcc
2650b57cec5SDimitry Andric     };
2660b57cec5SDimitry Andric     for (const auto &LC : LibraryCalls) {
2670b57cec5SDimitry Andric       setLibcallName(LC.Op, LC.Name);
2680b57cec5SDimitry Andric     }
2690b57cec5SDimitry Andric   } else if (STI.hasHWMult32()) {
2700b57cec5SDimitry Andric     const struct {
2710b57cec5SDimitry Andric       const RTLIB::Libcall Op;
2720b57cec5SDimitry Andric       const char * const Name;
2730b57cec5SDimitry Andric     } LibraryCalls[] = {
2740b57cec5SDimitry Andric       // Integer Multiply - EABI Table 9
2750b57cec5SDimitry Andric       { RTLIB::MUL_I16,   "__mspabi_mpyi_hw" },
2760b57cec5SDimitry Andric       { RTLIB::MUL_I32,   "__mspabi_mpyl_hw32" },
2770b57cec5SDimitry Andric       { RTLIB::MUL_I64,   "__mspabi_mpyll_hw32" },
2780b57cec5SDimitry Andric       // TODO The __mspabi_mpysl*_hw32 functions ARE implemented in libgcc
2790b57cec5SDimitry Andric       // TODO The __mspabi_mpyul*_hw32 functions ARE implemented in libgcc
2800b57cec5SDimitry Andric     };
2810b57cec5SDimitry Andric     for (const auto &LC : LibraryCalls) {
2820b57cec5SDimitry Andric       setLibcallName(LC.Op, LC.Name);
2830b57cec5SDimitry Andric     }
2840b57cec5SDimitry Andric   } else if (STI.hasHWMultF5()) {
2850b57cec5SDimitry Andric     const struct {
2860b57cec5SDimitry Andric       const RTLIB::Libcall Op;
2870b57cec5SDimitry Andric       const char * const Name;
2880b57cec5SDimitry Andric     } LibraryCalls[] = {
2890b57cec5SDimitry Andric       // Integer Multiply - EABI Table 9
2900b57cec5SDimitry Andric       { RTLIB::MUL_I16,   "__mspabi_mpyi_f5hw" },
2910b57cec5SDimitry Andric       { RTLIB::MUL_I32,   "__mspabi_mpyl_f5hw" },
2920b57cec5SDimitry Andric       { RTLIB::MUL_I64,   "__mspabi_mpyll_f5hw" },
2930b57cec5SDimitry Andric       // TODO The __mspabi_mpysl*_f5hw functions ARE implemented in libgcc
2940b57cec5SDimitry Andric       // TODO The __mspabi_mpyul*_f5hw functions ARE implemented in libgcc
2950b57cec5SDimitry Andric     };
2960b57cec5SDimitry Andric     for (const auto &LC : LibraryCalls) {
2970b57cec5SDimitry Andric       setLibcallName(LC.Op, LC.Name);
2980b57cec5SDimitry Andric     }
2990b57cec5SDimitry Andric   } else { // NoHWMult
3000b57cec5SDimitry Andric     const struct {
3010b57cec5SDimitry Andric       const RTLIB::Libcall Op;
3020b57cec5SDimitry Andric       const char * const Name;
3030b57cec5SDimitry Andric     } LibraryCalls[] = {
3040b57cec5SDimitry Andric       // Integer Multiply - EABI Table 9
3050b57cec5SDimitry Andric       { RTLIB::MUL_I16,   "__mspabi_mpyi" },
3060b57cec5SDimitry Andric       { RTLIB::MUL_I32,   "__mspabi_mpyl" },
3070b57cec5SDimitry Andric       { RTLIB::MUL_I64,   "__mspabi_mpyll" },
3080b57cec5SDimitry Andric       // The __mspabi_mpysl* functions are NOT implemented in libgcc
3090b57cec5SDimitry Andric       // The __mspabi_mpyul* functions are NOT implemented in libgcc
3100b57cec5SDimitry Andric     };
3110b57cec5SDimitry Andric     for (const auto &LC : LibraryCalls) {
3120b57cec5SDimitry Andric       setLibcallName(LC.Op, LC.Name);
3130b57cec5SDimitry Andric     }
3140b57cec5SDimitry Andric     setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::MSP430_BUILTIN);
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   // Several of the runtime library functions use a special calling conv
3180b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::MSP430_BUILTIN);
3190b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::MSP430_BUILTIN);
3200b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::MSP430_BUILTIN);
3210b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::MSP430_BUILTIN);
3220b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::MSP430_BUILTIN);
3230b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::MSP430_BUILTIN);
3240b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::MSP430_BUILTIN);
3250b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::MSP430_BUILTIN);
3260b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::MSP430_BUILTIN);
3270b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::MSP430_BUILTIN);
3280b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::MSP430_BUILTIN);
3290b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::MSP430_BUILTIN);
3300b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::MSP430_BUILTIN);
3310b57cec5SDimitry Andric   setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::MSP430_BUILTIN);
3320b57cec5SDimitry Andric   // TODO: __mspabi_srall, __mspabi_srlll, __mspabi_sllll
3330b57cec5SDimitry Andric 
3348bcb0991SDimitry Andric   setMinFunctionAlignment(Align(2));
3358bcb0991SDimitry Andric   setPrefFunctionAlignment(Align(2));
3361db9f3b2SDimitry Andric   setMaxAtomicSizeInBitsSupported(0);
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
3400b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
3410b57cec5SDimitry Andric   switch (Op.getOpcode()) {
3420b57cec5SDimitry Andric   case ISD::SHL: // FALLTHROUGH
3430b57cec5SDimitry Andric   case ISD::SRL:
3440b57cec5SDimitry Andric   case ISD::SRA:              return LowerShifts(Op, DAG);
3450b57cec5SDimitry Andric   case ISD::GlobalAddress:    return LowerGlobalAddress(Op, DAG);
3460b57cec5SDimitry Andric   case ISD::BlockAddress:     return LowerBlockAddress(Op, DAG);
3470b57cec5SDimitry Andric   case ISD::ExternalSymbol:   return LowerExternalSymbol(Op, DAG);
3480b57cec5SDimitry Andric   case ISD::SETCC:            return LowerSETCC(Op, DAG);
3490b57cec5SDimitry Andric   case ISD::BR_CC:            return LowerBR_CC(Op, DAG);
3500b57cec5SDimitry Andric   case ISD::SELECT_CC:        return LowerSELECT_CC(Op, DAG);
3510b57cec5SDimitry Andric   case ISD::SIGN_EXTEND:      return LowerSIGN_EXTEND(Op, DAG);
3520b57cec5SDimitry Andric   case ISD::RETURNADDR:       return LowerRETURNADDR(Op, DAG);
3530b57cec5SDimitry Andric   case ISD::FRAMEADDR:        return LowerFRAMEADDR(Op, DAG);
3540b57cec5SDimitry Andric   case ISD::VASTART:          return LowerVASTART(Op, DAG);
3550b57cec5SDimitry Andric   case ISD::JumpTable:        return LowerJumpTable(Op, DAG);
3560b57cec5SDimitry Andric   default:
3570b57cec5SDimitry Andric     llvm_unreachable("unimplemented operand");
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric 
361480093f4SDimitry Andric // Define non profitable transforms into shifts
362480093f4SDimitry Andric bool MSP430TargetLowering::shouldAvoidTransformToShift(EVT VT,
363480093f4SDimitry Andric                                                        unsigned Amount) const {
364480093f4SDimitry Andric   return !(Amount == 8 || Amount == 9 || Amount<=2);
3658bcb0991SDimitry Andric }
366480093f4SDimitry Andric 
367480093f4SDimitry Andric // Implemented to verify test case assertions in
368480093f4SDimitry Andric // tests/codegen/msp430/shift-amount-threshold-b.ll
369480093f4SDimitry Andric bool MSP430TargetLowering::isLegalICmpImmediate(int64_t Immed) const {
370480093f4SDimitry Andric   if (MSP430NoLegalImmediate)
371480093f4SDimitry Andric     return Immed >= -32 && Immed < 32;
372480093f4SDimitry Andric   return TargetLowering::isLegalICmpImmediate(Immed);
373480093f4SDimitry Andric }
374480093f4SDimitry Andric 
3750b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3760b57cec5SDimitry Andric //                       MSP430 Inline Assembly Support
3770b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric /// getConstraintType - Given a constraint letter, return the type of
3800b57cec5SDimitry Andric /// constraint it is for this target.
3810b57cec5SDimitry Andric TargetLowering::ConstraintType
3820b57cec5SDimitry Andric MSP430TargetLowering::getConstraintType(StringRef Constraint) const {
3830b57cec5SDimitry Andric   if (Constraint.size() == 1) {
3840b57cec5SDimitry Andric     switch (Constraint[0]) {
3850b57cec5SDimitry Andric     case 'r':
3860b57cec5SDimitry Andric       return C_RegisterClass;
3870b57cec5SDimitry Andric     default:
3880b57cec5SDimitry Andric       break;
3890b57cec5SDimitry Andric     }
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric   return TargetLowering::getConstraintType(Constraint);
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric std::pair<unsigned, const TargetRegisterClass *>
3950b57cec5SDimitry Andric MSP430TargetLowering::getRegForInlineAsmConstraint(
3960b57cec5SDimitry Andric     const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
3970b57cec5SDimitry Andric   if (Constraint.size() == 1) {
3980b57cec5SDimitry Andric     // GCC Constraint Letters
3990b57cec5SDimitry Andric     switch (Constraint[0]) {
4000b57cec5SDimitry Andric     default: break;
4010b57cec5SDimitry Andric     case 'r':   // GENERAL_REGS
4020b57cec5SDimitry Andric       if (VT == MVT::i8)
4030b57cec5SDimitry Andric         return std::make_pair(0U, &MSP430::GR8RegClass);
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric       return std::make_pair(0U, &MSP430::GR16RegClass);
4060b57cec5SDimitry Andric     }
4070b57cec5SDimitry Andric   }
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4130b57cec5SDimitry Andric //                      Calling Convention Implementation
4140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric #include "MSP430GenCallingConv.inc"
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric /// For each argument in a function store the number of pieces it is composed
4190b57cec5SDimitry Andric /// of.
4200b57cec5SDimitry Andric template<typename ArgT>
4210b57cec5SDimitry Andric static void ParseFunctionArgs(const SmallVectorImpl<ArgT> &Args,
4220b57cec5SDimitry Andric                               SmallVectorImpl<unsigned> &Out) {
4230b57cec5SDimitry Andric   unsigned CurrentArgIndex;
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   if (Args.empty())
4260b57cec5SDimitry Andric     return;
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   CurrentArgIndex = Args[0].OrigArgIndex;
4290b57cec5SDimitry Andric   Out.push_back(0);
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   for (auto &Arg : Args) {
4320b57cec5SDimitry Andric     if (CurrentArgIndex == Arg.OrigArgIndex) {
4330b57cec5SDimitry Andric       Out.back() += 1;
4340b57cec5SDimitry Andric     } else {
4350b57cec5SDimitry Andric       Out.push_back(1);
4360b57cec5SDimitry Andric       CurrentArgIndex = Arg.OrigArgIndex;
4370b57cec5SDimitry Andric     }
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric static void AnalyzeVarArgs(CCState &State,
4420b57cec5SDimitry Andric                            const SmallVectorImpl<ISD::OutputArg> &Outs) {
4430b57cec5SDimitry Andric   State.AnalyzeCallOperands(Outs, CC_MSP430_AssignStack);
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric static void AnalyzeVarArgs(CCState &State,
4470b57cec5SDimitry Andric                            const SmallVectorImpl<ISD::InputArg> &Ins) {
4480b57cec5SDimitry Andric   State.AnalyzeFormalArguments(Ins, CC_MSP430_AssignStack);
4490b57cec5SDimitry Andric }
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric /// Analyze incoming and outgoing function arguments. We need custom C++ code
4520b57cec5SDimitry Andric /// to handle special constraints in the ABI like reversing the order of the
4530b57cec5SDimitry Andric /// pieces of splitted arguments. In addition, all pieces of a certain argument
4540b57cec5SDimitry Andric /// have to be passed either using registers or the stack but never mixing both.
4550b57cec5SDimitry Andric template<typename ArgT>
4560b57cec5SDimitry Andric static void AnalyzeArguments(CCState &State,
4570b57cec5SDimitry Andric                              SmallVectorImpl<CCValAssign> &ArgLocs,
4580b57cec5SDimitry Andric                              const SmallVectorImpl<ArgT> &Args) {
4590b57cec5SDimitry Andric   static const MCPhysReg CRegList[] = {
4600b57cec5SDimitry Andric     MSP430::R12, MSP430::R13, MSP430::R14, MSP430::R15
4610b57cec5SDimitry Andric   };
462bdd1243dSDimitry Andric   static const unsigned CNbRegs = std::size(CRegList);
4630b57cec5SDimitry Andric   static const MCPhysReg BuiltinRegList[] = {
4640b57cec5SDimitry Andric     MSP430::R8, MSP430::R9, MSP430::R10, MSP430::R11,
4650b57cec5SDimitry Andric     MSP430::R12, MSP430::R13, MSP430::R14, MSP430::R15
4660b57cec5SDimitry Andric   };
467bdd1243dSDimitry Andric   static const unsigned BuiltinNbRegs = std::size(BuiltinRegList);
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric   ArrayRef<MCPhysReg> RegList;
4700b57cec5SDimitry Andric   unsigned NbRegs;
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   bool Builtin = (State.getCallingConv() == CallingConv::MSP430_BUILTIN);
4730b57cec5SDimitry Andric   if (Builtin) {
4740b57cec5SDimitry Andric     RegList = BuiltinRegList;
4750b57cec5SDimitry Andric     NbRegs = BuiltinNbRegs;
4760b57cec5SDimitry Andric   } else {
4770b57cec5SDimitry Andric     RegList = CRegList;
4780b57cec5SDimitry Andric     NbRegs = CNbRegs;
4790b57cec5SDimitry Andric   }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric   if (State.isVarArg()) {
4820b57cec5SDimitry Andric     AnalyzeVarArgs(State, Args);
4830b57cec5SDimitry Andric     return;
4840b57cec5SDimitry Andric   }
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   SmallVector<unsigned, 4> ArgsParts;
4870b57cec5SDimitry Andric   ParseFunctionArgs(Args, ArgsParts);
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   if (Builtin) {
4900b57cec5SDimitry Andric     assert(ArgsParts.size() == 2 &&
4910b57cec5SDimitry Andric         "Builtin calling convention requires two arguments");
4920b57cec5SDimitry Andric   }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric   unsigned RegsLeft = NbRegs;
4950b57cec5SDimitry Andric   bool UsedStack = false;
4960b57cec5SDimitry Andric   unsigned ValNo = 0;
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   for (unsigned i = 0, e = ArgsParts.size(); i != e; i++) {
4990b57cec5SDimitry Andric     MVT ArgVT = Args[ValNo].VT;
5000b57cec5SDimitry Andric     ISD::ArgFlagsTy ArgFlags = Args[ValNo].Flags;
5010b57cec5SDimitry Andric     MVT LocVT = ArgVT;
5020b57cec5SDimitry Andric     CCValAssign::LocInfo LocInfo = CCValAssign::Full;
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric     // Promote i8 to i16
5050b57cec5SDimitry Andric     if (LocVT == MVT::i8) {
5060b57cec5SDimitry Andric       LocVT = MVT::i16;
5070b57cec5SDimitry Andric       if (ArgFlags.isSExt())
5080b57cec5SDimitry Andric           LocInfo = CCValAssign::SExt;
5090b57cec5SDimitry Andric       else if (ArgFlags.isZExt())
5100b57cec5SDimitry Andric           LocInfo = CCValAssign::ZExt;
5110b57cec5SDimitry Andric       else
5120b57cec5SDimitry Andric           LocInfo = CCValAssign::AExt;
5130b57cec5SDimitry Andric     }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric     // Handle byval arguments
5160b57cec5SDimitry Andric     if (ArgFlags.isByVal()) {
5175ffd83dbSDimitry Andric       State.HandleByVal(ValNo++, ArgVT, LocVT, LocInfo, 2, Align(2), ArgFlags);
5180b57cec5SDimitry Andric       continue;
5190b57cec5SDimitry Andric     }
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric     unsigned Parts = ArgsParts[i];
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric     if (Builtin) {
5240b57cec5SDimitry Andric       assert(Parts == 4 &&
5250b57cec5SDimitry Andric           "Builtin calling convention requires 64-bit arguments");
5260b57cec5SDimitry Andric     }
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric     if (!UsedStack && Parts == 2 && RegsLeft == 1) {
5290b57cec5SDimitry Andric       // Special case for 32-bit register split, see EABI section 3.3.3
5300b57cec5SDimitry Andric       unsigned Reg = State.AllocateReg(RegList);
5310b57cec5SDimitry Andric       State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo));
5320b57cec5SDimitry Andric       RegsLeft -= 1;
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric       UsedStack = true;
5350b57cec5SDimitry Andric       CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State);
5360b57cec5SDimitry Andric     } else if (Parts <= RegsLeft) {
5370b57cec5SDimitry Andric       for (unsigned j = 0; j < Parts; j++) {
5380b57cec5SDimitry Andric         unsigned Reg = State.AllocateReg(RegList);
5390b57cec5SDimitry Andric         State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo));
5400b57cec5SDimitry Andric         RegsLeft--;
5410b57cec5SDimitry Andric       }
5420b57cec5SDimitry Andric     } else {
5430b57cec5SDimitry Andric       UsedStack = true;
5440b57cec5SDimitry Andric       for (unsigned j = 0; j < Parts; j++)
5450b57cec5SDimitry Andric         CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State);
5460b57cec5SDimitry Andric     }
5470b57cec5SDimitry Andric   }
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric static void AnalyzeRetResult(CCState &State,
5510b57cec5SDimitry Andric                              const SmallVectorImpl<ISD::InputArg> &Ins) {
5520b57cec5SDimitry Andric   State.AnalyzeCallResult(Ins, RetCC_MSP430);
5530b57cec5SDimitry Andric }
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric static void AnalyzeRetResult(CCState &State,
5560b57cec5SDimitry Andric                              const SmallVectorImpl<ISD::OutputArg> &Outs) {
5570b57cec5SDimitry Andric   State.AnalyzeReturn(Outs, RetCC_MSP430);
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric template<typename ArgT>
5610b57cec5SDimitry Andric static void AnalyzeReturnValues(CCState &State,
5620b57cec5SDimitry Andric                                 SmallVectorImpl<CCValAssign> &RVLocs,
5630b57cec5SDimitry Andric                                 const SmallVectorImpl<ArgT> &Args) {
5640b57cec5SDimitry Andric   AnalyzeRetResult(State, Args);
5650b57cec5SDimitry Andric }
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerFormalArguments(
5680b57cec5SDimitry Andric     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
5690b57cec5SDimitry Andric     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
5700b57cec5SDimitry Andric     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   switch (CallConv) {
5730b57cec5SDimitry Andric   default:
5740b57cec5SDimitry Andric     report_fatal_error("Unsupported calling convention");
5750b57cec5SDimitry Andric   case CallingConv::C:
5760b57cec5SDimitry Andric   case CallingConv::Fast:
5770b57cec5SDimitry Andric     return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
5780b57cec5SDimitry Andric   case CallingConv::MSP430_INTR:
5790b57cec5SDimitry Andric     if (Ins.empty())
5800b57cec5SDimitry Andric       return Chain;
5810b57cec5SDimitry Andric     report_fatal_error("ISRs cannot have arguments");
5820b57cec5SDimitry Andric   }
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric SDValue
5860b57cec5SDimitry Andric MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
5870b57cec5SDimitry Andric                                 SmallVectorImpl<SDValue> &InVals) const {
5880b57cec5SDimitry Andric   SelectionDAG &DAG                     = CLI.DAG;
5890b57cec5SDimitry Andric   SDLoc &dl                             = CLI.DL;
5900b57cec5SDimitry Andric   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
5910b57cec5SDimitry Andric   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
5920b57cec5SDimitry Andric   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
5930b57cec5SDimitry Andric   SDValue Chain                         = CLI.Chain;
5940b57cec5SDimitry Andric   SDValue Callee                        = CLI.Callee;
5950b57cec5SDimitry Andric   bool &isTailCall                      = CLI.IsTailCall;
5960b57cec5SDimitry Andric   CallingConv::ID CallConv              = CLI.CallConv;
5970b57cec5SDimitry Andric   bool isVarArg                         = CLI.IsVarArg;
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric   // MSP430 target does not yet support tail call optimization.
6000b57cec5SDimitry Andric   isTailCall = false;
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric   switch (CallConv) {
6030b57cec5SDimitry Andric   default:
6040b57cec5SDimitry Andric     report_fatal_error("Unsupported calling convention");
6050b57cec5SDimitry Andric   case CallingConv::MSP430_BUILTIN:
6060b57cec5SDimitry Andric   case CallingConv::Fast:
6070b57cec5SDimitry Andric   case CallingConv::C:
6080b57cec5SDimitry Andric     return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
6090b57cec5SDimitry Andric                           Outs, OutVals, Ins, dl, DAG, InVals);
6100b57cec5SDimitry Andric   case CallingConv::MSP430_INTR:
6110b57cec5SDimitry Andric     report_fatal_error("ISRs cannot be called directly");
6120b57cec5SDimitry Andric   }
6130b57cec5SDimitry Andric }
6140b57cec5SDimitry Andric 
6150b57cec5SDimitry Andric /// LowerCCCArguments - transform physical registers into virtual registers and
6160b57cec5SDimitry Andric /// generate load operations for arguments places on the stack.
6170b57cec5SDimitry Andric // FIXME: struct return stuff
6180b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerCCCArguments(
6190b57cec5SDimitry Andric     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
6200b57cec5SDimitry Andric     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
6210b57cec5SDimitry Andric     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
6220b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
6230b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
6240b57cec5SDimitry Andric   MachineRegisterInfo &RegInfo = MF.getRegInfo();
6250b57cec5SDimitry Andric   MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   // Assign locations to all of the incoming arguments.
6280b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> ArgLocs;
6290b57cec5SDimitry Andric   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
6300b57cec5SDimitry Andric                  *DAG.getContext());
6310b57cec5SDimitry Andric   AnalyzeArguments(CCInfo, ArgLocs, Ins);
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric   // Create frame index for the start of the first vararg value
6340b57cec5SDimitry Andric   if (isVarArg) {
63506c3fb27SDimitry Andric     unsigned Offset = CCInfo.getStackSize();
6360b57cec5SDimitry Andric     FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, Offset, true));
6370b57cec5SDimitry Andric   }
6380b57cec5SDimitry Andric 
6390b57cec5SDimitry Andric   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
6400b57cec5SDimitry Andric     CCValAssign &VA = ArgLocs[i];
6410b57cec5SDimitry Andric     if (VA.isRegLoc()) {
6420b57cec5SDimitry Andric       // Arguments passed in registers
6430b57cec5SDimitry Andric       EVT RegVT = VA.getLocVT();
6440b57cec5SDimitry Andric       switch (RegVT.getSimpleVT().SimpleTy) {
6450b57cec5SDimitry Andric       default:
6460b57cec5SDimitry Andric         {
6470b57cec5SDimitry Andric #ifndef NDEBUG
6480b57cec5SDimitry Andric           errs() << "LowerFormalArguments Unhandled argument type: "
64906c3fb27SDimitry Andric                  << RegVT << "\n";
6500b57cec5SDimitry Andric #endif
6510b57cec5SDimitry Andric           llvm_unreachable(nullptr);
6520b57cec5SDimitry Andric         }
6530b57cec5SDimitry Andric       case MVT::i16:
6548bcb0991SDimitry Andric         Register VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
6550b57cec5SDimitry Andric         RegInfo.addLiveIn(VA.getLocReg(), VReg);
6560b57cec5SDimitry Andric         SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric         // If this is an 8-bit value, it is really passed promoted to 16
6590b57cec5SDimitry Andric         // bits. Insert an assert[sz]ext to capture this, then truncate to the
6600b57cec5SDimitry Andric         // right size.
6610b57cec5SDimitry Andric         if (VA.getLocInfo() == CCValAssign::SExt)
6620b57cec5SDimitry Andric           ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
6630b57cec5SDimitry Andric                                  DAG.getValueType(VA.getValVT()));
6640b57cec5SDimitry Andric         else if (VA.getLocInfo() == CCValAssign::ZExt)
6650b57cec5SDimitry Andric           ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
6660b57cec5SDimitry Andric                                  DAG.getValueType(VA.getValVT()));
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric         if (VA.getLocInfo() != CCValAssign::Full)
6690b57cec5SDimitry Andric           ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric         InVals.push_back(ArgValue);
6720b57cec5SDimitry Andric       }
6730b57cec5SDimitry Andric     } else {
674349cc55cSDimitry Andric       // Only arguments passed on the stack should make it here.
6750b57cec5SDimitry Andric       assert(VA.isMemLoc());
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric       SDValue InVal;
6780b57cec5SDimitry Andric       ISD::ArgFlagsTy Flags = Ins[i].Flags;
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric       if (Flags.isByVal()) {
68181ad6265SDimitry Andric         MVT PtrVT = VA.getLocVT();
6820b57cec5SDimitry Andric         int FI = MFI.CreateFixedObject(Flags.getByValSize(),
6830b57cec5SDimitry Andric                                        VA.getLocMemOffset(), true);
68481ad6265SDimitry Andric         InVal = DAG.getFrameIndex(FI, PtrVT);
6850b57cec5SDimitry Andric       } else {
6860b57cec5SDimitry Andric         // Load the argument to a virtual register
6870b57cec5SDimitry Andric         unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
6880b57cec5SDimitry Andric         if (ObjSize > 2) {
6890b57cec5SDimitry Andric             errs() << "LowerFormalArguments Unhandled argument type: "
69006c3fb27SDimitry Andric                 << VA.getLocVT() << "\n";
6910b57cec5SDimitry Andric         }
6920b57cec5SDimitry Andric         // Create the frame index object for this incoming parameter...
6930b57cec5SDimitry Andric         int FI = MFI.CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric         // Create the SelectionDAG nodes corresponding to a load
6960b57cec5SDimitry Andric         //from this parameter
6970b57cec5SDimitry Andric         SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
6980b57cec5SDimitry Andric         InVal = DAG.getLoad(
6990b57cec5SDimitry Andric             VA.getLocVT(), dl, Chain, FIN,
7000b57cec5SDimitry Andric             MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
7010b57cec5SDimitry Andric       }
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric       InVals.push_back(InVal);
7040b57cec5SDimitry Andric     }
7050b57cec5SDimitry Andric   }
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
7080b57cec5SDimitry Andric     if (Ins[i].Flags.isSRet()) {
70904eeddc0SDimitry Andric       Register Reg = FuncInfo->getSRetReturnReg();
7100b57cec5SDimitry Andric       if (!Reg) {
7110b57cec5SDimitry Andric         Reg = MF.getRegInfo().createVirtualRegister(
7120b57cec5SDimitry Andric             getRegClassFor(MVT::i16));
7130b57cec5SDimitry Andric         FuncInfo->setSRetReturnReg(Reg);
7140b57cec5SDimitry Andric       }
7150b57cec5SDimitry Andric       SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[i]);
7160b57cec5SDimitry Andric       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
7170b57cec5SDimitry Andric     }
7180b57cec5SDimitry Andric   }
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   return Chain;
7210b57cec5SDimitry Andric }
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric bool
7240b57cec5SDimitry Andric MSP430TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
7250b57cec5SDimitry Andric                                      MachineFunction &MF,
7260b57cec5SDimitry Andric                                      bool IsVarArg,
7270b57cec5SDimitry Andric                                      const SmallVectorImpl<ISD::OutputArg> &Outs,
7280b57cec5SDimitry Andric                                      LLVMContext &Context) const {
7290b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RVLocs;
7300b57cec5SDimitry Andric   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
7310b57cec5SDimitry Andric   return CCInfo.CheckReturn(Outs, RetCC_MSP430);
7320b57cec5SDimitry Andric }
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric SDValue
7350b57cec5SDimitry Andric MSP430TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
7360b57cec5SDimitry Andric                                   bool isVarArg,
7370b57cec5SDimitry Andric                                   const SmallVectorImpl<ISD::OutputArg> &Outs,
7380b57cec5SDimitry Andric                                   const SmallVectorImpl<SDValue> &OutVals,
7390b57cec5SDimitry Andric                                   const SDLoc &dl, SelectionDAG &DAG) const {
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric   // CCValAssign - represent the assignment of the return value to a location
7440b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RVLocs;
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric   // ISRs cannot return any value.
7470b57cec5SDimitry Andric   if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
7480b57cec5SDimitry Andric     report_fatal_error("ISRs cannot return any value");
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric   // CCState - Info about the registers and stack slot.
7510b57cec5SDimitry Andric   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
7520b57cec5SDimitry Andric                  *DAG.getContext());
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric   // Analize return values.
7550b57cec5SDimitry Andric   AnalyzeReturnValues(CCInfo, RVLocs, Outs);
7560b57cec5SDimitry Andric 
75706c3fb27SDimitry Andric   SDValue Glue;
7580b57cec5SDimitry Andric   SmallVector<SDValue, 4> RetOps(1, Chain);
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric   // Copy the result values into the output registers.
7610b57cec5SDimitry Andric   for (unsigned i = 0; i != RVLocs.size(); ++i) {
7620b57cec5SDimitry Andric     CCValAssign &VA = RVLocs[i];
7630b57cec5SDimitry Andric     assert(VA.isRegLoc() && "Can only return in registers!");
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
76606c3fb27SDimitry Andric                              OutVals[i], Glue);
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric     // Guarantee that all emitted copies are stuck together,
7690b57cec5SDimitry Andric     // avoiding something bad.
77006c3fb27SDimitry Andric     Glue = Chain.getValue(1);
7710b57cec5SDimitry Andric     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
7720b57cec5SDimitry Andric   }
7730b57cec5SDimitry Andric 
7740b57cec5SDimitry Andric   if (MF.getFunction().hasStructRetAttr()) {
7750b57cec5SDimitry Andric     MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
77604eeddc0SDimitry Andric     Register Reg = FuncInfo->getSRetReturnReg();
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric     if (!Reg)
7790b57cec5SDimitry Andric       llvm_unreachable("sret virtual register not created in entry block");
7800b57cec5SDimitry Andric 
78181ad6265SDimitry Andric     MVT PtrVT = getFrameIndexTy(DAG.getDataLayout());
7820b57cec5SDimitry Andric     SDValue Val =
78381ad6265SDimitry Andric       DAG.getCopyFromReg(Chain, dl, Reg, PtrVT);
7840b57cec5SDimitry Andric     unsigned R12 = MSP430::R12;
7850b57cec5SDimitry Andric 
78606c3fb27SDimitry Andric     Chain = DAG.getCopyToReg(Chain, dl, R12, Val, Glue);
78706c3fb27SDimitry Andric     Glue = Chain.getValue(1);
78881ad6265SDimitry Andric     RetOps.push_back(DAG.getRegister(R12, PtrVT));
7890b57cec5SDimitry Andric   }
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric   unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
79206c3fb27SDimitry Andric                   MSP430ISD::RETI_GLUE : MSP430ISD::RET_GLUE);
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric   RetOps[0] = Chain;  // Update chain.
7950b57cec5SDimitry Andric 
79606c3fb27SDimitry Andric   // Add the glue if we have it.
79706c3fb27SDimitry Andric   if (Glue.getNode())
79806c3fb27SDimitry Andric     RetOps.push_back(Glue);
7990b57cec5SDimitry Andric 
8000b57cec5SDimitry Andric   return DAG.getNode(Opc, dl, MVT::Other, RetOps);
8010b57cec5SDimitry Andric }
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric /// LowerCCCCallTo - functions arguments are copied from virtual regs to
8040b57cec5SDimitry Andric /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
8050b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerCCCCallTo(
8060b57cec5SDimitry Andric     SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
8070b57cec5SDimitry Andric     bool isTailCall, const SmallVectorImpl<ISD::OutputArg> &Outs,
8080b57cec5SDimitry Andric     const SmallVectorImpl<SDValue> &OutVals,
8090b57cec5SDimitry Andric     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
8100b57cec5SDimitry Andric     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
8110b57cec5SDimitry Andric   // Analyze operands of the call, assigning locations to each operand.
8120b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> ArgLocs;
8130b57cec5SDimitry Andric   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
8140b57cec5SDimitry Andric                  *DAG.getContext());
8150b57cec5SDimitry Andric   AnalyzeArguments(CCInfo, ArgLocs, Outs);
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric   // Get a count of how many bytes are to be pushed on the stack.
81806c3fb27SDimitry Andric   unsigned NumBytes = CCInfo.getStackSize();
81981ad6265SDimitry Andric   MVT PtrVT = getFrameIndexTy(DAG.getDataLayout());
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric   Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
8220b57cec5SDimitry Andric 
8230b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
8240b57cec5SDimitry Andric   SmallVector<SDValue, 12> MemOpChains;
8250b57cec5SDimitry Andric   SDValue StackPtr;
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric   // Walk the register/memloc assignments, inserting copies/loads.
8280b57cec5SDimitry Andric   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
8290b57cec5SDimitry Andric     CCValAssign &VA = ArgLocs[i];
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric     SDValue Arg = OutVals[i];
8320b57cec5SDimitry Andric 
8330b57cec5SDimitry Andric     // Promote the value if needed.
8340b57cec5SDimitry Andric     switch (VA.getLocInfo()) {
8350b57cec5SDimitry Andric       default: llvm_unreachable("Unknown loc info!");
8360b57cec5SDimitry Andric       case CCValAssign::Full: break;
8370b57cec5SDimitry Andric       case CCValAssign::SExt:
8380b57cec5SDimitry Andric         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
8390b57cec5SDimitry Andric         break;
8400b57cec5SDimitry Andric       case CCValAssign::ZExt:
8410b57cec5SDimitry Andric         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
8420b57cec5SDimitry Andric         break;
8430b57cec5SDimitry Andric       case CCValAssign::AExt:
8440b57cec5SDimitry Andric         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
8450b57cec5SDimitry Andric         break;
8460b57cec5SDimitry Andric     }
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric     // Arguments that can be passed on register must be kept at RegsToPass
8490b57cec5SDimitry Andric     // vector
8500b57cec5SDimitry Andric     if (VA.isRegLoc()) {
8510b57cec5SDimitry Andric       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
8520b57cec5SDimitry Andric     } else {
8530b57cec5SDimitry Andric       assert(VA.isMemLoc());
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric       if (!StackPtr.getNode())
8560b57cec5SDimitry Andric         StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SP, PtrVT);
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric       SDValue PtrOff =
8590b57cec5SDimitry Andric           DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
8600b57cec5SDimitry Andric                       DAG.getIntPtrConstant(VA.getLocMemOffset(), dl));
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric       SDValue MemOp;
8630b57cec5SDimitry Andric       ISD::ArgFlagsTy Flags = Outs[i].Flags;
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric       if (Flags.isByVal()) {
8660b57cec5SDimitry Andric         SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i16);
867*0fca6ea1SDimitry Andric         MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
868*0fca6ea1SDimitry Andric                               Flags.getNonZeroByValAlign(),
8690b57cec5SDimitry Andric                               /*isVolatile*/ false,
8700b57cec5SDimitry Andric                               /*AlwaysInline=*/true,
871*0fca6ea1SDimitry Andric                               /*CI=*/nullptr, std::nullopt,
872*0fca6ea1SDimitry Andric                               MachinePointerInfo(), MachinePointerInfo());
8730b57cec5SDimitry Andric       } else {
8740b57cec5SDimitry Andric         MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
8750b57cec5SDimitry Andric       }
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric       MemOpChains.push_back(MemOp);
8780b57cec5SDimitry Andric     }
8790b57cec5SDimitry Andric   }
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   // Transform all store nodes into one single node because all store nodes are
8820b57cec5SDimitry Andric   // independent of each other.
8830b57cec5SDimitry Andric   if (!MemOpChains.empty())
8840b57cec5SDimitry Andric     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric   // Build a sequence of copy-to-reg nodes chained together with token chain and
88706c3fb27SDimitry Andric   // flag operands which copy the outgoing args into registers.  The InGlue in
8880b57cec5SDimitry Andric   // necessary since all emitted instructions must be stuck together.
88906c3fb27SDimitry Andric   SDValue InGlue;
8900b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
8910b57cec5SDimitry Andric     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
89206c3fb27SDimitry Andric                              RegsToPass[i].second, InGlue);
89306c3fb27SDimitry Andric     InGlue = Chain.getValue(1);
8940b57cec5SDimitry Andric   }
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric   // If the callee is a GlobalAddress node (quite common, every direct call is)
8970b57cec5SDimitry Andric   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
8980b57cec5SDimitry Andric   // Likewise ExternalSymbol -> TargetExternalSymbol.
8990b57cec5SDimitry Andric   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
9000b57cec5SDimitry Andric     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
9010b57cec5SDimitry Andric   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
9020b57cec5SDimitry Andric     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric   // Returns a chain & a flag for retval copy to use.
9050b57cec5SDimitry Andric   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
9060b57cec5SDimitry Andric   SmallVector<SDValue, 8> Ops;
9070b57cec5SDimitry Andric   Ops.push_back(Chain);
9080b57cec5SDimitry Andric   Ops.push_back(Callee);
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric   // Add argument registers to the end of the list so that they are
9110b57cec5SDimitry Andric   // known live into the call.
9120b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
9130b57cec5SDimitry Andric     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
9140b57cec5SDimitry Andric                                   RegsToPass[i].second.getValueType()));
9150b57cec5SDimitry Andric 
91606c3fb27SDimitry Andric   if (InGlue.getNode())
91706c3fb27SDimitry Andric     Ops.push_back(InGlue);
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric   Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, Ops);
92006c3fb27SDimitry Andric   InGlue = Chain.getValue(1);
9210b57cec5SDimitry Andric 
9220b57cec5SDimitry Andric   // Create the CALLSEQ_END node.
92306c3fb27SDimitry Andric   Chain = DAG.getCALLSEQ_END(Chain, NumBytes, 0, InGlue, dl);
92406c3fb27SDimitry Andric   InGlue = Chain.getValue(1);
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric   // Handle result values, copying them out of physregs into vregs that we
9270b57cec5SDimitry Andric   // return.
92806c3fb27SDimitry Andric   return LowerCallResult(Chain, InGlue, CallConv, isVarArg, Ins, dl,
9290b57cec5SDimitry Andric                          DAG, InVals);
9300b57cec5SDimitry Andric }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric /// LowerCallResult - Lower the result values of a call into the
9330b57cec5SDimitry Andric /// appropriate copies out of appropriate physical registers.
9340b57cec5SDimitry Andric ///
9350b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerCallResult(
93606c3fb27SDimitry Andric     SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool isVarArg,
9370b57cec5SDimitry Andric     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
9380b57cec5SDimitry Andric     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   // Assign locations to each value returned by this call.
9410b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RVLocs;
9420b57cec5SDimitry Andric   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
9430b57cec5SDimitry Andric                  *DAG.getContext());
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric   AnalyzeReturnValues(CCInfo, RVLocs, Ins);
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   // Copy all of the result registers out of their specified physreg.
9480b57cec5SDimitry Andric   for (unsigned i = 0; i != RVLocs.size(); ++i) {
9490b57cec5SDimitry Andric     Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
95006c3fb27SDimitry Andric                                RVLocs[i].getValVT(), InGlue).getValue(1);
95106c3fb27SDimitry Andric     InGlue = Chain.getValue(2);
9520b57cec5SDimitry Andric     InVals.push_back(Chain.getValue(0));
9530b57cec5SDimitry Andric   }
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric   return Chain;
9560b57cec5SDimitry Andric }
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
9590b57cec5SDimitry Andric                                           SelectionDAG &DAG) const {
9600b57cec5SDimitry Andric   unsigned Opc = Op.getOpcode();
9610b57cec5SDimitry Andric   SDNode* N = Op.getNode();
9620b57cec5SDimitry Andric   EVT VT = Op.getValueType();
9630b57cec5SDimitry Andric   SDLoc dl(N);
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric   // Expand non-constant shifts to loops:
9660b57cec5SDimitry Andric   if (!isa<ConstantSDNode>(N->getOperand(1)))
9670b57cec5SDimitry Andric     return Op;
9680b57cec5SDimitry Andric 
969647cbc5dSDimitry Andric   uint64_t ShiftAmount = N->getConstantOperandVal(1);
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric   // Expand the stuff into sequence of shifts.
9720b57cec5SDimitry Andric   SDValue Victim = N->getOperand(0);
9730b57cec5SDimitry Andric 
9740b57cec5SDimitry Andric   if (ShiftAmount >= 8) {
9750b57cec5SDimitry Andric     assert(VT == MVT::i16 && "Can not shift i8 by 8 and more");
9760b57cec5SDimitry Andric     switch(Opc) {
9770b57cec5SDimitry Andric     default:
9780b57cec5SDimitry Andric       llvm_unreachable("Unknown shift");
9790b57cec5SDimitry Andric     case ISD::SHL:
9800b57cec5SDimitry Andric       // foo << (8 + N) => swpb(zext(foo)) << N
9810b57cec5SDimitry Andric       Victim = DAG.getZeroExtendInReg(Victim, dl, MVT::i8);
9820b57cec5SDimitry Andric       Victim = DAG.getNode(ISD::BSWAP, dl, VT, Victim);
9830b57cec5SDimitry Andric       break;
9840b57cec5SDimitry Andric     case ISD::SRA:
9850b57cec5SDimitry Andric     case ISD::SRL:
9860b57cec5SDimitry Andric       // foo >> (8 + N) => sxt(swpb(foo)) >> N
9870b57cec5SDimitry Andric       Victim = DAG.getNode(ISD::BSWAP, dl, VT, Victim);
9880b57cec5SDimitry Andric       Victim = (Opc == ISD::SRA)
9890b57cec5SDimitry Andric                    ? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT, Victim,
9900b57cec5SDimitry Andric                                  DAG.getValueType(MVT::i8))
9910b57cec5SDimitry Andric                    : DAG.getZeroExtendInReg(Victim, dl, MVT::i8);
9920b57cec5SDimitry Andric       break;
9930b57cec5SDimitry Andric     }
9940b57cec5SDimitry Andric     ShiftAmount -= 8;
9950b57cec5SDimitry Andric   }
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   if (Opc == ISD::SRL && ShiftAmount) {
9980b57cec5SDimitry Andric     // Emit a special goodness here:
9990b57cec5SDimitry Andric     // srl A, 1 => clrc; rrc A
10000b57cec5SDimitry Andric     Victim = DAG.getNode(MSP430ISD::RRCL, dl, VT, Victim);
10010b57cec5SDimitry Andric     ShiftAmount -= 1;
10020b57cec5SDimitry Andric   }
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric   while (ShiftAmount--)
10050b57cec5SDimitry Andric     Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
10060b57cec5SDimitry Andric                          dl, VT, Victim);
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   return Victim;
10090b57cec5SDimitry Andric }
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
10120b57cec5SDimitry Andric                                                  SelectionDAG &DAG) const {
10130b57cec5SDimitry Andric   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
10140b57cec5SDimitry Andric   int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
101581ad6265SDimitry Andric   EVT PtrVT = Op.getValueType();
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric   // Create the TargetGlobalAddress node, folding in the constant offset.
10180b57cec5SDimitry Andric   SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op), PtrVT, Offset);
10190b57cec5SDimitry Andric   return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op), PtrVT, Result);
10200b57cec5SDimitry Andric }
10210b57cec5SDimitry Andric 
10220b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
10230b57cec5SDimitry Andric                                                   SelectionDAG &DAG) const {
10240b57cec5SDimitry Andric   SDLoc dl(Op);
10250b57cec5SDimitry Andric   const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
102681ad6265SDimitry Andric   EVT PtrVT = Op.getValueType();
10270b57cec5SDimitry Andric   SDValue Result = DAG.getTargetExternalSymbol(Sym, PtrVT);
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric   return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result);
10300b57cec5SDimitry Andric }
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
10330b57cec5SDimitry Andric                                                 SelectionDAG &DAG) const {
10340b57cec5SDimitry Andric   SDLoc dl(Op);
10350b57cec5SDimitry Andric   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
103681ad6265SDimitry Andric   EVT PtrVT = Op.getValueType();
10370b57cec5SDimitry Andric   SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT);
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric   return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result);
10400b57cec5SDimitry Andric }
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
10430b57cec5SDimitry Andric                        ISD::CondCode CC, const SDLoc &dl, SelectionDAG &DAG) {
10440b57cec5SDimitry Andric   // FIXME: Handle bittests someday
10450b57cec5SDimitry Andric   assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   // FIXME: Handle jump negative someday
10480b57cec5SDimitry Andric   MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
10490b57cec5SDimitry Andric   switch (CC) {
10500b57cec5SDimitry Andric   default: llvm_unreachable("Invalid integer condition!");
10510b57cec5SDimitry Andric   case ISD::SETEQ:
10520b57cec5SDimitry Andric     TCC = MSP430CC::COND_E;     // aka COND_Z
10530b57cec5SDimitry Andric     // Minor optimization: if LHS is a constant, swap operands, then the
10540b57cec5SDimitry Andric     // constant can be folded into comparison.
10550b57cec5SDimitry Andric     if (LHS.getOpcode() == ISD::Constant)
10560b57cec5SDimitry Andric       std::swap(LHS, RHS);
10570b57cec5SDimitry Andric     break;
10580b57cec5SDimitry Andric   case ISD::SETNE:
10590b57cec5SDimitry Andric     TCC = MSP430CC::COND_NE;    // aka COND_NZ
10600b57cec5SDimitry Andric     // Minor optimization: if LHS is a constant, swap operands, then the
10610b57cec5SDimitry Andric     // constant can be folded into comparison.
10620b57cec5SDimitry Andric     if (LHS.getOpcode() == ISD::Constant)
10630b57cec5SDimitry Andric       std::swap(LHS, RHS);
10640b57cec5SDimitry Andric     break;
10650b57cec5SDimitry Andric   case ISD::SETULE:
10660b57cec5SDimitry Andric     std::swap(LHS, RHS);
1067bdd1243dSDimitry Andric     [[fallthrough]];
10680b57cec5SDimitry Andric   case ISD::SETUGE:
10690b57cec5SDimitry Andric     // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
10700b57cec5SDimitry Andric     // fold constant into instruction.
10710b57cec5SDimitry Andric     if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
10720b57cec5SDimitry Andric       LHS = RHS;
10730b57cec5SDimitry Andric       RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
10740b57cec5SDimitry Andric       TCC = MSP430CC::COND_LO;
10750b57cec5SDimitry Andric       break;
10760b57cec5SDimitry Andric     }
10770b57cec5SDimitry Andric     TCC = MSP430CC::COND_HS;    // aka COND_C
10780b57cec5SDimitry Andric     break;
10790b57cec5SDimitry Andric   case ISD::SETUGT:
10800b57cec5SDimitry Andric     std::swap(LHS, RHS);
1081bdd1243dSDimitry Andric     [[fallthrough]];
10820b57cec5SDimitry Andric   case ISD::SETULT:
10830b57cec5SDimitry Andric     // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
10840b57cec5SDimitry Andric     // fold constant into instruction.
10850b57cec5SDimitry Andric     if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
10860b57cec5SDimitry Andric       LHS = RHS;
10870b57cec5SDimitry Andric       RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
10880b57cec5SDimitry Andric       TCC = MSP430CC::COND_HS;
10890b57cec5SDimitry Andric       break;
10900b57cec5SDimitry Andric     }
10910b57cec5SDimitry Andric     TCC = MSP430CC::COND_LO;    // aka COND_NC
10920b57cec5SDimitry Andric     break;
10930b57cec5SDimitry Andric   case ISD::SETLE:
10940b57cec5SDimitry Andric     std::swap(LHS, RHS);
1095bdd1243dSDimitry Andric     [[fallthrough]];
10960b57cec5SDimitry Andric   case ISD::SETGE:
10970b57cec5SDimitry Andric     // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
10980b57cec5SDimitry Andric     // fold constant into instruction.
10990b57cec5SDimitry Andric     if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
11000b57cec5SDimitry Andric       LHS = RHS;
11010b57cec5SDimitry Andric       RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
11020b57cec5SDimitry Andric       TCC = MSP430CC::COND_L;
11030b57cec5SDimitry Andric       break;
11040b57cec5SDimitry Andric     }
11050b57cec5SDimitry Andric     TCC = MSP430CC::COND_GE;
11060b57cec5SDimitry Andric     break;
11070b57cec5SDimitry Andric   case ISD::SETGT:
11080b57cec5SDimitry Andric     std::swap(LHS, RHS);
1109bdd1243dSDimitry Andric     [[fallthrough]];
11100b57cec5SDimitry Andric   case ISD::SETLT:
11110b57cec5SDimitry Andric     // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
11120b57cec5SDimitry Andric     // fold constant into instruction.
11130b57cec5SDimitry Andric     if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
11140b57cec5SDimitry Andric       LHS = RHS;
11150b57cec5SDimitry Andric       RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
11160b57cec5SDimitry Andric       TCC = MSP430CC::COND_GE;
11170b57cec5SDimitry Andric       break;
11180b57cec5SDimitry Andric     }
11190b57cec5SDimitry Andric     TCC = MSP430CC::COND_L;
11200b57cec5SDimitry Andric     break;
11210b57cec5SDimitry Andric   }
11220b57cec5SDimitry Andric 
11230b57cec5SDimitry Andric   TargetCC = DAG.getConstant(TCC, dl, MVT::i8);
11240b57cec5SDimitry Andric   return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
11290b57cec5SDimitry Andric   SDValue Chain = Op.getOperand(0);
11300b57cec5SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
11310b57cec5SDimitry Andric   SDValue LHS   = Op.getOperand(2);
11320b57cec5SDimitry Andric   SDValue RHS   = Op.getOperand(3);
11330b57cec5SDimitry Andric   SDValue Dest  = Op.getOperand(4);
11340b57cec5SDimitry Andric   SDLoc dl  (Op);
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric   SDValue TargetCC;
11370b57cec5SDimitry Andric   SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
11380b57cec5SDimitry Andric 
11390b57cec5SDimitry Andric   return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
11400b57cec5SDimitry Andric                      Chain, Dest, TargetCC, Flag);
11410b57cec5SDimitry Andric }
11420b57cec5SDimitry Andric 
11430b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
11440b57cec5SDimitry Andric   SDValue LHS   = Op.getOperand(0);
11450b57cec5SDimitry Andric   SDValue RHS   = Op.getOperand(1);
11460b57cec5SDimitry Andric   SDLoc dl  (Op);
11470b57cec5SDimitry Andric 
11480b57cec5SDimitry Andric   // If we are doing an AND and testing against zero, then the CMP
11490b57cec5SDimitry Andric   // will not be generated.  The AND (or BIT) will generate the condition codes,
11500b57cec5SDimitry Andric   // but they are different from CMP.
11510b57cec5SDimitry Andric   // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
11520b57cec5SDimitry Andric   // lowering & isel wouldn't diverge.
1153297eecfbSDimitry Andric   bool andCC = isNullConstant(RHS) && LHS.hasOneUse() &&
11540b57cec5SDimitry Andric                (LHS.getOpcode() == ISD::AND ||
11550b57cec5SDimitry Andric                 (LHS.getOpcode() == ISD::TRUNCATE &&
1156297eecfbSDimitry Andric                  LHS.getOperand(0).getOpcode() == ISD::AND));
11570b57cec5SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
11580b57cec5SDimitry Andric   SDValue TargetCC;
11590b57cec5SDimitry Andric   SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric   // Get the condition codes directly from the status register, if its easy.
11620b57cec5SDimitry Andric   // Otherwise a branch will be generated.  Note that the AND and BIT
11630b57cec5SDimitry Andric   // instructions generate different flags than CMP, the carry bit can be used
11640b57cec5SDimitry Andric   // for NE/EQ.
11650b57cec5SDimitry Andric   bool Invert = false;
11660b57cec5SDimitry Andric   bool Shift = false;
11670b57cec5SDimitry Andric   bool Convert = true;
11681db9f3b2SDimitry Andric   switch (TargetCC->getAsZExtVal()) {
11690b57cec5SDimitry Andric   default:
11700b57cec5SDimitry Andric     Convert = false;
11710b57cec5SDimitry Andric     break;
11720b57cec5SDimitry Andric    case MSP430CC::COND_HS:
11730b57cec5SDimitry Andric      // Res = SR & 1, no processing is required
11740b57cec5SDimitry Andric      break;
11750b57cec5SDimitry Andric    case MSP430CC::COND_LO:
11760b57cec5SDimitry Andric      // Res = ~(SR & 1)
11770b57cec5SDimitry Andric      Invert = true;
11780b57cec5SDimitry Andric      break;
11790b57cec5SDimitry Andric    case MSP430CC::COND_NE:
11800b57cec5SDimitry Andric      if (andCC) {
11810b57cec5SDimitry Andric        // C = ~Z, thus Res = SR & 1, no processing is required
11820b57cec5SDimitry Andric      } else {
11830b57cec5SDimitry Andric        // Res = ~((SR >> 1) & 1)
11840b57cec5SDimitry Andric        Shift = true;
11850b57cec5SDimitry Andric        Invert = true;
11860b57cec5SDimitry Andric      }
11870b57cec5SDimitry Andric      break;
11880b57cec5SDimitry Andric    case MSP430CC::COND_E:
11890b57cec5SDimitry Andric      Shift = true;
11900b57cec5SDimitry Andric      // C = ~Z for AND instruction, thus we can put Res = ~(SR & 1), however,
11910b57cec5SDimitry Andric      // Res = (SR >> 1) & 1 is 1 word shorter.
11920b57cec5SDimitry Andric      break;
11930b57cec5SDimitry Andric    }
11940b57cec5SDimitry Andric   EVT VT = Op.getValueType();
11950b57cec5SDimitry Andric   SDValue One  = DAG.getConstant(1, dl, VT);
11960b57cec5SDimitry Andric   if (Convert) {
11970b57cec5SDimitry Andric     SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SR,
11980b57cec5SDimitry Andric                                     MVT::i16, Flag);
11990b57cec5SDimitry Andric     if (Shift)
12000b57cec5SDimitry Andric       // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
12010b57cec5SDimitry Andric       SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
12020b57cec5SDimitry Andric     SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
12030b57cec5SDimitry Andric     if (Invert)
12040b57cec5SDimitry Andric       SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
12050b57cec5SDimitry Andric     return SR;
12060b57cec5SDimitry Andric   } else {
12070b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, dl, VT);
12080b57cec5SDimitry Andric     SDValue Ops[] = {One, Zero, TargetCC, Flag};
1209e8d8bef9SDimitry Andric     return DAG.getNode(MSP430ISD::SELECT_CC, dl, Op.getValueType(), Ops);
12100b57cec5SDimitry Andric   }
12110b57cec5SDimitry Andric }
12120b57cec5SDimitry Andric 
12130b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
12140b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
12150b57cec5SDimitry Andric   SDValue LHS    = Op.getOperand(0);
12160b57cec5SDimitry Andric   SDValue RHS    = Op.getOperand(1);
12170b57cec5SDimitry Andric   SDValue TrueV  = Op.getOperand(2);
12180b57cec5SDimitry Andric   SDValue FalseV = Op.getOperand(3);
12190b57cec5SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
12200b57cec5SDimitry Andric   SDLoc dl   (Op);
12210b57cec5SDimitry Andric 
12220b57cec5SDimitry Andric   SDValue TargetCC;
12230b57cec5SDimitry Andric   SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric   SDValue Ops[] = {TrueV, FalseV, TargetCC, Flag};
12260b57cec5SDimitry Andric 
1227e8d8bef9SDimitry Andric   return DAG.getNode(MSP430ISD::SELECT_CC, dl, Op.getValueType(), Ops);
12280b57cec5SDimitry Andric }
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
12310b57cec5SDimitry Andric                                                SelectionDAG &DAG) const {
12320b57cec5SDimitry Andric   SDValue Val = Op.getOperand(0);
12330b57cec5SDimitry Andric   EVT VT      = Op.getValueType();
12340b57cec5SDimitry Andric   SDLoc dl(Op);
12350b57cec5SDimitry Andric 
12360b57cec5SDimitry Andric   assert(VT == MVT::i16 && "Only support i16 for now!");
12370b57cec5SDimitry Andric 
12380b57cec5SDimitry Andric   return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
12390b57cec5SDimitry Andric                      DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
12400b57cec5SDimitry Andric                      DAG.getValueType(Val.getValueType()));
12410b57cec5SDimitry Andric }
12420b57cec5SDimitry Andric 
12430b57cec5SDimitry Andric SDValue
12440b57cec5SDimitry Andric MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
12450b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
12460b57cec5SDimitry Andric   MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
12470b57cec5SDimitry Andric   int ReturnAddrIndex = FuncInfo->getRAIndex();
124881ad6265SDimitry Andric   MVT PtrVT = getFrameIndexTy(MF.getDataLayout());
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric   if (ReturnAddrIndex == 0) {
12510b57cec5SDimitry Andric     // Set up a frame object for the return address.
125281ad6265SDimitry Andric     uint64_t SlotSize = PtrVT.getStoreSize();
12530b57cec5SDimitry Andric     ReturnAddrIndex = MF.getFrameInfo().CreateFixedObject(SlotSize, -SlotSize,
12540b57cec5SDimitry Andric                                                            true);
12550b57cec5SDimitry Andric     FuncInfo->setRAIndex(ReturnAddrIndex);
12560b57cec5SDimitry Andric   }
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric   return DAG.getFrameIndex(ReturnAddrIndex, PtrVT);
12590b57cec5SDimitry Andric }
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
12620b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
12630b57cec5SDimitry Andric   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
12640b57cec5SDimitry Andric   MFI.setReturnAddressIsTaken(true);
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
12670b57cec5SDimitry Andric     return SDValue();
12680b57cec5SDimitry Andric 
1269647cbc5dSDimitry Andric   unsigned Depth = Op.getConstantOperandVal(0);
12700b57cec5SDimitry Andric   SDLoc dl(Op);
127181ad6265SDimitry Andric   EVT PtrVT = Op.getValueType();
12720b57cec5SDimitry Andric 
12730b57cec5SDimitry Andric   if (Depth > 0) {
12740b57cec5SDimitry Andric     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
12750b57cec5SDimitry Andric     SDValue Offset =
127681ad6265SDimitry Andric       DAG.getConstant(PtrVT.getStoreSize(), dl, MVT::i16);
12770b57cec5SDimitry Andric     return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
12780b57cec5SDimitry Andric                        DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset),
12790b57cec5SDimitry Andric                        MachinePointerInfo());
12800b57cec5SDimitry Andric   }
12810b57cec5SDimitry Andric 
12820b57cec5SDimitry Andric   // Just load the return address.
12830b57cec5SDimitry Andric   SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
12840b57cec5SDimitry Andric   return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI,
12850b57cec5SDimitry Andric                      MachinePointerInfo());
12860b57cec5SDimitry Andric }
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
12890b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
12900b57cec5SDimitry Andric   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
12910b57cec5SDimitry Andric   MFI.setFrameAddressIsTaken(true);
12920b57cec5SDimitry Andric 
12930b57cec5SDimitry Andric   EVT VT = Op.getValueType();
12940b57cec5SDimitry Andric   SDLoc dl(Op);  // FIXME probably not meaningful
1295647cbc5dSDimitry Andric   unsigned Depth = Op.getConstantOperandVal(0);
12960b57cec5SDimitry Andric   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
12975ffd83dbSDimitry Andric                                          MSP430::R4, VT);
12980b57cec5SDimitry Andric   while (Depth--)
12990b57cec5SDimitry Andric     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
13000b57cec5SDimitry Andric                             MachinePointerInfo());
13010b57cec5SDimitry Andric   return FrameAddr;
13020b57cec5SDimitry Andric }
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
13050b57cec5SDimitry Andric                                            SelectionDAG &DAG) const {
13060b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
13070b57cec5SDimitry Andric   MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
130881ad6265SDimitry Andric 
130981ad6265SDimitry Andric   SDValue Ptr = Op.getOperand(1);
131081ad6265SDimitry Andric   EVT PtrVT = Ptr.getValueType();
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric   // Frame index of first vararg argument
13130b57cec5SDimitry Andric   SDValue FrameIndex =
13140b57cec5SDimitry Andric       DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
13150b57cec5SDimitry Andric   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
13160b57cec5SDimitry Andric 
13170b57cec5SDimitry Andric   // Create a store of the frame index to the location operand
131881ad6265SDimitry Andric   return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex, Ptr,
13190b57cec5SDimitry Andric                       MachinePointerInfo(SV));
13200b57cec5SDimitry Andric }
13210b57cec5SDimitry Andric 
13220b57cec5SDimitry Andric SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op,
13230b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
13240b57cec5SDimitry Andric     JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
132581ad6265SDimitry Andric     EVT PtrVT = Op.getValueType();
13260b57cec5SDimitry Andric     SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
13270b57cec5SDimitry Andric     return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT), PtrVT, Result);
13280b57cec5SDimitry Andric }
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric /// getPostIndexedAddressParts - returns true by value, base pointer and
13310b57cec5SDimitry Andric /// offset pointer and addressing mode by reference if this node can be
13320b57cec5SDimitry Andric /// combined with a load / store to form a post-indexed load / store.
13330b57cec5SDimitry Andric bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
13340b57cec5SDimitry Andric                                                       SDValue &Base,
13350b57cec5SDimitry Andric                                                       SDValue &Offset,
13360b57cec5SDimitry Andric                                                       ISD::MemIndexedMode &AM,
13370b57cec5SDimitry Andric                                                       SelectionDAG &DAG) const {
13380b57cec5SDimitry Andric 
13390b57cec5SDimitry Andric   LoadSDNode *LD = cast<LoadSDNode>(N);
13400b57cec5SDimitry Andric   if (LD->getExtensionType() != ISD::NON_EXTLOAD)
13410b57cec5SDimitry Andric     return false;
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric   EVT VT = LD->getMemoryVT();
13440b57cec5SDimitry Andric   if (VT != MVT::i8 && VT != MVT::i16)
13450b57cec5SDimitry Andric     return false;
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric   if (Op->getOpcode() != ISD::ADD)
13480b57cec5SDimitry Andric     return false;
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
13510b57cec5SDimitry Andric     uint64_t RHSC = RHS->getZExtValue();
13520b57cec5SDimitry Andric     if ((VT == MVT::i16 && RHSC != 2) ||
13530b57cec5SDimitry Andric         (VT == MVT::i8 && RHSC != 1))
13540b57cec5SDimitry Andric       return false;
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric     Base = Op->getOperand(0);
13570b57cec5SDimitry Andric     Offset = DAG.getConstant(RHSC, SDLoc(N), VT);
13580b57cec5SDimitry Andric     AM = ISD::POST_INC;
13590b57cec5SDimitry Andric     return true;
13600b57cec5SDimitry Andric   }
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric   return false;
13630b57cec5SDimitry Andric }
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric 
13660b57cec5SDimitry Andric const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
13670b57cec5SDimitry Andric   switch ((MSP430ISD::NodeType)Opcode) {
13680b57cec5SDimitry Andric   case MSP430ISD::FIRST_NUMBER:       break;
136906c3fb27SDimitry Andric   case MSP430ISD::RET_GLUE:           return "MSP430ISD::RET_GLUE";
137006c3fb27SDimitry Andric   case MSP430ISD::RETI_GLUE:          return "MSP430ISD::RETI_GLUE";
13710b57cec5SDimitry Andric   case MSP430ISD::RRA:                return "MSP430ISD::RRA";
13720b57cec5SDimitry Andric   case MSP430ISD::RLA:                return "MSP430ISD::RLA";
13730b57cec5SDimitry Andric   case MSP430ISD::RRC:                return "MSP430ISD::RRC";
13740b57cec5SDimitry Andric   case MSP430ISD::RRCL:               return "MSP430ISD::RRCL";
13750b57cec5SDimitry Andric   case MSP430ISD::CALL:               return "MSP430ISD::CALL";
13760b57cec5SDimitry Andric   case MSP430ISD::Wrapper:            return "MSP430ISD::Wrapper";
13770b57cec5SDimitry Andric   case MSP430ISD::BR_CC:              return "MSP430ISD::BR_CC";
13780b57cec5SDimitry Andric   case MSP430ISD::CMP:                return "MSP430ISD::CMP";
13790b57cec5SDimitry Andric   case MSP430ISD::SETCC:              return "MSP430ISD::SETCC";
13800b57cec5SDimitry Andric   case MSP430ISD::SELECT_CC:          return "MSP430ISD::SELECT_CC";
13810b57cec5SDimitry Andric   case MSP430ISD::DADD:               return "MSP430ISD::DADD";
13820b57cec5SDimitry Andric   }
13830b57cec5SDimitry Andric   return nullptr;
13840b57cec5SDimitry Andric }
13850b57cec5SDimitry Andric 
13860b57cec5SDimitry Andric bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
13870b57cec5SDimitry Andric                                           Type *Ty2) const {
13880b57cec5SDimitry Andric   if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
13890b57cec5SDimitry Andric     return false;
13900b57cec5SDimitry Andric 
1391bdd1243dSDimitry Andric   return (Ty1->getPrimitiveSizeInBits().getFixedValue() >
1392bdd1243dSDimitry Andric           Ty2->getPrimitiveSizeInBits().getFixedValue());
13930b57cec5SDimitry Andric }
13940b57cec5SDimitry Andric 
13950b57cec5SDimitry Andric bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
13960b57cec5SDimitry Andric   if (!VT1.isInteger() || !VT2.isInteger())
13970b57cec5SDimitry Andric     return false;
13980b57cec5SDimitry Andric 
1399e8d8bef9SDimitry Andric   return (VT1.getFixedSizeInBits() > VT2.getFixedSizeInBits());
14000b57cec5SDimitry Andric }
14010b57cec5SDimitry Andric 
14020b57cec5SDimitry Andric bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
14030b57cec5SDimitry Andric   // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
140404eeddc0SDimitry Andric   return false && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
14050b57cec5SDimitry Andric }
14060b57cec5SDimitry Andric 
14070b57cec5SDimitry Andric bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
14080b57cec5SDimitry Andric   // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
140904eeddc0SDimitry Andric   return false && VT1 == MVT::i8 && VT2 == MVT::i16;
14100b57cec5SDimitry Andric }
14110b57cec5SDimitry Andric 
14120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14130b57cec5SDimitry Andric //  Other Lowering Code
14140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric MachineBasicBlock *
14170b57cec5SDimitry Andric MSP430TargetLowering::EmitShiftInstr(MachineInstr &MI,
14180b57cec5SDimitry Andric                                      MachineBasicBlock *BB) const {
14190b57cec5SDimitry Andric   MachineFunction *F = BB->getParent();
14200b57cec5SDimitry Andric   MachineRegisterInfo &RI = F->getRegInfo();
14210b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
14220b57cec5SDimitry Andric   const TargetInstrInfo &TII = *F->getSubtarget().getInstrInfo();
14230b57cec5SDimitry Andric 
14240b57cec5SDimitry Andric   unsigned Opc;
14250b57cec5SDimitry Andric   bool ClearCarry = false;
14260b57cec5SDimitry Andric   const TargetRegisterClass * RC;
14270b57cec5SDimitry Andric   switch (MI.getOpcode()) {
14280b57cec5SDimitry Andric   default: llvm_unreachable("Invalid shift opcode!");
14290b57cec5SDimitry Andric   case MSP430::Shl8:
14300b57cec5SDimitry Andric     Opc = MSP430::ADD8rr;
14310b57cec5SDimitry Andric     RC = &MSP430::GR8RegClass;
14320b57cec5SDimitry Andric     break;
14330b57cec5SDimitry Andric   case MSP430::Shl16:
14340b57cec5SDimitry Andric     Opc = MSP430::ADD16rr;
14350b57cec5SDimitry Andric     RC = &MSP430::GR16RegClass;
14360b57cec5SDimitry Andric     break;
14370b57cec5SDimitry Andric   case MSP430::Sra8:
14380b57cec5SDimitry Andric     Opc = MSP430::RRA8r;
14390b57cec5SDimitry Andric     RC = &MSP430::GR8RegClass;
14400b57cec5SDimitry Andric     break;
14410b57cec5SDimitry Andric   case MSP430::Sra16:
14420b57cec5SDimitry Andric     Opc = MSP430::RRA16r;
14430b57cec5SDimitry Andric     RC = &MSP430::GR16RegClass;
14440b57cec5SDimitry Andric     break;
14450b57cec5SDimitry Andric   case MSP430::Srl8:
14460b57cec5SDimitry Andric     ClearCarry = true;
14470b57cec5SDimitry Andric     Opc = MSP430::RRC8r;
14480b57cec5SDimitry Andric     RC = &MSP430::GR8RegClass;
14490b57cec5SDimitry Andric     break;
14500b57cec5SDimitry Andric   case MSP430::Srl16:
14510b57cec5SDimitry Andric     ClearCarry = true;
14520b57cec5SDimitry Andric     Opc = MSP430::RRC16r;
14530b57cec5SDimitry Andric     RC = &MSP430::GR16RegClass;
14540b57cec5SDimitry Andric     break;
14550b57cec5SDimitry Andric   case MSP430::Rrcl8:
14560b57cec5SDimitry Andric   case MSP430::Rrcl16: {
14570b57cec5SDimitry Andric     BuildMI(*BB, MI, dl, TII.get(MSP430::BIC16rc), MSP430::SR)
14580b57cec5SDimitry Andric       .addReg(MSP430::SR).addImm(1);
14598bcb0991SDimitry Andric     Register SrcReg = MI.getOperand(1).getReg();
14608bcb0991SDimitry Andric     Register DstReg = MI.getOperand(0).getReg();
14610b57cec5SDimitry Andric     unsigned RrcOpc = MI.getOpcode() == MSP430::Rrcl16
14620b57cec5SDimitry Andric                     ? MSP430::RRC16r : MSP430::RRC8r;
14630b57cec5SDimitry Andric     BuildMI(*BB, MI, dl, TII.get(RrcOpc), DstReg)
14640b57cec5SDimitry Andric       .addReg(SrcReg);
14650b57cec5SDimitry Andric     MI.eraseFromParent(); // The pseudo instruction is gone now.
14660b57cec5SDimitry Andric     return BB;
14670b57cec5SDimitry Andric   }
14680b57cec5SDimitry Andric   }
14690b57cec5SDimitry Andric 
14700b57cec5SDimitry Andric   const BasicBlock *LLVM_BB = BB->getBasicBlock();
14710b57cec5SDimitry Andric   MachineFunction::iterator I = ++BB->getIterator();
14720b57cec5SDimitry Andric 
14730b57cec5SDimitry Andric   // Create loop block
14740b57cec5SDimitry Andric   MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
14750b57cec5SDimitry Andric   MachineBasicBlock *RemBB  = F->CreateMachineBasicBlock(LLVM_BB);
14760b57cec5SDimitry Andric 
14770b57cec5SDimitry Andric   F->insert(I, LoopBB);
14780b57cec5SDimitry Andric   F->insert(I, RemBB);
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric   // Update machine-CFG edges by transferring all successors of the current
14810b57cec5SDimitry Andric   // block to the block containing instructions after shift.
14820b57cec5SDimitry Andric   RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
14830b57cec5SDimitry Andric                 BB->end());
14840b57cec5SDimitry Andric   RemBB->transferSuccessorsAndUpdatePHIs(BB);
14850b57cec5SDimitry Andric 
14860b57cec5SDimitry Andric   // Add edges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
14870b57cec5SDimitry Andric   BB->addSuccessor(LoopBB);
14880b57cec5SDimitry Andric   BB->addSuccessor(RemBB);
14890b57cec5SDimitry Andric   LoopBB->addSuccessor(RemBB);
14900b57cec5SDimitry Andric   LoopBB->addSuccessor(LoopBB);
14910b57cec5SDimitry Andric 
14928bcb0991SDimitry Andric   Register ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
14938bcb0991SDimitry Andric   Register ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
14948bcb0991SDimitry Andric   Register ShiftReg = RI.createVirtualRegister(RC);
14958bcb0991SDimitry Andric   Register ShiftReg2 = RI.createVirtualRegister(RC);
14968bcb0991SDimitry Andric   Register ShiftAmtSrcReg = MI.getOperand(2).getReg();
14978bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
14988bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
14990b57cec5SDimitry Andric 
15000b57cec5SDimitry Andric   // BB:
15010b57cec5SDimitry Andric   // cmp 0, N
15020b57cec5SDimitry Andric   // je RemBB
15030b57cec5SDimitry Andric   BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
15040b57cec5SDimitry Andric     .addReg(ShiftAmtSrcReg).addImm(0);
15050b57cec5SDimitry Andric   BuildMI(BB, dl, TII.get(MSP430::JCC))
15060b57cec5SDimitry Andric     .addMBB(RemBB)
15070b57cec5SDimitry Andric     .addImm(MSP430CC::COND_E);
15080b57cec5SDimitry Andric 
15090b57cec5SDimitry Andric   // LoopBB:
15100b57cec5SDimitry Andric   // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
15110b57cec5SDimitry Andric   // ShiftAmt = phi [%N, BB],      [%ShiftAmt2, LoopBB]
15120b57cec5SDimitry Andric   // ShiftReg2 = shift ShiftReg
15130b57cec5SDimitry Andric   // ShiftAmt2 = ShiftAmt - 1;
15140b57cec5SDimitry Andric   BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
15150b57cec5SDimitry Andric     .addReg(SrcReg).addMBB(BB)
15160b57cec5SDimitry Andric     .addReg(ShiftReg2).addMBB(LoopBB);
15170b57cec5SDimitry Andric   BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
15180b57cec5SDimitry Andric     .addReg(ShiftAmtSrcReg).addMBB(BB)
15190b57cec5SDimitry Andric     .addReg(ShiftAmtReg2).addMBB(LoopBB);
15200b57cec5SDimitry Andric   if (ClearCarry)
15210b57cec5SDimitry Andric     BuildMI(LoopBB, dl, TII.get(MSP430::BIC16rc), MSP430::SR)
15220b57cec5SDimitry Andric       .addReg(MSP430::SR).addImm(1);
15230b57cec5SDimitry Andric   if (Opc == MSP430::ADD8rr || Opc == MSP430::ADD16rr)
15240b57cec5SDimitry Andric     BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
15250b57cec5SDimitry Andric       .addReg(ShiftReg)
15260b57cec5SDimitry Andric       .addReg(ShiftReg);
15270b57cec5SDimitry Andric   else
15280b57cec5SDimitry Andric     BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
15290b57cec5SDimitry Andric       .addReg(ShiftReg);
15300b57cec5SDimitry Andric   BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
15310b57cec5SDimitry Andric     .addReg(ShiftAmtReg).addImm(1);
15320b57cec5SDimitry Andric   BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
15330b57cec5SDimitry Andric     .addMBB(LoopBB)
15340b57cec5SDimitry Andric     .addImm(MSP430CC::COND_NE);
15350b57cec5SDimitry Andric 
15360b57cec5SDimitry Andric   // RemBB:
15370b57cec5SDimitry Andric   // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
15380b57cec5SDimitry Andric   BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
15390b57cec5SDimitry Andric     .addReg(SrcReg).addMBB(BB)
15400b57cec5SDimitry Andric     .addReg(ShiftReg2).addMBB(LoopBB);
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric   MI.eraseFromParent(); // The pseudo instruction is gone now.
15430b57cec5SDimitry Andric   return RemBB;
15440b57cec5SDimitry Andric }
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric MachineBasicBlock *
15470b57cec5SDimitry Andric MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
15480b57cec5SDimitry Andric                                                   MachineBasicBlock *BB) const {
15490b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
15500b57cec5SDimitry Andric 
15510b57cec5SDimitry Andric   if (Opc == MSP430::Shl8  || Opc == MSP430::Shl16 ||
15520b57cec5SDimitry Andric       Opc == MSP430::Sra8  || Opc == MSP430::Sra16 ||
15530b57cec5SDimitry Andric       Opc == MSP430::Srl8  || Opc == MSP430::Srl16 ||
15540b57cec5SDimitry Andric       Opc == MSP430::Rrcl8 || Opc == MSP430::Rrcl16)
15550b57cec5SDimitry Andric     return EmitShiftInstr(MI, BB);
15560b57cec5SDimitry Andric 
15570b57cec5SDimitry Andric   const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
15580b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric   assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
15610b57cec5SDimitry Andric          "Unexpected instr type to insert");
15620b57cec5SDimitry Andric 
15630b57cec5SDimitry Andric   // To "insert" a SELECT instruction, we actually have to insert the diamond
15640b57cec5SDimitry Andric   // control-flow pattern.  The incoming instruction knows the destination vreg
15650b57cec5SDimitry Andric   // to set, the condition code register to branch on, the true/false values to
15660b57cec5SDimitry Andric   // select between, and a branch opcode to use.
15670b57cec5SDimitry Andric   const BasicBlock *LLVM_BB = BB->getBasicBlock();
15680b57cec5SDimitry Andric   MachineFunction::iterator I = ++BB->getIterator();
15690b57cec5SDimitry Andric 
15700b57cec5SDimitry Andric   //  thisMBB:
15710b57cec5SDimitry Andric   //  ...
15720b57cec5SDimitry Andric   //   TrueVal = ...
15730b57cec5SDimitry Andric   //   cmpTY ccX, r1, r2
15740b57cec5SDimitry Andric   //   jCC copy1MBB
15750b57cec5SDimitry Andric   //   fallthrough --> copy0MBB
15760b57cec5SDimitry Andric   MachineBasicBlock *thisMBB = BB;
15770b57cec5SDimitry Andric   MachineFunction *F = BB->getParent();
15780b57cec5SDimitry Andric   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
15790b57cec5SDimitry Andric   MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
15800b57cec5SDimitry Andric   F->insert(I, copy0MBB);
15810b57cec5SDimitry Andric   F->insert(I, copy1MBB);
15820b57cec5SDimitry Andric   // Update machine-CFG edges by transferring all successors of the current
15830b57cec5SDimitry Andric   // block to the new block which will contain the Phi node for the select.
15840b57cec5SDimitry Andric   copy1MBB->splice(copy1MBB->begin(), BB,
15850b57cec5SDimitry Andric                    std::next(MachineBasicBlock::iterator(MI)), BB->end());
15860b57cec5SDimitry Andric   copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
15870b57cec5SDimitry Andric   // Next, add the true and fallthrough blocks as its successors.
15880b57cec5SDimitry Andric   BB->addSuccessor(copy0MBB);
15890b57cec5SDimitry Andric   BB->addSuccessor(copy1MBB);
15900b57cec5SDimitry Andric 
15910b57cec5SDimitry Andric   BuildMI(BB, dl, TII.get(MSP430::JCC))
15920b57cec5SDimitry Andric       .addMBB(copy1MBB)
15930b57cec5SDimitry Andric       .addImm(MI.getOperand(3).getImm());
15940b57cec5SDimitry Andric 
15950b57cec5SDimitry Andric   //  copy0MBB:
15960b57cec5SDimitry Andric   //   %FalseValue = ...
15970b57cec5SDimitry Andric   //   # fallthrough to copy1MBB
15980b57cec5SDimitry Andric   BB = copy0MBB;
15990b57cec5SDimitry Andric 
16000b57cec5SDimitry Andric   // Update machine-CFG edges
16010b57cec5SDimitry Andric   BB->addSuccessor(copy1MBB);
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   //  copy1MBB:
16040b57cec5SDimitry Andric   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
16050b57cec5SDimitry Andric   //  ...
16060b57cec5SDimitry Andric   BB = copy1MBB;
16070b57cec5SDimitry Andric   BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI), MI.getOperand(0).getReg())
16080b57cec5SDimitry Andric       .addReg(MI.getOperand(2).getReg())
16090b57cec5SDimitry Andric       .addMBB(copy0MBB)
16100b57cec5SDimitry Andric       .addReg(MI.getOperand(1).getReg())
16110b57cec5SDimitry Andric       .addMBB(thisMBB);
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric   MI.eraseFromParent(); // The pseudo instruction is gone now.
16140b57cec5SDimitry Andric   return BB;
16150b57cec5SDimitry Andric }
1616