blob: 85bd565571cb69a5b8721a3cb133d4add5b4c115 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsFastISel.cpp - Mips FastISel implementation --------------------===//
Vasileios Kalintirisa9e51542016-06-08 13:13:15 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
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"
20#include "MipsISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "MipsInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "MipsMachineFunction.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000023#include "MipsSubtarget.h"
24#include "MipsTargetMachine.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000025#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/SmallVector.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000029#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000030#include "llvm/CodeGen/CallingConvLower.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000031#include "llvm/CodeGen/FastISel.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000032#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000033#include "llvm/CodeGen/ISDOpcodes.h"
34#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
Reed Kotler67077b32014-04-29 17:57:50 +000036#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000037#include "llvm/CodeGen/MachineMemOperand.h"
Reed Kotleraa150ed2015-02-12 21:05:12 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000039#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetLowering.h"
Craig Topper2fa14362018-03-29 17:21:10 +000041#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000042#include "llvm/IR/Attributes.h"
43#include "llvm/IR/CallingConv.h"
44#include "llvm/IR/Constant.h"
45#include "llvm/IR/Constants.h"
46#include "llvm/IR/DataLayout.h"
47#include "llvm/IR/Function.h"
David Blaikie457343d2015-05-21 21:12:43 +000048#include "llvm/IR/GetElementPtrTypeIterator.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000049#include "llvm/IR/GlobalValue.h"
Reed Kotlerbab3f232014-05-01 20:39:21 +000050#include "llvm/IR/GlobalVariable.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000051#include "llvm/IR/InstrTypes.h"
52#include "llvm/IR/Instruction.h"
53#include "llvm/IR/Instructions.h"
54#include "llvm/IR/IntrinsicInst.h"
55#include "llvm/IR/Operator.h"
56#include "llvm/IR/Type.h"
57#include "llvm/IR/User.h"
58#include "llvm/IR/Value.h"
59#include "llvm/MC/MCInstrDesc.h"
60#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000061#include "llvm/MC/MCSymbol.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000062#include "llvm/Support/Casting.h"
63#include "llvm/Support/Compiler.h"
Daniel Sanderscbaca422016-07-29 12:27:28 +000064#include "llvm/Support/Debug.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000065#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000066#include "llvm/Support/MachineValueType.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000067#include "llvm/Support/MathExtras.h"
68#include "llvm/Support/raw_ostream.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000069#include <algorithm>
70#include <cassert>
71#include <cstdint>
Daniel Sanderscbaca422016-07-29 12:27:28 +000072
73#define DEBUG_TYPE "mips-fastisel"
Reed Kotler720c5ca2014-04-17 22:15:34 +000074
75using namespace llvm;
76
77namespace {
78
79class MipsFastISel final : public FastISel {
80
Reed Kotlera562b462014-10-13 21:46:41 +000081 // All possible address modes.
82 class Address {
83 public:
Eugene Zelenko79220eae2017-08-03 22:12:30 +000084 using BaseKind = enum { RegBase, FrameIndexBase };
Reed Kotlera562b462014-10-13 21:46:41 +000085
86 private:
Eugene Zelenkodde94e42017-01-30 23:21:32 +000087 BaseKind Kind = RegBase;
Reed Kotlera562b462014-10-13 21:46:41 +000088 union {
89 unsigned Reg;
90 int FI;
91 } Base;
92
Eugene Zelenkodde94e42017-01-30 23:21:32 +000093 int64_t Offset = 0;
Reed Kotlera562b462014-10-13 21:46:41 +000094
Eugene Zelenkodde94e42017-01-30 23:21:32 +000095 const GlobalValue *GV = nullptr;
Reed Kotlera562b462014-10-13 21:46:41 +000096
97 public:
98 // Innocuous defaults for our address.
Eugene Zelenkodde94e42017-01-30 23:21:32 +000099 Address() { Base.Reg = 0; }
100
Reed Kotlera562b462014-10-13 21:46:41 +0000101 void setKind(BaseKind K) { Kind = K; }
102 BaseKind getKind() const { return Kind; }
103 bool isRegBase() const { return Kind == RegBase; }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000104 bool isFIBase() const { return Kind == FrameIndexBase; }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000105
Reed Kotlera562b462014-10-13 21:46:41 +0000106 void setReg(unsigned Reg) {
107 assert(isRegBase() && "Invalid base register access!");
108 Base.Reg = Reg;
109 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000110
Reed Kotlera562b462014-10-13 21:46:41 +0000111 unsigned getReg() const {
112 assert(isRegBase() && "Invalid base register access!");
113 return Base.Reg;
114 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000115
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000116 void setFI(unsigned FI) {
117 assert(isFIBase() && "Invalid base frame index access!");
118 Base.FI = FI;
119 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000120
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000121 unsigned getFI() const {
122 assert(isFIBase() && "Invalid base frame index access!");
123 return Base.FI;
124 }
125
Reed Kotlera562b462014-10-13 21:46:41 +0000126 void setOffset(int64_t Offset_) { Offset = Offset_; }
127 int64_t getOffset() const { return Offset; }
128 void setGlobalValue(const GlobalValue *G) { GV = G; }
129 const GlobalValue *getGlobalValue() { return GV; }
130 };
131
Reed Kotler67077b32014-04-29 17:57:50 +0000132 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
133 /// make the right decision when generating code for different targets.
Reed Kotler67077b32014-04-29 17:57:50 +0000134 const TargetMachine &TM;
Eric Christopher96e72c62015-01-29 23:27:36 +0000135 const MipsSubtarget *Subtarget;
Reed Kotler67077b32014-04-29 17:57:50 +0000136 const TargetInstrInfo &TII;
137 const TargetLowering &TLI;
138 MipsFunctionInfo *MFI;
139
140 // Convenience variables to avoid some queries.
141 LLVMContext *Context;
142
Daniel Sanderscbaca422016-07-29 12:27:28 +0000143 bool fastLowerArguments() override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000144 bool fastLowerCall(CallLoweringInfo &CLI) override;
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000145 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000146
Reed Kotlera562b462014-10-13 21:46:41 +0000147 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
148 // floating point but not reject doing fast-isel in other
149 // situations
150
151private:
152 // Selection routines.
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000153 bool selectLogicalOp(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000154 bool selectLoad(const Instruction *I);
155 bool selectStore(const Instruction *I);
156 bool selectBranch(const Instruction *I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +0000157 bool selectSelect(const Instruction *I);
Reed Kotlera562b462014-10-13 21:46:41 +0000158 bool selectCmp(const Instruction *I);
159 bool selectFPExt(const Instruction *I);
160 bool selectFPTrunc(const Instruction *I);
161 bool selectFPToInt(const Instruction *I, bool IsSigned);
162 bool selectRet(const Instruction *I);
163 bool selectTrunc(const Instruction *I);
164 bool selectIntExt(const Instruction *I);
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +0000165 bool selectShift(const Instruction *I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +0000166 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
Reed Kotlera562b462014-10-13 21:46:41 +0000167
168 // Utility helper routines.
Reed Kotlera562b462014-10-13 21:46:41 +0000169 bool isTypeLegal(Type *Ty, MVT &VT);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000170 bool isTypeSupported(Type *Ty, MVT &VT);
Reed Kotlera562b462014-10-13 21:46:41 +0000171 bool isLoadTypeLegal(Type *Ty, MVT &VT);
172 bool computeAddress(const Value *Obj, Address &Addr);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000173 bool computeCallAddress(const Value *V, Address &Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000174 void simplifyAddress(Address &Addr);
Reed Kotlera562b462014-10-13 21:46:41 +0000175
176 // Emit helper routines.
177 bool emitCmp(unsigned DestReg, const CmpInst *CI);
178 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
179 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000180 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
181 MachineMemOperand *MMO = nullptr);
Reed Kotlera562b462014-10-13 21:46:41 +0000182 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
183 unsigned Alignment = 0);
Reed Kotlerd5c41962014-11-13 23:37:45 +0000184 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Reed Kotlera562b462014-10-13 21:46:41 +0000185 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
186
187 bool IsZExt);
188 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
189
190 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
191 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
192 unsigned DestReg);
193 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
194 unsigned DestReg);
195
196 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
197
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000198 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
199 const Value *RHS);
200
Reed Kotlera562b462014-10-13 21:46:41 +0000201 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
202 unsigned materializeGV(const GlobalValue *GV, MVT VT);
203 unsigned materializeInt(const Constant *C, MVT VT);
204 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000205 unsigned materializeExternalCallSym(MCSymbol *Syn);
Reed Kotlera562b462014-10-13 21:46:41 +0000206
207 MachineInstrBuilder emitInst(unsigned Opc) {
208 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
209 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000210
Reed Kotlera562b462014-10-13 21:46:41 +0000211 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
212 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
213 DstReg);
214 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000215
Reed Kotlera562b462014-10-13 21:46:41 +0000216 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
217 unsigned MemReg, int64_t MemOffset) {
218 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
219 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000220
Reed Kotlera562b462014-10-13 21:46:41 +0000221 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
222 unsigned MemReg, int64_t MemOffset) {
223 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
224 }
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +0000225
226 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
227 const TargetRegisterClass *RC,
228 unsigned Op0, bool Op0IsKill,
229 unsigned Op1, bool Op1IsKill);
230
Reed Kotlera562b462014-10-13 21:46:41 +0000231 // for some reason, this default is not generated by tablegen
232 // so we explicitly generate it here.
Reed Kotlera562b462014-10-13 21:46:41 +0000233 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
234 unsigned Op0, bool Op0IsKill, uint64_t imm1,
235 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
236 return 0;
237 }
Reed Kotler67077b32014-04-29 17:57:50 +0000238
Reed Kotlerd5c41962014-11-13 23:37:45 +0000239 // Call handling routines.
240private:
241 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
242 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
243 unsigned &NumBytes);
244 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000245
Daniel Sanderscbaca422016-07-29 12:27:28 +0000246 const MipsABIInfo &getABI() const {
247 return static_cast<const MipsTargetMachine &>(TM).getABI();
248 }
Reed Kotlerd5c41962014-11-13 23:37:45 +0000249
Reed Kotler720c5ca2014-04-17 22:15:34 +0000250public:
Reed Kotlera562b462014-10-13 21:46:41 +0000251 // Backend specific FastISel code.
Reed Kotler720c5ca2014-04-17 22:15:34 +0000252 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
253 const TargetLibraryInfo *libInfo)
Eric Christopher3ab98892014-12-20 00:07:09 +0000254 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
Eric Christopherb2a5fa92015-02-14 00:09:46 +0000255 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
Eric Christopher96e72c62015-01-29 23:27:36 +0000256 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
Reed Kotler67077b32014-04-29 17:57:50 +0000257 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
258 Context = &funcInfo.Fn->getContext();
Simon Dardis86b3a1e2016-10-04 10:35:07 +0000259 UnsupportedFPMode = Subtarget->isFP64bit() || Subtarget->useSoftFloat();
Reed Kotler67077b32014-04-29 17:57:50 +0000260 }
261
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000262 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000263 unsigned fastMaterializeConstant(const Constant *C) override;
Reed Kotlera562b462014-10-13 21:46:41 +0000264 bool fastSelectInstruction(const Instruction *I) override;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000265
Reed Kotler9fe25f32014-06-08 02:08:43 +0000266#include "MipsGenFastISel.inc"
Reed Kotler720c5ca2014-04-17 22:15:34 +0000267};
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000268
269} // end anonymous namespace
Reed Kotler67077b32014-04-29 17:57:50 +0000270
Reed Kotlerd5c41962014-11-13 23:37:45 +0000271static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
272 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Reid Klecknerd3781742014-11-14 00:39:33 +0000273 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +0000274
275static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
276 CCValAssign::LocInfo LocInfo,
277 ISD::ArgFlagsTy ArgFlags, CCState &State) {
278 llvm_unreachable("should not be called");
279}
280
Benjamin Kramer970eac42015-02-06 17:51:54 +0000281static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
282 CCValAssign::LocInfo LocInfo,
283 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Reed Kotlerd5c41962014-11-13 23:37:45 +0000284 llvm_unreachable("should not be called");
285}
286
287#include "MipsGenCallingConv.inc"
288
289CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
290 return CC_MipsO32;
291}
292
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000293unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
294 const Value *LHS, const Value *RHS) {
295 // Canonicalize immediates to the RHS first.
296 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
297 std::swap(LHS, RHS);
298
299 unsigned Opc;
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000300 switch (ISDOpc) {
Vasileios Kalintiris2a95f822015-10-12 15:39:41 +0000301 case ISD::AND:
302 Opc = Mips::AND;
303 break;
304 case ISD::OR:
305 Opc = Mips::OR;
306 break;
307 case ISD::XOR:
308 Opc = Mips::XOR;
309 break;
310 default:
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000311 llvm_unreachable("unexpected opcode");
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000312 }
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000313
314 unsigned LHSReg = getRegForValue(LHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000315 if (!LHSReg)
316 return 0;
317
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000318 unsigned RHSReg;
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000319 if (const auto *C = dyn_cast<ConstantInt>(RHS))
320 RHSReg = materializeInt(C, MVT::i32);
321 else
322 RHSReg = getRegForValue(RHS);
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000323 if (!RHSReg)
324 return 0;
325
Vasileios Kalintirisdaad5712015-10-07 18:14:24 +0000326 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
327 if (!ResultReg)
328 return 0;
329
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000330 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
331 return ResultReg;
332}
333
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000334unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000335 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 &&
Vasileios Kalintiris816ea842015-04-17 17:29:58 +0000336 "Alloca should always return a pointer.");
337
338 DenseMap<const AllocaInst *, int>::iterator SI =
339 FuncInfo.StaticAllocaMap.find(AI);
340
341 if (SI != FuncInfo.StaticAllocaMap.end()) {
342 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
343 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
344 ResultReg)
345 .addFrameIndex(SI->second)
346 .addImm(0);
347 return ResultReg;
348 }
349
350 return 0;
351}
352
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000353unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
354 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Reed Kotler497311a2014-10-10 17:39:51 +0000355 return 0;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000356 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
357 const ConstantInt *CI = cast<ConstantInt>(C);
Vasileios Kalintiris77fb0a32015-07-30 11:51:44 +0000358 return materialize32BitInt(CI->getZExtValue(), RC);
Reed Kotler497311a2014-10-10 17:39:51 +0000359}
360
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000361unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
362 const TargetRegisterClass *RC) {
363 unsigned ResultReg = createResultReg(RC);
364
365 if (isInt<16>(Imm)) {
366 unsigned Opc = Mips::ADDiu;
367 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
368 return ResultReg;
369 } else if (isUInt<16>(Imm)) {
370 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
371 return ResultReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000372 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000373 unsigned Lo = Imm & 0xFFFF;
374 unsigned Hi = (Imm >> 16) & 0xFFFF;
375 if (Lo) {
376 // Both Lo and Hi have nonzero bits.
377 unsigned TmpReg = createResultReg(RC);
378 emitInst(Mips::LUi, TmpReg).addImm(Hi);
379 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
380 } else {
381 emitInst(Mips::LUi, ResultReg).addImm(Hi);
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000382 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000383 return ResultReg;
384}
385
386unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
387 if (UnsupportedFPMode)
388 return 0;
389 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
390 if (VT == MVT::f32) {
391 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
392 unsigned DestReg = createResultReg(RC);
393 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
394 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
395 return DestReg;
396 } else if (VT == MVT::f64) {
397 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
398 unsigned DestReg = createResultReg(RC);
399 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
400 unsigned TempReg2 =
401 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
402 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
403 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000404 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000405 return 0;
406}
407
408unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
409 // For now 32-bit only.
410 if (VT != MVT::i32)
411 return 0;
412 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
413 unsigned DestReg = createResultReg(RC);
414 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
415 bool IsThreadLocal = GVar && GVar->isThreadLocal();
416 // TLS not supported at this time.
417 if (IsThreadLocal)
418 return 0;
419 emitInst(Mips::LW, DestReg)
420 .addReg(MFI->getGlobalBaseReg())
421 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
422 if ((GV->hasInternalLinkage() ||
423 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
424 unsigned TempReg = createResultReg(RC);
425 emitInst(Mips::ADDiu, TempReg)
426 .addReg(DestReg)
427 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
428 DestReg = TempReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000429 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000430 return DestReg;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +0000431}
432
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000433unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000434 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
435 unsigned DestReg = createResultReg(RC);
436 emitInst(Mips::LW, DestReg)
437 .addReg(MFI->getGlobalBaseReg())
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000438 .addSym(Sym, MipsII::MO_GOT);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000439 return DestReg;
440}
441
Reed Kotlerbab3f232014-05-01 20:39:21 +0000442// Materialize a constant into a register, and return the register
443// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000444unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000445 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000446
447 // Only handle simple types.
448 if (!CEVT.isSimple())
449 return 0;
450 MVT VT = CEVT.getSimpleVT();
451
452 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000453 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000454 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000455 return materializeGV(GV, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000456 else if (isa<ConstantInt>(C))
Reed Kotlera562b462014-10-13 21:46:41 +0000457 return materializeInt(C, VT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000458
459 return 0;
460}
461
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000462bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000463 const User *U = nullptr;
464 unsigned Opcode = Instruction::UserOp1;
465 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
466 // Don't walk into other basic blocks unless the object is an alloca from
467 // another block, otherwise it may not have a virtual register assigned.
468 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
469 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
470 Opcode = I->getOpcode();
471 U = I;
472 }
Vasileios Kalintiris32cd69a2015-05-12 12:08:31 +0000473 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
474 Opcode = C->getOpcode();
475 U = C;
476 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000477 switch (Opcode) {
478 default:
479 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000480 case Instruction::BitCast:
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000481 // Look through bitcasts.
482 return computeAddress(U->getOperand(0), Addr);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000483 case Instruction::GetElementPtr: {
484 Address SavedAddr = Addr;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000485 int64_t TmpOffset = Addr.getOffset();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000486 // Iterate through the GEP folding the constants into offsets where
487 // we can.
488 gep_type_iterator GTI = gep_type_begin(U);
489 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
490 ++i, ++GTI) {
491 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000492 if (StructType *STy = GTI.getStructTypeOrNull()) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000493 const StructLayout *SL = DL.getStructLayout(STy);
494 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
495 TmpOffset += SL->getElementOffset(Idx);
496 } else {
497 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000498 while (true) {
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000499 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
500 // Constant-offset addressing.
501 TmpOffset += CI->getSExtValue() * S;
502 break;
503 }
504 if (canFoldAddIntoGEP(U, Op)) {
505 // A compatible add with a constant operand. Fold the constant.
506 ConstantInt *CI =
507 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
508 TmpOffset += CI->getSExtValue() * S;
509 // Iterate on the other operand.
510 Op = cast<AddOperator>(Op)->getOperand(0);
511 continue;
512 }
513 // Unsupported
514 goto unsupported_gep;
515 }
516 }
517 }
518 // Try to grab the base operand now.
519 Addr.setOffset(TmpOffset);
520 if (computeAddress(U->getOperand(0), Addr))
521 return true;
522 // We failed, restore everything and try the other options.
523 Addr = SavedAddr;
524 unsupported_gep:
525 break;
526 }
527 case Instruction::Alloca: {
528 const AllocaInst *AI = cast<AllocaInst>(Obj);
529 DenseMap<const AllocaInst *, int>::iterator SI =
530 FuncInfo.StaticAllocaMap.find(AI);
531 if (SI != FuncInfo.StaticAllocaMap.end()) {
532 Addr.setKind(Address::FrameIndexBase);
533 Addr.setFI(SI->second);
534 return true;
535 }
536 break;
537 }
538 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000539 Addr.setReg(getRegForValue(Obj));
540 return Addr.getReg() != 0;
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000541}
542
Reed Kotlerd5c41962014-11-13 23:37:45 +0000543bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000544 const User *U = nullptr;
545 unsigned Opcode = Instruction::UserOp1;
546
547 if (const auto *I = dyn_cast<Instruction>(V)) {
548 // Check if the value is defined in the same basic block. This information
549 // is crucial to know whether or not folding an operand is valid.
550 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
551 Opcode = I->getOpcode();
552 U = I;
553 }
554 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
555 Opcode = C->getOpcode();
556 U = C;
557 }
558
559 switch (Opcode) {
560 default:
561 break;
562 case Instruction::BitCast:
563 // Look past bitcasts if its operand is in the same BB.
564 return computeCallAddress(U->getOperand(0), Addr);
565 break;
566 case Instruction::IntToPtr:
567 // Look past no-op inttoptrs if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000568 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
569 TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000570 return computeCallAddress(U->getOperand(0), Addr);
571 break;
572 case Instruction::PtrToInt:
573 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +0000574 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000575 return computeCallAddress(U->getOperand(0), Addr);
576 break;
577 }
578
Reed Kotlerd5c41962014-11-13 23:37:45 +0000579 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
580 Addr.setGlobalValue(GV);
581 return true;
582 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +0000583
584 // If all else fails, try to materialize the value in a register.
585 if (!Addr.getGlobalValue()) {
586 Addr.setReg(getRegForValue(V));
587 return Addr.getReg() != 0;
588 }
589
Reed Kotlerd5c41962014-11-13 23:37:45 +0000590 return false;
591}
592
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000593bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000594 EVT evt = TLI.getValueType(DL, Ty, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000595 // Only handle simple types.
596 if (evt == MVT::Other || !evt.isSimple())
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000597 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000598 VT = evt.getSimpleVT();
599
600 // Handle all legal types, i.e. a register that will directly hold this
601 // value.
602 return TLI.isTypeLegal(VT);
Reed Kotler3ebdcc92014-09-30 16:30:13 +0000603}
604
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000605bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
606 if (Ty->isVectorTy())
607 return false;
608
609 if (isTypeLegal(Ty, VT))
610 return true;
611
612 // If this is a type than can be sign or zero-extended to a basic operation
613 // go ahead and accept it now.
614 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
615 return true;
616
617 return false;
618}
619
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000620bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
621 if (isTypeLegal(Ty, VT))
Reed Kotler62de6b92014-10-11 00:55:18 +0000622 return true;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000623 // We will extend this in a later patch:
624 // If this is a type than can be sign or zero-extended to a basic operation
625 // go ahead and accept it now.
626 if (VT == MVT::i8 || VT == MVT::i16)
627 return true;
Reed Kotler62de6b92014-10-11 00:55:18 +0000628 return false;
629}
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000630
Reed Kotler62de6b92014-10-11 00:55:18 +0000631// Because of how EmitCmp is called with fast-isel, you can
Reed Kotler497311a2014-10-10 17:39:51 +0000632// end up with redundant "andi" instructions after the sequences emitted below.
633// We should try and solve this issue in the future.
634//
Reed Kotlera562b462014-10-13 21:46:41 +0000635bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000636 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000637 bool IsUnsigned = CI->isUnsigned();
Reed Kotler497311a2014-10-10 17:39:51 +0000638 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
639 if (LeftReg == 0)
640 return false;
641 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
642 if (RightReg == 0)
643 return false;
Reed Kotler1f64eca2014-10-10 20:46:28 +0000644 CmpInst::Predicate P = CI->getPredicate();
Reed Kotler62de6b92014-10-11 00:55:18 +0000645
Reed Kotler1f64eca2014-10-10 20:46:28 +0000646 switch (P) {
Reed Kotler497311a2014-10-10 17:39:51 +0000647 default:
648 return false;
649 case CmpInst::ICMP_EQ: {
650 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000651 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
652 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000653 break;
654 }
655 case CmpInst::ICMP_NE: {
656 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000657 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
658 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000659 break;
660 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000661 case CmpInst::ICMP_UGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000662 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000663 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000664 case CmpInst::ICMP_ULT:
Reed Kotlera562b462014-10-13 21:46:41 +0000665 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000666 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000667 case CmpInst::ICMP_UGE: {
668 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000669 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
670 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000671 break;
672 }
673 case CmpInst::ICMP_ULE: {
674 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000675 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
676 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000677 break;
678 }
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000679 case CmpInst::ICMP_SGT:
Reed Kotlera562b462014-10-13 21:46:41 +0000680 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000681 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000682 case CmpInst::ICMP_SLT:
Reed Kotlera562b462014-10-13 21:46:41 +0000683 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
Reed Kotler497311a2014-10-10 17:39:51 +0000684 break;
Reed Kotler497311a2014-10-10 17:39:51 +0000685 case CmpInst::ICMP_SGE: {
686 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000687 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
688 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000689 break;
690 }
691 case CmpInst::ICMP_SLE: {
692 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000693 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
694 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
Reed Kotler497311a2014-10-10 17:39:51 +0000695 break;
696 }
Reed Kotler1f64eca2014-10-10 20:46:28 +0000697 case CmpInst::FCMP_OEQ:
698 case CmpInst::FCMP_UNE:
699 case CmpInst::FCMP_OLT:
700 case CmpInst::FCMP_OLE:
701 case CmpInst::FCMP_OGT:
702 case CmpInst::FCMP_OGE: {
703 if (UnsupportedFPMode)
704 return false;
705 bool IsFloat = Left->getType()->isFloatTy();
706 bool IsDouble = Left->getType()->isDoubleTy();
707 if (!IsFloat && !IsDouble)
708 return false;
709 unsigned Opc, CondMovOpc;
710 switch (P) {
711 case CmpInst::FCMP_OEQ:
712 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
713 CondMovOpc = Mips::MOVT_I;
714 break;
715 case CmpInst::FCMP_UNE:
716 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
717 CondMovOpc = Mips::MOVF_I;
718 break;
719 case CmpInst::FCMP_OLT:
720 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
721 CondMovOpc = Mips::MOVT_I;
722 break;
723 case CmpInst::FCMP_OLE:
724 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
725 CondMovOpc = Mips::MOVT_I;
726 break;
727 case CmpInst::FCMP_OGT:
728 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
729 CondMovOpc = Mips::MOVF_I;
730 break;
731 case CmpInst::FCMP_OGE:
732 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
733 CondMovOpc = Mips::MOVF_I;
734 break;
735 default:
Chandler Carruth38811cc2014-10-10 21:07:03 +0000736 llvm_unreachable("Only switching of a subset of CCs.");
Reed Kotler1f64eca2014-10-10 20:46:28 +0000737 }
738 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
739 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000740 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
741 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
Simon Dardis730fdb72017-01-16 13:55:58 +0000742 emitInst(Opc).addReg(Mips::FCC0, RegState::Define).addReg(LeftReg)
743 .addReg(RightReg);
Daniel Sandersa6cda122016-05-06 12:57:26 +0000744 emitInst(CondMovOpc, ResultReg)
745 .addReg(RegWithOne)
746 .addReg(Mips::FCC0)
747 .addReg(RegWithZero);
Reed Kotler1f64eca2014-10-10 20:46:28 +0000748 break;
749 }
Reed Kotler497311a2014-10-10 17:39:51 +0000750 }
Reed Kotler62de6b92014-10-11 00:55:18 +0000751 return true;
752}
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000753
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000754bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
755 unsigned Alignment) {
756 //
757 // more cases will be handled here in following patches.
758 //
759 unsigned Opc;
760 switch (VT.SimpleTy) {
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000761 case MVT::i32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000762 ResultReg = createResultReg(&Mips::GPR32RegClass);
763 Opc = Mips::LW;
764 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000765 case MVT::i16:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000766 ResultReg = createResultReg(&Mips::GPR32RegClass);
767 Opc = Mips::LHu;
768 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000769 case MVT::i8:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000770 ResultReg = createResultReg(&Mips::GPR32RegClass);
771 Opc = Mips::LBu;
772 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000773 case MVT::f32:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000774 if (UnsupportedFPMode)
775 return false;
776 ResultReg = createResultReg(&Mips::FGR32RegClass);
777 Opc = Mips::LWC1;
778 break;
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000779 case MVT::f64:
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000780 if (UnsupportedFPMode)
781 return false;
782 ResultReg = createResultReg(&Mips::AFGR64RegClass);
783 Opc = Mips::LDC1;
784 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000785 default:
786 return false;
787 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000788 if (Addr.isRegBase()) {
789 simplifyAddress(Addr);
790 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
791 return true;
792 }
793 if (Addr.isFIBase()) {
794 unsigned FI = Addr.getFI();
795 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000796 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000797 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000798 MachineMemOperand *MMO = MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +0000799 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000800 MFI.getObjectSize(FI), Align);
801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
802 .addFrameIndex(FI)
803 .addImm(Offset)
804 .addMemOperand(MMO);
805 return true;
806 }
807 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000808}
809
810bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
811 unsigned Alignment) {
812 //
813 // more cases will be handled here in following patches.
814 //
815 unsigned Opc;
816 switch (VT.SimpleTy) {
817 case MVT::i8:
818 Opc = Mips::SB;
819 break;
820 case MVT::i16:
821 Opc = Mips::SH;
822 break;
823 case MVT::i32:
824 Opc = Mips::SW;
825 break;
826 case MVT::f32:
827 if (UnsupportedFPMode)
828 return false;
829 Opc = Mips::SWC1;
830 break;
831 case MVT::f64:
832 if (UnsupportedFPMode)
833 return false;
834 Opc = Mips::SDC1;
835 break;
836 default:
837 return false;
838 }
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000839 if (Addr.isRegBase()) {
840 simplifyAddress(Addr);
841 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
842 return true;
843 }
844 if (Addr.isFIBase()) {
845 unsigned FI = Addr.getFI();
846 unsigned Align = 4;
Simon Dardis8ca1cbc2016-11-16 11:29:07 +0000847 int64_t Offset = Addr.getOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000848 MachineFrameInfo &MFI = MF->getFrameInfo();
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000849 MachineMemOperand *MMO = MF->getMachineMemOperand(
Simon Dardisd8bceb92016-04-29 16:07:47 +0000850 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
Reed Kotler5fb7d8b2015-02-24 02:36:45 +0000851 MFI.getObjectSize(FI), Align);
852 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
853 .addReg(SrcReg)
854 .addFrameIndex(FI)
855 .addImm(Offset)
856 .addMemOperand(MMO);
857 return true;
858 }
859 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000860}
861
Reed Kotler07d3a2f2015-03-09 16:28:10 +0000862bool MipsFastISel::selectLogicalOp(const Instruction *I) {
863 MVT VT;
864 if (!isTypeSupported(I->getType(), VT))
865 return false;
866
867 unsigned ResultReg;
868 switch (I->getOpcode()) {
869 default:
870 llvm_unreachable("Unexpected instruction.");
871 case Instruction::And:
872 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
873 break;
874 case Instruction::Or:
875 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
876 break;
877 case Instruction::Xor:
878 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
879 break;
880 }
881
882 if (!ResultReg)
883 return false;
884
885 updateValueMap(I, ResultReg);
886 return true;
887}
888
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000889bool MipsFastISel::selectLoad(const Instruction *I) {
890 // Atomic loads need special handling.
891 if (cast<LoadInst>(I)->isAtomic())
892 return false;
893
894 // Verify we have a legal type before going any further.
895 MVT VT;
896 if (!isLoadTypeLegal(I->getType(), VT))
897 return false;
898
899 // See if we can handle this address.
900 Address Addr;
901 if (!computeAddress(I->getOperand(0), Addr))
902 return false;
903
904 unsigned ResultReg;
905 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
906 return false;
907 updateValueMap(I, ResultReg);
908 return true;
909}
910
911bool MipsFastISel::selectStore(const Instruction *I) {
912 Value *Op0 = I->getOperand(0);
913 unsigned SrcReg = 0;
914
915 // Atomic stores need special handling.
916 if (cast<StoreInst>(I)->isAtomic())
917 return false;
918
919 // Verify we have a legal type before going any further.
920 MVT VT;
921 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
922 return false;
923
924 // Get the value to be stored into a register.
925 SrcReg = getRegForValue(Op0);
926 if (SrcReg == 0)
927 return false;
928
929 // See if we can handle this address.
930 Address Addr;
931 if (!computeAddress(I->getOperand(1), Addr))
932 return false;
933
934 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
935 return false;
936 return true;
937}
938
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000939// This can cause a redundant sltiu to be generated.
940// FIXME: try and eliminate this in a future patch.
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000941bool MipsFastISel::selectBranch(const Instruction *I) {
942 const BranchInst *BI = cast<BranchInst>(I);
943 MachineBasicBlock *BrBB = FuncInfo.MBB;
944 //
945 // TBB is the basic block for the case where the comparison is true.
946 // FBB is the basic block for the case where the comparison is false.
947 // if (cond) goto TBB
948 // goto FBB
949 // TBB:
950 //
951 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
952 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
953 BI->getCondition();
954 // For now, just try the simplest case where it's fed by a compare.
955 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
956 unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
957 if (!emitCmp(CondReg, CI))
958 return false;
959 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
960 .addReg(CondReg)
961 .addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000962 finishCondBranch(BI->getParent(), TBB, FBB);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000963 return true;
964 }
965 return false;
966}
Reed Kotler62de6b92014-10-11 00:55:18 +0000967
Reed Kotlera562b462014-10-13 21:46:41 +0000968bool MipsFastISel::selectCmp(const Instruction *I) {
Reed Kotler62de6b92014-10-11 00:55:18 +0000969 const CmpInst *CI = cast<CmpInst>(I);
970 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotlera562b462014-10-13 21:46:41 +0000971 if (!emitCmp(ResultReg, CI))
Reed Kotler62de6b92014-10-11 00:55:18 +0000972 return false;
Reed Kotler497311a2014-10-10 17:39:51 +0000973 updateValueMap(I, ResultReg);
974 return true;
975}
976
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000977// Attempt to fast-select a floating-point extend instruction.
978bool MipsFastISel::selectFPExt(const Instruction *I) {
979 if (UnsupportedFPMode)
980 return false;
981 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000982 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
983 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000984
985 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
986 return false;
987
988 unsigned SrcReg =
Nico Weber2cf5e892016-06-10 20:06:03 +0000989 getRegForValue(Src); // this must be a 32bit floating point register class
Reed Kotlerd4ea29e2014-10-14 18:27:58 +0000990 // maybe we should handle this differently
991 if (!SrcReg)
992 return false;
993
994 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
995 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
996 updateValueMap(I, DestReg);
997 return true;
998}
999
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001000bool MipsFastISel::selectSelect(const Instruction *I) {
1001 assert(isa<SelectInst>(I) && "Expected a select instruction.");
1002
Simon Dardisb432a3e2016-09-06 12:36:24 +00001003 DEBUG(dbgs() << "selectSelect\n");
1004
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001005 MVT VT;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001006 if (!isTypeSupported(I->getType(), VT) || UnsupportedFPMode) {
1007 DEBUG(dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001008 return false;
Simon Dardisb432a3e2016-09-06 12:36:24 +00001009 }
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001010
1011 unsigned CondMovOpc;
1012 const TargetRegisterClass *RC;
1013
1014 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
1015 CondMovOpc = Mips::MOVN_I_I;
1016 RC = &Mips::GPR32RegClass;
1017 } else if (VT == MVT::f32) {
1018 CondMovOpc = Mips::MOVN_I_S;
1019 RC = &Mips::FGR32RegClass;
1020 } else if (VT == MVT::f64) {
1021 CondMovOpc = Mips::MOVN_I_D32;
1022 RC = &Mips::AFGR64RegClass;
1023 } else
1024 return false;
1025
1026 const SelectInst *SI = cast<SelectInst>(I);
1027 const Value *Cond = SI->getCondition();
1028 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
1029 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
1030 unsigned CondReg = getRegForValue(Cond);
1031
1032 if (!Src1Reg || !Src2Reg || !CondReg)
1033 return false;
1034
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001035 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
1036 if (!ZExtCondReg)
1037 return false;
1038
1039 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true))
1040 return false;
1041
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001042 unsigned ResultReg = createResultReg(RC);
1043 unsigned TempReg = createResultReg(RC);
1044
1045 if (!ResultReg || !TempReg)
1046 return false;
1047
1048 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
1049 emitInst(CondMovOpc, ResultReg)
Vasileios Kalintiris9ec61142015-07-28 19:57:25 +00001050 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00001051 updateValueMap(I, ResultReg);
1052 return true;
1053}
1054
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001055// Attempt to fast-select a floating-point truncate instruction.
1056bool MipsFastISel::selectFPTrunc(const Instruction *I) {
1057 if (UnsupportedFPMode)
1058 return false;
1059 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001060 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1061 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001062
1063 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1064 return false;
1065
1066 unsigned SrcReg = getRegForValue(Src);
1067 if (!SrcReg)
1068 return false;
1069
1070 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1071 if (!DestReg)
1072 return false;
1073
1074 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1075 updateValueMap(I, DestReg);
1076 return true;
1077}
1078
1079// Attempt to fast-select a floating-point-to-integer conversion.
1080bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1081 if (UnsupportedFPMode)
1082 return false;
1083 MVT DstVT, SrcVT;
1084 if (!IsSigned)
1085 return false; // We don't handle this case yet. There is no native
1086 // instruction for this but it can be synthesized.
1087 Type *DstTy = I->getType();
1088 if (!isTypeLegal(DstTy, DstVT))
1089 return false;
1090
1091 if (DstVT != MVT::i32)
1092 return false;
1093
1094 Value *Src = I->getOperand(0);
1095 Type *SrcTy = Src->getType();
1096 if (!isTypeLegal(SrcTy, SrcVT))
1097 return false;
1098
1099 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1100 return false;
1101
1102 unsigned SrcReg = getRegForValue(Src);
1103 if (SrcReg == 0)
1104 return false;
1105
1106 // Determine the opcode for the conversion, which takes place
1107 // entirely within FPRs.
1108 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1109 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001110 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001111
1112 // Generate the convert.
1113 emitInst(Opc, TempReg).addReg(SrcReg);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001114 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1115
1116 updateValueMap(I, DestReg);
1117 return true;
1118}
Vasileios Kalintiris6ae1b352015-10-07 19:43:31 +00001119
Reed Kotlerd5c41962014-11-13 23:37:45 +00001120bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1121 SmallVectorImpl<MVT> &OutVTs,
1122 unsigned &NumBytes) {
1123 CallingConv::ID CC = CLI.CallConv;
1124 SmallVector<CCValAssign, 16> ArgLocs;
1125 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1126 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1127 // Get a count of how many bytes are to be pushed on the stack.
1128 NumBytes = CCInfo.getNextStackOffset();
1129 // This is the minimum argument area used for A0-A3.
1130 if (NumBytes < 16)
1131 NumBytes = 16;
1132
Serge Pavlovd526b132017-05-09 13:35:13 +00001133 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001134 // Process the args.
1135 MVT firstMVT;
1136 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1137 CCValAssign &VA = ArgLocs[i];
1138 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1139 MVT ArgVT = OutVTs[VA.getValNo()];
1140
1141 if (i == 0) {
1142 firstMVT = ArgVT;
1143 if (ArgVT == MVT::f32) {
1144 VA.convertToReg(Mips::F12);
1145 } else if (ArgVT == MVT::f64) {
1146 VA.convertToReg(Mips::D6);
1147 }
1148 } else if (i == 1) {
1149 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1150 if (ArgVT == MVT::f32) {
1151 VA.convertToReg(Mips::F14);
1152 } else if (ArgVT == MVT::f64) {
1153 VA.convertToReg(Mips::D7);
1154 }
1155 }
1156 }
Vasileios Kalintirisb48c9052015-05-12 12:29:17 +00001157 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1158 (ArgVT == MVT::i8)) &&
1159 VA.isMemLoc()) {
Reed Kotlerd5c41962014-11-13 23:37:45 +00001160 switch (VA.getLocMemOffset()) {
1161 case 0:
1162 VA.convertToReg(Mips::A0);
1163 break;
1164 case 4:
1165 VA.convertToReg(Mips::A1);
1166 break;
1167 case 8:
1168 VA.convertToReg(Mips::A2);
1169 break;
1170 case 12:
1171 VA.convertToReg(Mips::A3);
1172 break;
1173 default:
1174 break;
1175 }
1176 }
1177 unsigned ArgReg = getRegForValue(ArgVal);
1178 if (!ArgReg)
1179 return false;
1180
1181 // Handle arg promotion: SExt, ZExt, AExt.
1182 switch (VA.getLocInfo()) {
1183 case CCValAssign::Full:
1184 break;
1185 case CCValAssign::AExt:
1186 case CCValAssign::SExt: {
1187 MVT DestVT = VA.getLocVT();
1188 MVT SrcVT = ArgVT;
1189 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1190 if (!ArgReg)
1191 return false;
1192 break;
1193 }
1194 case CCValAssign::ZExt: {
1195 MVT DestVT = VA.getLocVT();
1196 MVT SrcVT = ArgVT;
1197 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1198 if (!ArgReg)
1199 return false;
1200 break;
1201 }
1202 default:
1203 llvm_unreachable("Unknown arg promotion!");
1204 }
1205
1206 // Now copy/store arg to correct locations.
1207 if (VA.isRegLoc() && !VA.needsCustom()) {
1208 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1209 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1210 CLI.OutRegs.push_back(VA.getLocReg());
1211 } else if (VA.needsCustom()) {
1212 llvm_unreachable("Mips does not use custom args.");
1213 return false;
1214 } else {
1215 //
1216 // FIXME: This path will currently return false. It was copied
1217 // from the AArch64 port and should be essentially fine for Mips too.
1218 // The work to finish up this path will be done in a follow-on patch.
1219 //
1220 assert(VA.isMemLoc() && "Assuming store on stack.");
1221 // Don't emit stores for undef values.
1222 if (isa<UndefValue>(ArgVal))
1223 continue;
1224
1225 // Need to store on the stack.
1226 // FIXME: This alignment is incorrect but this path is disabled
1227 // for now (will return false). We need to determine the right alignment
1228 // based on the normal alignment for the underlying machine type.
1229 //
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001230 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001231
1232 unsigned BEAlign = 0;
1233 if (ArgSize < 8 && !Subtarget->isLittle())
1234 BEAlign = 8 - ArgSize;
1235
1236 Address Addr;
1237 Addr.setKind(Address::RegBase);
1238 Addr.setReg(Mips::SP);
1239 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1240
1241 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1242 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00001243 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
Reed Kotlerd5c41962014-11-13 23:37:45 +00001244 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1245 (void)(MMO);
1246 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1247 return false; // can't store on the stack yet.
1248 }
1249 }
1250
1251 return true;
1252}
1253
1254bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1255 unsigned NumBytes) {
1256 CallingConv::ID CC = CLI.CallConv;
Daniel Sanders01bcefd2016-05-03 14:19:26 +00001257 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001258 if (RetVT != MVT::isVoid) {
1259 SmallVector<CCValAssign, 16> RVLocs;
Simon Dardis70f79252017-04-26 11:10:38 +00001260 MipsCCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1261
1262 CCInfo.AnalyzeCallResult(CLI.Ins, RetCC_Mips, CLI.RetTy,
Simon Dardis9d580e82017-04-29 16:31:40 +00001263 CLI.Symbol ? CLI.Symbol->getName().data()
1264 : nullptr);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001265
1266 // Only handle a single return value.
1267 if (RVLocs.size() != 1)
1268 return false;
1269 // Copy all of the result registers out of their specified physreg.
1270 MVT CopyVT = RVLocs[0].getValVT();
1271 // Special handling for extended integers.
1272 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1273 CopyVT = MVT::i32;
1274
1275 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001276 if (!ResultReg)
1277 return false;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1279 TII.get(TargetOpcode::COPY),
1280 ResultReg).addReg(RVLocs[0].getLocReg());
1281 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1282
1283 CLI.ResultReg = ResultReg;
1284 CLI.NumResultRegs = 1;
1285 }
1286 return true;
1287}
1288
Daniel Sanderscbaca422016-07-29 12:27:28 +00001289bool MipsFastISel::fastLowerArguments() {
1290 DEBUG(dbgs() << "fastLowerArguments\n");
1291
1292 if (!FuncInfo.CanLowerReturn) {
1293 DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1294 return false;
1295 }
1296
1297 const Function *F = FuncInfo.Fn;
1298 if (F->isVarArg()) {
1299 DEBUG(dbgs() << ".. gave up (varargs)\n");
1300 return false;
1301 }
1302
1303 CallingConv::ID CC = F->getCallingConv();
1304 if (CC != CallingConv::C) {
1305 DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1306 return false;
1307 }
1308
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001309 const ArrayRef<MCPhysReg> GPR32ArgRegs = {Mips::A0, Mips::A1, Mips::A2,
1310 Mips::A3};
1311 const ArrayRef<MCPhysReg> FGR32ArgRegs = {Mips::F12, Mips::F14};
1312 const ArrayRef<MCPhysReg> AFGR64ArgRegs = {Mips::D6, Mips::D7};
1313 ArrayRef<MCPhysReg>::iterator NextGPR32 = GPR32ArgRegs.begin();
1314 ArrayRef<MCPhysReg>::iterator NextFGR32 = FGR32ArgRegs.begin();
1315 ArrayRef<MCPhysReg>::iterator NextAFGR64 = AFGR64ArgRegs.begin();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001316
1317 struct AllocatedReg {
1318 const TargetRegisterClass *RC;
1319 unsigned Reg;
1320 AllocatedReg(const TargetRegisterClass *RC, unsigned Reg)
1321 : RC(RC), Reg(Reg) {}
1322 };
1323
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001324 // Only handle simple cases. i.e. All arguments are directly mapped to
1325 // registers of the appropriate type.
Daniel Sanderscbaca422016-07-29 12:27:28 +00001326 SmallVector<AllocatedReg, 4> Allocation;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001327 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001328 if (FormalArg.hasAttribute(Attribute::InReg) ||
1329 FormalArg.hasAttribute(Attribute::StructRet) ||
1330 FormalArg.hasAttribute(Attribute::ByVal)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001331 DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1332 return false;
1333 }
1334
1335 Type *ArgTy = FormalArg.getType();
1336 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) {
1337 DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1338 return false;
1339 }
1340
1341 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Reid Kleckner6652a522017-04-28 18:37:16 +00001342 DEBUG(dbgs() << ".. " << FormalArg.getArgNo() << ": "
1343 << ArgVT.getEVTString() << "\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001344 if (!ArgVT.isSimple()) {
1345 DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1346 return false;
1347 }
1348
1349 switch (ArgVT.getSimpleVT().SimpleTy) {
1350 case MVT::i1:
1351 case MVT::i8:
1352 case MVT::i16:
Reid Kleckner6652a522017-04-28 18:37:16 +00001353 if (!FormalArg.hasAttribute(Attribute::SExt) &&
1354 !FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001355 // It must be any extend, this shouldn't happen for clang-generated IR
1356 // so just fall back on SelectionDAG.
1357 DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1358 return false;
1359 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001360
1361 if (NextGPR32 == GPR32ArgRegs.end()) {
1362 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1363 return false;
1364 }
1365
1366 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1367 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1368
1369 // Allocating any GPR32 prohibits further use of floating point arguments.
1370 NextFGR32 = FGR32ArgRegs.end();
1371 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001372 break;
1373
1374 case MVT::i32:
Reid Kleckner6652a522017-04-28 18:37:16 +00001375 if (FormalArg.hasAttribute(Attribute::ZExt)) {
Daniel Sanderscbaca422016-07-29 12:27:28 +00001376 // The O32 ABI does not permit a zero-extended i32.
1377 DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1378 return false;
1379 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001380
1381 if (NextGPR32 == GPR32ArgRegs.end()) {
1382 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1383 return false;
1384 }
1385
1386 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1387 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1388
1389 // Allocating any GPR32 prohibits further use of floating point arguments.
1390 NextFGR32 = FGR32ArgRegs.end();
1391 NextAFGR64 = AFGR64ArgRegs.end();
Daniel Sanderscbaca422016-07-29 12:27:28 +00001392 break;
1393
1394 case MVT::f32:
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001395 if (UnsupportedFPMode) {
1396 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1397 return false;
1398 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001399 if (NextFGR32 == FGR32ArgRegs.end()) {
1400 DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001401 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001402 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001403 DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32 << ")\n");
1404 Allocation.emplace_back(&Mips::FGR32RegClass, *NextFGR32++);
1405 // Allocating an FGR32 also allocates the super-register AFGR64, and
1406 // ABI rules require us to skip the corresponding GPR32.
1407 if (NextGPR32 != GPR32ArgRegs.end())
1408 NextGPR32++;
1409 if (NextAFGR64 != AFGR64ArgRegs.end())
1410 NextAFGR64++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001411 break;
1412
1413 case MVT::f64:
Simon Dardisb432a3e2016-09-06 12:36:24 +00001414 if (UnsupportedFPMode) {
Simon Dardis86b3a1e2016-10-04 10:35:07 +00001415 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
Simon Dardisb432a3e2016-09-06 12:36:24 +00001416 return false;
1417 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001418 if (NextAFGR64 == AFGR64ArgRegs.end()) {
1419 DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
Daniel Sanderscbaca422016-07-29 12:27:28 +00001420 return false;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001421 }
Daniel Sandersb3ae33c2016-08-01 15:32:51 +00001422 DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64 << ")\n");
1423 Allocation.emplace_back(&Mips::AFGR64RegClass, *NextAFGR64++);
1424 // Allocating an FGR32 also allocates the super-register AFGR64, and
1425 // ABI rules require us to skip the corresponding GPR32 pair.
1426 if (NextGPR32 != GPR32ArgRegs.end())
1427 NextGPR32++;
1428 if (NextGPR32 != GPR32ArgRegs.end())
1429 NextGPR32++;
1430 if (NextFGR32 != FGR32ArgRegs.end())
1431 NextFGR32++;
Daniel Sanderscbaca422016-07-29 12:27:28 +00001432 break;
1433
1434 default:
1435 DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1436 return false;
1437 }
Daniel Sanderscbaca422016-07-29 12:27:28 +00001438 }
1439
Daniel Sanderscbaca422016-07-29 12:27:28 +00001440 for (const auto &FormalArg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001441 unsigned ArgNo = FormalArg.getArgNo();
1442 unsigned SrcReg = Allocation[ArgNo].Reg;
1443 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001444 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1445 // Without this, EmitLiveInCopies may eliminate the livein if its only
1446 // use is a bitcast (which isn't turned into an instruction).
Reid Kleckner6652a522017-04-28 18:37:16 +00001447 unsigned ResultReg = createResultReg(Allocation[ArgNo].RC);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1449 TII.get(TargetOpcode::COPY), ResultReg)
1450 .addReg(DstReg, getKillRegState(true));
1451 updateValueMap(&FormalArg, ResultReg);
Daniel Sanderscbaca422016-07-29 12:27:28 +00001452 }
1453
1454 // Calculate the size of the incoming arguments area.
1455 // We currently reject all the cases where this would be non-zero.
1456 unsigned IncomingArgSizeInBytes = 0;
1457
1458 // Account for the reserved argument area on ABI's that have one (O32).
1459 // It seems strange to do this on the caller side but it's necessary in
1460 // SelectionDAG's implementation.
1461 IncomingArgSizeInBytes = std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC),
1462 IncomingArgSizeInBytes);
1463
1464 MF->getInfo<MipsFunctionInfo>()->setFormalArgInfo(IncomingArgSizeInBytes,
1465 false);
1466
1467 return true;
1468}
1469
Reed Kotlerd5c41962014-11-13 23:37:45 +00001470bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1471 CallingConv::ID CC = CLI.CallConv;
1472 bool IsTailCall = CLI.IsTailCall;
1473 bool IsVarArg = CLI.IsVarArg;
1474 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001475 MCSymbol *Symbol = CLI.Symbol;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001476
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001477 // Do not handle FastCC.
1478 if (CC == CallingConv::Fast)
1479 return false;
1480
Reed Kotlerd5c41962014-11-13 23:37:45 +00001481 // Allow SelectionDAG isel to handle tail calls.
1482 if (IsTailCall)
1483 return false;
1484
1485 // Let SDISel handle vararg functions.
1486 if (IsVarArg)
1487 return false;
1488
1489 // FIXME: Only handle *simple* calls for now.
1490 MVT RetVT;
1491 if (CLI.RetTy->isVoidTy())
1492 RetVT = MVT::isVoid;
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001493 else if (!isTypeSupported(CLI.RetTy, RetVT))
Reed Kotlerd5c41962014-11-13 23:37:45 +00001494 return false;
1495
1496 for (auto Flag : CLI.OutFlags)
1497 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1498 return false;
1499
1500 // Set up the argument vectors.
1501 SmallVector<MVT, 16> OutVTs;
1502 OutVTs.reserve(CLI.OutVals.size());
1503
1504 for (auto *Val : CLI.OutVals) {
1505 MVT VT;
1506 if (!isTypeLegal(Val->getType(), VT) &&
1507 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1508 return false;
1509
1510 // We don't handle vector parameters yet.
1511 if (VT.isVector() || VT.getSizeInBits() > 64)
1512 return false;
1513
1514 OutVTs.push_back(VT);
1515 }
1516
1517 Address Addr;
1518 if (!computeCallAddress(Callee, Addr))
1519 return false;
1520
1521 // Handle the arguments now that we've gotten them.
1522 unsigned NumBytes;
1523 if (!processCallArgs(CLI, OutVTs, NumBytes))
1524 return false;
1525
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001526 if (!Addr.getGlobalValue())
1527 return false;
1528
Reed Kotlerd5c41962014-11-13 23:37:45 +00001529 // Issue the call.
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001530 unsigned DestAddress;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001531 if (Symbol)
1532 DestAddress = materializeExternalCallSym(Symbol);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001533 else
1534 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
Reed Kotlerd5c41962014-11-13 23:37:45 +00001535 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1536 MachineInstrBuilder MIB =
1537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1538 Mips::RA).addReg(Mips::T9);
1539
1540 // Add implicit physical register uses to the call.
1541 for (auto Reg : CLI.OutRegs)
1542 MIB.addReg(Reg, RegState::Implicit);
1543
1544 // Add a register mask with the call-preserved registers.
1545 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001546 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Reed Kotlerd5c41962014-11-13 23:37:45 +00001547
1548 CLI.Call = MIB;
1549
Reed Kotlerd5c41962014-11-13 23:37:45 +00001550 // Finish off the call including any return values.
1551 return finishCall(CLI, RetVT, NumBytes);
1552}
1553
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001554bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1555 switch (II->getIntrinsicID()) {
1556 default:
1557 return false;
Vasileios Kalintiriscbbf8e02015-06-01 16:40:45 +00001558 case Intrinsic::bswap: {
1559 Type *RetTy = II->getCalledFunction()->getReturnType();
1560
1561 MVT VT;
1562 if (!isTypeSupported(RetTy, VT))
1563 return false;
1564
1565 unsigned SrcReg = getRegForValue(II->getOperand(0));
1566 if (SrcReg == 0)
1567 return false;
1568 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1569 if (DestReg == 0)
1570 return false;
1571 if (VT == MVT::i16) {
1572 if (Subtarget->hasMips32r2()) {
1573 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1574 updateValueMap(II, DestReg);
1575 return true;
1576 } else {
1577 unsigned TempReg[3];
1578 for (int i = 0; i < 3; i++) {
1579 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1580 if (TempReg[i] == 0)
1581 return false;
1582 }
1583 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1584 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1585 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1586 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1587 updateValueMap(II, DestReg);
1588 return true;
1589 }
1590 } else if (VT == MVT::i32) {
1591 if (Subtarget->hasMips32r2()) {
1592 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1593 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1594 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1595 updateValueMap(II, DestReg);
1596 return true;
1597 } else {
1598 unsigned TempReg[8];
1599 for (int i = 0; i < 8; i++) {
1600 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1601 if (TempReg[i] == 0)
1602 return false;
1603 }
1604
1605 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1606 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1607 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1608 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1609
1610 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1611 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1612
1613 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1614 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1615 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1616 updateValueMap(II, DestReg);
1617 return true;
1618 }
1619 }
1620 return false;
1621 }
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001622 case Intrinsic::memcpy:
1623 case Intrinsic::memmove: {
1624 const auto *MTI = cast<MemTransferInst>(II);
1625 // Don't handle volatile.
1626 if (MTI->isVolatile())
1627 return false;
1628 if (!MTI->getLength()->getType()->isIntegerTy(32))
1629 return false;
1630 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
Daniel Neilson1e687242018-01-19 17:13:12 +00001631 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 1);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001632 }
1633 case Intrinsic::memset: {
1634 const MemSetInst *MSI = cast<MemSetInst>(II);
1635 // Don't handle volatile.
1636 if (MSI->isVolatile())
1637 return false;
1638 if (!MSI->getLength()->getType()->isIntegerTy(32))
1639 return false;
Daniel Neilson1e687242018-01-19 17:13:12 +00001640 return lowerCallTo(II, "memset", II->getNumArgOperands() - 1);
Vasileios Kalintirisbdb91b32015-06-01 16:36:01 +00001641 }
1642 }
1643 return false;
1644}
1645
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001646bool MipsFastISel::selectRet(const Instruction *I) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001647 const Function &F = *I->getParent()->getParent();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001648 const ReturnInst *Ret = cast<ReturnInst>(I);
1649
Simon Dardisb432a3e2016-09-06 12:36:24 +00001650 DEBUG(dbgs() << "selectRet\n");
1651
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001652 if (!FuncInfo.CanLowerReturn)
1653 return false;
Reed Kotleraa150ed2015-02-12 21:05:12 +00001654
1655 // Build a list of return value registers.
1656 SmallVector<unsigned, 4> RetRegs;
1657
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001658 if (Ret->getNumOperands() > 0) {
Reed Kotleraa150ed2015-02-12 21:05:12 +00001659 CallingConv::ID CC = F.getCallingConv();
Vasileios Kalintiris98769462015-07-28 21:43:31 +00001660
1661 // Do not handle FastCC.
1662 if (CC == CallingConv::Fast)
1663 return false;
1664
Reed Kotleraa150ed2015-02-12 21:05:12 +00001665 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001666 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1667
Reed Kotleraa150ed2015-02-12 21:05:12 +00001668 // Analyze operands of the call, assigning locations to each operand.
1669 SmallVector<CCValAssign, 16> ValLocs;
1670 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1671 I->getContext());
1672 CCAssignFn *RetCC = RetCC_Mips;
1673 CCInfo.AnalyzeReturn(Outs, RetCC);
1674
1675 // Only handle a single return value for now.
1676 if (ValLocs.size() != 1)
1677 return false;
1678
1679 CCValAssign &VA = ValLocs[0];
1680 const Value *RV = Ret->getOperand(0);
1681
1682 // Don't bother handling odd stuff for now.
1683 if ((VA.getLocInfo() != CCValAssign::Full) &&
1684 (VA.getLocInfo() != CCValAssign::BCvt))
1685 return false;
1686
1687 // Only handle register returns for now.
1688 if (!VA.isRegLoc())
1689 return false;
1690
1691 unsigned Reg = getRegForValue(RV);
1692 if (Reg == 0)
1693 return false;
1694
1695 unsigned SrcReg = Reg + VA.getValNo();
1696 unsigned DestReg = VA.getLocReg();
1697 // Avoid a cross-class copy. This is very unlikely.
1698 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1699 return false;
1700
Mehdi Amini44ede332015-07-09 02:09:04 +00001701 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Reed Kotleraa150ed2015-02-12 21:05:12 +00001702 if (!RVEVT.isSimple())
1703 return false;
1704
1705 if (RVEVT.isVector())
1706 return false;
1707
1708 MVT RVVT = RVEVT.getSimpleVT();
1709 if (RVVT == MVT::f128)
1710 return false;
1711
Simon Dardisb432a3e2016-09-06 12:36:24 +00001712 // Do not handle FGR64 returns for now.
1713 if (RVVT == MVT::f64 && UnsupportedFPMode) {
1714 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1715 return false;
1716 }
1717
Reed Kotleraa150ed2015-02-12 21:05:12 +00001718 MVT DestVT = VA.getValVT();
1719 // Special handling for extended integers.
1720 if (RVVT != DestVT) {
1721 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1722 return false;
1723
Vasileios Kalintiris1249e742015-04-29 14:17:14 +00001724 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1725 bool IsZExt = Outs[0].Flags.isZExt();
1726 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1727 if (SrcReg == 0)
1728 return false;
1729 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001730 }
1731
1732 // Make the copy.
1733 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1734 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1735
1736 // Add register to return instruction.
1737 RetRegs.push_back(VA.getLocReg());
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001738 }
Reed Kotleraa150ed2015-02-12 21:05:12 +00001739 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1740 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1741 MIB.addReg(RetRegs[i], RegState::Implicit);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001742 return true;
1743}
1744
1745bool MipsFastISel::selectTrunc(const Instruction *I) {
1746 // The high bits for a type smaller than the register size are assumed to be
1747 // undefined.
1748 Value *Op = I->getOperand(0);
1749
1750 EVT SrcVT, DestVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001751 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1752 DestVT = TLI.getValueType(DL, I->getType(), true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001753
1754 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1755 return false;
1756 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1757 return false;
1758
1759 unsigned SrcReg = getRegForValue(Op);
1760 if (!SrcReg)
1761 return false;
1762
1763 // Because the high bits are undefined, a truncate doesn't generate
1764 // any code.
1765 updateValueMap(I, SrcReg);
1766 return true;
1767}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001768
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001769bool MipsFastISel::selectIntExt(const Instruction *I) {
1770 Type *DestTy = I->getType();
1771 Value *Src = I->getOperand(0);
1772 Type *SrcTy = Src->getType();
1773
1774 bool isZExt = isa<ZExtInst>(I);
1775 unsigned SrcReg = getRegForValue(Src);
1776 if (!SrcReg)
1777 return false;
1778
1779 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001780 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1781 DestEVT = TLI.getValueType(DL, DestTy, true);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001782 if (!SrcEVT.isSimple())
1783 return false;
1784 if (!DestEVT.isSimple())
1785 return false;
1786
1787 MVT SrcVT = SrcEVT.getSimpleVT();
1788 MVT DestVT = DestEVT.getSimpleVT();
1789 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1790
1791 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1792 return false;
1793 updateValueMap(I, ResultReg);
1794 return true;
1795}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00001796
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001797bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1798 unsigned DestReg) {
1799 unsigned ShiftAmt;
1800 switch (SrcVT.SimpleTy) {
1801 default:
1802 return false;
1803 case MVT::i8:
1804 ShiftAmt = 24;
1805 break;
1806 case MVT::i16:
1807 ShiftAmt = 16;
1808 break;
1809 }
1810 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1811 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1812 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1813 return true;
1814}
1815
1816bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1817 unsigned DestReg) {
1818 switch (SrcVT.SimpleTy) {
1819 default:
1820 return false;
1821 case MVT::i8:
1822 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1823 break;
1824 case MVT::i16:
1825 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1826 break;
1827 }
1828 return true;
1829}
1830
1831bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1832 unsigned DestReg) {
1833 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1834 return false;
1835 if (Subtarget->hasMips32r2())
1836 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1837 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1838}
1839
1840bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1841 unsigned DestReg) {
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001842 int64_t Imm;
1843
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001844 switch (SrcVT.SimpleTy) {
1845 default:
1846 return false;
1847 case MVT::i1:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001848 Imm = 1;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001849 break;
1850 case MVT::i8:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001851 Imm = 0xff;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001852 break;
1853 case MVT::i16:
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001854 Imm = 0xffff;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001855 break;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001856 }
Vasileios Kalintirisb876b582015-10-07 20:06:30 +00001857
1858 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm);
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001859 return true;
1860}
1861
1862bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1863 unsigned DestReg, bool IsZExt) {
Vasileios Kalintiris1202f362015-04-24 13:48:19 +00001864 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1865 // DestVT are odd things, so test to make sure that they are both types we can
1866 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1867 // bail out to SelectionDAG.
1868 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1869 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1870 return false;
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00001871 if (IsZExt)
1872 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1873 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1874}
Reed Kotlerd5c41962014-11-13 23:37:45 +00001875
1876unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1877 bool isZExt) {
1878 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
Reed Kotleraa150ed2015-02-12 21:05:12 +00001879 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1880 return Success ? DestReg : 0;
Reed Kotlerd5c41962014-11-13 23:37:45 +00001881}
1882
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001883bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001884 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00001885 if (!DestEVT.isSimple())
1886 return false;
1887
1888 MVT DestVT = DestEVT.getSimpleVT();
1889 if (DestVT != MVT::i32)
1890 return false;
1891
1892 unsigned DivOpc;
1893 switch (ISDOpcode) {
1894 default:
1895 return false;
1896 case ISD::SDIV:
1897 case ISD::SREM:
1898 DivOpc = Mips::SDIV;
1899 break;
1900 case ISD::UDIV:
1901 case ISD::UREM:
1902 DivOpc = Mips::UDIV;
1903 break;
1904 }
1905
1906 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1907 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1908 if (!Src0Reg || !Src1Reg)
1909 return false;
1910
1911 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1912 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1913
1914 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1915 if (!ResultReg)
1916 return false;
1917
1918 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1919 ? Mips::MFHI
1920 : Mips::MFLO;
1921 emitInst(MFOpc, ResultReg);
1922
1923 updateValueMap(I, ResultReg);
1924 return true;
1925}
1926
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001927bool MipsFastISel::selectShift(const Instruction *I) {
1928 MVT RetVT;
1929
1930 if (!isTypeSupported(I->getType(), RetVT))
1931 return false;
1932
1933 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1934 if (!ResultReg)
1935 return false;
1936
1937 unsigned Opcode = I->getOpcode();
1938 const Value *Op0 = I->getOperand(0);
1939 unsigned Op0Reg = getRegForValue(Op0);
1940 if (!Op0Reg)
1941 return false;
1942
1943 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1944 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1945 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1946 if (!TempReg)
1947 return false;
1948
Mehdi Amini44ede332015-07-09 02:09:04 +00001949 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00001950 bool IsZExt = Opcode == Instruction::LShr;
1951 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1952 return false;
1953
1954 Op0Reg = TempReg;
1955 }
1956
1957 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1958 uint64_t ShiftVal = C->getZExtValue();
1959
1960 switch (Opcode) {
1961 default:
1962 llvm_unreachable("Unexpected instruction.");
1963 case Instruction::Shl:
1964 Opcode = Mips::SLL;
1965 break;
1966 case Instruction::AShr:
1967 Opcode = Mips::SRA;
1968 break;
1969 case Instruction::LShr:
1970 Opcode = Mips::SRL;
1971 break;
1972 }
1973
1974 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1975 updateValueMap(I, ResultReg);
1976 return true;
1977 }
1978
1979 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1980 if (!Op1Reg)
1981 return false;
1982
1983 switch (Opcode) {
1984 default:
1985 llvm_unreachable("Unexpected instruction.");
1986 case Instruction::Shl:
1987 Opcode = Mips::SLLV;
1988 break;
1989 case Instruction::AShr:
1990 Opcode = Mips::SRAV;
1991 break;
1992 case Instruction::LShr:
1993 Opcode = Mips::SRLV;
1994 break;
1995 }
1996
1997 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1998 updateValueMap(I, ResultReg);
1999 return true;
2000}
2001
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002002bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
Reed Kotler67077b32014-04-29 17:57:50 +00002003 switch (I->getOpcode()) {
2004 default:
2005 break;
Reed Kotler9fe3bfd2014-06-16 22:05:47 +00002006 case Instruction::Load:
Reed Kotlera562b462014-10-13 21:46:41 +00002007 return selectLoad(I);
Reed Kotlerbab3f232014-05-01 20:39:21 +00002008 case Instruction::Store:
Reed Kotlera562b462014-10-13 21:46:41 +00002009 return selectStore(I);
Vasileios Kalintiris8fcb3982015-06-01 16:17:37 +00002010 case Instruction::SDiv:
2011 if (!selectBinaryOp(I, ISD::SDIV))
2012 return selectDivRem(I, ISD::SDIV);
2013 return true;
2014 case Instruction::UDiv:
2015 if (!selectBinaryOp(I, ISD::UDIV))
2016 return selectDivRem(I, ISD::UDIV);
2017 return true;
2018 case Instruction::SRem:
2019 if (!selectBinaryOp(I, ISD::SREM))
2020 return selectDivRem(I, ISD::SREM);
2021 return true;
2022 case Instruction::URem:
2023 if (!selectBinaryOp(I, ISD::UREM))
2024 return selectDivRem(I, ISD::UREM);
2025 return true;
Vasileios Kalintiris7a6b1872015-04-27 13:28:05 +00002026 case Instruction::Shl:
2027 case Instruction::LShr:
2028 case Instruction::AShr:
2029 return selectShift(I);
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002030 case Instruction::And:
2031 case Instruction::Or:
2032 case Instruction::Xor:
2033 return selectLogicalOp(I);
Reed Kotler62de6b92014-10-11 00:55:18 +00002034 case Instruction::Br:
Reed Kotlera562b462014-10-13 21:46:41 +00002035 return selectBranch(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002036 case Instruction::Ret:
Reed Kotlera562b462014-10-13 21:46:41 +00002037 return selectRet(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002038 case Instruction::Trunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002039 return selectTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002040 case Instruction::ZExt:
2041 case Instruction::SExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002042 return selectIntExt(I);
Reed Kotlerb9dc2482014-10-01 18:47:02 +00002043 case Instruction::FPTrunc:
Reed Kotlera562b462014-10-13 21:46:41 +00002044 return selectFPTrunc(I);
Reed Kotler3ebdcc92014-09-30 16:30:13 +00002045 case Instruction::FPExt:
Reed Kotlera562b462014-10-13 21:46:41 +00002046 return selectFPExt(I);
Reed Kotler12f94882014-10-10 17:00:46 +00002047 case Instruction::FPToSI:
Reed Kotlera562b462014-10-13 21:46:41 +00002048 return selectFPToInt(I, /*isSigned*/ true);
Reed Kotler12f94882014-10-10 17:00:46 +00002049 case Instruction::FPToUI:
Reed Kotlera562b462014-10-13 21:46:41 +00002050 return selectFPToInt(I, /*isSigned*/ false);
Reed Kotler497311a2014-10-10 17:39:51 +00002051 case Instruction::ICmp:
2052 case Instruction::FCmp:
Reed Kotlera562b462014-10-13 21:46:41 +00002053 return selectCmp(I);
Vasileios Kalintiris127f8942015-06-01 15:56:40 +00002054 case Instruction::Select:
2055 return selectSelect(I);
Reed Kotler67077b32014-04-29 17:57:50 +00002056 }
2057 return false;
2058}
Reed Kotler720c5ca2014-04-17 22:15:34 +00002059
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002060unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
2061 bool IsUnsigned) {
2062 unsigned VReg = getRegForValue(V);
2063 if (VReg == 0)
Reed Kotler12f94882014-10-10 17:00:46 +00002064 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00002065 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002066 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
2067 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
2068 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
2069 return 0;
2070 VReg = TempReg;
Reed Kotler063d4fb2014-06-10 16:45:44 +00002071 }
Reed Kotlerd4ea29e2014-10-14 18:27:58 +00002072 return VReg;
Reed Kotlerbab3f232014-05-01 20:39:21 +00002073}
2074
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002075void MipsFastISel::simplifyAddress(Address &Addr) {
2076 if (!isInt<16>(Addr.getOffset())) {
2077 unsigned TempReg =
Reed Kotler07d3a2f2015-03-09 16:28:10 +00002078 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
Reed Kotler5fb7d8b2015-02-24 02:36:45 +00002079 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
2080 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
2081 Addr.setReg(DestReg);
2082 Addr.setOffset(0);
2083 }
2084}
2085
Vasileios Kalintiris7f680e12015-06-01 15:48:09 +00002086unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2087 const TargetRegisterClass *RC,
2088 unsigned Op0, bool Op0IsKill,
2089 unsigned Op1, bool Op1IsKill) {
2090 // We treat the MUL instruction in a special way because it clobbers
2091 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2092 // mark these registers only as implicitly defined. As a result, the
2093 // register allocator runs out of registers when this instruction is
2094 // followed by another instruction that defines the same registers too.
2095 // We can fix this by explicitly marking those registers as dead.
2096 if (MachineInstOpcode == Mips::MUL) {
2097 unsigned ResultReg = createResultReg(RC);
2098 const MCInstrDesc &II = TII.get(MachineInstOpcode);
2099 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
2100 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
2101 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
2102 .addReg(Op0, getKillRegState(Op0IsKill))
2103 .addReg(Op1, getKillRegState(Op1IsKill))
2104 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
2105 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
2106 return ResultReg;
2107 }
2108
2109 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
2110 Op1IsKill);
2111}
2112
Reed Kotler720c5ca2014-04-17 22:15:34 +00002113namespace llvm {
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002114
Reed Kotler720c5ca2014-04-17 22:15:34 +00002115FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
2116 const TargetLibraryInfo *libInfo) {
2117 return new MipsFastISel(funcInfo, libInfo);
2118}
Eugene Zelenkodde94e42017-01-30 23:21:32 +00002119
2120} // end namespace llvm