blob: cbcedb8ee71cc5485bcf71486b5d4caf274a9690 [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"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// Instruction Selector Implementation
40//===----------------------------------------------------------------------===//
41
42//===----------------------------------------------------------------------===//
43// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
44// instructions for SelectionDAG operations.
45//===----------------------------------------------------------------------===//
46namespace {
47
Nick Lewycky6726b6d2009-10-25 06:33:48 +000048class MipsDAGToDAGISel : public SelectionDAGISel {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000049
50 /// TM - Keep a reference to MipsTargetMachine.
51 MipsTargetMachine &TM;
52
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000053 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
54 /// make the right decision when generating code for different targets.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000055 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056
57public:
Dan Gohman1002c022008-07-07 18:00:37 +000058 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +000059 SelectionDAGISel(tm),
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000060 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061
Dan Gohmanf350b272008-08-23 02:25:05 +000062 virtual void InstructionSelect();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063
64 // Pass Name
65 virtual const char *getPassName() const {
66 return "MIPS DAG->DAG Pattern Instruction Selection";
67 }
68
69
70private:
71 // Include the pieces autogenerated from the target description.
72 #include "MipsGenDAGISel.inc"
73
Dan Gohman99114052009-06-03 20:30:14 +000074 /// getTargetMachine - Return a reference to the TargetMachine, casted
75 /// to the target-specific type.
76 const MipsTargetMachine &getTargetMachine() {
77 return static_cast<const MipsTargetMachine &>(TM);
78 }
79
80 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
81 /// to the target-specific type.
82 const MipsInstrInfo *getInstrInfo() {
83 return getTargetMachine().getInstrInfo();
84 }
85
86 SDNode *getGlobalBaseReg();
Dan Gohman475871a2008-07-27 21:46:04 +000087 SDNode *Select(SDValue N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088
89 // Complex Pattern.
Dan Gohman475871a2008-07-27 21:46:04 +000090 bool SelectAddr(SDValue Op, SDValue N,
91 SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000092
93
94 // getI32Imm - Return a target constant with the specified
95 // value, of type i32.
Dan Gohman475871a2008-07-27 21:46:04 +000096 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000097 return CurDAG->getTargetConstant(Imm, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000098 }
99
100
101 #ifndef NDEBUG
102 unsigned Indent;
103 #endif
104};
105
106}
107
Evan Chengdb8d56b2008-06-30 20:45:06 +0000108/// InstructionSelect - This callback is invoked by
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000109/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner893e1c92009-08-23 06:49:22 +0000110void MipsDAGToDAGISel::InstructionSelect() {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000111 // Codegen the basic block.
Chris Lattner893e1c92009-08-23 06:49:22 +0000112 DEBUG(errs() << "===== Instruction selection begins:\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000113 DEBUG(Indent = 0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000114
115 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000116 SelectRoot(*CurDAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000117
Chris Lattner893e1c92009-08-23 06:49:22 +0000118 DEBUG(errs() << "===== Instruction selection ends:\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000119
Dan Gohmanf350b272008-08-23 02:25:05 +0000120 CurDAG->RemoveDeadNodes();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000121}
122
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000123/// getGlobalBaseReg - Output the instructions required to put the
124/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000125SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Dan Gohman99114052009-06-03 20:30:14 +0000126 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
127 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000128}
129
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000130/// ComplexPattern used on MipsInstrInfo
131/// Used on Mips Load/Store instructions
132bool MipsDAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000133SelectAddr(SDValue Op, SDValue Addr, SDValue &Offset, SDValue &Base)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000134{
135 // if Address is FI, get the TargetFrameIndex.
136 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
138 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000139 return true;
140 }
141
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000142 // on PIC code Load GA
143 if (TM.getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000144 if ((Addr.getOpcode() == ISD::TargetGlobalAddress) ||
145 (Addr.getOpcode() == ISD::TargetJumpTable)){
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000147 Offset = Addr;
148 return true;
149 }
150 } else {
Bill Wendling056292f2008-09-16 21:48:12 +0000151 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000152 Addr.getOpcode() == ISD::TargetGlobalAddress))
153 return false;
154 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000155
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000156 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000157 if (Addr.getOpcode() == ISD::ADD) {
158 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
159 if (Predicate_immSExt16(CN)) {
160
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000161 // If the first operand is a FI, get the TargetFI Node
162 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
163 (Addr.getOperand(0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000165 } else {
166 Base = Addr.getOperand(0);
167 }
168
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000170 return true;
171 }
172 }
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000173
174 // When loading from constant pools, load the lower address part in
175 // the instruction itself. Instead of:
176 // lui $2, %hi($CPI1_0)
177 // addiu $2, $2, %lo($CPI1_0)
178 // lwc1 $f0, 0($2)
179 // Generate:
180 // lui $2, %hi($CPI1_0)
181 // lwc1 $f0, %lo($CPI1_0)($2)
182 if (Addr.getOperand(0).getOpcode() == MipsISD::Hi &&
183 Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
184 SDValue LoVal = Addr.getOperand(1);
185 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(
186 LoVal.getOperand(0))) {
187 if (!CP->getOffset()) {
188 Base = Addr.getOperand(0);
189 Offset = LoVal.getOperand(0);
190 return true;
191 }
192 }
193 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000194 }
195
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000196 Base = Addr;
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000198 return true;
199}
200
201/// Select instructions not customized! Used for
202/// expanded, promoted and normal instructions
Chris Lattner893e1c92009-08-23 06:49:22 +0000203SDNode* MipsDAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000204 SDNode *Node = N.getNode();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000205 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000206 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000207
208 // Dump information about the Node being selected
Chris Lattner893e1c92009-08-23 06:49:22 +0000209 DEBUG(errs().indent(Indent) << "Selecting: ";
210 Node->dump(CurDAG);
211 errs() << "\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000212 DEBUG(Indent += 2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000213
214 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000215 if (Node->isMachineOpcode()) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000216 DEBUG(errs().indent(Indent-2) << "== ";
217 Node->dump(CurDAG);
218 errs() << "\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000219 DEBUG(Indent -= 2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000220 return NULL;
221 }
222
223 ///
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000224 // Instruction Selection not handled by the auto-generated
225 // tablegen selection should be handled here.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000226 ///
227 switch(Opcode) {
228
229 default: break;
230
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000231 case ISD::SUBE:
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000232 case ISD::ADDE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000233 SDValue InFlag = Node->getOperand(2), CmpLHS;
Chris Lattner30baa952008-12-14 21:38:24 +0000234 unsigned Opc = InFlag.getOpcode(); Opc=Opc;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000235 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
236 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
237 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
238
Chris Lattner30baa952008-12-14 21:38:24 +0000239 unsigned MOp;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000240 if (Opcode == ISD::ADDE) {
241 CmpLHS = InFlag.getValue(0);
242 MOp = Mips::ADDu;
243 } else {
244 CmpLHS = InFlag.getOperand(0);
245 MOp = Mips::SUBu;
246 }
247
Dan Gohman475871a2008-07-27 21:46:04 +0000248 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000249
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue LHS = Node->getOperand(0);
251 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000252
Owen Andersone50ed302009-08-10 22:56:29 +0000253 EVT VT = LHS.getValueType();
Dan Gohman602b0c82009-09-25 18:54:59 +0000254 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
255 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
256 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000257
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 return CurDAG->SelectNodeTo(N.getNode(), MOp, VT, MVT::Flag,
Dan Gohman475871a2008-07-27 21:46:04 +0000259 LHS, SDValue(AddCarry,0));
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000260 }
261
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000262 /// Mul/Div with two results
263 case ISD::SDIVREM:
264 case ISD::UDIVREM:
265 case ISD::SMUL_LOHI:
266 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +0000267 SDValue Op1 = Node->getOperand(0);
268 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000269
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000270 unsigned Op;
271 if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
272 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
273 else
274 Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000275
Dan Gohman602b0c82009-09-25 18:54:59 +0000276 SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000277
Dan Gohman475871a2008-07-27 21:46:04 +0000278 SDValue InFlag = SDValue(Node, 0);
Dan Gohman602b0c82009-09-25 18:54:59 +0000279 SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32,
280 MVT::Flag, InFlag);
Dan Gohman475871a2008-07-27 21:46:04 +0000281 InFlag = SDValue(Lo,1);
Dan Gohman602b0c82009-09-25 18:54:59 +0000282 SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000283
284 if (!N.getValue(0).use_empty())
Dan Gohman475871a2008-07-27 21:46:04 +0000285 ReplaceUses(N.getValue(0), SDValue(Lo,0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000286
287 if (!N.getValue(1).use_empty())
Dan Gohman475871a2008-07-27 21:46:04 +0000288 ReplaceUses(N.getValue(1), SDValue(Hi,0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000289
290 return NULL;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000291 }
292
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000293 /// Special Muls
294 case ISD::MUL:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000295 case ISD::MULHS:
296 case ISD::MULHU: {
Dan Gohman475871a2008-07-27 21:46:04 +0000297 SDValue MulOp1 = Node->getOperand(0);
298 SDValue MulOp2 = Node->getOperand(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000299
300 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Dan Gohman602b0c82009-09-25 18:54:59 +0000301 SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl,
302 MVT::Flag, MulOp1, MulOp2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000303
Dan Gohman475871a2008-07-27 21:46:04 +0000304 SDValue InFlag = SDValue(MulNode, 0);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000305
306 if (MulOp == ISD::MUL)
Dan Gohman602b0c82009-09-25 18:54:59 +0000307 return CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000308 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000309 return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000310 }
311
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000312 /// Div/Rem operations
313 case ISD::SREM:
314 case ISD::UREM:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000315 case ISD::SDIV:
316 case ISD::UDIV: {
Dan Gohman475871a2008-07-27 21:46:04 +0000317 SDValue Op1 = Node->getOperand(0);
318 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000319
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000320 unsigned Op, MOp;
321 if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
322 Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
323 MOp = Mips::MFLO;
324 } else {
325 Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
326 MOp = Mips::MFHI;
327 }
Dan Gohman602b0c82009-09-25 18:54:59 +0000328 SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000329
Dan Gohman475871a2008-07-27 21:46:04 +0000330 SDValue InFlag = SDValue(Node, 0);
Dan Gohman602b0c82009-09-25 18:54:59 +0000331 return CurDAG->getMachineNode(MOp, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000332 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000333
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000334 // Get target GOT address.
Dan Gohman99114052009-06-03 20:30:14 +0000335 case ISD::GLOBAL_OFFSET_TABLE:
336 return getGlobalBaseReg();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000337
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000338 case ISD::ConstantFP: {
339 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
340 if (N.getValueType() == MVT::f64 && CN->isExactlyValue(+0.0)) {
341 SDValue Zero = CurDAG->getRegister(Mips::ZERO, MVT::i32);
342 ReplaceUses(N, Zero);
343 return Zero.getNode();
344 }
345 break;
346 }
347
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000348 /// Handle direct and indirect calls when using PIC. On PIC, when
349 /// GOT is smaller than about 64k (small code) the GA target is
350 /// loaded with only one instruction. Otherwise GA's target must
351 /// be loaded with 3 instructions.
352 case MipsISD::JmpLink: {
353 if (TM.getRelocationModel() == Reloc::PIC_) {
Dan Gohman475871a2008-07-27 21:46:04 +0000354 SDValue Chain = Node->getOperand(0);
355 SDValue Callee = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 SDValue T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000357 SDValue InFlag(0, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000358
359 if ( (isa<GlobalAddressSDNode>(Callee)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000360 (isa<ExternalSymbolSDNode>(Callee)) )
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000361 {
362 /// Direct call for global addresses and external symbols
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000364
365 // Use load to get GOT target
Dan Gohman475871a2008-07-27 21:46:04 +0000366 SDValue Ops[] = { Callee, GPReg, Chain };
Dan Gohman602b0c82009-09-25 18:54:59 +0000367 SDValue Load = SDValue(CurDAG->getMachineNode(Mips::LW, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 MVT::Other, Ops, 3), 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000369 Chain = Load.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000370
371 // Call target must be on T9
Dale Johannesena05dca42009-02-04 23:02:30 +0000372 Chain = CurDAG->getCopyToReg(Chain, dl, T9Reg, Load, InFlag);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000373 } else
374 /// Indirect call
Dale Johannesena05dca42009-02-04 23:02:30 +0000375 Chain = CurDAG->getCopyToReg(Chain, dl, T9Reg, Callee, InFlag);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000376
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000377 // Emit Jump and Link Register
Dan Gohman602b0c82009-09-25 18:54:59 +0000378 SDNode *ResNode = CurDAG->getMachineNode(Mips::JALR, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 MVT::Flag, T9Reg, Chain);
Dan Gohman475871a2008-07-27 21:46:04 +0000380 Chain = SDValue(ResNode, 0);
381 InFlag = SDValue(ResNode, 1);
382 ReplaceUses(SDValue(Node, 0), Chain);
383 ReplaceUses(SDValue(Node, 1), InFlag);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000384 return ResNode;
385 }
386 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000387 }
388
389 // Select the default instruction
390 SDNode *ResNode = SelectCode(N);
391
Chris Lattner893e1c92009-08-23 06:49:22 +0000392 DEBUG(errs().indent(Indent-2) << "=> ");
Gabor Greifba36cb52008-08-28 21:40:38 +0000393 if (ResNode == NULL || ResNode == N.getNode())
394 DEBUG(N.getNode()->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000395 else
396 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000397 DEBUG(errs() << "\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000398 DEBUG(Indent -= 2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000399
400 return ResNode;
401}
402
403/// createMipsISelDag - This pass converts a legalized DAG into a
404/// MIPS-specific DAG, ready for instruction scheduling.
405FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
406 return new MipsDAGToDAGISel(TM);
407}