blob: eed1f062a300f22a757a5951683cbbced59af855 [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 Kotler720c5ca2014-04-17 22:15:34 +000015
16using namespace llvm;
17
18namespace {
19
Reed Kotlerbab3f232014-05-01 20:39:21 +000020// All possible address modes.
21typedef struct Address {
22 enum { RegBase, FrameIndexBase } BaseType;
23
24 union {
25 unsigned Reg;
26 int FI;
27 } Base;
28
29 int64_t Offset;
30
31 // Innocuous defaults for our address.
32 Address() : BaseType(RegBase), Offset(0) { Base.Reg = 0; }
33} Address;
34
Reed Kotler720c5ca2014-04-17 22:15:34 +000035class MipsFastISel final : public FastISel {
36
Reed Kotler67077b32014-04-29 17:57:50 +000037 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
38 /// make the right decision when generating code for different targets.
39 const MipsSubtarget *Subtarget;
40 Module &M;
41 const TargetMachine &TM;
42 const TargetInstrInfo &TII;
43 const TargetLowering &TLI;
44 MipsFunctionInfo *MFI;
45
46 // Convenience variables to avoid some queries.
47 LLVMContext *Context;
48
49 bool TargetSupported;
50
Reed Kotler720c5ca2014-04-17 22:15:34 +000051public:
52 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
53 const TargetLibraryInfo *libInfo)
Reed Kotler67077b32014-04-29 17:57:50 +000054 : FastISel(funcInfo, libInfo),
55 M(const_cast<Module &>(*funcInfo.Fn->getParent())),
56 TM(funcInfo.MF->getTarget()), TII(*TM.getInstrInfo()),
57 TLI(*TM.getTargetLowering()) {
58 Subtarget = &TM.getSubtarget<MipsSubtarget>();
59 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
60 Context = &funcInfo.Fn->getContext();
61 TargetSupported = ((Subtarget->getRelocationModel() == Reloc::PIC_) &&
62 (Subtarget->hasMips32r2() && (Subtarget->isABI_O32())));
63 }
64
65 bool TargetSelectInstruction(const Instruction *I) override;
Reed Kotlerbab3f232014-05-01 20:39:21 +000066 unsigned TargetMaterializeConstant(const Constant *C) override;
Reed Kotler67077b32014-04-29 17:57:50 +000067
Reed Kotlerbab3f232014-05-01 20:39:21 +000068 bool ComputeAddress(const Value *Obj, Address &Addr);
69
70private:
71 bool EmitStore(MVT VT, unsigned SrcReg, Address &Addr,
72 unsigned Alignment = 0);
Reed Kotler67077b32014-04-29 17:57:50 +000073 bool SelectRet(const Instruction *I);
Reed Kotlerbab3f232014-05-01 20:39:21 +000074 bool SelectStore(const Instruction *I);
75
76 bool isTypeLegal(Type *Ty, MVT &VT);
77 bool isLoadTypeLegal(Type *Ty, MVT &VT);
78
79 unsigned MaterializeFP(const ConstantFP *CFP, MVT VT);
80 unsigned MaterializeGV(const GlobalValue *GV, MVT VT);
81 unsigned MaterializeInt(const Constant *C, MVT VT);
Reed Kotler720c5ca2014-04-17 22:15:34 +000082};
Reed Kotler67077b32014-04-29 17:57:50 +000083
Reed Kotlerbab3f232014-05-01 20:39:21 +000084bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
85 EVT evt = TLI.getValueType(Ty, true);
86 // Only handle simple types.
87 if (evt == MVT::Other || !evt.isSimple())
88 return false;
89 VT = evt.getSimpleVT();
90
91 // Handle all legal types, i.e. a register that will directly hold this
92 // value.
93 return TLI.isTypeLegal(VT);
94}
95
96bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
97 if (isTypeLegal(Ty, VT))
98 return true;
99 // We will extend this in a later patch:
100 // If this is a type than can be sign or zero-extended to a basic operation
101 // go ahead and accept it now.
102 return false;
103}
104
105bool MipsFastISel::ComputeAddress(const Value *Obj, Address &Addr) {
106 // This construct looks a big awkward but it is how other ports handle this
107 // and as this function is more fully completed, these cases which
108 // return false will have additional code in them.
109 //
110 if (isa<Instruction>(Obj))
111 return false;
112 else if (isa<ConstantExpr>(Obj))
113 return false;
114 Addr.Base.Reg = getRegForValue(Obj);
115 return Addr.Base.Reg != 0;
116}
117
118// Materialize a constant into a register, and return the register
119// number (or zero if we failed to handle it).
120unsigned MipsFastISel::TargetMaterializeConstant(const Constant *C) {
121 EVT CEVT = TLI.getValueType(C->getType(), true);
122
123 // Only handle simple types.
124 if (!CEVT.isSimple())
125 return 0;
126 MVT VT = CEVT.getSimpleVT();
127
128 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
129 return MaterializeFP(CFP, VT);
130 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
131 return MaterializeGV(GV, VT);
132 else if (isa<ConstantInt>(C))
133 return MaterializeInt(C, VT);
134
135 return 0;
136}
137
138bool MipsFastISel::EmitStore(MVT VT, unsigned SrcReg, Address &Addr,
139 unsigned Alignment) {
140 //
141 // more cases will be handled here in following patches.
142 //
143 if (VT != MVT::i32)
144 return false;
145 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::SW))
146 .addReg(SrcReg)
147 .addReg(Addr.Base.Reg)
148 .addImm(Addr.Offset);
149 return true;
150}
151
152bool MipsFastISel::SelectStore(const Instruction *I) {
153 Value *Op0 = I->getOperand(0);
154 unsigned SrcReg = 0;
155
156 // Atomic stores need special handling.
157 if (cast<StoreInst>(I)->isAtomic())
158 return false;
159
160 // Verify we have a legal type before going any further.
161 MVT VT;
162 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
163 return false;
164
165 // Get the value to be stored into a register.
166 SrcReg = getRegForValue(Op0);
167 if (SrcReg == 0)
168 return false;
169
170 // See if we can handle this address.
171 Address Addr;
172 if (!ComputeAddress(I->getOperand(1), Addr))
173 return false;
174
175 if (!EmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
176 return false;
177 return true;
178}
179
Reed Kotler67077b32014-04-29 17:57:50 +0000180bool MipsFastISel::SelectRet(const Instruction *I) {
181 const ReturnInst *Ret = cast<ReturnInst>(I);
182
183 if (!FuncInfo.CanLowerReturn)
184 return false;
185 if (Ret->getNumOperands() > 0) {
186 return false;
187 }
188 unsigned RetOpc = Mips::RetRA;
189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(RetOpc));
190 return true;
191}
192
193bool MipsFastISel::TargetSelectInstruction(const Instruction *I) {
194 if (!TargetSupported)
195 return false;
196 switch (I->getOpcode()) {
197 default:
198 break;
Reed Kotlerbab3f232014-05-01 20:39:21 +0000199 case Instruction::Store:
200 return SelectStore(I);
Reed Kotler67077b32014-04-29 17:57:50 +0000201 case Instruction::Ret:
202 return SelectRet(I);
203 }
204 return false;
205}
Reed Kotler720c5ca2014-04-17 22:15:34 +0000206}
207
Reed Kotlerbab3f232014-05-01 20:39:21 +0000208unsigned MipsFastISel::MaterializeFP(const ConstantFP *CFP, MVT VT) {
209 return 0;
210}
211
212unsigned MipsFastISel::MaterializeGV(const GlobalValue *GV, MVT VT) {
213 // For now 32-bit only.
214 if (VT != MVT::i32)
215 return 0;
216 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
217 unsigned DestReg = createResultReg(RC);
218 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
219 bool IsThreadLocal = GVar && GVar->isThreadLocal();
220 // TLS not supported at this time.
221 if (IsThreadLocal)
222 return 0;
223 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LW), DestReg)
224 .addReg(MFI->getGlobalBaseReg())
225 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
226 return DestReg;
227}
228unsigned MipsFastISel::MaterializeInt(const Constant *C, MVT VT) {
229 if (VT != MVT::i32)
230 return 0;
231 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
232 // If the constant is in range, use a load-immediate.
233 const ConstantInt *CI = cast<ConstantInt>(C);
234 if (isInt<16>(CI->getSExtValue())) {
235 unsigned Opc = Mips::ADDiu;
236 unsigned ImmReg = createResultReg(RC);
237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
238 .addReg(Mips::ZERO)
239 .addImm(CI->getSExtValue());
240 return ImmReg;
241 }
242 return 0;
243}
244
Reed Kotler720c5ca2014-04-17 22:15:34 +0000245namespace llvm {
246FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
247 const TargetLibraryInfo *libInfo) {
248 return new MipsFastISel(funcInfo, libInfo);
249}
250}