blob: ba2de4044db78e690406469bc46dfc9afd438d65 [file] [log] [blame]
Wesley Pecka70f28c2010-02-23 19:15:24 +00001//===-- MBlazeISelLowering.cpp - MBlaze DAG Lowering Implementation -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that MBlaze uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mblaze-lower"
16#include "MBlazeISelLowering.h"
17#include "MBlazeMachineFunction.h"
18#include "MBlazeTargetMachine.h"
19#include "MBlazeTargetObjectFile.h"
20#include "MBlazeSubtarget.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36using namespace llvm;
37
Wesley Peck8397be02010-12-09 03:42:04 +000038static bool CC_MBlaze_AssignReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
39 CCValAssign::LocInfo &LocInfo,
40 ISD::ArgFlagsTy &ArgFlags,
41 CCState &State);
42
Wesley Pecka70f28c2010-02-23 19:15:24 +000043const char *MBlazeTargetLowering::getTargetNodeName(unsigned Opcode) const {
44 switch (Opcode) {
45 case MBlazeISD::JmpLink : return "MBlazeISD::JmpLink";
46 case MBlazeISD::GPRel : return "MBlazeISD::GPRel";
47 case MBlazeISD::Wrap : return "MBlazeISD::Wrap";
48 case MBlazeISD::ICmp : return "MBlazeISD::ICmp";
49 case MBlazeISD::Ret : return "MBlazeISD::Ret";
50 case MBlazeISD::Select_CC : return "MBlazeISD::Select_CC";
51 default : return NULL;
52 }
53}
54
55MBlazeTargetLowering::MBlazeTargetLowering(MBlazeTargetMachine &TM)
56 : TargetLowering(TM, new MBlazeTargetObjectFile()) {
57 Subtarget = &TM.getSubtarget<MBlazeSubtarget>();
58
59 // MBlaze does not have i1 type, so use i32 for
60 // setcc operations results (slt, sgt, ...).
61 setBooleanContents(ZeroOrOneBooleanContent);
62
63 // Set up the register classes
Wesley Peck4da992a2010-10-21 19:48:38 +000064 addRegisterClass(MVT::i32, MBlaze::GPRRegisterClass);
Wesley Pecka70f28c2010-02-23 19:15:24 +000065 if (Subtarget->hasFPU()) {
Wesley Peck4da992a2010-10-21 19:48:38 +000066 addRegisterClass(MVT::f32, MBlaze::GPRRegisterClass);
Wesley Pecka70f28c2010-02-23 19:15:24 +000067 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
68 }
69
70 // Floating point operations which are not supported
71 setOperationAction(ISD::FREM, MVT::f32, Expand);
72 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Expand);
73 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Expand);
74 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
75 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
76 setOperationAction(ISD::FP_ROUND, MVT::f32, Expand);
77 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
78 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
79 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
80 setOperationAction(ISD::FSIN, MVT::f32, Expand);
81 setOperationAction(ISD::FCOS, MVT::f32, Expand);
82 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
83 setOperationAction(ISD::FPOW, MVT::f32, Expand);
84 setOperationAction(ISD::FLOG, MVT::f32, Expand);
85 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
86 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
87 setOperationAction(ISD::FEXP, MVT::f32, Expand);
88
89 // Load extented operations for i1 types must be promoted
90 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
91 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
92 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
93
Wesley Peck4da992a2010-10-21 19:48:38 +000094 // Sign extended loads must be expanded
95 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
96 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
97
Wesley Pecka70f28c2010-02-23 19:15:24 +000098 // MBlaze has no REM or DIVREM operations.
99 setOperationAction(ISD::UREM, MVT::i32, Expand);
100 setOperationAction(ISD::SREM, MVT::i32, Expand);
101 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
102 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
103
104 // If the processor doesn't support multiply then expand it
105 if (!Subtarget->hasMul()) {
106 setOperationAction(ISD::MUL, MVT::i32, Expand);
107 }
108
109 // If the processor doesn't support 64-bit multiply then expand
110 if (!Subtarget->hasMul() || !Subtarget->hasMul64()) {
111 setOperationAction(ISD::MULHS, MVT::i32, Expand);
112 setOperationAction(ISD::MULHS, MVT::i64, Expand);
113 setOperationAction(ISD::MULHU, MVT::i32, Expand);
114 setOperationAction(ISD::MULHU, MVT::i64, Expand);
115 }
116
117 // If the processor doesn't support division then expand
118 if (!Subtarget->hasDiv()) {
119 setOperationAction(ISD::UDIV, MVT::i32, Expand);
120 setOperationAction(ISD::SDIV, MVT::i32, Expand);
121 }
122
123 // Expand unsupported conversions
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000124 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
125 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000126
127 // Expand SELECT_CC
128 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
129
130 // MBlaze doesn't have MUL_LOHI
131 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
132 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
133 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
134 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
135
136 // Used by legalize types to correctly generate the setcc result.
137 // Without this, every float setcc comes with a AND/OR with the result,
138 // we don't want this, since the fpcmp result goes to a flag register,
139 // which is used implicitly by brcond and select operations.
140 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
141 AddPromotedToType(ISD::SELECT, MVT::i1, MVT::i32);
142 AddPromotedToType(ISD::SELECT_CC, MVT::i1, MVT::i32);
143
144 // MBlaze Custom Operations
145 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
146 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
147 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
148 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
149
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000150 // Variable Argument support
151 setOperationAction(ISD::VASTART, MVT::Other, Custom);
152 setOperationAction(ISD::VAEND, MVT::Other, Expand);
153 setOperationAction(ISD::VAARG, MVT::Other, Expand);
154 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
155
156
Wesley Pecka70f28c2010-02-23 19:15:24 +0000157 // Operations not directly supported by MBlaze.
158 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
159 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
160 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
162 setOperationAction(ISD::ROTL, MVT::i32, Expand);
163 setOperationAction(ISD::ROTR, MVT::i32, Expand);
164 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
165 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
166 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
167 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
168 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
169 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
170 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
171
172 // We don't have line number support yet.
173 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
174
175 // Use the default for now
176 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
177 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000178
179 // MBlaze doesn't have extending float->double load/store
180 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
181 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
182
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000183 setMinFunctionAlignment(2);
184
Wesley Pecka70f28c2010-02-23 19:15:24 +0000185 setStackPointerRegisterToSaveRestore(MBlaze::R1);
186 computeRegisterProperties();
187}
188
189MVT::SimpleValueType MBlazeTargetLowering::getSetCCResultType(EVT VT) const {
190 return MVT::i32;
191}
192
Dan Gohmand858e902010-04-17 15:26:15 +0000193SDValue MBlazeTargetLowering::LowerOperation(SDValue Op,
194 SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000195 switch (Op.getOpcode())
196 {
197 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
198 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
199 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
200 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
201 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000202 case ISD::VASTART: return LowerVASTART(Op, DAG);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000203 }
204 return SDValue();
205}
206
207//===----------------------------------------------------------------------===//
208// Lower helper functions
209//===----------------------------------------------------------------------===//
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000210MachineBasicBlock*
211MBlazeTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000212 MachineBasicBlock *MBB)
213 const {
214 switch (MI->getOpcode()) {
215 default: assert(false && "Unexpected instr type to insert");
216
217 case MBlaze::ShiftRL:
218 case MBlaze::ShiftRA:
219 case MBlaze::ShiftL:
220 return EmitCustomShift(MI, MBB);
221
222 case MBlaze::Select_FCC:
223 case MBlaze::Select_CC:
224 return EmitCustomSelect(MI, MBB);
225
226 case MBlaze::CAS32:
227 case MBlaze::SWP32:
228 case MBlaze::LAA32:
229 case MBlaze::LAS32:
230 case MBlaze::LAD32:
231 case MBlaze::LAO32:
232 case MBlaze::LAX32:
233 case MBlaze::LAN32:
234 return EmitCustomAtomic(MI, MBB);
235
236 case MBlaze::MEMBARRIER:
237 // The Microblaze does not need memory barriers. Just delete the pseudo
238 // instruction and finish.
239 MI->eraseFromParent();
240 return MBB;
241 }
242}
243
244MachineBasicBlock*
245MBlazeTargetLowering::EmitCustomShift(MachineInstr *MI,
246 MachineBasicBlock *MBB) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000247 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
248 DebugLoc dl = MI->getDebugLoc();
249
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000250 // To "insert" a shift left instruction, we actually have to insert a
251 // simple loop. The incoming instruction knows the destination vreg to
252 // set, the source vreg to operate over and the shift amount.
253 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
254 MachineFunction::iterator It = MBB;
255 ++It;
256
257 // start:
258 // andi samt, samt, 31
259 // beqid samt, finish
260 // add dst, src, r0
261 // loop:
262 // addik samt, samt, -1
263 // sra dst, dst
264 // bneid samt, loop
265 // nop
266 // finish:
267 MachineFunction *F = MBB->getParent();
268 MachineRegisterInfo &R = F->getRegInfo();
269 MachineBasicBlock *loop = F->CreateMachineBasicBlock(LLVM_BB);
270 MachineBasicBlock *finish = F->CreateMachineBasicBlock(LLVM_BB);
271 F->insert(It, loop);
272 F->insert(It, finish);
273
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000274 // Update machine-CFG edges by transferring adding all successors and
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000275 // remaining instructions from the current block to the new block which
276 // will contain the Phi node for the select.
277 finish->splice(finish->begin(), MBB,
278 llvm::next(MachineBasicBlock::iterator(MI)),
279 MBB->end());
280 finish->transferSuccessorsAndUpdatePHIs(MBB);
281
282 // Add the true and fallthrough blocks as its successors.
283 MBB->addSuccessor(loop);
284 MBB->addSuccessor(finish);
285
286 // Next, add the finish block as a successor of the loop block
287 loop->addSuccessor(finish);
288 loop->addSuccessor(loop);
289
290 unsigned IAMT = R.createVirtualRegister(MBlaze::GPRRegisterClass);
291 BuildMI(MBB, dl, TII->get(MBlaze::ANDI), IAMT)
292 .addReg(MI->getOperand(2).getReg())
293 .addImm(31);
294
295 unsigned IVAL = R.createVirtualRegister(MBlaze::GPRRegisterClass);
296 BuildMI(MBB, dl, TII->get(MBlaze::ADDIK), IVAL)
297 .addReg(MI->getOperand(1).getReg())
298 .addImm(0);
299
300 BuildMI(MBB, dl, TII->get(MBlaze::BEQID))
301 .addReg(IAMT)
302 .addMBB(finish);
303
304 unsigned DST = R.createVirtualRegister(MBlaze::GPRRegisterClass);
305 unsigned NDST = R.createVirtualRegister(MBlaze::GPRRegisterClass);
306 BuildMI(loop, dl, TII->get(MBlaze::PHI), DST)
307 .addReg(IVAL).addMBB(MBB)
308 .addReg(NDST).addMBB(loop);
309
310 unsigned SAMT = R.createVirtualRegister(MBlaze::GPRRegisterClass);
311 unsigned NAMT = R.createVirtualRegister(MBlaze::GPRRegisterClass);
312 BuildMI(loop, dl, TII->get(MBlaze::PHI), SAMT)
313 .addReg(IAMT).addMBB(MBB)
314 .addReg(NAMT).addMBB(loop);
315
316 if (MI->getOpcode() == MBlaze::ShiftL)
317 BuildMI(loop, dl, TII->get(MBlaze::ADD), NDST).addReg(DST).addReg(DST);
318 else if (MI->getOpcode() == MBlaze::ShiftRA)
319 BuildMI(loop, dl, TII->get(MBlaze::SRA), NDST).addReg(DST);
320 else if (MI->getOpcode() == MBlaze::ShiftRL)
321 BuildMI(loop, dl, TII->get(MBlaze::SRL), NDST).addReg(DST);
322 else
323 llvm_unreachable("Cannot lower unknown shift instruction");
324
325 BuildMI(loop, dl, TII->get(MBlaze::ADDIK), NAMT)
326 .addReg(SAMT)
327 .addImm(-1);
328
329 BuildMI(loop, dl, TII->get(MBlaze::BNEID))
330 .addReg(NAMT)
331 .addMBB(loop);
332
333 BuildMI(*finish, finish->begin(), dl,
334 TII->get(MBlaze::PHI), MI->getOperand(0).getReg())
335 .addReg(IVAL).addMBB(MBB)
336 .addReg(NDST).addMBB(loop);
337
338 // The pseudo instruction is no longer needed so remove it
339 MI->eraseFromParent();
340 return finish;
341}
342
343MachineBasicBlock*
344MBlazeTargetLowering::EmitCustomSelect(MachineInstr *MI,
345 MachineBasicBlock *MBB) const {
346 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
347 DebugLoc dl = MI->getDebugLoc();
348
349 // To "insert" a SELECT_CC instruction, we actually have to insert the
350 // diamond control-flow pattern. The incoming instruction knows the
351 // destination vreg to set, the condition code register to branch on, the
352 // true/false values to select between, and a branch opcode to use.
353 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
354 MachineFunction::iterator It = MBB;
355 ++It;
356
357 // thisMBB:
358 // ...
359 // TrueVal = ...
360 // setcc r1, r2, r3
361 // bNE r1, r0, copy1MBB
362 // fallthrough --> copy0MBB
363 MachineFunction *F = MBB->getParent();
364 MachineBasicBlock *flsBB = F->CreateMachineBasicBlock(LLVM_BB);
365 MachineBasicBlock *dneBB = F->CreateMachineBasicBlock(LLVM_BB);
366
367 unsigned Opc;
368 switch (MI->getOperand(4).getImm()) {
369 default: llvm_unreachable("Unknown branch condition");
370 case MBlazeCC::EQ: Opc = MBlaze::BEQID; break;
371 case MBlazeCC::NE: Opc = MBlaze::BNEID; break;
372 case MBlazeCC::GT: Opc = MBlaze::BGTID; break;
373 case MBlazeCC::LT: Opc = MBlaze::BLTID; break;
374 case MBlazeCC::GE: Opc = MBlaze::BGEID; break;
375 case MBlazeCC::LE: Opc = MBlaze::BLEID; break;
376 }
377
378 F->insert(It, flsBB);
379 F->insert(It, dneBB);
380
381 // Transfer the remainder of MBB and its successor edges to dneBB.
382 dneBB->splice(dneBB->begin(), MBB,
383 llvm::next(MachineBasicBlock::iterator(MI)),
384 MBB->end());
385 dneBB->transferSuccessorsAndUpdatePHIs(MBB);
386
387 MBB->addSuccessor(flsBB);
388 MBB->addSuccessor(dneBB);
389 flsBB->addSuccessor(dneBB);
390
391 BuildMI(MBB, dl, TII->get(Opc))
392 .addReg(MI->getOperand(3).getReg())
393 .addMBB(dneBB);
394
395 // sinkMBB:
396 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
397 // ...
398 //BuildMI(dneBB, dl, TII->get(MBlaze::PHI), MI->getOperand(0).getReg())
399 // .addReg(MI->getOperand(1).getReg()).addMBB(flsBB)
400 // .addReg(MI->getOperand(2).getReg()).addMBB(BB);
401
402 BuildMI(*dneBB, dneBB->begin(), dl,
403 TII->get(MBlaze::PHI), MI->getOperand(0).getReg())
404 .addReg(MI->getOperand(2).getReg()).addMBB(flsBB)
405 .addReg(MI->getOperand(1).getReg()).addMBB(MBB);
406
407 MI->eraseFromParent(); // The pseudo instruction is gone now.
408 return dneBB;
409}
410
411MachineBasicBlock*
412MBlazeTargetLowering::EmitCustomAtomic(MachineInstr *MI,
413 MachineBasicBlock *MBB) const {
414 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
415 DebugLoc dl = MI->getDebugLoc();
416
417 // All atomic instructions on the Microblaze are implemented using the
418 // load-linked / store-conditional style atomic instruction sequences.
419 // Thus, all operations will look something like the following:
Eric Christopher471e4222011-06-08 23:55:35 +0000420 //
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000421 // start:
422 // lwx RV, RP, 0
423 // <do stuff>
424 // swx RV, RP, 0
425 // addic RC, R0, 0
426 // bneid RC, start
427 //
428 // exit:
429 //
430 // To "insert" a shift left instruction, we actually have to insert a
431 // simple loop. The incoming instruction knows the destination vreg to
432 // set, the source vreg to operate over and the shift amount.
433 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
434 MachineFunction::iterator It = MBB;
435 ++It;
436
437 // start:
438 // andi samt, samt, 31
439 // beqid samt, finish
440 // add dst, src, r0
441 // loop:
442 // addik samt, samt, -1
443 // sra dst, dst
444 // bneid samt, loop
445 // nop
446 // finish:
447 MachineFunction *F = MBB->getParent();
448 MachineRegisterInfo &R = F->getRegInfo();
449
450 // Create the start and exit basic blocks for the atomic operation
451 MachineBasicBlock *start = F->CreateMachineBasicBlock(LLVM_BB);
452 MachineBasicBlock *exit = F->CreateMachineBasicBlock(LLVM_BB);
453 F->insert(It, start);
454 F->insert(It, exit);
455
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000456 // Update machine-CFG edges by transferring adding all successors and
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000457 // remaining instructions from the current block to the new block which
458 // will contain the Phi node for the select.
459 exit->splice(exit->begin(), MBB, llvm::next(MachineBasicBlock::iterator(MI)),
460 MBB->end());
461 exit->transferSuccessorsAndUpdatePHIs(MBB);
462
463 // Add the fallthrough block as its successors.
464 MBB->addSuccessor(start);
465
466 BuildMI(start, dl, TII->get(MBlaze::LWX), MI->getOperand(0).getReg())
467 .addReg(MI->getOperand(1).getReg())
468 .addReg(MBlaze::R0);
469
470 MachineBasicBlock *final = start;
471 unsigned finalReg = 0;
472
Wesley Pecka70f28c2010-02-23 19:15:24 +0000473 switch (MI->getOpcode()) {
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000474 default: llvm_unreachable("Cannot lower unknown atomic instruction!");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000475
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000476 case MBlaze::SWP32:
477 finalReg = MI->getOperand(2).getReg();
478 start->addSuccessor(exit);
479 start->addSuccessor(start);
480 break;
Dan Gohman14152b42010-07-06 20:24:04 +0000481
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000482 case MBlaze::LAN32:
483 case MBlaze::LAX32:
484 case MBlaze::LAO32:
485 case MBlaze::LAD32:
486 case MBlaze::LAS32:
487 case MBlaze::LAA32: {
488 unsigned opcode = 0;
489 switch (MI->getOpcode()) {
490 default: llvm_unreachable("Cannot lower unknown atomic load!");
491 case MBlaze::LAA32: opcode = MBlaze::ADDIK; break;
492 case MBlaze::LAS32: opcode = MBlaze::RSUBIK; break;
493 case MBlaze::LAD32: opcode = MBlaze::AND; break;
494 case MBlaze::LAO32: opcode = MBlaze::OR; break;
495 case MBlaze::LAX32: opcode = MBlaze::XOR; break;
496 case MBlaze::LAN32: opcode = MBlaze::AND; break;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000497 }
498
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000499 finalReg = R.createVirtualRegister(MBlaze::GPRRegisterClass);
500 start->addSuccessor(exit);
501 start->addSuccessor(start);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000502
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000503 BuildMI(start, dl, TII->get(opcode), finalReg)
504 .addReg(MI->getOperand(0).getReg())
505 .addReg(MI->getOperand(2).getReg());
Wesley Pecka70f28c2010-02-23 19:15:24 +0000506
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000507 if (MI->getOpcode() == MBlaze::LAN32) {
508 unsigned tmp = finalReg;
509 finalReg = R.createVirtualRegister(MBlaze::GPRRegisterClass);
510 BuildMI(start, dl, TII->get(MBlaze::XORI), finalReg)
511 .addReg(tmp)
512 .addImm(-1);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000513 }
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000514 break;
515 }
Wesley Pecka70f28c2010-02-23 19:15:24 +0000516
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000517 case MBlaze::CAS32: {
518 finalReg = MI->getOperand(3).getReg();
519 final = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +0000520
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000521 F->insert(It, final);
522 start->addSuccessor(exit);
523 start->addSuccessor(final);
524 final->addSuccessor(exit);
525 final->addSuccessor(start);
Dan Gohman258c58c2010-07-06 15:49:48 +0000526
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000527 unsigned CMP = R.createVirtualRegister(MBlaze::GPRRegisterClass);
528 BuildMI(start, dl, TII->get(MBlaze::CMP), CMP)
529 .addReg(MI->getOperand(0).getReg())
530 .addReg(MI->getOperand(2).getReg());
Dan Gohman258c58c2010-07-06 15:49:48 +0000531
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000532 BuildMI(start, dl, TII->get(MBlaze::BNEID))
533 .addReg(CMP)
534 .addMBB(exit);
Dan Gohman14152b42010-07-06 20:24:04 +0000535
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000536 final->moveAfter(start);
537 exit->moveAfter(final);
538 break;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000539 }
540 }
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000541
542 unsigned CHK = R.createVirtualRegister(MBlaze::GPRRegisterClass);
543 BuildMI(final, dl, TII->get(MBlaze::SWX))
544 .addReg(finalReg)
545 .addReg(MI->getOperand(1).getReg())
546 .addReg(MBlaze::R0);
547
548 BuildMI(final, dl, TII->get(MBlaze::ADDIC), CHK)
549 .addReg(MBlaze::R0)
550 .addImm(0);
551
552 BuildMI(final, dl, TII->get(MBlaze::BNEID))
553 .addReg(CHK)
554 .addMBB(start);
555
556 // The pseudo instruction is no longer needed so remove it
557 MI->eraseFromParent();
558 return exit;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000559}
560
561//===----------------------------------------------------------------------===//
562// Misc Lower Operation implementation
563//===----------------------------------------------------------------------===//
564//
565
Dan Gohmand858e902010-04-17 15:26:15 +0000566SDValue MBlazeTargetLowering::LowerSELECT_CC(SDValue Op,
567 SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000568 SDValue LHS = Op.getOperand(0);
569 SDValue RHS = Op.getOperand(1);
570 SDValue TrueVal = Op.getOperand(2);
571 SDValue FalseVal = Op.getOperand(3);
572 DebugLoc dl = Op.getDebugLoc();
573 unsigned Opc;
574
575 SDValue CompareFlag;
576 if (LHS.getValueType() == MVT::i32) {
577 Opc = MBlazeISD::Select_CC;
578 CompareFlag = DAG.getNode(MBlazeISD::ICmp, dl, MVT::i32, LHS, RHS)
579 .getValue(1);
580 } else {
Wesley Peck0a67d922010-11-08 19:40:01 +0000581 llvm_unreachable("Cannot lower select_cc with unknown type");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000582 }
Wesley Peck0a67d922010-11-08 19:40:01 +0000583
Wesley Pecka70f28c2010-02-23 19:15:24 +0000584 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
585 CompareFlag);
586}
587
588SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000589LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000590 // FIXME there isn't actually debug info here
591 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000592 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Devang Patel0d881da2010-07-06 22:08:15 +0000593 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000594
595 return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, GA);
596}
597
598SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000599LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000600 llvm_unreachable("TLS not implemented for MicroBlaze.");
601 return SDValue(); // Not reached
602}
603
604SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000605LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000606 SDValue ResNode;
607 SDValue HiPart;
608 // FIXME there isn't actually debug info here
609 DebugLoc dl = Op.getDebugLoc();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000610
611 EVT PtrVT = Op.getValueType();
612 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
613
Wesley Peck0a67d922010-11-08 19:40:01 +0000614 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000615 return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, JTI);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000616}
617
618SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000619LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000620 SDValue ResNode;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000621 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000622 const Constant *C = N->getConstVal();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000623 DebugLoc dl = Op.getDebugLoc();
624
625 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Wesley Peck0a67d922010-11-08 19:40:01 +0000626 N->getOffset(), 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000627 return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, CP);
628}
629
Dan Gohmand858e902010-04-17 15:26:15 +0000630SDValue MBlazeTargetLowering::LowerVASTART(SDValue Op,
631 SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000632 MachineFunction &MF = DAG.getMachineFunction();
633 MBlazeFunctionInfo *FuncInfo = MF.getInfo<MBlazeFunctionInfo>();
634
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000635 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000636 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
637 getPointerTy());
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000638
639 // vastart just stores the address of the VarArgsFrameIndex slot into the
640 // memory location argument.
641 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000642 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
643 MachinePointerInfo(SV),
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000644 false, false, 0);
645}
646
Wesley Pecka70f28c2010-02-23 19:15:24 +0000647//===----------------------------------------------------------------------===//
648// Calling Convention Implementation
649//===----------------------------------------------------------------------===//
650
651#include "MBlazeGenCallingConv.inc"
652
Wesley Peck8397be02010-12-09 03:42:04 +0000653static bool CC_MBlaze_AssignReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
654 CCValAssign::LocInfo &LocInfo,
655 ISD::ArgFlagsTy &ArgFlags,
656 CCState &State) {
657 static const unsigned ArgRegs[] = {
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000658 MBlaze::R5, MBlaze::R6, MBlaze::R7,
659 MBlaze::R8, MBlaze::R9, MBlaze::R10
660 };
661
Wesley Peck8397be02010-12-09 03:42:04 +0000662 const unsigned NumArgRegs = array_lengthof(ArgRegs);
663 unsigned Reg = State.AllocateReg(ArgRegs, NumArgRegs);
664 if (!Reg) return false;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000665
Wesley Peck8397be02010-12-09 03:42:04 +0000666 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
667 State.AllocateStack(SizeInBytes, SizeInBytes);
668 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000669
Wesley Peck8397be02010-12-09 03:42:04 +0000670 return true;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000671}
672
Wesley Pecka70f28c2010-02-23 19:15:24 +0000673//===----------------------------------------------------------------------===//
674// Call Calling Convention Implementation
675//===----------------------------------------------------------------------===//
676
677/// LowerCall - functions arguments are copied from virtual regs to
678/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
679/// TODO: isVarArg, isTailCall.
680SDValue MBlazeTargetLowering::
681LowerCall(SDValue Chain, SDValue Callee, CallingConv::ID CallConv,
682 bool isVarArg, bool &isTailCall,
683 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000684 const SmallVectorImpl<SDValue> &OutVals,
Wesley Pecka70f28c2010-02-23 19:15:24 +0000685 const SmallVectorImpl<ISD::InputArg> &Ins,
686 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000687 SmallVectorImpl<SDValue> &InVals) const {
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000688 // MBlaze does not yet support tail call optimization
689 isTailCall = false;
690
Wesley Peck8397be02010-12-09 03:42:04 +0000691 // The MBlaze requires stack slots for arguments passed to var arg
692 // functions even if they are passed in registers.
693 bool needsRegArgSlots = isVarArg;
694
Wesley Pecka70f28c2010-02-23 19:15:24 +0000695 MachineFunction &MF = DAG.getMachineFunction();
696 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000697 const TargetFrameLowering &TFI = *MF.getTarget().getFrameLowering();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000698
699 // Analyze operands of the call, assigning locations to each operand.
700 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000701 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
702 getTargetMachine(), ArgLocs, *DAG.getContext());
Wesley Peck8397be02010-12-09 03:42:04 +0000703 CCInfo.AnalyzeCallOperands(Outs, CC_MBlaze);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000704
705 // Get a count of how many bytes are to be pushed on the stack.
706 unsigned NumBytes = CCInfo.getNextStackOffset();
Wesley Peck8397be02010-12-09 03:42:04 +0000707
708 // Variable argument function calls require a minimum of 24-bytes of stack
709 if (isVarArg && NumBytes < 24) NumBytes = 24;
710
Wesley Pecka70f28c2010-02-23 19:15:24 +0000711 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
712
713 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
714 SmallVector<SDValue, 8> MemOpChains;
715
Wesley Pecka70f28c2010-02-23 19:15:24 +0000716 // Walk the register/memloc assignments, inserting copies/loads.
717 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
718 CCValAssign &VA = ArgLocs[i];
Duncan Sands1e96bab2010-11-04 10:49:57 +0000719 MVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +0000720 SDValue Arg = OutVals[i];
Wesley Pecka70f28c2010-02-23 19:15:24 +0000721
722 // Promote the value if needed.
723 switch (VA.getLocInfo()) {
724 default: llvm_unreachable("Unknown loc info!");
725 case CCValAssign::Full: break;
726 case CCValAssign::SExt:
727 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
728 break;
729 case CCValAssign::ZExt:
730 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
731 break;
732 case CCValAssign::AExt:
733 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
734 break;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000735 }
736
737 // Arguments that can be passed on register must be kept at
738 // RegsToPass vector
739 if (VA.isRegLoc()) {
740 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
741 } else {
742 // Register can't get to this point...
743 assert(VA.isMemLoc());
744
Wesley Peck8397be02010-12-09 03:42:04 +0000745 // Since we are alread passing values on the stack we don't
746 // need to worry about creating additional slots for the
747 // values passed via registers.
748 needsRegArgSlots = false;
749
Wesley Pecka70f28c2010-02-23 19:15:24 +0000750 // Create the frame index object for this incoming parameter
Wesley Peck8397be02010-12-09 03:42:04 +0000751 unsigned ArgSize = VA.getValVT().getSizeInBits()/8;
752 unsigned StackLoc = VA.getLocMemOffset() + 4;
753 int FI = MFI->CreateFixedObject(ArgSize, StackLoc, true);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000754
755 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
756
757 // emit ISD::STORE whichs stores the
758 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +0000759 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
760 MachinePointerInfo(),
Wesley Pecka70f28c2010-02-23 19:15:24 +0000761 false, false, 0));
762 }
763 }
764
Wesley Peck8397be02010-12-09 03:42:04 +0000765 // If we need to reserve stack space for the arguments passed via registers
766 // then create a fixed stack object at the beginning of the stack.
767 if (needsRegArgSlots && TFI.hasReservedCallFrame(MF))
768 MFI->CreateFixedObject(28,0,true);
769
Wesley Pecka70f28c2010-02-23 19:15:24 +0000770 // Transform all store nodes into one single node because all store
771 // nodes are independent of each other.
772 if (!MemOpChains.empty())
773 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
774 &MemOpChains[0], MemOpChains.size());
775
776 // Build a sequence of copy-to-reg nodes chained together with token
777 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000778 // The InFlag in necessary since all emitted instructions must be
Wesley Pecka70f28c2010-02-23 19:15:24 +0000779 // stuck together.
780 SDValue InFlag;
781 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
782 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
783 RegsToPass[i].second, InFlag);
784 InFlag = Chain.getValue(1);
785 }
786
787 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
788 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
789 // node so that legalize doesn't hack it.
Wesley Pecka70f28c2010-02-23 19:15:24 +0000790 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000791 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Wesley Peck0a67d922010-11-08 19:40:01 +0000792 getPointerTy(), 0, 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000793 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
794 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Wesley Peck0a67d922010-11-08 19:40:01 +0000795 getPointerTy(), 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000796
797 // MBlazeJmpLink = #chain, #target_address, #opt_in_flags...
798 // = Chain, Callee, Reg#1, Reg#2, ...
799 //
800 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000801 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000802 SmallVector<SDValue, 8> Ops;
803 Ops.push_back(Chain);
804 Ops.push_back(Callee);
805
806 // Add argument registers to the end of the list so that they are
807 // known live into the call.
808 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
809 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
810 RegsToPass[i].second.getValueType()));
811 }
812
813 if (InFlag.getNode())
814 Ops.push_back(InFlag);
815
816 Chain = DAG.getNode(MBlazeISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
817 InFlag = Chain.getValue(1);
818
819 // Create the CALLSEQ_END node.
820 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
821 DAG.getIntPtrConstant(0, true), InFlag);
822 if (!Ins.empty())
823 InFlag = Chain.getValue(1);
824
825 // Handle result values, copying them out of physregs into vregs that we
826 // return.
827 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
828 Ins, dl, DAG, InVals);
829}
830
831/// LowerCallResult - Lower the result values of a call into the
832/// appropriate copies out of appropriate physical registers.
833SDValue MBlazeTargetLowering::
834LowerCallResult(SDValue Chain, SDValue InFlag, CallingConv::ID CallConv,
835 bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins,
836 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000837 SmallVectorImpl<SDValue> &InVals) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000838 // Assign locations to each value returned by this call.
839 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000840 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
841 getTargetMachine(), RVLocs, *DAG.getContext());
Wesley Pecka70f28c2010-02-23 19:15:24 +0000842
843 CCInfo.AnalyzeCallResult(Ins, RetCC_MBlaze);
844
845 // Copy all of the result registers out of their specified physreg.
846 for (unsigned i = 0; i != RVLocs.size(); ++i) {
847 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
848 RVLocs[i].getValVT(), InFlag).getValue(1);
849 InFlag = Chain.getValue(2);
850 InVals.push_back(Chain.getValue(0));
Wesley Peck0a67d922010-11-08 19:40:01 +0000851 }
Wesley Pecka70f28c2010-02-23 19:15:24 +0000852
853 return Chain;
854}
855
856//===----------------------------------------------------------------------===//
857// Formal Arguments Calling Convention Implementation
858//===----------------------------------------------------------------------===//
859
860/// LowerFormalArguments - transform physical registers into
861/// virtual registers and generate load operations for
862/// arguments places on the stack.
Wesley Pecka70f28c2010-02-23 19:15:24 +0000863SDValue MBlazeTargetLowering::
864LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
865 const SmallVectorImpl<ISD::InputArg> &Ins,
866 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000867 SmallVectorImpl<SDValue> &InVals) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000868 MachineFunction &MF = DAG.getMachineFunction();
869 MachineFrameInfo *MFI = MF.getFrameInfo();
870 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
871
872 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Dan Gohman1e93df62010-04-17 14:41:14 +0000873 MBlazeFI->setVarArgsFrameIndex(0);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000874
875 // Used with vargs to acumulate store chains.
876 std::vector<SDValue> OutChains;
877
878 // Keep track of the last register used for arguments
879 unsigned ArgRegEnd = 0;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000880
881 // Assign locations to all of the incoming arguments.
882 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000883 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
884 getTargetMachine(), ArgLocs, *DAG.getContext());
Wesley Pecka70f28c2010-02-23 19:15:24 +0000885
Wesley Peck8397be02010-12-09 03:42:04 +0000886 CCInfo.AnalyzeFormalArguments(Ins, CC_MBlaze);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000887 SDValue StackPtr;
888
Wesley Pecka70f28c2010-02-23 19:15:24 +0000889 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
890 CCValAssign &VA = ArgLocs[i];
891
892 // Arguments stored on registers
893 if (VA.isRegLoc()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +0000894 MVT RegVT = VA.getLocVT();
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000895 ArgRegEnd = VA.getLocReg();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000896 TargetRegisterClass *RC = 0;
897
898 if (RegVT == MVT::i32)
Wesley Peck4da992a2010-10-21 19:48:38 +0000899 RC = MBlaze::GPRRegisterClass;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000900 else if (RegVT == MVT::f32)
Wesley Peck4da992a2010-10-21 19:48:38 +0000901 RC = MBlaze::GPRRegisterClass;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000902 else
903 llvm_unreachable("RegVT not supported by LowerFormalArguments");
904
905 // Transform the arguments stored on
906 // physical registers into virtual ones
Devang Patel68e6bee2011-02-21 23:21:26 +0000907 unsigned Reg = MF.addLiveIn(ArgRegEnd, RC);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000908 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
909
910 // If this is an 8 or 16-bit value, it has been passed promoted
911 // to 32 bits. Insert an assert[sz]ext to capture this, then
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000912 // truncate to the right size. If if is a floating point value
913 // then convert to the correct type.
Wesley Pecka70f28c2010-02-23 19:15:24 +0000914 if (VA.getLocInfo() != CCValAssign::Full) {
915 unsigned Opcode = 0;
916 if (VA.getLocInfo() == CCValAssign::SExt)
917 Opcode = ISD::AssertSext;
918 else if (VA.getLocInfo() == CCValAssign::ZExt)
919 Opcode = ISD::AssertZext;
920 if (Opcode)
921 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
922 DAG.getValueType(VA.getValVT()));
923 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
924 }
925
926 InVals.push_back(ArgValue);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000927 } else { // VA.isRegLoc()
Wesley Pecka70f28c2010-02-23 19:15:24 +0000928 // sanity check
929 assert(VA.isMemLoc());
930
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000931 // The last argument is not a register
932 ArgRegEnd = 0;
933
Wesley Pecka70f28c2010-02-23 19:15:24 +0000934 // The stack pointer offset is relative to the caller stack frame.
935 // Since the real stack size is unknown here, a negative SPOffset
936 // is used so there's a way to adjust these offsets when the stack
937 // size get known (on EliminateFrameIndex). A dummy SPOffset is
938 // used instead of a direct negative address (which is recorded to
939 // be used on emitPrologue) to avoid mis-calc of the first stack
940 // offset on PEI::calculateFrameObjectOffsets.
941 // Arguments are always 32-bit.
942 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Wesley Peck8397be02010-12-09 03:42:04 +0000943 unsigned StackLoc = VA.getLocMemOffset() + 4;
Evan Chenged2ae132010-07-03 00:40:23 +0000944 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Wesley Peck8397be02010-12-09 03:42:04 +0000945 MBlazeFI->recordLoadArgsFI(FI, -StackLoc);
Wesley Peckeb133822010-12-12 20:52:31 +0000946 MBlazeFI->recordLiveIn(FI);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000947
948 // Create load nodes to retrieve arguments from the stack
949 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000950 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
951 MachinePointerInfo::getFixedStack(FI),
Wesley Pecka70f28c2010-02-23 19:15:24 +0000952 false, false, 0));
953 }
954 }
955
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000956 // To meet ABI, when VARARGS are passed on registers, the registers
957 // must have their values written to the caller stack frame. If the last
Wesley Peck0a67d922010-11-08 19:40:01 +0000958 // argument was placed in the stack, there's no need to save any register.
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000959 if ((isVarArg) && ArgRegEnd) {
960 if (StackPtr.getNode() == 0)
961 StackPtr = DAG.getRegister(StackReg, getPointerTy());
962
963 // The last register argument that must be saved is MBlaze::R10
Wesley Peck4da992a2010-10-21 19:48:38 +0000964 TargetRegisterClass *RC = MBlaze::GPRRegisterClass;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000965
966 unsigned Begin = MBlazeRegisterInfo::getRegisterNumbering(MBlaze::R5);
967 unsigned Start = MBlazeRegisterInfo::getRegisterNumbering(ArgRegEnd+1);
968 unsigned End = MBlazeRegisterInfo::getRegisterNumbering(MBlaze::R10);
Wesley Peck8397be02010-12-09 03:42:04 +0000969 unsigned StackLoc = Start - Begin + 1;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000970
971 for (; Start <= End; ++Start, ++StackLoc) {
972 unsigned Reg = MBlazeRegisterInfo::getRegisterFromNumbering(Start);
Devang Patel68e6bee2011-02-21 23:21:26 +0000973 unsigned LiveReg = MF.addLiveIn(Reg, RC);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000974 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, LiveReg, MVT::i32);
975
Evan Chenged2ae132010-07-03 00:40:23 +0000976 int FI = MFI->CreateFixedObject(4, 0, true);
Wesley Peck8397be02010-12-09 03:42:04 +0000977 MBlazeFI->recordStoreVarArgsFI(FI, -(StackLoc*4));
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000978 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +0000979 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
980 MachinePointerInfo(),
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000981 false, false, 0));
982
983 // Record the frame index of the first variable argument
984 // which is a value necessary to VASTART.
Dan Gohman1e93df62010-04-17 14:41:14 +0000985 if (!MBlazeFI->getVarArgsFrameIndex())
986 MBlazeFI->setVarArgsFrameIndex(FI);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000987 }
988 }
989
Wesley Peck0a67d922010-11-08 19:40:01 +0000990 // All stores are grouped in one node to allow the matching between
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000991 // the size of Ins and InVals. This only happens when on varg functions
992 if (!OutChains.empty()) {
993 OutChains.push_back(Chain);
994 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
995 &OutChains[0], OutChains.size());
996 }
997
Wesley Pecka70f28c2010-02-23 19:15:24 +0000998 return Chain;
999}
1000
1001//===----------------------------------------------------------------------===//
1002// Return Value Calling Convention Implementation
1003//===----------------------------------------------------------------------===//
1004
1005SDValue MBlazeTargetLowering::
1006LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1007 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001008 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001009 DebugLoc dl, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +00001010 // CCValAssign - represent the assignment of
1011 // the return value to a location
1012 SmallVector<CCValAssign, 16> RVLocs;
1013
1014 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00001015 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1016 getTargetMachine(), RVLocs, *DAG.getContext());
Wesley Pecka70f28c2010-02-23 19:15:24 +00001017
1018 // Analize return values.
1019 CCInfo.AnalyzeReturn(Outs, RetCC_MBlaze);
1020
1021 // If this is the first return lowered for this function, add
1022 // the regs to the liveout set for the function.
1023 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1024 for (unsigned i = 0; i != RVLocs.size(); ++i)
1025 if (RVLocs[i].isRegLoc())
1026 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1027 }
1028
1029 SDValue Flag;
1030
1031 // Copy the result values into the output registers.
1032 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1033 CCValAssign &VA = RVLocs[i];
1034 assert(VA.isRegLoc() && "Can only return in registers!");
1035
1036 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001037 OutVals[i], Flag);
Wesley Pecka70f28c2010-02-23 19:15:24 +00001038
1039 // guarantee that all emitted copies are
1040 // stuck together, avoiding something bad
1041 Flag = Chain.getValue(1);
1042 }
1043
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001044 // If this function is using the interrupt_handler calling convention
1045 // then use "rtid r14, 0" otherwise use "rtsd r15, 8"
Eric Christopher471e4222011-06-08 23:55:35 +00001046 unsigned Ret = (CallConv == llvm::CallingConv::MBLAZE_INTR) ? MBlazeISD::IRet
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001047 : MBlazeISD::Ret;
Eric Christopher471e4222011-06-08 23:55:35 +00001048 unsigned Reg = (CallConv == llvm::CallingConv::MBLAZE_INTR) ? MBlaze::R14
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001049 : MBlaze::R15;
1050 SDValue DReg = DAG.getRegister(Reg, MVT::i32);
1051
Wesley Pecka70f28c2010-02-23 19:15:24 +00001052 if (Flag.getNode())
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001053 return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg, Flag);
1054
1055 return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg);
Wesley Pecka70f28c2010-02-23 19:15:24 +00001056}
1057
1058//===----------------------------------------------------------------------===//
1059// MBlaze Inline Assembly Support
1060//===----------------------------------------------------------------------===//
1061
1062/// getConstraintType - Given a constraint letter, return the type of
1063/// constraint it is for this target.
1064MBlazeTargetLowering::ConstraintType MBlazeTargetLowering::
1065getConstraintType(const std::string &Constraint) const
1066{
1067 // MBlaze specific constrainy
1068 //
1069 // 'd' : An address register. Equivalent to r.
1070 // 'y' : Equivalent to r; retained for
1071 // backwards compatibility.
1072 // 'f' : Floating Point registers.
1073 if (Constraint.size() == 1) {
1074 switch (Constraint[0]) {
1075 default : break;
1076 case 'd':
1077 case 'y':
1078 case 'f':
1079 return C_RegisterClass;
1080 break;
1081 }
1082 }
1083 return TargetLowering::getConstraintType(Constraint);
1084}
1085
John Thompson44ab89e2010-10-29 17:29:13 +00001086/// Examine constraint type and operand type and determine a weight value.
1087/// This object must already have been set up with the operand type
1088/// and the current alternative constraint selected.
1089TargetLowering::ConstraintWeight
1090MBlazeTargetLowering::getSingleConstraintMatchWeight(
1091 AsmOperandInfo &info, const char *constraint) const {
1092 ConstraintWeight weight = CW_Invalid;
1093 Value *CallOperandVal = info.CallOperandVal;
1094 // If we don't have a value, we can't do a match,
1095 // but allow it at the lowest weight.
1096 if (CallOperandVal == NULL)
1097 return CW_Default;
1098 const Type *type = CallOperandVal->getType();
1099 // Look at the constraint type.
1100 switch (*constraint) {
1101 default:
1102 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
Chandler Carrutha4a2a032011-04-25 07:11:23 +00001103 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001104 case 'd':
1105 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001106 if (type->isIntegerTy())
1107 weight = CW_Register;
1108 break;
1109 case 'f':
1110 if (type->isFloatTy())
1111 weight = CW_Register;
1112 break;
1113 }
1114 return weight;
1115}
1116
Eric Christopher193f7e22011-06-29 19:12:24 +00001117/// Given a register class constraint, like 'r', if this corresponds directly
1118/// to an LLVM register class, return a register of 0 and the register class
1119/// pointer.
Wesley Pecka70f28c2010-02-23 19:15:24 +00001120std::pair<unsigned, const TargetRegisterClass*> MBlazeTargetLowering::
1121getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
1122 if (Constraint.size() == 1) {
1123 switch (Constraint[0]) {
1124 case 'r':
Wesley Peck4da992a2010-10-21 19:48:38 +00001125 return std::make_pair(0U, MBlaze::GPRRegisterClass);
Eric Christopher193f7e22011-06-29 19:12:24 +00001126 // TODO: These can't possibly be right, but match what was in
1127 // getRegClassForInlineAsmConstraint.
1128 case 'd':
1129 case 'y':
Wesley Pecka70f28c2010-02-23 19:15:24 +00001130 case 'f':
1131 if (VT == MVT::f32)
Wesley Peck4da992a2010-10-21 19:48:38 +00001132 return std::make_pair(0U, MBlaze::GPRRegisterClass);
Wesley Pecka70f28c2010-02-23 19:15:24 +00001133 }
1134 }
1135 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1136}
1137
Wesley Pecka70f28c2010-02-23 19:15:24 +00001138bool MBlazeTargetLowering::
1139isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1140 // The MBlaze target isn't yet aware of offsets.
1141 return false;
1142}
1143
1144bool MBlazeTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1145 return VT != MVT::f32;
1146}