blob: 4822e0159a0ebb04feb0b2e97ffa48e787393046 [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.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000061 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062
63public:
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000064 MipsDAGToDAGISel(MipsTargetMachine &tm) : SelectionDAGISel(MipsLowering),
65 TM(tm), MipsLowering(*TM.getTargetLowering()),
66 Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067
Evan Chengdb8d56b2008-06-30 20:45:06 +000068 virtual void InstructionSelect(SelectionDAG &SD);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069
70 // Pass Name
71 virtual const char *getPassName() const {
72 return "MIPS DAG->DAG Pattern Instruction Selection";
73 }
74
75
76private:
77 // Include the pieces autogenerated from the target description.
78 #include "MipsGenDAGISel.inc"
79
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000080 SDOperand getGlobalBaseReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081 SDNode *Select(SDOperand N);
82
83 // Complex Pattern.
84 bool SelectAddr(SDOperand Op, SDOperand N,
85 SDOperand &Base, SDOperand &Offset);
86
87
88 // getI32Imm - Return a target constant with the specified
89 // value, of type i32.
90 inline SDOperand getI32Imm(unsigned Imm) {
91 return CurDAG->getTargetConstant(Imm, MVT::i32);
92 }
93
94
95 #ifndef NDEBUG
96 unsigned Indent;
97 #endif
98};
99
100}
101
Evan Chengdb8d56b2008-06-30 20:45:06 +0000102/// InstructionSelect - This callback is invoked by
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000103/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
104void MipsDAGToDAGISel::
Evan Chengdb8d56b2008-06-30 20:45:06 +0000105InstructionSelect(SelectionDAG &SD)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000106{
107 DEBUG(BB->dump());
108 // Codegen the basic block.
109 #ifndef NDEBUG
110 DOUT << "===== Instruction selection begins:\n";
111 Indent = 0;
112 #endif
113
114 // Select target instructions for the DAG.
115 SD.setRoot(SelectRoot(SD.getRoot()));
116
117 #ifndef NDEBUG
118 DOUT << "===== Instruction selection ends:\n";
119 #endif
120
121 SD.RemoveDeadNodes();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000122}
123
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000124/// getGlobalBaseReg - Output the instructions required to put the
125/// GOT address into a register.
Chris Lattner84bc5422007-12-31 04:13:23 +0000126SDOperand MipsDAGToDAGISel::getGlobalBaseReg() {
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000127 MachineFunction* MF = BB->getParent();
128 unsigned GP = 0;
Chris Lattner84bc5422007-12-31 04:13:23 +0000129 for(MachineRegisterInfo::livein_iterator ii = MF->getRegInfo().livein_begin(),
130 ee = MF->getRegInfo().livein_end(); ii != ee; ++ii)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000131 if (ii->first == Mips::GP) {
132 GP = ii->second;
133 break;
134 }
135 assert(GP && "GOT PTR not in liveins");
136 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
137 GP, MVT::i32);
138}
139
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000140/// ComplexPattern used on MipsInstrInfo
141/// Used on Mips Load/Store instructions
142bool MipsDAGToDAGISel::
143SelectAddr(SDOperand Op, SDOperand Addr, SDOperand &Offset, SDOperand &Base)
144{
145 // if Address is FI, get the TargetFrameIndex.
146 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
147 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
148 Offset = CurDAG->getTargetConstant(0, MVT::i32);
149 return true;
150 }
151
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000152 // on PIC code Load GA
153 if (TM.getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000154 if ((Addr.getOpcode() == ISD::TargetGlobalAddress) ||
155 (Addr.getOpcode() == ISD::TargetJumpTable)){
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000156 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
157 Offset = Addr;
158 return true;
159 }
160 } else {
161 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
162 Addr.getOpcode() == ISD::TargetGlobalAddress))
163 return false;
164 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000165
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000166 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000167 if (Addr.getOpcode() == ISD::ADD) {
168 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
169 if (Predicate_immSExt16(CN)) {
170
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000171 // If the first operand is a FI, get the TargetFI Node
172 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
173 (Addr.getOperand(0))) {
174 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
175 } else {
176 Base = Addr.getOperand(0);
177 }
178
179 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
180 return true;
181 }
182 }
183 }
184
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000185 Base = Addr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000186 Offset = CurDAG->getTargetConstant(0, MVT::i32);
187 return true;
188}
189
190/// Select instructions not customized! Used for
191/// expanded, promoted and normal instructions
192SDNode* MipsDAGToDAGISel::
193Select(SDOperand N)
194{
195 SDNode *Node = N.Val;
196 unsigned Opcode = Node->getOpcode();
197
198 // Dump information about the Node being selected
199 #ifndef NDEBUG
200 DOUT << std::string(Indent, ' ') << "Selecting: ";
201 DEBUG(Node->dump(CurDAG));
202 DOUT << "\n";
203 Indent += 2;
204 #endif
205
206 // If we have a custom node, we already have selected!
207 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < MipsISD::FIRST_NUMBER) {
208 #ifndef NDEBUG
209 DOUT << std::string(Indent-2, ' ') << "== ";
210 DEBUG(Node->dump(CurDAG));
211 DOUT << "\n";
212 Indent -= 2;
213 #endif
214 return NULL;
215 }
216
217 ///
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000218 // Instruction Selection not handled by the auto-generated
219 // tablegen selection should be handled here.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000220 ///
221 switch(Opcode) {
222
223 default: break;
224
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000225 case ISD::SUBE:
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000226 case ISD::ADDE: {
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000227 SDOperand InFlag = Node->getOperand(2), CmpLHS;
228 unsigned Opc = InFlag.getOpcode(), MOp;
229
230 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
231 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
232 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
233
234 if (Opcode == ISD::ADDE) {
235 CmpLHS = InFlag.getValue(0);
236 MOp = Mips::ADDu;
237 } else {
238 CmpLHS = InFlag.getOperand(0);
239 MOp = Mips::SUBu;
240 }
241
242 SDOperand Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000243
244 SDOperand LHS = Node->getOperand(0);
245 SDOperand RHS = Node->getOperand(1);
246 AddToISelQueue(LHS);
247 AddToISelQueue(RHS);
248
Duncan Sands83ec4b62008-06-06 12:08:01 +0000249 MVT VT = LHS.getValueType();
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000250 SDNode *Carry = CurDAG->getTargetNode(Mips::SLTu, VT, Ops, 2);
251 SDNode *AddCarry = CurDAG->getTargetNode(Mips::ADDu, VT,
252 SDOperand(Carry,0), RHS);
253
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000254 return CurDAG->SelectNodeTo(N.Val, MOp, VT, MVT::Flag,
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000255 LHS, SDOperand(AddCarry,0));
256 }
257
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000258 /// Mul/Div with two results
259 case ISD::SDIVREM:
260 case ISD::UDIVREM:
261 case ISD::SMUL_LOHI:
262 case ISD::UMUL_LOHI: {
263 SDOperand Op1 = Node->getOperand(0);
264 SDOperand Op2 = Node->getOperand(1);
265 AddToISelQueue(Op1);
266 AddToISelQueue(Op2);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000267
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000268 unsigned Op;
269 if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
270 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
271 else
272 Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000273
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000274 SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000275
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000276 SDOperand InFlag = SDOperand(Node, 0);
277 SDNode *Lo = CurDAG->getTargetNode(Mips::MFLO, MVT::i32, MVT::Flag, InFlag);
278
279 InFlag = SDOperand(Lo,1);
280 SDNode *Hi = CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
281
282 if (!N.getValue(0).use_empty())
283 ReplaceUses(N.getValue(0), SDOperand(Lo,0));
284
285 if (!N.getValue(1).use_empty())
286 ReplaceUses(N.getValue(1), SDOperand(Hi,0));
287
288 return NULL;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000289 }
290
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000291 /// Special Muls
292 case ISD::MUL:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000293 case ISD::MULHS:
294 case ISD::MULHU: {
295 SDOperand MulOp1 = Node->getOperand(0);
296 SDOperand MulOp2 = Node->getOperand(1);
297 AddToISelQueue(MulOp1);
298 AddToISelQueue(MulOp2);
299
300 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
301 SDNode *MulNode = CurDAG->getTargetNode(MulOp, MVT::Flag, MulOp1, MulOp2);
302
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000303 SDOperand InFlag = SDOperand(MulNode, 0);
304
305 if (MulOp == ISD::MUL)
306 return CurDAG->getTargetNode(Mips::MFLO, MVT::i32, InFlag);
307 else
308 return CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000309 }
310
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000311 /// Div/Rem operations
312 case ISD::SREM:
313 case ISD::UREM:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000314 case ISD::SDIV:
315 case ISD::UDIV: {
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000316 SDOperand Op1 = Node->getOperand(0);
317 SDOperand Op2 = Node->getOperand(1);
318 AddToISelQueue(Op1);
319 AddToISelQueue(Op2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000320
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000321 unsigned Op, MOp;
322 if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
323 Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
324 MOp = Mips::MFLO;
325 } else {
326 Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
327 MOp = Mips::MFHI;
328 }
329 SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000330
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000331 SDOperand InFlag = SDOperand(Node, 0);
332 return CurDAG->getTargetNode(MOp, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000333 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000334
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000335 // Get target GOT address.
336 case ISD::GLOBAL_OFFSET_TABLE: {
337 SDOperand Result = getGlobalBaseReg();
338 ReplaceUses(N, Result);
339 return NULL;
340 }
341
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000342 /// Handle direct and indirect calls when using PIC. On PIC, when
343 /// GOT is smaller than about 64k (small code) the GA target is
344 /// loaded with only one instruction. Otherwise GA's target must
345 /// be loaded with 3 instructions.
346 case MipsISD::JmpLink: {
347 if (TM.getRelocationModel() == Reloc::PIC_) {
348 //bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
349 SDOperand Chain = Node->getOperand(0);
350 SDOperand Callee = Node->getOperand(1);
351 AddToISelQueue(Chain);
352 SDOperand T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32);
353 SDOperand InFlag(0, 0);
354
355 if ( (isa<GlobalAddressSDNode>(Callee)) ||
356 (isa<ExternalSymbolSDNode>(Callee)) )
357 {
358 /// Direct call for global addresses and external symbols
359 SDOperand GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
360
361 // Use load to get GOT target
362 SDOperand Ops[] = { Callee, GPReg, Chain };
363 SDOperand Load = SDOperand(CurDAG->getTargetNode(Mips::LW, MVT::i32,
364 MVT::Other, Ops, 3), 0);
365 Chain = Load.getValue(1);
366 AddToISelQueue(Chain);
367
368 // Call target must be on T9
369 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Load, InFlag);
370 } else
371 /// Indirect call
372 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Callee, InFlag);
373
374 AddToISelQueue(Chain);
375
376 // Emit Jump and Link Register
377 SDNode *ResNode = CurDAG->getTargetNode(Mips::JALR, MVT::Other,
378 MVT::Flag, T9Reg, Chain);
379 Chain = SDOperand(ResNode, 0);
380 InFlag = SDOperand(ResNode, 1);
381 ReplaceUses(SDOperand(Node, 0), Chain);
382 ReplaceUses(SDOperand(Node, 1), InFlag);
383 return ResNode;
384 }
385 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386 }
387
388 // Select the default instruction
389 SDNode *ResNode = SelectCode(N);
390
391 #ifndef NDEBUG
392 DOUT << std::string(Indent-2, ' ') << "=> ";
393 if (ResNode == NULL || ResNode == N.Val)
394 DEBUG(N.Val->dump(CurDAG));
395 else
396 DEBUG(ResNode->dump(CurDAG));
397 DOUT << "\n";
398 Indent -= 2;
399 #endif
400
401 return ResNode;
402}
403
404/// createMipsISelDag - This pass converts a legalized DAG into a
405/// MIPS-specific DAG, ready for instruction scheduling.
406FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
407 return new MipsDAGToDAGISel(TM);
408}