blob: 9a60aa7b9f4b96f0dfcb5d661cdfb7f3b4a832a9 [file] [log] [blame]
Reed Kotler720c5ca2014-04-17 22:15:34 +00001//===-- MipsastISel.cpp - Mips FastISel implementation
2//---------------------===//
3
4#include "llvm/CodeGen/FunctionLoweringInfo.h"
5#include "llvm/CodeGen/FastISel.h"
Reed Kotler67077b32014-04-29 17:57:50 +00006#include "llvm/CodeGen/MachineInstrBuilder.h"
Reed Kotlerbab3f232014-05-01 20:39:21 +00007#include "llvm/IR/GlobalAlias.h"
8#include "llvm/IR/GlobalVariable.h"
Reed Kotler67077b32014-04-29 17:57:50 +00009#include "llvm/Target/TargetInstrInfo.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000010#include "llvm/Target/TargetLibraryInfo.h"
Reed Kotlerbab3f232014-05-01 20:39:21 +000011#include "MipsRegisterInfo.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000012#include "MipsISelLowering.h"
Reed Kotler67077b32014-04-29 17:57:50 +000013#include "MipsMachineFunction.h"
14#include "MipsSubtarget.h"
Reed Kotler9fe25f32014-06-08 02:08:43 +000015#include "MipsTargetMachine.h"
Reed Kotler720c5ca2014-04-17 22:15:34 +000016
17using namespace llvm;
18
19namespace {
20
Reed Kotlerbab3f232014-05-01 20:39:21 +000021// All possible address modes.
22typedef struct Address {
23 enum { RegBase, FrameIndexBase } BaseType;
24
25 union {
26 unsigned Reg;
27 int FI;
28 } Base;
29
30 int64_t Offset;
31
32 // Innocuous defaults for our address.
33 Address() : BaseType(RegBase), Offset(0) { Base.Reg = 0; }
34} Address;
35
Reed Kotler720c5ca2014-04-17 22:15:34 +000036class MipsFastISel final : public FastISel {
37
Reed Kotler67077b32014-04-29 17:57:50 +000038 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
39 /// make the right decision when generating code for different targets.
Reed Kotler67077b32014-04-29 17:57:50 +000040 Module &M;
41 const TargetMachine &TM;
42 const TargetInstrInfo &TII;
43 const TargetLowering &TLI;
Reed Kotler9fe25f32014-06-08 02:08:43 +000044 const MipsSubtarget &Subtarget;
Reed Kotler67077b32014-04-29 17:57:50 +000045 MipsFunctionInfo *MFI;
46
47 // Convenience variables to avoid some queries.
48 LLVMContext *Context;
49
50 bool TargetSupported;
51
Reed Kotler720c5ca2014-04-17 22:15:34 +000052public:
53 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
54 const TargetLibraryInfo *libInfo)
Reed Kotler67077b32014-04-29 17:57:50 +000055 : FastISel(funcInfo, libInfo),
56 M(const_cast<Module &>(*funcInfo.Fn->getParent())),
57 TM(funcInfo.MF->getTarget()), TII(*TM.getInstrInfo()),
Reed Kotler9fe25f32014-06-08 02:08:43 +000058 TLI(*TM.getTargetLowering()),
59 Subtarget(TM.getSubtarget<MipsSubtarget>()) {
Reed Kotler67077b32014-04-29 17:57:50 +000060 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
61 Context = &funcInfo.Fn->getContext();
Reed Kotler9fe25f32014-06-08 02:08:43 +000062 TargetSupported = ((Subtarget.getRelocationModel() == Reloc::PIC_) &&
63 (Subtarget.hasMips32r2() && (Subtarget.isABI_O32())));
Reed Kotler67077b32014-04-29 17:57:50 +000064 }
65
66 bool TargetSelectInstruction(const Instruction *I) override;
Reed Kotlerbab3f232014-05-01 20:39:21 +000067 unsigned TargetMaterializeConstant(const Constant *C) override;
Reed Kotler67077b32014-04-29 17:57:50 +000068
Reed Kotlerbab3f232014-05-01 20:39:21 +000069 bool ComputeAddress(const Value *Obj, Address &Addr);
70
71private:
72 bool EmitStore(MVT VT, unsigned SrcReg, Address &Addr,
73 unsigned Alignment = 0);
Reed Kotler67077b32014-04-29 17:57:50 +000074 bool SelectRet(const Instruction *I);
Reed Kotlerbab3f232014-05-01 20:39:21 +000075 bool SelectStore(const Instruction *I);
76
77 bool isTypeLegal(Type *Ty, MVT &VT);
78 bool isLoadTypeLegal(Type *Ty, MVT &VT);
79
80 unsigned MaterializeFP(const ConstantFP *CFP, MVT VT);
81 unsigned MaterializeGV(const GlobalValue *GV, MVT VT);
82 unsigned MaterializeInt(const Constant *C, MVT VT);
Reed Kotler6280d972014-05-15 21:54:15 +000083 unsigned Materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
Reed Kotler9fe25f32014-06-08 02:08:43 +000084
85 // for some reason, this default is not generated by tablegen
Reed Kotlerfb77bc92014-06-08 03:04:42 +000086 // so we explicitly generate it here.
Reed Kotler9fe25f32014-06-08 02:08:43 +000087 //
88 unsigned FastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
89 unsigned Op0, bool Op0IsKill, uint64_t imm1,
90 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
91 return 0;
92 }
93
Reed Kotlerfb77bc92014-06-08 03:04:42 +000094 MachineInstrBuilder EmitInst(unsigned Opc) {
95 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
96 }
97
98 MachineInstrBuilder EmitInst(unsigned Opc, unsigned DstReg) {
99 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
100 DstReg);
101 }
102
103 MachineInstrBuilder EmitInstStore(unsigned Opc, unsigned SrcReg,
104 unsigned MemReg, int64_t MemOffset) {
105 return EmitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
106 }
107
Reed Kotler9fe25f32014-06-08 02:08:43 +0000108#include "MipsGenFastISel.inc"
Reed Kotler720c5ca2014-04-17 22:15:34 +0000109};
Reed Kotler67077b32014-04-29 17:57:50 +0000110
Reed Kotlerbab3f232014-05-01 20:39:21 +0000111bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
112 EVT evt = TLI.getValueType(Ty, true);
113 // Only handle simple types.
114 if (evt == MVT::Other || !evt.isSimple())
115 return false;
116 VT = evt.getSimpleVT();
117
118 // Handle all legal types, i.e. a register that will directly hold this
119 // value.
120 return TLI.isTypeLegal(VT);
121}
122
123bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
124 if (isTypeLegal(Ty, VT))
125 return true;
126 // We will extend this in a later patch:
127 // If this is a type than can be sign or zero-extended to a basic operation
128 // go ahead and accept it now.
129 return false;
130}
131
132bool MipsFastISel::ComputeAddress(const Value *Obj, Address &Addr) {
133 // This construct looks a big awkward but it is how other ports handle this
134 // and as this function is more fully completed, these cases which
135 // return false will have additional code in them.
136 //
137 if (isa<Instruction>(Obj))
138 return false;
139 else if (isa<ConstantExpr>(Obj))
140 return false;
141 Addr.Base.Reg = getRegForValue(Obj);
142 return Addr.Base.Reg != 0;
143}
144
145// Materialize a constant into a register, and return the register
146// number (or zero if we failed to handle it).
147unsigned MipsFastISel::TargetMaterializeConstant(const Constant *C) {
148 EVT CEVT = TLI.getValueType(C->getType(), true);
149
150 // Only handle simple types.
151 if (!CEVT.isSimple())
152 return 0;
153 MVT VT = CEVT.getSimpleVT();
154
155 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
156 return MaterializeFP(CFP, VT);
157 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
158 return MaterializeGV(GV, VT);
159 else if (isa<ConstantInt>(C))
160 return MaterializeInt(C, VT);
161
162 return 0;
163}
164
165bool MipsFastISel::EmitStore(MVT VT, unsigned SrcReg, Address &Addr,
166 unsigned Alignment) {
167 //
168 // more cases will be handled here in following patches.
169 //
Reed Kotler063d4fb2014-06-10 16:45:44 +0000170 if (VT == MVT::i32)
171 EmitInstStore(Mips::SW, SrcReg, Addr.Base.Reg, Addr.Offset);
172 else if (VT == MVT::f32)
173 EmitInstStore(Mips::SWC1, SrcReg, Addr.Base.Reg, Addr.Offset);
174 else if (VT == MVT::f64)
175 EmitInstStore(Mips::SDC1, SrcReg, Addr.Base.Reg, Addr.Offset);
176 else
Reed Kotlerbab3f232014-05-01 20:39:21 +0000177 return false;
Reed Kotlerbab3f232014-05-01 20:39:21 +0000178 return true;
179}
180
181bool MipsFastISel::SelectStore(const Instruction *I) {
182 Value *Op0 = I->getOperand(0);
183 unsigned SrcReg = 0;
184
185 // Atomic stores need special handling.
186 if (cast<StoreInst>(I)->isAtomic())
187 return false;
188
189 // Verify we have a legal type before going any further.
190 MVT VT;
191 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
192 return false;
193
194 // Get the value to be stored into a register.
195 SrcReg = getRegForValue(Op0);
196 if (SrcReg == 0)
197 return false;
198
199 // See if we can handle this address.
200 Address Addr;
201 if (!ComputeAddress(I->getOperand(1), Addr))
202 return false;
203
204 if (!EmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
205 return false;
206 return true;
207}
208
Reed Kotler67077b32014-04-29 17:57:50 +0000209bool MipsFastISel::SelectRet(const Instruction *I) {
210 const ReturnInst *Ret = cast<ReturnInst>(I);
211
212 if (!FuncInfo.CanLowerReturn)
213 return false;
214 if (Ret->getNumOperands() > 0) {
215 return false;
216 }
Reed Kotlerfb77bc92014-06-08 03:04:42 +0000217 EmitInst(Mips::RetRA);
Reed Kotler67077b32014-04-29 17:57:50 +0000218 return true;
219}
220
221bool MipsFastISel::TargetSelectInstruction(const Instruction *I) {
222 if (!TargetSupported)
223 return false;
224 switch (I->getOpcode()) {
225 default:
226 break;
Reed Kotlerbab3f232014-05-01 20:39:21 +0000227 case Instruction::Store:
228 return SelectStore(I);
Reed Kotler67077b32014-04-29 17:57:50 +0000229 case Instruction::Ret:
230 return SelectRet(I);
231 }
232 return false;
233}
Reed Kotler720c5ca2014-04-17 22:15:34 +0000234}
235
Reed Kotlerbab3f232014-05-01 20:39:21 +0000236unsigned MipsFastISel::MaterializeFP(const ConstantFP *CFP, MVT VT) {
Reed Kotler063d4fb2014-06-10 16:45:44 +0000237 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
238 if (VT == MVT::f32) {
239 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
240 unsigned DestReg = createResultReg(RC);
241 unsigned TempReg = Materialize32BitInt(Imm, &Mips::GPR32RegClass);
242 EmitInst(Mips::MTC1, DestReg).addReg(TempReg);
243 return DestReg;
244 } else if (VT == MVT::f64) {
245 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
246 unsigned DestReg = createResultReg(RC);
247 unsigned TempReg1 = Materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
248 unsigned TempReg2 =
249 Materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
250 EmitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
251 return DestReg;
252 }
Reed Kotlerbab3f232014-05-01 20:39:21 +0000253 return 0;
254}
255
256unsigned MipsFastISel::MaterializeGV(const GlobalValue *GV, MVT VT) {
257 // For now 32-bit only.
258 if (VT != MVT::i32)
259 return 0;
260 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
261 unsigned DestReg = createResultReg(RC);
262 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
263 bool IsThreadLocal = GVar && GVar->isThreadLocal();
264 // TLS not supported at this time.
265 if (IsThreadLocal)
266 return 0;
Reed Kotlerfb77bc92014-06-08 03:04:42 +0000267 EmitInst(Mips::LW, DestReg).addReg(MFI->getGlobalBaseReg()).addGlobalAddress(
268 GV, 0, MipsII::MO_GOT);
Reed Kotlerbab3f232014-05-01 20:39:21 +0000269 return DestReg;
270}
271unsigned MipsFastISel::MaterializeInt(const Constant *C, MVT VT) {
Reed Kotler6280d972014-05-15 21:54:15 +0000272 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Reed Kotlerbab3f232014-05-01 20:39:21 +0000273 return 0;
274 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Reed Kotlerbab3f232014-05-01 20:39:21 +0000275 const ConstantInt *CI = cast<ConstantInt>(C);
Reed Kotler6280d972014-05-15 21:54:15 +0000276 int64_t Imm;
277 if (CI->isNegative())
278 Imm = CI->getSExtValue();
279 else
280 Imm = CI->getZExtValue();
281 return Materialize32BitInt(Imm, RC);
282}
283
284unsigned MipsFastISel::Materialize32BitInt(int64_t Imm,
285 const TargetRegisterClass *RC) {
286 unsigned ResultReg = createResultReg(RC);
287
288 if (isInt<16>(Imm)) {
Reed Kotlerbab3f232014-05-01 20:39:21 +0000289 unsigned Opc = Mips::ADDiu;
Reed Kotlerfb77bc92014-06-08 03:04:42 +0000290 EmitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
Reed Kotler6280d972014-05-15 21:54:15 +0000291 return ResultReg;
292 } else if (isUInt<16>(Imm)) {
Reed Kotlerfb77bc92014-06-08 03:04:42 +0000293 EmitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
Reed Kotler6280d972014-05-15 21:54:15 +0000294 return ResultReg;
Reed Kotlerbab3f232014-05-01 20:39:21 +0000295 }
Reed Kotler6280d972014-05-15 21:54:15 +0000296 unsigned Lo = Imm & 0xFFFF;
297 unsigned Hi = (Imm >> 16) & 0xFFFF;
298 if (Lo) {
299 // Both Lo and Hi have nonzero bits.
300 unsigned TmpReg = createResultReg(RC);
Reed Kotlerfb77bc92014-06-08 03:04:42 +0000301 EmitInst(Mips::LUi, TmpReg).addImm(Hi);
302 EmitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
Reed Kotler6280d972014-05-15 21:54:15 +0000303 } else {
Reed Kotlerfb77bc92014-06-08 03:04:42 +0000304 EmitInst(Mips::LUi, ResultReg).addImm(Hi);
Reed Kotler6280d972014-05-15 21:54:15 +0000305 }
306 return ResultReg;
Reed Kotlerbab3f232014-05-01 20:39:21 +0000307}
308
Reed Kotler720c5ca2014-04-17 22:15:34 +0000309namespace llvm {
310FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
311 const TargetLibraryInfo *libInfo) {
312 return new MipsFastISel(funcInfo, libInfo);
313}
314}