106f32e7eSjoerg //===-- Mips16ISelLowering.h - Mips16 DAG Lowering Interface ----*- C++ -*-===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // Subclass of MipsTargetLowering specialized for mips16.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg #include "Mips16ISelLowering.h"
1306f32e7eSjoerg #include "MCTargetDesc/MipsBaseInfo.h"
1406f32e7eSjoerg #include "Mips16HardFloatInfo.h"
1506f32e7eSjoerg #include "MipsMachineFunction.h"
1606f32e7eSjoerg #include "MipsRegisterInfo.h"
1706f32e7eSjoerg #include "MipsTargetMachine.h"
1806f32e7eSjoerg #include "llvm/CodeGen/MachineInstrBuilder.h"
1906f32e7eSjoerg #include "llvm/CodeGen/TargetInstrInfo.h"
2006f32e7eSjoerg #include "llvm/Support/CommandLine.h"
2106f32e7eSjoerg 
2206f32e7eSjoerg using namespace llvm;
2306f32e7eSjoerg 
2406f32e7eSjoerg #define DEBUG_TYPE "mips-lower"
2506f32e7eSjoerg 
2606f32e7eSjoerg static cl::opt<bool> DontExpandCondPseudos16(
2706f32e7eSjoerg   "mips16-dont-expand-cond-pseudo",
2806f32e7eSjoerg   cl::init(false),
2906f32e7eSjoerg   cl::desc("Don't expand conditional move related "
3006f32e7eSjoerg            "pseudos for Mips 16"),
3106f32e7eSjoerg   cl::Hidden);
3206f32e7eSjoerg 
3306f32e7eSjoerg namespace {
3406f32e7eSjoerg struct Mips16Libcall {
3506f32e7eSjoerg   RTLIB::Libcall Libcall;
3606f32e7eSjoerg   const char *Name;
3706f32e7eSjoerg 
operator <__anon15f4d3030111::Mips16Libcall3806f32e7eSjoerg   bool operator<(const Mips16Libcall &RHS) const {
3906f32e7eSjoerg     return std::strcmp(Name, RHS.Name) < 0;
4006f32e7eSjoerg   }
4106f32e7eSjoerg };
4206f32e7eSjoerg 
4306f32e7eSjoerg struct Mips16IntrinsicHelperType{
4406f32e7eSjoerg   const char* Name;
4506f32e7eSjoerg   const char* Helper;
4606f32e7eSjoerg 
operator <__anon15f4d3030111::Mips16IntrinsicHelperType4706f32e7eSjoerg   bool operator<(const Mips16IntrinsicHelperType &RHS) const {
4806f32e7eSjoerg     return std::strcmp(Name, RHS.Name) < 0;
4906f32e7eSjoerg   }
operator ==__anon15f4d3030111::Mips16IntrinsicHelperType5006f32e7eSjoerg   bool operator==(const Mips16IntrinsicHelperType &RHS) const {
5106f32e7eSjoerg     return std::strcmp(Name, RHS.Name) == 0;
5206f32e7eSjoerg   }
5306f32e7eSjoerg };
5406f32e7eSjoerg }
5506f32e7eSjoerg 
5606f32e7eSjoerg // Libcalls for which no helper is generated. Sorted by name for binary search.
5706f32e7eSjoerg static const Mips16Libcall HardFloatLibCalls[] = {
5806f32e7eSjoerg   { RTLIB::ADD_F64, "__mips16_adddf3" },
5906f32e7eSjoerg   { RTLIB::ADD_F32, "__mips16_addsf3" },
6006f32e7eSjoerg   { RTLIB::DIV_F64, "__mips16_divdf3" },
6106f32e7eSjoerg   { RTLIB::DIV_F32, "__mips16_divsf3" },
6206f32e7eSjoerg   { RTLIB::OEQ_F64, "__mips16_eqdf2" },
6306f32e7eSjoerg   { RTLIB::OEQ_F32, "__mips16_eqsf2" },
6406f32e7eSjoerg   { RTLIB::FPEXT_F32_F64, "__mips16_extendsfdf2" },
6506f32e7eSjoerg   { RTLIB::FPTOSINT_F64_I32, "__mips16_fix_truncdfsi" },
6606f32e7eSjoerg   { RTLIB::FPTOSINT_F32_I32, "__mips16_fix_truncsfsi" },
6706f32e7eSjoerg   { RTLIB::SINTTOFP_I32_F64, "__mips16_floatsidf" },
6806f32e7eSjoerg   { RTLIB::SINTTOFP_I32_F32, "__mips16_floatsisf" },
6906f32e7eSjoerg   { RTLIB::UINTTOFP_I32_F64, "__mips16_floatunsidf" },
7006f32e7eSjoerg   { RTLIB::UINTTOFP_I32_F32, "__mips16_floatunsisf" },
7106f32e7eSjoerg   { RTLIB::OGE_F64, "__mips16_gedf2" },
7206f32e7eSjoerg   { RTLIB::OGE_F32, "__mips16_gesf2" },
7306f32e7eSjoerg   { RTLIB::OGT_F64, "__mips16_gtdf2" },
7406f32e7eSjoerg   { RTLIB::OGT_F32, "__mips16_gtsf2" },
7506f32e7eSjoerg   { RTLIB::OLE_F64, "__mips16_ledf2" },
7606f32e7eSjoerg   { RTLIB::OLE_F32, "__mips16_lesf2" },
7706f32e7eSjoerg   { RTLIB::OLT_F64, "__mips16_ltdf2" },
7806f32e7eSjoerg   { RTLIB::OLT_F32, "__mips16_ltsf2" },
7906f32e7eSjoerg   { RTLIB::MUL_F64, "__mips16_muldf3" },
8006f32e7eSjoerg   { RTLIB::MUL_F32, "__mips16_mulsf3" },
8106f32e7eSjoerg   { RTLIB::UNE_F64, "__mips16_nedf2" },
8206f32e7eSjoerg   { RTLIB::UNE_F32, "__mips16_nesf2" },
8306f32e7eSjoerg   { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_dc" }, // No associated libcall.
8406f32e7eSjoerg   { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_df" }, // No associated libcall.
8506f32e7eSjoerg   { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sc" }, // No associated libcall.
8606f32e7eSjoerg   { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sf" }, // No associated libcall.
8706f32e7eSjoerg   { RTLIB::SUB_F64, "__mips16_subdf3" },
8806f32e7eSjoerg   { RTLIB::SUB_F32, "__mips16_subsf3" },
8906f32e7eSjoerg   { RTLIB::FPROUND_F64_F32, "__mips16_truncdfsf2" },
9006f32e7eSjoerg   { RTLIB::UO_F64, "__mips16_unorddf2" },
9106f32e7eSjoerg   { RTLIB::UO_F32, "__mips16_unordsf2" }
9206f32e7eSjoerg };
9306f32e7eSjoerg 
9406f32e7eSjoerg static const Mips16IntrinsicHelperType Mips16IntrinsicHelper[] = {
9506f32e7eSjoerg   {"__fixunsdfsi", "__mips16_call_stub_2" },
9606f32e7eSjoerg   {"ceil",  "__mips16_call_stub_df_2"},
9706f32e7eSjoerg   {"ceilf", "__mips16_call_stub_sf_1"},
9806f32e7eSjoerg   {"copysign",  "__mips16_call_stub_df_10"},
9906f32e7eSjoerg   {"copysignf", "__mips16_call_stub_sf_5"},
10006f32e7eSjoerg   {"cos",  "__mips16_call_stub_df_2"},
10106f32e7eSjoerg   {"cosf", "__mips16_call_stub_sf_1"},
10206f32e7eSjoerg   {"exp2",  "__mips16_call_stub_df_2"},
10306f32e7eSjoerg   {"exp2f", "__mips16_call_stub_sf_1"},
10406f32e7eSjoerg   {"floor",  "__mips16_call_stub_df_2"},
10506f32e7eSjoerg   {"floorf", "__mips16_call_stub_sf_1"},
10606f32e7eSjoerg   {"log2",  "__mips16_call_stub_df_2"},
10706f32e7eSjoerg   {"log2f", "__mips16_call_stub_sf_1"},
10806f32e7eSjoerg   {"nearbyint",  "__mips16_call_stub_df_2"},
10906f32e7eSjoerg   {"nearbyintf", "__mips16_call_stub_sf_1"},
11006f32e7eSjoerg   {"rint",  "__mips16_call_stub_df_2"},
11106f32e7eSjoerg   {"rintf", "__mips16_call_stub_sf_1"},
11206f32e7eSjoerg   {"sin",  "__mips16_call_stub_df_2"},
11306f32e7eSjoerg   {"sinf", "__mips16_call_stub_sf_1"},
11406f32e7eSjoerg   {"sqrt",  "__mips16_call_stub_df_2"},
11506f32e7eSjoerg   {"sqrtf", "__mips16_call_stub_sf_1"},
11606f32e7eSjoerg   {"trunc",  "__mips16_call_stub_df_2"},
11706f32e7eSjoerg   {"truncf", "__mips16_call_stub_sf_1"},
11806f32e7eSjoerg };
11906f32e7eSjoerg 
Mips16TargetLowering(const MipsTargetMachine & TM,const MipsSubtarget & STI)12006f32e7eSjoerg Mips16TargetLowering::Mips16TargetLowering(const MipsTargetMachine &TM,
12106f32e7eSjoerg                                            const MipsSubtarget &STI)
12206f32e7eSjoerg     : MipsTargetLowering(TM, STI) {
12306f32e7eSjoerg 
12406f32e7eSjoerg   // Set up the register classes
12506f32e7eSjoerg   addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
12606f32e7eSjoerg 
12706f32e7eSjoerg   if (!Subtarget.useSoftFloat())
12806f32e7eSjoerg     setMips16HardFloatLibCalls();
12906f32e7eSjoerg 
13006f32e7eSjoerg   setOperationAction(ISD::ATOMIC_FENCE,       MVT::Other, Expand);
13106f32e7eSjoerg   setOperationAction(ISD::ATOMIC_CMP_SWAP,    MVT::i32,   Expand);
13206f32e7eSjoerg   setOperationAction(ISD::ATOMIC_SWAP,        MVT::i32,   Expand);
13306f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_ADD,    MVT::i32,   Expand);
13406f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_SUB,    MVT::i32,   Expand);
13506f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_AND,    MVT::i32,   Expand);
13606f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_OR,     MVT::i32,   Expand);
13706f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_XOR,    MVT::i32,   Expand);
13806f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_NAND,   MVT::i32,   Expand);
13906f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_MIN,    MVT::i32,   Expand);
14006f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_MAX,    MVT::i32,   Expand);
14106f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_UMIN,   MVT::i32,   Expand);
14206f32e7eSjoerg   setOperationAction(ISD::ATOMIC_LOAD_UMAX,   MVT::i32,   Expand);
14306f32e7eSjoerg 
14406f32e7eSjoerg   setOperationAction(ISD::ROTR, MVT::i32,  Expand);
14506f32e7eSjoerg   setOperationAction(ISD::ROTR, MVT::i64,  Expand);
14606f32e7eSjoerg   setOperationAction(ISD::BSWAP, MVT::i32, Expand);
14706f32e7eSjoerg   setOperationAction(ISD::BSWAP, MVT::i64, Expand);
14806f32e7eSjoerg 
14906f32e7eSjoerg   computeRegisterProperties(STI.getRegisterInfo());
15006f32e7eSjoerg }
15106f32e7eSjoerg 
15206f32e7eSjoerg const MipsTargetLowering *
createMips16TargetLowering(const MipsTargetMachine & TM,const MipsSubtarget & STI)15306f32e7eSjoerg llvm::createMips16TargetLowering(const MipsTargetMachine &TM,
15406f32e7eSjoerg                                  const MipsSubtarget &STI) {
15506f32e7eSjoerg   return new Mips16TargetLowering(TM, STI);
15606f32e7eSjoerg }
15706f32e7eSjoerg 
allowsMisalignedMemoryAccesses(EVT VT,unsigned,Align,MachineMemOperand::Flags,bool * Fast) const15806f32e7eSjoerg bool Mips16TargetLowering::allowsMisalignedMemoryAccesses(
159*da58b97aSjoerg     EVT VT, unsigned, Align, MachineMemOperand::Flags, bool *Fast) const {
16006f32e7eSjoerg   return false;
16106f32e7eSjoerg }
16206f32e7eSjoerg 
16306f32e7eSjoerg MachineBasicBlock *
EmitInstrWithCustomInserter(MachineInstr & MI,MachineBasicBlock * BB) const16406f32e7eSjoerg Mips16TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
16506f32e7eSjoerg                                                   MachineBasicBlock *BB) const {
16606f32e7eSjoerg   switch (MI.getOpcode()) {
16706f32e7eSjoerg   default:
16806f32e7eSjoerg     return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
16906f32e7eSjoerg   case Mips::SelBeqZ:
17006f32e7eSjoerg     return emitSel16(Mips::BeqzRxImm16, MI, BB);
17106f32e7eSjoerg   case Mips::SelBneZ:
17206f32e7eSjoerg     return emitSel16(Mips::BnezRxImm16, MI, BB);
17306f32e7eSjoerg   case Mips::SelTBteqZCmpi:
17406f32e7eSjoerg     return emitSeliT16(Mips::Bteqz16, Mips::CmpiRxImmX16, MI, BB);
17506f32e7eSjoerg   case Mips::SelTBteqZSlti:
17606f32e7eSjoerg     return emitSeliT16(Mips::Bteqz16, Mips::SltiRxImmX16, MI, BB);
17706f32e7eSjoerg   case Mips::SelTBteqZSltiu:
17806f32e7eSjoerg     return emitSeliT16(Mips::Bteqz16, Mips::SltiuRxImmX16, MI, BB);
17906f32e7eSjoerg   case Mips::SelTBtneZCmpi:
18006f32e7eSjoerg     return emitSeliT16(Mips::Btnez16, Mips::CmpiRxImmX16, MI, BB);
18106f32e7eSjoerg   case Mips::SelTBtneZSlti:
18206f32e7eSjoerg     return emitSeliT16(Mips::Btnez16, Mips::SltiRxImmX16, MI, BB);
18306f32e7eSjoerg   case Mips::SelTBtneZSltiu:
18406f32e7eSjoerg     return emitSeliT16(Mips::Btnez16, Mips::SltiuRxImmX16, MI, BB);
18506f32e7eSjoerg   case Mips::SelTBteqZCmp:
18606f32e7eSjoerg     return emitSelT16(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
18706f32e7eSjoerg   case Mips::SelTBteqZSlt:
18806f32e7eSjoerg     return emitSelT16(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
18906f32e7eSjoerg   case Mips::SelTBteqZSltu:
19006f32e7eSjoerg     return emitSelT16(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
19106f32e7eSjoerg   case Mips::SelTBtneZCmp:
19206f32e7eSjoerg     return emitSelT16(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
19306f32e7eSjoerg   case Mips::SelTBtneZSlt:
19406f32e7eSjoerg     return emitSelT16(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
19506f32e7eSjoerg   case Mips::SelTBtneZSltu:
19606f32e7eSjoerg     return emitSelT16(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
19706f32e7eSjoerg   case Mips::BteqzT8CmpX16:
19806f32e7eSjoerg     return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
19906f32e7eSjoerg   case Mips::BteqzT8SltX16:
20006f32e7eSjoerg     return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
20106f32e7eSjoerg   case Mips::BteqzT8SltuX16:
20206f32e7eSjoerg     // TBD: figure out a way to get this or remove the instruction
20306f32e7eSjoerg     // altogether.
20406f32e7eSjoerg     return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
20506f32e7eSjoerg   case Mips::BtnezT8CmpX16:
20606f32e7eSjoerg     return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
20706f32e7eSjoerg   case Mips::BtnezT8SltX16:
20806f32e7eSjoerg     return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
20906f32e7eSjoerg   case Mips::BtnezT8SltuX16:
21006f32e7eSjoerg     // TBD: figure out a way to get this or remove the instruction
21106f32e7eSjoerg     // altogether.
21206f32e7eSjoerg     return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
21306f32e7eSjoerg   case Mips::BteqzT8CmpiX16: return emitFEXT_T8I8I16_ins(
21406f32e7eSjoerg     Mips::Bteqz16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
21506f32e7eSjoerg   case Mips::BteqzT8SltiX16: return emitFEXT_T8I8I16_ins(
21606f32e7eSjoerg     Mips::Bteqz16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
21706f32e7eSjoerg   case Mips::BteqzT8SltiuX16: return emitFEXT_T8I8I16_ins(
21806f32e7eSjoerg     Mips::Bteqz16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
21906f32e7eSjoerg   case Mips::BtnezT8CmpiX16: return emitFEXT_T8I8I16_ins(
22006f32e7eSjoerg     Mips::Btnez16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
22106f32e7eSjoerg   case Mips::BtnezT8SltiX16: return emitFEXT_T8I8I16_ins(
22206f32e7eSjoerg     Mips::Btnez16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
22306f32e7eSjoerg   case Mips::BtnezT8SltiuX16: return emitFEXT_T8I8I16_ins(
22406f32e7eSjoerg     Mips::Btnez16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
22506f32e7eSjoerg     break;
22606f32e7eSjoerg   case Mips::SltCCRxRy16:
22706f32e7eSjoerg     return emitFEXT_CCRX16_ins(Mips::SltRxRy16, MI, BB);
22806f32e7eSjoerg     break;
22906f32e7eSjoerg   case Mips::SltiCCRxImmX16:
23006f32e7eSjoerg     return emitFEXT_CCRXI16_ins
23106f32e7eSjoerg       (Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
23206f32e7eSjoerg   case Mips::SltiuCCRxImmX16:
23306f32e7eSjoerg     return emitFEXT_CCRXI16_ins
23406f32e7eSjoerg       (Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
23506f32e7eSjoerg   case Mips::SltuCCRxRy16:
23606f32e7eSjoerg     return emitFEXT_CCRX16_ins
23706f32e7eSjoerg       (Mips::SltuRxRy16, MI, BB);
23806f32e7eSjoerg   }
23906f32e7eSjoerg }
24006f32e7eSjoerg 
isEligibleForTailCallOptimization(const CCState & CCInfo,unsigned NextStackOffset,const MipsFunctionInfo & FI) const24106f32e7eSjoerg bool Mips16TargetLowering::isEligibleForTailCallOptimization(
24206f32e7eSjoerg     const CCState &CCInfo, unsigned NextStackOffset,
24306f32e7eSjoerg     const MipsFunctionInfo &FI) const {
24406f32e7eSjoerg   // No tail call optimization for mips16.
24506f32e7eSjoerg   return false;
24606f32e7eSjoerg }
24706f32e7eSjoerg 
setMips16HardFloatLibCalls()24806f32e7eSjoerg void Mips16TargetLowering::setMips16HardFloatLibCalls() {
24906f32e7eSjoerg   for (unsigned I = 0; I != array_lengthof(HardFloatLibCalls); ++I) {
25006f32e7eSjoerg     assert((I == 0 || HardFloatLibCalls[I - 1] < HardFloatLibCalls[I]) &&
25106f32e7eSjoerg            "Array not sorted!");
25206f32e7eSjoerg     if (HardFloatLibCalls[I].Libcall != RTLIB::UNKNOWN_LIBCALL)
25306f32e7eSjoerg       setLibcallName(HardFloatLibCalls[I].Libcall, HardFloatLibCalls[I].Name);
25406f32e7eSjoerg   }
25506f32e7eSjoerg }
25606f32e7eSjoerg 
25706f32e7eSjoerg //
25806f32e7eSjoerg // The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
25906f32e7eSjoerg // cleaner way to do all of this but it will have to wait until the traditional
26006f32e7eSjoerg // gcc mechanism is completed.
26106f32e7eSjoerg //
26206f32e7eSjoerg // For Pic, in order for Mips16 code to call Mips32 code which according the abi
26306f32e7eSjoerg // have either arguments or returned values placed in floating point registers,
26406f32e7eSjoerg // we use a set of helper functions. (This includes functions which return type
26506f32e7eSjoerg //  complex which on Mips are returned in a pair of floating point registers).
26606f32e7eSjoerg //
26706f32e7eSjoerg // This is an encoding that we inherited from gcc.
26806f32e7eSjoerg // In Mips traditional O32, N32 ABI, floating point numbers are passed in
26906f32e7eSjoerg // floating point argument registers 1,2 only when the first and optionally
27006f32e7eSjoerg // the second arguments are float (sf) or double (df).
27106f32e7eSjoerg // For Mips16 we are only concerned with the situations where floating point
27206f32e7eSjoerg // arguments are being passed in floating point registers by the ABI, because
27306f32e7eSjoerg // Mips16 mode code cannot execute floating point instructions to load those
27406f32e7eSjoerg // values and hence helper functions are needed.
27506f32e7eSjoerg // The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
27606f32e7eSjoerg // the helper function suffixs for these are:
27706f32e7eSjoerg //                        0,  1,    5,        9,         2,   6,        10
27806f32e7eSjoerg // this suffix can then be calculated as follows:
27906f32e7eSjoerg // for a given argument Arg:
28006f32e7eSjoerg //     Arg1x, Arg2x = 1 :  Arg is sf
28106f32e7eSjoerg //                    2 :  Arg is df
28206f32e7eSjoerg //                    0:   Arg is neither sf or df
28306f32e7eSjoerg // So this stub is the string for number Arg1x + Arg2x*4.
28406f32e7eSjoerg // However not all numbers between 0 and 10 are possible, we check anyway and
28506f32e7eSjoerg // assert if the impossible exists.
28606f32e7eSjoerg //
28706f32e7eSjoerg 
getMips16HelperFunctionStubNumber(ArgListTy & Args) const28806f32e7eSjoerg unsigned int Mips16TargetLowering::getMips16HelperFunctionStubNumber
28906f32e7eSjoerg   (ArgListTy &Args) const {
29006f32e7eSjoerg   unsigned int resultNum = 0;
29106f32e7eSjoerg   if (Args.size() >= 1) {
29206f32e7eSjoerg     Type *t = Args[0].Ty;
29306f32e7eSjoerg     if (t->isFloatTy()) {
29406f32e7eSjoerg       resultNum = 1;
29506f32e7eSjoerg     }
29606f32e7eSjoerg     else if (t->isDoubleTy()) {
29706f32e7eSjoerg       resultNum = 2;
29806f32e7eSjoerg     }
29906f32e7eSjoerg   }
30006f32e7eSjoerg   if (resultNum) {
30106f32e7eSjoerg     if (Args.size() >=2) {
30206f32e7eSjoerg       Type *t = Args[1].Ty;
30306f32e7eSjoerg       if (t->isFloatTy()) {
30406f32e7eSjoerg         resultNum += 4;
30506f32e7eSjoerg       }
30606f32e7eSjoerg       else if (t->isDoubleTy()) {
30706f32e7eSjoerg         resultNum += 8;
30806f32e7eSjoerg       }
30906f32e7eSjoerg     }
31006f32e7eSjoerg   }
31106f32e7eSjoerg   return resultNum;
31206f32e7eSjoerg }
31306f32e7eSjoerg 
31406f32e7eSjoerg //
31506f32e7eSjoerg // Prefixes are attached to stub numbers depending on the return type.
31606f32e7eSjoerg // return type: float  sf_
31706f32e7eSjoerg //              double df_
31806f32e7eSjoerg //              single complex sc_
31906f32e7eSjoerg //              double complext dc_
32006f32e7eSjoerg //              others  NO PREFIX
32106f32e7eSjoerg //
32206f32e7eSjoerg //
32306f32e7eSjoerg // The full name of a helper function is__mips16_call_stub +
32406f32e7eSjoerg //    return type dependent prefix + stub number
32506f32e7eSjoerg //
32606f32e7eSjoerg // FIXME: This is something that probably should be in a different source file
32706f32e7eSjoerg // and perhaps done differently but my main purpose is to not waste runtime
32806f32e7eSjoerg // on something that we can enumerate in the source. Another possibility is
32906f32e7eSjoerg // to have a python script to generate these mapping tables. This will do
33006f32e7eSjoerg // for now. There are a whole series of helper function mapping arrays, one
33106f32e7eSjoerg // for each return type class as outlined above. There there are 11 possible
33206f32e7eSjoerg // entries. Ones with 0 are ones which should never be selected.
33306f32e7eSjoerg //
33406f32e7eSjoerg // All the arrays are similar except for ones which return neither
33506f32e7eSjoerg // sf, df, sc, dc, in which we only care about ones which have sf or df as a
33606f32e7eSjoerg // first parameter.
33706f32e7eSjoerg //
33806f32e7eSjoerg #define P_ "__mips16_call_stub_"
33906f32e7eSjoerg #define MAX_STUB_NUMBER 10
34006f32e7eSjoerg #define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
34106f32e7eSjoerg #define T P "0" , T1
34206f32e7eSjoerg #define P P_
34306f32e7eSjoerg static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
34406f32e7eSjoerg   {nullptr, T1 };
34506f32e7eSjoerg #undef P
34606f32e7eSjoerg #define P P_ "sf_"
34706f32e7eSjoerg static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
34806f32e7eSjoerg   { T };
34906f32e7eSjoerg #undef P
35006f32e7eSjoerg #define P P_ "df_"
35106f32e7eSjoerg static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
35206f32e7eSjoerg   { T };
35306f32e7eSjoerg #undef P
35406f32e7eSjoerg #define P P_ "sc_"
35506f32e7eSjoerg static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
35606f32e7eSjoerg   { T };
35706f32e7eSjoerg #undef P
35806f32e7eSjoerg #define P P_ "dc_"
35906f32e7eSjoerg static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
36006f32e7eSjoerg   { T };
36106f32e7eSjoerg #undef P
36206f32e7eSjoerg #undef P_
36306f32e7eSjoerg 
36406f32e7eSjoerg 
36506f32e7eSjoerg const char* Mips16TargetLowering::
getMips16HelperFunction(Type * RetTy,ArgListTy & Args,bool & needHelper) const36606f32e7eSjoerg   getMips16HelperFunction
36706f32e7eSjoerg     (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
36806f32e7eSjoerg   const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
36906f32e7eSjoerg #ifndef NDEBUG
37006f32e7eSjoerg   const unsigned int maxStubNum = 10;
37106f32e7eSjoerg   assert(stubNum <= maxStubNum);
37206f32e7eSjoerg   const bool validStubNum[maxStubNum+1] =
37306f32e7eSjoerg     {true, true, true, false, false, true, true, false, false, true, true};
37406f32e7eSjoerg   assert(validStubNum[stubNum]);
37506f32e7eSjoerg #endif
37606f32e7eSjoerg   const char *result;
37706f32e7eSjoerg   if (RetTy->isFloatTy()) {
37806f32e7eSjoerg     result = sfMips16Helper[stubNum];
37906f32e7eSjoerg   }
38006f32e7eSjoerg   else if (RetTy ->isDoubleTy()) {
38106f32e7eSjoerg     result = dfMips16Helper[stubNum];
38206f32e7eSjoerg   } else if (StructType *SRetTy = dyn_cast<StructType>(RetTy)) {
38306f32e7eSjoerg     // check if it's complex
38406f32e7eSjoerg     if (SRetTy->getNumElements() == 2) {
38506f32e7eSjoerg       if ((SRetTy->getElementType(0)->isFloatTy()) &&
38606f32e7eSjoerg           (SRetTy->getElementType(1)->isFloatTy())) {
38706f32e7eSjoerg         result = scMips16Helper[stubNum];
38806f32e7eSjoerg       } else if ((SRetTy->getElementType(0)->isDoubleTy()) &&
38906f32e7eSjoerg                  (SRetTy->getElementType(1)->isDoubleTy())) {
39006f32e7eSjoerg         result = dcMips16Helper[stubNum];
39106f32e7eSjoerg       } else {
39206f32e7eSjoerg         llvm_unreachable("Uncovered condition");
39306f32e7eSjoerg       }
39406f32e7eSjoerg     } else {
39506f32e7eSjoerg       llvm_unreachable("Uncovered condition");
39606f32e7eSjoerg     }
39706f32e7eSjoerg   } else {
39806f32e7eSjoerg     if (stubNum == 0) {
39906f32e7eSjoerg       needHelper = false;
40006f32e7eSjoerg       return "";
40106f32e7eSjoerg     }
40206f32e7eSjoerg     result = vMips16Helper[stubNum];
40306f32e7eSjoerg   }
40406f32e7eSjoerg   needHelper = true;
40506f32e7eSjoerg   return result;
40606f32e7eSjoerg }
40706f32e7eSjoerg 
40806f32e7eSjoerg void Mips16TargetLowering::
getOpndList(SmallVectorImpl<SDValue> & Ops,std::deque<std::pair<unsigned,SDValue>> & RegsToPass,bool IsPICCall,bool GlobalOrExternal,bool InternalLinkage,bool IsCallReloc,CallLoweringInfo & CLI,SDValue Callee,SDValue Chain) const40906f32e7eSjoerg getOpndList(SmallVectorImpl<SDValue> &Ops,
41006f32e7eSjoerg             std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
41106f32e7eSjoerg             bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
41206f32e7eSjoerg             bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
41306f32e7eSjoerg             SDValue Chain) const {
41406f32e7eSjoerg   SelectionDAG &DAG = CLI.DAG;
41506f32e7eSjoerg   MachineFunction &MF = DAG.getMachineFunction();
41606f32e7eSjoerg   MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
41706f32e7eSjoerg   const char* Mips16HelperFunction = nullptr;
41806f32e7eSjoerg   bool NeedMips16Helper = false;
41906f32e7eSjoerg 
42006f32e7eSjoerg   if (Subtarget.inMips16HardFloat()) {
42106f32e7eSjoerg     //
42206f32e7eSjoerg     // currently we don't have symbols tagged with the mips16 or mips32
42306f32e7eSjoerg     // qualifier so we will assume that we don't know what kind it is.
42406f32e7eSjoerg     // and generate the helper
42506f32e7eSjoerg     //
42606f32e7eSjoerg     bool LookupHelper = true;
42706f32e7eSjoerg     if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(CLI.Callee)) {
42806f32e7eSjoerg       Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL, S->getSymbol() };
42906f32e7eSjoerg 
43006f32e7eSjoerg       if (std::binary_search(std::begin(HardFloatLibCalls),
43106f32e7eSjoerg                              std::end(HardFloatLibCalls), Find))
43206f32e7eSjoerg         LookupHelper = false;
43306f32e7eSjoerg       else {
43406f32e7eSjoerg         const char *Symbol = S->getSymbol();
43506f32e7eSjoerg         Mips16IntrinsicHelperType IntrinsicFind = { Symbol, "" };
43606f32e7eSjoerg         const Mips16HardFloatInfo::FuncSignature *Signature =
43706f32e7eSjoerg             Mips16HardFloatInfo::findFuncSignature(Symbol);
43806f32e7eSjoerg         if (!IsPICCall && (Signature && (FuncInfo->StubsNeeded.find(Symbol) ==
43906f32e7eSjoerg                                          FuncInfo->StubsNeeded.end()))) {
44006f32e7eSjoerg           FuncInfo->StubsNeeded[Symbol] = Signature;
44106f32e7eSjoerg           //
44206f32e7eSjoerg           // S2 is normally saved if the stub is for a function which
44306f32e7eSjoerg           // returns a float or double value and is not otherwise. This is
44406f32e7eSjoerg           // because more work is required after the function the stub
44506f32e7eSjoerg           // is calling completes, and so the stub cannot directly return
44606f32e7eSjoerg           // and the stub has no stack space to store the return address so
44706f32e7eSjoerg           // S2 is used for that purpose.
44806f32e7eSjoerg           // In order to take advantage of not saving S2, we need to also
44906f32e7eSjoerg           // optimize the call in the stub and this requires some further
45006f32e7eSjoerg           // functionality in MipsAsmPrinter which we don't have yet.
45106f32e7eSjoerg           // So for now we always save S2. The optimization will be done
45206f32e7eSjoerg           // in a follow-on patch.
45306f32e7eSjoerg           //
45406f32e7eSjoerg           if (1 || (Signature->RetSig != Mips16HardFloatInfo::NoFPRet))
45506f32e7eSjoerg             FuncInfo->setSaveS2();
45606f32e7eSjoerg         }
45706f32e7eSjoerg         // one more look at list of intrinsics
45806f32e7eSjoerg         const Mips16IntrinsicHelperType *Helper =
45906f32e7eSjoerg             llvm::lower_bound(Mips16IntrinsicHelper, IntrinsicFind);
46006f32e7eSjoerg         if (Helper != std::end(Mips16IntrinsicHelper) &&
46106f32e7eSjoerg             *Helper == IntrinsicFind) {
46206f32e7eSjoerg           Mips16HelperFunction = Helper->Helper;
46306f32e7eSjoerg           NeedMips16Helper = true;
46406f32e7eSjoerg           LookupHelper = false;
46506f32e7eSjoerg         }
46606f32e7eSjoerg 
46706f32e7eSjoerg       }
46806f32e7eSjoerg     } else if (GlobalAddressSDNode *G =
46906f32e7eSjoerg                    dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
47006f32e7eSjoerg       Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL,
47106f32e7eSjoerg                              G->getGlobal()->getName().data() };
47206f32e7eSjoerg 
47306f32e7eSjoerg       if (std::binary_search(std::begin(HardFloatLibCalls),
47406f32e7eSjoerg                              std::end(HardFloatLibCalls), Find))
47506f32e7eSjoerg         LookupHelper = false;
47606f32e7eSjoerg     }
47706f32e7eSjoerg     if (LookupHelper)
47806f32e7eSjoerg       Mips16HelperFunction =
47906f32e7eSjoerg         getMips16HelperFunction(CLI.RetTy, CLI.getArgs(), NeedMips16Helper);
48006f32e7eSjoerg   }
48106f32e7eSjoerg 
48206f32e7eSjoerg   SDValue JumpTarget = Callee;
48306f32e7eSjoerg 
48406f32e7eSjoerg   // T9 should contain the address of the callee function if
48506f32e7eSjoerg   // -relocation-model=pic or it is an indirect call.
48606f32e7eSjoerg   if (IsPICCall || !GlobalOrExternal) {
48706f32e7eSjoerg     unsigned V0Reg = Mips::V0;
48806f32e7eSjoerg     if (NeedMips16Helper) {
48906f32e7eSjoerg       RegsToPass.push_front(std::make_pair(V0Reg, Callee));
49006f32e7eSjoerg       JumpTarget = DAG.getExternalSymbol(Mips16HelperFunction,
49106f32e7eSjoerg                                          getPointerTy(DAG.getDataLayout()));
49206f32e7eSjoerg       ExternalSymbolSDNode *S = cast<ExternalSymbolSDNode>(JumpTarget);
49306f32e7eSjoerg       JumpTarget = getAddrGlobal(S, CLI.DL, JumpTarget.getValueType(), DAG,
49406f32e7eSjoerg                                  MipsII::MO_GOT, Chain,
495*da58b97aSjoerg                                  FuncInfo->callPtrInfo(MF, S->getSymbol()));
49606f32e7eSjoerg     } else
49706f32e7eSjoerg       RegsToPass.push_front(std::make_pair((unsigned)Mips::T9, Callee));
49806f32e7eSjoerg   }
49906f32e7eSjoerg 
50006f32e7eSjoerg   Ops.push_back(JumpTarget);
50106f32e7eSjoerg 
50206f32e7eSjoerg   MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
50306f32e7eSjoerg                                   InternalLinkage, IsCallReloc, CLI, Callee,
50406f32e7eSjoerg                                   Chain);
50506f32e7eSjoerg }
50606f32e7eSjoerg 
50706f32e7eSjoerg MachineBasicBlock *
emitSel16(unsigned Opc,MachineInstr & MI,MachineBasicBlock * BB) const50806f32e7eSjoerg Mips16TargetLowering::emitSel16(unsigned Opc, MachineInstr &MI,
50906f32e7eSjoerg                                 MachineBasicBlock *BB) const {
51006f32e7eSjoerg   if (DontExpandCondPseudos16)
51106f32e7eSjoerg     return BB;
51206f32e7eSjoerg   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
51306f32e7eSjoerg   DebugLoc DL = MI.getDebugLoc();
51406f32e7eSjoerg   // To "insert" a SELECT_CC instruction, we actually have to insert the
51506f32e7eSjoerg   // diamond control-flow pattern.  The incoming instruction knows the
51606f32e7eSjoerg   // destination vreg to set, the condition code register to branch on, the
51706f32e7eSjoerg   // true/false values to select between, and a branch opcode to use.
51806f32e7eSjoerg   const BasicBlock *LLVM_BB = BB->getBasicBlock();
51906f32e7eSjoerg   MachineFunction::iterator It = ++BB->getIterator();
52006f32e7eSjoerg 
52106f32e7eSjoerg   //  thisMBB:
52206f32e7eSjoerg   //  ...
52306f32e7eSjoerg   //   TrueVal = ...
52406f32e7eSjoerg   //   setcc r1, r2, r3
52506f32e7eSjoerg   //   bNE   r1, r0, copy1MBB
52606f32e7eSjoerg   //   fallthrough --> copy0MBB
52706f32e7eSjoerg   MachineBasicBlock *thisMBB  = BB;
52806f32e7eSjoerg   MachineFunction *F = BB->getParent();
52906f32e7eSjoerg   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
53006f32e7eSjoerg   MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
53106f32e7eSjoerg   F->insert(It, copy0MBB);
53206f32e7eSjoerg   F->insert(It, sinkMBB);
53306f32e7eSjoerg 
53406f32e7eSjoerg   // Transfer the remainder of BB and its successor edges to sinkMBB.
53506f32e7eSjoerg   sinkMBB->splice(sinkMBB->begin(), BB,
53606f32e7eSjoerg                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
53706f32e7eSjoerg   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
53806f32e7eSjoerg 
53906f32e7eSjoerg   // Next, add the true and fallthrough blocks as its successors.
54006f32e7eSjoerg   BB->addSuccessor(copy0MBB);
54106f32e7eSjoerg   BB->addSuccessor(sinkMBB);
54206f32e7eSjoerg 
54306f32e7eSjoerg   BuildMI(BB, DL, TII->get(Opc))
54406f32e7eSjoerg       .addReg(MI.getOperand(3).getReg())
54506f32e7eSjoerg       .addMBB(sinkMBB);
54606f32e7eSjoerg 
54706f32e7eSjoerg   //  copy0MBB:
54806f32e7eSjoerg   //   %FalseValue = ...
54906f32e7eSjoerg   //   # fallthrough to sinkMBB
55006f32e7eSjoerg   BB = copy0MBB;
55106f32e7eSjoerg 
55206f32e7eSjoerg   // Update machine-CFG edges
55306f32e7eSjoerg   BB->addSuccessor(sinkMBB);
55406f32e7eSjoerg 
55506f32e7eSjoerg   //  sinkMBB:
55606f32e7eSjoerg   //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
55706f32e7eSjoerg   //  ...
55806f32e7eSjoerg   BB = sinkMBB;
55906f32e7eSjoerg 
56006f32e7eSjoerg   BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
56106f32e7eSjoerg       .addReg(MI.getOperand(1).getReg())
56206f32e7eSjoerg       .addMBB(thisMBB)
56306f32e7eSjoerg       .addReg(MI.getOperand(2).getReg())
56406f32e7eSjoerg       .addMBB(copy0MBB);
56506f32e7eSjoerg 
56606f32e7eSjoerg   MI.eraseFromParent(); // The pseudo instruction is gone now.
56706f32e7eSjoerg   return BB;
56806f32e7eSjoerg }
56906f32e7eSjoerg 
57006f32e7eSjoerg MachineBasicBlock *
emitSelT16(unsigned Opc1,unsigned Opc2,MachineInstr & MI,MachineBasicBlock * BB) const57106f32e7eSjoerg Mips16TargetLowering::emitSelT16(unsigned Opc1, unsigned Opc2, MachineInstr &MI,
57206f32e7eSjoerg                                  MachineBasicBlock *BB) const {
57306f32e7eSjoerg   if (DontExpandCondPseudos16)
57406f32e7eSjoerg     return BB;
57506f32e7eSjoerg   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
57606f32e7eSjoerg   DebugLoc DL = MI.getDebugLoc();
57706f32e7eSjoerg   // To "insert" a SELECT_CC instruction, we actually have to insert the
57806f32e7eSjoerg   // diamond control-flow pattern.  The incoming instruction knows the
57906f32e7eSjoerg   // destination vreg to set, the condition code register to branch on, the
58006f32e7eSjoerg   // true/false values to select between, and a branch opcode to use.
58106f32e7eSjoerg   const BasicBlock *LLVM_BB = BB->getBasicBlock();
58206f32e7eSjoerg   MachineFunction::iterator It = ++BB->getIterator();
58306f32e7eSjoerg 
58406f32e7eSjoerg   //  thisMBB:
58506f32e7eSjoerg   //  ...
58606f32e7eSjoerg   //   TrueVal = ...
58706f32e7eSjoerg   //   setcc r1, r2, r3
58806f32e7eSjoerg   //   bNE   r1, r0, copy1MBB
58906f32e7eSjoerg   //   fallthrough --> copy0MBB
59006f32e7eSjoerg   MachineBasicBlock *thisMBB  = BB;
59106f32e7eSjoerg   MachineFunction *F = BB->getParent();
59206f32e7eSjoerg   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
59306f32e7eSjoerg   MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
59406f32e7eSjoerg   F->insert(It, copy0MBB);
59506f32e7eSjoerg   F->insert(It, sinkMBB);
59606f32e7eSjoerg 
59706f32e7eSjoerg   // Transfer the remainder of BB and its successor edges to sinkMBB.
59806f32e7eSjoerg   sinkMBB->splice(sinkMBB->begin(), BB,
59906f32e7eSjoerg                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
60006f32e7eSjoerg   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
60106f32e7eSjoerg 
60206f32e7eSjoerg   // Next, add the true and fallthrough blocks as its successors.
60306f32e7eSjoerg   BB->addSuccessor(copy0MBB);
60406f32e7eSjoerg   BB->addSuccessor(sinkMBB);
60506f32e7eSjoerg 
60606f32e7eSjoerg   BuildMI(BB, DL, TII->get(Opc2))
60706f32e7eSjoerg       .addReg(MI.getOperand(3).getReg())
60806f32e7eSjoerg       .addReg(MI.getOperand(4).getReg());
60906f32e7eSjoerg   BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
61006f32e7eSjoerg 
61106f32e7eSjoerg   //  copy0MBB:
61206f32e7eSjoerg   //   %FalseValue = ...
61306f32e7eSjoerg   //   # fallthrough to sinkMBB
61406f32e7eSjoerg   BB = copy0MBB;
61506f32e7eSjoerg 
61606f32e7eSjoerg   // Update machine-CFG edges
61706f32e7eSjoerg   BB->addSuccessor(sinkMBB);
61806f32e7eSjoerg 
61906f32e7eSjoerg   //  sinkMBB:
62006f32e7eSjoerg   //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
62106f32e7eSjoerg   //  ...
62206f32e7eSjoerg   BB = sinkMBB;
62306f32e7eSjoerg 
62406f32e7eSjoerg   BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
62506f32e7eSjoerg       .addReg(MI.getOperand(1).getReg())
62606f32e7eSjoerg       .addMBB(thisMBB)
62706f32e7eSjoerg       .addReg(MI.getOperand(2).getReg())
62806f32e7eSjoerg       .addMBB(copy0MBB);
62906f32e7eSjoerg 
63006f32e7eSjoerg   MI.eraseFromParent(); // The pseudo instruction is gone now.
63106f32e7eSjoerg   return BB;
63206f32e7eSjoerg 
63306f32e7eSjoerg }
63406f32e7eSjoerg 
63506f32e7eSjoerg MachineBasicBlock *
emitSeliT16(unsigned Opc1,unsigned Opc2,MachineInstr & MI,MachineBasicBlock * BB) const63606f32e7eSjoerg Mips16TargetLowering::emitSeliT16(unsigned Opc1, unsigned Opc2,
63706f32e7eSjoerg                                   MachineInstr &MI,
63806f32e7eSjoerg                                   MachineBasicBlock *BB) const {
63906f32e7eSjoerg   if (DontExpandCondPseudos16)
64006f32e7eSjoerg     return BB;
64106f32e7eSjoerg   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
64206f32e7eSjoerg   DebugLoc DL = MI.getDebugLoc();
64306f32e7eSjoerg   // To "insert" a SELECT_CC instruction, we actually have to insert the
64406f32e7eSjoerg   // diamond control-flow pattern.  The incoming instruction knows the
64506f32e7eSjoerg   // destination vreg to set, the condition code register to branch on, the
64606f32e7eSjoerg   // true/false values to select between, and a branch opcode to use.
64706f32e7eSjoerg   const BasicBlock *LLVM_BB = BB->getBasicBlock();
64806f32e7eSjoerg   MachineFunction::iterator It = ++BB->getIterator();
64906f32e7eSjoerg 
65006f32e7eSjoerg   //  thisMBB:
65106f32e7eSjoerg   //  ...
65206f32e7eSjoerg   //   TrueVal = ...
65306f32e7eSjoerg   //   setcc r1, r2, r3
65406f32e7eSjoerg   //   bNE   r1, r0, copy1MBB
65506f32e7eSjoerg   //   fallthrough --> copy0MBB
65606f32e7eSjoerg   MachineBasicBlock *thisMBB  = BB;
65706f32e7eSjoerg   MachineFunction *F = BB->getParent();
65806f32e7eSjoerg   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
65906f32e7eSjoerg   MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
66006f32e7eSjoerg   F->insert(It, copy0MBB);
66106f32e7eSjoerg   F->insert(It, sinkMBB);
66206f32e7eSjoerg 
66306f32e7eSjoerg   // Transfer the remainder of BB and its successor edges to sinkMBB.
66406f32e7eSjoerg   sinkMBB->splice(sinkMBB->begin(), BB,
66506f32e7eSjoerg                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
66606f32e7eSjoerg   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
66706f32e7eSjoerg 
66806f32e7eSjoerg   // Next, add the true and fallthrough blocks as its successors.
66906f32e7eSjoerg   BB->addSuccessor(copy0MBB);
67006f32e7eSjoerg   BB->addSuccessor(sinkMBB);
67106f32e7eSjoerg 
67206f32e7eSjoerg   BuildMI(BB, DL, TII->get(Opc2))
67306f32e7eSjoerg       .addReg(MI.getOperand(3).getReg())
67406f32e7eSjoerg       .addImm(MI.getOperand(4).getImm());
67506f32e7eSjoerg   BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
67606f32e7eSjoerg 
67706f32e7eSjoerg   //  copy0MBB:
67806f32e7eSjoerg   //   %FalseValue = ...
67906f32e7eSjoerg   //   # fallthrough to sinkMBB
68006f32e7eSjoerg   BB = copy0MBB;
68106f32e7eSjoerg 
68206f32e7eSjoerg   // Update machine-CFG edges
68306f32e7eSjoerg   BB->addSuccessor(sinkMBB);
68406f32e7eSjoerg 
68506f32e7eSjoerg   //  sinkMBB:
68606f32e7eSjoerg   //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
68706f32e7eSjoerg   //  ...
68806f32e7eSjoerg   BB = sinkMBB;
68906f32e7eSjoerg 
69006f32e7eSjoerg   BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
69106f32e7eSjoerg       .addReg(MI.getOperand(1).getReg())
69206f32e7eSjoerg       .addMBB(thisMBB)
69306f32e7eSjoerg       .addReg(MI.getOperand(2).getReg())
69406f32e7eSjoerg       .addMBB(copy0MBB);
69506f32e7eSjoerg 
69606f32e7eSjoerg   MI.eraseFromParent(); // The pseudo instruction is gone now.
69706f32e7eSjoerg   return BB;
69806f32e7eSjoerg 
69906f32e7eSjoerg }
70006f32e7eSjoerg 
70106f32e7eSjoerg MachineBasicBlock *
emitFEXT_T8I816_ins(unsigned BtOpc,unsigned CmpOpc,MachineInstr & MI,MachineBasicBlock * BB) const70206f32e7eSjoerg Mips16TargetLowering::emitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
70306f32e7eSjoerg                                           MachineInstr &MI,
70406f32e7eSjoerg                                           MachineBasicBlock *BB) const {
70506f32e7eSjoerg   if (DontExpandCondPseudos16)
70606f32e7eSjoerg     return BB;
70706f32e7eSjoerg   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
70806f32e7eSjoerg   Register regX = MI.getOperand(0).getReg();
70906f32e7eSjoerg   Register regY = MI.getOperand(1).getReg();
71006f32e7eSjoerg   MachineBasicBlock *target = MI.getOperand(2).getMBB();
71106f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(CmpOpc))
71206f32e7eSjoerg       .addReg(regX)
71306f32e7eSjoerg       .addReg(regY);
71406f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(BtOpc)).addMBB(target);
71506f32e7eSjoerg   MI.eraseFromParent(); // The pseudo instruction is gone now.
71606f32e7eSjoerg   return BB;
71706f32e7eSjoerg }
71806f32e7eSjoerg 
emitFEXT_T8I8I16_ins(unsigned BtOpc,unsigned CmpiOpc,unsigned CmpiXOpc,bool ImmSigned,MachineInstr & MI,MachineBasicBlock * BB) const71906f32e7eSjoerg MachineBasicBlock *Mips16TargetLowering::emitFEXT_T8I8I16_ins(
72006f32e7eSjoerg     unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc, bool ImmSigned,
72106f32e7eSjoerg     MachineInstr &MI, MachineBasicBlock *BB) const {
72206f32e7eSjoerg   if (DontExpandCondPseudos16)
72306f32e7eSjoerg     return BB;
72406f32e7eSjoerg   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
72506f32e7eSjoerg   Register regX = MI.getOperand(0).getReg();
72606f32e7eSjoerg   int64_t imm = MI.getOperand(1).getImm();
72706f32e7eSjoerg   MachineBasicBlock *target = MI.getOperand(2).getMBB();
72806f32e7eSjoerg   unsigned CmpOpc;
72906f32e7eSjoerg   if (isUInt<8>(imm))
73006f32e7eSjoerg     CmpOpc = CmpiOpc;
73106f32e7eSjoerg   else if ((!ImmSigned && isUInt<16>(imm)) ||
73206f32e7eSjoerg            (ImmSigned && isInt<16>(imm)))
73306f32e7eSjoerg     CmpOpc = CmpiXOpc;
73406f32e7eSjoerg   else
73506f32e7eSjoerg     llvm_unreachable("immediate field not usable");
73606f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(CmpOpc)).addReg(regX).addImm(imm);
73706f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(BtOpc)).addMBB(target);
73806f32e7eSjoerg   MI.eraseFromParent(); // The pseudo instruction is gone now.
73906f32e7eSjoerg   return BB;
74006f32e7eSjoerg }
74106f32e7eSjoerg 
Mips16WhichOp8uOr16simm(unsigned shortOp,unsigned longOp,int64_t Imm)74206f32e7eSjoerg static unsigned Mips16WhichOp8uOr16simm
74306f32e7eSjoerg   (unsigned shortOp, unsigned longOp, int64_t Imm) {
74406f32e7eSjoerg   if (isUInt<8>(Imm))
74506f32e7eSjoerg     return shortOp;
74606f32e7eSjoerg   else if (isInt<16>(Imm))
74706f32e7eSjoerg     return longOp;
74806f32e7eSjoerg   else
74906f32e7eSjoerg     llvm_unreachable("immediate field not usable");
75006f32e7eSjoerg }
75106f32e7eSjoerg 
75206f32e7eSjoerg MachineBasicBlock *
emitFEXT_CCRX16_ins(unsigned SltOpc,MachineInstr & MI,MachineBasicBlock * BB) const75306f32e7eSjoerg Mips16TargetLowering::emitFEXT_CCRX16_ins(unsigned SltOpc, MachineInstr &MI,
75406f32e7eSjoerg                                           MachineBasicBlock *BB) const {
75506f32e7eSjoerg   if (DontExpandCondPseudos16)
75606f32e7eSjoerg     return BB;
75706f32e7eSjoerg   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
75806f32e7eSjoerg   Register CC = MI.getOperand(0).getReg();
75906f32e7eSjoerg   Register regX = MI.getOperand(1).getReg();
76006f32e7eSjoerg   Register regY = MI.getOperand(2).getReg();
76106f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SltOpc))
76206f32e7eSjoerg       .addReg(regX)
76306f32e7eSjoerg       .addReg(regY);
76406f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(Mips::MoveR3216), CC)
76506f32e7eSjoerg       .addReg(Mips::T8);
76606f32e7eSjoerg   MI.eraseFromParent(); // The pseudo instruction is gone now.
76706f32e7eSjoerg   return BB;
76806f32e7eSjoerg }
76906f32e7eSjoerg 
77006f32e7eSjoerg MachineBasicBlock *
emitFEXT_CCRXI16_ins(unsigned SltiOpc,unsigned SltiXOpc,MachineInstr & MI,MachineBasicBlock * BB) const77106f32e7eSjoerg Mips16TargetLowering::emitFEXT_CCRXI16_ins(unsigned SltiOpc, unsigned SltiXOpc,
77206f32e7eSjoerg                                            MachineInstr &MI,
77306f32e7eSjoerg                                            MachineBasicBlock *BB) const {
77406f32e7eSjoerg   if (DontExpandCondPseudos16)
77506f32e7eSjoerg     return BB;
77606f32e7eSjoerg   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
77706f32e7eSjoerg   Register CC = MI.getOperand(0).getReg();
77806f32e7eSjoerg   Register regX = MI.getOperand(1).getReg();
77906f32e7eSjoerg   int64_t Imm = MI.getOperand(2).getImm();
78006f32e7eSjoerg   unsigned SltOpc = Mips16WhichOp8uOr16simm(SltiOpc, SltiXOpc, Imm);
78106f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SltOpc)).addReg(regX).addImm(Imm);
78206f32e7eSjoerg   BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(Mips::MoveR3216), CC)
78306f32e7eSjoerg       .addReg(Mips::T8);
78406f32e7eSjoerg   MI.eraseFromParent(); // The pseudo instruction is gone now.
78506f32e7eSjoerg   return BB;
78606f32e7eSjoerg 
78706f32e7eSjoerg }
788