Mips16ISelLowering.cpp revision b0ee97a366a07c05b2c8ab314e29f5e72a9b7bfb
1//===-- Mips16ISelLowering.h - Mips16 DAG Lowering Interface ----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips16.
11//
12//===----------------------------------------------------------------------===//
13#define DEBUG_TYPE "mips-lower"
14#include "Mips16ISelLowering.h"
15#include "MipsRegisterInfo.h"
16#include "MipsTargetMachine.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include <set>
22
23using namespace llvm;
24
25static cl::opt<bool> DontExpandCondPseudos16(
26  "mips16-dont-expand-cond-pseudo",
27  cl::init(false),
28  cl::desc("Dont expand conditional move related "
29           "pseudos for Mips 16"),
30  cl::Hidden);
31
32namespace {
33  std::set<const char*, MipsTargetLowering::LTStr> NoHelperNeeded;
34}
35
36Mips16TargetLowering::Mips16TargetLowering(MipsTargetMachine &TM)
37  : MipsTargetLowering(TM) {
38  //
39  // set up as if mips32 and then revert so we can test the mechanism
40  // for switching
41  addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
42  addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
43  computeRegisterProperties();
44  clearRegisterClasses();
45
46  // Set up the register classes
47  addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
48
49  if (Subtarget->inMips16HardFloat()) {
50    setMips16HardFloatLibCalls();
51    NoHelperNeeded.insert("__mips16_ret_sf");
52    NoHelperNeeded.insert("__mips16_ret_df");
53    NoHelperNeeded.insert("__mips16_ret_sc");
54    NoHelperNeeded.insert("__mips16_ret_dc");
55  }
56  setOperationAction(ISD::ATOMIC_FENCE,       MVT::Other, Expand);
57  setOperationAction(ISD::ATOMIC_CMP_SWAP,    MVT::i32,   Expand);
58  setOperationAction(ISD::ATOMIC_SWAP,        MVT::i32,   Expand);
59  setOperationAction(ISD::ATOMIC_LOAD_ADD,    MVT::i32,   Expand);
60  setOperationAction(ISD::ATOMIC_LOAD_SUB,    MVT::i32,   Expand);
61  setOperationAction(ISD::ATOMIC_LOAD_AND,    MVT::i32,   Expand);
62  setOperationAction(ISD::ATOMIC_LOAD_OR,     MVT::i32,   Expand);
63  setOperationAction(ISD::ATOMIC_LOAD_XOR,    MVT::i32,   Expand);
64  setOperationAction(ISD::ATOMIC_LOAD_NAND,   MVT::i32,   Expand);
65  setOperationAction(ISD::ATOMIC_LOAD_MIN,    MVT::i32,   Expand);
66  setOperationAction(ISD::ATOMIC_LOAD_MAX,    MVT::i32,   Expand);
67  setOperationAction(ISD::ATOMIC_LOAD_UMIN,   MVT::i32,   Expand);
68  setOperationAction(ISD::ATOMIC_LOAD_UMAX,   MVT::i32,   Expand);
69
70  computeRegisterProperties();
71}
72
73const MipsTargetLowering *
74llvm::createMips16TargetLowering(MipsTargetMachine &TM) {
75  return new Mips16TargetLowering(TM);
76}
77
78bool
79Mips16TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
80  return false;
81}
82
83MachineBasicBlock *
84Mips16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
85                                                  MachineBasicBlock *BB) const {
86  switch (MI->getOpcode()) {
87  default:
88    return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
89  case Mips::SelBeqZ:
90    return emitSel16(Mips::BeqzRxImm16, MI, BB);
91  case Mips::SelBneZ:
92    return emitSel16(Mips::BnezRxImm16, MI, BB);
93  case Mips::SelTBteqZCmpi:
94    return emitSeliT16(Mips::BteqzX16, Mips::CmpiRxImmX16, MI, BB);
95  case Mips::SelTBteqZSlti:
96    return emitSeliT16(Mips::BteqzX16, Mips::SltiRxImmX16, MI, BB);
97  case Mips::SelTBteqZSltiu:
98    return emitSeliT16(Mips::BteqzX16, Mips::SltiuRxImmX16, MI, BB);
99  case Mips::SelTBtneZCmpi:
100    return emitSeliT16(Mips::BtnezX16, Mips::CmpiRxImmX16, MI, BB);
101  case Mips::SelTBtneZSlti:
102    return emitSeliT16(Mips::BtnezX16, Mips::SltiRxImmX16, MI, BB);
103  case Mips::SelTBtneZSltiu:
104    return emitSeliT16(Mips::BtnezX16, Mips::SltiuRxImmX16, MI, BB);
105  case Mips::SelTBteqZCmp:
106    return emitSelT16(Mips::BteqzX16, Mips::CmpRxRy16, MI, BB);
107  case Mips::SelTBteqZSlt:
108    return emitSelT16(Mips::BteqzX16, Mips::SltRxRy16, MI, BB);
109  case Mips::SelTBteqZSltu:
110    return emitSelT16(Mips::BteqzX16, Mips::SltuRxRy16, MI, BB);
111  case Mips::SelTBtneZCmp:
112    return emitSelT16(Mips::BtnezX16, Mips::CmpRxRy16, MI, BB);
113  case Mips::SelTBtneZSlt:
114    return emitSelT16(Mips::BtnezX16, Mips::SltRxRy16, MI, BB);
115  case Mips::SelTBtneZSltu:
116    return emitSelT16(Mips::BtnezX16, Mips::SltuRxRy16, MI, BB);
117  case Mips::BteqzT8CmpX16:
118    return emitFEXT_T8I816_ins(Mips::BteqzX16, Mips::CmpRxRy16, MI, BB);
119  case Mips::BteqzT8SltX16:
120    return emitFEXT_T8I816_ins(Mips::BteqzX16, Mips::SltRxRy16, MI, BB);
121  case Mips::BteqzT8SltuX16:
122    // TBD: figure out a way to get this or remove the instruction
123    // altogether.
124    return emitFEXT_T8I816_ins(Mips::BteqzX16, Mips::SltuRxRy16, MI, BB);
125  case Mips::BtnezT8CmpX16:
126    return emitFEXT_T8I816_ins(Mips::BtnezX16, Mips::CmpRxRy16, MI, BB);
127  case Mips::BtnezT8SltX16:
128    return emitFEXT_T8I816_ins(Mips::BtnezX16, Mips::SltRxRy16, MI, BB);
129  case Mips::BtnezT8SltuX16:
130    // TBD: figure out a way to get this or remove the instruction
131    // altogether.
132    return emitFEXT_T8I816_ins(Mips::BtnezX16, Mips::SltuRxRy16, MI, BB);
133  case Mips::BteqzT8CmpiX16: return emitFEXT_T8I8I16_ins(
134    Mips::BteqzX16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
135  case Mips::BteqzT8SltiX16: return emitFEXT_T8I8I16_ins(
136    Mips::BteqzX16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
137  case Mips::BteqzT8SltiuX16: return emitFEXT_T8I8I16_ins(
138    Mips::BteqzX16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
139  case Mips::BtnezT8CmpiX16: return emitFEXT_T8I8I16_ins(
140    Mips::BtnezX16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
141  case Mips::BtnezT8SltiX16: return emitFEXT_T8I8I16_ins(
142    Mips::BtnezX16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
143  case Mips::BtnezT8SltiuX16: return emitFEXT_T8I8I16_ins(
144    Mips::BtnezX16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
145    break;
146  case Mips::SltCCRxRy16:
147    return emitFEXT_CCRX16_ins(Mips::SltRxRy16, MI, BB);
148    break;
149  case Mips::SltiCCRxImmX16:
150    return emitFEXT_CCRXI16_ins
151      (Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
152  case Mips::SltiuCCRxImmX16:
153    return emitFEXT_CCRXI16_ins
154      (Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
155  case Mips::SltuCCRxRy16:
156    return emitFEXT_CCRX16_ins
157      (Mips::SltuRxRy16, MI, BB);
158  }
159}
160
161bool Mips16TargetLowering::
162isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
163                                  unsigned NextStackOffset,
164                                  const MipsFunctionInfo& FI) const {
165  // No tail call optimization for mips16.
166  return false;
167}
168
169void Mips16TargetLowering::setMips16LibcallName
170  (RTLIB::Libcall L, const char *Name) {
171  setLibcallName(L, Name);
172  NoHelperNeeded.insert(Name);
173}
174
175void Mips16TargetLowering::setMips16HardFloatLibCalls() {
176  setMips16LibcallName(RTLIB::ADD_F32, "__mips16_addsf3");
177  setMips16LibcallName(RTLIB::ADD_F64, "__mips16_adddf3");
178  setMips16LibcallName(RTLIB::SUB_F32, "__mips16_subsf3");
179  setMips16LibcallName(RTLIB::SUB_F64, "__mips16_subdf3");
180  setMips16LibcallName(RTLIB::MUL_F32, "__mips16_mulsf3");
181  setMips16LibcallName(RTLIB::MUL_F64, "__mips16_muldf3");
182  setMips16LibcallName(RTLIB::DIV_F32, "__mips16_divsf3");
183  setMips16LibcallName(RTLIB::DIV_F64, "__mips16_divdf3");
184  setMips16LibcallName(RTLIB::FPEXT_F32_F64, "__mips16_extendsfdf2");
185  setMips16LibcallName(RTLIB::FPROUND_F64_F32, "__mips16_truncdfsf2");
186  setMips16LibcallName(RTLIB::FPTOSINT_F32_I32, "__mips16_fix_truncsfsi");
187  setMips16LibcallName(RTLIB::FPTOSINT_F64_I32, "__mips16_fix_truncdfsi");
188  setMips16LibcallName(RTLIB::SINTTOFP_I32_F32, "__mips16_floatsisf");
189  setMips16LibcallName(RTLIB::SINTTOFP_I32_F64, "__mips16_floatsidf");
190  setMips16LibcallName(RTLIB::UINTTOFP_I32_F32, "__mips16_floatunsisf");
191  setMips16LibcallName(RTLIB::UINTTOFP_I32_F64, "__mips16_floatunsidf");
192  setMips16LibcallName(RTLIB::OEQ_F32, "__mips16_eqsf2");
193  setMips16LibcallName(RTLIB::OEQ_F64, "__mips16_eqdf2");
194  setMips16LibcallName(RTLIB::UNE_F32, "__mips16_nesf2");
195  setMips16LibcallName(RTLIB::UNE_F64, "__mips16_nedf2");
196  setMips16LibcallName(RTLIB::OGE_F32, "__mips16_gesf2");
197  setMips16LibcallName(RTLIB::OGE_F64, "__mips16_gedf2");
198  setMips16LibcallName(RTLIB::OLT_F32, "__mips16_ltsf2");
199  setMips16LibcallName(RTLIB::OLT_F64, "__mips16_ltdf2");
200  setMips16LibcallName(RTLIB::OLE_F32, "__mips16_lesf2");
201  setMips16LibcallName(RTLIB::OLE_F64, "__mips16_ledf2");
202  setMips16LibcallName(RTLIB::OGT_F32, "__mips16_gtsf2");
203  setMips16LibcallName(RTLIB::OGT_F64, "__mips16_gtdf2");
204  setMips16LibcallName(RTLIB::UO_F32, "__mips16_unordsf2");
205  setMips16LibcallName(RTLIB::UO_F64, "__mips16_unorddf2");
206  setMips16LibcallName(RTLIB::O_F32, "__mips16_unordsf2");
207  setMips16LibcallName(RTLIB::O_F64, "__mips16_unorddf2");
208}
209
210
211//
212// The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
213// cleaner way to do all of this but it will have to wait until the traditional
214// gcc mechanism is completed.
215//
216// For Pic, in order for Mips16 code to call Mips32 code which according the abi
217// have either arguments or returned values placed in floating point registers,
218// we use a set of helper functions. (This includes functions which return type
219//  complex which on Mips are returned in a pair of floating point registers).
220//
221// This is an encoding that we inherited from gcc.
222// In Mips traditional O32, N32 ABI, floating point numbers are passed in
223// floating point argument registers 1,2 only when the first and optionally
224// the second arguments are float (sf) or double (df).
225// For Mips16 we are only concerned with the situations where floating point
226// arguments are being passed in floating point registers by the ABI, because
227// Mips16 mode code cannot execute floating point instructions to load those
228// values and hence helper functions are needed.
229// The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
230// the helper function suffixs for these are:
231//                        0,  1,    5,        9,         2,   6,        10
232// this suffix can then be calculated as follows:
233// for a given argument Arg:
234//     Arg1x, Arg2x = 1 :  Arg is sf
235//                    2 :  Arg is df
236//                    0:   Arg is neither sf or df
237// So this stub is the string for number Arg1x + Arg2x*4.
238// However not all numbers between 0 and 10 are possible, we check anyway and
239// assert if the impossible exists.
240//
241
242unsigned int Mips16TargetLowering::getMips16HelperFunctionStubNumber
243  (ArgListTy &Args) const {
244  unsigned int resultNum = 0;
245  if (Args.size() >= 1) {
246    Type *t = Args[0].Ty;
247    if (t->isFloatTy()) {
248      resultNum = 1;
249    }
250    else if (t->isDoubleTy()) {
251      resultNum = 2;
252    }
253  }
254  if (resultNum) {
255    if (Args.size() >=2) {
256      Type *t = Args[1].Ty;
257      if (t->isFloatTy()) {
258        resultNum += 4;
259      }
260      else if (t->isDoubleTy()) {
261        resultNum += 8;
262      }
263    }
264  }
265  return resultNum;
266}
267
268//
269// prefixs are attached to stub numbers depending on the return type .
270// return type: float  sf_
271//              double df_
272//              single complex sc_
273//              double complext dc_
274//              others  NO PREFIX
275//
276//
277// The full name of a helper function is__mips16_call_stub +
278//    return type dependent prefix + stub number
279//
280//
281// This is something that probably should be in a different source file and
282// perhaps done differently but my main purpose is to not waste runtime
283// on something that we can enumerate in the source. Another possibility is
284// to have a python script to generate these mapping tables. This will do
285// for now. There are a whole series of helper function mapping arrays, one
286// for each return type class as outlined above. There there are 11 possible
287//  entries. Ones with 0 are ones which should never be selected
288//
289// All the arrays are similar except for ones which return neither
290// sf, df, sc, dc, in which only care about ones which have sf or df as a
291// first parameter.
292//
293#define P_ "__mips16_call_stub_"
294#define MAX_STUB_NUMBER 10
295#define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
296#define T P "0" , T1
297#define P P_
298static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
299  {0, T1 };
300#undef P
301#define P P_ "sf_"
302static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
303  { T };
304#undef P
305#define P P_ "df_"
306static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
307  { T };
308#undef P
309#define P P_ "sc_"
310static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
311  { T };
312#undef P
313#define P P_ "dc_"
314static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
315  { T };
316#undef P
317#undef P_
318
319
320const char* Mips16TargetLowering::
321  getMips16HelperFunction
322    (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
323  const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
324#ifndef NDEBUG
325  const unsigned int maxStubNum = 10;
326  assert(stubNum <= maxStubNum);
327  const bool validStubNum[maxStubNum+1] =
328    {true, true, true, false, false, true, true, false, false, true, true};
329  assert(validStubNum[stubNum]);
330#endif
331  const char *result;
332  if (RetTy->isFloatTy()) {
333    result = sfMips16Helper[stubNum];
334  }
335  else if (RetTy ->isDoubleTy()) {
336    result = dfMips16Helper[stubNum];
337  }
338  else if (RetTy->isStructTy()) {
339    // check if it's complex
340    if (RetTy->getNumContainedTypes() == 2) {
341      if ((RetTy->getContainedType(0)->isFloatTy()) &&
342          (RetTy->getContainedType(1)->isFloatTy())) {
343        result = scMips16Helper[stubNum];
344      }
345      else if ((RetTy->getContainedType(0)->isDoubleTy()) &&
346               (RetTy->getContainedType(1)->isDoubleTy())) {
347        result = dcMips16Helper[stubNum];
348      }
349      else {
350        llvm_unreachable("Uncovered condition");
351      }
352    }
353    else {
354      llvm_unreachable("Uncovered condition");
355    }
356  }
357  else {
358    if (stubNum == 0) {
359      needHelper = false;
360      return "";
361    }
362    result = vMips16Helper[stubNum];
363  }
364  needHelper = true;
365  return result;
366}
367
368void Mips16TargetLowering::
369getOpndList(SmallVectorImpl<SDValue> &Ops,
370            std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
371            bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
372            CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
373  SelectionDAG &DAG = CLI.DAG;
374  const char* Mips16HelperFunction = 0;
375  bool NeedMips16Helper = false;
376
377  if (getTargetMachine().Options.UseSoftFloat &&
378      Subtarget->inMips16HardFloat()) {
379    //
380    // currently we don't have symbols tagged with the mips16 or mips32
381    // qualifier so we will assume that we don't know what kind it is.
382    // and generate the helper
383    //
384    bool LookupHelper = true;
385    if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(CLI.Callee)) {
386      if (NoHelperNeeded.find(S->getSymbol()) != NoHelperNeeded.end()) {
387        LookupHelper = false;
388      }
389    }
390    else if (GlobalAddressSDNode *G =
391             dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
392      if (NoHelperNeeded.find(G->getGlobal()->getName().data()) !=
393                              NoHelperNeeded.end()) {
394        LookupHelper = false;
395      }
396    }
397    if (LookupHelper) Mips16HelperFunction =
398      getMips16HelperFunction(CLI.RetTy, CLI.Args, NeedMips16Helper);
399
400  }
401
402  SDValue JumpTarget = Callee;
403
404  // T9 should contain the address of the callee function if
405  // -reloction-model=pic or it is an indirect call.
406  if (IsPICCall || !GlobalOrExternal) {
407    unsigned V0Reg = Mips::V0;
408    if (NeedMips16Helper) {
409      RegsToPass.push_front(std::make_pair(V0Reg, Callee));
410      JumpTarget = DAG.getExternalSymbol(Mips16HelperFunction, getPointerTy());
411      JumpTarget = getAddrGlobal(JumpTarget, DAG, MipsII::MO_GOT);
412    } else
413      RegsToPass.push_front(std::make_pair((unsigned)Mips::T9, Callee));
414  }
415
416  Ops.push_back(JumpTarget);
417
418  MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
419                                  InternalLinkage, CLI, Callee, Chain);
420}
421
422MachineBasicBlock *Mips16TargetLowering::
423emitSel16(unsigned Opc, MachineInstr *MI, MachineBasicBlock *BB) const {
424  if (DontExpandCondPseudos16)
425    return BB;
426  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
427  DebugLoc DL = MI->getDebugLoc();
428  // To "insert" a SELECT_CC instruction, we actually have to insert the
429  // diamond control-flow pattern.  The incoming instruction knows the
430  // destination vreg to set, the condition code register to branch on, the
431  // true/false values to select between, and a branch opcode to use.
432  const BasicBlock *LLVM_BB = BB->getBasicBlock();
433  MachineFunction::iterator It = BB;
434  ++It;
435
436  //  thisMBB:
437  //  ...
438  //   TrueVal = ...
439  //   setcc r1, r2, r3
440  //   bNE   r1, r0, copy1MBB
441  //   fallthrough --> copy0MBB
442  MachineBasicBlock *thisMBB  = BB;
443  MachineFunction *F = BB->getParent();
444  MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
445  MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
446  F->insert(It, copy0MBB);
447  F->insert(It, sinkMBB);
448
449  // Transfer the remainder of BB and its successor edges to sinkMBB.
450  sinkMBB->splice(sinkMBB->begin(), BB,
451                  llvm::next(MachineBasicBlock::iterator(MI)),
452                  BB->end());
453  sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
454
455  // Next, add the true and fallthrough blocks as its successors.
456  BB->addSuccessor(copy0MBB);
457  BB->addSuccessor(sinkMBB);
458
459  BuildMI(BB, DL, TII->get(Opc)).addReg(MI->getOperand(3).getReg())
460    .addMBB(sinkMBB);
461
462  //  copy0MBB:
463  //   %FalseValue = ...
464  //   # fallthrough to sinkMBB
465  BB = copy0MBB;
466
467  // Update machine-CFG edges
468  BB->addSuccessor(sinkMBB);
469
470  //  sinkMBB:
471  //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
472  //  ...
473  BB = sinkMBB;
474
475  BuildMI(*BB, BB->begin(), DL,
476          TII->get(Mips::PHI), MI->getOperand(0).getReg())
477    .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
478    .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
479
480  MI->eraseFromParent();   // The pseudo instruction is gone now.
481  return BB;
482}
483
484MachineBasicBlock *Mips16TargetLowering::emitSelT16
485  (unsigned Opc1, unsigned Opc2,
486   MachineInstr *MI, MachineBasicBlock *BB) const {
487  if (DontExpandCondPseudos16)
488    return BB;
489  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
490  DebugLoc DL = MI->getDebugLoc();
491  // To "insert" a SELECT_CC instruction, we actually have to insert the
492  // diamond control-flow pattern.  The incoming instruction knows the
493  // destination vreg to set, the condition code register to branch on, the
494  // true/false values to select between, and a branch opcode to use.
495  const BasicBlock *LLVM_BB = BB->getBasicBlock();
496  MachineFunction::iterator It = BB;
497  ++It;
498
499  //  thisMBB:
500  //  ...
501  //   TrueVal = ...
502  //   setcc r1, r2, r3
503  //   bNE   r1, r0, copy1MBB
504  //   fallthrough --> copy0MBB
505  MachineBasicBlock *thisMBB  = BB;
506  MachineFunction *F = BB->getParent();
507  MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
508  MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
509  F->insert(It, copy0MBB);
510  F->insert(It, sinkMBB);
511
512  // Transfer the remainder of BB and its successor edges to sinkMBB.
513  sinkMBB->splice(sinkMBB->begin(), BB,
514                  llvm::next(MachineBasicBlock::iterator(MI)),
515                  BB->end());
516  sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
517
518  // Next, add the true and fallthrough blocks as its successors.
519  BB->addSuccessor(copy0MBB);
520  BB->addSuccessor(sinkMBB);
521
522  BuildMI(BB, DL, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
523    .addReg(MI->getOperand(4).getReg());
524  BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
525
526  //  copy0MBB:
527  //   %FalseValue = ...
528  //   # fallthrough to sinkMBB
529  BB = copy0MBB;
530
531  // Update machine-CFG edges
532  BB->addSuccessor(sinkMBB);
533
534  //  sinkMBB:
535  //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
536  //  ...
537  BB = sinkMBB;
538
539  BuildMI(*BB, BB->begin(), DL,
540          TII->get(Mips::PHI), MI->getOperand(0).getReg())
541    .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
542    .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
543
544  MI->eraseFromParent();   // The pseudo instruction is gone now.
545  return BB;
546
547}
548
549MachineBasicBlock *Mips16TargetLowering::emitSeliT16
550  (unsigned Opc1, unsigned Opc2,
551   MachineInstr *MI, MachineBasicBlock *BB) const {
552  if (DontExpandCondPseudos16)
553    return BB;
554  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
555  DebugLoc DL = MI->getDebugLoc();
556  // To "insert" a SELECT_CC instruction, we actually have to insert the
557  // diamond control-flow pattern.  The incoming instruction knows the
558  // destination vreg to set, the condition code register to branch on, the
559  // true/false values to select between, and a branch opcode to use.
560  const BasicBlock *LLVM_BB = BB->getBasicBlock();
561  MachineFunction::iterator It = BB;
562  ++It;
563
564  //  thisMBB:
565  //  ...
566  //   TrueVal = ...
567  //   setcc r1, r2, r3
568  //   bNE   r1, r0, copy1MBB
569  //   fallthrough --> copy0MBB
570  MachineBasicBlock *thisMBB  = BB;
571  MachineFunction *F = BB->getParent();
572  MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
573  MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
574  F->insert(It, copy0MBB);
575  F->insert(It, sinkMBB);
576
577  // Transfer the remainder of BB and its successor edges to sinkMBB.
578  sinkMBB->splice(sinkMBB->begin(), BB,
579                  llvm::next(MachineBasicBlock::iterator(MI)),
580                  BB->end());
581  sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
582
583  // Next, add the true and fallthrough blocks as its successors.
584  BB->addSuccessor(copy0MBB);
585  BB->addSuccessor(sinkMBB);
586
587  BuildMI(BB, DL, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
588    .addImm(MI->getOperand(4).getImm());
589  BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
590
591  //  copy0MBB:
592  //   %FalseValue = ...
593  //   # fallthrough to sinkMBB
594  BB = copy0MBB;
595
596  // Update machine-CFG edges
597  BB->addSuccessor(sinkMBB);
598
599  //  sinkMBB:
600  //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
601  //  ...
602  BB = sinkMBB;
603
604  BuildMI(*BB, BB->begin(), DL,
605          TII->get(Mips::PHI), MI->getOperand(0).getReg())
606    .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
607    .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
608
609  MI->eraseFromParent();   // The pseudo instruction is gone now.
610  return BB;
611
612}
613
614MachineBasicBlock
615  *Mips16TargetLowering::emitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
616                                             MachineInstr *MI,
617                                             MachineBasicBlock *BB) const {
618  if (DontExpandCondPseudos16)
619    return BB;
620  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
621  unsigned regX = MI->getOperand(0).getReg();
622  unsigned regY = MI->getOperand(1).getReg();
623  MachineBasicBlock *target = MI->getOperand(2).getMBB();
624  BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX)
625    .addReg(regY);
626  BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
627  MI->eraseFromParent();   // The pseudo instruction is gone now.
628  return BB;
629}
630
631MachineBasicBlock *Mips16TargetLowering::emitFEXT_T8I8I16_ins(
632  unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc, bool ImmSigned,
633  MachineInstr *MI,  MachineBasicBlock *BB) const {
634  if (DontExpandCondPseudos16)
635    return BB;
636  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
637  unsigned regX = MI->getOperand(0).getReg();
638  int64_t imm = MI->getOperand(1).getImm();
639  MachineBasicBlock *target = MI->getOperand(2).getMBB();
640  unsigned CmpOpc;
641  if (isUInt<8>(imm))
642    CmpOpc = CmpiOpc;
643  else if ((!ImmSigned && isUInt<16>(imm)) ||
644           (ImmSigned && isInt<16>(imm)))
645    CmpOpc = CmpiXOpc;
646  else
647    llvm_unreachable("immediate field not usable");
648  BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX)
649    .addImm(imm);
650  BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
651  MI->eraseFromParent();   // The pseudo instruction is gone now.
652  return BB;
653}
654
655static unsigned Mips16WhichOp8uOr16simm
656  (unsigned shortOp, unsigned longOp, int64_t Imm) {
657  if (isUInt<8>(Imm))
658    return shortOp;
659  else if (isInt<16>(Imm))
660    return longOp;
661  else
662    llvm_unreachable("immediate field not usable");
663}
664
665MachineBasicBlock *Mips16TargetLowering::emitFEXT_CCRX16_ins(
666  unsigned SltOpc,
667  MachineInstr *MI,  MachineBasicBlock *BB) const {
668  if (DontExpandCondPseudos16)
669    return BB;
670  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
671  unsigned CC = MI->getOperand(0).getReg();
672  unsigned regX = MI->getOperand(1).getReg();
673  unsigned regY = MI->getOperand(2).getReg();
674  BuildMI(*BB, MI, MI->getDebugLoc(),
675		  TII->get(SltOpc)).addReg(regX).addReg(regY);
676  BuildMI(*BB, MI, MI->getDebugLoc(),
677          TII->get(Mips::MoveR3216), CC).addReg(Mips::T8);
678  MI->eraseFromParent();   // The pseudo instruction is gone now.
679  return BB;
680}
681
682MachineBasicBlock *Mips16TargetLowering::emitFEXT_CCRXI16_ins(
683  unsigned SltiOpc, unsigned SltiXOpc,
684  MachineInstr *MI,  MachineBasicBlock *BB )const {
685  if (DontExpandCondPseudos16)
686    return BB;
687  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
688  unsigned CC = MI->getOperand(0).getReg();
689  unsigned regX = MI->getOperand(1).getReg();
690  int64_t Imm = MI->getOperand(2).getImm();
691  unsigned SltOpc = Mips16WhichOp8uOr16simm(SltiOpc, SltiXOpc, Imm);
692  BuildMI(*BB, MI, MI->getDebugLoc(),
693          TII->get(SltOpc)).addReg(regX).addImm(Imm);
694  BuildMI(*BB, MI, MI->getDebugLoc(),
695          TII->get(Mips::MoveR3216), CC).addReg(Mips::T8);
696  MI->eraseFromParent();   // The pseudo instruction is gone now.
697  return BB;
698
699}
700