blob: 58a53a079378eeaf69c185459028f56f4479dc27 [file] [log] [blame]
Vasileios Kalintiris22ec97f2016-06-16 14:25:13 +00001//===-- MipsFastISel.cpp - Mips FastISel implementation --------------------===//
Vasileios Kalintirisa9e51542016-06-08 13:13:15 +00002//
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/// \file
11/// \brief This file defines the MIPS-specific support for the FastISel class.
12/// Some of the target-specific code is generated by tablegen in the file
13/// MipsGenFastISel.inc, which is #included here.
14///
15//===----------------------------------------------------------------------===//
Reed Kotler720c5ca2014-04-17 22:15:34 +000016
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "MipsCCState.h"
Reed Kotler5fb7d8b2015-02-24 02:36:45 +000018#include "MipsInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "MipsISelLowering.h"
20#include "MipsMachineFunction.h"
21#include "MipsRegisterInfo.h"
22#include "MipsSubtarget.h"
23#include "MipsTargetMachine.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000025#include "llvm/CodeGen/FastISel.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000026#include "llvm/CodeGen/FunctionLoweringInfo.h"
Reed Kotler67077b32014-04-29 17:57:50 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie457343d2015-05-21 21:12:43 +000029#include "llvm/IR/GetElementPtrTypeIterator.h"
Reed Kotlerbab3f232014-05-01 20:39:21 +000030#include "llvm/IR/GlobalAlias.h"
31#include "llvm/IR/GlobalVariable.h"
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000032#include "llvm/MC/MCSymbol.h"
Reed Kotler67077b32014-04-29 17:57:50 +000033#include "llvm/Target/TargetInstrInfo.h"
Daniel Sanderscbaca422016-07-29 12:27:28 +000034#include "llvm/Support/Debug.h"
35
36#define DEBUG_TYPE "mips-fastisel"
Reed Kotler720c5ca2014-04-17 22:15:34 +000037
38using namespace llvm;
39
40namespace {
41
42class MipsFastISel final : public FastISel {
43
Reed Kotlera562b462014-10-13 21:46:41 +000044 // All possible address modes.
45 class Address {
46 public:
47 typedef enum { RegBase, FrameIndexBase } BaseKind;
48
49 private:
50 BaseKind Kind;
51 union {
52 unsigned Reg;
53 int FI;
54 } Base;
55
56 int64_t Offset;
57
58 const GlobalValue *GV;
59
60 public:
61 // Innocuous defaults for our address.
62 Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; }
63 void setKind(BaseKind K) { Kind = K; }
64 BaseKind getKind() const { return Kind; }
65 bool isRegBase() const { return Kind == RegBase; }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +000066 bool isFIBase() const { return Kind == FrameIndexBase; }
Reed Kotlera562b462014-10-13 21:46:41 +000067 void setReg(unsigned Reg) {
68 assert(isRegBase() && "Invalid base register access!");
69 Base.Reg = Reg;
70 }
71 unsigned getReg() const {
72 assert(isRegBase() && "Invalid base register access!");
73 return Base.Reg;
74 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +000075 void setFI(unsigned FI) {
76 assert(isFIBase() && "Invalid base frame index access!");
77 Base.FI = FI;
78 }
79 unsigned getFI() const {
80 assert(isFIBase() && "Invalid base frame index access!");
81 return Base.FI;
82 }
83
Reed Kotlera562b462014-10-13 21:46:41 +000084 void setOffset(int64_t Offset_) { Offset = Offset_; }
85 int64_t getOffset() const { return Offset; }
86 void setGlobalValue(const GlobalValue *G) { GV = G; }
87 const GlobalValue *getGlobalValue() { return GV; }
88 };
89
Reed Kotler67077b32014-04-29 17:57:50 +000090 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
91 /// make the right decision when generating code for different targets.
Reed Kotler67077b32014-04-29 17:57:50 +000092 const TargetMachine &TM;
Eric Christopher96e72c62015-01-29 23:27:36 +000093 const MipsSubtarget *Subtarget;
Reed Kotler67077b32014-04-29 17:57:50 +000094 const TargetInstrInfo &TII;
95 const TargetLowering &TLI;
96 MipsFunctionInfo *MFI;
97
98 // Convenience variables to avoid some queries.
99 LLVMContext *Context;
100
Daniel Sanderscbaca422016-07-29 12:27:28 +0000101 bool fastLowerArguments() override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000102 bool fastLowerCall(CallLoweringInfo &CLI) override;
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000103 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000104
Reed Kotler67077b32014-04-29 17:57:50 +0000105 bool TargetSupported;
Reed Kotlera562b462014-10-13 21:46:41 +0000106 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
107 // floating point but not reject doing fast-isel in other
108 // situations
109
110private:
111 // Selection routines.
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000112 bool selectLogicalOp(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000113 bool selectLoad(const Instruction *I);
114 bool selectStore(const Instruction *I);
115 bool selectBranch(const Instruction *I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000116 bool selectSelect(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000117 bool selectCmp(const Instruction *I);
118 bool selectFPExt(const Instruction *I);
119 bool selectFPTrunc(const Instruction *I);
120 bool selectFPToInt(const Instruction *I, bool IsSigned);
121 bool selectRet(const Instruction *I);
122 bool selectTrunc(const Instruction *I);
123 bool selectIntExt(const Instruction *I);
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +0000124 bool selectShift(const Instruction *I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +0000125 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
Reed Kotlera562b462014-10-13 21:46:41 +0000126
127 // Utility helper routines.
Reed Kotlera562b462014-10-13 21:46:41 +0000128 bool isTypeLegal(Type *Ty, MVT &VT);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000129 bool isTypeSupported(Type *Ty, MVT &VT);
Reed Kotlera562b462014-10-13 21:46:41 +0000130 bool isLoadTypeLegal(Type *Ty, MVT &VT);
131 bool computeAddress(const Value *Obj, Address &Addr);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000132 bool computeCallAddress(const Value *V, Address &Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000133 void simplifyAddress(Address &Addr);
Reed Kotlera562b462014-10-13 21:46:41 +0000134
135 // Emit helper routines.
136 bool emitCmp(unsigned DestReg, const CmpInst *CI);
137 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
138 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000139 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
140 MachineMemOperand *MMO = nullptr);
Reed Kotlera562b462014-10-13 21:46:41 +0000141 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
142 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000143 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Reed Kotlera562b462014-10-13 21:46:41 +0000144 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
145
146 bool IsZExt);
147 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
148
149 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
150 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
151 unsigned DestReg);
152 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
153 unsigned DestReg);
154
155 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
156
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000157 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
158 const Value *RHS);
159
Reed Kotlera562b462014-10-13 21:46:41 +0000160 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
161 unsigned materializeGV(const GlobalValue *GV, MVT VT);
162 unsigned materializeInt(const Constant *C, MVT VT);
163 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000164 unsigned materializeExternalCallSym(MCSymbol *Syn);
Reed Kotlera562b462014-10-13 21:46:41 +0000165
166 MachineInstrBuilder emitInst(unsigned Opc) {
167 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
168 }
169 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
170 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
171 DstReg);
172 }
173 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
174 unsigned MemReg, int64_t MemOffset) {
175 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
176 }
177 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
178 unsigned MemReg, int64_t MemOffset) {
179 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
180 }
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +0000181
182 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
183 const TargetRegisterClass *RC,
184 unsigned Op0, bool Op0IsKill,
185 unsigned Op1, bool Op1IsKill);
186
Reed Kotlera562b462014-10-13 21:46:41 +0000187 // for some reason, this default is not generated by tablegen
188 // so we explicitly generate it here.
189 //
190 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
191 unsigned Op0, bool Op0IsKill, uint64_t imm1,
192 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
193 return 0;
194 }
Reed Kotler67077b32014-04-29 17:57:50 +0000195
Reed Kotlerd5c41962014-11-13 23:37:45 +0000196 // Call handling routines.
197private:
198 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
199 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
200 unsigned &NumBytes);
201 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
Daniel Sanderscbaca422016-07-29 12:27:28 +0000202 const MipsABIInfo &getABI() const {
203 return static_cast<const MipsTargetMachine &>(TM).getABI();
204 }
Reed Kotlerd5c41962014-11-13 23:37:45 +0000205
Reed Kotler720c5ca2014-04-17 22:15:34 +0000206public:
Reed Kotlera562b462014-10-13 21:46:41 +0000207 // Backend specific FastISel code.
Reed Kotler720c5ca2014-04-17 22:15:34 +0000208 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
209 const TargetLibraryInfo *libInfo)
Eric Christopher3ab98892014-12-20 00:07:09 +0000210 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
Eric Christopherb2a5fa92015-02-14 00:09:46 +0000211 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
Eric Christopher96e72c62015-01-29 23:27:36 +0000212 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
Reed Kotler67077b32014-04-29 17:57:50 +0000213 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
214 Context = &funcInfo.Fn->getContext();
Zoran Jovanovic838eabc2016-01-28 11:08:03 +0000215 bool ISASupported = !Subtarget->hasMips32r6() &&
216 !Subtarget->inMicroMipsMode() && Subtarget->hasMips32();
Eric Christopherd86af632015-01-29 23:27:45 +0000217 TargetSupported =
Daniel Sanderscbaca422016-07-29 12:27:28 +0000218 ISASupported && TM.isPositionIndependent() && getABI().IsO32();
Reed Kotler12f94882014-10-10 17:00:46 +0000219 UnsupportedFPMode = Subtarget->isFP64bit();
Reed Kotler67077b32014-04-29 17:57:50 +0000220 }
221
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000222 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000223 unsigned fastMaterializeConstant(const Constant *C) override;
Reed Kotlera562b462014-10-13 21:46:41 +0000224 bool fastSelectInstruction(const Instruction *I) override;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000225
Reed Kotler9fe25f32014-06-08 02:08:43 +0000226#include "MipsGenFastISel.inc"
Reed Kotler720c5ca2014-04-17 22:15:34 +0000227};
Reed Kotlera562b462014-10-13 21:46:41 +0000228} // end anonymous namespace.
Reed Kotler67077b32014-04-29 17:57:50 +0000229
Reed Kotlerd5c41962014-11-13 23:37:45 +0000230static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
231 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Reid Klecknerd3781742014-11-14 00:39:33 +0000232 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000233
234static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
235 CCValAssign::LocInfo LocInfo,
236 ISD::ArgFlagsTy ArgFlags, CCState &State) {
237 llvm_unreachable("should not be called");
238}
239
Benjamin Kramer970eac42015-02-06 17:51:54 +0000240static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
241 CCValAssign::LocInfo LocInfo,
242 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Reed Kotlerd5c41962014-11-13 23:37:45 +0000243 llvm_unreachable("should not be called");
244}
245
246#include "MipsGenCallingConv.inc"
247
248CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
249 return CC_MipsO32;
250}
251
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000252unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
253 const Value *LHS, const Value *RHS) {
254 // Canonicalize immediates to the RHS first.
255 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
256 std::swap(LHS, RHS);
257
258 unsigned Opc;
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000259 switch (ISDOpc) {
Vasileios Kalintiris2a95f822015-10-12 15:39:41 +0000260 case ISD::AND:
261 Opc = Mips::AND;
262 break;
263 case ISD::OR:
264 Opc = Mips::OR;
265 break;
266 case ISD::XOR:
267 Opc = Mips::XOR;
268 break;
269 default:
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000270 llvm_unreachable("unexpected opcode");
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000271 }
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000272
273 unsigned LHSReg = getRegForValue(LHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000274 if (!LHSReg)
275 return 0;
276
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000277 unsigned RHSReg;
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000278 if (const auto *C = dyn_cast<ConstantInt>(RHS))
279 RHSReg = materializeInt(C, MVT::i32);
280 else
281 RHSReg = getRegForValue(RHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000282 if (!RHSReg)
283 return 0;
284
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000285 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
286 if (!ResultReg)
287 return 0;
288
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000289 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
290 return ResultReg;
291}
292
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000293unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Vasileios Kalintiris2f12b2e2015-08-04 14:35:50 +0000294 if (!TargetSupported)
295 return 0;
296
Mehdi Amini44ede332015-07-09 02:09:04 +0000297 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 &&
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000298 "Alloca should always return a pointer.");
299
300 DenseMap<const AllocaInst *, int>::iterator SI =
301 FuncInfo.StaticAllocaMap.find(AI);
302
303 if (SI != FuncInfo.StaticAllocaMap.end()) {
304 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
306 ResultReg)
307 .addFrameIndex(SI->second)
308 .addImm(0);
309 return ResultReg;
310 }
311
312 return 0;
313}
314
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000315unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
316 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Reed Kotler497311a2014-10-10 17:39:51 +0000317 return 0;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000318 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
319 const ConstantInt *CI = cast<ConstantInt>(C);
Vasileios Kalintiris77fb0a32015-07-30 11:51:44 +0000320 return materialize32BitInt(CI->getZExtValue(), RC);
Reed Kotler497311a2014-10-10 17:39:51 +0000321}
322
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000323unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
324 const TargetRegisterClass *RC) {
325 unsigned ResultReg = createResultReg(RC);
326
327 if (isInt<16>(Imm)) {
328 unsigned Opc = Mips::ADDiu;
329 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
330 return ResultReg;
331 } else if (isUInt<16>(Imm)) {
332 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
333 return ResultReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000334 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000335 unsigned Lo = Imm & 0xFFFF;
336 unsigned Hi = (Imm >> 16) & 0xFFFF;
337 if (Lo) {
338 // Both Lo and Hi have nonzero bits.
339 unsigned TmpReg = createResultReg(RC);
340 emitInst(Mips::LUi, TmpReg).addImm(Hi);
341 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
342 } else {
343 emitInst(Mips::LUi, ResultReg).addImm(Hi);
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000344 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000345 return ResultReg;
346}
347
348unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
349 if (UnsupportedFPMode)
350 return 0;
351 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
352 if (VT == MVT::f32) {
353 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
354 unsigned DestReg = createResultReg(RC);
355 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
356 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
357 return DestReg;
358 } else if (VT == MVT::f64) {
359 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
360 unsigned DestReg = createResultReg(RC);
361 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
362 unsigned TempReg2 =
363 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
364 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
365 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000366 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000367 return 0;
368}
369
370unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
371 // For now 32-bit only.
372 if (VT != MVT::i32)
373 return 0;
374 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
375 unsigned DestReg = createResultReg(RC);
376 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
377 bool IsThreadLocal = GVar && GVar->isThreadLocal();
378 // TLS not supported at this time.
379 if (IsThreadLocal)
380 return 0;
381 emitInst(Mips::LW, DestReg)
382 .addReg(MFI->getGlobalBaseReg())
383 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
384 if ((GV->hasInternalLinkage() ||
385 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
386 unsigned TempReg = createResultReg(RC);
387 emitInst(Mips::ADDiu, TempReg)
388 .addReg(DestReg)
389 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
390 DestReg = TempReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000391 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000392 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000393}
394
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000395unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000396 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
397 unsigned DestReg = createResultReg(RC);
398 emitInst(Mips::LW, DestReg)
399 .addReg(MFI->getGlobalBaseReg())
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000400 .addSym(Sym, MipsII::MO_GOT);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000401 return DestReg;
402}
403
Reed Kotlerbab3f232014-05-01 20:39:21 +0000404// Materialize a constant into a register, and return the register
405// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000406unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
Vasileios Kalintiris2f12b2e2015-08-04 14:35:50 +0000407 if (!TargetSupported)
408 return 0;
409
Mehdi Amini44ede332015-07-09 02:09:04 +0000410 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000411
412 // Only handle simple types.
413 if (!CEVT.isSimple())
414 return 0;
415 MVT VT = CEVT.getSimpleVT();
416
417 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000418 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000419 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000420 return materializeGV(GV, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000421 else if (isa<ConstantInt>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000422 return materializeInt(C, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000423
424 return 0;
425}
426
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000427bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000428
429 const User *U = nullptr;
430 unsigned Opcode = Instruction::UserOp1;
431 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
432 // Don't walk into other basic blocks unless the object is an alloca from
433 // another block, otherwise it may not have a virtual register assigned.
434 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
435 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
436 Opcode = I->getOpcode();
437 U = I;
438 }
Vasileios Kalintiris32cd69a2015-05-12 12:08:31 +0000439 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
440 Opcode = C->getOpcode();
441 U = C;
442 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000443 switch (Opcode) {
444 default:
445 break;
446 case Instruction::BitCast: {
447 // Look through bitcasts.
448 return computeAddress(U->getOperand(0), Addr);
449 }
450 case Instruction::GetElementPtr: {
451 Address SavedAddr = Addr;
452 uint64_t TmpOffset = Addr.getOffset();
453 // Iterate through the GEP folding the constants into offsets where
454 // we can.
455 gep_type_iterator GTI = gep_type_begin(U);
456 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
457 ++i, ++GTI) {
458 const Value *Op = *i;
459 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
460 const StructLayout *SL = DL.getStructLayout(STy);
461 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
462 TmpOffset += SL->getElementOffset(Idx);
463 } else {
464 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
465 for (;;) {
466 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
467 // Constant-offset addressing.
468 TmpOffset += CI->getSExtValue() * S;
469 break;
470 }
471 if (canFoldAddIntoGEP(U, Op)) {
472 // A compatible add with a constant operand. Fold the constant.
473 ConstantInt *CI =
474 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
475 TmpOffset += CI->getSExtValue() * S;
476 // Iterate on the other operand.
477 Op = cast<AddOperator>(Op)->getOperand(0);
478 continue;
479 }
480 // Unsupported
481 goto unsupported_gep;
482 }
483 }
484 }
485 // Try to grab the base operand now.
486 Addr.setOffset(TmpOffset);
487 if (computeAddress(U->getOperand(0), Addr))
488 return true;
489 // We failed, restore everything and try the other options.
490 Addr = SavedAddr;
491 unsupported_gep:
492 break;
493 }
494 case Instruction::Alloca: {
495 const AllocaInst *AI = cast<AllocaInst>(Obj);
496 DenseMap<const AllocaInst *, int>::iterator SI =
497 FuncInfo.StaticAllocaMap.find(AI);
498 if (SI != FuncInfo.StaticAllocaMap.end()) {
499 Addr.setKind(Address::FrameIndexBase);
500 Addr.setFI(SI->second);
501 return true;
502 }
503 break;
504 }
505 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000506 Addr.setReg(getRegForValue(Obj));
507 return Addr.getReg() != 0;
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000508}
509
Reed Kotlerd5c41962014-11-13 23:37:45 +0000510bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000511 const User *U = nullptr;
512 unsigned Opcode = Instruction::UserOp1;
513
514 if (const auto *I = dyn_cast<Instruction>(V)) {
515 // Check if the value is defined in the same basic block. This information
516 // is crucial to know whether or not folding an operand is valid.
517 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
518 Opcode = I->getOpcode();
519 U = I;
520 }
521 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
522 Opcode = C->getOpcode();
523 U = C;
524 }
525
526 switch (Opcode) {
527 default:
528 break;
529 case Instruction::BitCast:
530 // Look past bitcasts if its operand is in the same BB.
531 return computeCallAddress(U->getOperand(0), Addr);
532 break;
533 case Instruction::IntToPtr:
534 // Look past no-op inttoptrs if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000535 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
536 TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000537 return computeCallAddress(U->getOperand(0), Addr);
538 break;
539 case Instruction::PtrToInt:
540 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000541 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000542 return computeCallAddress(U->getOperand(0), Addr);
543 break;
544 }
545
Reed Kotlerd5c41962014-11-13 23:37:45 +0000546 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
547 Addr.setGlobalValue(GV);
548 return true;
549 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000550
551 // If all else fails, try to materialize the value in a register.
552 if (!Addr.getGlobalValue()) {
553 Addr.setReg(getRegForValue(V));
554 return Addr.getReg() != 0;
555 }
556
Reed Kotlerd5c41962014-11-13 23:37:45 +0000557 return false;
558}
559
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000560bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000561 EVT evt = TLI.getValueType(DL, Ty, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000562 // Only handle simple types.
563 if (evt == MVT::Other || !evt.isSimple())
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000564 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000565 VT = evt.getSimpleVT();
566
567 // Handle all legal types, i.e. a register that will directly hold this
568 // value.
569 return TLI.isTypeLegal(VT);
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000570}
571
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000572bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
573 if (Ty->isVectorTy())
574 return false;
575
576 if (isTypeLegal(Ty, VT))
577 return true;
578
579 // If this is a type than can be sign or zero-extended to a basic operation
580 // go ahead and accept it now.
581 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
582 return true;
583
584 return false;
585}
586
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000587bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
588 if (isTypeLegal(Ty, VT))
Reed Kotler62de6b92014-10-11 00:55:18 +0000589 return true;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000590 // We will extend this in a later patch:
591 // If this is a type than can be sign or zero-extended to a basic operation
592 // go ahead and accept it now.
593 if (VT == MVT::i8 || VT == MVT::i16)
594 return true;
Reed Kotler62de6b92014-10-11 00:55:18 +0000595 return false;
596}
Reed Kotler62de6b92014-10-11 00:55:18 +0000597// Because of how EmitCmp is called with fast-isel, you can
Reed Kotler497311a2014-10-10 17:39:51 +0000598// end up with redundant "andi" instructions after the sequences emitted below.
599// We should try and solve this issue in the future.
600//
Reed Kotlera562b462014-10-13 21:46:41 +0000601bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000602 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000603 bool IsUnsigned = CI->isUnsigned();
Reed Kotler497311a2014-10-10 17:39:51 +0000604 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
605 if (LeftReg == 0)
606 return false;
607 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
608 if (RightReg == 0)
609 return false;
Reed Kotler1f64eca2014-10-10 20:46:28 +0000610 CmpInst::Predicate P = CI->getPredicate();
Reed Kotler62de6b92014-10-11 00:55:18 +0000611
Reed Kotler1f64eca2014-10-10 20:46:28 +0000612 switch (P) {
Reed Kotler497311a2014-10-10 17:39:51 +0000613 default:
614 return false;
615 case CmpInst::ICMP_EQ: {
616 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000617 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
618 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000619 break;
620 }
621 case CmpInst::ICMP_NE: {
622 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000623 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
624 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000625 break;
626 }
627 case CmpInst::ICMP_UGT: {
Reed Kotlera562b462014-10-13 21:46:41 +0000628 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000629 break;
630 }
631 case CmpInst::ICMP_ULT: {
Reed Kotlera562b462014-10-13 21:46:41 +0000632 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000633 break;
634 }
635 case CmpInst::ICMP_UGE: {
636 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000637 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
638 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000639 break;
640 }
641 case CmpInst::ICMP_ULE: {
642 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000643 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
644 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000645 break;
646 }
647 case CmpInst::ICMP_SGT: {
Reed Kotlera562b462014-10-13 21:46:41 +0000648 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000649 break;
650 }
651 case CmpInst::ICMP_SLT: {
Reed Kotlera562b462014-10-13 21:46:41 +0000652 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000653 break;
654 }
655 case CmpInst::ICMP_SGE: {
656 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000657 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
658 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000659 break;
660 }
661 case CmpInst::ICMP_SLE: {
662 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000663 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
664 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000665 break;
666 }
Reed Kotler1f64eca2014-10-10 20:46:28 +0000667 case CmpInst::FCMP_OEQ:
668 case CmpInst::FCMP_UNE:
669 case CmpInst::FCMP_OLT:
670 case CmpInst::FCMP_OLE:
671 case CmpInst::FCMP_OGT:
672 case CmpInst::FCMP_OGE: {
673 if (UnsupportedFPMode)
674 return false;
675 bool IsFloat = Left->getType()->isFloatTy();
676 bool IsDouble = Left->getType()->isDoubleTy();
677 if (!IsFloat && !IsDouble)
678 return false;
679 unsigned Opc, CondMovOpc;
680 switch (P) {
681 case CmpInst::FCMP_OEQ:
682 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
683 CondMovOpc = Mips::MOVT_I;
684 break;
685 case CmpInst::FCMP_UNE:
686 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
687 CondMovOpc = Mips::MOVF_I;
688 break;
689 case CmpInst::FCMP_OLT:
690 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
691 CondMovOpc = Mips::MOVT_I;
692 break;
693 case CmpInst::FCMP_OLE:
694 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
695 CondMovOpc = Mips::MOVT_I;
696 break;
697 case CmpInst::FCMP_OGT:
698 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
699 CondMovOpc = Mips::MOVF_I;
700 break;
701 case CmpInst::FCMP_OGE:
702 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
703 CondMovOpc = Mips::MOVF_I;
704 break;
705 default:
Chandler Carruth38811cc2014-10-10 21:07:03 +0000706 llvm_unreachable("Only switching of a subset of CCs.");
Reed Kotler1f64eca2014-10-10 20:46:28 +0000707 }
708 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
709 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000710 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
711 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
Simon Dardis8efa9792016-09-09 09:22:52 +0000712 emitInst(Opc)
713 .addReg(Mips::FCC0, RegState::Define)
714 .addReg(LeftReg)
715 .addReg(RightReg);
Daniel Sandersa6cda122016-05-06 12:57:26 +0000716 emitInst(CondMovOpc, ResultReg)
717 .addReg(RegWithOne)
718 .addReg(Mips::FCC0)
719 .addReg(RegWithZero);
Reed Kotler1f64eca2014-10-10 20:46:28 +0000720 break;
721 }
Reed Kotler497311a2014-10-10 17:39:51 +0000722 }
Reed Kotler62de6b92014-10-11 00:55:18 +0000723 return true;
724}
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000725bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
726 unsigned Alignment) {
727 //
728 // more cases will be handled here in following patches.
729 //
730 unsigned Opc;
731 switch (VT.SimpleTy) {
732 case MVT::i32: {
733 ResultReg = createResultReg(&Mips::GPR32RegClass);
734 Opc = Mips::LW;
735 break;
736 }
737 case MVT::i16: {
738 ResultReg = createResultReg(&Mips::GPR32RegClass);
739 Opc = Mips::LHu;
740 break;
741 }
742 case MVT::i8: {
743 ResultReg = createResultReg(&Mips::GPR32RegClass);
744 Opc = Mips::LBu;
745 break;
746 }
747 case MVT::f32: {
748 if (UnsupportedFPMode)
749 return false;
750 ResultReg = createResultReg(&Mips::FGR32RegClass);
751 Opc = Mips::LWC1;
752 break;
753 }
754 case MVT::f64: {
755 if (UnsupportedFPMode)
756 return false;
757 ResultReg = createResultReg(&Mips::AFGR64RegClass);
758 Opc = Mips::LDC1;
759 break;
760 }
761 default:
762 return false;
763 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000764 if (Addr.isRegBase()) {
765 simplifyAddress(Addr);
766 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
767 return true;
768 }
769 if (Addr.isFIBase()) {
770 unsigned FI = Addr.getFI();
771 unsigned Align = 4;
772 unsigned Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000773 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000774 MachineMemOperand *MMO = MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +0000775 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000776 MFI.getObjectSize(FI), Align);
777 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
778 .addFrameIndex(FI)
779 .addImm(Offset)
780 .addMemOperand(MMO);
781 return true;
782 }
783 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000784}
785
786bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
787 unsigned Alignment) {
788 //
789 // more cases will be handled here in following patches.
790 //
791 unsigned Opc;
792 switch (VT.SimpleTy) {
793 case MVT::i8:
794 Opc = Mips::SB;
795 break;
796 case MVT::i16:
797 Opc = Mips::SH;
798 break;
799 case MVT::i32:
800 Opc = Mips::SW;
801 break;
802 case MVT::f32:
803 if (UnsupportedFPMode)
804 return false;
805 Opc = Mips::SWC1;
806 break;
807 case MVT::f64:
808 if (UnsupportedFPMode)
809 return false;
810 Opc = Mips::SDC1;
811 break;
812 default:
813 return false;
814 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000815 if (Addr.isRegBase()) {
816 simplifyAddress(Addr);
817 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
818 return true;
819 }
820 if (Addr.isFIBase()) {
821 unsigned FI = Addr.getFI();
822 unsigned Align = 4;
823 unsigned Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000824 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000825 MachineMemOperand *MMO = MF->getMachineMemOperand(
Simon Dardisd8bceb92016-04-29 16:07:47 +0000826 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000827 MFI.getObjectSize(FI), Align);
828 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
829 .addReg(SrcReg)
830 .addFrameIndex(FI)
831 .addImm(Offset)
832 .addMemOperand(MMO);
833 return true;
834 }
835 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000836}
837
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000838bool MipsFastISel::selectLogicalOp(const Instruction *I) {
839 MVT VT;
840 if (!isTypeSupported(I->getType(), VT))
841 return false;
842
843 unsigned ResultReg;
844 switch (I->getOpcode()) {
845 default:
846 llvm_unreachable("Unexpected instruction.");
847 case Instruction::And:
848 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
849 break;
850 case Instruction::Or:
851 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
852 break;
853 case Instruction::Xor:
854 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
855 break;
856 }
857
858 if (!ResultReg)
859 return false;
860
861 updateValueMap(I, ResultReg);
862 return true;
863}
864
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000865bool MipsFastISel::selectLoad(const Instruction *I) {
866 // Atomic loads need special handling.
867 if (cast<LoadInst>(I)->isAtomic())
868 return false;
869
870 // Verify we have a legal type before going any further.
871 MVT VT;
872 if (!isLoadTypeLegal(I->getType(), VT))
873 return false;
874
875 // See if we can handle this address.
876 Address Addr;
877 if (!computeAddress(I->getOperand(0), Addr))
878 return false;
879
880 unsigned ResultReg;
881 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
882 return false;
883 updateValueMap(I, ResultReg);
884 return true;
885}
886
887bool MipsFastISel::selectStore(const Instruction *I) {
888 Value *Op0 = I->getOperand(0);
889 unsigned SrcReg = 0;
890
891 // Atomic stores need special handling.
892 if (cast<StoreInst>(I)->isAtomic())
893 return false;
894
895 // Verify we have a legal type before going any further.
896 MVT VT;
897 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
898 return false;
899
900 // Get the value to be stored into a register.
901 SrcReg = getRegForValue(Op0);
902 if (SrcReg == 0)
903 return false;
904
905 // See if we can handle this address.
906 Address Addr;
907 if (!computeAddress(I->getOperand(1), Addr))
908 return false;
909
910 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
911 return false;
912 return true;
913}
914
915//
916// This can cause a redundant sltiu to be generated.
917// FIXME: try and eliminate this in a future patch.
918//
919bool MipsFastISel::selectBranch(const Instruction *I) {
920 const BranchInst *BI = cast<BranchInst>(I);
921 MachineBasicBlock *BrBB = FuncInfo.MBB;
922 //
923 // TBB is the basic block for the case where the comparison is true.
924 // FBB is the basic block for the case where the comparison is false.
925 // if (cond) goto TBB
926 // goto FBB
927 // TBB:
928 //
929 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
930 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
931 BI->getCondition();
932 // For now, just try the simplest case where it's fed by a compare.
933 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
934 unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
935 if (!emitCmp(CondReg, CI))
936 return false;
937 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
938 .addReg(CondReg)
939 .addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000940 finishCondBranch(BI->getParent(), TBB, FBB);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000941 return true;
942 }
943 return false;
944}
Reed Kotler62de6b92014-10-11 00:55:18 +0000945
Reed Kotlera562b462014-10-13 21:46:41 +0000946bool MipsFastISel::selectCmp(const Instruction *I) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000947 const CmpInst *CI = cast<CmpInst>(I);
948 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000949 if (!emitCmp(ResultReg, CI))
Reed Kotler62de6b92014-10-11 00:55:18 +0000950 return false;
Reed Kotler497311a2014-10-10 17:39:51 +0000951 updateValueMap(I, ResultReg);
952 return true;
953}
954
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000955// Attempt to fast-select a floating-point extend instruction.
956bool MipsFastISel::selectFPExt(const Instruction *I) {
957 if (UnsupportedFPMode)
958 return false;
959 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000960 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
961 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000962
963 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
964 return false;
965
966 unsigned SrcReg =
Nico Weber2cf5e892016-06-10 20:06:03 +0000967 getRegForValue(Src); // this must be a 32bit floating point register class
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000968 // maybe we should handle this differently
969 if (!SrcReg)
970 return false;
971
972 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
973 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
974 updateValueMap(I, DestReg);
975 return true;
976}
977
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000978bool MipsFastISel::selectSelect(const Instruction *I) {
979 assert(isa<SelectInst>(I) && "Expected a select instruction.");
980
Simon Dardisb432a3e2016-09-06 12:36:24 +0000981 DEBUG(dbgs() << "selectSelect\n");
982
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000983 MVT VT;
Simon Dardisb432a3e2016-09-06 12:36:24 +0000984 if (!isTypeSupported(I->getType(), VT) || UnsupportedFPMode) {
985 DEBUG(dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000986 return false;
Simon Dardisb432a3e2016-09-06 12:36:24 +0000987 }
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000988
989 unsigned CondMovOpc;
990 const TargetRegisterClass *RC;
991
992 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
993 CondMovOpc = Mips::MOVN_I_I;
994 RC = &Mips::GPR32RegClass;
995 } else if (VT == MVT::f32) {
996 CondMovOpc = Mips::MOVN_I_S;
997 RC = &Mips::FGR32RegClass;
998 } else if (VT == MVT::f64) {
999 CondMovOpc = Mips::MOVN_I_D32;
1000 RC = &Mips::AFGR64RegClass;
1001 } else
1002 return false;
1003
1004 const SelectInst *SI = cast<SelectInst>(I);
1005 const Value *Cond = SI->getCondition();
1006 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
1007 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
1008 unsigned CondReg = getRegForValue(Cond);
1009
1010 if (!Src1Reg || !Src2Reg || !CondReg)
1011 return false;
1012
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001013 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
1014 if (!ZExtCondReg)
1015 return false;
1016
1017 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true))
1018 return false;
1019
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001020 unsigned ResultReg = createResultReg(RC);
1021 unsigned TempReg = createResultReg(RC);
1022
1023 if (!ResultReg || !TempReg)
1024 return false;
1025
1026 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
1027 emitInst(CondMovOpc, ResultReg)
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001028 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001029 updateValueMap(I, ResultReg);
1030 return true;
1031}
1032
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001033// Attempt to fast-select a floating-point truncate instruction.
1034bool MipsFastISel::selectFPTrunc(const Instruction *I) {
1035 if (UnsupportedFPMode)
1036 return false;
1037 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001038 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1039 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001040
1041 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1042 return false;
1043
1044 unsigned SrcReg = getRegForValue(Src);
1045 if (!SrcReg)
1046 return false;
1047
1048 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1049 if (!DestReg)
1050 return false;
1051
1052 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1053 updateValueMap(I, DestReg);
1054 return true;
1055}
1056
1057// Attempt to fast-select a floating-point-to-integer conversion.
1058bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1059 if (UnsupportedFPMode)
1060 return false;
1061 MVT DstVT, SrcVT;
1062 if (!IsSigned)
1063 return false; // We don't handle this case yet. There is no native
1064 // instruction for this but it can be synthesized.
1065 Type *DstTy = I->getType();
1066 if (!isTypeLegal(DstTy, DstVT))
1067 return false;
1068
1069 if (DstVT != MVT::i32)
1070 return false;
1071
1072 Value *Src = I->getOperand(0);
1073 Type *SrcTy = Src->getType();
1074 if (!isTypeLegal(SrcTy, SrcVT))
1075 return false;
1076
1077 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1078 return false;
1079
1080 unsigned SrcReg = getRegForValue(Src);
1081 if (SrcReg == 0)
1082 return false;
1083
1084 // Determine the opcode for the conversion, which takes place
1085 // entirely within FPRs.
1086 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1087 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001088 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001089
1090 // Generate the convert.
1091 emitInst(Opc, TempReg).addReg(SrcReg);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001092 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1093
1094 updateValueMap(I, DestReg);
1095 return true;
1096}
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001097
Reed Kotlerd5c41962014-11-13 23:37:45 +00001098bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1099 SmallVectorImpl<MVT> &OutVTs,
1100 unsigned &NumBytes) {
1101 CallingConv::ID CC = CLI.CallConv;
1102 SmallVector<CCValAssign, 16> ArgLocs;
1103 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1104 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1105 // Get a count of how many bytes are to be pushed on the stack.
1106 NumBytes = CCInfo.getNextStackOffset();
1107 // This is the minimum argument area used for A0-A3.
1108 if (NumBytes < 16)
1109 NumBytes = 16;
1110
1111 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16);
1112 // Process the args.
1113 MVT firstMVT;
1114 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1115 CCValAssign &VA = ArgLocs[i];
1116 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1117 MVT ArgVT = OutVTs[VA.getValNo()];
1118
1119 if (i == 0) {
1120 firstMVT = ArgVT;
1121 if (ArgVT == MVT::f32) {
1122 VA.convertToReg(Mips::F12);
1123 } else if (ArgVT == MVT::f64) {
1124 VA.convertToReg(Mips::D6);
1125 }
1126 } else if (i == 1) {
1127 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1128 if (ArgVT == MVT::f32) {
1129 VA.convertToReg(Mips::F14);
1130 } else if (ArgVT == MVT::f64) {
1131 VA.convertToReg(Mips::D7);
1132 }
1133 }
1134 }
Vasileios Kalintirisb48c9052015-05-12 12:29:17 +00001135 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1136 (ArgVT == MVT::i8)) &&
1137 VA.isMemLoc()) {
Reed Kotlerd5c41962014-11-13 23:37:45 +00001138 switch (VA.getLocMemOffset()) {
1139 case 0:
1140 VA.convertToReg(Mips::A0);
1141 break;
1142 case 4:
1143 VA.convertToReg(Mips::A1);
1144 break;
1145 case 8:
1146 VA.convertToReg(Mips::A2);
1147 break;
1148 case 12:
1149 VA.convertToReg(Mips::A3);
1150 break;
1151 default:
1152 break;
1153 }
1154 }
1155 unsigned ArgReg = getRegForValue(ArgVal);
1156 if (!ArgReg)
1157 return false;
1158
1159 // Handle arg promotion: SExt, ZExt, AExt.
1160 switch (VA.getLocInfo()) {
1161 case CCValAssign::Full:
1162 break;
1163 case CCValAssign::AExt:
1164 case CCValAssign::SExt: {
1165 MVT DestVT = VA.getLocVT();
1166 MVT SrcVT = ArgVT;
1167 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1168 if (!ArgReg)
1169 return false;
1170 break;
1171 }
1172 case CCValAssign::ZExt: {
1173 MVT DestVT = VA.getLocVT();
1174 MVT SrcVT = ArgVT;
1175 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1176 if (!ArgReg)
1177 return false;
1178 break;
1179 }
1180 default:
1181 llvm_unreachable("Unknown arg promotion!");
1182 }
1183
1184 // Now copy/store arg to correct locations.
1185 if (VA.isRegLoc() && !VA.needsCustom()) {
1186 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1187 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1188 CLI.OutRegs.push_back(VA.getLocReg());
1189 } else if (VA.needsCustom()) {
1190 llvm_unreachable("Mips does not use custom args.");
1191 return false;
1192 } else {
1193 //
1194 // FIXME: This path will currently return false. It was copied
1195 // from the AArch64 port and should be essentially fine for Mips too.
1196 // The work to finish up this path will be done in a follow-on patch.
1197 //
1198 assert(VA.isMemLoc() && "Assuming store on stack.");
1199 // Don't emit stores for undef values.
1200 if (isa<UndefValue>(ArgVal))
1201 continue;
1202
1203 // Need to store on the stack.
1204 // FIXME: This alignment is incorrect but this path is disabled
1205 // for now (will return false). We need to determine the right alignment
1206 // based on the normal alignment for the underlying machine type.
1207 //
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001208 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001209
1210 unsigned BEAlign = 0;
1211 if (ArgSize < 8 && !Subtarget->isLittle())
1212 BEAlign = 8 - ArgSize;
1213
1214 Address Addr;
1215 Addr.setKind(Address::RegBase);
1216 Addr.setReg(Mips::SP);
1217 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1218
1219 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1220 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00001221 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
Reed Kotlerd5c41962014-11-13 23:37:45 +00001222 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1223 (void)(MMO);
1224 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1225 return false; // can't store on the stack yet.
1226 }
1227 }
1228
1229 return true;
1230}
1231
1232bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1233 unsigned NumBytes) {
1234 CallingConv::ID CC = CLI.CallConv;
Daniel Sanders01bcefd2016-05-03 14:19:26 +00001235 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001236 if (RetVT != MVT::isVoid) {
1237 SmallVector<CCValAssign, 16> RVLocs;
1238 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1239 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips);
1240
1241 // Only handle a single return value.
1242 if (RVLocs.size() != 1)
1243 return false;
1244 // Copy all of the result registers out of their specified physreg.
1245 MVT CopyVT = RVLocs[0].getValVT();
1246 // Special handling for extended integers.
1247 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1248 CopyVT = MVT::i32;
1249
1250 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001251 if (!ResultReg)
1252 return false;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1254 TII.get(TargetOpcode::COPY),
1255 ResultReg).addReg(RVLocs[0].getLocReg());
1256 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1257
1258 CLI.ResultReg = ResultReg;
1259 CLI.NumResultRegs = 1;
1260 }
1261 return true;
1262}
1263
Daniel Sanderscbaca422016-07-29 12:27:28 +00001264bool MipsFastISel::fastLowerArguments() {
1265 DEBUG(dbgs() << "fastLowerArguments\n");
1266
1267 if (!FuncInfo.CanLowerReturn) {
1268 DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1269 return false;
1270 }
1271
1272 const Function *F = FuncInfo.Fn;
1273 if (F->isVarArg()) {
1274 DEBUG(dbgs() << ".. gave up (varargs)\n");
1275 return false;
1276 }
1277
1278 CallingConv::ID CC = F->getCallingConv();
1279 if (CC != CallingConv::C) {
1280 DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1281 return false;
1282 }
1283
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001284 const ArrayRef<MCPhysReg> GPR32ArgRegs = {Mips::A0, Mips::A1, Mips::A2,
1285 Mips::A3};
1286 const ArrayRef<MCPhysReg> FGR32ArgRegs = {Mips::F12, Mips::F14};
1287 const ArrayRef<MCPhysReg> AFGR64ArgRegs = {Mips::D6, Mips::D7};
1288 ArrayRef<MCPhysReg>::iterator NextGPR32 = GPR32ArgRegs.begin();
1289 ArrayRef<MCPhysReg>::iterator NextFGR32 = FGR32ArgRegs.begin();
1290 ArrayRef<MCPhysReg>::iterator NextAFGR64 = AFGR64ArgRegs.begin();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001291
1292 struct AllocatedReg {
1293 const TargetRegisterClass *RC;
1294 unsigned Reg;
1295 AllocatedReg(const TargetRegisterClass *RC, unsigned Reg)
1296 : RC(RC), Reg(Reg) {}
1297 };
1298
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001299 // Only handle simple cases. i.e. All arguments are directly mapped to
1300 // registers of the appropriate type.
Daniel Sanderscbaca422016-07-29 12:27:28 +00001301 SmallVector<AllocatedReg, 4> Allocation;
1302 unsigned Idx = 1;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001303 for (const auto &FormalArg : F->args()) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001304 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1305 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1306 F->getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
1307 DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1308 return false;
1309 }
1310
1311 Type *ArgTy = FormalArg.getType();
1312 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) {
1313 DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1314 return false;
1315 }
1316
1317 EVT ArgVT = TLI.getValueType(DL, ArgTy);
1318 DEBUG(dbgs() << ".. " << (Idx - 1) << ": " << ArgVT.getEVTString() << "\n");
1319 if (!ArgVT.isSimple()) {
1320 DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1321 return false;
1322 }
1323
1324 switch (ArgVT.getSimpleVT().SimpleTy) {
1325 case MVT::i1:
1326 case MVT::i8:
1327 case MVT::i16:
1328 if (!F->getAttributes().hasAttribute(Idx, Attribute::SExt) &&
1329 !F->getAttributes().hasAttribute(Idx, Attribute::ZExt)) {
1330 // It must be any extend, this shouldn't happen for clang-generated IR
1331 // so just fall back on SelectionDAG.
1332 DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1333 return false;
1334 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001335
1336 if (NextGPR32 == GPR32ArgRegs.end()) {
1337 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1338 return false;
1339 }
1340
1341 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1342 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1343
1344 // Allocating any GPR32 prohibits further use of floating point arguments.
1345 NextFGR32 = FGR32ArgRegs.end();
1346 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001347 break;
1348
1349 case MVT::i32:
1350 if (F->getAttributes().hasAttribute(Idx, Attribute::ZExt)) {
1351 // The O32 ABI does not permit a zero-extended i32.
1352 DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1353 return false;
1354 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001355
1356 if (NextGPR32 == GPR32ArgRegs.end()) {
1357 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1358 return false;
1359 }
1360
1361 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1362 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1363
1364 // Allocating any GPR32 prohibits further use of floating point arguments.
1365 NextFGR32 = FGR32ArgRegs.end();
1366 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001367 break;
1368
1369 case MVT::f32:
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001370 if (NextFGR32 == FGR32ArgRegs.end()) {
1371 DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001372 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001373 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001374 DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32 << ")\n");
1375 Allocation.emplace_back(&Mips::FGR32RegClass, *NextFGR32++);
1376 // Allocating an FGR32 also allocates the super-register AFGR64, and
1377 // ABI rules require us to skip the corresponding GPR32.
1378 if (NextGPR32 != GPR32ArgRegs.end())
1379 NextGPR32++;
1380 if (NextAFGR64 != AFGR64ArgRegs.end())
1381 NextAFGR64++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001382 break;
1383
1384 case MVT::f64:
Simon Dardisb432a3e2016-09-06 12:36:24 +00001385 if (UnsupportedFPMode) {
1386 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1387 return false;
1388 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001389 if (NextAFGR64 == AFGR64ArgRegs.end()) {
1390 DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001391 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001392 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001393 DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64 << ")\n");
1394 Allocation.emplace_back(&Mips::AFGR64RegClass, *NextAFGR64++);
1395 // Allocating an FGR32 also allocates the super-register AFGR64, and
1396 // ABI rules require us to skip the corresponding GPR32 pair.
1397 if (NextGPR32 != GPR32ArgRegs.end())
1398 NextGPR32++;
1399 if (NextGPR32 != GPR32ArgRegs.end())
1400 NextGPR32++;
1401 if (NextFGR32 != FGR32ArgRegs.end())
1402 NextFGR32++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001403 break;
1404
1405 default:
1406 DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1407 return false;
1408 }
1409
1410 ++Idx;
1411 }
1412
1413 Idx = 0;
1414 for (const auto &FormalArg : F->args()) {
1415 unsigned SrcReg = Allocation[Idx].Reg;
1416 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, Allocation[Idx].RC);
1417 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1418 // Without this, EmitLiveInCopies may eliminate the livein if its only
1419 // use is a bitcast (which isn't turned into an instruction).
1420 unsigned ResultReg = createResultReg(Allocation[Idx].RC);
1421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1422 TII.get(TargetOpcode::COPY), ResultReg)
1423 .addReg(DstReg, getKillRegState(true));
1424 updateValueMap(&FormalArg, ResultReg);
1425 ++Idx;
1426 }
1427
1428 // Calculate the size of the incoming arguments area.
1429 // We currently reject all the cases where this would be non-zero.
1430 unsigned IncomingArgSizeInBytes = 0;
1431
1432 // Account for the reserved argument area on ABI's that have one (O32).
1433 // It seems strange to do this on the caller side but it's necessary in
1434 // SelectionDAG's implementation.
1435 IncomingArgSizeInBytes = std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC),
1436 IncomingArgSizeInBytes);
1437
1438 MF->getInfo<MipsFunctionInfo>()->setFormalArgInfo(IncomingArgSizeInBytes,
1439 false);
1440
1441 return true;
1442}
1443
Reed Kotlerd5c41962014-11-13 23:37:45 +00001444bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
Vasileios Kalintiris2f12b2e2015-08-04 14:35:50 +00001445 if (!TargetSupported)
1446 return false;
1447
Reed Kotlerd5c41962014-11-13 23:37:45 +00001448 CallingConv::ID CC = CLI.CallConv;
1449 bool IsTailCall = CLI.IsTailCall;
1450 bool IsVarArg = CLI.IsVarArg;
1451 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001452 MCSymbol *Symbol = CLI.Symbol;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001453
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001454 // Do not handle FastCC.
1455 if (CC == CallingConv::Fast)
1456 return false;
1457
Reed Kotlerd5c41962014-11-13 23:37:45 +00001458 // Allow SelectionDAG isel to handle tail calls.
1459 if (IsTailCall)
1460 return false;
1461
1462 // Let SDISel handle vararg functions.
1463 if (IsVarArg)
1464 return false;
1465
1466 // FIXME: Only handle *simple* calls for now.
1467 MVT RetVT;
1468 if (CLI.RetTy->isVoidTy())
1469 RetVT = MVT::isVoid;
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001470 else if (!isTypeSupported(CLI.RetTy, RetVT))
Reed Kotlerd5c41962014-11-13 23:37:45 +00001471 return false;
1472
1473 for (auto Flag : CLI.OutFlags)
1474 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1475 return false;
1476
1477 // Set up the argument vectors.
1478 SmallVector<MVT, 16> OutVTs;
1479 OutVTs.reserve(CLI.OutVals.size());
1480
1481 for (auto *Val : CLI.OutVals) {
1482 MVT VT;
1483 if (!isTypeLegal(Val->getType(), VT) &&
1484 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1485 return false;
1486
1487 // We don't handle vector parameters yet.
1488 if (VT.isVector() || VT.getSizeInBits() > 64)
1489 return false;
1490
1491 OutVTs.push_back(VT);
1492 }
1493
1494 Address Addr;
1495 if (!computeCallAddress(Callee, Addr))
1496 return false;
1497
1498 // Handle the arguments now that we've gotten them.
1499 unsigned NumBytes;
1500 if (!processCallArgs(CLI, OutVTs, NumBytes))
1501 return false;
1502
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001503 if (!Addr.getGlobalValue())
1504 return false;
1505
Reed Kotlerd5c41962014-11-13 23:37:45 +00001506 // Issue the call.
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001507 unsigned DestAddress;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001508 if (Symbol)
1509 DestAddress = materializeExternalCallSym(Symbol);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001510 else
1511 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001512 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1513 MachineInstrBuilder MIB =
1514 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1515 Mips::RA).addReg(Mips::T9);
1516
1517 // Add implicit physical register uses to the call.
1518 for (auto Reg : CLI.OutRegs)
1519 MIB.addReg(Reg, RegState::Implicit);
1520
1521 // Add a register mask with the call-preserved registers.
1522 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001523 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Reed Kotlerd5c41962014-11-13 23:37:45 +00001524
1525 CLI.Call = MIB;
1526
Reed Kotlerd5c41962014-11-13 23:37:45 +00001527 // Finish off the call including any return values.
1528 return finishCall(CLI, RetVT, NumBytes);
1529}
1530
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001531bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
Vasileios Kalintiris2f12b2e2015-08-04 14:35:50 +00001532 if (!TargetSupported)
1533 return false;
1534
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001535 switch (II->getIntrinsicID()) {
1536 default:
1537 return false;
Vasileios Kalintiriscbbf8e02015-06-01 16:40:45 +00001538 case Intrinsic::bswap: {
1539 Type *RetTy = II->getCalledFunction()->getReturnType();
1540
1541 MVT VT;
1542 if (!isTypeSupported(RetTy, VT))
1543 return false;
1544
1545 unsigned SrcReg = getRegForValue(II->getOperand(0));
1546 if (SrcReg == 0)
1547 return false;
1548 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1549 if (DestReg == 0)
1550 return false;
1551 if (VT == MVT::i16) {
1552 if (Subtarget->hasMips32r2()) {
1553 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1554 updateValueMap(II, DestReg);
1555 return true;
1556 } else {
1557 unsigned TempReg[3];
1558 for (int i = 0; i < 3; i++) {
1559 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1560 if (TempReg[i] == 0)
1561 return false;
1562 }
1563 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1564 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1565 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1566 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1567 updateValueMap(II, DestReg);
1568 return true;
1569 }
1570 } else if (VT == MVT::i32) {
1571 if (Subtarget->hasMips32r2()) {
1572 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1573 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1574 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1575 updateValueMap(II, DestReg);
1576 return true;
1577 } else {
1578 unsigned TempReg[8];
1579 for (int i = 0; i < 8; i++) {
1580 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1581 if (TempReg[i] == 0)
1582 return false;
1583 }
1584
1585 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1586 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1587 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1588 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1589
1590 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1591 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1592
1593 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1594 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1595 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1596 updateValueMap(II, DestReg);
1597 return true;
1598 }
1599 }
1600 return false;
1601 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001602 case Intrinsic::memcpy:
1603 case Intrinsic::memmove: {
1604 const auto *MTI = cast<MemTransferInst>(II);
1605 // Don't handle volatile.
1606 if (MTI->isVolatile())
1607 return false;
1608 if (!MTI->getLength()->getType()->isIntegerTy(32))
1609 return false;
1610 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
Pete Cooper67cf9a72015-11-19 05:56:52 +00001611 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001612 }
1613 case Intrinsic::memset: {
1614 const MemSetInst *MSI = cast<MemSetInst>(II);
1615 // Don't handle volatile.
1616 if (MSI->isVolatile())
1617 return false;
1618 if (!MSI->getLength()->getType()->isIntegerTy(32))
1619 return false;
Pete Cooper67cf9a72015-11-19 05:56:52 +00001620 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001621 }
1622 }
1623 return false;
1624}
1625
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001626bool MipsFastISel::selectRet(const Instruction *I) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001627 const Function &F = *I->getParent()->getParent();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001628 const ReturnInst *Ret = cast<ReturnInst>(I);
1629
Simon Dardisb432a3e2016-09-06 12:36:24 +00001630 DEBUG(dbgs() << "selectRet\n");
1631
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001632 if (!FuncInfo.CanLowerReturn)
1633 return false;
Reed Kotleraa150ed2015-02-12 21:05:12 +00001634
1635 // Build a list of return value registers.
1636 SmallVector<unsigned, 4> RetRegs;
1637
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001638 if (Ret->getNumOperands() > 0) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001639 CallingConv::ID CC = F.getCallingConv();
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001640
1641 // Do not handle FastCC.
1642 if (CC == CallingConv::Fast)
1643 return false;
1644
Reed Kotleraa150ed2015-02-12 21:05:12 +00001645 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001646 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1647
Reed Kotleraa150ed2015-02-12 21:05:12 +00001648 // Analyze operands of the call, assigning locations to each operand.
1649 SmallVector<CCValAssign, 16> ValLocs;
1650 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1651 I->getContext());
1652 CCAssignFn *RetCC = RetCC_Mips;
1653 CCInfo.AnalyzeReturn(Outs, RetCC);
1654
1655 // Only handle a single return value for now.
1656 if (ValLocs.size() != 1)
1657 return false;
1658
1659 CCValAssign &VA = ValLocs[0];
1660 const Value *RV = Ret->getOperand(0);
1661
1662 // Don't bother handling odd stuff for now.
1663 if ((VA.getLocInfo() != CCValAssign::Full) &&
1664 (VA.getLocInfo() != CCValAssign::BCvt))
1665 return false;
1666
1667 // Only handle register returns for now.
1668 if (!VA.isRegLoc())
1669 return false;
1670
1671 unsigned Reg = getRegForValue(RV);
1672 if (Reg == 0)
1673 return false;
1674
1675 unsigned SrcReg = Reg + VA.getValNo();
1676 unsigned DestReg = VA.getLocReg();
1677 // Avoid a cross-class copy. This is very unlikely.
1678 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1679 return false;
1680
Mehdi Amini44ede332015-07-09 02:09:04 +00001681 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Reed Kotleraa150ed2015-02-12 21:05:12 +00001682 if (!RVEVT.isSimple())
1683 return false;
1684
1685 if (RVEVT.isVector())
1686 return false;
1687
1688 MVT RVVT = RVEVT.getSimpleVT();
1689 if (RVVT == MVT::f128)
1690 return false;
1691
Simon Dardisb432a3e2016-09-06 12:36:24 +00001692 // Do not handle FGR64 returns for now.
1693 if (RVVT == MVT::f64 && UnsupportedFPMode) {
1694 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1695 return false;
1696 }
1697
Reed Kotleraa150ed2015-02-12 21:05:12 +00001698 MVT DestVT = VA.getValVT();
1699 // Special handling for extended integers.
1700 if (RVVT != DestVT) {
1701 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1702 return false;
1703
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001704 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1705 bool IsZExt = Outs[0].Flags.isZExt();
1706 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1707 if (SrcReg == 0)
1708 return false;
1709 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001710 }
1711
1712 // Make the copy.
1713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1714 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1715
1716 // Add register to return instruction.
1717 RetRegs.push_back(VA.getLocReg());
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001718 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001719 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1720 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1721 MIB.addReg(RetRegs[i], RegState::Implicit);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001722 return true;
1723}
1724
1725bool MipsFastISel::selectTrunc(const Instruction *I) {
1726 // The high bits for a type smaller than the register size are assumed to be
1727 // undefined.
1728 Value *Op = I->getOperand(0);
1729
1730 EVT SrcVT, DestVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001731 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1732 DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001733
1734 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1735 return false;
1736 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1737 return false;
1738
1739 unsigned SrcReg = getRegForValue(Op);
1740 if (!SrcReg)
1741 return false;
1742
1743 // Because the high bits are undefined, a truncate doesn't generate
1744 // any code.
1745 updateValueMap(I, SrcReg);
1746 return true;
1747}
1748bool MipsFastISel::selectIntExt(const Instruction *I) {
1749 Type *DestTy = I->getType();
1750 Value *Src = I->getOperand(0);
1751 Type *SrcTy = Src->getType();
1752
1753 bool isZExt = isa<ZExtInst>(I);
1754 unsigned SrcReg = getRegForValue(Src);
1755 if (!SrcReg)
1756 return false;
1757
1758 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001759 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1760 DestEVT = TLI.getValueType(DL, DestTy, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001761 if (!SrcEVT.isSimple())
1762 return false;
1763 if (!DestEVT.isSimple())
1764 return false;
1765
1766 MVT SrcVT = SrcEVT.getSimpleVT();
1767 MVT DestVT = DestEVT.getSimpleVT();
1768 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1769
1770 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1771 return false;
1772 updateValueMap(I, ResultReg);
1773 return true;
1774}
1775bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1776 unsigned DestReg) {
1777 unsigned ShiftAmt;
1778 switch (SrcVT.SimpleTy) {
1779 default:
1780 return false;
1781 case MVT::i8:
1782 ShiftAmt = 24;
1783 break;
1784 case MVT::i16:
1785 ShiftAmt = 16;
1786 break;
1787 }
1788 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1789 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1790 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1791 return true;
1792}
1793
1794bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1795 unsigned DestReg) {
1796 switch (SrcVT.SimpleTy) {
1797 default:
1798 return false;
1799 case MVT::i8:
1800 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1801 break;
1802 case MVT::i16:
1803 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1804 break;
1805 }
1806 return true;
1807}
1808
1809bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1810 unsigned DestReg) {
1811 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1812 return false;
1813 if (Subtarget->hasMips32r2())
1814 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1815 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1816}
1817
1818bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1819 unsigned DestReg) {
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001820 int64_t Imm;
1821
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001822 switch (SrcVT.SimpleTy) {
1823 default:
1824 return false;
1825 case MVT::i1:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001826 Imm = 1;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001827 break;
1828 case MVT::i8:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001829 Imm = 0xff;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001830 break;
1831 case MVT::i16:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001832 Imm = 0xffff;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001833 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001834 }
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001835
1836 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001837 return true;
1838}
1839
1840bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1841 unsigned DestReg, bool IsZExt) {
Vasileios Kalintiris1202f362015-04-24 13:48:19 +00001842 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1843 // DestVT are odd things, so test to make sure that they are both types we can
1844 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1845 // bail out to SelectionDAG.
1846 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1847 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1848 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001849 if (IsZExt)
1850 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1851 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1852}
Reed Kotlerd5c41962014-11-13 23:37:45 +00001853
1854unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1855 bool isZExt) {
1856 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotleraa150ed2015-02-12 21:05:12 +00001857 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1858 return Success ? DestReg : 0;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001859}
1860
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001861bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001862 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001863 if (!DestEVT.isSimple())
1864 return false;
1865
1866 MVT DestVT = DestEVT.getSimpleVT();
1867 if (DestVT != MVT::i32)
1868 return false;
1869
1870 unsigned DivOpc;
1871 switch (ISDOpcode) {
1872 default:
1873 return false;
1874 case ISD::SDIV:
1875 case ISD::SREM:
1876 DivOpc = Mips::SDIV;
1877 break;
1878 case ISD::UDIV:
1879 case ISD::UREM:
1880 DivOpc = Mips::UDIV;
1881 break;
1882 }
1883
1884 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1885 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1886 if (!Src0Reg || !Src1Reg)
1887 return false;
1888
1889 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1890 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1891
1892 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1893 if (!ResultReg)
1894 return false;
1895
1896 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1897 ? Mips::MFHI
1898 : Mips::MFLO;
1899 emitInst(MFOpc, ResultReg);
1900
1901 updateValueMap(I, ResultReg);
1902 return true;
1903}
1904
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001905bool MipsFastISel::selectShift(const Instruction *I) {
1906 MVT RetVT;
1907
1908 if (!isTypeSupported(I->getType(), RetVT))
1909 return false;
1910
1911 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1912 if (!ResultReg)
1913 return false;
1914
1915 unsigned Opcode = I->getOpcode();
1916 const Value *Op0 = I->getOperand(0);
1917 unsigned Op0Reg = getRegForValue(Op0);
1918 if (!Op0Reg)
1919 return false;
1920
1921 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1922 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1923 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1924 if (!TempReg)
1925 return false;
1926
Mehdi Amini44ede332015-07-09 02:09:04 +00001927 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001928 bool IsZExt = Opcode == Instruction::LShr;
1929 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1930 return false;
1931
1932 Op0Reg = TempReg;
1933 }
1934
1935 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1936 uint64_t ShiftVal = C->getZExtValue();
1937
1938 switch (Opcode) {
1939 default:
1940 llvm_unreachable("Unexpected instruction.");
1941 case Instruction::Shl:
1942 Opcode = Mips::SLL;
1943 break;
1944 case Instruction::AShr:
1945 Opcode = Mips::SRA;
1946 break;
1947 case Instruction::LShr:
1948 Opcode = Mips::SRL;
1949 break;
1950 }
1951
1952 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1953 updateValueMap(I, ResultReg);
1954 return true;
1955 }
1956
1957 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1958 if (!Op1Reg)
1959 return false;
1960
1961 switch (Opcode) {
1962 default:
1963 llvm_unreachable("Unexpected instruction.");
1964 case Instruction::Shl:
1965 Opcode = Mips::SLLV;
1966 break;
1967 case Instruction::AShr:
1968 Opcode = Mips::SRAV;
1969 break;
1970 case Instruction::LShr:
1971 Opcode = Mips::SRLV;
1972 break;
1973 }
1974
1975 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1976 updateValueMap(I, ResultReg);
1977 return true;
1978}
1979
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001980bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
Reed Kotler67077b32014-04-29 17:57:50 +00001981 if (!TargetSupported)
1982 return false;
1983 switch (I->getOpcode()) {
1984 default:
1985 break;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +00001986 case Instruction::Load:
Reed Kotlera562b462014-10-13 21:46:41 +00001987 return selectLoad(I);
Reed Kotlerbab3f232014-05-01 20:39:21 +00001988 case Instruction::Store:
Reed Kotlera562b462014-10-13 21:46:41 +00001989 return selectStore(I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001990 case Instruction::SDiv:
1991 if (!selectBinaryOp(I, ISD::SDIV))
1992 return selectDivRem(I, ISD::SDIV);
1993 return true;
1994 case Instruction::UDiv:
1995 if (!selectBinaryOp(I, ISD::UDIV))
1996 return selectDivRem(I, ISD::UDIV);
1997 return true;
1998 case Instruction::SRem:
1999 if (!selectBinaryOp(I, ISD::SREM))
2000 return selectDivRem(I, ISD::SREM);
2001 return true;
2002 case Instruction::URem:
2003 if (!selectBinaryOp(I, ISD::UREM))
2004 return selectDivRem(I, ISD::UREM);
2005 return true;
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00002006 case Instruction::Shl:
2007 case Instruction::LShr:
2008 case Instruction::AShr:
2009 return selectShift(I);
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002010 case Instruction::And:
2011 case Instruction::Or:
2012 case Instruction::Xor:
2013 return selectLogicalOp(I);
Reed Kotler62de6b92014-10-11 00:55:18 +00002014 case Instruction::Br:
Reed Kotlera562b462014-10-13 21:46:41 +00002015 return selectBranch(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002016 case Instruction::Ret:
Reed Kotlera562b462014-10-13 21:46:41 +00002017 return selectRet(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002018 case Instruction::Trunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002019 return selectTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002020 case Instruction::ZExt:
2021 case Instruction::SExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002022 return selectIntExt(I);
Reed Kotlerb9dc2482014-10-01 18:47:02 +00002023 case Instruction::FPTrunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002024 return selectFPTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002025 case Instruction::FPExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002026 return selectFPExt(I);
Reed Kotler12f94882014-10-10 17:00:46 +00002027 case Instruction::FPToSI:
Reed Kotlera562b462014-10-13 21:46:41 +00002028 return selectFPToInt(I, /*isSigned*/ true);
Reed Kotler12f94882014-10-10 17:00:46 +00002029 case Instruction::FPToUI:
Reed Kotlera562b462014-10-13 21:46:41 +00002030 return selectFPToInt(I, /*isSigned*/ false);
Reed Kotler497311a2014-10-10 17:39:51 +00002031 case Instruction::ICmp:
2032 case Instruction::FCmp:
Reed Kotlera562b462014-10-13 21:46:41 +00002033 return selectCmp(I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00002034 case Instruction::Select:
2035 return selectSelect(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002036 }
2037 return false;
2038}
Reed Kotler720c5ca2014-04-17 22:15:34 +00002039
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002040unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
2041 bool IsUnsigned) {
2042 unsigned VReg = getRegForValue(V);
2043 if (VReg == 0)
Reed Kotler12f94882014-10-10 17:00:46 +00002044 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00002045 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002046 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
2047 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
2048 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
2049 return 0;
2050 VReg = TempReg;
Reed Kotler063d4fb2014-06-10 16:45:44 +00002051 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002052 return VReg;
Reed Kotlerbab3f232014-05-01 20:39:21 +00002053}
2054
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002055void MipsFastISel::simplifyAddress(Address &Addr) {
2056 if (!isInt<16>(Addr.getOffset())) {
2057 unsigned TempReg =
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002058 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002059 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
2060 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
2061 Addr.setReg(DestReg);
2062 Addr.setOffset(0);
2063 }
2064}
2065
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +00002066unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2067 const TargetRegisterClass *RC,
2068 unsigned Op0, bool Op0IsKill,
2069 unsigned Op1, bool Op1IsKill) {
2070 // We treat the MUL instruction in a special way because it clobbers
2071 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2072 // mark these registers only as implicitly defined. As a result, the
2073 // register allocator runs out of registers when this instruction is
2074 // followed by another instruction that defines the same registers too.
2075 // We can fix this by explicitly marking those registers as dead.
2076 if (MachineInstOpcode == Mips::MUL) {
2077 unsigned ResultReg = createResultReg(RC);
2078 const MCInstrDesc &II = TII.get(MachineInstOpcode);
2079 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
2080 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
2081 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
2082 .addReg(Op0, getKillRegState(Op0IsKill))
2083 .addReg(Op1, getKillRegState(Op1IsKill))
2084 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
2085 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
2086 return ResultReg;
2087 }
2088
2089 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
2090 Op1IsKill);
2091}
2092
Reed Kotler720c5ca2014-04-17 22:15:34 +00002093namespace llvm {
2094FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
2095 const TargetLibraryInfo *libInfo) {
2096 return new MipsFastISel(funcInfo, libInfo);
2097}
2098}