Convert assert(0) to llvm_unreachable
[oota-llvm.git] / lib / Target / MBlaze / MBlazeISelLowering.cpp
1 //===-- 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"
36 using namespace llvm;
37
38 static bool CC_MBlaze_AssignReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
39                                 CCValAssign::LocInfo &LocInfo,
40                                 ISD::ArgFlagsTy &ArgFlags,
41                                 CCState &State);
42
43 const 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
55 MBlazeTargetLowering::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   setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
63
64   // Set up the register classes
65   addRegisterClass(MVT::i32, MBlaze::GPRRegisterClass);
66   if (Subtarget->hasFPU()) {
67     addRegisterClass(MVT::f32, MBlaze::GPRRegisterClass);
68     setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
69   }
70
71   // Floating point operations which are not supported
72   setOperationAction(ISD::FREM,       MVT::f32, Expand);
73   setOperationAction(ISD::FMA,        MVT::f32, Expand);
74   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
96   // Sign extended loads must be expanded
97   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
98   setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
99
100   // 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
126   setOperationAction(ISD::BITCAST, MVT::f32, Expand);
127   setOperationAction(ISD::BITCAST, MVT::i32, Expand);
128
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
152   // 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
159   // 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);
170   setOperationAction(ISD::CTLZ_ZERO_UNDEF,    MVT::i32,   Expand);
171   setOperationAction(ISD::CTTZ,               MVT::i32,   Expand);
172   setOperationAction(ISD::CTTZ_ZERO_UNDEF,    MVT::i32,   Expand);
173   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);
182
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
187   setMinFunctionAlignment(2);
188
189   setStackPointerRegisterToSaveRestore(MBlaze::R1);
190   computeRegisterProperties();
191 }
192
193 EVT MBlazeTargetLowering::getSetCCResultType(EVT VT) const {
194   return MVT::i32;
195 }
196
197 SDValue MBlazeTargetLowering::LowerOperation(SDValue Op,
198                                              SelectionDAG &DAG) const {
199   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);
206     case ISD::VASTART:            return LowerVASTART(Op, DAG);
207   }
208   return SDValue();
209 }
210
211 //===----------------------------------------------------------------------===//
212 //  Lower helper functions
213 //===----------------------------------------------------------------------===//
214 MachineBasicBlock*
215 MBlazeTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
216                                                   MachineBasicBlock *MBB)
217                                                   const {
218   switch (MI->getOpcode()) {
219   default: llvm_unreachable("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
248 MachineBasicBlock*
249 MBlazeTargetLowering::EmitCustomShift(MachineInstr *MI,
250                                       MachineBasicBlock *MBB) const {
251   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
252   DebugLoc dl = MI->getDebugLoc();
253
254   // 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
278   // Update machine-CFG edges by transferring adding all successors and
279   // 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
347 MachineBasicBlock*
348 MBlazeTargetLowering::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
415 MachineBasicBlock*
416 MBlazeTargetLowering::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:
424   //
425   //  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
460   // Update machine-CFG edges by transferring adding all successors and
461   // 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
477   switch (MI->getOpcode()) {
478   default: llvm_unreachable("Cannot lower unknown atomic instruction!");
479
480   case MBlaze::SWP32:
481     finalReg = MI->getOperand(2).getReg();
482     start->addSuccessor(exit);
483     start->addSuccessor(start);
484     break;
485
486   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;
501     }
502
503     finalReg = R.createVirtualRegister(MBlaze::GPRRegisterClass);
504     start->addSuccessor(exit);
505     start->addSuccessor(start);
506
507     BuildMI(start, dl, TII->get(opcode), finalReg)
508       .addReg(MI->getOperand(0).getReg())
509       .addReg(MI->getOperand(2).getReg());
510
511     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);
517     }
518     break;
519   }
520
521   case MBlaze::CAS32: {
522     finalReg = MI->getOperand(3).getReg();
523     final = F->CreateMachineBasicBlock(LLVM_BB);
524
525     F->insert(It, final);
526     start->addSuccessor(exit);
527     start->addSuccessor(final);
528     final->addSuccessor(exit);
529     final->addSuccessor(start);
530
531     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());
535
536     BuildMI(start, dl, TII->get(MBlaze::BNEID))
537       .addReg(CMP)
538       .addMBB(exit);
539
540     final->moveAfter(start);
541     exit->moveAfter(final);
542     break;
543   }
544   }
545
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;
563 }
564
565 //===----------------------------------------------------------------------===//
566 //  Misc Lower Operation implementation
567 //===----------------------------------------------------------------------===//
568 //
569
570 SDValue MBlazeTargetLowering::LowerSELECT_CC(SDValue Op,
571                                              SelectionDAG &DAG) const {
572   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 {
585     llvm_unreachable("Cannot lower select_cc with unknown type");
586   }
587
588   return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
589                      CompareFlag);
590 }
591
592 SDValue MBlazeTargetLowering::
593 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
594   // FIXME there isn't actually debug info here
595   DebugLoc dl = Op.getDebugLoc();
596   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
597   SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
598
599   return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, GA);
600 }
601
602 SDValue MBlazeTargetLowering::
603 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
604   llvm_unreachable("TLS not implemented for MicroBlaze.");
605 }
606
607 SDValue MBlazeTargetLowering::
608 LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
609   SDValue ResNode;
610   SDValue HiPart;
611   // FIXME there isn't actually debug info here
612   DebugLoc dl = Op.getDebugLoc();
613
614   EVT PtrVT = Op.getValueType();
615   JumpTableSDNode *JT  = cast<JumpTableSDNode>(Op);
616
617   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 0);
618   return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, JTI);
619 }
620
621 SDValue MBlazeTargetLowering::
622 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
623   SDValue ResNode;
624   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
625   const Constant *C = N->getConstVal();
626   DebugLoc dl = Op.getDebugLoc();
627
628   SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
629                                          N->getOffset(), 0);
630   return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, CP);
631 }
632
633 SDValue MBlazeTargetLowering::LowerVASTART(SDValue Op,
634                                            SelectionDAG &DAG) const {
635   MachineFunction &MF = DAG.getMachineFunction();
636   MBlazeFunctionInfo *FuncInfo = MF.getInfo<MBlazeFunctionInfo>();
637
638   DebugLoc dl = Op.getDebugLoc();
639   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
640                                  getPointerTy());
641
642   // vastart just stores the address of the VarArgsFrameIndex slot into the
643   // memory location argument.
644   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
645   return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
646                       MachinePointerInfo(SV),
647                       false, false, 0);
648 }
649
650 //===----------------------------------------------------------------------===//
651 //                      Calling Convention Implementation
652 //===----------------------------------------------------------------------===//
653
654 #include "MBlazeGenCallingConv.inc"
655
656 static bool CC_MBlaze_AssignReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
657                                 CCValAssign::LocInfo &LocInfo,
658                                 ISD::ArgFlagsTy &ArgFlags,
659                                 CCState &State) {
660   static const unsigned ArgRegs[] = {
661     MBlaze::R5, MBlaze::R6, MBlaze::R7,
662     MBlaze::R8, MBlaze::R9, MBlaze::R10
663   };
664
665   const unsigned NumArgRegs = array_lengthof(ArgRegs);
666   unsigned Reg = State.AllocateReg(ArgRegs, NumArgRegs);
667   if (!Reg) return false;
668
669   unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
670   State.AllocateStack(SizeInBytes, SizeInBytes);
671   State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
672
673   return true;
674 }
675
676 //===----------------------------------------------------------------------===//
677 //                  Call Calling Convention Implementation
678 //===----------------------------------------------------------------------===//
679
680 /// LowerCall - functions arguments are copied from virtual regs to
681 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
682 /// TODO: isVarArg, isTailCall.
683 SDValue MBlazeTargetLowering::
684 LowerCall(SDValue Chain, SDValue Callee, CallingConv::ID CallConv,
685           bool isVarArg, bool &isTailCall,
686           const SmallVectorImpl<ISD::OutputArg> &Outs,
687           const SmallVectorImpl<SDValue> &OutVals,
688           const SmallVectorImpl<ISD::InputArg> &Ins,
689           DebugLoc dl, SelectionDAG &DAG,
690           SmallVectorImpl<SDValue> &InVals) const {
691   // MBlaze does not yet support tail call optimization
692   isTailCall = false;
693
694   // The MBlaze requires stack slots for arguments passed to var arg
695   // functions even if they are passed in registers.
696   bool needsRegArgSlots = isVarArg;
697
698   MachineFunction &MF = DAG.getMachineFunction();
699   MachineFrameInfo *MFI = MF.getFrameInfo();
700   const TargetFrameLowering &TFI = *MF.getTarget().getFrameLowering();
701
702   // Analyze operands of the call, assigning locations to each operand.
703   SmallVector<CCValAssign, 16> ArgLocs;
704   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
705                  getTargetMachine(), ArgLocs, *DAG.getContext());
706   CCInfo.AnalyzeCallOperands(Outs, CC_MBlaze);
707
708   // Get a count of how many bytes are to be pushed on the stack.
709   unsigned NumBytes = CCInfo.getNextStackOffset();
710
711   // Variable argument function calls require a minimum of 24-bytes of stack
712   if (isVarArg && NumBytes < 24) NumBytes = 24;
713
714   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
715
716   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
717   SmallVector<SDValue, 8> MemOpChains;
718
719   // Walk the register/memloc assignments, inserting copies/loads.
720   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
721     CCValAssign &VA = ArgLocs[i];
722     MVT RegVT = VA.getLocVT();
723     SDValue Arg = OutVals[i];
724
725     // Promote the value if needed.
726     switch (VA.getLocInfo()) {
727     default: llvm_unreachable("Unknown loc info!");
728     case CCValAssign::Full: break;
729     case CCValAssign::SExt:
730       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
731       break;
732     case CCValAssign::ZExt:
733       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
734       break;
735     case CCValAssign::AExt:
736       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
737       break;
738     }
739
740     // Arguments that can be passed on register must be kept at
741     // RegsToPass vector
742     if (VA.isRegLoc()) {
743       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
744     } else {
745       // Register can't get to this point...
746       assert(VA.isMemLoc());
747
748       // Since we are alread passing values on the stack we don't
749       // need to worry about creating additional slots for the
750       // values passed via registers.
751       needsRegArgSlots = false;
752
753       // Create the frame index object for this incoming parameter
754       unsigned ArgSize = VA.getValVT().getSizeInBits()/8;
755       unsigned StackLoc = VA.getLocMemOffset() + 4;
756       int FI = MFI->CreateFixedObject(ArgSize, StackLoc, true);
757
758       SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
759
760       // emit ISD::STORE whichs stores the
761       // parameter value to a stack Location
762       MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
763                                          MachinePointerInfo(),
764                                          false, false, 0));
765     }
766   }
767
768   // If we need to reserve stack space for the arguments passed via registers
769   // then create a fixed stack object at the beginning of the stack.
770   if (needsRegArgSlots && TFI.hasReservedCallFrame(MF))
771     MFI->CreateFixedObject(28,0,true);
772
773   // Transform all store nodes into one single node because all store
774   // nodes are independent of each other.
775   if (!MemOpChains.empty())
776     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
777                         &MemOpChains[0], MemOpChains.size());
778
779   // Build a sequence of copy-to-reg nodes chained together with token
780   // chain and flag operands which copy the outgoing args into registers.
781   // The InFlag in necessary since all emitted instructions must be
782   // stuck together.
783   SDValue InFlag;
784   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
785     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
786                              RegsToPass[i].second, InFlag);
787     InFlag = Chain.getValue(1);
788   }
789
790   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
791   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
792   // node so that legalize doesn't hack it.
793   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
794     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
795                                 getPointerTy(), 0, 0);
796   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
797     Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
798                                 getPointerTy(), 0);
799
800   // MBlazeJmpLink = #chain, #target_address, #opt_in_flags...
801   //             = Chain, Callee, Reg#1, Reg#2, ...
802   //
803   // Returns a chain & a flag for retval copy to use.
804   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
805   SmallVector<SDValue, 8> Ops;
806   Ops.push_back(Chain);
807   Ops.push_back(Callee);
808
809   // Add argument registers to the end of the list so that they are
810   // known live into the call.
811   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
812     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
813                                   RegsToPass[i].second.getValueType()));
814   }
815
816   if (InFlag.getNode())
817     Ops.push_back(InFlag);
818
819   Chain  = DAG.getNode(MBlazeISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
820   InFlag = Chain.getValue(1);
821
822   // Create the CALLSEQ_END node.
823   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
824                              DAG.getIntPtrConstant(0, true), InFlag);
825   if (!Ins.empty())
826     InFlag = Chain.getValue(1);
827
828   // Handle result values, copying them out of physregs into vregs that we
829   // return.
830   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
831                          Ins, dl, DAG, InVals);
832 }
833
834 /// LowerCallResult - Lower the result values of a call into the
835 /// appropriate copies out of appropriate physical registers.
836 SDValue MBlazeTargetLowering::
837 LowerCallResult(SDValue Chain, SDValue InFlag, CallingConv::ID CallConv,
838                 bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins,
839                 DebugLoc dl, SelectionDAG &DAG,
840                 SmallVectorImpl<SDValue> &InVals) const {
841   // Assign locations to each value returned by this call.
842   SmallVector<CCValAssign, 16> RVLocs;
843   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
844                  getTargetMachine(), RVLocs, *DAG.getContext());
845
846   CCInfo.AnalyzeCallResult(Ins, RetCC_MBlaze);
847
848   // Copy all of the result registers out of their specified physreg.
849   for (unsigned i = 0; i != RVLocs.size(); ++i) {
850     Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
851                                RVLocs[i].getValVT(), InFlag).getValue(1);
852     InFlag = Chain.getValue(2);
853     InVals.push_back(Chain.getValue(0));
854   }
855
856   return Chain;
857 }
858
859 //===----------------------------------------------------------------------===//
860 //             Formal Arguments Calling Convention Implementation
861 //===----------------------------------------------------------------------===//
862
863 /// LowerFormalArguments - transform physical registers into
864 /// virtual registers and generate load operations for
865 /// arguments places on the stack.
866 SDValue MBlazeTargetLowering::
867 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
868                      const SmallVectorImpl<ISD::InputArg> &Ins,
869                      DebugLoc dl, SelectionDAG &DAG,
870                      SmallVectorImpl<SDValue> &InVals) const {
871   MachineFunction &MF = DAG.getMachineFunction();
872   MachineFrameInfo *MFI = MF.getFrameInfo();
873   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
874
875   unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
876   MBlazeFI->setVarArgsFrameIndex(0);
877
878   // Used with vargs to acumulate store chains.
879   std::vector<SDValue> OutChains;
880
881   // Keep track of the last register used for arguments
882   unsigned ArgRegEnd = 0;
883
884   // Assign locations to all of the incoming arguments.
885   SmallVector<CCValAssign, 16> ArgLocs;
886   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
887                  getTargetMachine(), ArgLocs, *DAG.getContext());
888
889   CCInfo.AnalyzeFormalArguments(Ins, CC_MBlaze);
890   SDValue StackPtr;
891
892   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
893     CCValAssign &VA = ArgLocs[i];
894
895     // Arguments stored on registers
896     if (VA.isRegLoc()) {
897       MVT RegVT = VA.getLocVT();
898       ArgRegEnd = VA.getLocReg();
899       TargetRegisterClass *RC = 0;
900
901       if (RegVT == MVT::i32)
902         RC = MBlaze::GPRRegisterClass;
903       else if (RegVT == MVT::f32)
904         RC = MBlaze::GPRRegisterClass;
905       else
906         llvm_unreachable("RegVT not supported by LowerFormalArguments");
907
908       // Transform the arguments stored on
909       // physical registers into virtual ones
910       unsigned Reg = MF.addLiveIn(ArgRegEnd, RC);
911       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
912
913       // If this is an 8 or 16-bit value, it has been passed promoted
914       // to 32 bits.  Insert an assert[sz]ext to capture this, then
915       // truncate to the right size. If if is a floating point value
916       // then convert to the correct type.
917       if (VA.getLocInfo() != CCValAssign::Full) {
918         unsigned Opcode = 0;
919         if (VA.getLocInfo() == CCValAssign::SExt)
920           Opcode = ISD::AssertSext;
921         else if (VA.getLocInfo() == CCValAssign::ZExt)
922           Opcode = ISD::AssertZext;
923         if (Opcode)
924           ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
925                                  DAG.getValueType(VA.getValVT()));
926         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
927       }
928
929       InVals.push_back(ArgValue);
930     } else { // VA.isRegLoc()
931       // sanity check
932       assert(VA.isMemLoc());
933
934       // The last argument is not a register
935       ArgRegEnd = 0;
936
937       // The stack pointer offset is relative to the caller stack frame.
938       // Since the real stack size is unknown here, a negative SPOffset
939       // is used so there's a way to adjust these offsets when the stack
940       // size get known (on EliminateFrameIndex). A dummy SPOffset is
941       // used instead of a direct negative address (which is recorded to
942       // be used on emitPrologue) to avoid mis-calc of the first stack
943       // offset on PEI::calculateFrameObjectOffsets.
944       // Arguments are always 32-bit.
945       unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
946       unsigned StackLoc = VA.getLocMemOffset() + 4;
947       int FI = MFI->CreateFixedObject(ArgSize, 0, true);
948       MBlazeFI->recordLoadArgsFI(FI, -StackLoc);
949       MBlazeFI->recordLiveIn(FI);
950
951       // Create load nodes to retrieve arguments from the stack
952       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
953       InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
954                                    MachinePointerInfo::getFixedStack(FI),
955                                    false, false, false, 0));
956     }
957   }
958
959   // To meet ABI, when VARARGS are passed on registers, the registers
960   // must have their values written to the caller stack frame. If the last
961   // argument was placed in the stack, there's no need to save any register.
962   if ((isVarArg) && ArgRegEnd) {
963     if (StackPtr.getNode() == 0)
964       StackPtr = DAG.getRegister(StackReg, getPointerTy());
965
966     // The last register argument that must be saved is MBlaze::R10
967     TargetRegisterClass *RC = MBlaze::GPRRegisterClass;
968
969     unsigned Begin = getMBlazeRegisterNumbering(MBlaze::R5);
970     unsigned Start = getMBlazeRegisterNumbering(ArgRegEnd+1);
971     unsigned End   = getMBlazeRegisterNumbering(MBlaze::R10);
972     unsigned StackLoc = Start - Begin + 1;
973
974     for (; Start <= End; ++Start, ++StackLoc) {
975       unsigned Reg = getMBlazeRegisterFromNumbering(Start);
976       unsigned LiveReg = MF.addLiveIn(Reg, RC);
977       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, LiveReg, MVT::i32);
978
979       int FI = MFI->CreateFixedObject(4, 0, true);
980       MBlazeFI->recordStoreVarArgsFI(FI, -(StackLoc*4));
981       SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
982       OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
983                                        MachinePointerInfo(),
984                                        false, false, 0));
985
986       // Record the frame index of the first variable argument
987       // which is a value necessary to VASTART.
988       if (!MBlazeFI->getVarArgsFrameIndex())
989         MBlazeFI->setVarArgsFrameIndex(FI);
990     }
991   }
992
993   // All stores are grouped in one node to allow the matching between
994   // the size of Ins and InVals. This only happens when on varg functions
995   if (!OutChains.empty()) {
996     OutChains.push_back(Chain);
997     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
998                         &OutChains[0], OutChains.size());
999   }
1000
1001   return Chain;
1002 }
1003
1004 //===----------------------------------------------------------------------===//
1005 //               Return Value Calling Convention Implementation
1006 //===----------------------------------------------------------------------===//
1007
1008 SDValue MBlazeTargetLowering::
1009 LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1010             const SmallVectorImpl<ISD::OutputArg> &Outs,
1011             const SmallVectorImpl<SDValue> &OutVals,
1012             DebugLoc dl, SelectionDAG &DAG) const {
1013   // CCValAssign - represent the assignment of
1014   // the return value to a location
1015   SmallVector<CCValAssign, 16> RVLocs;
1016
1017   // CCState - Info about the registers and stack slot.
1018   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1019                  getTargetMachine(), RVLocs, *DAG.getContext());
1020
1021   // Analize return values.
1022   CCInfo.AnalyzeReturn(Outs, RetCC_MBlaze);
1023
1024   // If this is the first return lowered for this function, add
1025   // the regs to the liveout set for the function.
1026   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1027     for (unsigned i = 0; i != RVLocs.size(); ++i)
1028       if (RVLocs[i].isRegLoc())
1029         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1030   }
1031
1032   SDValue Flag;
1033
1034   // Copy the result values into the output registers.
1035   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1036     CCValAssign &VA = RVLocs[i];
1037     assert(VA.isRegLoc() && "Can only return in registers!");
1038
1039     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1040                              OutVals[i], Flag);
1041
1042     // guarantee that all emitted copies are
1043     // stuck together, avoiding something bad
1044     Flag = Chain.getValue(1);
1045   }
1046
1047   // If this function is using the interrupt_handler calling convention
1048   // then use "rtid r14, 0" otherwise use "rtsd r15, 8"
1049   unsigned Ret = (CallConv == llvm::CallingConv::MBLAZE_INTR) ? MBlazeISD::IRet
1050                                                               : MBlazeISD::Ret;
1051   unsigned Reg = (CallConv == llvm::CallingConv::MBLAZE_INTR) ? MBlaze::R14
1052                                                               : MBlaze::R15;
1053   SDValue DReg = DAG.getRegister(Reg, MVT::i32);
1054
1055   if (Flag.getNode())
1056     return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg, Flag);
1057
1058   return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg);
1059 }
1060
1061 //===----------------------------------------------------------------------===//
1062 //                           MBlaze Inline Assembly Support
1063 //===----------------------------------------------------------------------===//
1064
1065 /// getConstraintType - Given a constraint letter, return the type of
1066 /// constraint it is for this target.
1067 MBlazeTargetLowering::ConstraintType MBlazeTargetLowering::
1068 getConstraintType(const std::string &Constraint) const
1069 {
1070   // MBlaze specific constrainy
1071   //
1072   // 'd' : An address register. Equivalent to r.
1073   // 'y' : Equivalent to r; retained for
1074   //       backwards compatibility.
1075   // 'f' : Floating Point registers.
1076   if (Constraint.size() == 1) {
1077     switch (Constraint[0]) {
1078       default : break;
1079       case 'd':
1080       case 'y':
1081       case 'f':
1082         return C_RegisterClass;
1083     }
1084   }
1085   return TargetLowering::getConstraintType(Constraint);
1086 }
1087
1088 /// Examine constraint type and operand type and determine a weight value.
1089 /// This object must already have been set up with the operand type
1090 /// and the current alternative constraint selected.
1091 TargetLowering::ConstraintWeight
1092 MBlazeTargetLowering::getSingleConstraintMatchWeight(
1093     AsmOperandInfo &info, const char *constraint) const {
1094   ConstraintWeight weight = CW_Invalid;
1095   Value *CallOperandVal = info.CallOperandVal;
1096     // If we don't have a value, we can't do a match,
1097     // but allow it at the lowest weight.
1098   if (CallOperandVal == NULL)
1099     return CW_Default;
1100   Type *type = CallOperandVal->getType();
1101   // Look at the constraint type.
1102   switch (*constraint) {
1103   default:
1104     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1105     break;
1106   case 'd':
1107   case 'y':
1108     if (type->isIntegerTy())
1109       weight = CW_Register;
1110     break;
1111   case 'f':
1112     if (type->isFloatTy())
1113       weight = CW_Register;
1114     break;
1115   }
1116   return weight;
1117 }
1118
1119 /// Given a register class constraint, like 'r', if this corresponds directly
1120 /// to an LLVM register class, return a register of 0 and the register class
1121 /// pointer.
1122 std::pair<unsigned, const TargetRegisterClass*> MBlazeTargetLowering::
1123 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
1124   if (Constraint.size() == 1) {
1125     switch (Constraint[0]) {
1126     case 'r':
1127       return std::make_pair(0U, MBlaze::GPRRegisterClass);
1128       // TODO: These can't possibly be right, but match what was in
1129       // getRegClassForInlineAsmConstraint.
1130     case 'd':
1131     case 'y':
1132     case 'f':
1133       if (VT == MVT::f32)
1134         return std::make_pair(0U, MBlaze::GPRRegisterClass);
1135     }
1136   }
1137   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1138 }
1139
1140 bool MBlazeTargetLowering::
1141 isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1142   // The MBlaze target isn't yet aware of offsets.
1143   return false;
1144 }
1145
1146 bool MBlazeTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1147   return VT != MVT::f32;
1148 }