blob: 0002174f2ba5dfbbcde295e9ee4a526d6da935a0 [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);
Duncan Sands28b77e92011-09-06 19:07:46 +000062 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Wesley Pecka70f28c2010-02-23 19:15:24 +000063
64 // Set up the register classes
Wesley Peck4da992a2010-10-21 19:48:38 +000065 addRegisterClass(MVT::i32, MBlaze::GPRRegisterClass);
Wesley Pecka70f28c2010-02-23 19:15:24 +000066 if (Subtarget->hasFPU()) {
Wesley Peck4da992a2010-10-21 19:48:38 +000067 addRegisterClass(MVT::f32, MBlaze::GPRRegisterClass);
Wesley Pecka70f28c2010-02-23 19:15:24 +000068 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
69 }
70
71 // Floating point operations which are not supported
72 setOperationAction(ISD::FREM, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +000073 setOperationAction(ISD::FMA, MVT::f32, Expand);
Wesley Pecka70f28c2010-02-23 19:15:24 +000074 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Expand);
75 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Expand);
76 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
77 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
78 setOperationAction(ISD::FP_ROUND, MVT::f32, Expand);
79 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
80 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
81 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
82 setOperationAction(ISD::FSIN, MVT::f32, Expand);
83 setOperationAction(ISD::FCOS, MVT::f32, Expand);
84 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
85 setOperationAction(ISD::FPOW, MVT::f32, Expand);
86 setOperationAction(ISD::FLOG, MVT::f32, Expand);
87 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
88 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
89 setOperationAction(ISD::FEXP, MVT::f32, Expand);
90
91 // Load extented operations for i1 types must be promoted
92 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
93 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
94 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
95
Wesley Peck4da992a2010-10-21 19:48:38 +000096 // Sign extended loads must be expanded
97 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
98 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
99
Wesley Pecka70f28c2010-02-23 19:15:24 +0000100 // MBlaze has no REM or DIVREM operations.
101 setOperationAction(ISD::UREM, MVT::i32, Expand);
102 setOperationAction(ISD::SREM, MVT::i32, Expand);
103 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
104 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
105
106 // If the processor doesn't support multiply then expand it
107 if (!Subtarget->hasMul()) {
108 setOperationAction(ISD::MUL, MVT::i32, Expand);
109 }
110
111 // If the processor doesn't support 64-bit multiply then expand
112 if (!Subtarget->hasMul() || !Subtarget->hasMul64()) {
113 setOperationAction(ISD::MULHS, MVT::i32, Expand);
114 setOperationAction(ISD::MULHS, MVT::i64, Expand);
115 setOperationAction(ISD::MULHU, MVT::i32, Expand);
116 setOperationAction(ISD::MULHU, MVT::i64, Expand);
117 }
118
119 // If the processor doesn't support division then expand
120 if (!Subtarget->hasDiv()) {
121 setOperationAction(ISD::UDIV, MVT::i32, Expand);
122 setOperationAction(ISD::SDIV, MVT::i32, Expand);
123 }
124
125 // Expand unsupported conversions
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000126 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
127 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000128
129 // Expand SELECT_CC
130 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
131
132 // MBlaze doesn't have MUL_LOHI
133 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
134 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
135 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
136 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
137
138 // Used by legalize types to correctly generate the setcc result.
139 // Without this, every float setcc comes with a AND/OR with the result,
140 // we don't want this, since the fpcmp result goes to a flag register,
141 // which is used implicitly by brcond and select operations.
142 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
143 AddPromotedToType(ISD::SELECT, MVT::i1, MVT::i32);
144 AddPromotedToType(ISD::SELECT_CC, MVT::i1, MVT::i32);
145
146 // MBlaze Custom Operations
147 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
148 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
149 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
150 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
151
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000152 // Variable Argument support
153 setOperationAction(ISD::VASTART, MVT::Other, Custom);
154 setOperationAction(ISD::VAEND, MVT::Other, Expand);
155 setOperationAction(ISD::VAARG, MVT::Other, Expand);
156 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
157
158
Wesley Pecka70f28c2010-02-23 19:15:24 +0000159 // Operations not directly supported by MBlaze.
160 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
161 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
162 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
163 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
164 setOperationAction(ISD::ROTL, MVT::i32, Expand);
165 setOperationAction(ISD::ROTR, MVT::i32, Expand);
166 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
167 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
168 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
169 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000170 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000171 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000172 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000173 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
174 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
175
176 // We don't have line number support yet.
177 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
178
179 // Use the default for now
180 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
181 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000182
183 // MBlaze doesn't have extending float->double load/store
184 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
185 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
186
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000187 setMinFunctionAlignment(2);
188
Wesley Pecka70f28c2010-02-23 19:15:24 +0000189 setStackPointerRegisterToSaveRestore(MBlaze::R1);
190 computeRegisterProperties();
191}
192
Duncan Sands28b77e92011-09-06 19:07:46 +0000193EVT MBlazeTargetLowering::getSetCCResultType(EVT VT) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000194 return MVT::i32;
195}
196
Dan Gohmand858e902010-04-17 15:26:15 +0000197SDValue MBlazeTargetLowering::LowerOperation(SDValue Op,
198 SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000199 switch (Op.getOpcode())
200 {
201 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
202 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
203 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
204 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
205 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000206 case ISD::VASTART: return LowerVASTART(Op, DAG);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000207 }
208 return SDValue();
209}
210
211//===----------------------------------------------------------------------===//
212// Lower helper functions
213//===----------------------------------------------------------------------===//
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000214MachineBasicBlock*
215MBlazeTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000216 MachineBasicBlock *MBB)
217 const {
218 switch (MI->getOpcode()) {
219 default: assert(false && "Unexpected instr type to insert");
220
221 case MBlaze::ShiftRL:
222 case MBlaze::ShiftRA:
223 case MBlaze::ShiftL:
224 return EmitCustomShift(MI, MBB);
225
226 case MBlaze::Select_FCC:
227 case MBlaze::Select_CC:
228 return EmitCustomSelect(MI, MBB);
229
230 case MBlaze::CAS32:
231 case MBlaze::SWP32:
232 case MBlaze::LAA32:
233 case MBlaze::LAS32:
234 case MBlaze::LAD32:
235 case MBlaze::LAO32:
236 case MBlaze::LAX32:
237 case MBlaze::LAN32:
238 return EmitCustomAtomic(MI, MBB);
239
240 case MBlaze::MEMBARRIER:
241 // The Microblaze does not need memory barriers. Just delete the pseudo
242 // instruction and finish.
243 MI->eraseFromParent();
244 return MBB;
245 }
246}
247
248MachineBasicBlock*
249MBlazeTargetLowering::EmitCustomShift(MachineInstr *MI,
250 MachineBasicBlock *MBB) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000251 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
252 DebugLoc dl = MI->getDebugLoc();
253
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000254 // To "insert" a shift left instruction, we actually have to insert a
255 // simple loop. The incoming instruction knows the destination vreg to
256 // set, the source vreg to operate over and the shift amount.
257 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
258 MachineFunction::iterator It = MBB;
259 ++It;
260
261 // start:
262 // andi samt, samt, 31
263 // beqid samt, finish
264 // add dst, src, r0
265 // loop:
266 // addik samt, samt, -1
267 // sra dst, dst
268 // bneid samt, loop
269 // nop
270 // finish:
271 MachineFunction *F = MBB->getParent();
272 MachineRegisterInfo &R = F->getRegInfo();
273 MachineBasicBlock *loop = F->CreateMachineBasicBlock(LLVM_BB);
274 MachineBasicBlock *finish = F->CreateMachineBasicBlock(LLVM_BB);
275 F->insert(It, loop);
276 F->insert(It, finish);
277
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000278 // Update machine-CFG edges by transferring adding all successors and
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000279 // remaining instructions from the current block to the new block which
280 // will contain the Phi node for the select.
281 finish->splice(finish->begin(), MBB,
282 llvm::next(MachineBasicBlock::iterator(MI)),
283 MBB->end());
284 finish->transferSuccessorsAndUpdatePHIs(MBB);
285
286 // Add the true and fallthrough blocks as its successors.
287 MBB->addSuccessor(loop);
288 MBB->addSuccessor(finish);
289
290 // Next, add the finish block as a successor of the loop block
291 loop->addSuccessor(finish);
292 loop->addSuccessor(loop);
293
294 unsigned IAMT = R.createVirtualRegister(MBlaze::GPRRegisterClass);
295 BuildMI(MBB, dl, TII->get(MBlaze::ANDI), IAMT)
296 .addReg(MI->getOperand(2).getReg())
297 .addImm(31);
298
299 unsigned IVAL = R.createVirtualRegister(MBlaze::GPRRegisterClass);
300 BuildMI(MBB, dl, TII->get(MBlaze::ADDIK), IVAL)
301 .addReg(MI->getOperand(1).getReg())
302 .addImm(0);
303
304 BuildMI(MBB, dl, TII->get(MBlaze::BEQID))
305 .addReg(IAMT)
306 .addMBB(finish);
307
308 unsigned DST = R.createVirtualRegister(MBlaze::GPRRegisterClass);
309 unsigned NDST = R.createVirtualRegister(MBlaze::GPRRegisterClass);
310 BuildMI(loop, dl, TII->get(MBlaze::PHI), DST)
311 .addReg(IVAL).addMBB(MBB)
312 .addReg(NDST).addMBB(loop);
313
314 unsigned SAMT = R.createVirtualRegister(MBlaze::GPRRegisterClass);
315 unsigned NAMT = R.createVirtualRegister(MBlaze::GPRRegisterClass);
316 BuildMI(loop, dl, TII->get(MBlaze::PHI), SAMT)
317 .addReg(IAMT).addMBB(MBB)
318 .addReg(NAMT).addMBB(loop);
319
320 if (MI->getOpcode() == MBlaze::ShiftL)
321 BuildMI(loop, dl, TII->get(MBlaze::ADD), NDST).addReg(DST).addReg(DST);
322 else if (MI->getOpcode() == MBlaze::ShiftRA)
323 BuildMI(loop, dl, TII->get(MBlaze::SRA), NDST).addReg(DST);
324 else if (MI->getOpcode() == MBlaze::ShiftRL)
325 BuildMI(loop, dl, TII->get(MBlaze::SRL), NDST).addReg(DST);
326 else
327 llvm_unreachable("Cannot lower unknown shift instruction");
328
329 BuildMI(loop, dl, TII->get(MBlaze::ADDIK), NAMT)
330 .addReg(SAMT)
331 .addImm(-1);
332
333 BuildMI(loop, dl, TII->get(MBlaze::BNEID))
334 .addReg(NAMT)
335 .addMBB(loop);
336
337 BuildMI(*finish, finish->begin(), dl,
338 TII->get(MBlaze::PHI), MI->getOperand(0).getReg())
339 .addReg(IVAL).addMBB(MBB)
340 .addReg(NDST).addMBB(loop);
341
342 // The pseudo instruction is no longer needed so remove it
343 MI->eraseFromParent();
344 return finish;
345}
346
347MachineBasicBlock*
348MBlazeTargetLowering::EmitCustomSelect(MachineInstr *MI,
349 MachineBasicBlock *MBB) const {
350 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
351 DebugLoc dl = MI->getDebugLoc();
352
353 // To "insert" a SELECT_CC instruction, we actually have to insert the
354 // diamond control-flow pattern. The incoming instruction knows the
355 // destination vreg to set, the condition code register to branch on, the
356 // true/false values to select between, and a branch opcode to use.
357 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
358 MachineFunction::iterator It = MBB;
359 ++It;
360
361 // thisMBB:
362 // ...
363 // TrueVal = ...
364 // setcc r1, r2, r3
365 // bNE r1, r0, copy1MBB
366 // fallthrough --> copy0MBB
367 MachineFunction *F = MBB->getParent();
368 MachineBasicBlock *flsBB = F->CreateMachineBasicBlock(LLVM_BB);
369 MachineBasicBlock *dneBB = F->CreateMachineBasicBlock(LLVM_BB);
370
371 unsigned Opc;
372 switch (MI->getOperand(4).getImm()) {
373 default: llvm_unreachable("Unknown branch condition");
374 case MBlazeCC::EQ: Opc = MBlaze::BEQID; break;
375 case MBlazeCC::NE: Opc = MBlaze::BNEID; break;
376 case MBlazeCC::GT: Opc = MBlaze::BGTID; break;
377 case MBlazeCC::LT: Opc = MBlaze::BLTID; break;
378 case MBlazeCC::GE: Opc = MBlaze::BGEID; break;
379 case MBlazeCC::LE: Opc = MBlaze::BLEID; break;
380 }
381
382 F->insert(It, flsBB);
383 F->insert(It, dneBB);
384
385 // Transfer the remainder of MBB and its successor edges to dneBB.
386 dneBB->splice(dneBB->begin(), MBB,
387 llvm::next(MachineBasicBlock::iterator(MI)),
388 MBB->end());
389 dneBB->transferSuccessorsAndUpdatePHIs(MBB);
390
391 MBB->addSuccessor(flsBB);
392 MBB->addSuccessor(dneBB);
393 flsBB->addSuccessor(dneBB);
394
395 BuildMI(MBB, dl, TII->get(Opc))
396 .addReg(MI->getOperand(3).getReg())
397 .addMBB(dneBB);
398
399 // sinkMBB:
400 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
401 // ...
402 //BuildMI(dneBB, dl, TII->get(MBlaze::PHI), MI->getOperand(0).getReg())
403 // .addReg(MI->getOperand(1).getReg()).addMBB(flsBB)
404 // .addReg(MI->getOperand(2).getReg()).addMBB(BB);
405
406 BuildMI(*dneBB, dneBB->begin(), dl,
407 TII->get(MBlaze::PHI), MI->getOperand(0).getReg())
408 .addReg(MI->getOperand(2).getReg()).addMBB(flsBB)
409 .addReg(MI->getOperand(1).getReg()).addMBB(MBB);
410
411 MI->eraseFromParent(); // The pseudo instruction is gone now.
412 return dneBB;
413}
414
415MachineBasicBlock*
416MBlazeTargetLowering::EmitCustomAtomic(MachineInstr *MI,
417 MachineBasicBlock *MBB) const {
418 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
419 DebugLoc dl = MI->getDebugLoc();
420
421 // All atomic instructions on the Microblaze are implemented using the
422 // load-linked / store-conditional style atomic instruction sequences.
423 // Thus, all operations will look something like the following:
Eric Christopher471e4222011-06-08 23:55:35 +0000424 //
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000425 // start:
426 // lwx RV, RP, 0
427 // <do stuff>
428 // swx RV, RP, 0
429 // addic RC, R0, 0
430 // bneid RC, start
431 //
432 // exit:
433 //
434 // To "insert" a shift left instruction, we actually have to insert a
435 // simple loop. The incoming instruction knows the destination vreg to
436 // set, the source vreg to operate over and the shift amount.
437 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
438 MachineFunction::iterator It = MBB;
439 ++It;
440
441 // start:
442 // andi samt, samt, 31
443 // beqid samt, finish
444 // add dst, src, r0
445 // loop:
446 // addik samt, samt, -1
447 // sra dst, dst
448 // bneid samt, loop
449 // nop
450 // finish:
451 MachineFunction *F = MBB->getParent();
452 MachineRegisterInfo &R = F->getRegInfo();
453
454 // Create the start and exit basic blocks for the atomic operation
455 MachineBasicBlock *start = F->CreateMachineBasicBlock(LLVM_BB);
456 MachineBasicBlock *exit = F->CreateMachineBasicBlock(LLVM_BB);
457 F->insert(It, start);
458 F->insert(It, exit);
459
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000460 // Update machine-CFG edges by transferring adding all successors and
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000461 // remaining instructions from the current block to the new block which
462 // will contain the Phi node for the select.
463 exit->splice(exit->begin(), MBB, llvm::next(MachineBasicBlock::iterator(MI)),
464 MBB->end());
465 exit->transferSuccessorsAndUpdatePHIs(MBB);
466
467 // Add the fallthrough block as its successors.
468 MBB->addSuccessor(start);
469
470 BuildMI(start, dl, TII->get(MBlaze::LWX), MI->getOperand(0).getReg())
471 .addReg(MI->getOperand(1).getReg())
472 .addReg(MBlaze::R0);
473
474 MachineBasicBlock *final = start;
475 unsigned finalReg = 0;
476
Wesley Pecka70f28c2010-02-23 19:15:24 +0000477 switch (MI->getOpcode()) {
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000478 default: llvm_unreachable("Cannot lower unknown atomic instruction!");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000479
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000480 case MBlaze::SWP32:
481 finalReg = MI->getOperand(2).getReg();
482 start->addSuccessor(exit);
483 start->addSuccessor(start);
484 break;
Dan Gohman14152b42010-07-06 20:24:04 +0000485
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000486 case MBlaze::LAN32:
487 case MBlaze::LAX32:
488 case MBlaze::LAO32:
489 case MBlaze::LAD32:
490 case MBlaze::LAS32:
491 case MBlaze::LAA32: {
492 unsigned opcode = 0;
493 switch (MI->getOpcode()) {
494 default: llvm_unreachable("Cannot lower unknown atomic load!");
495 case MBlaze::LAA32: opcode = MBlaze::ADDIK; break;
496 case MBlaze::LAS32: opcode = MBlaze::RSUBIK; break;
497 case MBlaze::LAD32: opcode = MBlaze::AND; break;
498 case MBlaze::LAO32: opcode = MBlaze::OR; break;
499 case MBlaze::LAX32: opcode = MBlaze::XOR; break;
500 case MBlaze::LAN32: opcode = MBlaze::AND; break;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000501 }
502
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000503 finalReg = R.createVirtualRegister(MBlaze::GPRRegisterClass);
504 start->addSuccessor(exit);
505 start->addSuccessor(start);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000506
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000507 BuildMI(start, dl, TII->get(opcode), finalReg)
508 .addReg(MI->getOperand(0).getReg())
509 .addReg(MI->getOperand(2).getReg());
Wesley Pecka70f28c2010-02-23 19:15:24 +0000510
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000511 if (MI->getOpcode() == MBlaze::LAN32) {
512 unsigned tmp = finalReg;
513 finalReg = R.createVirtualRegister(MBlaze::GPRRegisterClass);
514 BuildMI(start, dl, TII->get(MBlaze::XORI), finalReg)
515 .addReg(tmp)
516 .addImm(-1);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000517 }
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000518 break;
519 }
Wesley Pecka70f28c2010-02-23 19:15:24 +0000520
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000521 case MBlaze::CAS32: {
522 finalReg = MI->getOperand(3).getReg();
523 final = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +0000524
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000525 F->insert(It, final);
526 start->addSuccessor(exit);
527 start->addSuccessor(final);
528 final->addSuccessor(exit);
529 final->addSuccessor(start);
Dan Gohman258c58c2010-07-06 15:49:48 +0000530
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000531 unsigned CMP = R.createVirtualRegister(MBlaze::GPRRegisterClass);
532 BuildMI(start, dl, TII->get(MBlaze::CMP), CMP)
533 .addReg(MI->getOperand(0).getReg())
534 .addReg(MI->getOperand(2).getReg());
Dan Gohman258c58c2010-07-06 15:49:48 +0000535
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000536 BuildMI(start, dl, TII->get(MBlaze::BNEID))
537 .addReg(CMP)
538 .addMBB(exit);
Dan Gohman14152b42010-07-06 20:24:04 +0000539
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000540 final->moveAfter(start);
541 exit->moveAfter(final);
542 break;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000543 }
544 }
Wesley Peck6b3bbb12010-12-22 01:15:01 +0000545
546 unsigned CHK = R.createVirtualRegister(MBlaze::GPRRegisterClass);
547 BuildMI(final, dl, TII->get(MBlaze::SWX))
548 .addReg(finalReg)
549 .addReg(MI->getOperand(1).getReg())
550 .addReg(MBlaze::R0);
551
552 BuildMI(final, dl, TII->get(MBlaze::ADDIC), CHK)
553 .addReg(MBlaze::R0)
554 .addImm(0);
555
556 BuildMI(final, dl, TII->get(MBlaze::BNEID))
557 .addReg(CHK)
558 .addMBB(start);
559
560 // The pseudo instruction is no longer needed so remove it
561 MI->eraseFromParent();
562 return exit;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000563}
564
565//===----------------------------------------------------------------------===//
566// Misc Lower Operation implementation
567//===----------------------------------------------------------------------===//
568//
569
Dan Gohmand858e902010-04-17 15:26:15 +0000570SDValue MBlazeTargetLowering::LowerSELECT_CC(SDValue Op,
571 SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000572 SDValue LHS = Op.getOperand(0);
573 SDValue RHS = Op.getOperand(1);
574 SDValue TrueVal = Op.getOperand(2);
575 SDValue FalseVal = Op.getOperand(3);
576 DebugLoc dl = Op.getDebugLoc();
577 unsigned Opc;
578
579 SDValue CompareFlag;
580 if (LHS.getValueType() == MVT::i32) {
581 Opc = MBlazeISD::Select_CC;
582 CompareFlag = DAG.getNode(MBlazeISD::ICmp, dl, MVT::i32, LHS, RHS)
583 .getValue(1);
584 } else {
Wesley Peck0a67d922010-11-08 19:40:01 +0000585 llvm_unreachable("Cannot lower select_cc with unknown type");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000586 }
Wesley Peck0a67d922010-11-08 19:40:01 +0000587
Wesley Pecka70f28c2010-02-23 19:15:24 +0000588 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
589 CompareFlag);
590}
591
592SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000593LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000594 // FIXME there isn't actually debug info here
595 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000596 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Devang Patel0d881da2010-07-06 22:08:15 +0000597 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000598
599 return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, GA);
600}
601
602SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000603LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000604 llvm_unreachable("TLS not implemented for MicroBlaze.");
605 return SDValue(); // Not reached
606}
607
608SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000609LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000610 SDValue ResNode;
611 SDValue HiPart;
612 // FIXME there isn't actually debug info here
613 DebugLoc dl = Op.getDebugLoc();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000614
615 EVT PtrVT = Op.getValueType();
616 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
617
Wesley Peck0a67d922010-11-08 19:40:01 +0000618 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000619 return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, JTI);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000620}
621
622SDValue MBlazeTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000623LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000624 SDValue ResNode;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000625 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000626 const Constant *C = N->getConstVal();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000627 DebugLoc dl = Op.getDebugLoc();
628
629 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Wesley Peck0a67d922010-11-08 19:40:01 +0000630 N->getOffset(), 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000631 return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, CP);
632}
633
Dan Gohmand858e902010-04-17 15:26:15 +0000634SDValue MBlazeTargetLowering::LowerVASTART(SDValue Op,
635 SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000636 MachineFunction &MF = DAG.getMachineFunction();
637 MBlazeFunctionInfo *FuncInfo = MF.getInfo<MBlazeFunctionInfo>();
638
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000639 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000640 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
641 getPointerTy());
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000642
643 // vastart just stores the address of the VarArgsFrameIndex slot into the
644 // memory location argument.
645 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000646 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
647 MachinePointerInfo(SV),
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000648 false, false, 0);
649}
650
Wesley Pecka70f28c2010-02-23 19:15:24 +0000651//===----------------------------------------------------------------------===//
652// Calling Convention Implementation
653//===----------------------------------------------------------------------===//
654
655#include "MBlazeGenCallingConv.inc"
656
Wesley Peck8397be02010-12-09 03:42:04 +0000657static bool CC_MBlaze_AssignReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
658 CCValAssign::LocInfo &LocInfo,
659 ISD::ArgFlagsTy &ArgFlags,
660 CCState &State) {
661 static const unsigned ArgRegs[] = {
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000662 MBlaze::R5, MBlaze::R6, MBlaze::R7,
663 MBlaze::R8, MBlaze::R9, MBlaze::R10
664 };
665
Wesley Peck8397be02010-12-09 03:42:04 +0000666 const unsigned NumArgRegs = array_lengthof(ArgRegs);
667 unsigned Reg = State.AllocateReg(ArgRegs, NumArgRegs);
668 if (!Reg) return false;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000669
Wesley Peck8397be02010-12-09 03:42:04 +0000670 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
671 State.AllocateStack(SizeInBytes, SizeInBytes);
672 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000673
Wesley Peck8397be02010-12-09 03:42:04 +0000674 return true;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000675}
676
Wesley Pecka70f28c2010-02-23 19:15:24 +0000677//===----------------------------------------------------------------------===//
678// Call Calling Convention Implementation
679//===----------------------------------------------------------------------===//
680
681/// LowerCall - functions arguments are copied from virtual regs to
682/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
683/// TODO: isVarArg, isTailCall.
684SDValue MBlazeTargetLowering::
685LowerCall(SDValue Chain, SDValue Callee, CallingConv::ID CallConv,
686 bool isVarArg, bool &isTailCall,
687 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000688 const SmallVectorImpl<SDValue> &OutVals,
Wesley Pecka70f28c2010-02-23 19:15:24 +0000689 const SmallVectorImpl<ISD::InputArg> &Ins,
690 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000691 SmallVectorImpl<SDValue> &InVals) const {
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000692 // MBlaze does not yet support tail call optimization
693 isTailCall = false;
694
Wesley Peck8397be02010-12-09 03:42:04 +0000695 // The MBlaze requires stack slots for arguments passed to var arg
696 // functions even if they are passed in registers.
697 bool needsRegArgSlots = isVarArg;
698
Wesley Pecka70f28c2010-02-23 19:15:24 +0000699 MachineFunction &MF = DAG.getMachineFunction();
700 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000701 const TargetFrameLowering &TFI = *MF.getTarget().getFrameLowering();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000702
703 // Analyze operands of the call, assigning locations to each operand.
704 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000705 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
706 getTargetMachine(), ArgLocs, *DAG.getContext());
Wesley Peck8397be02010-12-09 03:42:04 +0000707 CCInfo.AnalyzeCallOperands(Outs, CC_MBlaze);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000708
709 // Get a count of how many bytes are to be pushed on the stack.
710 unsigned NumBytes = CCInfo.getNextStackOffset();
Wesley Peck8397be02010-12-09 03:42:04 +0000711
712 // Variable argument function calls require a minimum of 24-bytes of stack
713 if (isVarArg && NumBytes < 24) NumBytes = 24;
714
Wesley Pecka70f28c2010-02-23 19:15:24 +0000715 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
716
717 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
718 SmallVector<SDValue, 8> MemOpChains;
719
Wesley Pecka70f28c2010-02-23 19:15:24 +0000720 // Walk the register/memloc assignments, inserting copies/loads.
721 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
722 CCValAssign &VA = ArgLocs[i];
Duncan Sands1e96bab2010-11-04 10:49:57 +0000723 MVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +0000724 SDValue Arg = OutVals[i];
Wesley Pecka70f28c2010-02-23 19:15:24 +0000725
726 // Promote the value if needed.
727 switch (VA.getLocInfo()) {
728 default: llvm_unreachable("Unknown loc info!");
729 case CCValAssign::Full: break;
730 case CCValAssign::SExt:
731 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
732 break;
733 case CCValAssign::ZExt:
734 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
735 break;
736 case CCValAssign::AExt:
737 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
738 break;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000739 }
740
741 // Arguments that can be passed on register must be kept at
742 // RegsToPass vector
743 if (VA.isRegLoc()) {
744 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
745 } else {
746 // Register can't get to this point...
747 assert(VA.isMemLoc());
748
Wesley Peck8397be02010-12-09 03:42:04 +0000749 // Since we are alread passing values on the stack we don't
750 // need to worry about creating additional slots for the
751 // values passed via registers.
752 needsRegArgSlots = false;
753
Wesley Pecka70f28c2010-02-23 19:15:24 +0000754 // Create the frame index object for this incoming parameter
Wesley Peck8397be02010-12-09 03:42:04 +0000755 unsigned ArgSize = VA.getValVT().getSizeInBits()/8;
756 unsigned StackLoc = VA.getLocMemOffset() + 4;
757 int FI = MFI->CreateFixedObject(ArgSize, StackLoc, true);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000758
759 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
760
761 // emit ISD::STORE whichs stores the
762 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +0000763 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
764 MachinePointerInfo(),
Wesley Pecka70f28c2010-02-23 19:15:24 +0000765 false, false, 0));
766 }
767 }
768
Wesley Peck8397be02010-12-09 03:42:04 +0000769 // If we need to reserve stack space for the arguments passed via registers
770 // then create a fixed stack object at the beginning of the stack.
771 if (needsRegArgSlots && TFI.hasReservedCallFrame(MF))
772 MFI->CreateFixedObject(28,0,true);
773
Wesley Pecka70f28c2010-02-23 19:15:24 +0000774 // Transform all store nodes into one single node because all store
775 // nodes are independent of each other.
776 if (!MemOpChains.empty())
777 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
778 &MemOpChains[0], MemOpChains.size());
779
780 // Build a sequence of copy-to-reg nodes chained together with token
781 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000782 // The InFlag in necessary since all emitted instructions must be
Wesley Pecka70f28c2010-02-23 19:15:24 +0000783 // stuck together.
784 SDValue InFlag;
785 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
786 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
787 RegsToPass[i].second, InFlag);
788 InFlag = Chain.getValue(1);
789 }
790
791 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
792 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
793 // node so that legalize doesn't hack it.
Wesley Pecka70f28c2010-02-23 19:15:24 +0000794 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000795 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Wesley Peck0a67d922010-11-08 19:40:01 +0000796 getPointerTy(), 0, 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000797 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
798 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Wesley Peck0a67d922010-11-08 19:40:01 +0000799 getPointerTy(), 0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000800
801 // MBlazeJmpLink = #chain, #target_address, #opt_in_flags...
802 // = Chain, Callee, Reg#1, Reg#2, ...
803 //
804 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000805 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000806 SmallVector<SDValue, 8> Ops;
807 Ops.push_back(Chain);
808 Ops.push_back(Callee);
809
810 // Add argument registers to the end of the list so that they are
811 // known live into the call.
812 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
813 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
814 RegsToPass[i].second.getValueType()));
815 }
816
817 if (InFlag.getNode())
818 Ops.push_back(InFlag);
819
820 Chain = DAG.getNode(MBlazeISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
821 InFlag = Chain.getValue(1);
822
823 // Create the CALLSEQ_END node.
824 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
825 DAG.getIntPtrConstant(0, true), InFlag);
826 if (!Ins.empty())
827 InFlag = Chain.getValue(1);
828
829 // Handle result values, copying them out of physregs into vregs that we
830 // return.
831 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
832 Ins, dl, DAG, InVals);
833}
834
835/// LowerCallResult - Lower the result values of a call into the
836/// appropriate copies out of appropriate physical registers.
837SDValue MBlazeTargetLowering::
838LowerCallResult(SDValue Chain, SDValue InFlag, CallingConv::ID CallConv,
839 bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins,
840 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000841 SmallVectorImpl<SDValue> &InVals) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000842 // Assign locations to each value returned by this call.
843 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000844 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
845 getTargetMachine(), RVLocs, *DAG.getContext());
Wesley Pecka70f28c2010-02-23 19:15:24 +0000846
847 CCInfo.AnalyzeCallResult(Ins, RetCC_MBlaze);
848
849 // Copy all of the result registers out of their specified physreg.
850 for (unsigned i = 0; i != RVLocs.size(); ++i) {
851 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
852 RVLocs[i].getValVT(), InFlag).getValue(1);
853 InFlag = Chain.getValue(2);
854 InVals.push_back(Chain.getValue(0));
Wesley Peck0a67d922010-11-08 19:40:01 +0000855 }
Wesley Pecka70f28c2010-02-23 19:15:24 +0000856
857 return Chain;
858}
859
860//===----------------------------------------------------------------------===//
861// Formal Arguments Calling Convention Implementation
862//===----------------------------------------------------------------------===//
863
864/// LowerFormalArguments - transform physical registers into
865/// virtual registers and generate load operations for
866/// arguments places on the stack.
Wesley Pecka70f28c2010-02-23 19:15:24 +0000867SDValue MBlazeTargetLowering::
868LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
869 const SmallVectorImpl<ISD::InputArg> &Ins,
870 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000871 SmallVectorImpl<SDValue> &InVals) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000872 MachineFunction &MF = DAG.getMachineFunction();
873 MachineFrameInfo *MFI = MF.getFrameInfo();
874 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
875
876 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Dan Gohman1e93df62010-04-17 14:41:14 +0000877 MBlazeFI->setVarArgsFrameIndex(0);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000878
879 // Used with vargs to acumulate store chains.
880 std::vector<SDValue> OutChains;
881
882 // Keep track of the last register used for arguments
883 unsigned ArgRegEnd = 0;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000884
885 // Assign locations to all of the incoming arguments.
886 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000887 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
888 getTargetMachine(), ArgLocs, *DAG.getContext());
Wesley Pecka70f28c2010-02-23 19:15:24 +0000889
Wesley Peck8397be02010-12-09 03:42:04 +0000890 CCInfo.AnalyzeFormalArguments(Ins, CC_MBlaze);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000891 SDValue StackPtr;
892
Wesley Pecka70f28c2010-02-23 19:15:24 +0000893 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
894 CCValAssign &VA = ArgLocs[i];
895
896 // Arguments stored on registers
897 if (VA.isRegLoc()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +0000898 MVT RegVT = VA.getLocVT();
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000899 ArgRegEnd = VA.getLocReg();
Wesley Pecka70f28c2010-02-23 19:15:24 +0000900 TargetRegisterClass *RC = 0;
901
902 if (RegVT == MVT::i32)
Wesley Peck4da992a2010-10-21 19:48:38 +0000903 RC = MBlaze::GPRRegisterClass;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000904 else if (RegVT == MVT::f32)
Wesley Peck4da992a2010-10-21 19:48:38 +0000905 RC = MBlaze::GPRRegisterClass;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000906 else
907 llvm_unreachable("RegVT not supported by LowerFormalArguments");
908
909 // Transform the arguments stored on
910 // physical registers into virtual ones
Devang Patel68e6bee2011-02-21 23:21:26 +0000911 unsigned Reg = MF.addLiveIn(ArgRegEnd, RC);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000912 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
913
914 // If this is an 8 or 16-bit value, it has been passed promoted
915 // to 32 bits. Insert an assert[sz]ext to capture this, then
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000916 // truncate to the right size. If if is a floating point value
917 // then convert to the correct type.
Wesley Pecka70f28c2010-02-23 19:15:24 +0000918 if (VA.getLocInfo() != CCValAssign::Full) {
919 unsigned Opcode = 0;
920 if (VA.getLocInfo() == CCValAssign::SExt)
921 Opcode = ISD::AssertSext;
922 else if (VA.getLocInfo() == CCValAssign::ZExt)
923 Opcode = ISD::AssertZext;
924 if (Opcode)
925 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
926 DAG.getValueType(VA.getValVT()));
927 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
928 }
929
930 InVals.push_back(ArgValue);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000931 } else { // VA.isRegLoc()
Wesley Pecka70f28c2010-02-23 19:15:24 +0000932 // sanity check
933 assert(VA.isMemLoc());
934
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000935 // The last argument is not a register
936 ArgRegEnd = 0;
937
Wesley Pecka70f28c2010-02-23 19:15:24 +0000938 // The stack pointer offset is relative to the caller stack frame.
939 // Since the real stack size is unknown here, a negative SPOffset
940 // is used so there's a way to adjust these offsets when the stack
941 // size get known (on EliminateFrameIndex). A dummy SPOffset is
942 // used instead of a direct negative address (which is recorded to
943 // be used on emitPrologue) to avoid mis-calc of the first stack
944 // offset on PEI::calculateFrameObjectOffsets.
945 // Arguments are always 32-bit.
946 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Wesley Peck8397be02010-12-09 03:42:04 +0000947 unsigned StackLoc = VA.getLocMemOffset() + 4;
Evan Chenged2ae132010-07-03 00:40:23 +0000948 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Wesley Peck8397be02010-12-09 03:42:04 +0000949 MBlazeFI->recordLoadArgsFI(FI, -StackLoc);
Wesley Peckeb133822010-12-12 20:52:31 +0000950 MBlazeFI->recordLiveIn(FI);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000951
952 // Create load nodes to retrieve arguments from the stack
953 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000954 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
955 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000956 false, false, false, 0));
Wesley Pecka70f28c2010-02-23 19:15:24 +0000957 }
958 }
959
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000960 // To meet ABI, when VARARGS are passed on registers, the registers
961 // must have their values written to the caller stack frame. If the last
Wesley Peck0a67d922010-11-08 19:40:01 +0000962 // argument was placed in the stack, there's no need to save any register.
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000963 if ((isVarArg) && ArgRegEnd) {
964 if (StackPtr.getNode() == 0)
965 StackPtr = DAG.getRegister(StackReg, getPointerTy());
966
967 // The last register argument that must be saved is MBlaze::R10
Wesley Peck4da992a2010-10-21 19:48:38 +0000968 TargetRegisterClass *RC = MBlaze::GPRRegisterClass;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000969
Evan Cheng8cb2d612011-07-25 20:18:18 +0000970 unsigned Begin = getMBlazeRegisterNumbering(MBlaze::R5);
971 unsigned Start = getMBlazeRegisterNumbering(ArgRegEnd+1);
972 unsigned End = getMBlazeRegisterNumbering(MBlaze::R10);
Wesley Peck8397be02010-12-09 03:42:04 +0000973 unsigned StackLoc = Start - Begin + 1;
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000974
975 for (; Start <= End; ++Start, ++StackLoc) {
Evan Cheng617793d2011-07-25 22:16:37 +0000976 unsigned Reg = getMBlazeRegisterFromNumbering(Start);
Devang Patel68e6bee2011-02-21 23:21:26 +0000977 unsigned LiveReg = MF.addLiveIn(Reg, RC);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000978 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, LiveReg, MVT::i32);
979
Evan Chenged2ae132010-07-03 00:40:23 +0000980 int FI = MFI->CreateFixedObject(4, 0, true);
Wesley Peck8397be02010-12-09 03:42:04 +0000981 MBlazeFI->recordStoreVarArgsFI(FI, -(StackLoc*4));
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000982 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +0000983 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
984 MachinePointerInfo(),
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000985 false, false, 0));
986
987 // Record the frame index of the first variable argument
988 // which is a value necessary to VASTART.
Dan Gohman1e93df62010-04-17 14:41:14 +0000989 if (!MBlazeFI->getVarArgsFrameIndex())
990 MBlazeFI->setVarArgsFrameIndex(FI);
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000991 }
992 }
993
Wesley Peck0a67d922010-11-08 19:40:01 +0000994 // All stores are grouped in one node to allow the matching between
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000995 // the size of Ins and InVals. This only happens when on varg functions
996 if (!OutChains.empty()) {
997 OutChains.push_back(Chain);
998 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
999 &OutChains[0], OutChains.size());
1000 }
1001
Wesley Pecka70f28c2010-02-23 19:15:24 +00001002 return Chain;
1003}
1004
1005//===----------------------------------------------------------------------===//
1006// Return Value Calling Convention Implementation
1007//===----------------------------------------------------------------------===//
1008
1009SDValue MBlazeTargetLowering::
1010LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1011 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001012 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001013 DebugLoc dl, SelectionDAG &DAG) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +00001014 // CCValAssign - represent the assignment of
1015 // the return value to a location
1016 SmallVector<CCValAssign, 16> RVLocs;
1017
1018 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00001019 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1020 getTargetMachine(), RVLocs, *DAG.getContext());
Wesley Pecka70f28c2010-02-23 19:15:24 +00001021
1022 // Analize return values.
1023 CCInfo.AnalyzeReturn(Outs, RetCC_MBlaze);
1024
1025 // If this is the first return lowered for this function, add
1026 // the regs to the liveout set for the function.
1027 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1028 for (unsigned i = 0; i != RVLocs.size(); ++i)
1029 if (RVLocs[i].isRegLoc())
1030 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1031 }
1032
1033 SDValue Flag;
1034
1035 // Copy the result values into the output registers.
1036 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1037 CCValAssign &VA = RVLocs[i];
1038 assert(VA.isRegLoc() && "Can only return in registers!");
1039
1040 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001041 OutVals[i], Flag);
Wesley Pecka70f28c2010-02-23 19:15:24 +00001042
1043 // guarantee that all emitted copies are
1044 // stuck together, avoiding something bad
1045 Flag = Chain.getValue(1);
1046 }
1047
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001048 // If this function is using the interrupt_handler calling convention
1049 // then use "rtid r14, 0" otherwise use "rtsd r15, 8"
Eric Christopher471e4222011-06-08 23:55:35 +00001050 unsigned Ret = (CallConv == llvm::CallingConv::MBLAZE_INTR) ? MBlazeISD::IRet
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001051 : MBlazeISD::Ret;
Eric Christopher471e4222011-06-08 23:55:35 +00001052 unsigned Reg = (CallConv == llvm::CallingConv::MBLAZE_INTR) ? MBlaze::R14
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001053 : MBlaze::R15;
1054 SDValue DReg = DAG.getRegister(Reg, MVT::i32);
1055
Wesley Pecka70f28c2010-02-23 19:15:24 +00001056 if (Flag.getNode())
Wesley Peckdc9d87a2010-12-15 20:27:28 +00001057 return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg, Flag);
1058
1059 return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg);
Wesley Pecka70f28c2010-02-23 19:15:24 +00001060}
1061
1062//===----------------------------------------------------------------------===//
1063// MBlaze Inline Assembly Support
1064//===----------------------------------------------------------------------===//
1065
1066/// getConstraintType - Given a constraint letter, return the type of
1067/// constraint it is for this target.
1068MBlazeTargetLowering::ConstraintType MBlazeTargetLowering::
1069getConstraintType(const std::string &Constraint) const
1070{
1071 // MBlaze specific constrainy
1072 //
1073 // 'd' : An address register. Equivalent to r.
1074 // 'y' : Equivalent to r; retained for
1075 // backwards compatibility.
1076 // 'f' : Floating Point registers.
1077 if (Constraint.size() == 1) {
1078 switch (Constraint[0]) {
1079 default : break;
1080 case 'd':
1081 case 'y':
1082 case 'f':
1083 return C_RegisterClass;
1084 break;
1085 }
1086 }
1087 return TargetLowering::getConstraintType(Constraint);
1088}
1089
John Thompson44ab89e2010-10-29 17:29:13 +00001090/// Examine constraint type and operand type and determine a weight value.
1091/// This object must already have been set up with the operand type
1092/// and the current alternative constraint selected.
1093TargetLowering::ConstraintWeight
1094MBlazeTargetLowering::getSingleConstraintMatchWeight(
1095 AsmOperandInfo &info, const char *constraint) const {
1096 ConstraintWeight weight = CW_Invalid;
1097 Value *CallOperandVal = info.CallOperandVal;
1098 // If we don't have a value, we can't do a match,
1099 // but allow it at the lowest weight.
1100 if (CallOperandVal == NULL)
1101 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001102 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00001103 // Look at the constraint type.
1104 switch (*constraint) {
1105 default:
1106 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
Chandler Carrutha4a2a032011-04-25 07:11:23 +00001107 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001108 case 'd':
1109 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001110 if (type->isIntegerTy())
1111 weight = CW_Register;
1112 break;
1113 case 'f':
1114 if (type->isFloatTy())
1115 weight = CW_Register;
1116 break;
1117 }
1118 return weight;
1119}
1120
Eric Christopher193f7e22011-06-29 19:12:24 +00001121/// Given a register class constraint, like 'r', if this corresponds directly
1122/// to an LLVM register class, return a register of 0 and the register class
1123/// pointer.
Wesley Pecka70f28c2010-02-23 19:15:24 +00001124std::pair<unsigned, const TargetRegisterClass*> MBlazeTargetLowering::
1125getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
1126 if (Constraint.size() == 1) {
1127 switch (Constraint[0]) {
1128 case 'r':
Wesley Peck4da992a2010-10-21 19:48:38 +00001129 return std::make_pair(0U, MBlaze::GPRRegisterClass);
Eric Christopher193f7e22011-06-29 19:12:24 +00001130 // TODO: These can't possibly be right, but match what was in
1131 // getRegClassForInlineAsmConstraint.
1132 case 'd':
1133 case 'y':
Wesley Pecka70f28c2010-02-23 19:15:24 +00001134 case 'f':
1135 if (VT == MVT::f32)
Wesley Peck4da992a2010-10-21 19:48:38 +00001136 return std::make_pair(0U, MBlaze::GPRRegisterClass);
Wesley Pecka70f28c2010-02-23 19:15:24 +00001137 }
1138 }
1139 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1140}
1141
Wesley Pecka70f28c2010-02-23 19:15:24 +00001142bool MBlazeTargetLowering::
1143isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1144 // The MBlaze target isn't yet aware of offsets.
1145 return false;
1146}
1147
1148bool MBlazeTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1149 return VT != MVT::f32;
1150}