1//===-- Mips16ISelLowering.h - Mips16 DAG Lowering Interface ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Subclass of MipsTargetLowering specialized for mips16.
10//
11//===----------------------------------------------------------------------===//
12#include "Mips16ISelLowering.h"
13#include "MCTargetDesc/MipsBaseInfo.h"
14#include "Mips16HardFloatInfo.h"
15#include "MipsMachineFunction.h"
16#include "MipsRegisterInfo.h"
17#include "MipsTargetMachine.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/TargetInstrInfo.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "mips-lower"
24
25namespace {
26struct Mips16IntrinsicHelperType{
27 const char* Name;
28 const char* Helper;
29
30 bool operator<(const Mips16IntrinsicHelperType &RHS) const {
31 return std::strcmp(s1: Name, s2: RHS.Name) < 0;
32 }
33 bool operator==(const Mips16IntrinsicHelperType &RHS) const {
34 return std::strcmp(s1: Name, s2: RHS.Name) == 0;
35 }
36};
37} // namespace
38
39static const Mips16IntrinsicHelperType Mips16IntrinsicHelper[] = {
40 {.Name: "__fixunsdfsi", .Helper: "__mips16_call_stub_2" },
41 {.Name: "ceil", .Helper: "__mips16_call_stub_df_2"},
42 {.Name: "ceilf", .Helper: "__mips16_call_stub_sf_1"},
43 {.Name: "copysign", .Helper: "__mips16_call_stub_df_10"},
44 {.Name: "copysignf", .Helper: "__mips16_call_stub_sf_5"},
45 {.Name: "cos", .Helper: "__mips16_call_stub_df_2"},
46 {.Name: "cosf", .Helper: "__mips16_call_stub_sf_1"},
47 {.Name: "exp2", .Helper: "__mips16_call_stub_df_2"},
48 {.Name: "exp2f", .Helper: "__mips16_call_stub_sf_1"},
49 {.Name: "floor", .Helper: "__mips16_call_stub_df_2"},
50 {.Name: "floorf", .Helper: "__mips16_call_stub_sf_1"},
51 {.Name: "log2", .Helper: "__mips16_call_stub_df_2"},
52 {.Name: "log2f", .Helper: "__mips16_call_stub_sf_1"},
53 {.Name: "nearbyint", .Helper: "__mips16_call_stub_df_2"},
54 {.Name: "nearbyintf", .Helper: "__mips16_call_stub_sf_1"},
55 {.Name: "rint", .Helper: "__mips16_call_stub_df_2"},
56 {.Name: "rintf", .Helper: "__mips16_call_stub_sf_1"},
57 {.Name: "sin", .Helper: "__mips16_call_stub_df_2"},
58 {.Name: "sinf", .Helper: "__mips16_call_stub_sf_1"},
59 {.Name: "sqrt", .Helper: "__mips16_call_stub_df_2"},
60 {.Name: "sqrtf", .Helper: "__mips16_call_stub_sf_1"},
61 {.Name: "trunc", .Helper: "__mips16_call_stub_df_2"},
62 {.Name: "truncf", .Helper: "__mips16_call_stub_sf_1"},
63};
64
65Mips16TargetLowering::Mips16TargetLowering(const MipsTargetMachine &TM,
66 const MipsSubtarget &STI)
67 : MipsTargetLowering(TM, STI) {
68
69 // Set up the register classes
70 addRegisterClass(VT: MVT::i32, RC: &Mips::CPU16RegsRegClass);
71
72 setOperationAction(Op: ISD::ATOMIC_FENCE, VT: MVT::Other, Action: LibCall);
73 setOperationAction(
74 Ops: {ISD::ATOMIC_CMP_SWAP, ISD::ATOMIC_SWAP, ISD::ATOMIC_LOAD_ADD,
75 ISD::ATOMIC_LOAD_SUB, ISD::ATOMIC_LOAD_AND, ISD::ATOMIC_LOAD_OR,
76 ISD::ATOMIC_LOAD_XOR, ISD::ATOMIC_LOAD_NAND, ISD::ATOMIC_LOAD_MIN,
77 ISD::ATOMIC_LOAD_MAX, ISD::ATOMIC_LOAD_UMIN, ISD::ATOMIC_LOAD_UMAX},
78 VTs: {MVT::i8, MVT::i16, MVT::i32}, Action: LibCall);
79
80 setOperationAction(Op: ISD::ROTR, VT: MVT::i32, Action: Expand);
81 setOperationAction(Op: ISD::ROTR, VT: MVT::i64, Action: Expand);
82 setOperationAction(Op: ISD::BSWAP, VT: MVT::i32, Action: Expand);
83 setOperationAction(Op: ISD::BSWAP, VT: MVT::i64, Action: Expand);
84
85 computeRegisterProperties(TRI: STI.getRegisterInfo());
86}
87
88const MipsTargetLowering *
89llvm::createMips16TargetLowering(const MipsTargetMachine &TM,
90 const MipsSubtarget &STI) {
91 return new Mips16TargetLowering(TM, STI);
92}
93
94bool Mips16TargetLowering::allowsMisalignedMemoryAccesses(
95 EVT VT, unsigned, Align, MachineMemOperand::Flags, unsigned *Fast) const {
96 return false;
97}
98
99MachineBasicBlock *
100Mips16TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
101 MachineBasicBlock *BB) const {
102 switch (MI.getOpcode()) {
103 default:
104 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, MBB: BB);
105 case Mips::SelBeqZ:
106 return emitSel16(Opc: Mips::BeqzRxImm16, MI, BB);
107 case Mips::SelBneZ:
108 return emitSel16(Opc: Mips::BnezRxImm16, MI, BB);
109 case Mips::SelTBteqZCmpi:
110 return emitSeliT16(Opc1: Mips::Bteqz16, Opc2: Mips::CmpiRxImmX16, MI, BB);
111 case Mips::SelTBteqZSlti:
112 return emitSeliT16(Opc1: Mips::Bteqz16, Opc2: Mips::SltiRxImmX16, MI, BB);
113 case Mips::SelTBteqZSltiu:
114 return emitSeliT16(Opc1: Mips::Bteqz16, Opc2: Mips::SltiuRxImmX16, MI, BB);
115 case Mips::SelTBtneZCmpi:
116 return emitSeliT16(Opc1: Mips::Btnez16, Opc2: Mips::CmpiRxImmX16, MI, BB);
117 case Mips::SelTBtneZSlti:
118 return emitSeliT16(Opc1: Mips::Btnez16, Opc2: Mips::SltiRxImmX16, MI, BB);
119 case Mips::SelTBtneZSltiu:
120 return emitSeliT16(Opc1: Mips::Btnez16, Opc2: Mips::SltiuRxImmX16, MI, BB);
121 case Mips::SelTBteqZCmp:
122 return emitSelT16(Opc1: Mips::Bteqz16, Opc2: Mips::CmpRxRy16, MI, BB);
123 case Mips::SelTBteqZSlt:
124 return emitSelT16(Opc1: Mips::Bteqz16, Opc2: Mips::SltRxRy16, MI, BB);
125 case Mips::SelTBteqZSltu:
126 return emitSelT16(Opc1: Mips::Bteqz16, Opc2: Mips::SltuRxRy16, MI, BB);
127 case Mips::SelTBtneZCmp:
128 return emitSelT16(Opc1: Mips::Btnez16, Opc2: Mips::CmpRxRy16, MI, BB);
129 case Mips::SelTBtneZSlt:
130 return emitSelT16(Opc1: Mips::Btnez16, Opc2: Mips::SltRxRy16, MI, BB);
131 case Mips::SelTBtneZSltu:
132 return emitSelT16(Opc1: Mips::Btnez16, Opc2: Mips::SltuRxRy16, MI, BB);
133 case Mips::BteqzT8CmpX16:
134 return emitFEXT_T8I816_ins(BtOpc: Mips::Bteqz16, CmpOpc: Mips::CmpRxRy16, MI, BB);
135 case Mips::BteqzT8SltX16:
136 return emitFEXT_T8I816_ins(BtOpc: Mips::Bteqz16, CmpOpc: Mips::SltRxRy16, MI, BB);
137 case Mips::BteqzT8SltuX16:
138 // TBD: figure out a way to get this or remove the instruction
139 // altogether.
140 return emitFEXT_T8I816_ins(BtOpc: Mips::Bteqz16, CmpOpc: Mips::SltuRxRy16, MI, BB);
141 case Mips::BtnezT8CmpX16:
142 return emitFEXT_T8I816_ins(BtOpc: Mips::Btnez16, CmpOpc: Mips::CmpRxRy16, MI, BB);
143 case Mips::BtnezT8SltX16:
144 return emitFEXT_T8I816_ins(BtOpc: Mips::Btnez16, CmpOpc: Mips::SltRxRy16, MI, BB);
145 case Mips::BtnezT8SltuX16:
146 // TBD: figure out a way to get this or remove the instruction
147 // altogether.
148 return emitFEXT_T8I816_ins(BtOpc: Mips::Btnez16, CmpOpc: Mips::SltuRxRy16, MI, BB);
149 case Mips::BteqzT8CmpiX16: return emitFEXT_T8I8I16_ins(
150 BtOpc: Mips::Bteqz16, CmpiOpc: Mips::CmpiRxImm16, CmpiXOpc: Mips::CmpiRxImmX16, ImmSigned: false, MI, BB);
151 case Mips::BteqzT8SltiX16: return emitFEXT_T8I8I16_ins(
152 BtOpc: Mips::Bteqz16, CmpiOpc: Mips::SltiRxImm16, CmpiXOpc: Mips::SltiRxImmX16, ImmSigned: true, MI, BB);
153 case Mips::BteqzT8SltiuX16: return emitFEXT_T8I8I16_ins(
154 BtOpc: Mips::Bteqz16, CmpiOpc: Mips::SltiuRxImm16, CmpiXOpc: Mips::SltiuRxImmX16, ImmSigned: false, MI, BB);
155 case Mips::BtnezT8CmpiX16: return emitFEXT_T8I8I16_ins(
156 BtOpc: Mips::Btnez16, CmpiOpc: Mips::CmpiRxImm16, CmpiXOpc: Mips::CmpiRxImmX16, ImmSigned: false, MI, BB);
157 case Mips::BtnezT8SltiX16: return emitFEXT_T8I8I16_ins(
158 BtOpc: Mips::Btnez16, CmpiOpc: Mips::SltiRxImm16, CmpiXOpc: Mips::SltiRxImmX16, ImmSigned: true, MI, BB);
159 case Mips::BtnezT8SltiuX16: return emitFEXT_T8I8I16_ins(
160 BtOpc: Mips::Btnez16, CmpiOpc: Mips::SltiuRxImm16, CmpiXOpc: Mips::SltiuRxImmX16, ImmSigned: false, MI, BB);
161 break;
162 case Mips::SltCCRxRy16:
163 return emitFEXT_CCRX16_ins(SltOpc: Mips::SltRxRy16, MI, BB);
164 break;
165 case Mips::SltiCCRxImmX16:
166 return emitFEXT_CCRXI16_ins
167 (SltiOpc: Mips::SltiRxImm16, SltiXOpc: Mips::SltiRxImmX16, MI, BB);
168 case Mips::SltiuCCRxImmX16:
169 return emitFEXT_CCRXI16_ins
170 (SltiOpc: Mips::SltiuRxImm16, SltiXOpc: Mips::SltiuRxImmX16, MI, BB);
171 case Mips::SltuCCRxRy16:
172 return emitFEXT_CCRX16_ins
173 (SltOpc: Mips::SltuRxRy16, MI, BB);
174 }
175}
176
177bool Mips16TargetLowering::isEligibleForTailCallOptimization(
178 const CCState &CCInfo, unsigned NextStackOffset,
179 const MipsFunctionInfo &FI) const {
180 // No tail call optimization for mips16.
181 return false;
182}
183
184//
185// The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
186// cleaner way to do all of this but it will have to wait until the traditional
187// gcc mechanism is completed.
188//
189// For Pic, in order for Mips16 code to call Mips32 code which according the abi
190// have either arguments or returned values placed in floating point registers,
191// we use a set of helper functions. (This includes functions which return type
192// complex which on Mips are returned in a pair of floating point registers).
193//
194// This is an encoding that we inherited from gcc.
195// In Mips traditional O32, N32 ABI, floating point numbers are passed in
196// floating point argument registers 1,2 only when the first and optionally
197// the second arguments are float (sf) or double (df).
198// For Mips16 we are only concerned with the situations where floating point
199// arguments are being passed in floating point registers by the ABI, because
200// Mips16 mode code cannot execute floating point instructions to load those
201// values and hence helper functions are needed.
202// The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
203// the helper function suffixs for these are:
204// 0, 1, 5, 9, 2, 6, 10
205// this suffix can then be calculated as follows:
206// for a given argument Arg:
207// Arg1x, Arg2x = 1 : Arg is sf
208// 2 : Arg is df
209// 0: Arg is neither sf or df
210// So this stub is the string for number Arg1x + Arg2x*4.
211// However not all numbers between 0 and 10 are possible, we check anyway and
212// assert if the impossible exists.
213//
214
215unsigned int Mips16TargetLowering::getMips16HelperFunctionStubNumber
216 (ArgListTy &Args) const {
217 unsigned int resultNum = 0;
218 if (Args.size() >= 1) {
219 Type *t = Args[0].Ty;
220 if (t->isFloatTy()) {
221 resultNum = 1;
222 }
223 else if (t->isDoubleTy()) {
224 resultNum = 2;
225 }
226 }
227 if (resultNum) {
228 if (Args.size() >=2) {
229 Type *t = Args[1].Ty;
230 if (t->isFloatTy()) {
231 resultNum += 4;
232 }
233 else if (t->isDoubleTy()) {
234 resultNum += 8;
235 }
236 }
237 }
238 return resultNum;
239}
240
241//
242// Prefixes are attached to stub numbers depending on the return type.
243// return type: float sf_
244// double df_
245// single complex sc_
246// double complext dc_
247// others NO PREFIX
248//
249//
250// The full name of a helper function is__mips16_call_stub +
251// return type dependent prefix + stub number
252//
253// FIXME: This is something that probably should be in a different source file
254// and perhaps done differently but my main purpose is to not waste runtime
255// on something that we can enumerate in the source. Another possibility is
256// to have a python script to generate these mapping tables. This will do
257// for now. There are a whole series of helper function mapping arrays, one
258// for each return type class as outlined above. There there are 11 possible
259// entries. Ones with 0 are ones which should never be selected.
260//
261// All the arrays are similar except for ones which return neither
262// sf, df, sc, dc, in which we only care about ones which have sf or df as a
263// first parameter.
264//
265#define P_ "__mips16_call_stub_"
266#define MAX_STUB_NUMBER 10
267#define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
268#define T P "0" , T1
269#define P P_
270static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
271 {nullptr, T1 };
272#undef P
273#define P P_ "sf_"
274static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
275 { T };
276#undef P
277#define P P_ "df_"
278static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
279 { T };
280#undef P
281#define P P_ "sc_"
282static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
283 { T };
284#undef P
285#define P P_ "dc_"
286static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
287 { T };
288#undef P
289#undef P_
290
291
292const char* Mips16TargetLowering::
293 getMips16HelperFunction
294 (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
295 const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
296#ifndef NDEBUG
297 const unsigned int maxStubNum = 10;
298 assert(stubNum <= maxStubNum);
299 const bool validStubNum[maxStubNum+1] =
300 {true, true, true, false, false, true, true, false, false, true, true};
301 assert(validStubNum[stubNum]);
302#endif
303 const char *result;
304 if (RetTy->isFloatTy()) {
305 result = sfMips16Helper[stubNum];
306 }
307 else if (RetTy ->isDoubleTy()) {
308 result = dfMips16Helper[stubNum];
309 } else if (StructType *SRetTy = dyn_cast<StructType>(Val: RetTy)) {
310 // check if it's complex
311 if (SRetTy->getNumElements() == 2) {
312 if ((SRetTy->getElementType(N: 0)->isFloatTy()) &&
313 (SRetTy->getElementType(N: 1)->isFloatTy())) {
314 result = scMips16Helper[stubNum];
315 } else if ((SRetTy->getElementType(N: 0)->isDoubleTy()) &&
316 (SRetTy->getElementType(N: 1)->isDoubleTy())) {
317 result = dcMips16Helper[stubNum];
318 } else {
319 llvm_unreachable("Uncovered condition");
320 }
321 } else {
322 llvm_unreachable("Uncovered condition");
323 }
324 } else {
325 if (stubNum == 0) {
326 needHelper = false;
327 return "";
328 }
329 result = vMips16Helper[stubNum];
330 }
331 needHelper = true;
332 return result;
333}
334
335static bool isMips16HardFloatLibcall(StringRef Name) {
336 // FIXME: Use getSupportedLibcallImpl instead of blindly parsing the name.
337 iota_range<RTLIB::LibcallImpl> ParsedLibcalls =
338 RTLIB::RuntimeLibcallsInfo::lookupLibcallImplName(Name);
339 return !ParsedLibcalls.empty() &&
340 binary_search(Range: MipsSubtarget::HardFloatLibCalls,
341 Value: *ParsedLibcalls.begin());
342}
343
344void Mips16TargetLowering::getOpndList(
345 SmallVectorImpl<SDValue> &Ops,
346 std::deque<std::pair<unsigned, SDValue>> &RegsToPass, bool IsPICCall,
347 bool GlobalOrExternal, bool LocalLinkage, bool IsCallReloc,
348 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
349 SelectionDAG &DAG = CLI.DAG;
350 MachineFunction &MF = DAG.getMachineFunction();
351 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
352 const char* Mips16HelperFunction = nullptr;
353 bool NeedMips16Helper = false;
354
355 if (Subtarget.inMips16HardFloat()) {
356 //
357 // currently we don't have symbols tagged with the mips16 or mips32
358 // qualifier so we will assume that we don't know what kind it is.
359 // and generate the helper
360 //
361 bool LookupHelper = true;
362 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Val&: CLI.Callee)) {
363 if (isMips16HardFloatLibcall(Name: S->getSymbol()))
364 LookupHelper = false;
365 else {
366 const char *Symbol = S->getSymbol();
367 Mips16IntrinsicHelperType IntrinsicFind = { .Name: Symbol, .Helper: "" };
368 const Mips16HardFloatInfo::FuncSignature *Signature =
369 Mips16HardFloatInfo::findFuncSignature(name: Symbol);
370 if (!IsPICCall && Signature &&
371 FuncInfo->StubsNeeded.try_emplace(k: Symbol, args&: Signature).second) {
372 //
373 // S2 is normally saved if the stub is for a function which
374 // returns a float or double value and is not otherwise. This is
375 // because more work is required after the function the stub
376 // is calling completes, and so the stub cannot directly return
377 // and the stub has no stack space to store the return address so
378 // S2 is used for that purpose.
379 // In order to take advantage of not saving S2, we need to also
380 // optimize the call in the stub and this requires some further
381 // functionality in MipsAsmPrinter which we don't have yet.
382 // So for now we always save S2. The optimization will be done
383 // in a follow-on patch.
384 //
385 if (true || (Signature->RetSig != Mips16HardFloatInfo::NoFPRet))
386 FuncInfo->setSaveS2();
387 }
388 // one more look at list of intrinsics
389 const Mips16IntrinsicHelperType *Helper =
390 llvm::lower_bound(Range: Mips16IntrinsicHelper, Value&: IntrinsicFind);
391 if (Helper != std::end(arr: Mips16IntrinsicHelper) &&
392 *Helper == IntrinsicFind) {
393 Mips16HelperFunction = Helper->Helper;
394 NeedMips16Helper = true;
395 LookupHelper = false;
396 }
397
398 }
399 } else if (GlobalAddressSDNode *G =
400 dyn_cast<GlobalAddressSDNode>(Val&: CLI.Callee)) {
401
402 if (isMips16HardFloatLibcall(Name: G->getGlobal()->getName()))
403 LookupHelper = false;
404 }
405 if (LookupHelper)
406 Mips16HelperFunction =
407 getMips16HelperFunction(RetTy: CLI.RetTy, Args&: CLI.getArgs(), needHelper&: NeedMips16Helper);
408 }
409
410 SDValue JumpTarget = Callee;
411
412 // T9 should contain the address of the callee function if
413 // -relocation-model=pic or it is an indirect call.
414 if (IsPICCall || !GlobalOrExternal) {
415 unsigned V0Reg = Mips::R2;
416 if (NeedMips16Helper) {
417 RegsToPass.push_front(x: std::make_pair(x&: V0Reg, y&: Callee));
418 JumpTarget = DAG.getExternalSymbol(Sym: Mips16HelperFunction,
419 VT: getPointerTy(DL: DAG.getDataLayout()));
420 ExternalSymbolSDNode *S = cast<ExternalSymbolSDNode>(Val&: JumpTarget);
421 JumpTarget = getAddrGlobal(N: S, DL: CLI.DL, Ty: JumpTarget.getValueType(), DAG,
422 Flag: MipsII::MO_GOT, Chain,
423 PtrInfo: FuncInfo->callPtrInfo(MF, ES: S->getSymbol()));
424 } else
425 RegsToPass.push_front(x: std::make_pair(x: (unsigned)Mips::R25, y&: Callee));
426 }
427
428 Ops.push_back(Elt: JumpTarget);
429
430 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
431 LocalLinkage, IsCallReloc, CLI, Callee,
432 Chain);
433}
434
435MachineBasicBlock *
436Mips16TargetLowering::emitSel16(unsigned Opc, MachineInstr &MI,
437 MachineBasicBlock *BB) const {
438 if (Subtarget.getCLOpts().mips16_dont_expand_cond_pseudo)
439 return BB;
440 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
441 DebugLoc DL = MI.getDebugLoc();
442 // To "insert" a SELECT_CC instruction, we actually have to insert the
443 // diamond control-flow pattern. The incoming instruction knows the
444 // destination vreg to set, the condition code register to branch on, the
445 // true/false values to select between, and a branch opcode to use.
446 const BasicBlock *LLVM_BB = BB->getBasicBlock();
447 MachineFunction::iterator It = ++BB->getIterator();
448
449 // thisMBB:
450 // ...
451 // TrueVal = ...
452 // setcc r1, r2, r3
453 // bNE r1, r0, copy1MBB
454 // fallthrough --> copy0MBB
455 MachineBasicBlock *thisMBB = BB;
456 MachineFunction *F = BB->getParent();
457 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
458 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
459 F->insert(MBBI: It, MBB: copy0MBB);
460 F->insert(MBBI: It, MBB: sinkMBB);
461
462 // Transfer the remainder of BB and its successor edges to sinkMBB.
463 sinkMBB->splice(Where: sinkMBB->begin(), Other: BB,
464 From: std::next(x: MachineBasicBlock::iterator(MI)), To: BB->end());
465 sinkMBB->transferSuccessorsAndUpdatePHIs(FromMBB: BB);
466
467 // Next, add the true and fallthrough blocks as its successors.
468 BB->addSuccessor(Succ: copy0MBB);
469 BB->addSuccessor(Succ: sinkMBB);
470
471 BuildMI(BB, MIMD: DL, MCID: TII->get(Opcode: Opc))
472 .addReg(RegNo: MI.getOperand(i: 3).getReg())
473 .addMBB(MBB: sinkMBB);
474
475 // copy0MBB:
476 // %FalseValue = ...
477 // # fallthrough to sinkMBB
478 BB = copy0MBB;
479
480 // Update machine-CFG edges
481 BB->addSuccessor(Succ: sinkMBB);
482
483 // sinkMBB:
484 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
485 // ...
486 BB = sinkMBB;
487
488 BuildMI(BB&: *BB, I: BB->begin(), MIMD: DL, MCID: TII->get(Opcode: Mips::PHI), DestReg: MI.getOperand(i: 0).getReg())
489 .addReg(RegNo: MI.getOperand(i: 1).getReg())
490 .addMBB(MBB: thisMBB)
491 .addReg(RegNo: MI.getOperand(i: 2).getReg())
492 .addMBB(MBB: copy0MBB);
493
494 MI.eraseFromParent(); // The pseudo instruction is gone now.
495 return BB;
496}
497
498MachineBasicBlock *
499Mips16TargetLowering::emitSelT16(unsigned Opc1, unsigned Opc2, MachineInstr &MI,
500 MachineBasicBlock *BB) const {
501 if (Subtarget.getCLOpts().mips16_dont_expand_cond_pseudo)
502 return BB;
503 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
504 DebugLoc DL = MI.getDebugLoc();
505 // To "insert" a SELECT_CC instruction, we actually have to insert the
506 // diamond control-flow pattern. The incoming instruction knows the
507 // destination vreg to set, the condition code register to branch on, the
508 // true/false values to select between, and a branch opcode to use.
509 const BasicBlock *LLVM_BB = BB->getBasicBlock();
510 MachineFunction::iterator It = ++BB->getIterator();
511
512 // thisMBB:
513 // ...
514 // TrueVal = ...
515 // setcc r1, r2, r3
516 // bNE r1, r0, copy1MBB
517 // fallthrough --> copy0MBB
518 MachineBasicBlock *thisMBB = BB;
519 MachineFunction *F = BB->getParent();
520 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
521 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
522 F->insert(MBBI: It, MBB: copy0MBB);
523 F->insert(MBBI: It, MBB: sinkMBB);
524
525 // Transfer the remainder of BB and its successor edges to sinkMBB.
526 sinkMBB->splice(Where: sinkMBB->begin(), Other: BB,
527 From: std::next(x: MachineBasicBlock::iterator(MI)), To: BB->end());
528 sinkMBB->transferSuccessorsAndUpdatePHIs(FromMBB: BB);
529
530 // Next, add the true and fallthrough blocks as its successors.
531 BB->addSuccessor(Succ: copy0MBB);
532 BB->addSuccessor(Succ: sinkMBB);
533
534 BuildMI(BB, MIMD: DL, MCID: TII->get(Opcode: Opc2))
535 .addReg(RegNo: MI.getOperand(i: 3).getReg())
536 .addReg(RegNo: MI.getOperand(i: 4).getReg());
537 BuildMI(BB, MIMD: DL, MCID: TII->get(Opcode: Opc1)).addMBB(MBB: sinkMBB);
538
539 // copy0MBB:
540 // %FalseValue = ...
541 // # fallthrough to sinkMBB
542 BB = copy0MBB;
543
544 // Update machine-CFG edges
545 BB->addSuccessor(Succ: sinkMBB);
546
547 // sinkMBB:
548 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
549 // ...
550 BB = sinkMBB;
551
552 BuildMI(BB&: *BB, I: BB->begin(), MIMD: DL, MCID: TII->get(Opcode: Mips::PHI), DestReg: MI.getOperand(i: 0).getReg())
553 .addReg(RegNo: MI.getOperand(i: 1).getReg())
554 .addMBB(MBB: thisMBB)
555 .addReg(RegNo: MI.getOperand(i: 2).getReg())
556 .addMBB(MBB: copy0MBB);
557
558 MI.eraseFromParent(); // The pseudo instruction is gone now.
559 return BB;
560
561}
562
563MachineBasicBlock *
564Mips16TargetLowering::emitSeliT16(unsigned Opc1, unsigned Opc2,
565 MachineInstr &MI,
566 MachineBasicBlock *BB) const {
567 if (Subtarget.getCLOpts().mips16_dont_expand_cond_pseudo)
568 return BB;
569 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
570 DebugLoc DL = MI.getDebugLoc();
571 // To "insert" a SELECT_CC instruction, we actually have to insert the
572 // diamond control-flow pattern. The incoming instruction knows the
573 // destination vreg to set, the condition code register to branch on, the
574 // true/false values to select between, and a branch opcode to use.
575 const BasicBlock *LLVM_BB = BB->getBasicBlock();
576 MachineFunction::iterator It = ++BB->getIterator();
577
578 // thisMBB:
579 // ...
580 // TrueVal = ...
581 // setcc r1, r2, r3
582 // bNE r1, r0, copy1MBB
583 // fallthrough --> copy0MBB
584 MachineBasicBlock *thisMBB = BB;
585 MachineFunction *F = BB->getParent();
586 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
587 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
588 F->insert(MBBI: It, MBB: copy0MBB);
589 F->insert(MBBI: It, MBB: sinkMBB);
590
591 // Transfer the remainder of BB and its successor edges to sinkMBB.
592 sinkMBB->splice(Where: sinkMBB->begin(), Other: BB,
593 From: std::next(x: MachineBasicBlock::iterator(MI)), To: BB->end());
594 sinkMBB->transferSuccessorsAndUpdatePHIs(FromMBB: BB);
595
596 // Next, add the true and fallthrough blocks as its successors.
597 BB->addSuccessor(Succ: copy0MBB);
598 BB->addSuccessor(Succ: sinkMBB);
599
600 BuildMI(BB, MIMD: DL, MCID: TII->get(Opcode: Opc2))
601 .addReg(RegNo: MI.getOperand(i: 3).getReg())
602 .addImm(Val: MI.getOperand(i: 4).getImm());
603 BuildMI(BB, MIMD: DL, MCID: TII->get(Opcode: Opc1)).addMBB(MBB: sinkMBB);
604
605 // copy0MBB:
606 // %FalseValue = ...
607 // # fallthrough to sinkMBB
608 BB = copy0MBB;
609
610 // Update machine-CFG edges
611 BB->addSuccessor(Succ: sinkMBB);
612
613 // sinkMBB:
614 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
615 // ...
616 BB = sinkMBB;
617
618 BuildMI(BB&: *BB, I: BB->begin(), MIMD: DL, MCID: TII->get(Opcode: Mips::PHI), DestReg: MI.getOperand(i: 0).getReg())
619 .addReg(RegNo: MI.getOperand(i: 1).getReg())
620 .addMBB(MBB: thisMBB)
621 .addReg(RegNo: MI.getOperand(i: 2).getReg())
622 .addMBB(MBB: copy0MBB);
623
624 MI.eraseFromParent(); // The pseudo instruction is gone now.
625 return BB;
626
627}
628
629MachineBasicBlock *
630Mips16TargetLowering::emitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
631 MachineInstr &MI,
632 MachineBasicBlock *BB) const {
633 if (Subtarget.getCLOpts().mips16_dont_expand_cond_pseudo)
634 return BB;
635 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
636 Register regX = MI.getOperand(i: 0).getReg();
637 Register regY = MI.getOperand(i: 1).getReg();
638 MachineBasicBlock *target = MI.getOperand(i: 2).getMBB();
639 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: CmpOpc))
640 .addReg(RegNo: regX)
641 .addReg(RegNo: regY);
642 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: BtOpc)).addMBB(MBB: target);
643 MI.eraseFromParent(); // The pseudo instruction is gone now.
644 return BB;
645}
646
647MachineBasicBlock *Mips16TargetLowering::emitFEXT_T8I8I16_ins(
648 unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc, bool ImmSigned,
649 MachineInstr &MI, MachineBasicBlock *BB) const {
650 if (Subtarget.getCLOpts().mips16_dont_expand_cond_pseudo)
651 return BB;
652 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
653 Register regX = MI.getOperand(i: 0).getReg();
654 int64_t imm = MI.getOperand(i: 1).getImm();
655 MachineBasicBlock *target = MI.getOperand(i: 2).getMBB();
656 unsigned CmpOpc;
657 if (isUInt<8>(x: imm))
658 CmpOpc = CmpiOpc;
659 else if ((!ImmSigned && isUInt<16>(x: imm)) ||
660 (ImmSigned && isInt<16>(x: imm)))
661 CmpOpc = CmpiXOpc;
662 else
663 llvm_unreachable("immediate field not usable");
664 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: CmpOpc)).addReg(RegNo: regX).addImm(Val: imm);
665 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: BtOpc)).addMBB(MBB: target);
666 MI.eraseFromParent(); // The pseudo instruction is gone now.
667 return BB;
668}
669
670static unsigned Mips16WhichOp8uOr16simm
671 (unsigned shortOp, unsigned longOp, int64_t Imm) {
672 if (isUInt<8>(x: Imm))
673 return shortOp;
674 else if (isInt<16>(x: Imm))
675 return longOp;
676 else
677 llvm_unreachable("immediate field not usable");
678}
679
680MachineBasicBlock *
681Mips16TargetLowering::emitFEXT_CCRX16_ins(unsigned SltOpc, MachineInstr &MI,
682 MachineBasicBlock *BB) const {
683 if (Subtarget.getCLOpts().mips16_dont_expand_cond_pseudo)
684 return BB;
685 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
686 Register CC = MI.getOperand(i: 0).getReg();
687 Register regX = MI.getOperand(i: 1).getReg();
688 Register regY = MI.getOperand(i: 2).getReg();
689 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: SltOpc))
690 .addReg(RegNo: regX)
691 .addReg(RegNo: regY);
692 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: Mips::MoveR3216), DestReg: CC)
693 .addReg(RegNo: Mips::R24);
694 MI.eraseFromParent(); // The pseudo instruction is gone now.
695 return BB;
696}
697
698MachineBasicBlock *
699Mips16TargetLowering::emitFEXT_CCRXI16_ins(unsigned SltiOpc, unsigned SltiXOpc,
700 MachineInstr &MI,
701 MachineBasicBlock *BB) const {
702 if (Subtarget.getCLOpts().mips16_dont_expand_cond_pseudo)
703 return BB;
704 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
705 Register CC = MI.getOperand(i: 0).getReg();
706 Register regX = MI.getOperand(i: 1).getReg();
707 int64_t Imm = MI.getOperand(i: 2).getImm();
708 unsigned SltOpc = Mips16WhichOp8uOr16simm(shortOp: SltiOpc, longOp: SltiXOpc, Imm);
709 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: SltOpc)).addReg(RegNo: regX).addImm(Val: Imm);
710 BuildMI(BB&: *BB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: Mips::MoveR3216), DestReg: CC)
711 .addReg(RegNo: Mips::R24);
712 MI.eraseFromParent(); // The pseudo instruction is gone now.
713 return BB;
714
715}
716