blob: ef4e0c5178507b234ed2bfb76414c31e72dd9b00 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsFastISel.cpp - Mips FastISel implementation --------------------===//
Vasileios Kalintirisa9e51542016-06-08 13:13:15 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Vasileios Kalintirisa9e51542016-06-08 13:13:15 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file defines the MIPS-specific support for the FastISel class.
Vasileios Kalintirisa9e51542016-06-08 13:13:15 +000011/// Some of the target-specific code is generated by tablegen in the file
12/// MipsGenFastISel.inc, which is #included here.
13///
14//===----------------------------------------------------------------------===//
Reed Kotler720c5ca2014-04-17 22:15:34 +000015
Eugene Zelenkodde94e42017-01-30 23:21:32 +000016#include "MCTargetDesc/MipsABIInfo.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "MipsCCState.h"
19#include "MipsISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "MipsInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "MipsMachineFunction.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "MipsSubtarget.h"
23#include "MipsTargetMachine.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000024#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/ArrayRef.h"
26#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/SmallVector.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000028#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000029#include "llvm/CodeGen/CallingConvLower.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000030#include "llvm/CodeGen/FastISel.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000031#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000032#include "llvm/CodeGen/ISDOpcodes.h"
33#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
Reed Kotler67077b32014-04-29 17:57:50 +000035#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000038#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000039#include "llvm/CodeGen/TargetLowering.h"
Craig Topper2fa14362018-03-29 17:21:10 +000040#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000041#include "llvm/IR/Attributes.h"
42#include "llvm/IR/CallingConv.h"
43#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
45#include "llvm/IR/DataLayout.h"
46#include "llvm/IR/Function.h"
David Blaikie457343d2015-05-21 21:12:43 +000047#include "llvm/IR/GetElementPtrTypeIterator.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000048#include "llvm/IR/GlobalValue.h"
Reed Kotlerbab3f232014-05-01 20:39:21 +000049#include "llvm/IR/GlobalVariable.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000050#include "llvm/IR/InstrTypes.h"
51#include "llvm/IR/Instruction.h"
52#include "llvm/IR/Instructions.h"
53#include "llvm/IR/IntrinsicInst.h"
54#include "llvm/IR/Operator.h"
55#include "llvm/IR/Type.h"
56#include "llvm/IR/User.h"
57#include "llvm/IR/Value.h"
Vladimir Stefanovic3daf8bc2019-01-17 21:50:37 +000058#include "llvm/MC/MCContext.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000059#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
Vladimir Stefanovic3daf8bc2019-01-17 21:50:37 +000078extern cl::opt<bool> EmitJalrReloc;
79
Reed Kotler720c5ca2014-04-17 22:15:34 +000080namespace {
81
82class MipsFastISel final : public FastISel {
83
Reed Kotlera562b462014-10-13 21:46:41 +000084 // All possible address modes.
85 class Address {
86 public:
Eugene Zelenko79220eae2017-08-03 22:12:30 +000087 using BaseKind = enum { RegBase, FrameIndexBase };
Reed Kotlera562b462014-10-13 21:46:41 +000088
89 private:
Eugene Zelenkodde94e42017-01-30 23:21:32 +000090 BaseKind Kind = RegBase;
Reed Kotlera562b462014-10-13 21:46:41 +000091 union {
92 unsigned Reg;
93 int FI;
94 } Base;
95
Eugene Zelenkodde94e42017-01-30 23:21:32 +000096 int64_t Offset = 0;
Reed Kotlera562b462014-10-13 21:46:41 +000097
Eugene Zelenkodde94e42017-01-30 23:21:32 +000098 const GlobalValue *GV = nullptr;
Reed Kotlera562b462014-10-13 21:46:41 +000099
100 public:
101 // Innocuous defaults for our address.
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000102 Address() { Base.Reg = 0; }
103
Reed Kotlera562b462014-10-13 21:46:41 +0000104 void setKind(BaseKind K) { Kind = K; }
105 BaseKind getKind() const { return Kind; }
106 bool isRegBase() const { return Kind == RegBase; }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000107 bool isFIBase() const { return Kind == FrameIndexBase; }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000108
Reed Kotlera562b462014-10-13 21:46:41 +0000109 void setReg(unsigned Reg) {
110 assert(isRegBase() && "Invalid base register access!");
111 Base.Reg = Reg;
112 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000113
Reed Kotlera562b462014-10-13 21:46:41 +0000114 unsigned getReg() const {
115 assert(isRegBase() && "Invalid base register access!");
116 return Base.Reg;
117 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000118
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000119 void setFI(unsigned FI) {
120 assert(isFIBase() && "Invalid base frame index access!");
121 Base.FI = FI;
122 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000123
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000124 unsigned getFI() const {
125 assert(isFIBase() && "Invalid base frame index access!");
126 return Base.FI;
127 }
128
Reed Kotlera562b462014-10-13 21:46:41 +0000129 void setOffset(int64_t Offset_) { Offset = Offset_; }
130 int64_t getOffset() const { return Offset; }
131 void setGlobalValue(const GlobalValue *G) { GV = G; }
132 const GlobalValue *getGlobalValue() { return GV; }
133 };
134
Reed Kotler67077b32014-04-29 17:57:50 +0000135 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
136 /// make the right decision when generating code for different targets.
Reed Kotler67077b32014-04-29 17:57:50 +0000137 const TargetMachine &TM;
Eric Christopher96e72c62015-01-29 23:27:36 +0000138 const MipsSubtarget *Subtarget;
Reed Kotler67077b32014-04-29 17:57:50 +0000139 const TargetInstrInfo &TII;
140 const TargetLowering &TLI;
141 MipsFunctionInfo *MFI;
142
143 // Convenience variables to avoid some queries.
144 LLVMContext *Context;
145
Daniel Sanderscbaca422016-07-29 12:27:28 +0000146 bool fastLowerArguments() override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000147 bool fastLowerCall(CallLoweringInfo &CLI) override;
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000148 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000149
Reed Kotlera562b462014-10-13 21:46:41 +0000150 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
151 // floating point but not reject doing fast-isel in other
152 // situations
153
154private:
155 // Selection routines.
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000156 bool selectLogicalOp(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000157 bool selectLoad(const Instruction *I);
158 bool selectStore(const Instruction *I);
159 bool selectBranch(const Instruction *I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000160 bool selectSelect(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000161 bool selectCmp(const Instruction *I);
162 bool selectFPExt(const Instruction *I);
163 bool selectFPTrunc(const Instruction *I);
164 bool selectFPToInt(const Instruction *I, bool IsSigned);
165 bool selectRet(const Instruction *I);
166 bool selectTrunc(const Instruction *I);
167 bool selectIntExt(const Instruction *I);
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +0000168 bool selectShift(const Instruction *I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +0000169 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
Reed Kotlera562b462014-10-13 21:46:41 +0000170
171 // Utility helper routines.
Reed Kotlera562b462014-10-13 21:46:41 +0000172 bool isTypeLegal(Type *Ty, MVT &VT);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000173 bool isTypeSupported(Type *Ty, MVT &VT);
Reed Kotlera562b462014-10-13 21:46:41 +0000174 bool isLoadTypeLegal(Type *Ty, MVT &VT);
175 bool computeAddress(const Value *Obj, Address &Addr);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000176 bool computeCallAddress(const Value *V, Address &Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000177 void simplifyAddress(Address &Addr);
Reed Kotlera562b462014-10-13 21:46:41 +0000178
179 // Emit helper routines.
180 bool emitCmp(unsigned DestReg, const CmpInst *CI);
181 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
182 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000183 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
184 MachineMemOperand *MMO = nullptr);
Reed Kotlera562b462014-10-13 21:46:41 +0000185 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
186 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000187 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Reed Kotlera562b462014-10-13 21:46:41 +0000188 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
189
190 bool IsZExt);
191 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
192
193 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
194 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
195 unsigned DestReg);
196 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
197 unsigned DestReg);
198
199 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
200
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000201 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
202 const Value *RHS);
203
Reed Kotlera562b462014-10-13 21:46:41 +0000204 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
205 unsigned materializeGV(const GlobalValue *GV, MVT VT);
206 unsigned materializeInt(const Constant *C, MVT VT);
207 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000208 unsigned materializeExternalCallSym(MCSymbol *Syn);
Reed Kotlera562b462014-10-13 21:46:41 +0000209
210 MachineInstrBuilder emitInst(unsigned Opc) {
211 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
212 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000213
Reed Kotlera562b462014-10-13 21:46:41 +0000214 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
215 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
216 DstReg);
217 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000218
Reed Kotlera562b462014-10-13 21:46:41 +0000219 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
220 unsigned MemReg, int64_t MemOffset) {
221 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
222 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000223
Reed Kotlera562b462014-10-13 21:46:41 +0000224 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
225 unsigned MemReg, int64_t MemOffset) {
226 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
227 }
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +0000228
229 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
230 const TargetRegisterClass *RC,
231 unsigned Op0, bool Op0IsKill,
232 unsigned Op1, bool Op1IsKill);
233
Reed Kotlera562b462014-10-13 21:46:41 +0000234 // for some reason, this default is not generated by tablegen
235 // so we explicitly generate it here.
Reed Kotlera562b462014-10-13 21:46:41 +0000236 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
237 unsigned Op0, bool Op0IsKill, uint64_t imm1,
238 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
239 return 0;
240 }
Reed Kotler67077b32014-04-29 17:57:50 +0000241
Reed Kotlerd5c41962014-11-13 23:37:45 +0000242 // Call handling routines.
243private:
244 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
245 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
246 unsigned &NumBytes);
247 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000248
Daniel Sanderscbaca422016-07-29 12:27:28 +0000249 const MipsABIInfo &getABI() const {
250 return static_cast<const MipsTargetMachine &>(TM).getABI();
251 }
Reed Kotlerd5c41962014-11-13 23:37:45 +0000252
Reed Kotler720c5ca2014-04-17 22:15:34 +0000253public:
Reed Kotlera562b462014-10-13 21:46:41 +0000254 // Backend specific FastISel code.
Reed Kotler720c5ca2014-04-17 22:15:34 +0000255 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
256 const TargetLibraryInfo *libInfo)
Eric Christopher3ab98892014-12-20 00:07:09 +0000257 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
Eric Christopherb2a5fa92015-02-14 00:09:46 +0000258 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
Eric Christopher96e72c62015-01-29 23:27:36 +0000259 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
Reed Kotler67077b32014-04-29 17:57:50 +0000260 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
261 Context = &funcInfo.Fn->getContext();
Simon Dardis86b3a1e2016-10-04 10:35:07 +0000262 UnsupportedFPMode = Subtarget->isFP64bit() || Subtarget->useSoftFloat();
Reed Kotler67077b32014-04-29 17:57:50 +0000263 }
264
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000265 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000266 unsigned fastMaterializeConstant(const Constant *C) override;
Reed Kotlera562b462014-10-13 21:46:41 +0000267 bool fastSelectInstruction(const Instruction *I) override;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000268
Reed Kotler9fe25f32014-06-08 02:08:43 +0000269#include "MipsGenFastISel.inc"
Reed Kotler720c5ca2014-04-17 22:15:34 +0000270};
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000271
272} // end anonymous namespace
Reed Kotler67077b32014-04-29 17:57:50 +0000273
Reed Kotlerd5c41962014-11-13 23:37:45 +0000274static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
275 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Reid Klecknerd3781742014-11-14 00:39:33 +0000276 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000277
278static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
279 CCValAssign::LocInfo LocInfo,
280 ISD::ArgFlagsTy ArgFlags, CCState &State) {
281 llvm_unreachable("should not be called");
282}
283
Benjamin Kramer970eac42015-02-06 17:51:54 +0000284static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
285 CCValAssign::LocInfo LocInfo,
286 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Reed Kotlerd5c41962014-11-13 23:37:45 +0000287 llvm_unreachable("should not be called");
288}
289
290#include "MipsGenCallingConv.inc"
291
292CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
293 return CC_MipsO32;
294}
295
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000296unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
297 const Value *LHS, const Value *RHS) {
298 // Canonicalize immediates to the RHS first.
299 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
300 std::swap(LHS, RHS);
301
302 unsigned Opc;
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000303 switch (ISDOpc) {
Vasileios Kalintiris2a95f822015-10-12 15:39:41 +0000304 case ISD::AND:
305 Opc = Mips::AND;
306 break;
307 case ISD::OR:
308 Opc = Mips::OR;
309 break;
310 case ISD::XOR:
311 Opc = Mips::XOR;
312 break;
313 default:
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000314 llvm_unreachable("unexpected opcode");
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000315 }
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000316
317 unsigned LHSReg = getRegForValue(LHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000318 if (!LHSReg)
319 return 0;
320
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000321 unsigned RHSReg;
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000322 if (const auto *C = dyn_cast<ConstantInt>(RHS))
323 RHSReg = materializeInt(C, MVT::i32);
324 else
325 RHSReg = getRegForValue(RHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000326 if (!RHSReg)
327 return 0;
328
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000329 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
330 if (!ResultReg)
331 return 0;
332
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000333 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
334 return ResultReg;
335}
336
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000337unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000338 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 &&
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000339 "Alloca should always return a pointer.");
340
341 DenseMap<const AllocaInst *, int>::iterator SI =
342 FuncInfo.StaticAllocaMap.find(AI);
343
344 if (SI != FuncInfo.StaticAllocaMap.end()) {
345 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
346 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
347 ResultReg)
348 .addFrameIndex(SI->second)
349 .addImm(0);
350 return ResultReg;
351 }
352
353 return 0;
354}
355
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000356unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
357 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Reed Kotler497311a2014-10-10 17:39:51 +0000358 return 0;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000359 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
360 const ConstantInt *CI = cast<ConstantInt>(C);
Vasileios Kalintiris77fb0a32015-07-30 11:51:44 +0000361 return materialize32BitInt(CI->getZExtValue(), RC);
Reed Kotler497311a2014-10-10 17:39:51 +0000362}
363
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000364unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
365 const TargetRegisterClass *RC) {
366 unsigned ResultReg = createResultReg(RC);
367
368 if (isInt<16>(Imm)) {
369 unsigned Opc = Mips::ADDiu;
370 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
371 return ResultReg;
372 } else if (isUInt<16>(Imm)) {
373 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
374 return ResultReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000375 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000376 unsigned Lo = Imm & 0xFFFF;
377 unsigned Hi = (Imm >> 16) & 0xFFFF;
378 if (Lo) {
379 // Both Lo and Hi have nonzero bits.
380 unsigned TmpReg = createResultReg(RC);
381 emitInst(Mips::LUi, TmpReg).addImm(Hi);
382 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
383 } else {
384 emitInst(Mips::LUi, ResultReg).addImm(Hi);
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000385 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000386 return ResultReg;
387}
388
389unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
390 if (UnsupportedFPMode)
391 return 0;
392 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
393 if (VT == MVT::f32) {
394 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
395 unsigned DestReg = createResultReg(RC);
396 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
397 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
398 return DestReg;
399 } else if (VT == MVT::f64) {
400 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
401 unsigned DestReg = createResultReg(RC);
402 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
403 unsigned TempReg2 =
404 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
405 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
406 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000407 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000408 return 0;
409}
410
411unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
412 // For now 32-bit only.
413 if (VT != MVT::i32)
414 return 0;
415 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
416 unsigned DestReg = createResultReg(RC);
417 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
418 bool IsThreadLocal = GVar && GVar->isThreadLocal();
419 // TLS not supported at this time.
420 if (IsThreadLocal)
421 return 0;
422 emitInst(Mips::LW, DestReg)
423 .addReg(MFI->getGlobalBaseReg())
424 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
425 if ((GV->hasInternalLinkage() ||
426 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
427 unsigned TempReg = createResultReg(RC);
428 emitInst(Mips::ADDiu, TempReg)
429 .addReg(DestReg)
430 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
431 DestReg = TempReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000432 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000433 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000434}
435
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000436unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000437 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
438 unsigned DestReg = createResultReg(RC);
439 emitInst(Mips::LW, DestReg)
440 .addReg(MFI->getGlobalBaseReg())
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000441 .addSym(Sym, MipsII::MO_GOT);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000442 return DestReg;
443}
444
Reed Kotlerbab3f232014-05-01 20:39:21 +0000445// Materialize a constant into a register, and return the register
446// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000447unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000448 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000449
450 // Only handle simple types.
451 if (!CEVT.isSimple())
452 return 0;
453 MVT VT = CEVT.getSimpleVT();
454
455 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000456 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000457 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000458 return materializeGV(GV, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000459 else if (isa<ConstantInt>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000460 return materializeInt(C, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000461
462 return 0;
463}
464
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000465bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000466 const User *U = nullptr;
467 unsigned Opcode = Instruction::UserOp1;
468 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
469 // Don't walk into other basic blocks unless the object is an alloca from
470 // another block, otherwise it may not have a virtual register assigned.
471 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
472 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
473 Opcode = I->getOpcode();
474 U = I;
475 }
Vasileios Kalintiris32cd69a2015-05-12 12:08:31 +0000476 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
477 Opcode = C->getOpcode();
478 U = C;
479 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000480 switch (Opcode) {
481 default:
482 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000483 case Instruction::BitCast:
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000484 // Look through bitcasts.
485 return computeAddress(U->getOperand(0), Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000486 case Instruction::GetElementPtr: {
487 Address SavedAddr = Addr;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000488 int64_t TmpOffset = Addr.getOffset();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000489 // Iterate through the GEP folding the constants into offsets where
490 // we can.
491 gep_type_iterator GTI = gep_type_begin(U);
492 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
493 ++i, ++GTI) {
494 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000495 if (StructType *STy = GTI.getStructTypeOrNull()) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000496 const StructLayout *SL = DL.getStructLayout(STy);
497 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
498 TmpOffset += SL->getElementOffset(Idx);
499 } else {
500 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000501 while (true) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000502 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
503 // Constant-offset addressing.
504 TmpOffset += CI->getSExtValue() * S;
505 break;
506 }
507 if (canFoldAddIntoGEP(U, Op)) {
508 // A compatible add with a constant operand. Fold the constant.
509 ConstantInt *CI =
510 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
511 TmpOffset += CI->getSExtValue() * S;
512 // Iterate on the other operand.
513 Op = cast<AddOperator>(Op)->getOperand(0);
514 continue;
515 }
516 // Unsupported
517 goto unsupported_gep;
518 }
519 }
520 }
521 // Try to grab the base operand now.
522 Addr.setOffset(TmpOffset);
523 if (computeAddress(U->getOperand(0), Addr))
524 return true;
525 // We failed, restore everything and try the other options.
526 Addr = SavedAddr;
527 unsupported_gep:
528 break;
529 }
530 case Instruction::Alloca: {
531 const AllocaInst *AI = cast<AllocaInst>(Obj);
532 DenseMap<const AllocaInst *, int>::iterator SI =
533 FuncInfo.StaticAllocaMap.find(AI);
534 if (SI != FuncInfo.StaticAllocaMap.end()) {
535 Addr.setKind(Address::FrameIndexBase);
536 Addr.setFI(SI->second);
537 return true;
538 }
539 break;
540 }
541 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000542 Addr.setReg(getRegForValue(Obj));
543 return Addr.getReg() != 0;
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000544}
545
Reed Kotlerd5c41962014-11-13 23:37:45 +0000546bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000547 const User *U = nullptr;
548 unsigned Opcode = Instruction::UserOp1;
549
550 if (const auto *I = dyn_cast<Instruction>(V)) {
551 // Check if the value is defined in the same basic block. This information
552 // is crucial to know whether or not folding an operand is valid.
553 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
554 Opcode = I->getOpcode();
555 U = I;
556 }
557 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
558 Opcode = C->getOpcode();
559 U = C;
560 }
561
562 switch (Opcode) {
563 default:
564 break;
565 case Instruction::BitCast:
566 // Look past bitcasts if its operand is in the same BB.
567 return computeCallAddress(U->getOperand(0), Addr);
568 break;
569 case Instruction::IntToPtr:
570 // Look past no-op inttoptrs if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000571 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
572 TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000573 return computeCallAddress(U->getOperand(0), Addr);
574 break;
575 case Instruction::PtrToInt:
576 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000577 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000578 return computeCallAddress(U->getOperand(0), Addr);
579 break;
580 }
581
Reed Kotlerd5c41962014-11-13 23:37:45 +0000582 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
583 Addr.setGlobalValue(GV);
584 return true;
585 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000586
587 // If all else fails, try to materialize the value in a register.
588 if (!Addr.getGlobalValue()) {
589 Addr.setReg(getRegForValue(V));
590 return Addr.getReg() != 0;
591 }
592
Reed Kotlerd5c41962014-11-13 23:37:45 +0000593 return false;
594}
595
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000596bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000597 EVT evt = TLI.getValueType(DL, Ty, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000598 // Only handle simple types.
599 if (evt == MVT::Other || !evt.isSimple())
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000600 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000601 VT = evt.getSimpleVT();
602
603 // Handle all legal types, i.e. a register that will directly hold this
604 // value.
605 return TLI.isTypeLegal(VT);
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000606}
607
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000608bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
609 if (Ty->isVectorTy())
610 return false;
611
612 if (isTypeLegal(Ty, VT))
613 return true;
614
615 // If this is a type than can be sign or zero-extended to a basic operation
616 // go ahead and accept it now.
617 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
618 return true;
619
620 return false;
621}
622
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000623bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
624 if (isTypeLegal(Ty, VT))
Reed Kotler62de6b92014-10-11 00:55:18 +0000625 return true;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000626 // We will extend this in a later patch:
627 // If this is a type than can be sign or zero-extended to a basic operation
628 // go ahead and accept it now.
629 if (VT == MVT::i8 || VT == MVT::i16)
630 return true;
Reed Kotler62de6b92014-10-11 00:55:18 +0000631 return false;
632}
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000633
Reed Kotler62de6b92014-10-11 00:55:18 +0000634// Because of how EmitCmp is called with fast-isel, you can
Reed Kotler497311a2014-10-10 17:39:51 +0000635// end up with redundant "andi" instructions after the sequences emitted below.
636// We should try and solve this issue in the future.
637//
Reed Kotlera562b462014-10-13 21:46:41 +0000638bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000639 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000640 bool IsUnsigned = CI->isUnsigned();
Reed Kotler497311a2014-10-10 17:39:51 +0000641 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
642 if (LeftReg == 0)
643 return false;
644 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
645 if (RightReg == 0)
646 return false;
Reed Kotler1f64eca2014-10-10 20:46:28 +0000647 CmpInst::Predicate P = CI->getPredicate();
Reed Kotler62de6b92014-10-11 00:55:18 +0000648
Reed Kotler1f64eca2014-10-10 20:46:28 +0000649 switch (P) {
Reed Kotler497311a2014-10-10 17:39:51 +0000650 default:
651 return false;
652 case CmpInst::ICMP_EQ: {
653 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000654 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
655 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000656 break;
657 }
658 case CmpInst::ICMP_NE: {
659 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000660 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
661 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000662 break;
663 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000664 case CmpInst::ICMP_UGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000665 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000666 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000667 case CmpInst::ICMP_ULT:
Reed Kotlera562b462014-10-13 21:46:41 +0000668 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000669 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000670 case CmpInst::ICMP_UGE: {
671 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000672 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
673 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000674 break;
675 }
676 case CmpInst::ICMP_ULE: {
677 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000678 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
679 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000680 break;
681 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000682 case CmpInst::ICMP_SGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000683 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000684 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000685 case CmpInst::ICMP_SLT:
Reed Kotlera562b462014-10-13 21:46:41 +0000686 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000687 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000688 case CmpInst::ICMP_SGE: {
689 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000690 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
691 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000692 break;
693 }
694 case CmpInst::ICMP_SLE: {
695 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000696 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
697 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000698 break;
699 }
Reed Kotler1f64eca2014-10-10 20:46:28 +0000700 case CmpInst::FCMP_OEQ:
701 case CmpInst::FCMP_UNE:
702 case CmpInst::FCMP_OLT:
703 case CmpInst::FCMP_OLE:
704 case CmpInst::FCMP_OGT:
705 case CmpInst::FCMP_OGE: {
706 if (UnsupportedFPMode)
707 return false;
708 bool IsFloat = Left->getType()->isFloatTy();
709 bool IsDouble = Left->getType()->isDoubleTy();
710 if (!IsFloat && !IsDouble)
711 return false;
712 unsigned Opc, CondMovOpc;
713 switch (P) {
714 case CmpInst::FCMP_OEQ:
715 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
716 CondMovOpc = Mips::MOVT_I;
717 break;
718 case CmpInst::FCMP_UNE:
719 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
720 CondMovOpc = Mips::MOVF_I;
721 break;
722 case CmpInst::FCMP_OLT:
723 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
724 CondMovOpc = Mips::MOVT_I;
725 break;
726 case CmpInst::FCMP_OLE:
727 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
728 CondMovOpc = Mips::MOVT_I;
729 break;
730 case CmpInst::FCMP_OGT:
731 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
732 CondMovOpc = Mips::MOVF_I;
733 break;
734 case CmpInst::FCMP_OGE:
735 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
736 CondMovOpc = Mips::MOVF_I;
737 break;
738 default:
Chandler Carruth38811cc2014-10-10 21:07:03 +0000739 llvm_unreachable("Only switching of a subset of CCs.");
Reed Kotler1f64eca2014-10-10 20:46:28 +0000740 }
741 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
742 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000743 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
744 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
Simon Dardis730fdb72017-01-16 13:55:58 +0000745 emitInst(Opc).addReg(Mips::FCC0, RegState::Define).addReg(LeftReg)
746 .addReg(RightReg);
Daniel Sandersa6cda122016-05-06 12:57:26 +0000747 emitInst(CondMovOpc, ResultReg)
748 .addReg(RegWithOne)
749 .addReg(Mips::FCC0)
750 .addReg(RegWithZero);
Reed Kotler1f64eca2014-10-10 20:46:28 +0000751 break;
752 }
Reed Kotler497311a2014-10-10 17:39:51 +0000753 }
Reed Kotler62de6b92014-10-11 00:55:18 +0000754 return true;
755}
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000756
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000757bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
758 unsigned Alignment) {
759 //
760 // more cases will be handled here in following patches.
761 //
762 unsigned Opc;
763 switch (VT.SimpleTy) {
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000764 case MVT::i32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000765 ResultReg = createResultReg(&Mips::GPR32RegClass);
766 Opc = Mips::LW;
767 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000768 case MVT::i16:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000769 ResultReg = createResultReg(&Mips::GPR32RegClass);
770 Opc = Mips::LHu;
771 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000772 case MVT::i8:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000773 ResultReg = createResultReg(&Mips::GPR32RegClass);
774 Opc = Mips::LBu;
775 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000776 case MVT::f32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000777 if (UnsupportedFPMode)
778 return false;
779 ResultReg = createResultReg(&Mips::FGR32RegClass);
780 Opc = Mips::LWC1;
781 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000782 case MVT::f64:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000783 if (UnsupportedFPMode)
784 return false;
785 ResultReg = createResultReg(&Mips::AFGR64RegClass);
786 Opc = Mips::LDC1;
787 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000788 default:
789 return false;
790 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000791 if (Addr.isRegBase()) {
792 simplifyAddress(Addr);
793 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
794 return true;
795 }
796 if (Addr.isFIBase()) {
797 unsigned FI = Addr.getFI();
798 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000799 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000800 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000801 MachineMemOperand *MMO = MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +0000802 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000803 MFI.getObjectSize(FI), Align);
804 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
805 .addFrameIndex(FI)
806 .addImm(Offset)
807 .addMemOperand(MMO);
808 return true;
809 }
810 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000811}
812
813bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
814 unsigned Alignment) {
815 //
816 // more cases will be handled here in following patches.
817 //
818 unsigned Opc;
819 switch (VT.SimpleTy) {
820 case MVT::i8:
821 Opc = Mips::SB;
822 break;
823 case MVT::i16:
824 Opc = Mips::SH;
825 break;
826 case MVT::i32:
827 Opc = Mips::SW;
828 break;
829 case MVT::f32:
830 if (UnsupportedFPMode)
831 return false;
832 Opc = Mips::SWC1;
833 break;
834 case MVT::f64:
835 if (UnsupportedFPMode)
836 return false;
837 Opc = Mips::SDC1;
838 break;
839 default:
840 return false;
841 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000842 if (Addr.isRegBase()) {
843 simplifyAddress(Addr);
844 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
845 return true;
846 }
847 if (Addr.isFIBase()) {
848 unsigned FI = Addr.getFI();
849 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000850 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000851 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000852 MachineMemOperand *MMO = MF->getMachineMemOperand(
Simon Dardisd8bceb92016-04-29 16:07:47 +0000853 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000854 MFI.getObjectSize(FI), Align);
855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
856 .addReg(SrcReg)
857 .addFrameIndex(FI)
858 .addImm(Offset)
859 .addMemOperand(MMO);
860 return true;
861 }
862 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000863}
864
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000865bool MipsFastISel::selectLogicalOp(const Instruction *I) {
866 MVT VT;
867 if (!isTypeSupported(I->getType(), VT))
868 return false;
869
870 unsigned ResultReg;
871 switch (I->getOpcode()) {
872 default:
873 llvm_unreachable("Unexpected instruction.");
874 case Instruction::And:
875 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
876 break;
877 case Instruction::Or:
878 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
879 break;
880 case Instruction::Xor:
881 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
882 break;
883 }
884
885 if (!ResultReg)
886 return false;
887
888 updateValueMap(I, ResultReg);
889 return true;
890}
891
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000892bool MipsFastISel::selectLoad(const Instruction *I) {
893 // Atomic loads need special handling.
894 if (cast<LoadInst>(I)->isAtomic())
895 return false;
896
897 // Verify we have a legal type before going any further.
898 MVT VT;
899 if (!isLoadTypeLegal(I->getType(), VT))
900 return false;
901
902 // See if we can handle this address.
903 Address Addr;
904 if (!computeAddress(I->getOperand(0), Addr))
905 return false;
906
907 unsigned ResultReg;
908 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
909 return false;
910 updateValueMap(I, ResultReg);
911 return true;
912}
913
914bool MipsFastISel::selectStore(const Instruction *I) {
915 Value *Op0 = I->getOperand(0);
916 unsigned SrcReg = 0;
917
918 // Atomic stores need special handling.
919 if (cast<StoreInst>(I)->isAtomic())
920 return false;
921
922 // Verify we have a legal type before going any further.
923 MVT VT;
924 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
925 return false;
926
927 // Get the value to be stored into a register.
928 SrcReg = getRegForValue(Op0);
929 if (SrcReg == 0)
930 return false;
931
932 // See if we can handle this address.
933 Address Addr;
934 if (!computeAddress(I->getOperand(1), Addr))
935 return false;
936
937 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
938 return false;
939 return true;
940}
941
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000942// This can cause a redundant sltiu to be generated.
943// FIXME: try and eliminate this in a future patch.
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000944bool MipsFastISel::selectBranch(const Instruction *I) {
945 const BranchInst *BI = cast<BranchInst>(I);
946 MachineBasicBlock *BrBB = FuncInfo.MBB;
947 //
948 // TBB is the basic block for the case where the comparison is true.
949 // FBB is the basic block for the case where the comparison is false.
950 // if (cond) goto TBB
951 // goto FBB
952 // TBB:
953 //
954 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
955 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000956 // For now, just try the simplest case where it's fed by a compare.
957 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Petar Jovanovicf9808c52018-09-24 14:14:19 +0000958 MVT CIMVT =
959 TLI.getValueType(DL, CI->getOperand(0)->getType(), true).getSimpleVT();
960 if (CIMVT == MVT::i1)
961 return false;
962
Petar Jovanovic3af2c9922018-07-02 08:56:57 +0000963 unsigned CondReg = getRegForValue(CI);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000964 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
965 .addReg(CondReg)
966 .addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000967 finishCondBranch(BI->getParent(), TBB, FBB);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000968 return true;
969 }
970 return false;
971}
Reed Kotler62de6b92014-10-11 00:55:18 +0000972
Reed Kotlera562b462014-10-13 21:46:41 +0000973bool MipsFastISel::selectCmp(const Instruction *I) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000974 const CmpInst *CI = cast<CmpInst>(I);
975 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000976 if (!emitCmp(ResultReg, CI))
Reed Kotler62de6b92014-10-11 00:55:18 +0000977 return false;
Reed Kotler497311a2014-10-10 17:39:51 +0000978 updateValueMap(I, ResultReg);
979 return true;
980}
981
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000982// Attempt to fast-select a floating-point extend instruction.
983bool MipsFastISel::selectFPExt(const Instruction *I) {
984 if (UnsupportedFPMode)
985 return false;
986 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000987 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
988 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000989
990 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
991 return false;
992
993 unsigned SrcReg =
Nico Weber2cf5e892016-06-10 20:06:03 +0000994 getRegForValue(Src); // this must be a 32bit floating point register class
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000995 // maybe we should handle this differently
996 if (!SrcReg)
997 return false;
998
999 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
1000 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
1001 updateValueMap(I, DestReg);
1002 return true;
1003}
1004
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001005bool MipsFastISel::selectSelect(const Instruction *I) {
1006 assert(isa<SelectInst>(I) && "Expected a select instruction.");
1007
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001008 LLVM_DEBUG(dbgs() << "selectSelect\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001009
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001010 MVT VT;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001011 if (!isTypeSupported(I->getType(), VT) || UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001012 LLVM_DEBUG(
1013 dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001014 return false;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001015 }
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001016
1017 unsigned CondMovOpc;
1018 const TargetRegisterClass *RC;
1019
1020 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
1021 CondMovOpc = Mips::MOVN_I_I;
1022 RC = &Mips::GPR32RegClass;
1023 } else if (VT == MVT::f32) {
1024 CondMovOpc = Mips::MOVN_I_S;
1025 RC = &Mips::FGR32RegClass;
1026 } else if (VT == MVT::f64) {
1027 CondMovOpc = Mips::MOVN_I_D32;
1028 RC = &Mips::AFGR64RegClass;
1029 } else
1030 return false;
1031
1032 const SelectInst *SI = cast<SelectInst>(I);
1033 const Value *Cond = SI->getCondition();
1034 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
1035 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
1036 unsigned CondReg = getRegForValue(Cond);
1037
1038 if (!Src1Reg || !Src2Reg || !CondReg)
1039 return false;
1040
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001041 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
1042 if (!ZExtCondReg)
1043 return false;
1044
1045 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true))
1046 return false;
1047
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001048 unsigned ResultReg = createResultReg(RC);
1049 unsigned TempReg = createResultReg(RC);
1050
1051 if (!ResultReg || !TempReg)
1052 return false;
1053
1054 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
1055 emitInst(CondMovOpc, ResultReg)
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001056 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001057 updateValueMap(I, ResultReg);
1058 return true;
1059}
1060
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001061// Attempt to fast-select a floating-point truncate instruction.
1062bool MipsFastISel::selectFPTrunc(const Instruction *I) {
1063 if (UnsupportedFPMode)
1064 return false;
1065 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001066 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1067 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001068
1069 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1070 return false;
1071
1072 unsigned SrcReg = getRegForValue(Src);
1073 if (!SrcReg)
1074 return false;
1075
1076 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1077 if (!DestReg)
1078 return false;
1079
1080 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1081 updateValueMap(I, DestReg);
1082 return true;
1083}
1084
1085// Attempt to fast-select a floating-point-to-integer conversion.
1086bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1087 if (UnsupportedFPMode)
1088 return false;
1089 MVT DstVT, SrcVT;
1090 if (!IsSigned)
1091 return false; // We don't handle this case yet. There is no native
1092 // instruction for this but it can be synthesized.
1093 Type *DstTy = I->getType();
1094 if (!isTypeLegal(DstTy, DstVT))
1095 return false;
1096
1097 if (DstVT != MVT::i32)
1098 return false;
1099
1100 Value *Src = I->getOperand(0);
1101 Type *SrcTy = Src->getType();
1102 if (!isTypeLegal(SrcTy, SrcVT))
1103 return false;
1104
1105 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1106 return false;
1107
1108 unsigned SrcReg = getRegForValue(Src);
1109 if (SrcReg == 0)
1110 return false;
1111
1112 // Determine the opcode for the conversion, which takes place
1113 // entirely within FPRs.
1114 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1115 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001116 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001117
1118 // Generate the convert.
1119 emitInst(Opc, TempReg).addReg(SrcReg);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001120 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1121
1122 updateValueMap(I, DestReg);
1123 return true;
1124}
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001125
Reed Kotlerd5c41962014-11-13 23:37:45 +00001126bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1127 SmallVectorImpl<MVT> &OutVTs,
1128 unsigned &NumBytes) {
1129 CallingConv::ID CC = CLI.CallConv;
1130 SmallVector<CCValAssign, 16> ArgLocs;
1131 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1132 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1133 // Get a count of how many bytes are to be pushed on the stack.
1134 NumBytes = CCInfo.getNextStackOffset();
1135 // This is the minimum argument area used for A0-A3.
1136 if (NumBytes < 16)
1137 NumBytes = 16;
1138
Serge Pavlovd526b132017-05-09 13:35:13 +00001139 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001140 // Process the args.
1141 MVT firstMVT;
1142 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1143 CCValAssign &VA = ArgLocs[i];
1144 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1145 MVT ArgVT = OutVTs[VA.getValNo()];
1146
1147 if (i == 0) {
1148 firstMVT = ArgVT;
1149 if (ArgVT == MVT::f32) {
1150 VA.convertToReg(Mips::F12);
1151 } else if (ArgVT == MVT::f64) {
1152 VA.convertToReg(Mips::D6);
1153 }
1154 } else if (i == 1) {
1155 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1156 if (ArgVT == MVT::f32) {
1157 VA.convertToReg(Mips::F14);
1158 } else if (ArgVT == MVT::f64) {
1159 VA.convertToReg(Mips::D7);
1160 }
1161 }
1162 }
Vasileios Kalintirisb48c9052015-05-12 12:29:17 +00001163 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1164 (ArgVT == MVT::i8)) &&
1165 VA.isMemLoc()) {
Reed Kotlerd5c41962014-11-13 23:37:45 +00001166 switch (VA.getLocMemOffset()) {
1167 case 0:
1168 VA.convertToReg(Mips::A0);
1169 break;
1170 case 4:
1171 VA.convertToReg(Mips::A1);
1172 break;
1173 case 8:
1174 VA.convertToReg(Mips::A2);
1175 break;
1176 case 12:
1177 VA.convertToReg(Mips::A3);
1178 break;
1179 default:
1180 break;
1181 }
1182 }
1183 unsigned ArgReg = getRegForValue(ArgVal);
1184 if (!ArgReg)
1185 return false;
1186
1187 // Handle arg promotion: SExt, ZExt, AExt.
1188 switch (VA.getLocInfo()) {
1189 case CCValAssign::Full:
1190 break;
1191 case CCValAssign::AExt:
1192 case CCValAssign::SExt: {
1193 MVT DestVT = VA.getLocVT();
1194 MVT SrcVT = ArgVT;
1195 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1196 if (!ArgReg)
1197 return false;
1198 break;
1199 }
1200 case CCValAssign::ZExt: {
1201 MVT DestVT = VA.getLocVT();
1202 MVT SrcVT = ArgVT;
1203 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1204 if (!ArgReg)
1205 return false;
1206 break;
1207 }
1208 default:
1209 llvm_unreachable("Unknown arg promotion!");
1210 }
1211
1212 // Now copy/store arg to correct locations.
1213 if (VA.isRegLoc() && !VA.needsCustom()) {
1214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1215 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1216 CLI.OutRegs.push_back(VA.getLocReg());
1217 } else if (VA.needsCustom()) {
1218 llvm_unreachable("Mips does not use custom args.");
1219 return false;
1220 } else {
1221 //
1222 // FIXME: This path will currently return false. It was copied
1223 // from the AArch64 port and should be essentially fine for Mips too.
1224 // The work to finish up this path will be done in a follow-on patch.
1225 //
1226 assert(VA.isMemLoc() && "Assuming store on stack.");
1227 // Don't emit stores for undef values.
1228 if (isa<UndefValue>(ArgVal))
1229 continue;
1230
1231 // Need to store on the stack.
1232 // FIXME: This alignment is incorrect but this path is disabled
1233 // for now (will return false). We need to determine the right alignment
1234 // based on the normal alignment for the underlying machine type.
1235 //
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001236 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001237
1238 unsigned BEAlign = 0;
1239 if (ArgSize < 8 && !Subtarget->isLittle())
1240 BEAlign = 8 - ArgSize;
1241
1242 Address Addr;
1243 Addr.setKind(Address::RegBase);
1244 Addr.setReg(Mips::SP);
1245 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1246
1247 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1248 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00001249 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
Reed Kotlerd5c41962014-11-13 23:37:45 +00001250 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1251 (void)(MMO);
1252 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1253 return false; // can't store on the stack yet.
1254 }
1255 }
1256
1257 return true;
1258}
1259
1260bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1261 unsigned NumBytes) {
1262 CallingConv::ID CC = CLI.CallConv;
Daniel Sanders01bcefd2016-05-03 14:19:26 +00001263 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001264 if (RetVT != MVT::isVoid) {
1265 SmallVector<CCValAssign, 16> RVLocs;
Simon Dardis70f79252017-04-26 11:10:38 +00001266 MipsCCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1267
1268 CCInfo.AnalyzeCallResult(CLI.Ins, RetCC_Mips, CLI.RetTy,
Simon Dardis9d580e82017-04-29 16:31:40 +00001269 CLI.Symbol ? CLI.Symbol->getName().data()
1270 : nullptr);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001271
1272 // Only handle a single return value.
1273 if (RVLocs.size() != 1)
1274 return false;
1275 // Copy all of the result registers out of their specified physreg.
1276 MVT CopyVT = RVLocs[0].getValVT();
1277 // Special handling for extended integers.
1278 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1279 CopyVT = MVT::i32;
1280
1281 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001282 if (!ResultReg)
1283 return false;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1285 TII.get(TargetOpcode::COPY),
1286 ResultReg).addReg(RVLocs[0].getLocReg());
1287 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1288
1289 CLI.ResultReg = ResultReg;
1290 CLI.NumResultRegs = 1;
1291 }
1292 return true;
1293}
1294
Daniel Sanderscbaca422016-07-29 12:27:28 +00001295bool MipsFastISel::fastLowerArguments() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001296 LLVM_DEBUG(dbgs() << "fastLowerArguments\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001297
1298 if (!FuncInfo.CanLowerReturn) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001299 LLVM_DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001300 return false;
1301 }
1302
1303 const Function *F = FuncInfo.Fn;
1304 if (F->isVarArg()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001305 LLVM_DEBUG(dbgs() << ".. gave up (varargs)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001306 return false;
1307 }
1308
1309 CallingConv::ID CC = F->getCallingConv();
1310 if (CC != CallingConv::C) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001311 LLVM_DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001312 return false;
1313 }
1314
Manoj Guptaafb355b2018-04-05 23:23:29 +00001315 std::array<MCPhysReg, 4> GPR32ArgRegs = {{Mips::A0, Mips::A1, Mips::A2,
1316 Mips::A3}};
1317 std::array<MCPhysReg, 2> FGR32ArgRegs = {{Mips::F12, Mips::F14}};
1318 std::array<MCPhysReg, 2> AFGR64ArgRegs = {{Mips::D6, Mips::D7}};
Manoj Gupta9d68b9e2018-04-05 22:47:25 +00001319 auto NextGPR32 = GPR32ArgRegs.begin();
1320 auto NextFGR32 = FGR32ArgRegs.begin();
1321 auto NextAFGR64 = AFGR64ArgRegs.begin();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001322
1323 struct AllocatedReg {
1324 const TargetRegisterClass *RC;
1325 unsigned Reg;
1326 AllocatedReg(const TargetRegisterClass *RC, unsigned Reg)
1327 : RC(RC), Reg(Reg) {}
1328 };
1329
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001330 // Only handle simple cases. i.e. All arguments are directly mapped to
1331 // registers of the appropriate type.
Daniel Sanderscbaca422016-07-29 12:27:28 +00001332 SmallVector<AllocatedReg, 4> Allocation;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001333 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001334 if (FormalArg.hasAttribute(Attribute::InReg) ||
1335 FormalArg.hasAttribute(Attribute::StructRet) ||
1336 FormalArg.hasAttribute(Attribute::ByVal)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001337 LLVM_DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001338 return false;
1339 }
1340
1341 Type *ArgTy = FormalArg.getType();
1342 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001343 LLVM_DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001344 return false;
1345 }
1346
1347 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001348 LLVM_DEBUG(dbgs() << ".. " << FormalArg.getArgNo() << ": "
1349 << ArgVT.getEVTString() << "\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001350 if (!ArgVT.isSimple()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001351 LLVM_DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001352 return false;
1353 }
1354
1355 switch (ArgVT.getSimpleVT().SimpleTy) {
1356 case MVT::i1:
1357 case MVT::i8:
1358 case MVT::i16:
Reid Kleckner6652a522017-04-28 18:37:16 +00001359 if (!FormalArg.hasAttribute(Attribute::SExt) &&
1360 !FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001361 // It must be any extend, this shouldn't happen for clang-generated IR
1362 // so just fall back on SelectionDAG.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001363 LLVM_DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001364 return false;
1365 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001366
1367 if (NextGPR32 == GPR32ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001368 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001369 return false;
1370 }
1371
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001372 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001373 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1374
1375 // Allocating any GPR32 prohibits further use of floating point arguments.
1376 NextFGR32 = FGR32ArgRegs.end();
1377 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001378 break;
1379
1380 case MVT::i32:
Reid Kleckner6652a522017-04-28 18:37:16 +00001381 if (FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001382 // The O32 ABI does not permit a zero-extended i32.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001383 LLVM_DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001384 return false;
1385 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001386
1387 if (NextGPR32 == GPR32ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001388 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001389 return false;
1390 }
1391
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001392 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001393 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1394
1395 // Allocating any GPR32 prohibits further use of floating point arguments.
1396 NextFGR32 = FGR32ArgRegs.end();
1397 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001398 break;
1399
1400 case MVT::f32:
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001401 if (UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001402 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001403 return false;
1404 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001405 if (NextFGR32 == FGR32ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001406 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001407 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001408 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001409 LLVM_DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001410 Allocation.emplace_back(&Mips::FGR32RegClass, *NextFGR32++);
1411 // Allocating an FGR32 also allocates the super-register AFGR64, and
1412 // ABI rules require us to skip the corresponding GPR32.
1413 if (NextGPR32 != GPR32ArgRegs.end())
1414 NextGPR32++;
1415 if (NextAFGR64 != AFGR64ArgRegs.end())
1416 NextAFGR64++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001417 break;
1418
1419 case MVT::f64:
Simon Dardisb432a3e2016-09-06 12:36:24 +00001420 if (UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001421 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001422 return false;
1423 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001424 if (NextAFGR64 == AFGR64ArgRegs.end()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001425 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001426 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001427 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001428 LLVM_DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64 << ")\n");
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001429 Allocation.emplace_back(&Mips::AFGR64RegClass, *NextAFGR64++);
1430 // Allocating an FGR32 also allocates the super-register AFGR64, and
1431 // ABI rules require us to skip the corresponding GPR32 pair.
1432 if (NextGPR32 != GPR32ArgRegs.end())
1433 NextGPR32++;
1434 if (NextGPR32 != GPR32ArgRegs.end())
1435 NextGPR32++;
1436 if (NextFGR32 != FGR32ArgRegs.end())
1437 NextFGR32++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001438 break;
1439
1440 default:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001441 LLVM_DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001442 return false;
1443 }
Daniel Sanderscbaca422016-07-29 12:27:28 +00001444 }
1445
Daniel Sanderscbaca422016-07-29 12:27:28 +00001446 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001447 unsigned ArgNo = FormalArg.getArgNo();
1448 unsigned SrcReg = Allocation[ArgNo].Reg;
1449 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001450 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1451 // Without this, EmitLiveInCopies may eliminate the livein if its only
1452 // use is a bitcast (which isn't turned into an instruction).
Reid Kleckner6652a522017-04-28 18:37:16 +00001453 unsigned ResultReg = createResultReg(Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001454 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1455 TII.get(TargetOpcode::COPY), ResultReg)
1456 .addReg(DstReg, getKillRegState(true));
1457 updateValueMap(&FormalArg, ResultReg);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001458 }
1459
1460 // Calculate the size of the incoming arguments area.
1461 // We currently reject all the cases where this would be non-zero.
1462 unsigned IncomingArgSizeInBytes = 0;
1463
1464 // Account for the reserved argument area on ABI's that have one (O32).
1465 // It seems strange to do this on the caller side but it's necessary in
1466 // SelectionDAG's implementation.
1467 IncomingArgSizeInBytes = std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC),
1468 IncomingArgSizeInBytes);
1469
1470 MF->getInfo<MipsFunctionInfo>()->setFormalArgInfo(IncomingArgSizeInBytes,
1471 false);
1472
1473 return true;
1474}
1475
Reed Kotlerd5c41962014-11-13 23:37:45 +00001476bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1477 CallingConv::ID CC = CLI.CallConv;
1478 bool IsTailCall = CLI.IsTailCall;
1479 bool IsVarArg = CLI.IsVarArg;
1480 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001481 MCSymbol *Symbol = CLI.Symbol;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001482
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001483 // Do not handle FastCC.
1484 if (CC == CallingConv::Fast)
1485 return false;
1486
Reed Kotlerd5c41962014-11-13 23:37:45 +00001487 // Allow SelectionDAG isel to handle tail calls.
1488 if (IsTailCall)
1489 return false;
1490
1491 // Let SDISel handle vararg functions.
1492 if (IsVarArg)
1493 return false;
1494
1495 // FIXME: Only handle *simple* calls for now.
1496 MVT RetVT;
1497 if (CLI.RetTy->isVoidTy())
1498 RetVT = MVT::isVoid;
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001499 else if (!isTypeSupported(CLI.RetTy, RetVT))
Reed Kotlerd5c41962014-11-13 23:37:45 +00001500 return false;
1501
1502 for (auto Flag : CLI.OutFlags)
1503 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1504 return false;
1505
1506 // Set up the argument vectors.
1507 SmallVector<MVT, 16> OutVTs;
1508 OutVTs.reserve(CLI.OutVals.size());
1509
1510 for (auto *Val : CLI.OutVals) {
1511 MVT VT;
1512 if (!isTypeLegal(Val->getType(), VT) &&
1513 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1514 return false;
1515
1516 // We don't handle vector parameters yet.
1517 if (VT.isVector() || VT.getSizeInBits() > 64)
1518 return false;
1519
1520 OutVTs.push_back(VT);
1521 }
1522
1523 Address Addr;
1524 if (!computeCallAddress(Callee, Addr))
1525 return false;
1526
1527 // Handle the arguments now that we've gotten them.
1528 unsigned NumBytes;
1529 if (!processCallArgs(CLI, OutVTs, NumBytes))
1530 return false;
1531
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001532 if (!Addr.getGlobalValue())
1533 return false;
1534
Reed Kotlerd5c41962014-11-13 23:37:45 +00001535 // Issue the call.
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001536 unsigned DestAddress;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001537 if (Symbol)
1538 DestAddress = materializeExternalCallSym(Symbol);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001539 else
1540 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001541 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1542 MachineInstrBuilder MIB =
1543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1544 Mips::RA).addReg(Mips::T9);
1545
1546 // Add implicit physical register uses to the call.
1547 for (auto Reg : CLI.OutRegs)
1548 MIB.addReg(Reg, RegState::Implicit);
1549
1550 // Add a register mask with the call-preserved registers.
1551 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001552 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Reed Kotlerd5c41962014-11-13 23:37:45 +00001553
1554 CLI.Call = MIB;
1555
Vladimir Stefanovic3daf8bc2019-01-17 21:50:37 +00001556 if (EmitJalrReloc && !Subtarget->inMips16Mode()) {
1557 // Attach callee address to the instruction, let asm printer emit
1558 // .reloc R_MIPS_JALR.
1559 if (Symbol)
1560 MIB.addSym(Symbol, MipsII::MO_JALR);
1561 else
1562 MIB.addSym(FuncInfo.MF->getContext().getOrCreateSymbol(
1563 Addr.getGlobalValue()->getName()), MipsII::MO_JALR);
1564 }
1565
Reed Kotlerd5c41962014-11-13 23:37:45 +00001566 // Finish off the call including any return values.
1567 return finishCall(CLI, RetVT, NumBytes);
1568}
1569
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001570bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1571 switch (II->getIntrinsicID()) {
1572 default:
1573 return false;
Vasileios Kalintiriscbbf8e02015-06-01 16:40:45 +00001574 case Intrinsic::bswap: {
1575 Type *RetTy = II->getCalledFunction()->getReturnType();
1576
1577 MVT VT;
1578 if (!isTypeSupported(RetTy, VT))
1579 return false;
1580
1581 unsigned SrcReg = getRegForValue(II->getOperand(0));
1582 if (SrcReg == 0)
1583 return false;
1584 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1585 if (DestReg == 0)
1586 return false;
1587 if (VT == MVT::i16) {
1588 if (Subtarget->hasMips32r2()) {
1589 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1590 updateValueMap(II, DestReg);
1591 return true;
1592 } else {
1593 unsigned TempReg[3];
1594 for (int i = 0; i < 3; i++) {
1595 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1596 if (TempReg[i] == 0)
1597 return false;
1598 }
1599 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1600 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1601 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1602 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1603 updateValueMap(II, DestReg);
1604 return true;
1605 }
1606 } else if (VT == MVT::i32) {
1607 if (Subtarget->hasMips32r2()) {
1608 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1609 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1610 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1611 updateValueMap(II, DestReg);
1612 return true;
1613 } else {
1614 unsigned TempReg[8];
1615 for (int i = 0; i < 8; i++) {
1616 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1617 if (TempReg[i] == 0)
1618 return false;
1619 }
1620
1621 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1622 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1623 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1624 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1625
1626 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1627 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1628
1629 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1630 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1631 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1632 updateValueMap(II, DestReg);
1633 return true;
1634 }
1635 }
1636 return false;
1637 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001638 case Intrinsic::memcpy:
1639 case Intrinsic::memmove: {
1640 const auto *MTI = cast<MemTransferInst>(II);
1641 // Don't handle volatile.
1642 if (MTI->isVolatile())
1643 return false;
1644 if (!MTI->getLength()->getType()->isIntegerTy(32))
1645 return false;
1646 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
Daniel Neilson1e687242018-01-19 17:13:12 +00001647 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 1);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001648 }
1649 case Intrinsic::memset: {
1650 const MemSetInst *MSI = cast<MemSetInst>(II);
1651 // Don't handle volatile.
1652 if (MSI->isVolatile())
1653 return false;
1654 if (!MSI->getLength()->getType()->isIntegerTy(32))
1655 return false;
Daniel Neilson1e687242018-01-19 17:13:12 +00001656 return lowerCallTo(II, "memset", II->getNumArgOperands() - 1);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001657 }
1658 }
1659 return false;
1660}
1661
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001662bool MipsFastISel::selectRet(const Instruction *I) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001663 const Function &F = *I->getParent()->getParent();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001664 const ReturnInst *Ret = cast<ReturnInst>(I);
1665
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001666 LLVM_DEBUG(dbgs() << "selectRet\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001667
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001668 if (!FuncInfo.CanLowerReturn)
1669 return false;
Reed Kotleraa150ed2015-02-12 21:05:12 +00001670
1671 // Build a list of return value registers.
1672 SmallVector<unsigned, 4> RetRegs;
1673
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001674 if (Ret->getNumOperands() > 0) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001675 CallingConv::ID CC = F.getCallingConv();
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001676
1677 // Do not handle FastCC.
1678 if (CC == CallingConv::Fast)
1679 return false;
1680
Reed Kotleraa150ed2015-02-12 21:05:12 +00001681 SmallVector<ISD::OutputArg, 4> Outs;
Matt Arsenault81920b02018-07-28 13:25:19 +00001682 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Mehdi Amini56228da2015-07-09 01:57:34 +00001683
Reed Kotleraa150ed2015-02-12 21:05:12 +00001684 // Analyze operands of the call, assigning locations to each operand.
1685 SmallVector<CCValAssign, 16> ValLocs;
1686 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1687 I->getContext());
1688 CCAssignFn *RetCC = RetCC_Mips;
1689 CCInfo.AnalyzeReturn(Outs, RetCC);
1690
1691 // Only handle a single return value for now.
1692 if (ValLocs.size() != 1)
1693 return false;
1694
1695 CCValAssign &VA = ValLocs[0];
1696 const Value *RV = Ret->getOperand(0);
1697
1698 // Don't bother handling odd stuff for now.
1699 if ((VA.getLocInfo() != CCValAssign::Full) &&
1700 (VA.getLocInfo() != CCValAssign::BCvt))
1701 return false;
1702
1703 // Only handle register returns for now.
1704 if (!VA.isRegLoc())
1705 return false;
1706
1707 unsigned Reg = getRegForValue(RV);
1708 if (Reg == 0)
1709 return false;
1710
1711 unsigned SrcReg = Reg + VA.getValNo();
1712 unsigned DestReg = VA.getLocReg();
1713 // Avoid a cross-class copy. This is very unlikely.
1714 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1715 return false;
1716
Mehdi Amini44ede332015-07-09 02:09:04 +00001717 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Reed Kotleraa150ed2015-02-12 21:05:12 +00001718 if (!RVEVT.isSimple())
1719 return false;
1720
1721 if (RVEVT.isVector())
1722 return false;
1723
1724 MVT RVVT = RVEVT.getSimpleVT();
1725 if (RVVT == MVT::f128)
1726 return false;
1727
Simon Dardisb432a3e2016-09-06 12:36:24 +00001728 // Do not handle FGR64 returns for now.
1729 if (RVVT == MVT::f64 && UnsupportedFPMode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001730 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001731 return false;
1732 }
1733
Reed Kotleraa150ed2015-02-12 21:05:12 +00001734 MVT DestVT = VA.getValVT();
1735 // Special handling for extended integers.
1736 if (RVVT != DestVT) {
1737 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1738 return false;
1739
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001740 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1741 bool IsZExt = Outs[0].Flags.isZExt();
1742 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1743 if (SrcReg == 0)
1744 return false;
1745 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001746 }
1747
1748 // Make the copy.
1749 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1750 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1751
1752 // Add register to return instruction.
1753 RetRegs.push_back(VA.getLocReg());
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001754 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001755 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1756 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1757 MIB.addReg(RetRegs[i], RegState::Implicit);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001758 return true;
1759}
1760
1761bool MipsFastISel::selectTrunc(const Instruction *I) {
1762 // The high bits for a type smaller than the register size are assumed to be
1763 // undefined.
1764 Value *Op = I->getOperand(0);
1765
1766 EVT SrcVT, DestVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001767 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1768 DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001769
1770 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1771 return false;
1772 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1773 return false;
1774
1775 unsigned SrcReg = getRegForValue(Op);
1776 if (!SrcReg)
1777 return false;
1778
1779 // Because the high bits are undefined, a truncate doesn't generate
1780 // any code.
1781 updateValueMap(I, SrcReg);
1782 return true;
1783}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001784
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001785bool MipsFastISel::selectIntExt(const Instruction *I) {
1786 Type *DestTy = I->getType();
1787 Value *Src = I->getOperand(0);
1788 Type *SrcTy = Src->getType();
1789
1790 bool isZExt = isa<ZExtInst>(I);
1791 unsigned SrcReg = getRegForValue(Src);
1792 if (!SrcReg)
1793 return false;
1794
1795 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001796 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1797 DestEVT = TLI.getValueType(DL, DestTy, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001798 if (!SrcEVT.isSimple())
1799 return false;
1800 if (!DestEVT.isSimple())
1801 return false;
1802
1803 MVT SrcVT = SrcEVT.getSimpleVT();
1804 MVT DestVT = DestEVT.getSimpleVT();
1805 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1806
1807 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1808 return false;
1809 updateValueMap(I, ResultReg);
1810 return true;
1811}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001812
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001813bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1814 unsigned DestReg) {
1815 unsigned ShiftAmt;
1816 switch (SrcVT.SimpleTy) {
1817 default:
1818 return false;
1819 case MVT::i8:
1820 ShiftAmt = 24;
1821 break;
1822 case MVT::i16:
1823 ShiftAmt = 16;
1824 break;
1825 }
1826 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1827 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1828 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1829 return true;
1830}
1831
1832bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1833 unsigned DestReg) {
1834 switch (SrcVT.SimpleTy) {
1835 default:
1836 return false;
1837 case MVT::i8:
1838 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1839 break;
1840 case MVT::i16:
1841 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1842 break;
1843 }
1844 return true;
1845}
1846
1847bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1848 unsigned DestReg) {
1849 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1850 return false;
1851 if (Subtarget->hasMips32r2())
1852 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1853 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1854}
1855
1856bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1857 unsigned DestReg) {
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001858 int64_t Imm;
1859
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001860 switch (SrcVT.SimpleTy) {
1861 default:
1862 return false;
1863 case MVT::i1:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001864 Imm = 1;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001865 break;
1866 case MVT::i8:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001867 Imm = 0xff;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001868 break;
1869 case MVT::i16:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001870 Imm = 0xffff;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001871 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001872 }
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001873
1874 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001875 return true;
1876}
1877
1878bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1879 unsigned DestReg, bool IsZExt) {
Vasileios Kalintiris1202f362015-04-24 13:48:19 +00001880 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1881 // DestVT are odd things, so test to make sure that they are both types we can
1882 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1883 // bail out to SelectionDAG.
1884 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1885 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1886 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001887 if (IsZExt)
1888 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1889 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1890}
Reed Kotlerd5c41962014-11-13 23:37:45 +00001891
1892unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1893 bool isZExt) {
1894 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotleraa150ed2015-02-12 21:05:12 +00001895 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1896 return Success ? DestReg : 0;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001897}
1898
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001899bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001900 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001901 if (!DestEVT.isSimple())
1902 return false;
1903
1904 MVT DestVT = DestEVT.getSimpleVT();
1905 if (DestVT != MVT::i32)
1906 return false;
1907
1908 unsigned DivOpc;
1909 switch (ISDOpcode) {
1910 default:
1911 return false;
1912 case ISD::SDIV:
1913 case ISD::SREM:
1914 DivOpc = Mips::SDIV;
1915 break;
1916 case ISD::UDIV:
1917 case ISD::UREM:
1918 DivOpc = Mips::UDIV;
1919 break;
1920 }
1921
1922 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1923 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1924 if (!Src0Reg || !Src1Reg)
1925 return false;
1926
1927 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1928 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1929
1930 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1931 if (!ResultReg)
1932 return false;
1933
1934 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1935 ? Mips::MFHI
1936 : Mips::MFLO;
1937 emitInst(MFOpc, ResultReg);
1938
1939 updateValueMap(I, ResultReg);
1940 return true;
1941}
1942
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001943bool MipsFastISel::selectShift(const Instruction *I) {
1944 MVT RetVT;
1945
1946 if (!isTypeSupported(I->getType(), RetVT))
1947 return false;
1948
1949 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1950 if (!ResultReg)
1951 return false;
1952
1953 unsigned Opcode = I->getOpcode();
1954 const Value *Op0 = I->getOperand(0);
1955 unsigned Op0Reg = getRegForValue(Op0);
1956 if (!Op0Reg)
1957 return false;
1958
1959 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1960 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1961 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1962 if (!TempReg)
1963 return false;
1964
Mehdi Amini44ede332015-07-09 02:09:04 +00001965 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001966 bool IsZExt = Opcode == Instruction::LShr;
1967 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1968 return false;
1969
1970 Op0Reg = TempReg;
1971 }
1972
1973 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1974 uint64_t ShiftVal = C->getZExtValue();
1975
1976 switch (Opcode) {
1977 default:
1978 llvm_unreachable("Unexpected instruction.");
1979 case Instruction::Shl:
1980 Opcode = Mips::SLL;
1981 break;
1982 case Instruction::AShr:
1983 Opcode = Mips::SRA;
1984 break;
1985 case Instruction::LShr:
1986 Opcode = Mips::SRL;
1987 break;
1988 }
1989
1990 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1991 updateValueMap(I, ResultReg);
1992 return true;
1993 }
1994
1995 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1996 if (!Op1Reg)
1997 return false;
1998
1999 switch (Opcode) {
2000 default:
2001 llvm_unreachable("Unexpected instruction.");
2002 case Instruction::Shl:
2003 Opcode = Mips::SLLV;
2004 break;
2005 case Instruction::AShr:
2006 Opcode = Mips::SRAV;
2007 break;
2008 case Instruction::LShr:
2009 Opcode = Mips::SRLV;
2010 break;
2011 }
2012
2013 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
2014 updateValueMap(I, ResultReg);
2015 return true;
2016}
2017
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002018bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
Reed Kotler67077b32014-04-29 17:57:50 +00002019 switch (I->getOpcode()) {
2020 default:
2021 break;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +00002022 case Instruction::Load:
Reed Kotlera562b462014-10-13 21:46:41 +00002023 return selectLoad(I);
Reed Kotlerbab3f232014-05-01 20:39:21 +00002024 case Instruction::Store:
Reed Kotlera562b462014-10-13 21:46:41 +00002025 return selectStore(I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00002026 case Instruction::SDiv:
2027 if (!selectBinaryOp(I, ISD::SDIV))
2028 return selectDivRem(I, ISD::SDIV);
2029 return true;
2030 case Instruction::UDiv:
2031 if (!selectBinaryOp(I, ISD::UDIV))
2032 return selectDivRem(I, ISD::UDIV);
2033 return true;
2034 case Instruction::SRem:
2035 if (!selectBinaryOp(I, ISD::SREM))
2036 return selectDivRem(I, ISD::SREM);
2037 return true;
2038 case Instruction::URem:
2039 if (!selectBinaryOp(I, ISD::UREM))
2040 return selectDivRem(I, ISD::UREM);
2041 return true;
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00002042 case Instruction::Shl:
2043 case Instruction::LShr:
2044 case Instruction::AShr:
2045 return selectShift(I);
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002046 case Instruction::And:
2047 case Instruction::Or:
2048 case Instruction::Xor:
2049 return selectLogicalOp(I);
Reed Kotler62de6b92014-10-11 00:55:18 +00002050 case Instruction::Br:
Reed Kotlera562b462014-10-13 21:46:41 +00002051 return selectBranch(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002052 case Instruction::Ret:
Reed Kotlera562b462014-10-13 21:46:41 +00002053 return selectRet(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002054 case Instruction::Trunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002055 return selectTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002056 case Instruction::ZExt:
2057 case Instruction::SExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002058 return selectIntExt(I);
Reed Kotlerb9dc2482014-10-01 18:47:02 +00002059 case Instruction::FPTrunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002060 return selectFPTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002061 case Instruction::FPExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002062 return selectFPExt(I);
Reed Kotler12f94882014-10-10 17:00:46 +00002063 case Instruction::FPToSI:
Reed Kotlera562b462014-10-13 21:46:41 +00002064 return selectFPToInt(I, /*isSigned*/ true);
Reed Kotler12f94882014-10-10 17:00:46 +00002065 case Instruction::FPToUI:
Reed Kotlera562b462014-10-13 21:46:41 +00002066 return selectFPToInt(I, /*isSigned*/ false);
Reed Kotler497311a2014-10-10 17:39:51 +00002067 case Instruction::ICmp:
2068 case Instruction::FCmp:
Reed Kotlera562b462014-10-13 21:46:41 +00002069 return selectCmp(I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00002070 case Instruction::Select:
2071 return selectSelect(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002072 }
2073 return false;
2074}
Reed Kotler720c5ca2014-04-17 22:15:34 +00002075
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002076unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
2077 bool IsUnsigned) {
2078 unsigned VReg = getRegForValue(V);
2079 if (VReg == 0)
Reed Kotler12f94882014-10-10 17:00:46 +00002080 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00002081 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
Petar Jovanovicf10e4792018-07-17 14:57:46 +00002082
2083 if (VMVT == MVT::i1)
2084 return 0;
2085
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002086 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
2087 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
2088 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
2089 return 0;
2090 VReg = TempReg;
Reed Kotler063d4fb2014-06-10 16:45:44 +00002091 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002092 return VReg;
Reed Kotlerbab3f232014-05-01 20:39:21 +00002093}
2094
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002095void MipsFastISel::simplifyAddress(Address &Addr) {
2096 if (!isInt<16>(Addr.getOffset())) {
2097 unsigned TempReg =
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002098 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002099 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
2100 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
2101 Addr.setReg(DestReg);
2102 Addr.setOffset(0);
2103 }
2104}
2105
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +00002106unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2107 const TargetRegisterClass *RC,
2108 unsigned Op0, bool Op0IsKill,
2109 unsigned Op1, bool Op1IsKill) {
2110 // We treat the MUL instruction in a special way because it clobbers
2111 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2112 // mark these registers only as implicitly defined. As a result, the
2113 // register allocator runs out of registers when this instruction is
2114 // followed by another instruction that defines the same registers too.
2115 // We can fix this by explicitly marking those registers as dead.
2116 if (MachineInstOpcode == Mips::MUL) {
2117 unsigned ResultReg = createResultReg(RC);
2118 const MCInstrDesc &II = TII.get(MachineInstOpcode);
2119 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
2120 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
2121 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
2122 .addReg(Op0, getKillRegState(Op0IsKill))
2123 .addReg(Op1, getKillRegState(Op1IsKill))
2124 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
2125 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
2126 return ResultReg;
2127 }
2128
2129 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
2130 Op1IsKill);
2131}
2132
Reed Kotler720c5ca2014-04-17 22:15:34 +00002133namespace llvm {
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002134
Reed Kotler720c5ca2014-04-17 22:15:34 +00002135FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
2136 const TargetLibraryInfo *libInfo) {
2137 return new MipsFastISel(funcInfo, libInfo);
2138}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002139
2140} // end namespace llvm