blob: b83f44a74d5be16ea694f0530f69f0c02925c7cd [file] [log] [blame]
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001//===-- MipsFastISel.cpp - Mips FastISel implementation -------------------===//
Vasileios Kalintirisa9e51542016-06-08 13:13:15 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file defines the MIPS-specific support for the FastISel class.
12/// Some of the target-specific code is generated by tablegen in the file
13/// MipsGenFastISel.inc, which is #included here.
14///
15//===----------------------------------------------------------------------===//
Reed Kotler720c5ca2014-04-17 22:15:34 +000016
Eugene Zelenkodde94e42017-01-30 23:21:32 +000017#include "MCTargetDesc/MipsABIInfo.h"
18#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "MipsCCState.h"
Reed Kotler5fb7d8b2015-02-24 02:36:45 +000020#include "MipsInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "MipsISelLowering.h"
22#include "MipsMachineFunction.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000023#include "MipsSubtarget.h"
24#include "MipsTargetMachine.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000025#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/SmallVector.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000029#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000030#include "llvm/CodeGen/CallingConvLower.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000031#include "llvm/CodeGen/FastISel.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000032#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000033#include "llvm/CodeGen/ISDOpcodes.h"
34#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
Reed Kotler67077b32014-04-29 17:57:50 +000036#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000037#include "llvm/CodeGen/MachineMemOperand.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000039#include "llvm/CodeGen/MachineValueType.h"
40#include "llvm/CodeGen/ValueTypes.h"
41#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"
58#include "llvm/MC/MCInstrDesc.h"
59#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000060#include "llvm/MC/MCSymbol.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000061#include "llvm/Support/Casting.h"
62#include "llvm/Support/Compiler.h"
Daniel Sanderscbaca422016-07-29 12:27:28 +000063#include "llvm/Support/Debug.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000064#include "llvm/Support/ErrorHandling.h"
65#include "llvm/Support/MathExtras.h"
66#include "llvm/Support/raw_ostream.h"
67#include "llvm/Target/TargetInstrInfo.h"
68#include "llvm/Target/TargetLowering.h"
69#include <algorithm>
70#include <cassert>
71#include <cstdint>
72#include <new>
Daniel Sanderscbaca422016-07-29 12:27:28 +000073
74#define DEBUG_TYPE "mips-fastisel"
Reed Kotler720c5ca2014-04-17 22:15:34 +000075
76using namespace llvm;
77
78namespace {
79
80class MipsFastISel final : public FastISel {
81
Reed Kotlera562b462014-10-13 21:46:41 +000082 // All possible address modes.
83 class Address {
84 public:
85 typedef enum { RegBase, FrameIndexBase } BaseKind;
86
87 private:
Eugene Zelenkodde94e42017-01-30 23:21:32 +000088 BaseKind Kind = RegBase;
Reed Kotlera562b462014-10-13 21:46:41 +000089 union {
90 unsigned Reg;
91 int FI;
92 } Base;
93
Eugene Zelenkodde94e42017-01-30 23:21:32 +000094 int64_t Offset = 0;
Reed Kotlera562b462014-10-13 21:46:41 +000095
Eugene Zelenkodde94e42017-01-30 23:21:32 +000096 const GlobalValue *GV = nullptr;
Reed Kotlera562b462014-10-13 21:46:41 +000097
98 public:
99 // Innocuous defaults for our address.
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000100 Address() { Base.Reg = 0; }
101
Reed Kotlera562b462014-10-13 21:46:41 +0000102 void setKind(BaseKind K) { Kind = K; }
103 BaseKind getKind() const { return Kind; }
104 bool isRegBase() const { return Kind == RegBase; }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000105 bool isFIBase() const { return Kind == FrameIndexBase; }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000106
Reed Kotlera562b462014-10-13 21:46:41 +0000107 void setReg(unsigned Reg) {
108 assert(isRegBase() && "Invalid base register access!");
109 Base.Reg = Reg;
110 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000111
Reed Kotlera562b462014-10-13 21:46:41 +0000112 unsigned getReg() const {
113 assert(isRegBase() && "Invalid base register access!");
114 return Base.Reg;
115 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000116
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000117 void setFI(unsigned FI) {
118 assert(isFIBase() && "Invalid base frame index access!");
119 Base.FI = FI;
120 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000121
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000122 unsigned getFI() const {
123 assert(isFIBase() && "Invalid base frame index access!");
124 return Base.FI;
125 }
126
Reed Kotlera562b462014-10-13 21:46:41 +0000127 void setOffset(int64_t Offset_) { Offset = Offset_; }
128 int64_t getOffset() const { return Offset; }
129 void setGlobalValue(const GlobalValue *G) { GV = G; }
130 const GlobalValue *getGlobalValue() { return GV; }
131 };
132
Reed Kotler67077b32014-04-29 17:57:50 +0000133 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
134 /// make the right decision when generating code for different targets.
Reed Kotler67077b32014-04-29 17:57:50 +0000135 const TargetMachine &TM;
Eric Christopher96e72c62015-01-29 23:27:36 +0000136 const MipsSubtarget *Subtarget;
Reed Kotler67077b32014-04-29 17:57:50 +0000137 const TargetInstrInfo &TII;
138 const TargetLowering &TLI;
139 MipsFunctionInfo *MFI;
140
141 // Convenience variables to avoid some queries.
142 LLVMContext *Context;
143
Daniel Sanderscbaca422016-07-29 12:27:28 +0000144 bool fastLowerArguments() override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000145 bool fastLowerCall(CallLoweringInfo &CLI) override;
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000146 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000147
Reed Kotlera562b462014-10-13 21:46:41 +0000148 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
149 // floating point but not reject doing fast-isel in other
150 // situations
151
152private:
153 // Selection routines.
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000154 bool selectLogicalOp(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000155 bool selectLoad(const Instruction *I);
156 bool selectStore(const Instruction *I);
157 bool selectBranch(const Instruction *I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000158 bool selectSelect(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000159 bool selectCmp(const Instruction *I);
160 bool selectFPExt(const Instruction *I);
161 bool selectFPTrunc(const Instruction *I);
162 bool selectFPToInt(const Instruction *I, bool IsSigned);
163 bool selectRet(const Instruction *I);
164 bool selectTrunc(const Instruction *I);
165 bool selectIntExt(const Instruction *I);
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +0000166 bool selectShift(const Instruction *I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +0000167 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
Reed Kotlera562b462014-10-13 21:46:41 +0000168
169 // Utility helper routines.
Reed Kotlera562b462014-10-13 21:46:41 +0000170 bool isTypeLegal(Type *Ty, MVT &VT);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000171 bool isTypeSupported(Type *Ty, MVT &VT);
Reed Kotlera562b462014-10-13 21:46:41 +0000172 bool isLoadTypeLegal(Type *Ty, MVT &VT);
173 bool computeAddress(const Value *Obj, Address &Addr);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000174 bool computeCallAddress(const Value *V, Address &Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000175 void simplifyAddress(Address &Addr);
Reed Kotlera562b462014-10-13 21:46:41 +0000176
177 // Emit helper routines.
178 bool emitCmp(unsigned DestReg, const CmpInst *CI);
179 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
180 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000181 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
182 MachineMemOperand *MMO = nullptr);
Reed Kotlera562b462014-10-13 21:46:41 +0000183 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
184 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000185 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Reed Kotlera562b462014-10-13 21:46:41 +0000186 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
187
188 bool IsZExt);
189 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
190
191 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
192 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
193 unsigned DestReg);
194 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
195 unsigned DestReg);
196
197 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
198
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000199 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
200 const Value *RHS);
201
Reed Kotlera562b462014-10-13 21:46:41 +0000202 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
203 unsigned materializeGV(const GlobalValue *GV, MVT VT);
204 unsigned materializeInt(const Constant *C, MVT VT);
205 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000206 unsigned materializeExternalCallSym(MCSymbol *Syn);
Reed Kotlera562b462014-10-13 21:46:41 +0000207
208 MachineInstrBuilder emitInst(unsigned Opc) {
209 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
210 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000211
Reed Kotlera562b462014-10-13 21:46:41 +0000212 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
213 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
214 DstReg);
215 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000216
Reed Kotlera562b462014-10-13 21:46:41 +0000217 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
218 unsigned MemReg, int64_t MemOffset) {
219 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
220 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000221
Reed Kotlera562b462014-10-13 21:46:41 +0000222 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
223 unsigned MemReg, int64_t MemOffset) {
224 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
225 }
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +0000226
227 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
228 const TargetRegisterClass *RC,
229 unsigned Op0, bool Op0IsKill,
230 unsigned Op1, bool Op1IsKill);
231
Reed Kotlera562b462014-10-13 21:46:41 +0000232 // for some reason, this default is not generated by tablegen
233 // so we explicitly generate it here.
234 //
235 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
236 unsigned Op0, bool Op0IsKill, uint64_t imm1,
237 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
238 return 0;
239 }
Reed Kotler67077b32014-04-29 17:57:50 +0000240
Reed Kotlerd5c41962014-11-13 23:37:45 +0000241 // Call handling routines.
242private:
243 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
244 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
245 unsigned &NumBytes);
246 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000247
Daniel Sanderscbaca422016-07-29 12:27:28 +0000248 const MipsABIInfo &getABI() const {
249 return static_cast<const MipsTargetMachine &>(TM).getABI();
250 }
Reed Kotlerd5c41962014-11-13 23:37:45 +0000251
Reed Kotler720c5ca2014-04-17 22:15:34 +0000252public:
Reed Kotlera562b462014-10-13 21:46:41 +0000253 // Backend specific FastISel code.
Reed Kotler720c5ca2014-04-17 22:15:34 +0000254 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
255 const TargetLibraryInfo *libInfo)
Eric Christopher3ab98892014-12-20 00:07:09 +0000256 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
Eric Christopherb2a5fa92015-02-14 00:09:46 +0000257 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
Eric Christopher96e72c62015-01-29 23:27:36 +0000258 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
Reed Kotler67077b32014-04-29 17:57:50 +0000259 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
260 Context = &funcInfo.Fn->getContext();
Simon Dardis86b3a1e2016-10-04 10:35:07 +0000261 UnsupportedFPMode = Subtarget->isFP64bit() || Subtarget->useSoftFloat();
Reed Kotler67077b32014-04-29 17:57:50 +0000262 }
263
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000264 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000265 unsigned fastMaterializeConstant(const Constant *C) override;
Reed Kotlera562b462014-10-13 21:46:41 +0000266 bool fastSelectInstruction(const Instruction *I) override;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000267
Reed Kotler9fe25f32014-06-08 02:08:43 +0000268#include "MipsGenFastISel.inc"
Reed Kotler720c5ca2014-04-17 22:15:34 +0000269};
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000270
271} // end anonymous namespace
Reed Kotler67077b32014-04-29 17:57:50 +0000272
Reed Kotlerd5c41962014-11-13 23:37:45 +0000273static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
274 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Reid Klecknerd3781742014-11-14 00:39:33 +0000275 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000276
277static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
278 CCValAssign::LocInfo LocInfo,
279 ISD::ArgFlagsTy ArgFlags, CCState &State) {
280 llvm_unreachable("should not be called");
281}
282
Benjamin Kramer970eac42015-02-06 17:51:54 +0000283static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
284 CCValAssign::LocInfo LocInfo,
285 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Reed Kotlerd5c41962014-11-13 23:37:45 +0000286 llvm_unreachable("should not be called");
287}
288
289#include "MipsGenCallingConv.inc"
290
291CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
292 return CC_MipsO32;
293}
294
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000295unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
296 const Value *LHS, const Value *RHS) {
297 // Canonicalize immediates to the RHS first.
298 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
299 std::swap(LHS, RHS);
300
301 unsigned Opc;
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000302 switch (ISDOpc) {
Vasileios Kalintiris2a95f822015-10-12 15:39:41 +0000303 case ISD::AND:
304 Opc = Mips::AND;
305 break;
306 case ISD::OR:
307 Opc = Mips::OR;
308 break;
309 case ISD::XOR:
310 Opc = Mips::XOR;
311 break;
312 default:
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000313 llvm_unreachable("unexpected opcode");
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000314 }
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000315
316 unsigned LHSReg = getRegForValue(LHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000317 if (!LHSReg)
318 return 0;
319
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000320 unsigned RHSReg;
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000321 if (const auto *C = dyn_cast<ConstantInt>(RHS))
322 RHSReg = materializeInt(C, MVT::i32);
323 else
324 RHSReg = getRegForValue(RHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000325 if (!RHSReg)
326 return 0;
327
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000328 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
329 if (!ResultReg)
330 return 0;
331
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000332 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
333 return ResultReg;
334}
335
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000336unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000337 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 &&
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000338 "Alloca should always return a pointer.");
339
340 DenseMap<const AllocaInst *, int>::iterator SI =
341 FuncInfo.StaticAllocaMap.find(AI);
342
343 if (SI != FuncInfo.StaticAllocaMap.end()) {
344 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
345 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
346 ResultReg)
347 .addFrameIndex(SI->second)
348 .addImm(0);
349 return ResultReg;
350 }
351
352 return 0;
353}
354
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000355unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
356 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Reed Kotler497311a2014-10-10 17:39:51 +0000357 return 0;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000358 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
359 const ConstantInt *CI = cast<ConstantInt>(C);
Vasileios Kalintiris77fb0a32015-07-30 11:51:44 +0000360 return materialize32BitInt(CI->getZExtValue(), RC);
Reed Kotler497311a2014-10-10 17:39:51 +0000361}
362
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000363unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
364 const TargetRegisterClass *RC) {
365 unsigned ResultReg = createResultReg(RC);
366
367 if (isInt<16>(Imm)) {
368 unsigned Opc = Mips::ADDiu;
369 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
370 return ResultReg;
371 } else if (isUInt<16>(Imm)) {
372 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
373 return ResultReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000374 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000375 unsigned Lo = Imm & 0xFFFF;
376 unsigned Hi = (Imm >> 16) & 0xFFFF;
377 if (Lo) {
378 // Both Lo and Hi have nonzero bits.
379 unsigned TmpReg = createResultReg(RC);
380 emitInst(Mips::LUi, TmpReg).addImm(Hi);
381 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
382 } else {
383 emitInst(Mips::LUi, ResultReg).addImm(Hi);
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000384 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000385 return ResultReg;
386}
387
388unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
389 if (UnsupportedFPMode)
390 return 0;
391 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
392 if (VT == MVT::f32) {
393 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
394 unsigned DestReg = createResultReg(RC);
395 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
396 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
397 return DestReg;
398 } else if (VT == MVT::f64) {
399 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
400 unsigned DestReg = createResultReg(RC);
401 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
402 unsigned TempReg2 =
403 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
404 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
405 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000406 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000407 return 0;
408}
409
410unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
411 // For now 32-bit only.
412 if (VT != MVT::i32)
413 return 0;
414 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
415 unsigned DestReg = createResultReg(RC);
416 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
417 bool IsThreadLocal = GVar && GVar->isThreadLocal();
418 // TLS not supported at this time.
419 if (IsThreadLocal)
420 return 0;
421 emitInst(Mips::LW, DestReg)
422 .addReg(MFI->getGlobalBaseReg())
423 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
424 if ((GV->hasInternalLinkage() ||
425 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
426 unsigned TempReg = createResultReg(RC);
427 emitInst(Mips::ADDiu, TempReg)
428 .addReg(DestReg)
429 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
430 DestReg = TempReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000431 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000432 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000433}
434
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000435unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000436 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
437 unsigned DestReg = createResultReg(RC);
438 emitInst(Mips::LW, DestReg)
439 .addReg(MFI->getGlobalBaseReg())
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000440 .addSym(Sym, MipsII::MO_GOT);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000441 return DestReg;
442}
443
Reed Kotlerbab3f232014-05-01 20:39:21 +0000444// Materialize a constant into a register, and return the register
445// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000446unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000447 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000448
449 // Only handle simple types.
450 if (!CEVT.isSimple())
451 return 0;
452 MVT VT = CEVT.getSimpleVT();
453
454 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000455 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000456 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000457 return materializeGV(GV, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000458 else if (isa<ConstantInt>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000459 return materializeInt(C, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000460
461 return 0;
462}
463
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000464bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000465 const User *U = nullptr;
466 unsigned Opcode = Instruction::UserOp1;
467 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
468 // Don't walk into other basic blocks unless the object is an alloca from
469 // another block, otherwise it may not have a virtual register assigned.
470 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
471 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
472 Opcode = I->getOpcode();
473 U = I;
474 }
Vasileios Kalintiris32cd69a2015-05-12 12:08:31 +0000475 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
476 Opcode = C->getOpcode();
477 U = C;
478 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000479 switch (Opcode) {
480 default:
481 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000482 case Instruction::BitCast:
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000483 // Look through bitcasts.
484 return computeAddress(U->getOperand(0), Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000485 case Instruction::GetElementPtr: {
486 Address SavedAddr = Addr;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000487 int64_t TmpOffset = Addr.getOffset();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000488 // Iterate through the GEP folding the constants into offsets where
489 // we can.
490 gep_type_iterator GTI = gep_type_begin(U);
491 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
492 ++i, ++GTI) {
493 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000494 if (StructType *STy = GTI.getStructTypeOrNull()) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000495 const StructLayout *SL = DL.getStructLayout(STy);
496 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
497 TmpOffset += SL->getElementOffset(Idx);
498 } else {
499 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000500 while (true) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000501 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
502 // Constant-offset addressing.
503 TmpOffset += CI->getSExtValue() * S;
504 break;
505 }
506 if (canFoldAddIntoGEP(U, Op)) {
507 // A compatible add with a constant operand. Fold the constant.
508 ConstantInt *CI =
509 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
510 TmpOffset += CI->getSExtValue() * S;
511 // Iterate on the other operand.
512 Op = cast<AddOperator>(Op)->getOperand(0);
513 continue;
514 }
515 // Unsupported
516 goto unsupported_gep;
517 }
518 }
519 }
520 // Try to grab the base operand now.
521 Addr.setOffset(TmpOffset);
522 if (computeAddress(U->getOperand(0), Addr))
523 return true;
524 // We failed, restore everything and try the other options.
525 Addr = SavedAddr;
526 unsupported_gep:
527 break;
528 }
529 case Instruction::Alloca: {
530 const AllocaInst *AI = cast<AllocaInst>(Obj);
531 DenseMap<const AllocaInst *, int>::iterator SI =
532 FuncInfo.StaticAllocaMap.find(AI);
533 if (SI != FuncInfo.StaticAllocaMap.end()) {
534 Addr.setKind(Address::FrameIndexBase);
535 Addr.setFI(SI->second);
536 return true;
537 }
538 break;
539 }
540 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000541 Addr.setReg(getRegForValue(Obj));
542 return Addr.getReg() != 0;
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000543}
544
Reed Kotlerd5c41962014-11-13 23:37:45 +0000545bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000546 const User *U = nullptr;
547 unsigned Opcode = Instruction::UserOp1;
548
549 if (const auto *I = dyn_cast<Instruction>(V)) {
550 // Check if the value is defined in the same basic block. This information
551 // is crucial to know whether or not folding an operand is valid.
552 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
553 Opcode = I->getOpcode();
554 U = I;
555 }
556 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
557 Opcode = C->getOpcode();
558 U = C;
559 }
560
561 switch (Opcode) {
562 default:
563 break;
564 case Instruction::BitCast:
565 // Look past bitcasts if its operand is in the same BB.
566 return computeCallAddress(U->getOperand(0), Addr);
567 break;
568 case Instruction::IntToPtr:
569 // Look past no-op inttoptrs if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000570 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
571 TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000572 return computeCallAddress(U->getOperand(0), Addr);
573 break;
574 case Instruction::PtrToInt:
575 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000576 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000577 return computeCallAddress(U->getOperand(0), Addr);
578 break;
579 }
580
Reed Kotlerd5c41962014-11-13 23:37:45 +0000581 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
582 Addr.setGlobalValue(GV);
583 return true;
584 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000585
586 // If all else fails, try to materialize the value in a register.
587 if (!Addr.getGlobalValue()) {
588 Addr.setReg(getRegForValue(V));
589 return Addr.getReg() != 0;
590 }
591
Reed Kotlerd5c41962014-11-13 23:37:45 +0000592 return false;
593}
594
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000595bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000596 EVT evt = TLI.getValueType(DL, Ty, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000597 // Only handle simple types.
598 if (evt == MVT::Other || !evt.isSimple())
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000599 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000600 VT = evt.getSimpleVT();
601
602 // Handle all legal types, i.e. a register that will directly hold this
603 // value.
604 return TLI.isTypeLegal(VT);
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000605}
606
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000607bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
608 if (Ty->isVectorTy())
609 return false;
610
611 if (isTypeLegal(Ty, VT))
612 return true;
613
614 // If this is a type than can be sign or zero-extended to a basic operation
615 // go ahead and accept it now.
616 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
617 return true;
618
619 return false;
620}
621
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000622bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
623 if (isTypeLegal(Ty, VT))
Reed Kotler62de6b92014-10-11 00:55:18 +0000624 return true;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000625 // We will extend this in a later patch:
626 // If this is a type than can be sign or zero-extended to a basic operation
627 // go ahead and accept it now.
628 if (VT == MVT::i8 || VT == MVT::i16)
629 return true;
Reed Kotler62de6b92014-10-11 00:55:18 +0000630 return false;
631}
Reed Kotler62de6b92014-10-11 00:55:18 +0000632// Because of how EmitCmp is called with fast-isel, you can
Reed Kotler497311a2014-10-10 17:39:51 +0000633// end up with redundant "andi" instructions after the sequences emitted below.
634// We should try and solve this issue in the future.
635//
Reed Kotlera562b462014-10-13 21:46:41 +0000636bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000637 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000638 bool IsUnsigned = CI->isUnsigned();
Reed Kotler497311a2014-10-10 17:39:51 +0000639 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
640 if (LeftReg == 0)
641 return false;
642 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
643 if (RightReg == 0)
644 return false;
Reed Kotler1f64eca2014-10-10 20:46:28 +0000645 CmpInst::Predicate P = CI->getPredicate();
Reed Kotler62de6b92014-10-11 00:55:18 +0000646
Reed Kotler1f64eca2014-10-10 20:46:28 +0000647 switch (P) {
Reed Kotler497311a2014-10-10 17:39:51 +0000648 default:
649 return false;
650 case CmpInst::ICMP_EQ: {
651 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000652 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
653 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000654 break;
655 }
656 case CmpInst::ICMP_NE: {
657 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000658 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
659 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000660 break;
661 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000662 case CmpInst::ICMP_UGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000663 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000664 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000665 case CmpInst::ICMP_ULT:
Reed Kotlera562b462014-10-13 21:46:41 +0000666 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000667 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000668 case CmpInst::ICMP_UGE: {
669 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000670 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
671 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000672 break;
673 }
674 case CmpInst::ICMP_ULE: {
675 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000676 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
677 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000678 break;
679 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000680 case CmpInst::ICMP_SGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000681 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000682 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000683 case CmpInst::ICMP_SLT:
Reed Kotlera562b462014-10-13 21:46:41 +0000684 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000685 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000686 case CmpInst::ICMP_SGE: {
687 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000688 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
689 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000690 break;
691 }
692 case CmpInst::ICMP_SLE: {
693 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000694 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
695 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000696 break;
697 }
Reed Kotler1f64eca2014-10-10 20:46:28 +0000698 case CmpInst::FCMP_OEQ:
699 case CmpInst::FCMP_UNE:
700 case CmpInst::FCMP_OLT:
701 case CmpInst::FCMP_OLE:
702 case CmpInst::FCMP_OGT:
703 case CmpInst::FCMP_OGE: {
704 if (UnsupportedFPMode)
705 return false;
706 bool IsFloat = Left->getType()->isFloatTy();
707 bool IsDouble = Left->getType()->isDoubleTy();
708 if (!IsFloat && !IsDouble)
709 return false;
710 unsigned Opc, CondMovOpc;
711 switch (P) {
712 case CmpInst::FCMP_OEQ:
713 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
714 CondMovOpc = Mips::MOVT_I;
715 break;
716 case CmpInst::FCMP_UNE:
717 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
718 CondMovOpc = Mips::MOVF_I;
719 break;
720 case CmpInst::FCMP_OLT:
721 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
722 CondMovOpc = Mips::MOVT_I;
723 break;
724 case CmpInst::FCMP_OLE:
725 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
726 CondMovOpc = Mips::MOVT_I;
727 break;
728 case CmpInst::FCMP_OGT:
729 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
730 CondMovOpc = Mips::MOVF_I;
731 break;
732 case CmpInst::FCMP_OGE:
733 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
734 CondMovOpc = Mips::MOVF_I;
735 break;
736 default:
Chandler Carruth38811cc2014-10-10 21:07:03 +0000737 llvm_unreachable("Only switching of a subset of CCs.");
Reed Kotler1f64eca2014-10-10 20:46:28 +0000738 }
739 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
740 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000741 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
742 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
Simon Dardis730fdb72017-01-16 13:55:58 +0000743 emitInst(Opc).addReg(Mips::FCC0, RegState::Define).addReg(LeftReg)
744 .addReg(RightReg);
Daniel Sandersa6cda122016-05-06 12:57:26 +0000745 emitInst(CondMovOpc, ResultReg)
746 .addReg(RegWithOne)
747 .addReg(Mips::FCC0)
748 .addReg(RegWithZero);
Reed Kotler1f64eca2014-10-10 20:46:28 +0000749 break;
750 }
Reed Kotler497311a2014-10-10 17:39:51 +0000751 }
Reed Kotler62de6b92014-10-11 00:55:18 +0000752 return true;
753}
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000754
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000755bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
756 unsigned Alignment) {
757 //
758 // more cases will be handled here in following patches.
759 //
760 unsigned Opc;
761 switch (VT.SimpleTy) {
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000762 case MVT::i32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000763 ResultReg = createResultReg(&Mips::GPR32RegClass);
764 Opc = Mips::LW;
765 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000766 case MVT::i16:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000767 ResultReg = createResultReg(&Mips::GPR32RegClass);
768 Opc = Mips::LHu;
769 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000770 case MVT::i8:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000771 ResultReg = createResultReg(&Mips::GPR32RegClass);
772 Opc = Mips::LBu;
773 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000774 case MVT::f32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000775 if (UnsupportedFPMode)
776 return false;
777 ResultReg = createResultReg(&Mips::FGR32RegClass);
778 Opc = Mips::LWC1;
779 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000780 case MVT::f64:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000781 if (UnsupportedFPMode)
782 return false;
783 ResultReg = createResultReg(&Mips::AFGR64RegClass);
784 Opc = Mips::LDC1;
785 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000786 default:
787 return false;
788 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000789 if (Addr.isRegBase()) {
790 simplifyAddress(Addr);
791 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
792 return true;
793 }
794 if (Addr.isFIBase()) {
795 unsigned FI = Addr.getFI();
796 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000797 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000798 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000799 MachineMemOperand *MMO = MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +0000800 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000801 MFI.getObjectSize(FI), Align);
802 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
803 .addFrameIndex(FI)
804 .addImm(Offset)
805 .addMemOperand(MMO);
806 return true;
807 }
808 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000809}
810
811bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
812 unsigned Alignment) {
813 //
814 // more cases will be handled here in following patches.
815 //
816 unsigned Opc;
817 switch (VT.SimpleTy) {
818 case MVT::i8:
819 Opc = Mips::SB;
820 break;
821 case MVT::i16:
822 Opc = Mips::SH;
823 break;
824 case MVT::i32:
825 Opc = Mips::SW;
826 break;
827 case MVT::f32:
828 if (UnsupportedFPMode)
829 return false;
830 Opc = Mips::SWC1;
831 break;
832 case MVT::f64:
833 if (UnsupportedFPMode)
834 return false;
835 Opc = Mips::SDC1;
836 break;
837 default:
838 return false;
839 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000840 if (Addr.isRegBase()) {
841 simplifyAddress(Addr);
842 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
843 return true;
844 }
845 if (Addr.isFIBase()) {
846 unsigned FI = Addr.getFI();
847 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000848 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000849 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000850 MachineMemOperand *MMO = MF->getMachineMemOperand(
Simon Dardisd8bceb92016-04-29 16:07:47 +0000851 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000852 MFI.getObjectSize(FI), Align);
853 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
854 .addReg(SrcReg)
855 .addFrameIndex(FI)
856 .addImm(Offset)
857 .addMemOperand(MMO);
858 return true;
859 }
860 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000861}
862
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000863bool MipsFastISel::selectLogicalOp(const Instruction *I) {
864 MVT VT;
865 if (!isTypeSupported(I->getType(), VT))
866 return false;
867
868 unsigned ResultReg;
869 switch (I->getOpcode()) {
870 default:
871 llvm_unreachable("Unexpected instruction.");
872 case Instruction::And:
873 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
874 break;
875 case Instruction::Or:
876 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
877 break;
878 case Instruction::Xor:
879 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
880 break;
881 }
882
883 if (!ResultReg)
884 return false;
885
886 updateValueMap(I, ResultReg);
887 return true;
888}
889
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000890bool MipsFastISel::selectLoad(const Instruction *I) {
891 // Atomic loads need special handling.
892 if (cast<LoadInst>(I)->isAtomic())
893 return false;
894
895 // Verify we have a legal type before going any further.
896 MVT VT;
897 if (!isLoadTypeLegal(I->getType(), VT))
898 return false;
899
900 // See if we can handle this address.
901 Address Addr;
902 if (!computeAddress(I->getOperand(0), Addr))
903 return false;
904
905 unsigned ResultReg;
906 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
907 return false;
908 updateValueMap(I, ResultReg);
909 return true;
910}
911
912bool MipsFastISel::selectStore(const Instruction *I) {
913 Value *Op0 = I->getOperand(0);
914 unsigned SrcReg = 0;
915
916 // Atomic stores need special handling.
917 if (cast<StoreInst>(I)->isAtomic())
918 return false;
919
920 // Verify we have a legal type before going any further.
921 MVT VT;
922 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
923 return false;
924
925 // Get the value to be stored into a register.
926 SrcReg = getRegForValue(Op0);
927 if (SrcReg == 0)
928 return false;
929
930 // See if we can handle this address.
931 Address Addr;
932 if (!computeAddress(I->getOperand(1), Addr))
933 return false;
934
935 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
936 return false;
937 return true;
938}
939
940//
941// This can cause a redundant sltiu to be generated.
942// FIXME: try and eliminate this in a future patch.
943//
944bool 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)];
956 BI->getCondition();
957 // For now, just try the simplest case where it's fed by a compare.
958 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
959 unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
960 if (!emitCmp(CondReg, CI))
961 return false;
962 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
963 .addReg(CondReg)
964 .addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000965 finishCondBranch(BI->getParent(), TBB, FBB);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000966 return true;
967 }
968 return false;
969}
Reed Kotler62de6b92014-10-11 00:55:18 +0000970
Reed Kotlera562b462014-10-13 21:46:41 +0000971bool MipsFastISel::selectCmp(const Instruction *I) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000972 const CmpInst *CI = cast<CmpInst>(I);
973 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000974 if (!emitCmp(ResultReg, CI))
Reed Kotler62de6b92014-10-11 00:55:18 +0000975 return false;
Reed Kotler497311a2014-10-10 17:39:51 +0000976 updateValueMap(I, ResultReg);
977 return true;
978}
979
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000980// Attempt to fast-select a floating-point extend instruction.
981bool MipsFastISel::selectFPExt(const Instruction *I) {
982 if (UnsupportedFPMode)
983 return false;
984 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000985 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
986 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000987
988 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
989 return false;
990
991 unsigned SrcReg =
Nico Weber2cf5e892016-06-10 20:06:03 +0000992 getRegForValue(Src); // this must be a 32bit floating point register class
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000993 // maybe we should handle this differently
994 if (!SrcReg)
995 return false;
996
997 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
998 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
999 updateValueMap(I, DestReg);
1000 return true;
1001}
1002
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001003bool MipsFastISel::selectSelect(const Instruction *I) {
1004 assert(isa<SelectInst>(I) && "Expected a select instruction.");
1005
Simon Dardisb432a3e2016-09-06 12:36:24 +00001006 DEBUG(dbgs() << "selectSelect\n");
1007
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001008 MVT VT;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001009 if (!isTypeSupported(I->getType(), VT) || UnsupportedFPMode) {
1010 DEBUG(dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001011 return false;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001012 }
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001013
1014 unsigned CondMovOpc;
1015 const TargetRegisterClass *RC;
1016
1017 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
1018 CondMovOpc = Mips::MOVN_I_I;
1019 RC = &Mips::GPR32RegClass;
1020 } else if (VT == MVT::f32) {
1021 CondMovOpc = Mips::MOVN_I_S;
1022 RC = &Mips::FGR32RegClass;
1023 } else if (VT == MVT::f64) {
1024 CondMovOpc = Mips::MOVN_I_D32;
1025 RC = &Mips::AFGR64RegClass;
1026 } else
1027 return false;
1028
1029 const SelectInst *SI = cast<SelectInst>(I);
1030 const Value *Cond = SI->getCondition();
1031 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
1032 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
1033 unsigned CondReg = getRegForValue(Cond);
1034
1035 if (!Src1Reg || !Src2Reg || !CondReg)
1036 return false;
1037
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001038 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
1039 if (!ZExtCondReg)
1040 return false;
1041
1042 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true))
1043 return false;
1044
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001045 unsigned ResultReg = createResultReg(RC);
1046 unsigned TempReg = createResultReg(RC);
1047
1048 if (!ResultReg || !TempReg)
1049 return false;
1050
1051 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
1052 emitInst(CondMovOpc, ResultReg)
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001053 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001054 updateValueMap(I, ResultReg);
1055 return true;
1056}
1057
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001058// Attempt to fast-select a floating-point truncate instruction.
1059bool MipsFastISel::selectFPTrunc(const Instruction *I) {
1060 if (UnsupportedFPMode)
1061 return false;
1062 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001063 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1064 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001065
1066 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1067 return false;
1068
1069 unsigned SrcReg = getRegForValue(Src);
1070 if (!SrcReg)
1071 return false;
1072
1073 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1074 if (!DestReg)
1075 return false;
1076
1077 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1078 updateValueMap(I, DestReg);
1079 return true;
1080}
1081
1082// Attempt to fast-select a floating-point-to-integer conversion.
1083bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1084 if (UnsupportedFPMode)
1085 return false;
1086 MVT DstVT, SrcVT;
1087 if (!IsSigned)
1088 return false; // We don't handle this case yet. There is no native
1089 // instruction for this but it can be synthesized.
1090 Type *DstTy = I->getType();
1091 if (!isTypeLegal(DstTy, DstVT))
1092 return false;
1093
1094 if (DstVT != MVT::i32)
1095 return false;
1096
1097 Value *Src = I->getOperand(0);
1098 Type *SrcTy = Src->getType();
1099 if (!isTypeLegal(SrcTy, SrcVT))
1100 return false;
1101
1102 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1103 return false;
1104
1105 unsigned SrcReg = getRegForValue(Src);
1106 if (SrcReg == 0)
1107 return false;
1108
1109 // Determine the opcode for the conversion, which takes place
1110 // entirely within FPRs.
1111 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1112 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001113 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001114
1115 // Generate the convert.
1116 emitInst(Opc, TempReg).addReg(SrcReg);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001117 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1118
1119 updateValueMap(I, DestReg);
1120 return true;
1121}
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001122
Reed Kotlerd5c41962014-11-13 23:37:45 +00001123bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1124 SmallVectorImpl<MVT> &OutVTs,
1125 unsigned &NumBytes) {
1126 CallingConv::ID CC = CLI.CallConv;
1127 SmallVector<CCValAssign, 16> ArgLocs;
1128 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1129 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1130 // Get a count of how many bytes are to be pushed on the stack.
1131 NumBytes = CCInfo.getNextStackOffset();
1132 // This is the minimum argument area used for A0-A3.
1133 if (NumBytes < 16)
1134 NumBytes = 16;
1135
Serge Pavlovd526b132017-05-09 13:35:13 +00001136 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001137 // Process the args.
1138 MVT firstMVT;
1139 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1140 CCValAssign &VA = ArgLocs[i];
1141 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1142 MVT ArgVT = OutVTs[VA.getValNo()];
1143
1144 if (i == 0) {
1145 firstMVT = ArgVT;
1146 if (ArgVT == MVT::f32) {
1147 VA.convertToReg(Mips::F12);
1148 } else if (ArgVT == MVT::f64) {
1149 VA.convertToReg(Mips::D6);
1150 }
1151 } else if (i == 1) {
1152 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1153 if (ArgVT == MVT::f32) {
1154 VA.convertToReg(Mips::F14);
1155 } else if (ArgVT == MVT::f64) {
1156 VA.convertToReg(Mips::D7);
1157 }
1158 }
1159 }
Vasileios Kalintirisb48c9052015-05-12 12:29:17 +00001160 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1161 (ArgVT == MVT::i8)) &&
1162 VA.isMemLoc()) {
Reed Kotlerd5c41962014-11-13 23:37:45 +00001163 switch (VA.getLocMemOffset()) {
1164 case 0:
1165 VA.convertToReg(Mips::A0);
1166 break;
1167 case 4:
1168 VA.convertToReg(Mips::A1);
1169 break;
1170 case 8:
1171 VA.convertToReg(Mips::A2);
1172 break;
1173 case 12:
1174 VA.convertToReg(Mips::A3);
1175 break;
1176 default:
1177 break;
1178 }
1179 }
1180 unsigned ArgReg = getRegForValue(ArgVal);
1181 if (!ArgReg)
1182 return false;
1183
1184 // Handle arg promotion: SExt, ZExt, AExt.
1185 switch (VA.getLocInfo()) {
1186 case CCValAssign::Full:
1187 break;
1188 case CCValAssign::AExt:
1189 case CCValAssign::SExt: {
1190 MVT DestVT = VA.getLocVT();
1191 MVT SrcVT = ArgVT;
1192 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1193 if (!ArgReg)
1194 return false;
1195 break;
1196 }
1197 case CCValAssign::ZExt: {
1198 MVT DestVT = VA.getLocVT();
1199 MVT SrcVT = ArgVT;
1200 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1201 if (!ArgReg)
1202 return false;
1203 break;
1204 }
1205 default:
1206 llvm_unreachable("Unknown arg promotion!");
1207 }
1208
1209 // Now copy/store arg to correct locations.
1210 if (VA.isRegLoc() && !VA.needsCustom()) {
1211 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1212 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1213 CLI.OutRegs.push_back(VA.getLocReg());
1214 } else if (VA.needsCustom()) {
1215 llvm_unreachable("Mips does not use custom args.");
1216 return false;
1217 } else {
1218 //
1219 // FIXME: This path will currently return false. It was copied
1220 // from the AArch64 port and should be essentially fine for Mips too.
1221 // The work to finish up this path will be done in a follow-on patch.
1222 //
1223 assert(VA.isMemLoc() && "Assuming store on stack.");
1224 // Don't emit stores for undef values.
1225 if (isa<UndefValue>(ArgVal))
1226 continue;
1227
1228 // Need to store on the stack.
1229 // FIXME: This alignment is incorrect but this path is disabled
1230 // for now (will return false). We need to determine the right alignment
1231 // based on the normal alignment for the underlying machine type.
1232 //
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001233 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001234
1235 unsigned BEAlign = 0;
1236 if (ArgSize < 8 && !Subtarget->isLittle())
1237 BEAlign = 8 - ArgSize;
1238
1239 Address Addr;
1240 Addr.setKind(Address::RegBase);
1241 Addr.setReg(Mips::SP);
1242 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1243
1244 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1245 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00001246 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
Reed Kotlerd5c41962014-11-13 23:37:45 +00001247 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1248 (void)(MMO);
1249 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1250 return false; // can't store on the stack yet.
1251 }
1252 }
1253
1254 return true;
1255}
1256
1257bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1258 unsigned NumBytes) {
1259 CallingConv::ID CC = CLI.CallConv;
Daniel Sanders01bcefd2016-05-03 14:19:26 +00001260 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001261 if (RetVT != MVT::isVoid) {
1262 SmallVector<CCValAssign, 16> RVLocs;
Simon Dardis70f79252017-04-26 11:10:38 +00001263 MipsCCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1264
1265 CCInfo.AnalyzeCallResult(CLI.Ins, RetCC_Mips, CLI.RetTy,
Simon Dardis9d580e82017-04-29 16:31:40 +00001266 CLI.Symbol ? CLI.Symbol->getName().data()
1267 : nullptr);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001268
1269 // Only handle a single return value.
1270 if (RVLocs.size() != 1)
1271 return false;
1272 // Copy all of the result registers out of their specified physreg.
1273 MVT CopyVT = RVLocs[0].getValVT();
1274 // Special handling for extended integers.
1275 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1276 CopyVT = MVT::i32;
1277
1278 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001279 if (!ResultReg)
1280 return false;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1282 TII.get(TargetOpcode::COPY),
1283 ResultReg).addReg(RVLocs[0].getLocReg());
1284 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1285
1286 CLI.ResultReg = ResultReg;
1287 CLI.NumResultRegs = 1;
1288 }
1289 return true;
1290}
1291
Daniel Sanderscbaca422016-07-29 12:27:28 +00001292bool MipsFastISel::fastLowerArguments() {
1293 DEBUG(dbgs() << "fastLowerArguments\n");
1294
1295 if (!FuncInfo.CanLowerReturn) {
1296 DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1297 return false;
1298 }
1299
1300 const Function *F = FuncInfo.Fn;
1301 if (F->isVarArg()) {
1302 DEBUG(dbgs() << ".. gave up (varargs)\n");
1303 return false;
1304 }
1305
1306 CallingConv::ID CC = F->getCallingConv();
1307 if (CC != CallingConv::C) {
1308 DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1309 return false;
1310 }
1311
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001312 const ArrayRef<MCPhysReg> GPR32ArgRegs = {Mips::A0, Mips::A1, Mips::A2,
1313 Mips::A3};
1314 const ArrayRef<MCPhysReg> FGR32ArgRegs = {Mips::F12, Mips::F14};
1315 const ArrayRef<MCPhysReg> AFGR64ArgRegs = {Mips::D6, Mips::D7};
1316 ArrayRef<MCPhysReg>::iterator NextGPR32 = GPR32ArgRegs.begin();
1317 ArrayRef<MCPhysReg>::iterator NextFGR32 = FGR32ArgRegs.begin();
1318 ArrayRef<MCPhysReg>::iterator NextAFGR64 = AFGR64ArgRegs.begin();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001319
1320 struct AllocatedReg {
1321 const TargetRegisterClass *RC;
1322 unsigned Reg;
1323 AllocatedReg(const TargetRegisterClass *RC, unsigned Reg)
1324 : RC(RC), Reg(Reg) {}
1325 };
1326
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001327 // Only handle simple cases. i.e. All arguments are directly mapped to
1328 // registers of the appropriate type.
Daniel Sanderscbaca422016-07-29 12:27:28 +00001329 SmallVector<AllocatedReg, 4> Allocation;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001330 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001331 if (FormalArg.hasAttribute(Attribute::InReg) ||
1332 FormalArg.hasAttribute(Attribute::StructRet) ||
1333 FormalArg.hasAttribute(Attribute::ByVal)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001334 DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1335 return false;
1336 }
1337
1338 Type *ArgTy = FormalArg.getType();
1339 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) {
1340 DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1341 return false;
1342 }
1343
1344 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Reid Kleckner6652a522017-04-28 18:37:16 +00001345 DEBUG(dbgs() << ".. " << FormalArg.getArgNo() << ": "
1346 << ArgVT.getEVTString() << "\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001347 if (!ArgVT.isSimple()) {
1348 DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1349 return false;
1350 }
1351
1352 switch (ArgVT.getSimpleVT().SimpleTy) {
1353 case MVT::i1:
1354 case MVT::i8:
1355 case MVT::i16:
Reid Kleckner6652a522017-04-28 18:37:16 +00001356 if (!FormalArg.hasAttribute(Attribute::SExt) &&
1357 !FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001358 // It must be any extend, this shouldn't happen for clang-generated IR
1359 // so just fall back on SelectionDAG.
1360 DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1361 return false;
1362 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001363
1364 if (NextGPR32 == GPR32ArgRegs.end()) {
1365 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1366 return false;
1367 }
1368
1369 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1370 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1371
1372 // Allocating any GPR32 prohibits further use of floating point arguments.
1373 NextFGR32 = FGR32ArgRegs.end();
1374 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001375 break;
1376
1377 case MVT::i32:
Reid Kleckner6652a522017-04-28 18:37:16 +00001378 if (FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001379 // The O32 ABI does not permit a zero-extended i32.
1380 DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1381 return false;
1382 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001383
1384 if (NextGPR32 == GPR32ArgRegs.end()) {
1385 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1386 return false;
1387 }
1388
1389 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1390 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1391
1392 // Allocating any GPR32 prohibits further use of floating point arguments.
1393 NextFGR32 = FGR32ArgRegs.end();
1394 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001395 break;
1396
1397 case MVT::f32:
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001398 if (UnsupportedFPMode) {
1399 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1400 return false;
1401 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001402 if (NextFGR32 == FGR32ArgRegs.end()) {
1403 DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001404 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001405 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001406 DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32 << ")\n");
1407 Allocation.emplace_back(&Mips::FGR32RegClass, *NextFGR32++);
1408 // Allocating an FGR32 also allocates the super-register AFGR64, and
1409 // ABI rules require us to skip the corresponding GPR32.
1410 if (NextGPR32 != GPR32ArgRegs.end())
1411 NextGPR32++;
1412 if (NextAFGR64 != AFGR64ArgRegs.end())
1413 NextAFGR64++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001414 break;
1415
1416 case MVT::f64:
Simon Dardisb432a3e2016-09-06 12:36:24 +00001417 if (UnsupportedFPMode) {
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001418 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001419 return false;
1420 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001421 if (NextAFGR64 == AFGR64ArgRegs.end()) {
1422 DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001423 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001424 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001425 DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64 << ")\n");
1426 Allocation.emplace_back(&Mips::AFGR64RegClass, *NextAFGR64++);
1427 // Allocating an FGR32 also allocates the super-register AFGR64, and
1428 // ABI rules require us to skip the corresponding GPR32 pair.
1429 if (NextGPR32 != GPR32ArgRegs.end())
1430 NextGPR32++;
1431 if (NextGPR32 != GPR32ArgRegs.end())
1432 NextGPR32++;
1433 if (NextFGR32 != FGR32ArgRegs.end())
1434 NextFGR32++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001435 break;
1436
1437 default:
1438 DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1439 return false;
1440 }
Daniel Sanderscbaca422016-07-29 12:27:28 +00001441 }
1442
Daniel Sanderscbaca422016-07-29 12:27:28 +00001443 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001444 unsigned ArgNo = FormalArg.getArgNo();
1445 unsigned SrcReg = Allocation[ArgNo].Reg;
1446 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001447 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1448 // Without this, EmitLiveInCopies may eliminate the livein if its only
1449 // use is a bitcast (which isn't turned into an instruction).
Reid Kleckner6652a522017-04-28 18:37:16 +00001450 unsigned ResultReg = createResultReg(Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001451 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1452 TII.get(TargetOpcode::COPY), ResultReg)
1453 .addReg(DstReg, getKillRegState(true));
1454 updateValueMap(&FormalArg, ResultReg);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001455 }
1456
1457 // Calculate the size of the incoming arguments area.
1458 // We currently reject all the cases where this would be non-zero.
1459 unsigned IncomingArgSizeInBytes = 0;
1460
1461 // Account for the reserved argument area on ABI's that have one (O32).
1462 // It seems strange to do this on the caller side but it's necessary in
1463 // SelectionDAG's implementation.
1464 IncomingArgSizeInBytes = std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC),
1465 IncomingArgSizeInBytes);
1466
1467 MF->getInfo<MipsFunctionInfo>()->setFormalArgInfo(IncomingArgSizeInBytes,
1468 false);
1469
1470 return true;
1471}
1472
Reed Kotlerd5c41962014-11-13 23:37:45 +00001473bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1474 CallingConv::ID CC = CLI.CallConv;
1475 bool IsTailCall = CLI.IsTailCall;
1476 bool IsVarArg = CLI.IsVarArg;
1477 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001478 MCSymbol *Symbol = CLI.Symbol;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001479
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001480 // Do not handle FastCC.
1481 if (CC == CallingConv::Fast)
1482 return false;
1483
Reed Kotlerd5c41962014-11-13 23:37:45 +00001484 // Allow SelectionDAG isel to handle tail calls.
1485 if (IsTailCall)
1486 return false;
1487
1488 // Let SDISel handle vararg functions.
1489 if (IsVarArg)
1490 return false;
1491
1492 // FIXME: Only handle *simple* calls for now.
1493 MVT RetVT;
1494 if (CLI.RetTy->isVoidTy())
1495 RetVT = MVT::isVoid;
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001496 else if (!isTypeSupported(CLI.RetTy, RetVT))
Reed Kotlerd5c41962014-11-13 23:37:45 +00001497 return false;
1498
1499 for (auto Flag : CLI.OutFlags)
1500 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1501 return false;
1502
1503 // Set up the argument vectors.
1504 SmallVector<MVT, 16> OutVTs;
1505 OutVTs.reserve(CLI.OutVals.size());
1506
1507 for (auto *Val : CLI.OutVals) {
1508 MVT VT;
1509 if (!isTypeLegal(Val->getType(), VT) &&
1510 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1511 return false;
1512
1513 // We don't handle vector parameters yet.
1514 if (VT.isVector() || VT.getSizeInBits() > 64)
1515 return false;
1516
1517 OutVTs.push_back(VT);
1518 }
1519
1520 Address Addr;
1521 if (!computeCallAddress(Callee, Addr))
1522 return false;
1523
1524 // Handle the arguments now that we've gotten them.
1525 unsigned NumBytes;
1526 if (!processCallArgs(CLI, OutVTs, NumBytes))
1527 return false;
1528
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001529 if (!Addr.getGlobalValue())
1530 return false;
1531
Reed Kotlerd5c41962014-11-13 23:37:45 +00001532 // Issue the call.
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001533 unsigned DestAddress;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001534 if (Symbol)
1535 DestAddress = materializeExternalCallSym(Symbol);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001536 else
1537 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001538 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1539 MachineInstrBuilder MIB =
1540 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1541 Mips::RA).addReg(Mips::T9);
1542
1543 // Add implicit physical register uses to the call.
1544 for (auto Reg : CLI.OutRegs)
1545 MIB.addReg(Reg, RegState::Implicit);
1546
1547 // Add a register mask with the call-preserved registers.
1548 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001549 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Reed Kotlerd5c41962014-11-13 23:37:45 +00001550
1551 CLI.Call = MIB;
1552
Reed Kotlerd5c41962014-11-13 23:37:45 +00001553 // Finish off the call including any return values.
1554 return finishCall(CLI, RetVT, NumBytes);
1555}
1556
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001557bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1558 switch (II->getIntrinsicID()) {
1559 default:
1560 return false;
Vasileios Kalintiriscbbf8e02015-06-01 16:40:45 +00001561 case Intrinsic::bswap: {
1562 Type *RetTy = II->getCalledFunction()->getReturnType();
1563
1564 MVT VT;
1565 if (!isTypeSupported(RetTy, VT))
1566 return false;
1567
1568 unsigned SrcReg = getRegForValue(II->getOperand(0));
1569 if (SrcReg == 0)
1570 return false;
1571 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1572 if (DestReg == 0)
1573 return false;
1574 if (VT == MVT::i16) {
1575 if (Subtarget->hasMips32r2()) {
1576 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1577 updateValueMap(II, DestReg);
1578 return true;
1579 } else {
1580 unsigned TempReg[3];
1581 for (int i = 0; i < 3; i++) {
1582 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1583 if (TempReg[i] == 0)
1584 return false;
1585 }
1586 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1587 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1588 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1589 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1590 updateValueMap(II, DestReg);
1591 return true;
1592 }
1593 } else if (VT == MVT::i32) {
1594 if (Subtarget->hasMips32r2()) {
1595 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1596 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1597 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1598 updateValueMap(II, DestReg);
1599 return true;
1600 } else {
1601 unsigned TempReg[8];
1602 for (int i = 0; i < 8; i++) {
1603 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1604 if (TempReg[i] == 0)
1605 return false;
1606 }
1607
1608 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1609 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1610 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1611 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1612
1613 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1614 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1615
1616 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1617 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1618 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1619 updateValueMap(II, DestReg);
1620 return true;
1621 }
1622 }
1623 return false;
1624 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001625 case Intrinsic::memcpy:
1626 case Intrinsic::memmove: {
1627 const auto *MTI = cast<MemTransferInst>(II);
1628 // Don't handle volatile.
1629 if (MTI->isVolatile())
1630 return false;
1631 if (!MTI->getLength()->getType()->isIntegerTy(32))
1632 return false;
1633 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
Pete Cooper67cf9a72015-11-19 05:56:52 +00001634 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001635 }
1636 case Intrinsic::memset: {
1637 const MemSetInst *MSI = cast<MemSetInst>(II);
1638 // Don't handle volatile.
1639 if (MSI->isVolatile())
1640 return false;
1641 if (!MSI->getLength()->getType()->isIntegerTy(32))
1642 return false;
Pete Cooper67cf9a72015-11-19 05:56:52 +00001643 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001644 }
1645 }
1646 return false;
1647}
1648
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001649bool MipsFastISel::selectRet(const Instruction *I) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001650 const Function &F = *I->getParent()->getParent();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001651 const ReturnInst *Ret = cast<ReturnInst>(I);
1652
Simon Dardisb432a3e2016-09-06 12:36:24 +00001653 DEBUG(dbgs() << "selectRet\n");
1654
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001655 if (!FuncInfo.CanLowerReturn)
1656 return false;
Reed Kotleraa150ed2015-02-12 21:05:12 +00001657
1658 // Build a list of return value registers.
1659 SmallVector<unsigned, 4> RetRegs;
1660
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001661 if (Ret->getNumOperands() > 0) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001662 CallingConv::ID CC = F.getCallingConv();
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001663
1664 // Do not handle FastCC.
1665 if (CC == CallingConv::Fast)
1666 return false;
1667
Reed Kotleraa150ed2015-02-12 21:05:12 +00001668 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001669 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1670
Reed Kotleraa150ed2015-02-12 21:05:12 +00001671 // Analyze operands of the call, assigning locations to each operand.
1672 SmallVector<CCValAssign, 16> ValLocs;
1673 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1674 I->getContext());
1675 CCAssignFn *RetCC = RetCC_Mips;
1676 CCInfo.AnalyzeReturn(Outs, RetCC);
1677
1678 // Only handle a single return value for now.
1679 if (ValLocs.size() != 1)
1680 return false;
1681
1682 CCValAssign &VA = ValLocs[0];
1683 const Value *RV = Ret->getOperand(0);
1684
1685 // Don't bother handling odd stuff for now.
1686 if ((VA.getLocInfo() != CCValAssign::Full) &&
1687 (VA.getLocInfo() != CCValAssign::BCvt))
1688 return false;
1689
1690 // Only handle register returns for now.
1691 if (!VA.isRegLoc())
1692 return false;
1693
1694 unsigned Reg = getRegForValue(RV);
1695 if (Reg == 0)
1696 return false;
1697
1698 unsigned SrcReg = Reg + VA.getValNo();
1699 unsigned DestReg = VA.getLocReg();
1700 // Avoid a cross-class copy. This is very unlikely.
1701 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1702 return false;
1703
Mehdi Amini44ede332015-07-09 02:09:04 +00001704 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Reed Kotleraa150ed2015-02-12 21:05:12 +00001705 if (!RVEVT.isSimple())
1706 return false;
1707
1708 if (RVEVT.isVector())
1709 return false;
1710
1711 MVT RVVT = RVEVT.getSimpleVT();
1712 if (RVVT == MVT::f128)
1713 return false;
1714
Simon Dardisb432a3e2016-09-06 12:36:24 +00001715 // Do not handle FGR64 returns for now.
1716 if (RVVT == MVT::f64 && UnsupportedFPMode) {
1717 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1718 return false;
1719 }
1720
Reed Kotleraa150ed2015-02-12 21:05:12 +00001721 MVT DestVT = VA.getValVT();
1722 // Special handling for extended integers.
1723 if (RVVT != DestVT) {
1724 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1725 return false;
1726
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001727 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1728 bool IsZExt = Outs[0].Flags.isZExt();
1729 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1730 if (SrcReg == 0)
1731 return false;
1732 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001733 }
1734
1735 // Make the copy.
1736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1737 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1738
1739 // Add register to return instruction.
1740 RetRegs.push_back(VA.getLocReg());
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001741 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001742 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1743 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1744 MIB.addReg(RetRegs[i], RegState::Implicit);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001745 return true;
1746}
1747
1748bool MipsFastISel::selectTrunc(const Instruction *I) {
1749 // The high bits for a type smaller than the register size are assumed to be
1750 // undefined.
1751 Value *Op = I->getOperand(0);
1752
1753 EVT SrcVT, DestVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001754 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1755 DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001756
1757 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1758 return false;
1759 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1760 return false;
1761
1762 unsigned SrcReg = getRegForValue(Op);
1763 if (!SrcReg)
1764 return false;
1765
1766 // Because the high bits are undefined, a truncate doesn't generate
1767 // any code.
1768 updateValueMap(I, SrcReg);
1769 return true;
1770}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001771
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001772bool MipsFastISel::selectIntExt(const Instruction *I) {
1773 Type *DestTy = I->getType();
1774 Value *Src = I->getOperand(0);
1775 Type *SrcTy = Src->getType();
1776
1777 bool isZExt = isa<ZExtInst>(I);
1778 unsigned SrcReg = getRegForValue(Src);
1779 if (!SrcReg)
1780 return false;
1781
1782 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001783 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1784 DestEVT = TLI.getValueType(DL, DestTy, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001785 if (!SrcEVT.isSimple())
1786 return false;
1787 if (!DestEVT.isSimple())
1788 return false;
1789
1790 MVT SrcVT = SrcEVT.getSimpleVT();
1791 MVT DestVT = DestEVT.getSimpleVT();
1792 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1793
1794 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1795 return false;
1796 updateValueMap(I, ResultReg);
1797 return true;
1798}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001799
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001800bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1801 unsigned DestReg) {
1802 unsigned ShiftAmt;
1803 switch (SrcVT.SimpleTy) {
1804 default:
1805 return false;
1806 case MVT::i8:
1807 ShiftAmt = 24;
1808 break;
1809 case MVT::i16:
1810 ShiftAmt = 16;
1811 break;
1812 }
1813 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1814 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1815 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1816 return true;
1817}
1818
1819bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1820 unsigned DestReg) {
1821 switch (SrcVT.SimpleTy) {
1822 default:
1823 return false;
1824 case MVT::i8:
1825 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1826 break;
1827 case MVT::i16:
1828 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1829 break;
1830 }
1831 return true;
1832}
1833
1834bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1835 unsigned DestReg) {
1836 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1837 return false;
1838 if (Subtarget->hasMips32r2())
1839 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1840 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1841}
1842
1843bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1844 unsigned DestReg) {
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001845 int64_t Imm;
1846
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001847 switch (SrcVT.SimpleTy) {
1848 default:
1849 return false;
1850 case MVT::i1:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001851 Imm = 1;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001852 break;
1853 case MVT::i8:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001854 Imm = 0xff;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001855 break;
1856 case MVT::i16:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001857 Imm = 0xffff;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001858 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001859 }
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001860
1861 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001862 return true;
1863}
1864
1865bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1866 unsigned DestReg, bool IsZExt) {
Vasileios Kalintiris1202f362015-04-24 13:48:19 +00001867 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1868 // DestVT are odd things, so test to make sure that they are both types we can
1869 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1870 // bail out to SelectionDAG.
1871 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1872 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1873 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001874 if (IsZExt)
1875 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1876 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1877}
Reed Kotlerd5c41962014-11-13 23:37:45 +00001878
1879unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1880 bool isZExt) {
1881 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotleraa150ed2015-02-12 21:05:12 +00001882 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1883 return Success ? DestReg : 0;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001884}
1885
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001886bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001887 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001888 if (!DestEVT.isSimple())
1889 return false;
1890
1891 MVT DestVT = DestEVT.getSimpleVT();
1892 if (DestVT != MVT::i32)
1893 return false;
1894
1895 unsigned DivOpc;
1896 switch (ISDOpcode) {
1897 default:
1898 return false;
1899 case ISD::SDIV:
1900 case ISD::SREM:
1901 DivOpc = Mips::SDIV;
1902 break;
1903 case ISD::UDIV:
1904 case ISD::UREM:
1905 DivOpc = Mips::UDIV;
1906 break;
1907 }
1908
1909 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1910 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1911 if (!Src0Reg || !Src1Reg)
1912 return false;
1913
1914 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1915 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1916
1917 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1918 if (!ResultReg)
1919 return false;
1920
1921 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1922 ? Mips::MFHI
1923 : Mips::MFLO;
1924 emitInst(MFOpc, ResultReg);
1925
1926 updateValueMap(I, ResultReg);
1927 return true;
1928}
1929
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001930bool MipsFastISel::selectShift(const Instruction *I) {
1931 MVT RetVT;
1932
1933 if (!isTypeSupported(I->getType(), RetVT))
1934 return false;
1935
1936 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1937 if (!ResultReg)
1938 return false;
1939
1940 unsigned Opcode = I->getOpcode();
1941 const Value *Op0 = I->getOperand(0);
1942 unsigned Op0Reg = getRegForValue(Op0);
1943 if (!Op0Reg)
1944 return false;
1945
1946 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1947 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1948 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1949 if (!TempReg)
1950 return false;
1951
Mehdi Amini44ede332015-07-09 02:09:04 +00001952 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001953 bool IsZExt = Opcode == Instruction::LShr;
1954 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1955 return false;
1956
1957 Op0Reg = TempReg;
1958 }
1959
1960 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1961 uint64_t ShiftVal = C->getZExtValue();
1962
1963 switch (Opcode) {
1964 default:
1965 llvm_unreachable("Unexpected instruction.");
1966 case Instruction::Shl:
1967 Opcode = Mips::SLL;
1968 break;
1969 case Instruction::AShr:
1970 Opcode = Mips::SRA;
1971 break;
1972 case Instruction::LShr:
1973 Opcode = Mips::SRL;
1974 break;
1975 }
1976
1977 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1978 updateValueMap(I, ResultReg);
1979 return true;
1980 }
1981
1982 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1983 if (!Op1Reg)
1984 return false;
1985
1986 switch (Opcode) {
1987 default:
1988 llvm_unreachable("Unexpected instruction.");
1989 case Instruction::Shl:
1990 Opcode = Mips::SLLV;
1991 break;
1992 case Instruction::AShr:
1993 Opcode = Mips::SRAV;
1994 break;
1995 case Instruction::LShr:
1996 Opcode = Mips::SRLV;
1997 break;
1998 }
1999
2000 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
2001 updateValueMap(I, ResultReg);
2002 return true;
2003}
2004
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002005bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
Reed Kotler67077b32014-04-29 17:57:50 +00002006 switch (I->getOpcode()) {
2007 default:
2008 break;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +00002009 case Instruction::Load:
Reed Kotlera562b462014-10-13 21:46:41 +00002010 return selectLoad(I);
Reed Kotlerbab3f232014-05-01 20:39:21 +00002011 case Instruction::Store:
Reed Kotlera562b462014-10-13 21:46:41 +00002012 return selectStore(I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00002013 case Instruction::SDiv:
2014 if (!selectBinaryOp(I, ISD::SDIV))
2015 return selectDivRem(I, ISD::SDIV);
2016 return true;
2017 case Instruction::UDiv:
2018 if (!selectBinaryOp(I, ISD::UDIV))
2019 return selectDivRem(I, ISD::UDIV);
2020 return true;
2021 case Instruction::SRem:
2022 if (!selectBinaryOp(I, ISD::SREM))
2023 return selectDivRem(I, ISD::SREM);
2024 return true;
2025 case Instruction::URem:
2026 if (!selectBinaryOp(I, ISD::UREM))
2027 return selectDivRem(I, ISD::UREM);
2028 return true;
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00002029 case Instruction::Shl:
2030 case Instruction::LShr:
2031 case Instruction::AShr:
2032 return selectShift(I);
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002033 case Instruction::And:
2034 case Instruction::Or:
2035 case Instruction::Xor:
2036 return selectLogicalOp(I);
Reed Kotler62de6b92014-10-11 00:55:18 +00002037 case Instruction::Br:
Reed Kotlera562b462014-10-13 21:46:41 +00002038 return selectBranch(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002039 case Instruction::Ret:
Reed Kotlera562b462014-10-13 21:46:41 +00002040 return selectRet(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002041 case Instruction::Trunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002042 return selectTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002043 case Instruction::ZExt:
2044 case Instruction::SExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002045 return selectIntExt(I);
Reed Kotlerb9dc2482014-10-01 18:47:02 +00002046 case Instruction::FPTrunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002047 return selectFPTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002048 case Instruction::FPExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002049 return selectFPExt(I);
Reed Kotler12f94882014-10-10 17:00:46 +00002050 case Instruction::FPToSI:
Reed Kotlera562b462014-10-13 21:46:41 +00002051 return selectFPToInt(I, /*isSigned*/ true);
Reed Kotler12f94882014-10-10 17:00:46 +00002052 case Instruction::FPToUI:
Reed Kotlera562b462014-10-13 21:46:41 +00002053 return selectFPToInt(I, /*isSigned*/ false);
Reed Kotler497311a2014-10-10 17:39:51 +00002054 case Instruction::ICmp:
2055 case Instruction::FCmp:
Reed Kotlera562b462014-10-13 21:46:41 +00002056 return selectCmp(I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00002057 case Instruction::Select:
2058 return selectSelect(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002059 }
2060 return false;
2061}
Reed Kotler720c5ca2014-04-17 22:15:34 +00002062
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002063unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
2064 bool IsUnsigned) {
2065 unsigned VReg = getRegForValue(V);
2066 if (VReg == 0)
Reed Kotler12f94882014-10-10 17:00:46 +00002067 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00002068 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002069 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
2070 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
2071 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
2072 return 0;
2073 VReg = TempReg;
Reed Kotler063d4fb2014-06-10 16:45:44 +00002074 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002075 return VReg;
Reed Kotlerbab3f232014-05-01 20:39:21 +00002076}
2077
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002078void MipsFastISel::simplifyAddress(Address &Addr) {
2079 if (!isInt<16>(Addr.getOffset())) {
2080 unsigned TempReg =
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002081 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002082 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
2083 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
2084 Addr.setReg(DestReg);
2085 Addr.setOffset(0);
2086 }
2087}
2088
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +00002089unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2090 const TargetRegisterClass *RC,
2091 unsigned Op0, bool Op0IsKill,
2092 unsigned Op1, bool Op1IsKill) {
2093 // We treat the MUL instruction in a special way because it clobbers
2094 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2095 // mark these registers only as implicitly defined. As a result, the
2096 // register allocator runs out of registers when this instruction is
2097 // followed by another instruction that defines the same registers too.
2098 // We can fix this by explicitly marking those registers as dead.
2099 if (MachineInstOpcode == Mips::MUL) {
2100 unsigned ResultReg = createResultReg(RC);
2101 const MCInstrDesc &II = TII.get(MachineInstOpcode);
2102 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
2103 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
2104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
2105 .addReg(Op0, getKillRegState(Op0IsKill))
2106 .addReg(Op1, getKillRegState(Op1IsKill))
2107 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
2108 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
2109 return ResultReg;
2110 }
2111
2112 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
2113 Op1IsKill);
2114}
2115
Reed Kotler720c5ca2014-04-17 22:15:34 +00002116namespace llvm {
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002117
Reed Kotler720c5ca2014-04-17 22:15:34 +00002118FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
2119 const TargetLibraryInfo *libInfo) {
2120 return new MipsFastISel(funcInfo, libInfo);
2121}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002122
2123} // end namespace llvm