blob: 18cedcf7579e6e44435d44451267bcf71fd9e66a [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
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 the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mips-lower"
16
17#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/CallingConv.h"
24#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030#include "llvm/CodeGen/ValueTypes.h"
31#include "llvm/Support/Debug.h"
32#include <queue>
33#include <set>
34
35using namespace llvm;
36
37const char *MipsTargetLowering::
38getTargetNodeName(unsigned Opcode) const
39{
40 switch (Opcode)
41 {
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000042 case MipsISD::JmpLink : return "MipsISD::JmpLink";
43 case MipsISD::Hi : return "MipsISD::Hi";
44 case MipsISD::Lo : return "MipsISD::Lo";
45 case MipsISD::Ret : return "MipsISD::Ret";
46 case MipsISD::SelectCC : return "MipsISD::SelectCC";
47 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000048 }
49}
50
51MipsTargetLowering::
52MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
53{
54 // Mips does not have i1 type, so use i32 for
55 // setcc operations results (slt, sgt, ...).
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056 setSetCCResultContents(ZeroOrOneSetCCResult);
57
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000058 // JumpTable targets must use GOT when using PIC_
59 setUsesGlobalOffsetTable(true);
60
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 // Set up the register classes
62 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
63
64 // Custom
65 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +000066 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 setOperationAction(ISD::RET, MVT::Other, Custom);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000068 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000069 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070
71 // Load extented operations for i1 types must be promoted
72 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
73 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
74 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
75
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000076 // Mips does not have these NodeTypes below.
77 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
78 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
79 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000080 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000082
83 // Mips not supported intrinsics.
Andrew Lenharthd497d9f2008-02-16 14:46:26 +000084 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000085
86 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
87 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
88 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
89 setOperationAction(ISD::ROTL , MVT::i32, Expand);
90 setOperationAction(ISD::ROTR , MVT::i32, Expand);
91 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
92
93 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
94 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
95 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
96
97 // We don't have line number support yet.
Dan Gohman7f460202008-06-30 20:59:49 +000098 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000100 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
101 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000102
103 // Use the default for now
104 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
105 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
106
107 setStackPointerRegisterToSaveRestore(Mips::SP);
108 computeRegisterProperties();
109}
110
111
Duncan Sands83ec4b62008-06-06 12:08:01 +0000112MVT MipsTargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000113 return MVT::i32;
114}
115
116
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000117SDOperand MipsTargetLowering::
118LowerOperation(SDOperand Op, SelectionDAG &DAG)
119{
120 switch (Op.getOpcode())
121 {
122 case ISD::CALL: return LowerCALL(Op, DAG);
123 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
124 case ISD::RET: return LowerRET(Op, DAG);
125 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000126 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000127 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000128 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000129 }
130 return SDOperand();
131}
132
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000133MachineBasicBlock *
134MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
135 MachineBasicBlock *BB)
136{
137 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
138 switch (MI->getOpcode()) {
139 default: assert(false && "Unexpected instr type to insert");
140 case Mips::Select_CC: {
141 // To "insert" a SELECT_CC instruction, we actually have to insert the
142 // diamond control-flow pattern. The incoming instruction knows the
143 // destination vreg to set, the condition code register to branch on, the
144 // true/false values to select between, and a branch opcode to use.
145 const BasicBlock *LLVM_BB = BB->getBasicBlock();
146 ilist<MachineBasicBlock>::iterator It = BB;
147 ++It;
148
149 // thisMBB:
150 // ...
151 // TrueVal = ...
152 // setcc r1, r2, r3
153 // bNE r1, r0, copy1MBB
154 // fallthrough --> copy0MBB
155 MachineBasicBlock *thisMBB = BB;
156 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
157 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
158 BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
159 .addReg(Mips::ZERO).addMBB(sinkMBB);
160 MachineFunction *F = BB->getParent();
161 F->getBasicBlockList().insert(It, copy0MBB);
162 F->getBasicBlockList().insert(It, sinkMBB);
163 // Update machine-CFG edges by first adding all successors of the current
164 // block to the new block which will contain the Phi node for the select.
165 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
166 e = BB->succ_end(); i != e; ++i)
167 sinkMBB->addSuccessor(*i);
168 // Next, remove all successors of the current block, and add the true
169 // and fallthrough blocks as its successors.
170 while(!BB->succ_empty())
171 BB->removeSuccessor(BB->succ_begin());
172 BB->addSuccessor(copy0MBB);
173 BB->addSuccessor(sinkMBB);
174
175 // copy0MBB:
176 // %FalseValue = ...
177 // # fallthrough to sinkMBB
178 BB = copy0MBB;
179
180 // Update machine-CFG edges
181 BB->addSuccessor(sinkMBB);
182
183 // sinkMBB:
184 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
185 // ...
186 BB = sinkMBB;
187 BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
188 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
189 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
190
191 delete MI; // The pseudo instruction is gone now.
192 return BB;
193 }
194 }
195}
196
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000197//===----------------------------------------------------------------------===//
198// Lower helper functions
199//===----------------------------------------------------------------------===//
200
201// AddLiveIn - This helper function adds the specified physical register to the
202// MachineFunction as a live in value. It also creates a corresponding
203// virtual register for it.
204static unsigned
205AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
206{
207 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000208 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
209 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000210 return VReg;
211}
212
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000213//===----------------------------------------------------------------------===//
214// Misc Lower Operation implementation
215//===----------------------------------------------------------------------===//
216SDOperand MipsTargetLowering::
217LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
218{
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000219 SDOperand ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000220 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000221 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000222 bool isPIC = (getTargetMachine().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000223
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000224 SDOperand HiPart;
225 if (!isPIC) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000226 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000227 SDOperand Ops[] = { GA };
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000228 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
229 } else // Emit Load from Global Pointer
230 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000231
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000232 // On functions and global targets not internal linked only
233 // a load from got/GP is necessary for PIC to work.
234 if ((isPIC) && ((!GV->hasInternalLinkage()) || (isa<Function>(GV))))
235 return HiPart;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000236
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000237 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
238 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000239
240 return ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000241}
242
243SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000244LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
245{
246 assert(0 && "TLS not implemented for MIPS.");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000247 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000248}
249
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000250SDOperand MipsTargetLowering::
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000251LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG)
252{
253 SDOperand LHS = Op.getOperand(0);
254 SDOperand RHS = Op.getOperand(1);
255 SDOperand True = Op.getOperand(2);
256 SDOperand False = Op.getOperand(3);
257 SDOperand CC = Op.getOperand(4);
258
Duncan Sands83ec4b62008-06-06 12:08:01 +0000259 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000260 SDOperand Ops[] = { LHS, RHS, CC };
261 SDOperand SetCCRes = DAG.getNode(ISD::SETCC, VTs, 1, Ops, 3);
262
263 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
264 SetCCRes, True, False);
265}
266
267SDOperand MipsTargetLowering::
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000268LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
269{
270 SDOperand ResNode;
271 SDOperand HiPart;
272
Duncan Sands83ec4b62008-06-06 12:08:01 +0000273 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000274 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
275 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
276
277 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000278 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000279 SDOperand Ops[] = { JTI };
280 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
281 } else // Emit Load from Global Pointer
282 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
283
284 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
285 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
286
287 return ResNode;
288}
289
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000290//===----------------------------------------------------------------------===//
291// Calling Convention Implementation
292//
293// The lower operations present on calling convention works on this order:
294// LowerCALL (virt regs --> phys regs, virt regs --> stack)
295// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
296// LowerRET (virt regs --> phys regs)
297// LowerCALL (phys regs --> virt regs)
298//
299//===----------------------------------------------------------------------===//
300
301#include "MipsGenCallingConv.inc"
302
303//===----------------------------------------------------------------------===//
304// CALL Calling Convention Implementation
305//===----------------------------------------------------------------------===//
306
307/// Mips custom CALL implementation
308SDOperand MipsTargetLowering::
309LowerCALL(SDOperand Op, SelectionDAG &DAG)
310{
Chris Lattnere0b12152008-03-17 06:57:02 +0000311 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000312
313 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000314 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000315 default:
316 assert(0 && "Unsupported calling convention");
317 case CallingConv::Fast:
318 case CallingConv::C:
319 return LowerCCCCallTo(Op, DAG, CallingConv);
320 }
321}
322
323/// LowerCCCCallTo - functions arguments are copied from virtual
324/// regs to (physical regs)/(stack frame), CALLSEQ_START and
325/// CALLSEQ_END are emitted.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000326/// TODO: isVarArg, isTailCall, sret.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000327SDOperand MipsTargetLowering::
328LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
329{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000330 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000331
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000332 SDOperand Chain = Op.getOperand(0);
333 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000334 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
335
336 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000337
338 // Analyze operands of the call, assigning locations to each operand.
339 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000340 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
341
342 // To meet ABI, Mips must always allocate 16 bytes on
343 // the stack (even if less than 4 are used as arguments)
Duncan Sands83ec4b62008-06-06 12:08:01 +0000344 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000345 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000346
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000347 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
348
349 // Get a count of how many bytes are to be pushed on the stack.
350 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000351 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
352 getPointerTy()));
353
354 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
355 SmallVector<SDOperand, 8> MemOpChains;
356
Chris Lattnere0b12152008-03-17 06:57:02 +0000357 int LastStackLoc = 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000358
359 // Walk the register/memloc assignments, inserting copies/loads.
360 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
361 CCValAssign &VA = ArgLocs[i];
362
363 // Arguments start after the 5 first operands of ISD::CALL
364 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
365
366 // Promote the value if needed.
367 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000368 default: assert(0 && "Unknown loc info!");
369 case CCValAssign::Full: break;
370 case CCValAssign::SExt:
371 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
372 break;
373 case CCValAssign::ZExt:
374 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
375 break;
376 case CCValAssign::AExt:
377 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
378 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000379 }
380
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000381 // Arguments that can be passed on register must be kept at
382 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000383 if (VA.isRegLoc()) {
384 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000385 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000387
388 assert(VA.isMemLoc());
389
390 // Create the frame index object for this incoming parameter
391 // This guarantees that when allocating Local Area the firsts
392 // 16 bytes which are alwayes reserved won't be overwritten.
393 LastStackLoc = (16 + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000394 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Chris Lattnere0b12152008-03-17 06:57:02 +0000395 LastStackLoc);
396
397 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
398
399 // emit ISD::STORE whichs stores the
400 // parameter value to a stack Location
401 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000402 }
403
404 // Transform all store nodes into one single node because
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000405 // all store nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000406 if (!MemOpChains.empty())
407 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
408 &MemOpChains[0], MemOpChains.size());
409
410 // Build a sequence of copy-to-reg nodes chained together with token
411 // chain and flag operands which copy the outgoing args into registers.
412 // The InFlag in necessary since all emited instructions must be
413 // stuck together.
414 SDOperand InFlag;
415 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
416 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
417 RegsToPass[i].second, InFlag);
418 InFlag = Chain.getValue(1);
419 }
420
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000421 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
422 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000423 // node so that legalize doesn't hack it.
424 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000425 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000426 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000427 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
428
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000429
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000430 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
431 // = Chain, Callee, Reg#1, Reg#2, ...
432 //
433 // Returns a chain & a flag for retval copy to use.
434 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
435 SmallVector<SDOperand, 8> Ops;
436 Ops.push_back(Chain);
437 Ops.push_back(Callee);
438
439 // Add argument registers to the end of the list so that they are
440 // known live into the call.
441 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
442 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
443 RegsToPass[i].second.getValueType()));
444
445 if (InFlag.Val)
446 Ops.push_back(InFlag);
447
448 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
449 InFlag = Chain.getValue(1);
450
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000451 // Create the CALLSEQ_END node.
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000452 Chain = DAG.getCALLSEQ_END(Chain,
453 DAG.getConstant(NumBytes, getPointerTy()),
454 DAG.getConstant(0, getPointerTy()),
455 InFlag);
456 InFlag = Chain.getValue(1);
457
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000458 // Create a stack location to hold GP when PIC is used. This stack
459 // location is used on function prologue to save GP and also after all
460 // emited CALL's to restore GP.
461 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000462 // Function can have an arbitrary number of calls, so
463 // hold the LastStackLoc with the biggest offset.
464 int FI;
465 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
466 if (LastStackLoc >= MipsFI->getGPStackOffset()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000467 LastStackLoc = (!LastStackLoc) ? (16) : (LastStackLoc+4);
468 // Create the frame index only once. SPOffset here can be anything
469 // (this will be fixed on processFunctionBeforeFrameFinalized)
470 if (MipsFI->getGPStackOffset() == -1) {
471 FI = MFI->CreateFixedObject(4, 0);
472 MipsFI->setGPFI(FI);
473 }
474 MipsFI->setGPStackOffset(LastStackLoc);
475 }
476
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000477 // Reload GP value.
478 FI = MipsFI->getGPFI();
479 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
480 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
481 Chain = GPLoad.getValue(1);
482 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
483 GPLoad, SDOperand(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000484 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000485 }
486
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000487 // Handle result values, copying them out of physregs into vregs that we
488 // return.
489 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
490}
491
492/// LowerCallResult - Lower the result values of an ISD::CALL into the
493/// appropriate copies out of appropriate physical registers. This assumes that
494/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
495/// being lowered. Returns a SDNode with the same number of values as the
496/// ISD::CALL.
497SDNode *MipsTargetLowering::
498LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
499 unsigned CallingConv, SelectionDAG &DAG) {
500
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000501 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
502
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000503 // Assign locations to each value returned by this call.
504 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000505 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
506
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000507 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
508 SmallVector<SDOperand, 8> ResultVals;
509
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000510 // Copy all of the result registers out of their specified physreg.
511 for (unsigned i = 0; i != RVLocs.size(); ++i) {
512 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
513 RVLocs[i].getValVT(), InFlag).getValue(1);
514 InFlag = Chain.getValue(2);
515 ResultVals.push_back(Chain.getValue(0));
516 }
517
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000518 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000519
520 // Merge everything together with a MERGE_VALUES node.
Duncan Sandsf9516202008-06-30 10:19:09 +0000521 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
522 ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000523}
524
525//===----------------------------------------------------------------------===//
526// FORMAL_ARGUMENTS Calling Convention Implementation
527//===----------------------------------------------------------------------===//
528
529/// Mips custom FORMAL_ARGUMENTS implementation
530SDOperand MipsTargetLowering::
531LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
532{
533 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
534 switch(CC)
535 {
536 default:
537 assert(0 && "Unsupported calling convention");
538 case CallingConv::C:
539 return LowerCCCArguments(Op, DAG);
540 }
541}
542
543/// LowerCCCArguments - transform physical registers into
544/// virtual registers and generate load operations for
545/// arguments places on the stack.
546/// TODO: isVarArg, sret
547SDOperand MipsTargetLowering::
548LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
549{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000550 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000551 MachineFunction &MF = DAG.getMachineFunction();
552 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000553 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000554
555 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
556 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
557
558 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000559
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000560 // GP holds the GOT address on PIC calls.
561 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
562 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
563
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000564 // Assign locations to all of the incoming arguments.
565 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000566 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
567
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000568 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
569 SmallVector<SDOperand, 8> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000570 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000571
572 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
573
574 CCValAssign &VA = ArgLocs[i];
575
576 // Arguments stored on registers
577 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000578 MVT RegVT = VA.getLocVT();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579 TargetRegisterClass *RC;
580
581 if (RegVT == MVT::i32)
582 RC = Mips::CPURegsRegisterClass;
583 else
584 assert(0 && "support only Mips::CPURegsRegisterClass");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000585
586 // Transform the arguments stored on
587 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000588 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000589 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
590
591 // If this is an 8 or 16-bit value, it is really passed promoted
592 // to 32 bits. Insert an assert[sz]ext to capture this, then
593 // truncate to the right size.
594 if (VA.getLocInfo() == CCValAssign::SExt)
595 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
596 DAG.getValueType(VA.getValVT()));
597 else if (VA.getLocInfo() == CCValAssign::ZExt)
598 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
599 DAG.getValueType(VA.getValVT()));
600
601 if (VA.getLocInfo() != CCValAssign::Full)
602 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
603
604 ArgValues.push_back(ArgValue);
605
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000606 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000607 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000608 if (isVarArg) {
609
610 if (StackPtr.Val == 0)
611 StackPtr = DAG.getRegister(StackReg, getPointerTy());
612
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000613 // The stack pointer offset is relative to the caller stack frame.
614 // Since the real stack size is unknown here, a negative SPOffset
615 // is used so there's a way to adjust these offsets when the stack
616 // size get known (on EliminateFrameIndex). A dummy SPOffset is
617 // used instead of a direct negative address (which is recorded to
618 // be used on emitPrologue) to avoid mis-calc of the first stack
619 // offset on PEI::calculateFrameObjectOffsets.
620 // Arguments are always 32-bit.
621 int FI = MFI->CreateFixedObject(4, 0);
622 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000623 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
624
625 // emit ISD::STORE whichs stores the
626 // parameter value to a stack Location
627 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
628 }
629
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000630 } else {
631 // sanity check
632 assert(VA.isMemLoc());
633
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000634 // The stack pointer offset is relative to the caller stack frame.
635 // Since the real stack size is unknown here, a negative SPOffset
636 // is used so there's a way to adjust these offsets when the stack
637 // size get known (on EliminateFrameIndex). A dummy SPOffset is
638 // used instead of a direct negative address (which is recorded to
639 // be used on emitPrologue) to avoid mis-calc of the first stack
640 // offset on PEI::calculateFrameObjectOffsets.
641 // Arguments are always 32-bit.
642 int FI = MFI->CreateFixedObject(4, 0);
643 MipsFI->recordLoadArgsFI(FI, -(4+(16+VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000644
645 // Create load nodes to retrieve arguments from the stack
646 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
647 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
648 }
649 }
650 ArgValues.push_back(Root);
651
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000652 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +0000653 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
654 ArgValues.size()).getValue(Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000655}
656
657//===----------------------------------------------------------------------===//
658// Return Value Calling Convention Implementation
659//===----------------------------------------------------------------------===//
660
661SDOperand MipsTargetLowering::
662LowerRET(SDOperand Op, SelectionDAG &DAG)
663{
664 // CCValAssign - represent the assignment of
665 // the return value to a location
666 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000667 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
668 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000669
670 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000671 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000672
673 // Analize return values of ISD::RET
674 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
675
676 // If this is the first return lowered for this function, add
677 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000678 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000679 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000680 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000681 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000682 }
683
684 // The chain is always operand #0
685 SDOperand Chain = Op.getOperand(0);
686 SDOperand Flag;
687
688 // Copy the result values into the output registers.
689 for (unsigned i = 0; i != RVLocs.size(); ++i) {
690 CCValAssign &VA = RVLocs[i];
691 assert(VA.isRegLoc() && "Can only return in registers!");
692
693 // ISD::RET => ret chain, (regnum1,val1), ...
694 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000695 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000696
697 // guarantee that all emitted copies are
698 // stuck together, avoiding something bad
699 Flag = Chain.getValue(1);
700 }
701
702 // Return on Mips is always a "jr $ra"
703 if (Flag.Val)
704 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000705 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000706 else // Return Void
707 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000708 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000709}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000710
711//===----------------------------------------------------------------------===//
712// Mips Inline Assembly Support
713//===----------------------------------------------------------------------===//
714
715/// getConstraintType - Given a constraint letter, return the type of
716/// constraint it is for this target.
717MipsTargetLowering::ConstraintType MipsTargetLowering::
718getConstraintType(const std::string &Constraint) const
719{
720 if (Constraint.size() == 1) {
721 // Mips specific constrainy
722 // GCC config/mips/constraints.md
723 //
724 // 'd' : An address register. Equivalent to r
725 // unless generating MIPS16 code.
726 // 'y' : Equivalent to r; retained for
727 // backwards compatibility.
728 //
729 switch (Constraint[0]) {
730 default : break;
731 case 'd':
732 case 'y':
733 return C_RegisterClass;
734 break;
735 }
736 }
737 return TargetLowering::getConstraintType(Constraint);
738}
739
740std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +0000741getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000742{
743 if (Constraint.size() == 1) {
744 switch (Constraint[0]) {
745 case 'r':
746 return std::make_pair(0U, Mips::CPURegsRegisterClass);
747 break;
748 }
749 }
750 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
751}
752
753std::vector<unsigned> MipsTargetLowering::
754getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000755 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000756{
757 if (Constraint.size() != 1)
758 return std::vector<unsigned>();
759
760 switch (Constraint[0]) {
761 default : break;
762 case 'r':
763 // GCC Mips Constraint Letters
764 case 'd':
765 case 'y':
766 return make_vector<unsigned>(Mips::V0, Mips::V1, Mips::A0,
767 Mips::A1, Mips::A2, Mips::A3,
768 Mips::T0, Mips::T1, Mips::T2,
769 Mips::T3, Mips::T4, Mips::T5,
770 Mips::T6, Mips::T7, Mips::S0,
771 Mips::S1, Mips::S2, Mips::S3,
772 Mips::S4, Mips::S5, Mips::S6,
773 Mips::S7, Mips::T8, Mips::T9, 0);
774 break;
775 }
776 return std::vector<unsigned>();
777}