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