blob: 3b0edfe0b632e5ec977b53f38800720c14eeda53 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file defines the MIPS-specific support for the FastISel class.
Vasileios Kalintirisa9e51542016-06-08 13:13:15 +000012/// 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
Eugene Zelenkodde94e42017-01-30 23:21:32 +000017#include "MCTargetDesc/MipsABIInfo.h"
18#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "MipsCCState.h"
20#include "MipsISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "MipsInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "MipsMachineFunction.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000023#include "MipsSubtarget.h"
24#include "MipsTargetMachine.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000025#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/SmallVector.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000029#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000030#include "llvm/CodeGen/CallingConvLower.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000031#include "llvm/CodeGen/FastISel.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000032#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000033#include "llvm/CodeGen/ISDOpcodes.h"
34#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
Reed Kotler67077b32014-04-29 17:57:50 +000036#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000037#include "llvm/CodeGen/MachineMemOperand.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000039#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetLowering.h"
Craig Topper2fa14362018-03-29 17:21:10 +000041#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000042#include "llvm/IR/Attributes.h"
43#include "llvm/IR/CallingConv.h"
44#include "llvm/IR/Constant.h"
45#include "llvm/IR/Constants.h"
46#include "llvm/IR/DataLayout.h"
47#include "llvm/IR/Function.h"
David Blaikie457343d2015-05-21 21:12:43 +000048#include "llvm/IR/GetElementPtrTypeIterator.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000049#include "llvm/IR/GlobalValue.h"
Reed Kotlerbab3f232014-05-01 20:39:21 +000050#include "llvm/IR/GlobalVariable.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000051#include "llvm/IR/InstrTypes.h"
52#include "llvm/IR/Instruction.h"
53#include "llvm/IR/Instructions.h"
54#include "llvm/IR/IntrinsicInst.h"
55#include "llvm/IR/Operator.h"
56#include "llvm/IR/Type.h"
57#include "llvm/IR/User.h"
58#include "llvm/IR/Value.h"
59#include "llvm/MC/MCInstrDesc.h"
60#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000061#include "llvm/MC/MCSymbol.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000062#include "llvm/Support/Casting.h"
63#include "llvm/Support/Compiler.h"
Daniel Sanderscbaca422016-07-29 12:27:28 +000064#include "llvm/Support/Debug.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000065#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000066#include "llvm/Support/MachineValueType.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000067#include "llvm/Support/MathExtras.h"
68#include "llvm/Support/raw_ostream.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000069#include <algorithm>
Manoj Gupta9d68b9e2018-04-05 22:47:25 +000070#include <array>
Eugene Zelenkodde94e42017-01-30 23:21:32 +000071#include <cassert>
72#include <cstdint>
Daniel Sanderscbaca422016-07-29 12:27:28 +000073
74#define DEBUG_TYPE "mips-fastisel"
Reed Kotler720c5ca2014-04-17 22:15:34 +000075
76using namespace llvm;
77
78namespace {
79
80class MipsFastISel final : public FastISel {
81
Reed Kotlera562b462014-10-13 21:46:41 +000082 // All possible address modes.
83 class Address {
84 public:
Eugene Zelenko79220eae2017-08-03 22:12:30 +000085 using BaseKind = enum { RegBase, FrameIndexBase };
Reed Kotlera562b462014-10-13 21:46:41 +000086
87 private:
Eugene Zelenkodde94e42017-01-30 23:21:32 +000088 BaseKind Kind = RegBase;
Reed Kotlera562b462014-10-13 21:46:41 +000089 union {
90 unsigned Reg;
91 int FI;
92 } Base;
93
Eugene Zelenkodde94e42017-01-30 23:21:32 +000094 int64_t Offset = 0;
Reed Kotlera562b462014-10-13 21:46:41 +000095
Eugene Zelenkodde94e42017-01-30 23:21:32 +000096 const GlobalValue *GV = nullptr;
Reed Kotlera562b462014-10-13 21:46:41 +000097
98 public:
99 // Innocuous defaults for our address.
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000100 Address() { Base.Reg = 0; }
101
Reed Kotlera562b462014-10-13 21:46:41 +0000102 void setKind(BaseKind K) { Kind = K; }
103 BaseKind getKind() const { return Kind; }
104 bool isRegBase() const { return Kind == RegBase; }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000105 bool isFIBase() const { return Kind == FrameIndexBase; }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000106
Reed Kotlera562b462014-10-13 21:46:41 +0000107 void setReg(unsigned Reg) {
108 assert(isRegBase() && "Invalid base register access!");
109 Base.Reg = Reg;
110 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000111
Reed Kotlera562b462014-10-13 21:46:41 +0000112 unsigned getReg() const {
113 assert(isRegBase() && "Invalid base register access!");
114 return Base.Reg;
115 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000116
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000117 void setFI(unsigned FI) {
118 assert(isFIBase() && "Invalid base frame index access!");
119 Base.FI = FI;
120 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000121
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000122 unsigned getFI() const {
123 assert(isFIBase() && "Invalid base frame index access!");
124 return Base.FI;
125 }
126
Reed Kotlera562b462014-10-13 21:46:41 +0000127 void setOffset(int64_t Offset_) { Offset = Offset_; }
128 int64_t getOffset() const { return Offset; }
129 void setGlobalValue(const GlobalValue *G) { GV = G; }
130 const GlobalValue *getGlobalValue() { return GV; }
131 };
132
Reed Kotler67077b32014-04-29 17:57:50 +0000133 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
134 /// make the right decision when generating code for different targets.
Reed Kotler67077b32014-04-29 17:57:50 +0000135 const TargetMachine &TM;
Eric Christopher96e72c62015-01-29 23:27:36 +0000136 const MipsSubtarget *Subtarget;
Reed Kotler67077b32014-04-29 17:57:50 +0000137 const TargetInstrInfo &TII;
138 const TargetLowering &TLI;
139 MipsFunctionInfo *MFI;
140
141 // Convenience variables to avoid some queries.
142 LLVMContext *Context;
143
Daniel Sanderscbaca422016-07-29 12:27:28 +0000144 bool fastLowerArguments() override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000145 bool fastLowerCall(CallLoweringInfo &CLI) override;
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000146 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000147
Reed Kotlera562b462014-10-13 21:46:41 +0000148 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
149 // floating point but not reject doing fast-isel in other
150 // situations
151
152private:
153 // Selection routines.
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000154 bool selectLogicalOp(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000155 bool selectLoad(const Instruction *I);
156 bool selectStore(const Instruction *I);
157 bool selectBranch(const Instruction *I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000158 bool selectSelect(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000159 bool selectCmp(const Instruction *I);
160 bool selectFPExt(const Instruction *I);
161 bool selectFPTrunc(const Instruction *I);
162 bool selectFPToInt(const Instruction *I, bool IsSigned);
163 bool selectRet(const Instruction *I);
164 bool selectTrunc(const Instruction *I);
165 bool selectIntExt(const Instruction *I);
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +0000166 bool selectShift(const Instruction *I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +0000167 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
Reed Kotlera562b462014-10-13 21:46:41 +0000168
169 // Utility helper routines.
Reed Kotlera562b462014-10-13 21:46:41 +0000170 bool isTypeLegal(Type *Ty, MVT &VT);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000171 bool isTypeSupported(Type *Ty, MVT &VT);
Reed Kotlera562b462014-10-13 21:46:41 +0000172 bool isLoadTypeLegal(Type *Ty, MVT &VT);
173 bool computeAddress(const Value *Obj, Address &Addr);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000174 bool computeCallAddress(const Value *V, Address &Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000175 void simplifyAddress(Address &Addr);
Reed Kotlera562b462014-10-13 21:46:41 +0000176
177 // Emit helper routines.
178 bool emitCmp(unsigned DestReg, const CmpInst *CI);
179 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
180 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000181 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
182 MachineMemOperand *MMO = nullptr);
Reed Kotlera562b462014-10-13 21:46:41 +0000183 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
184 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000185 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Reed Kotlera562b462014-10-13 21:46:41 +0000186 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
187
188 bool IsZExt);
189 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
190
191 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
192 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
193 unsigned DestReg);
194 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
195 unsigned DestReg);
196
197 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
198
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000199 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
200 const Value *RHS);
201
Reed Kotlera562b462014-10-13 21:46:41 +0000202 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
203 unsigned materializeGV(const GlobalValue *GV, MVT VT);
204 unsigned materializeInt(const Constant *C, MVT VT);
205 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000206 unsigned materializeExternalCallSym(MCSymbol *Syn);
Reed Kotlera562b462014-10-13 21:46:41 +0000207
208 MachineInstrBuilder emitInst(unsigned Opc) {
209 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
210 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000211
Reed Kotlera562b462014-10-13 21:46:41 +0000212 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
213 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
214 DstReg);
215 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000216
Reed Kotlera562b462014-10-13 21:46:41 +0000217 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
218 unsigned MemReg, int64_t MemOffset) {
219 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
220 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000221
Reed Kotlera562b462014-10-13 21:46:41 +0000222 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
223 unsigned MemReg, int64_t MemOffset) {
224 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
225 }
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +0000226
227 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
228 const TargetRegisterClass *RC,
229 unsigned Op0, bool Op0IsKill,
230 unsigned Op1, bool Op1IsKill);
231
Reed Kotlera562b462014-10-13 21:46:41 +0000232 // for some reason, this default is not generated by tablegen
233 // so we explicitly generate it here.
Reed Kotlera562b462014-10-13 21:46:41 +0000234 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
235 unsigned Op0, bool Op0IsKill, uint64_t imm1,
236 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
237 return 0;
238 }
Reed Kotler67077b32014-04-29 17:57:50 +0000239
Reed Kotlerd5c41962014-11-13 23:37:45 +0000240 // Call handling routines.
241private:
242 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
243 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
244 unsigned &NumBytes);
245 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000246
Daniel Sanderscbaca422016-07-29 12:27:28 +0000247 const MipsABIInfo &getABI() const {
248 return static_cast<const MipsTargetMachine &>(TM).getABI();
249 }
Reed Kotlerd5c41962014-11-13 23:37:45 +0000250
Reed Kotler720c5ca2014-04-17 22:15:34 +0000251public:
Reed Kotlera562b462014-10-13 21:46:41 +0000252 // Backend specific FastISel code.
Reed Kotler720c5ca2014-04-17 22:15:34 +0000253 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
254 const TargetLibraryInfo *libInfo)
Eric Christopher3ab98892014-12-20 00:07:09 +0000255 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
Eric Christopherb2a5fa92015-02-14 00:09:46 +0000256 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
Eric Christopher96e72c62015-01-29 23:27:36 +0000257 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
Reed Kotler67077b32014-04-29 17:57:50 +0000258 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
259 Context = &funcInfo.Fn->getContext();
Simon Dardis86b3a1e2016-10-04 10:35:07 +0000260 UnsupportedFPMode = Subtarget->isFP64bit() || Subtarget->useSoftFloat();
Reed Kotler67077b32014-04-29 17:57:50 +0000261 }
262
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000263 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000264 unsigned fastMaterializeConstant(const Constant *C) override;
Reed Kotlera562b462014-10-13 21:46:41 +0000265 bool fastSelectInstruction(const Instruction *I) override;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000266
Reed Kotler9fe25f32014-06-08 02:08:43 +0000267#include "MipsGenFastISel.inc"
Reed Kotler720c5ca2014-04-17 22:15:34 +0000268};
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000269
270} // end anonymous namespace
Reed Kotler67077b32014-04-29 17:57:50 +0000271
Reed Kotlerd5c41962014-11-13 23:37:45 +0000272static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
273 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Reid Klecknerd3781742014-11-14 00:39:33 +0000274 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000275
276static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
277 CCValAssign::LocInfo LocInfo,
278 ISD::ArgFlagsTy ArgFlags, CCState &State) {
279 llvm_unreachable("should not be called");
280}
281
Benjamin Kramer970eac42015-02-06 17:51:54 +0000282static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
283 CCValAssign::LocInfo LocInfo,
284 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Reed Kotlerd5c41962014-11-13 23:37:45 +0000285 llvm_unreachable("should not be called");
286}
287
288#include "MipsGenCallingConv.inc"
289
290CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
291 return CC_MipsO32;
292}
293
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000294unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
295 const Value *LHS, const Value *RHS) {
296 // Canonicalize immediates to the RHS first.
297 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
298 std::swap(LHS, RHS);
299
300 unsigned Opc;
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000301 switch (ISDOpc) {
Vasileios Kalintiris2a95f822015-10-12 15:39:41 +0000302 case ISD::AND:
303 Opc = Mips::AND;
304 break;
305 case ISD::OR:
306 Opc = Mips::OR;
307 break;
308 case ISD::XOR:
309 Opc = Mips::XOR;
310 break;
311 default:
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000312 llvm_unreachable("unexpected opcode");
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000313 }
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000314
315 unsigned LHSReg = getRegForValue(LHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000316 if (!LHSReg)
317 return 0;
318
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000319 unsigned RHSReg;
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000320 if (const auto *C = dyn_cast<ConstantInt>(RHS))
321 RHSReg = materializeInt(C, MVT::i32);
322 else
323 RHSReg = getRegForValue(RHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000324 if (!RHSReg)
325 return 0;
326
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000327 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
328 if (!ResultReg)
329 return 0;
330
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000331 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
332 return ResultReg;
333}
334
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000335unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000336 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 &&
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000337 "Alloca should always return a pointer.");
338
339 DenseMap<const AllocaInst *, int>::iterator SI =
340 FuncInfo.StaticAllocaMap.find(AI);
341
342 if (SI != FuncInfo.StaticAllocaMap.end()) {
343 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
344 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
345 ResultReg)
346 .addFrameIndex(SI->second)
347 .addImm(0);
348 return ResultReg;
349 }
350
351 return 0;
352}
353
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000354unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
355 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Reed Kotler497311a2014-10-10 17:39:51 +0000356 return 0;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000357 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
358 const ConstantInt *CI = cast<ConstantInt>(C);
Vasileios Kalintiris77fb0a32015-07-30 11:51:44 +0000359 return materialize32BitInt(CI->getZExtValue(), RC);
Reed Kotler497311a2014-10-10 17:39:51 +0000360}
361
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000362unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
363 const TargetRegisterClass *RC) {
364 unsigned ResultReg = createResultReg(RC);
365
366 if (isInt<16>(Imm)) {
367 unsigned Opc = Mips::ADDiu;
368 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
369 return ResultReg;
370 } else if (isUInt<16>(Imm)) {
371 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
372 return ResultReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000373 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000374 unsigned Lo = Imm & 0xFFFF;
375 unsigned Hi = (Imm >> 16) & 0xFFFF;
376 if (Lo) {
377 // Both Lo and Hi have nonzero bits.
378 unsigned TmpReg = createResultReg(RC);
379 emitInst(Mips::LUi, TmpReg).addImm(Hi);
380 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
381 } else {
382 emitInst(Mips::LUi, ResultReg).addImm(Hi);
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000383 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000384 return ResultReg;
385}
386
387unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
388 if (UnsupportedFPMode)
389 return 0;
390 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
391 if (VT == MVT::f32) {
392 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
393 unsigned DestReg = createResultReg(RC);
394 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
395 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
396 return DestReg;
397 } else if (VT == MVT::f64) {
398 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
399 unsigned DestReg = createResultReg(RC);
400 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
401 unsigned TempReg2 =
402 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
403 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
404 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000405 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000406 return 0;
407}
408
409unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
410 // For now 32-bit only.
411 if (VT != MVT::i32)
412 return 0;
413 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
414 unsigned DestReg = createResultReg(RC);
415 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
416 bool IsThreadLocal = GVar && GVar->isThreadLocal();
417 // TLS not supported at this time.
418 if (IsThreadLocal)
419 return 0;
420 emitInst(Mips::LW, DestReg)
421 .addReg(MFI->getGlobalBaseReg())
422 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
423 if ((GV->hasInternalLinkage() ||
424 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
425 unsigned TempReg = createResultReg(RC);
426 emitInst(Mips::ADDiu, TempReg)
427 .addReg(DestReg)
428 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
429 DestReg = TempReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000430 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000431 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000432}
433
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000434unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000435 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
436 unsigned DestReg = createResultReg(RC);
437 emitInst(Mips::LW, DestReg)
438 .addReg(MFI->getGlobalBaseReg())
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000439 .addSym(Sym, MipsII::MO_GOT);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000440 return DestReg;
441}
442
Reed Kotlerbab3f232014-05-01 20:39:21 +0000443// Materialize a constant into a register, and return the register
444// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000445unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000446 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000447
448 // Only handle simple types.
449 if (!CEVT.isSimple())
450 return 0;
451 MVT VT = CEVT.getSimpleVT();
452
453 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000454 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000455 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000456 return materializeGV(GV, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000457 else if (isa<ConstantInt>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000458 return materializeInt(C, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000459
460 return 0;
461}
462
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000463bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000464 const User *U = nullptr;
465 unsigned Opcode = Instruction::UserOp1;
466 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
467 // Don't walk into other basic blocks unless the object is an alloca from
468 // another block, otherwise it may not have a virtual register assigned.
469 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
470 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
471 Opcode = I->getOpcode();
472 U = I;
473 }
Vasileios Kalintiris32cd69a2015-05-12 12:08:31 +0000474 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
475 Opcode = C->getOpcode();
476 U = C;
477 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000478 switch (Opcode) {
479 default:
480 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000481 case Instruction::BitCast:
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000482 // Look through bitcasts.
483 return computeAddress(U->getOperand(0), Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000484 case Instruction::GetElementPtr: {
485 Address SavedAddr = Addr;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000486 int64_t TmpOffset = Addr.getOffset();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000487 // Iterate through the GEP folding the constants into offsets where
488 // we can.
489 gep_type_iterator GTI = gep_type_begin(U);
490 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
491 ++i, ++GTI) {
492 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000493 if (StructType *STy = GTI.getStructTypeOrNull()) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000494 const StructLayout *SL = DL.getStructLayout(STy);
495 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
496 TmpOffset += SL->getElementOffset(Idx);
497 } else {
498 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000499 while (true) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000500 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
501 // Constant-offset addressing.
502 TmpOffset += CI->getSExtValue() * S;
503 break;
504 }
505 if (canFoldAddIntoGEP(U, Op)) {
506 // A compatible add with a constant operand. Fold the constant.
507 ConstantInt *CI =
508 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
509 TmpOffset += CI->getSExtValue() * S;
510 // Iterate on the other operand.
511 Op = cast<AddOperator>(Op)->getOperand(0);
512 continue;
513 }
514 // Unsupported
515 goto unsupported_gep;
516 }
517 }
518 }
519 // Try to grab the base operand now.
520 Addr.setOffset(TmpOffset);
521 if (computeAddress(U->getOperand(0), Addr))
522 return true;
523 // We failed, restore everything and try the other options.
524 Addr = SavedAddr;
525 unsupported_gep:
526 break;
527 }
528 case Instruction::Alloca: {
529 const AllocaInst *AI = cast<AllocaInst>(Obj);
530 DenseMap<const AllocaInst *, int>::iterator SI =
531 FuncInfo.StaticAllocaMap.find(AI);
532 if (SI != FuncInfo.StaticAllocaMap.end()) {
533 Addr.setKind(Address::FrameIndexBase);
534 Addr.setFI(SI->second);
535 return true;
536 }
537 break;
538 }
539 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000540 Addr.setReg(getRegForValue(Obj));
541 return Addr.getReg() != 0;
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000542}
543
Reed Kotlerd5c41962014-11-13 23:37:45 +0000544bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000545 const User *U = nullptr;
546 unsigned Opcode = Instruction::UserOp1;
547
548 if (const auto *I = dyn_cast<Instruction>(V)) {
549 // Check if the value is defined in the same basic block. This information
550 // is crucial to know whether or not folding an operand is valid.
551 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
552 Opcode = I->getOpcode();
553 U = I;
554 }
555 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
556 Opcode = C->getOpcode();
557 U = C;
558 }
559
560 switch (Opcode) {
561 default:
562 break;
563 case Instruction::BitCast:
564 // Look past bitcasts if its operand is in the same BB.
565 return computeCallAddress(U->getOperand(0), Addr);
566 break;
567 case Instruction::IntToPtr:
568 // Look past no-op inttoptrs if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000569 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
570 TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000571 return computeCallAddress(U->getOperand(0), Addr);
572 break;
573 case Instruction::PtrToInt:
574 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000575 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000576 return computeCallAddress(U->getOperand(0), Addr);
577 break;
578 }
579
Reed Kotlerd5c41962014-11-13 23:37:45 +0000580 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
581 Addr.setGlobalValue(GV);
582 return true;
583 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000584
585 // If all else fails, try to materialize the value in a register.
586 if (!Addr.getGlobalValue()) {
587 Addr.setReg(getRegForValue(V));
588 return Addr.getReg() != 0;
589 }
590
Reed Kotlerd5c41962014-11-13 23:37:45 +0000591 return false;
592}
593
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000594bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000595 EVT evt = TLI.getValueType(DL, Ty, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000596 // Only handle simple types.
597 if (evt == MVT::Other || !evt.isSimple())
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000598 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000599 VT = evt.getSimpleVT();
600
601 // Handle all legal types, i.e. a register that will directly hold this
602 // value.
603 return TLI.isTypeLegal(VT);
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000604}
605
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000606bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
607 if (Ty->isVectorTy())
608 return false;
609
610 if (isTypeLegal(Ty, VT))
611 return true;
612
613 // If this is a type than can be sign or zero-extended to a basic operation
614 // go ahead and accept it now.
615 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
616 return true;
617
618 return false;
619}
620
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000621bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
622 if (isTypeLegal(Ty, VT))
Reed Kotler62de6b92014-10-11 00:55:18 +0000623 return true;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000624 // We will extend this in a later patch:
625 // If this is a type than can be sign or zero-extended to a basic operation
626 // go ahead and accept it now.
627 if (VT == MVT::i8 || VT == MVT::i16)
628 return true;
Reed Kotler62de6b92014-10-11 00:55:18 +0000629 return false;
630}
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000631
Reed Kotler62de6b92014-10-11 00:55:18 +0000632// Because of how EmitCmp is called with fast-isel, you can
Reed Kotler497311a2014-10-10 17:39:51 +0000633// end up with redundant "andi" instructions after the sequences emitted below.
634// We should try and solve this issue in the future.
635//
Reed Kotlera562b462014-10-13 21:46:41 +0000636bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000637 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000638 bool IsUnsigned = CI->isUnsigned();
Reed Kotler497311a2014-10-10 17:39:51 +0000639 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
640 if (LeftReg == 0)
641 return false;
642 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
643 if (RightReg == 0)
644 return false;
Reed Kotler1f64eca2014-10-10 20:46:28 +0000645 CmpInst::Predicate P = CI->getPredicate();
Reed Kotler62de6b92014-10-11 00:55:18 +0000646
Reed Kotler1f64eca2014-10-10 20:46:28 +0000647 switch (P) {
Reed Kotler497311a2014-10-10 17:39:51 +0000648 default:
649 return false;
650 case CmpInst::ICMP_EQ: {
651 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000652 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
653 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000654 break;
655 }
656 case CmpInst::ICMP_NE: {
657 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000658 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
659 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000660 break;
661 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000662 case CmpInst::ICMP_UGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000663 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000664 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000665 case CmpInst::ICMP_ULT:
Reed Kotlera562b462014-10-13 21:46:41 +0000666 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000667 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000668 case CmpInst::ICMP_UGE: {
669 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000670 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
671 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000672 break;
673 }
674 case CmpInst::ICMP_ULE: {
675 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000676 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
677 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000678 break;
679 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000680 case CmpInst::ICMP_SGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000681 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000682 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000683 case CmpInst::ICMP_SLT:
Reed Kotlera562b462014-10-13 21:46:41 +0000684 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000685 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000686 case CmpInst::ICMP_SGE: {
687 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000688 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
689 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000690 break;
691 }
692 case CmpInst::ICMP_SLE: {
693 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000694 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
695 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000696 break;
697 }
Reed Kotler1f64eca2014-10-10 20:46:28 +0000698 case CmpInst::FCMP_OEQ:
699 case CmpInst::FCMP_UNE:
700 case CmpInst::FCMP_OLT:
701 case CmpInst::FCMP_OLE:
702 case CmpInst::FCMP_OGT:
703 case CmpInst::FCMP_OGE: {
704 if (UnsupportedFPMode)
705 return false;
706 bool IsFloat = Left->getType()->isFloatTy();
707 bool IsDouble = Left->getType()->isDoubleTy();
708 if (!IsFloat && !IsDouble)
709 return false;
710 unsigned Opc, CondMovOpc;
711 switch (P) {
712 case CmpInst::FCMP_OEQ:
713 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
714 CondMovOpc = Mips::MOVT_I;
715 break;
716 case CmpInst::FCMP_UNE:
717 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
718 CondMovOpc = Mips::MOVF_I;
719 break;
720 case CmpInst::FCMP_OLT:
721 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
722 CondMovOpc = Mips::MOVT_I;
723 break;
724 case CmpInst::FCMP_OLE:
725 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
726 CondMovOpc = Mips::MOVT_I;
727 break;
728 case CmpInst::FCMP_OGT:
729 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
730 CondMovOpc = Mips::MOVF_I;
731 break;
732 case CmpInst::FCMP_OGE:
733 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
734 CondMovOpc = Mips::MOVF_I;
735 break;
736 default:
Chandler Carruth38811cc2014-10-10 21:07:03 +0000737 llvm_unreachable("Only switching of a subset of CCs.");
Reed Kotler1f64eca2014-10-10 20:46:28 +0000738 }
739 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
740 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000741 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
742 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
Simon Dardis730fdb72017-01-16 13:55:58 +0000743 emitInst(Opc).addReg(Mips::FCC0, RegState::Define).addReg(LeftReg)
744 .addReg(RightReg);
Daniel Sandersa6cda122016-05-06 12:57:26 +0000745 emitInst(CondMovOpc, ResultReg)
746 .addReg(RegWithOne)
747 .addReg(Mips::FCC0)
748 .addReg(RegWithZero);
Reed Kotler1f64eca2014-10-10 20:46:28 +0000749 break;
750 }
Reed Kotler497311a2014-10-10 17:39:51 +0000751 }
Reed Kotler62de6b92014-10-11 00:55:18 +0000752 return true;
753}
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000754
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000755bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
756 unsigned Alignment) {
757 //
758 // more cases will be handled here in following patches.
759 //
760 unsigned Opc;
761 switch (VT.SimpleTy) {
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000762 case MVT::i32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000763 ResultReg = createResultReg(&Mips::GPR32RegClass);
764 Opc = Mips::LW;
765 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000766 case MVT::i16:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000767 ResultReg = createResultReg(&Mips::GPR32RegClass);
768 Opc = Mips::LHu;
769 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000770 case MVT::i8:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000771 ResultReg = createResultReg(&Mips::GPR32RegClass);
772 Opc = Mips::LBu;
773 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000774 case MVT::f32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000775 if (UnsupportedFPMode)
776 return false;
777 ResultReg = createResultReg(&Mips::FGR32RegClass);
778 Opc = Mips::LWC1;
779 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000780 case MVT::f64:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000781 if (UnsupportedFPMode)
782 return false;
783 ResultReg = createResultReg(&Mips::AFGR64RegClass);
784 Opc = Mips::LDC1;
785 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000786 default:
787 return false;
788 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000789 if (Addr.isRegBase()) {
790 simplifyAddress(Addr);
791 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
792 return true;
793 }
794 if (Addr.isFIBase()) {
795 unsigned FI = Addr.getFI();
796 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000797 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000798 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000799 MachineMemOperand *MMO = MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +0000800 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000801 MFI.getObjectSize(FI), Align);
802 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
803 .addFrameIndex(FI)
804 .addImm(Offset)
805 .addMemOperand(MMO);
806 return true;
807 }
808 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000809}
810
811bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
812 unsigned Alignment) {
813 //
814 // more cases will be handled here in following patches.
815 //
816 unsigned Opc;
817 switch (VT.SimpleTy) {
818 case MVT::i8:
819 Opc = Mips::SB;
820 break;
821 case MVT::i16:
822 Opc = Mips::SH;
823 break;
824 case MVT::i32:
825 Opc = Mips::SW;
826 break;
827 case MVT::f32:
828 if (UnsupportedFPMode)
829 return false;
830 Opc = Mips::SWC1;
831 break;
832 case MVT::f64:
833 if (UnsupportedFPMode)
834 return false;
835 Opc = Mips::SDC1;
836 break;
837 default:
838 return false;
839 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000840 if (Addr.isRegBase()) {
841 simplifyAddress(Addr);
842 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
843 return true;
844 }
845 if (Addr.isFIBase()) {
846 unsigned FI = Addr.getFI();
847 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000848 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000849 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000850 MachineMemOperand *MMO = MF->getMachineMemOperand(
Simon Dardisd8bceb92016-04-29 16:07:47 +0000851 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000852 MFI.getObjectSize(FI), Align);
853 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
854 .addReg(SrcReg)
855 .addFrameIndex(FI)
856 .addImm(Offset)
857 .addMemOperand(MMO);
858 return true;
859 }
860 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000861}
862
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000863bool MipsFastISel::selectLogicalOp(const Instruction *I) {
864 MVT VT;
865 if (!isTypeSupported(I->getType(), VT))
866 return false;
867
868 unsigned ResultReg;
869 switch (I->getOpcode()) {
870 default:
871 llvm_unreachable("Unexpected instruction.");
872 case Instruction::And:
873 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
874 break;
875 case Instruction::Or:
876 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
877 break;
878 case Instruction::Xor:
879 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
880 break;
881 }
882
883 if (!ResultReg)
884 return false;
885
886 updateValueMap(I, ResultReg);
887 return true;
888}
889
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000890bool MipsFastISel::selectLoad(const Instruction *I) {
891 // Atomic loads need special handling.
892 if (cast<LoadInst>(I)->isAtomic())
893 return false;
894
895 // Verify we have a legal type before going any further.
896 MVT VT;
897 if (!isLoadTypeLegal(I->getType(), VT))
898 return false;
899
900 // See if we can handle this address.
901 Address Addr;
902 if (!computeAddress(I->getOperand(0), Addr))
903 return false;
904
905 unsigned ResultReg;
906 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
907 return false;
908 updateValueMap(I, ResultReg);
909 return true;
910}
911
912bool MipsFastISel::selectStore(const Instruction *I) {
913 Value *Op0 = I->getOperand(0);
914 unsigned SrcReg = 0;
915
916 // Atomic stores need special handling.
917 if (cast<StoreInst>(I)->isAtomic())
918 return false;
919
920 // Verify we have a legal type before going any further.
921 MVT VT;
922 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
923 return false;
924
925 // Get the value to be stored into a register.
926 SrcReg = getRegForValue(Op0);
927 if (SrcReg == 0)
928 return false;
929
930 // See if we can handle this address.
931 Address Addr;
932 if (!computeAddress(I->getOperand(1), Addr))
933 return false;
934
935 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
936 return false;
937 return true;
938}
939
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000940// This can cause a redundant sltiu to be generated.
941// FIXME: try and eliminate this in a future patch.
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000942bool MipsFastISel::selectBranch(const Instruction *I) {
943 const BranchInst *BI = cast<BranchInst>(I);
944 MachineBasicBlock *BrBB = FuncInfo.MBB;
945 //
946 // TBB is the basic block for the case where the comparison is true.
947 // FBB is the basic block for the case where the comparison is false.
948 // if (cond) goto TBB
949 // goto FBB
950 // TBB:
951 //
952 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
953 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000954 // For now, just try the simplest case where it's fed by a compare.
955 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Petar Jovanovic3af2c9922018-07-02 08:56:57 +0000956 unsigned CondReg = getRegForValue(CI);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000957 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
958 .addReg(CondReg)
959 .addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000960 finishCondBranch(BI->getParent(), TBB, FBB);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000961 return true;
962 }
963 return false;
964}
Reed Kotler62de6b92014-10-11 00:55:18 +0000965
Reed Kotlera562b462014-10-13 21:46:41 +0000966bool MipsFastISel::selectCmp(const Instruction *I) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000967 const CmpInst *CI = cast<CmpInst>(I);
968 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000969 if (!emitCmp(ResultReg, CI))
Reed Kotler62de6b92014-10-11 00:55:18 +0000970 return false;
Reed Kotler497311a2014-10-10 17:39:51 +0000971 updateValueMap(I, ResultReg);
972 return true;
973}
974
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000975// Attempt to fast-select a floating-point extend instruction.
976bool MipsFastISel::selectFPExt(const Instruction *I) {
977 if (UnsupportedFPMode)
978 return false;
979 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000980 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
981 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000982
983 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
984 return false;
985
986 unsigned SrcReg =
Nico Weber2cf5e892016-06-10 20:06:03 +0000987 getRegForValue(Src); // this must be a 32bit floating point register class
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000988 // maybe we should handle this differently
989 if (!SrcReg)
990 return false;
991
992 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
993 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
994 updateValueMap(I, DestReg);
995 return true;
996}
997
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000998bool MipsFastISel::selectSelect(const Instruction *I) {
999 assert(isa<SelectInst>(I) && "Expected a select instruction.");
1000
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001001 LLVM_DEBUG(dbgs() << "selectSelect\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001002
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001003 MVT VT;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001004 if (!isTypeSupported(I->getType(), VT) || UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001005 LLVM_DEBUG(
1006 dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001007 return false;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001008 }
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001009
1010 unsigned CondMovOpc;
1011 const TargetRegisterClass *RC;
1012
1013 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
1014 CondMovOpc = Mips::MOVN_I_I;
1015 RC = &Mips::GPR32RegClass;
1016 } else if (VT == MVT::f32) {
1017 CondMovOpc = Mips::MOVN_I_S;
1018 RC = &Mips::FGR32RegClass;
1019 } else if (VT == MVT::f64) {
1020 CondMovOpc = Mips::MOVN_I_D32;
1021 RC = &Mips::AFGR64RegClass;
1022 } else
1023 return false;
1024
1025 const SelectInst *SI = cast<SelectInst>(I);
1026 const Value *Cond = SI->getCondition();
1027 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
1028 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
1029 unsigned CondReg = getRegForValue(Cond);
1030
1031 if (!Src1Reg || !Src2Reg || !CondReg)
1032 return false;
1033
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001034 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
1035 if (!ZExtCondReg)
1036 return false;
1037
1038 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true))
1039 return false;
1040
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001041 unsigned ResultReg = createResultReg(RC);
1042 unsigned TempReg = createResultReg(RC);
1043
1044 if (!ResultReg || !TempReg)
1045 return false;
1046
1047 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
1048 emitInst(CondMovOpc, ResultReg)
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001049 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001050 updateValueMap(I, ResultReg);
1051 return true;
1052}
1053
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001054// Attempt to fast-select a floating-point truncate instruction.
1055bool MipsFastISel::selectFPTrunc(const Instruction *I) {
1056 if (UnsupportedFPMode)
1057 return false;
1058 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001059 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1060 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001061
1062 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1063 return false;
1064
1065 unsigned SrcReg = getRegForValue(Src);
1066 if (!SrcReg)
1067 return false;
1068
1069 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1070 if (!DestReg)
1071 return false;
1072
1073 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1074 updateValueMap(I, DestReg);
1075 return true;
1076}
1077
1078// Attempt to fast-select a floating-point-to-integer conversion.
1079bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1080 if (UnsupportedFPMode)
1081 return false;
1082 MVT DstVT, SrcVT;
1083 if (!IsSigned)
1084 return false; // We don't handle this case yet. There is no native
1085 // instruction for this but it can be synthesized.
1086 Type *DstTy = I->getType();
1087 if (!isTypeLegal(DstTy, DstVT))
1088 return false;
1089
1090 if (DstVT != MVT::i32)
1091 return false;
1092
1093 Value *Src = I->getOperand(0);
1094 Type *SrcTy = Src->getType();
1095 if (!isTypeLegal(SrcTy, SrcVT))
1096 return false;
1097
1098 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1099 return false;
1100
1101 unsigned SrcReg = getRegForValue(Src);
1102 if (SrcReg == 0)
1103 return false;
1104
1105 // Determine the opcode for the conversion, which takes place
1106 // entirely within FPRs.
1107 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1108 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001109 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001110
1111 // Generate the convert.
1112 emitInst(Opc, TempReg).addReg(SrcReg);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001113 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1114
1115 updateValueMap(I, DestReg);
1116 return true;
1117}
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001118
Reed Kotlerd5c41962014-11-13 23:37:45 +00001119bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1120 SmallVectorImpl<MVT> &OutVTs,
1121 unsigned &NumBytes) {
1122 CallingConv::ID CC = CLI.CallConv;
1123 SmallVector<CCValAssign, 16> ArgLocs;
1124 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1125 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1126 // Get a count of how many bytes are to be pushed on the stack.
1127 NumBytes = CCInfo.getNextStackOffset();
1128 // This is the minimum argument area used for A0-A3.
1129 if (NumBytes < 16)
1130 NumBytes = 16;
1131
Serge Pavlovd526b132017-05-09 13:35:13 +00001132 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001133 // Process the args.
1134 MVT firstMVT;
1135 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1136 CCValAssign &VA = ArgLocs[i];
1137 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1138 MVT ArgVT = OutVTs[VA.getValNo()];
1139
1140 if (i == 0) {
1141 firstMVT = ArgVT;
1142 if (ArgVT == MVT::f32) {
1143 VA.convertToReg(Mips::F12);
1144 } else if (ArgVT == MVT::f64) {
1145 VA.convertToReg(Mips::D6);
1146 }
1147 } else if (i == 1) {
1148 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1149 if (ArgVT == MVT::f32) {
1150 VA.convertToReg(Mips::F14);
1151 } else if (ArgVT == MVT::f64) {
1152 VA.convertToReg(Mips::D7);
1153 }
1154 }
1155 }
Vasileios Kalintirisb48c9052015-05-12 12:29:17 +00001156 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1157 (ArgVT == MVT::i8)) &&
1158 VA.isMemLoc()) {
Reed Kotlerd5c41962014-11-13 23:37:45 +00001159 switch (VA.getLocMemOffset()) {
1160 case 0:
1161 VA.convertToReg(Mips::A0);
1162 break;
1163 case 4:
1164 VA.convertToReg(Mips::A1);
1165 break;
1166 case 8:
1167 VA.convertToReg(Mips::A2);
1168 break;
1169 case 12:
1170 VA.convertToReg(Mips::A3);
1171 break;
1172 default:
1173 break;
1174 }
1175 }
1176 unsigned ArgReg = getRegForValue(ArgVal);
1177 if (!ArgReg)
1178 return false;
1179
1180 // Handle arg promotion: SExt, ZExt, AExt.
1181 switch (VA.getLocInfo()) {
1182 case CCValAssign::Full:
1183 break;
1184 case CCValAssign::AExt:
1185 case CCValAssign::SExt: {
1186 MVT DestVT = VA.getLocVT();
1187 MVT SrcVT = ArgVT;
1188 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1189 if (!ArgReg)
1190 return false;
1191 break;
1192 }
1193 case CCValAssign::ZExt: {
1194 MVT DestVT = VA.getLocVT();
1195 MVT SrcVT = ArgVT;
1196 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1197 if (!ArgReg)
1198 return false;
1199 break;
1200 }
1201 default:
1202 llvm_unreachable("Unknown arg promotion!");
1203 }
1204
1205 // Now copy/store arg to correct locations.
1206 if (VA.isRegLoc() && !VA.needsCustom()) {
1207 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1208 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1209 CLI.OutRegs.push_back(VA.getLocReg());
1210 } else if (VA.needsCustom()) {
1211 llvm_unreachable("Mips does not use custom args.");
1212 return false;
1213 } else {
1214 //
1215 // FIXME: This path will currently return false. It was copied
1216 // from the AArch64 port and should be essentially fine for Mips too.
1217 // The work to finish up this path will be done in a follow-on patch.
1218 //
1219 assert(VA.isMemLoc() && "Assuming store on stack.");
1220 // Don't emit stores for undef values.
1221 if (isa<UndefValue>(ArgVal))
1222 continue;
1223
1224 // Need to store on the stack.
1225 // FIXME: This alignment is incorrect but this path is disabled
1226 // for now (will return false). We need to determine the right alignment
1227 // based on the normal alignment for the underlying machine type.
1228 //
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001229 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001230
1231 unsigned BEAlign = 0;
1232 if (ArgSize < 8 && !Subtarget->isLittle())
1233 BEAlign = 8 - ArgSize;
1234
1235 Address Addr;
1236 Addr.setKind(Address::RegBase);
1237 Addr.setReg(Mips::SP);
1238 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1239
1240 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1241 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00001242 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
Reed Kotlerd5c41962014-11-13 23:37:45 +00001243 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1244 (void)(MMO);
1245 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1246 return false; // can't store on the stack yet.
1247 }
1248 }
1249
1250 return true;
1251}
1252
1253bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1254 unsigned NumBytes) {
1255 CallingConv::ID CC = CLI.CallConv;
Daniel Sanders01bcefd2016-05-03 14:19:26 +00001256 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001257 if (RetVT != MVT::isVoid) {
1258 SmallVector<CCValAssign, 16> RVLocs;
Simon Dardis70f79252017-04-26 11:10:38 +00001259 MipsCCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1260
1261 CCInfo.AnalyzeCallResult(CLI.Ins, RetCC_Mips, CLI.RetTy,
Simon Dardis9d580e82017-04-29 16:31:40 +00001262 CLI.Symbol ? CLI.Symbol->getName().data()
1263 : nullptr);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001264
1265 // Only handle a single return value.
1266 if (RVLocs.size() != 1)
1267 return false;
1268 // Copy all of the result registers out of their specified physreg.
1269 MVT CopyVT = RVLocs[0].getValVT();
1270 // Special handling for extended integers.
1271 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1272 CopyVT = MVT::i32;
1273
1274 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001275 if (!ResultReg)
1276 return false;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001277 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1278 TII.get(TargetOpcode::COPY),
1279 ResultReg).addReg(RVLocs[0].getLocReg());
1280 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1281
1282 CLI.ResultReg = ResultReg;
1283 CLI.NumResultRegs = 1;
1284 }
1285 return true;
1286}
1287
Daniel Sanderscbaca422016-07-29 12:27:28 +00001288bool MipsFastISel::fastLowerArguments() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001289 LLVM_DEBUG(dbgs() << "fastLowerArguments\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001290
1291 if (!FuncInfo.CanLowerReturn) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001292 LLVM_DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001293 return false;
1294 }
1295
1296 const Function *F = FuncInfo.Fn;
1297 if (F->isVarArg()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001298 LLVM_DEBUG(dbgs() << ".. gave up (varargs)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001299 return false;
1300 }
1301
1302 CallingConv::ID CC = F->getCallingConv();
1303 if (CC != CallingConv::C) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001304 LLVM_DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001305 return false;
1306 }
1307
Manoj Guptaafb355b2018-04-05 23:23:29 +00001308 std::array<MCPhysReg, 4> GPR32ArgRegs = {{Mips::A0, Mips::A1, Mips::A2,
1309 Mips::A3}};
1310 std::array<MCPhysReg, 2> FGR32ArgRegs = {{Mips::F12, Mips::F14}};
1311 std::array<MCPhysReg, 2> AFGR64ArgRegs = {{Mips::D6, Mips::D7}};
Manoj Gupta9d68b9e2018-04-05 22:47:25 +00001312 auto NextGPR32 = GPR32ArgRegs.begin();
1313 auto NextFGR32 = FGR32ArgRegs.begin();
1314 auto NextAFGR64 = AFGR64ArgRegs.begin();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001315
1316 struct AllocatedReg {
1317 const TargetRegisterClass *RC;
1318 unsigned Reg;
1319 AllocatedReg(const TargetRegisterClass *RC, unsigned Reg)
1320 : RC(RC), Reg(Reg) {}
1321 };
1322
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001323 // Only handle simple cases. i.e. All arguments are directly mapped to
1324 // registers of the appropriate type.
Daniel Sanderscbaca422016-07-29 12:27:28 +00001325 SmallVector<AllocatedReg, 4> Allocation;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001326 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001327 if (FormalArg.hasAttribute(Attribute::InReg) ||
1328 FormalArg.hasAttribute(Attribute::StructRet) ||
1329 FormalArg.hasAttribute(Attribute::ByVal)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001330 LLVM_DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001331 return false;
1332 }
1333
1334 Type *ArgTy = FormalArg.getType();
1335 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001336 LLVM_DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001337 return false;
1338 }
1339
1340 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001341 LLVM_DEBUG(dbgs() << ".. " << FormalArg.getArgNo() << ": "
1342 << ArgVT.getEVTString() << "\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001343 if (!ArgVT.isSimple()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001344 LLVM_DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001345 return false;
1346 }
1347
1348 switch (ArgVT.getSimpleVT().SimpleTy) {
1349 case MVT::i1:
1350 case MVT::i8:
1351 case MVT::i16:
Reid Kleckner6652a522017-04-28 18:37:16 +00001352 if (!FormalArg.hasAttribute(Attribute::SExt) &&
1353 !FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001354 // It must be any extend, this shouldn't happen for clang-generated IR
1355 // so just fall back on SelectionDAG.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001356 LLVM_DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001357 return false;
1358 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001359
1360 if (NextGPR32 == GPR32ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001361 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001362 return false;
1363 }
1364
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001365 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001366 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1367
1368 // Allocating any GPR32 prohibits further use of floating point arguments.
1369 NextFGR32 = FGR32ArgRegs.end();
1370 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001371 break;
1372
1373 case MVT::i32:
Reid Kleckner6652a522017-04-28 18:37:16 +00001374 if (FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001375 // The O32 ABI does not permit a zero-extended i32.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001376 LLVM_DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001377 return false;
1378 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001379
1380 if (NextGPR32 == GPR32ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001381 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001382 return false;
1383 }
1384
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001385 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001386 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1387
1388 // Allocating any GPR32 prohibits further use of floating point arguments.
1389 NextFGR32 = FGR32ArgRegs.end();
1390 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001391 break;
1392
1393 case MVT::f32:
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001394 if (UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001395 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001396 return false;
1397 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001398 if (NextFGR32 == FGR32ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001399 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001400 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001401 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001402 LLVM_DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001403 Allocation.emplace_back(&Mips::FGR32RegClass, *NextFGR32++);
1404 // Allocating an FGR32 also allocates the super-register AFGR64, and
1405 // ABI rules require us to skip the corresponding GPR32.
1406 if (NextGPR32 != GPR32ArgRegs.end())
1407 NextGPR32++;
1408 if (NextAFGR64 != AFGR64ArgRegs.end())
1409 NextAFGR64++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001410 break;
1411
1412 case MVT::f64:
Simon Dardisb432a3e2016-09-06 12:36:24 +00001413 if (UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001414 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001415 return false;
1416 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001417 if (NextAFGR64 == AFGR64ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001418 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001419 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001420 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001421 LLVM_DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001422 Allocation.emplace_back(&Mips::AFGR64RegClass, *NextAFGR64++);
1423 // Allocating an FGR32 also allocates the super-register AFGR64, and
1424 // ABI rules require us to skip the corresponding GPR32 pair.
1425 if (NextGPR32 != GPR32ArgRegs.end())
1426 NextGPR32++;
1427 if (NextGPR32 != GPR32ArgRegs.end())
1428 NextGPR32++;
1429 if (NextFGR32 != FGR32ArgRegs.end())
1430 NextFGR32++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001431 break;
1432
1433 default:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001434 LLVM_DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001435 return false;
1436 }
Daniel Sanderscbaca422016-07-29 12:27:28 +00001437 }
1438
Daniel Sanderscbaca422016-07-29 12:27:28 +00001439 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001440 unsigned ArgNo = FormalArg.getArgNo();
1441 unsigned SrcReg = Allocation[ArgNo].Reg;
1442 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001443 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1444 // Without this, EmitLiveInCopies may eliminate the livein if its only
1445 // use is a bitcast (which isn't turned into an instruction).
Reid Kleckner6652a522017-04-28 18:37:16 +00001446 unsigned ResultReg = createResultReg(Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1448 TII.get(TargetOpcode::COPY), ResultReg)
1449 .addReg(DstReg, getKillRegState(true));
1450 updateValueMap(&FormalArg, ResultReg);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001451 }
1452
1453 // Calculate the size of the incoming arguments area.
1454 // We currently reject all the cases where this would be non-zero.
1455 unsigned IncomingArgSizeInBytes = 0;
1456
1457 // Account for the reserved argument area on ABI's that have one (O32).
1458 // It seems strange to do this on the caller side but it's necessary in
1459 // SelectionDAG's implementation.
1460 IncomingArgSizeInBytes = std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC),
1461 IncomingArgSizeInBytes);
1462
1463 MF->getInfo<MipsFunctionInfo>()->setFormalArgInfo(IncomingArgSizeInBytes,
1464 false);
1465
1466 return true;
1467}
1468
Reed Kotlerd5c41962014-11-13 23:37:45 +00001469bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1470 CallingConv::ID CC = CLI.CallConv;
1471 bool IsTailCall = CLI.IsTailCall;
1472 bool IsVarArg = CLI.IsVarArg;
1473 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001474 MCSymbol *Symbol = CLI.Symbol;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001475
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001476 // Do not handle FastCC.
1477 if (CC == CallingConv::Fast)
1478 return false;
1479
Reed Kotlerd5c41962014-11-13 23:37:45 +00001480 // Allow SelectionDAG isel to handle tail calls.
1481 if (IsTailCall)
1482 return false;
1483
1484 // Let SDISel handle vararg functions.
1485 if (IsVarArg)
1486 return false;
1487
1488 // FIXME: Only handle *simple* calls for now.
1489 MVT RetVT;
1490 if (CLI.RetTy->isVoidTy())
1491 RetVT = MVT::isVoid;
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001492 else if (!isTypeSupported(CLI.RetTy, RetVT))
Reed Kotlerd5c41962014-11-13 23:37:45 +00001493 return false;
1494
1495 for (auto Flag : CLI.OutFlags)
1496 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1497 return false;
1498
1499 // Set up the argument vectors.
1500 SmallVector<MVT, 16> OutVTs;
1501 OutVTs.reserve(CLI.OutVals.size());
1502
1503 for (auto *Val : CLI.OutVals) {
1504 MVT VT;
1505 if (!isTypeLegal(Val->getType(), VT) &&
1506 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1507 return false;
1508
1509 // We don't handle vector parameters yet.
1510 if (VT.isVector() || VT.getSizeInBits() > 64)
1511 return false;
1512
1513 OutVTs.push_back(VT);
1514 }
1515
1516 Address Addr;
1517 if (!computeCallAddress(Callee, Addr))
1518 return false;
1519
1520 // Handle the arguments now that we've gotten them.
1521 unsigned NumBytes;
1522 if (!processCallArgs(CLI, OutVTs, NumBytes))
1523 return false;
1524
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001525 if (!Addr.getGlobalValue())
1526 return false;
1527
Reed Kotlerd5c41962014-11-13 23:37:45 +00001528 // Issue the call.
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001529 unsigned DestAddress;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001530 if (Symbol)
1531 DestAddress = materializeExternalCallSym(Symbol);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001532 else
1533 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001534 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1535 MachineInstrBuilder MIB =
1536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1537 Mips::RA).addReg(Mips::T9);
1538
1539 // Add implicit physical register uses to the call.
1540 for (auto Reg : CLI.OutRegs)
1541 MIB.addReg(Reg, RegState::Implicit);
1542
1543 // Add a register mask with the call-preserved registers.
1544 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001545 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Reed Kotlerd5c41962014-11-13 23:37:45 +00001546
1547 CLI.Call = MIB;
1548
Reed Kotlerd5c41962014-11-13 23:37:45 +00001549 // Finish off the call including any return values.
1550 return finishCall(CLI, RetVT, NumBytes);
1551}
1552
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001553bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1554 switch (II->getIntrinsicID()) {
1555 default:
1556 return false;
Vasileios Kalintiriscbbf8e02015-06-01 16:40:45 +00001557 case Intrinsic::bswap: {
1558 Type *RetTy = II->getCalledFunction()->getReturnType();
1559
1560 MVT VT;
1561 if (!isTypeSupported(RetTy, VT))
1562 return false;
1563
1564 unsigned SrcReg = getRegForValue(II->getOperand(0));
1565 if (SrcReg == 0)
1566 return false;
1567 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1568 if (DestReg == 0)
1569 return false;
1570 if (VT == MVT::i16) {
1571 if (Subtarget->hasMips32r2()) {
1572 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1573 updateValueMap(II, DestReg);
1574 return true;
1575 } else {
1576 unsigned TempReg[3];
1577 for (int i = 0; i < 3; i++) {
1578 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1579 if (TempReg[i] == 0)
1580 return false;
1581 }
1582 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1583 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1584 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1585 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1586 updateValueMap(II, DestReg);
1587 return true;
1588 }
1589 } else if (VT == MVT::i32) {
1590 if (Subtarget->hasMips32r2()) {
1591 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1592 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1593 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1594 updateValueMap(II, DestReg);
1595 return true;
1596 } else {
1597 unsigned TempReg[8];
1598 for (int i = 0; i < 8; i++) {
1599 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1600 if (TempReg[i] == 0)
1601 return false;
1602 }
1603
1604 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1605 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1606 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1607 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1608
1609 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1610 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1611
1612 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1613 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1614 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1615 updateValueMap(II, DestReg);
1616 return true;
1617 }
1618 }
1619 return false;
1620 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001621 case Intrinsic::memcpy:
1622 case Intrinsic::memmove: {
1623 const auto *MTI = cast<MemTransferInst>(II);
1624 // Don't handle volatile.
1625 if (MTI->isVolatile())
1626 return false;
1627 if (!MTI->getLength()->getType()->isIntegerTy(32))
1628 return false;
1629 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
Daniel Neilson1e687242018-01-19 17:13:12 +00001630 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 1);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001631 }
1632 case Intrinsic::memset: {
1633 const MemSetInst *MSI = cast<MemSetInst>(II);
1634 // Don't handle volatile.
1635 if (MSI->isVolatile())
1636 return false;
1637 if (!MSI->getLength()->getType()->isIntegerTy(32))
1638 return false;
Daniel Neilson1e687242018-01-19 17:13:12 +00001639 return lowerCallTo(II, "memset", II->getNumArgOperands() - 1);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001640 }
1641 }
1642 return false;
1643}
1644
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001645bool MipsFastISel::selectRet(const Instruction *I) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001646 const Function &F = *I->getParent()->getParent();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001647 const ReturnInst *Ret = cast<ReturnInst>(I);
1648
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001649 LLVM_DEBUG(dbgs() << "selectRet\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001650
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001651 if (!FuncInfo.CanLowerReturn)
1652 return false;
Reed Kotleraa150ed2015-02-12 21:05:12 +00001653
1654 // Build a list of return value registers.
1655 SmallVector<unsigned, 4> RetRegs;
1656
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001657 if (Ret->getNumOperands() > 0) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001658 CallingConv::ID CC = F.getCallingConv();
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001659
1660 // Do not handle FastCC.
1661 if (CC == CallingConv::Fast)
1662 return false;
1663
Reed Kotleraa150ed2015-02-12 21:05:12 +00001664 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001665 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1666
Reed Kotleraa150ed2015-02-12 21:05:12 +00001667 // Analyze operands of the call, assigning locations to each operand.
1668 SmallVector<CCValAssign, 16> ValLocs;
1669 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1670 I->getContext());
1671 CCAssignFn *RetCC = RetCC_Mips;
1672 CCInfo.AnalyzeReturn(Outs, RetCC);
1673
1674 // Only handle a single return value for now.
1675 if (ValLocs.size() != 1)
1676 return false;
1677
1678 CCValAssign &VA = ValLocs[0];
1679 const Value *RV = Ret->getOperand(0);
1680
1681 // Don't bother handling odd stuff for now.
1682 if ((VA.getLocInfo() != CCValAssign::Full) &&
1683 (VA.getLocInfo() != CCValAssign::BCvt))
1684 return false;
1685
1686 // Only handle register returns for now.
1687 if (!VA.isRegLoc())
1688 return false;
1689
1690 unsigned Reg = getRegForValue(RV);
1691 if (Reg == 0)
1692 return false;
1693
1694 unsigned SrcReg = Reg + VA.getValNo();
1695 unsigned DestReg = VA.getLocReg();
1696 // Avoid a cross-class copy. This is very unlikely.
1697 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1698 return false;
1699
Mehdi Amini44ede332015-07-09 02:09:04 +00001700 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Reed Kotleraa150ed2015-02-12 21:05:12 +00001701 if (!RVEVT.isSimple())
1702 return false;
1703
1704 if (RVEVT.isVector())
1705 return false;
1706
1707 MVT RVVT = RVEVT.getSimpleVT();
1708 if (RVVT == MVT::f128)
1709 return false;
1710
Simon Dardisb432a3e2016-09-06 12:36:24 +00001711 // Do not handle FGR64 returns for now.
1712 if (RVVT == MVT::f64 && UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001713 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001714 return false;
1715 }
1716
Reed Kotleraa150ed2015-02-12 21:05:12 +00001717 MVT DestVT = VA.getValVT();
1718 // Special handling for extended integers.
1719 if (RVVT != DestVT) {
1720 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1721 return false;
1722
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001723 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1724 bool IsZExt = Outs[0].Flags.isZExt();
1725 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1726 if (SrcReg == 0)
1727 return false;
1728 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001729 }
1730
1731 // Make the copy.
1732 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1733 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1734
1735 // Add register to return instruction.
1736 RetRegs.push_back(VA.getLocReg());
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001737 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001738 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1739 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1740 MIB.addReg(RetRegs[i], RegState::Implicit);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001741 return true;
1742}
1743
1744bool MipsFastISel::selectTrunc(const Instruction *I) {
1745 // The high bits for a type smaller than the register size are assumed to be
1746 // undefined.
1747 Value *Op = I->getOperand(0);
1748
1749 EVT SrcVT, DestVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001750 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1751 DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001752
1753 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1754 return false;
1755 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1756 return false;
1757
1758 unsigned SrcReg = getRegForValue(Op);
1759 if (!SrcReg)
1760 return false;
1761
1762 // Because the high bits are undefined, a truncate doesn't generate
1763 // any code.
1764 updateValueMap(I, SrcReg);
1765 return true;
1766}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001767
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001768bool MipsFastISel::selectIntExt(const Instruction *I) {
1769 Type *DestTy = I->getType();
1770 Value *Src = I->getOperand(0);
1771 Type *SrcTy = Src->getType();
1772
1773 bool isZExt = isa<ZExtInst>(I);
1774 unsigned SrcReg = getRegForValue(Src);
1775 if (!SrcReg)
1776 return false;
1777
1778 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001779 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1780 DestEVT = TLI.getValueType(DL, DestTy, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001781 if (!SrcEVT.isSimple())
1782 return false;
1783 if (!DestEVT.isSimple())
1784 return false;
1785
1786 MVT SrcVT = SrcEVT.getSimpleVT();
1787 MVT DestVT = DestEVT.getSimpleVT();
1788 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1789
1790 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1791 return false;
1792 updateValueMap(I, ResultReg);
1793 return true;
1794}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001795
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001796bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1797 unsigned DestReg) {
1798 unsigned ShiftAmt;
1799 switch (SrcVT.SimpleTy) {
1800 default:
1801 return false;
1802 case MVT::i8:
1803 ShiftAmt = 24;
1804 break;
1805 case MVT::i16:
1806 ShiftAmt = 16;
1807 break;
1808 }
1809 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1810 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1811 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1812 return true;
1813}
1814
1815bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1816 unsigned DestReg) {
1817 switch (SrcVT.SimpleTy) {
1818 default:
1819 return false;
1820 case MVT::i8:
1821 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1822 break;
1823 case MVT::i16:
1824 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1825 break;
1826 }
1827 return true;
1828}
1829
1830bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1831 unsigned DestReg) {
1832 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1833 return false;
1834 if (Subtarget->hasMips32r2())
1835 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1836 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1837}
1838
1839bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1840 unsigned DestReg) {
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001841 int64_t Imm;
1842
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001843 switch (SrcVT.SimpleTy) {
1844 default:
1845 return false;
1846 case MVT::i1:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001847 Imm = 1;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001848 break;
1849 case MVT::i8:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001850 Imm = 0xff;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001851 break;
1852 case MVT::i16:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001853 Imm = 0xffff;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001854 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001855 }
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001856
1857 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001858 return true;
1859}
1860
1861bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1862 unsigned DestReg, bool IsZExt) {
Vasileios Kalintiris1202f362015-04-24 13:48:19 +00001863 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1864 // DestVT are odd things, so test to make sure that they are both types we can
1865 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1866 // bail out to SelectionDAG.
1867 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1868 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1869 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001870 if (IsZExt)
1871 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1872 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1873}
Reed Kotlerd5c41962014-11-13 23:37:45 +00001874
1875unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1876 bool isZExt) {
1877 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotleraa150ed2015-02-12 21:05:12 +00001878 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1879 return Success ? DestReg : 0;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001880}
1881
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001882bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001883 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001884 if (!DestEVT.isSimple())
1885 return false;
1886
1887 MVT DestVT = DestEVT.getSimpleVT();
1888 if (DestVT != MVT::i32)
1889 return false;
1890
1891 unsigned DivOpc;
1892 switch (ISDOpcode) {
1893 default:
1894 return false;
1895 case ISD::SDIV:
1896 case ISD::SREM:
1897 DivOpc = Mips::SDIV;
1898 break;
1899 case ISD::UDIV:
1900 case ISD::UREM:
1901 DivOpc = Mips::UDIV;
1902 break;
1903 }
1904
1905 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1906 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1907 if (!Src0Reg || !Src1Reg)
1908 return false;
1909
1910 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1911 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1912
1913 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1914 if (!ResultReg)
1915 return false;
1916
1917 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1918 ? Mips::MFHI
1919 : Mips::MFLO;
1920 emitInst(MFOpc, ResultReg);
1921
1922 updateValueMap(I, ResultReg);
1923 return true;
1924}
1925
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001926bool MipsFastISel::selectShift(const Instruction *I) {
1927 MVT RetVT;
1928
1929 if (!isTypeSupported(I->getType(), RetVT))
1930 return false;
1931
1932 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1933 if (!ResultReg)
1934 return false;
1935
1936 unsigned Opcode = I->getOpcode();
1937 const Value *Op0 = I->getOperand(0);
1938 unsigned Op0Reg = getRegForValue(Op0);
1939 if (!Op0Reg)
1940 return false;
1941
1942 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1943 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1944 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1945 if (!TempReg)
1946 return false;
1947
Mehdi Amini44ede332015-07-09 02:09:04 +00001948 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001949 bool IsZExt = Opcode == Instruction::LShr;
1950 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1951 return false;
1952
1953 Op0Reg = TempReg;
1954 }
1955
1956 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1957 uint64_t ShiftVal = C->getZExtValue();
1958
1959 switch (Opcode) {
1960 default:
1961 llvm_unreachable("Unexpected instruction.");
1962 case Instruction::Shl:
1963 Opcode = Mips::SLL;
1964 break;
1965 case Instruction::AShr:
1966 Opcode = Mips::SRA;
1967 break;
1968 case Instruction::LShr:
1969 Opcode = Mips::SRL;
1970 break;
1971 }
1972
1973 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1974 updateValueMap(I, ResultReg);
1975 return true;
1976 }
1977
1978 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1979 if (!Op1Reg)
1980 return false;
1981
1982 switch (Opcode) {
1983 default:
1984 llvm_unreachable("Unexpected instruction.");
1985 case Instruction::Shl:
1986 Opcode = Mips::SLLV;
1987 break;
1988 case Instruction::AShr:
1989 Opcode = Mips::SRAV;
1990 break;
1991 case Instruction::LShr:
1992 Opcode = Mips::SRLV;
1993 break;
1994 }
1995
1996 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1997 updateValueMap(I, ResultReg);
1998 return true;
1999}
2000
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002001bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
Reed Kotler67077b32014-04-29 17:57:50 +00002002 switch (I->getOpcode()) {
2003 default:
2004 break;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +00002005 case Instruction::Load:
Reed Kotlera562b462014-10-13 21:46:41 +00002006 return selectLoad(I);
Reed Kotlerbab3f232014-05-01 20:39:21 +00002007 case Instruction::Store:
Reed Kotlera562b462014-10-13 21:46:41 +00002008 return selectStore(I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00002009 case Instruction::SDiv:
2010 if (!selectBinaryOp(I, ISD::SDIV))
2011 return selectDivRem(I, ISD::SDIV);
2012 return true;
2013 case Instruction::UDiv:
2014 if (!selectBinaryOp(I, ISD::UDIV))
2015 return selectDivRem(I, ISD::UDIV);
2016 return true;
2017 case Instruction::SRem:
2018 if (!selectBinaryOp(I, ISD::SREM))
2019 return selectDivRem(I, ISD::SREM);
2020 return true;
2021 case Instruction::URem:
2022 if (!selectBinaryOp(I, ISD::UREM))
2023 return selectDivRem(I, ISD::UREM);
2024 return true;
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00002025 case Instruction::Shl:
2026 case Instruction::LShr:
2027 case Instruction::AShr:
2028 return selectShift(I);
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002029 case Instruction::And:
2030 case Instruction::Or:
2031 case Instruction::Xor:
2032 return selectLogicalOp(I);
Reed Kotler62de6b92014-10-11 00:55:18 +00002033 case Instruction::Br:
Reed Kotlera562b462014-10-13 21:46:41 +00002034 return selectBranch(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002035 case Instruction::Ret:
Reed Kotlera562b462014-10-13 21:46:41 +00002036 return selectRet(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002037 case Instruction::Trunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002038 return selectTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002039 case Instruction::ZExt:
2040 case Instruction::SExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002041 return selectIntExt(I);
Reed Kotlerb9dc2482014-10-01 18:47:02 +00002042 case Instruction::FPTrunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002043 return selectFPTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002044 case Instruction::FPExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002045 return selectFPExt(I);
Reed Kotler12f94882014-10-10 17:00:46 +00002046 case Instruction::FPToSI:
Reed Kotlera562b462014-10-13 21:46:41 +00002047 return selectFPToInt(I, /*isSigned*/ true);
Reed Kotler12f94882014-10-10 17:00:46 +00002048 case Instruction::FPToUI:
Reed Kotlera562b462014-10-13 21:46:41 +00002049 return selectFPToInt(I, /*isSigned*/ false);
Reed Kotler497311a2014-10-10 17:39:51 +00002050 case Instruction::ICmp:
2051 case Instruction::FCmp:
Reed Kotlera562b462014-10-13 21:46:41 +00002052 return selectCmp(I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00002053 case Instruction::Select:
2054 return selectSelect(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002055 }
2056 return false;
2057}
Reed Kotler720c5ca2014-04-17 22:15:34 +00002058
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002059unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
2060 bool IsUnsigned) {
2061 unsigned VReg = getRegForValue(V);
2062 if (VReg == 0)
Reed Kotler12f94882014-10-10 17:00:46 +00002063 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00002064 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002065 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
2066 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
2067 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
2068 return 0;
2069 VReg = TempReg;
Reed Kotler063d4fb2014-06-10 16:45:44 +00002070 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002071 return VReg;
Reed Kotlerbab3f232014-05-01 20:39:21 +00002072}
2073
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002074void MipsFastISel::simplifyAddress(Address &Addr) {
2075 if (!isInt<16>(Addr.getOffset())) {
2076 unsigned TempReg =
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002077 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002078 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
2079 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
2080 Addr.setReg(DestReg);
2081 Addr.setOffset(0);
2082 }
2083}
2084
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +00002085unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2086 const TargetRegisterClass *RC,
2087 unsigned Op0, bool Op0IsKill,
2088 unsigned Op1, bool Op1IsKill) {
2089 // We treat the MUL instruction in a special way because it clobbers
2090 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2091 // mark these registers only as implicitly defined. As a result, the
2092 // register allocator runs out of registers when this instruction is
2093 // followed by another instruction that defines the same registers too.
2094 // We can fix this by explicitly marking those registers as dead.
2095 if (MachineInstOpcode == Mips::MUL) {
2096 unsigned ResultReg = createResultReg(RC);
2097 const MCInstrDesc &II = TII.get(MachineInstOpcode);
2098 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
2099 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
2100 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
2101 .addReg(Op0, getKillRegState(Op0IsKill))
2102 .addReg(Op1, getKillRegState(Op1IsKill))
2103 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
2104 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
2105 return ResultReg;
2106 }
2107
2108 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
2109 Op1IsKill);
2110}
2111
Reed Kotler720c5ca2014-04-17 22:15:34 +00002112namespace llvm {
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002113
Reed Kotler720c5ca2014-04-17 22:15:34 +00002114FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
2115 const TargetLibraryInfo *libInfo) {
2116 return new MipsFastISel(funcInfo, libInfo);
2117}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002118
2119} // end namespace llvm