blob: 8043f7b173bd10f5d9f3a7c5fc3f0695beb7a4df [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the MIPS target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "mips-isel"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "Mips.h"
16#include "MipsISelLowering.h"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsRegisterInfo.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "llvm/GlobalValue.h"
22#include "llvm/Instructions.h"
23#include "llvm/Intrinsics.h"
24#include "llvm/Support/CFG.h"
25#include "llvm/Type.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/Debug.h"
35#include <queue>
36#include <set>
37
38using namespace llvm;
39
40//===----------------------------------------------------------------------===//
41// Instruction Selector Implementation
42//===----------------------------------------------------------------------===//
43
44//===----------------------------------------------------------------------===//
45// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46// instructions for SelectionDAG operations.
47//===----------------------------------------------------------------------===//
48namespace {
49
50class VISIBILITY_HIDDEN MipsDAGToDAGISel : public SelectionDAGISel {
51
52 /// TM - Keep a reference to MipsTargetMachine.
53 MipsTargetMachine &TM;
54
55 /// MipsLowering - This object fully describes how to lower LLVM code to an
56 /// Mips-specific SelectionDAG.
57 MipsTargetLowering MipsLowering;
58
59 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
60 /// make the right decision when generating code for different targets.
61 //TODO: add initialization on constructor
62 //const MipsSubtarget *Subtarget;
63
64public:
65 MipsDAGToDAGISel(MipsTargetMachine &tm) :
66 SelectionDAGISel(MipsLowering),
67 TM(tm), MipsLowering(*TM.getTargetLowering()) {}
68
69 virtual void InstructionSelectBasicBlock(SelectionDAG &SD);
70
71 // Pass Name
72 virtual const char *getPassName() const {
73 return "MIPS DAG->DAG Pattern Instruction Selection";
74 }
75
76
77private:
78 // Include the pieces autogenerated from the target description.
79 #include "MipsGenDAGISel.inc"
80
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000081 SDOperand getGlobalBaseReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000082 SDNode *Select(SDOperand N);
83
84 // Complex Pattern.
85 bool SelectAddr(SDOperand Op, SDOperand N,
86 SDOperand &Base, SDOperand &Offset);
87
88
89 // getI32Imm - Return a target constant with the specified
90 // value, of type i32.
91 inline SDOperand getI32Imm(unsigned Imm) {
92 return CurDAG->getTargetConstant(Imm, MVT::i32);
93 }
94
95
96 #ifndef NDEBUG
97 unsigned Indent;
98 #endif
99};
100
101}
102
103/// InstructionSelectBasicBlock - This callback is invoked by
104/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
105void MipsDAGToDAGISel::
106InstructionSelectBasicBlock(SelectionDAG &SD)
107{
108 DEBUG(BB->dump());
109 // Codegen the basic block.
110 #ifndef NDEBUG
111 DOUT << "===== Instruction selection begins:\n";
112 Indent = 0;
113 #endif
114
115 // Select target instructions for the DAG.
116 SD.setRoot(SelectRoot(SD.getRoot()));
117
118 #ifndef NDEBUG
119 DOUT << "===== Instruction selection ends:\n";
120 #endif
121
122 SD.RemoveDeadNodes();
123
124 // Emit machine code to BB.
125 ScheduleAndEmitDAG(SD);
126}
127
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000128/// getGlobalBaseReg - Output the instructions required to put the
129/// GOT address into a register.
Chris Lattner84bc5422007-12-31 04:13:23 +0000130SDOperand MipsDAGToDAGISel::getGlobalBaseReg() {
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000131 MachineFunction* MF = BB->getParent();
132 unsigned GP = 0;
Chris Lattner84bc5422007-12-31 04:13:23 +0000133 for(MachineRegisterInfo::livein_iterator ii = MF->getRegInfo().livein_begin(),
134 ee = MF->getRegInfo().livein_end(); ii != ee; ++ii)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000135 if (ii->first == Mips::GP) {
136 GP = ii->second;
137 break;
138 }
139 assert(GP && "GOT PTR not in liveins");
140 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
141 GP, MVT::i32);
142}
143
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000144/// ComplexPattern used on MipsInstrInfo
145/// Used on Mips Load/Store instructions
146bool MipsDAGToDAGISel::
147SelectAddr(SDOperand Op, SDOperand Addr, SDOperand &Offset, SDOperand &Base)
148{
149 // if Address is FI, get the TargetFrameIndex.
150 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
151 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
152 Offset = CurDAG->getTargetConstant(0, MVT::i32);
153 return true;
154 }
155
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000156 // on PIC code Load GA
157 if (TM.getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000158 if ((Addr.getOpcode() == ISD::TargetGlobalAddress) ||
159 (Addr.getOpcode() == ISD::TargetJumpTable)){
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000160 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
161 Offset = Addr;
162 return true;
163 }
164 } else {
165 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
166 Addr.getOpcode() == ISD::TargetGlobalAddress))
167 return false;
168 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000169
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000170 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000171 if (Addr.getOpcode() == ISD::ADD) {
172 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
173 if (Predicate_immSExt16(CN)) {
174
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000175 // If the first operand is a FI, get the TargetFI Node
176 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
177 (Addr.getOperand(0))) {
178 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
179 } else {
180 Base = Addr.getOperand(0);
181 }
182
183 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
184 return true;
185 }
186 }
187 }
188
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000189 Base = Addr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000190 Offset = CurDAG->getTargetConstant(0, MVT::i32);
191 return true;
192}
193
194/// Select instructions not customized! Used for
195/// expanded, promoted and normal instructions
196SDNode* MipsDAGToDAGISel::
197Select(SDOperand N)
198{
199 SDNode *Node = N.Val;
200 unsigned Opcode = Node->getOpcode();
201
202 // Dump information about the Node being selected
203 #ifndef NDEBUG
204 DOUT << std::string(Indent, ' ') << "Selecting: ";
205 DEBUG(Node->dump(CurDAG));
206 DOUT << "\n";
207 Indent += 2;
208 #endif
209
210 // If we have a custom node, we already have selected!
211 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < MipsISD::FIRST_NUMBER) {
212 #ifndef NDEBUG
213 DOUT << std::string(Indent-2, ' ') << "== ";
214 DEBUG(Node->dump(CurDAG));
215 DOUT << "\n";
216 Indent -= 2;
217 #endif
218 return NULL;
219 }
220
221 ///
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000222 // Instruction Selection not handled by the auto-generated
223 // tablegen selection should be handled here.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000224 ///
225 switch(Opcode) {
226
227 default: break;
228
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000229 case ISD::ADDE: {
230 // ADDE is usally attached with a ADDC instruction, we must
231 // compare ADDC operands and set a register if we have a carry.
232 SDOperand InFlag = Node->getOperand(2);
233 unsigned Opc = InFlag.getOpcode();
234 assert((Opc == ISD::ADDC || Opc == ISD::ADDE) &&
235 "ADDE flag operand must come from a ADDC or ADDE");
236 SDOperand Ops[] = { InFlag.getValue(0), InFlag.getOperand(1) };
237
238 SDOperand LHS = Node->getOperand(0);
239 SDOperand RHS = Node->getOperand(1);
240 AddToISelQueue(LHS);
241 AddToISelQueue(RHS);
242
243 MVT::ValueType VT = LHS.getValueType();
244 SDNode *Carry = CurDAG->getTargetNode(Mips::SLTu, VT, Ops, 2);
245 SDNode *AddCarry = CurDAG->getTargetNode(Mips::ADDu, VT,
246 SDOperand(Carry,0), RHS);
247
248 return CurDAG->SelectNodeTo(N.Val, Mips::ADDu, VT, MVT::Flag,
249 LHS, SDOperand(AddCarry,0));
250 }
251
252 case ISD::SUBE: {
253 // SUBE is usally attached with a SUBC instruction, we must
254 // compare SUBC operands and set a register if we have a carry.
255 SDOperand InFlag = Node->getOperand(2);
256 unsigned Opc = InFlag.getOpcode();
257 assert((Opc == ISD::SUBC || Opc == ISD::SUBE) &&
258 "SUBE flag operand must come from a SUBC or SUBE");
259 SDOperand Ops[] = { InFlag.getOperand(0), InFlag.getOperand(1) };
260
261 SDOperand LHS = Node->getOperand(0);
262 SDOperand RHS = Node->getOperand(1);
263 AddToISelQueue(LHS);
264 AddToISelQueue(RHS);
265
266 MVT::ValueType VT = LHS.getValueType();
267 SDNode *Carry = CurDAG->getTargetNode(Mips::SLTu, VT, Ops, 2);
268 SDNode *AddCarry = CurDAG->getTargetNode(Mips::ADDu, VT,
269 SDOperand(Carry,0), RHS);
270
271 return CurDAG->SelectNodeTo(N.Val, Mips::SUBu, VT, MVT::Flag,
272 LHS, SDOperand(AddCarry,0));
273 }
274
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000275 /// Special Mul operations
276 case ISD::MULHS:
277 case ISD::MULHU: {
278 SDOperand MulOp1 = Node->getOperand(0);
279 SDOperand MulOp2 = Node->getOperand(1);
280 AddToISelQueue(MulOp1);
281 AddToISelQueue(MulOp2);
282
283 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
284 SDNode *MulNode = CurDAG->getTargetNode(MulOp, MVT::Flag, MulOp1, MulOp2);
285
286 SDOperand MFInFlag = SDOperand(MulNode, 0);
287 return CurDAG->getTargetNode(Mips::MFHI, MVT::i32, MFInFlag);
288 }
289
290 /// Div operations
291 case ISD::SDIV:
292 case ISD::UDIV: {
293 SDOperand DivOp1 = Node->getOperand(0);
294 SDOperand DivOp2 = Node->getOperand(1);
295 AddToISelQueue(DivOp1);
296 AddToISelQueue(DivOp2);
297
298 unsigned DivOp = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
299 SDNode *DivNode = CurDAG->getTargetNode(DivOp, MVT::Flag, DivOp1, DivOp2);
300
301 SDOperand MFInFlag = SDOperand(DivNode, 0);
302 return CurDAG->getTargetNode(Mips::MFLO, MVT::i32, MFInFlag);
303 }
304
305 /// Rem operations
306 case ISD::SREM:
307 case ISD::UREM: {
308 SDOperand RemOp1 = Node->getOperand(0);
309 SDOperand RemOp2 = Node->getOperand(1);
310 AddToISelQueue(RemOp1);
311 AddToISelQueue(RemOp2);
312
313 unsigned RemOp = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
314 SDNode *RemNode = CurDAG->getTargetNode(RemOp, MVT::Flag, RemOp1, RemOp2);
315
316 SDOperand MFInFlag = SDOperand(RemNode, 0);
317 return CurDAG->getTargetNode(Mips::MFHI, MVT::i32, MFInFlag);
318 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000319
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000320 // Get target GOT address.
321 case ISD::GLOBAL_OFFSET_TABLE: {
322 SDOperand Result = getGlobalBaseReg();
323 ReplaceUses(N, Result);
324 return NULL;
325 }
326
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000327 /// Handle direct and indirect calls when using PIC. On PIC, when
328 /// GOT is smaller than about 64k (small code) the GA target is
329 /// loaded with only one instruction. Otherwise GA's target must
330 /// be loaded with 3 instructions.
331 case MipsISD::JmpLink: {
332 if (TM.getRelocationModel() == Reloc::PIC_) {
333 //bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
334 SDOperand Chain = Node->getOperand(0);
335 SDOperand Callee = Node->getOperand(1);
336 AddToISelQueue(Chain);
337 SDOperand T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32);
338 SDOperand InFlag(0, 0);
339
340 if ( (isa<GlobalAddressSDNode>(Callee)) ||
341 (isa<ExternalSymbolSDNode>(Callee)) )
342 {
343 /// Direct call for global addresses and external symbols
344 SDOperand GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
345
346 // Use load to get GOT target
347 SDOperand Ops[] = { Callee, GPReg, Chain };
348 SDOperand Load = SDOperand(CurDAG->getTargetNode(Mips::LW, MVT::i32,
349 MVT::Other, Ops, 3), 0);
350 Chain = Load.getValue(1);
351 AddToISelQueue(Chain);
352
353 // Call target must be on T9
354 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Load, InFlag);
355 } else
356 /// Indirect call
357 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Callee, InFlag);
358
359 AddToISelQueue(Chain);
360
361 // Emit Jump and Link Register
362 SDNode *ResNode = CurDAG->getTargetNode(Mips::JALR, MVT::Other,
363 MVT::Flag, T9Reg, Chain);
364 Chain = SDOperand(ResNode, 0);
365 InFlag = SDOperand(ResNode, 1);
366 ReplaceUses(SDOperand(Node, 0), Chain);
367 ReplaceUses(SDOperand(Node, 1), InFlag);
368 return ResNode;
369 }
370 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000371 }
372
373 // Select the default instruction
374 SDNode *ResNode = SelectCode(N);
375
376 #ifndef NDEBUG
377 DOUT << std::string(Indent-2, ' ') << "=> ";
378 if (ResNode == NULL || ResNode == N.Val)
379 DEBUG(N.Val->dump(CurDAG));
380 else
381 DEBUG(ResNode->dump(CurDAG));
382 DOUT << "\n";
383 Indent -= 2;
384 #endif
385
386 return ResNode;
387}
388
389/// createMipsISelDag - This pass converts a legalized DAG into a
390/// MIPS-specific DAG, ready for instruction scheduling.
391FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
392 return new MipsDAGToDAGISel(TM);
393}