Implement rdar://6295824 and PR6724 with two tiny changes
[oota-llvm.git] / lib / Target / Blackfin / BlackfinISelLowering.cpp
1 //===- BlackfinISelLowering.cpp - Blackfin 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 implements the interfaces that Blackfin uses to lower LLVM code
11 // into a selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "BlackfinISelLowering.h"
16 #include "BlackfinTargetMachine.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/CallingConvLower.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
26 #include "llvm/ADT/VectorExtras.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 using namespace llvm;
30
31 //===----------------------------------------------------------------------===//
32 // Calling Convention Implementation
33 //===----------------------------------------------------------------------===//
34
35 #include "BlackfinGenCallingConv.inc"
36
37 //===----------------------------------------------------------------------===//
38 // TargetLowering Implementation
39 //===----------------------------------------------------------------------===//
40
41 BlackfinTargetLowering::BlackfinTargetLowering(TargetMachine &TM)
42   : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
43   setShiftAmountType(MVT::i16);
44   setBooleanContents(ZeroOrOneBooleanContent);
45   setStackPointerRegisterToSaveRestore(BF::SP);
46   setIntDivIsCheap(false);
47
48   // Set up the legal register classes.
49   addRegisterClass(MVT::i32, BF::DRegisterClass);
50   addRegisterClass(MVT::i16, BF::D16RegisterClass);
51
52   computeRegisterProperties();
53
54   // Blackfin doesn't have i1 loads or stores
55   setLoadExtAction(ISD::EXTLOAD,  MVT::i1, Promote);
56   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
57   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
58
59   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
60   setOperationAction(ISD::JumpTable,     MVT::i32, Custom);
61
62   setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
63   setOperationAction(ISD::BR_JT,     MVT::Other, Expand);
64   setOperationAction(ISD::BR_CC,     MVT::Other, Expand);
65
66   // i16 registers don't do much
67   setOperationAction(ISD::AND,   MVT::i16, Promote);
68   setOperationAction(ISD::OR,    MVT::i16, Promote);
69   setOperationAction(ISD::XOR,   MVT::i16, Promote);
70   setOperationAction(ISD::CTPOP, MVT::i16, Promote);
71   // The expansion of CTLZ/CTTZ uses AND/OR, so we might as well promote
72   // immediately.
73   setOperationAction(ISD::CTLZ,  MVT::i16, Promote);
74   setOperationAction(ISD::CTTZ,  MVT::i16, Promote);
75   setOperationAction(ISD::SETCC, MVT::i16, Promote);
76
77   // Blackfin has no division
78   setOperationAction(ISD::SDIV,    MVT::i16, Expand);
79   setOperationAction(ISD::SDIV,    MVT::i32, Expand);
80   setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
81   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
82   setOperationAction(ISD::SREM,    MVT::i16, Expand);
83   setOperationAction(ISD::SREM,    MVT::i32, Expand);
84   setOperationAction(ISD::UDIV,    MVT::i16, Expand);
85   setOperationAction(ISD::UDIV,    MVT::i32, Expand);
86   setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
87   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
88   setOperationAction(ISD::UREM,    MVT::i16, Expand);
89   setOperationAction(ISD::UREM,    MVT::i32, Expand);
90
91   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
92   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
93   setOperationAction(ISD::MULHU,     MVT::i32, Expand);
94   setOperationAction(ISD::MULHS,     MVT::i32, Expand);
95
96   // No carry-in operations.
97   setOperationAction(ISD::ADDE, MVT::i32, Custom);
98   setOperationAction(ISD::SUBE, MVT::i32, Custom);
99
100   // Blackfin has no intrinsics for these particular operations.
101   setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
102   setOperationAction(ISD::BSWAP, MVT::i32, Expand);
103
104   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
105   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
106   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
107
108   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
109
110   // i32 has native CTPOP, but not CTLZ/CTTZ
111   setOperationAction(ISD::CTLZ, MVT::i32, Expand);
112   setOperationAction(ISD::CTTZ, MVT::i32, Expand);
113
114   // READCYCLECOUNTER needs special type legalization.
115   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
116
117   setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
118
119   // Use the default implementation.
120   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
121   setOperationAction(ISD::VAEND, MVT::Other, Expand);
122   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
123   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
124 }
125
126 const char *BlackfinTargetLowering::getTargetNodeName(unsigned Opcode) const {
127   switch (Opcode) {
128   default: return 0;
129   case BFISD::CALL:     return "BFISD::CALL";
130   case BFISD::RET_FLAG: return "BFISD::RET_FLAG";
131   case BFISD::Wrapper:  return "BFISD::Wrapper";
132   }
133 }
134
135 MVT::SimpleValueType BlackfinTargetLowering::getSetCCResultType(EVT VT) const {
136   // SETCC always sets the CC register. Technically that is an i1 register, but
137   // that type is not legal, so we treat it as an i32 register.
138   return MVT::i32;
139 }
140
141 SDValue BlackfinTargetLowering::LowerGlobalAddress(SDValue Op,
142                                                    SelectionDAG &DAG) const {
143   DebugLoc DL = Op.getDebugLoc();
144   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
145
146   Op = DAG.getTargetGlobalAddress(GV, MVT::i32);
147   return DAG.getNode(BFISD::Wrapper, DL, MVT::i32, Op);
148 }
149
150 SDValue BlackfinTargetLowering::LowerJumpTable(SDValue Op,
151                                                SelectionDAG &DAG) const {
152   DebugLoc DL = Op.getDebugLoc();
153   int JTI = cast<JumpTableSDNode>(Op)->getIndex();
154
155   Op = DAG.getTargetJumpTable(JTI, MVT::i32);
156   return DAG.getNode(BFISD::Wrapper, DL, MVT::i32, Op);
157 }
158
159 SDValue
160 BlackfinTargetLowering::LowerFormalArguments(SDValue Chain,
161                                              CallingConv::ID CallConv, bool isVarArg,
162                                             const SmallVectorImpl<ISD::InputArg>
163                                                &Ins,
164                                              DebugLoc dl, SelectionDAG &DAG,
165                                              SmallVectorImpl<SDValue> &InVals)
166                                                const {
167
168   MachineFunction &MF = DAG.getMachineFunction();
169   MachineFrameInfo *MFI = MF.getFrameInfo();
170
171   SmallVector<CCValAssign, 16> ArgLocs;
172   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
173                  ArgLocs, *DAG.getContext());
174   CCInfo.AllocateStack(12, 4);  // ABI requires 12 bytes stack space
175   CCInfo.AnalyzeFormalArguments(Ins, CC_Blackfin);
176
177   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
178     CCValAssign &VA = ArgLocs[i];
179
180     if (VA.isRegLoc()) {
181       EVT RegVT = VA.getLocVT();
182       TargetRegisterClass *RC = VA.getLocReg() == BF::P0 ?
183         BF::PRegisterClass : BF::DRegisterClass;
184       assert(RC->contains(VA.getLocReg()) && "Unexpected regclass in CCState");
185       assert(RC->hasType(RegVT) && "Unexpected regclass in CCState");
186
187       unsigned Reg = MF.getRegInfo().createVirtualRegister(RC);
188       MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg);
189       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
190
191       // If this is an 8 or 16-bit value, it is really passed promoted to 32
192       // bits.  Insert an assert[sz]ext to capture this, then truncate to the
193       // right size.
194       if (VA.getLocInfo() == CCValAssign::SExt)
195         ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
196                                DAG.getValueType(VA.getValVT()));
197       else if (VA.getLocInfo() == CCValAssign::ZExt)
198         ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
199                                DAG.getValueType(VA.getValVT()));
200
201       if (VA.getLocInfo() != CCValAssign::Full)
202         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
203
204       InVals.push_back(ArgValue);
205     } else {
206       assert(VA.isMemLoc() && "CCValAssign must be RegLoc or MemLoc");
207       unsigned ObjSize = VA.getLocVT().getStoreSize();
208       int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(),
209                                       true, false);
210       SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
211       InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0,
212                                    false, false, 0));
213     }
214   }
215
216   return Chain;
217 }
218
219 SDValue
220 BlackfinTargetLowering::LowerReturn(SDValue Chain,
221                                     CallingConv::ID CallConv, bool isVarArg,
222                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
223                                     DebugLoc dl, SelectionDAG &DAG) const {
224
225   // CCValAssign - represent the assignment of the return value to locations.
226   SmallVector<CCValAssign, 16> RVLocs;
227
228   // CCState - Info about the registers and stack slot.
229   CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
230                  RVLocs, *DAG.getContext());
231
232   // Analize return values.
233   CCInfo.AnalyzeReturn(Outs, RetCC_Blackfin);
234
235   // If this is the first return lowered for this function, add the regs to the
236   // liveout set for the function.
237   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
238     for (unsigned i = 0; i != RVLocs.size(); ++i)
239       DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
240   }
241
242   SDValue Flag;
243
244   // Copy the result values into the output registers.
245   for (unsigned i = 0; i != RVLocs.size(); ++i) {
246     CCValAssign &VA = RVLocs[i];
247     assert(VA.isRegLoc() && "Can only return in registers!");
248     SDValue Opi = Outs[i].Val;
249
250     // Expand to i32 if necessary
251     switch (VA.getLocInfo()) {
252     default: llvm_unreachable("Unknown loc info!");
253     case CCValAssign::Full: break;
254     case CCValAssign::SExt:
255       Opi = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Opi);
256       break;
257     case CCValAssign::ZExt:
258       Opi = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Opi);
259       break;
260     case CCValAssign::AExt:
261       Opi = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Opi);
262       break;
263     }
264     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Opi, SDValue());
265     // Guarantee that all emitted copies are stuck together with flags.
266     Flag = Chain.getValue(1);
267   }
268
269   if (Flag.getNode()) {
270     return DAG.getNode(BFISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
271   } else {
272     return DAG.getNode(BFISD::RET_FLAG, dl, MVT::Other, Chain);
273   }
274 }
275
276 SDValue
277 BlackfinTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
278                                   CallingConv::ID CallConv, bool isVarArg,
279                                   bool &isTailCall,
280                                   const SmallVectorImpl<ISD::OutputArg> &Outs,
281                                   const SmallVectorImpl<ISD::InputArg> &Ins,
282                                   DebugLoc dl, SelectionDAG &DAG,
283                                   SmallVectorImpl<SDValue> &InVals) const {
284   // Blackfin target does not yet support tail call optimization.
285   isTailCall = false;
286
287   // Analyze operands of the call, assigning locations to each operand.
288   SmallVector<CCValAssign, 16> ArgLocs;
289   CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs,
290                  *DAG.getContext());
291   CCInfo.AllocateStack(12, 4);  // ABI requires 12 bytes stack space
292   CCInfo.AnalyzeCallOperands(Outs, CC_Blackfin);
293
294   // Get the size of the outgoing arguments stack space requirement.
295   unsigned ArgsSize = CCInfo.getNextStackOffset();
296
297   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
298   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
299   SmallVector<SDValue, 8> MemOpChains;
300
301   // Walk the register/memloc assignments, inserting copies/loads.
302   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
303     CCValAssign &VA = ArgLocs[i];
304     SDValue Arg = Outs[i].Val;
305
306     // Promote the value if needed.
307     switch (VA.getLocInfo()) {
308     default: llvm_unreachable("Unknown loc info!");
309     case CCValAssign::Full: break;
310     case CCValAssign::SExt:
311       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
312       break;
313     case CCValAssign::ZExt:
314       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
315       break;
316     case CCValAssign::AExt:
317       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
318       break;
319     }
320
321     // Arguments that can be passed on register must be kept at
322     // RegsToPass vector
323     if (VA.isRegLoc()) {
324       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
325     } else {
326       assert(VA.isMemLoc() && "CCValAssign must be RegLoc or MemLoc");
327       int Offset = VA.getLocMemOffset();
328       assert(Offset%4 == 0 && "Unaligned LocMemOffset");
329       assert(VA.getLocVT()==MVT::i32 && "Illegal CCValAssign type");
330       SDValue SPN = DAG.getCopyFromReg(Chain, dl, BF::SP, MVT::i32);
331       SDValue OffsetN = DAG.getIntPtrConstant(Offset);
332       OffsetN = DAG.getNode(ISD::ADD, dl, MVT::i32, SPN, OffsetN);
333       MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, OffsetN,
334                                          PseudoSourceValue::getStack(),
335                                          Offset, false, false, 0));
336     }
337   }
338
339   // Transform all store nodes into one single node because
340   // all store nodes are independent of each other.
341   if (!MemOpChains.empty())
342     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
343                         &MemOpChains[0], MemOpChains.size());
344
345   // Build a sequence of copy-to-reg nodes chained together with token
346   // chain and flag operands which copy the outgoing args into registers.
347   // The InFlag in necessary since all emited instructions must be
348   // stuck together.
349   SDValue InFlag;
350   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
351     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
352                              RegsToPass[i].second, InFlag);
353     InFlag = Chain.getValue(1);
354   }
355
356   // If the callee is a GlobalAddress node (quite common, every direct call is)
357   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
358   // Likewise ExternalSymbol -> TargetExternalSymbol.
359   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
360     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
361   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
362     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
363
364   std::vector<EVT> NodeTys;
365   NodeTys.push_back(MVT::Other);   // Returns a chain
366   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
367   SDValue Ops[] = { Chain, Callee, InFlag };
368   Chain = DAG.getNode(BFISD::CALL, dl, NodeTys, Ops,
369                       InFlag.getNode() ? 3 : 2);
370   InFlag = Chain.getValue(1);
371
372   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
373                              DAG.getIntPtrConstant(0, true), InFlag);
374   InFlag = Chain.getValue(1);
375
376   // Assign locations to each value returned by this call.
377   SmallVector<CCValAssign, 16> RVLocs;
378   CCState RVInfo(CallConv, isVarArg, DAG.getTarget(), RVLocs,
379                  *DAG.getContext());
380
381   RVInfo.AnalyzeCallResult(Ins, RetCC_Blackfin);
382
383   // Copy all of the result registers out of their specified physreg.
384   for (unsigned i = 0; i != RVLocs.size(); ++i) {
385     CCValAssign &RV = RVLocs[i];
386     unsigned Reg = RV.getLocReg();
387
388     Chain = DAG.getCopyFromReg(Chain, dl, Reg,
389                                RVLocs[i].getLocVT(), InFlag);
390     SDValue Val = Chain.getValue(0);
391     InFlag = Chain.getValue(2);
392     Chain = Chain.getValue(1);
393
394     // Callee is responsible for extending any i16 return values.
395     switch (RV.getLocInfo()) {
396     case CCValAssign::SExt:
397       Val = DAG.getNode(ISD::AssertSext, dl, RV.getLocVT(), Val,
398                         DAG.getValueType(RV.getValVT()));
399       break;
400     case CCValAssign::ZExt:
401       Val = DAG.getNode(ISD::AssertZext, dl, RV.getLocVT(), Val,
402                         DAG.getValueType(RV.getValVT()));
403       break;
404     default:
405       break;
406     }
407
408     // Truncate to valtype
409     if (RV.getLocInfo() != CCValAssign::Full)
410       Val = DAG.getNode(ISD::TRUNCATE, dl, RV.getValVT(), Val);
411     InVals.push_back(Val);
412   }
413
414   return Chain;
415 }
416
417 // Expansion of ADDE / SUBE. This is a bit involved since blackfin doesn't have
418 // add-with-carry instructions.
419 SDValue BlackfinTargetLowering::LowerADDE(SDValue Op, SelectionDAG &DAG) const {
420   // Operands: lhs, rhs, carry-in (AC0 flag)
421   // Results: sum, carry-out (AC0 flag)
422   DebugLoc dl = Op.getDebugLoc();
423
424   unsigned Opcode = Op.getOpcode()==ISD::ADDE ? BF::ADD : BF::SUB;
425
426   // zext incoming carry flag in AC0 to 32 bits
427   SDNode* CarryIn = DAG.getMachineNode(BF::MOVE_cc_ac0, dl, MVT::i32,
428                                        /* flag= */ Op.getOperand(2));
429   CarryIn = DAG.getMachineNode(BF::MOVECC_zext, dl, MVT::i32,
430                                SDValue(CarryIn, 0));
431
432   // Add operands, produce sum and carry flag
433   SDNode *Sum = DAG.getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
434                                    Op.getOperand(0), Op.getOperand(1));
435
436   // Store intermediate carry from Sum
437   SDNode* Carry1 = DAG.getMachineNode(BF::MOVE_cc_ac0, dl, MVT::i32,
438                                       /* flag= */ SDValue(Sum, 1));
439
440   // Add incoming carry, again producing an output flag
441   Sum = DAG.getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
442                            SDValue(Sum, 0), SDValue(CarryIn, 0));
443
444   // Update AC0 with the intermediate carry, producing a flag.
445   SDNode *CarryOut = DAG.getMachineNode(BF::OR_ac0_cc, dl, MVT::Flag,
446                                         SDValue(Carry1, 0));
447
448   // Compose (i32, flag) pair
449   SDValue ops[2] = { SDValue(Sum, 0), SDValue(CarryOut, 0) };
450   return DAG.getMergeValues(ops, 2, dl);
451 }
452
453 SDValue BlackfinTargetLowering::LowerOperation(SDValue Op,
454                                                SelectionDAG &DAG) const {
455   switch (Op.getOpcode()) {
456   default:
457     Op.getNode()->dump();
458     llvm_unreachable("Should not custom lower this!");
459   case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
460   case ISD::GlobalTLSAddress:
461     llvm_unreachable("TLS not implemented for Blackfin.");
462   case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
463     // Frame & Return address.  Currently unimplemented
464   case ISD::FRAMEADDR:          return SDValue();
465   case ISD::RETURNADDR:         return SDValue();
466   case ISD::ADDE:
467   case ISD::SUBE:               return LowerADDE(Op, DAG);
468   }
469 }
470
471 void
472 BlackfinTargetLowering::ReplaceNodeResults(SDNode *N,
473                                            SmallVectorImpl<SDValue> &Results,
474                                            SelectionDAG &DAG) const {
475   DebugLoc dl = N->getDebugLoc();
476   switch (N->getOpcode()) {
477   default:
478     llvm_unreachable("Do not know how to custom type legalize this operation!");
479     return;
480   case ISD::READCYCLECOUNTER: {
481     // The low part of the cycle counter is in CYCLES, the high part in
482     // CYCLES2. Reading CYCLES will latch the value of CYCLES2, so we must read
483     // CYCLES2 last.
484     SDValue TheChain = N->getOperand(0);
485     SDValue lo = DAG.getCopyFromReg(TheChain, dl, BF::CYCLES, MVT::i32);
486     SDValue hi = DAG.getCopyFromReg(lo.getValue(1), dl, BF::CYCLES2, MVT::i32);
487     // Use a buildpair to merge the two 32-bit values into a 64-bit one.
488     Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, lo, hi));
489     // Outgoing chain. If we were to use the chain from lo instead, it would be
490     // possible to entirely eliminate the CYCLES2 read in (i32 (trunc
491     // readcyclecounter)). Unfortunately this could possibly delay the CYCLES2
492     // read beyond the next CYCLES read, leading to invalid results.
493     Results.push_back(hi.getValue(1));
494     return;
495   }
496   }
497 }
498
499 /// getFunctionAlignment - Return the Log2 alignment of this function.
500 unsigned BlackfinTargetLowering::getFunctionAlignment(const Function *F) const {
501   return 2;
502 }
503
504 //===----------------------------------------------------------------------===//
505 //                         Blackfin Inline Assembly Support
506 //===----------------------------------------------------------------------===//
507
508 /// getConstraintType - Given a constraint letter, return the type of
509 /// constraint it is for this target.
510 BlackfinTargetLowering::ConstraintType
511 BlackfinTargetLowering::getConstraintType(const std::string &Constraint) const {
512   if (Constraint.size() != 1)
513     return TargetLowering::getConstraintType(Constraint);
514
515   switch (Constraint[0]) {
516     // Standard constraints
517   case 'r':
518     return C_RegisterClass;
519
520     // Blackfin-specific constraints
521   case 'a':
522   case 'd':
523   case 'z':
524   case 'D':
525   case 'W':
526   case 'e':
527   case 'b':
528   case 'v':
529   case 'f':
530   case 'c':
531   case 't':
532   case 'u':
533   case 'k':
534   case 'x':
535   case 'y':
536   case 'w':
537     return C_RegisterClass;
538   case 'A':
539   case 'B':
540   case 'C':
541   case 'Z':
542   case 'Y':
543     return C_Register;
544   }
545
546   // Not implemented: q0-q7, qA. Use {R2} etc instead
547
548   return TargetLowering::getConstraintType(Constraint);
549 }
550
551 /// getRegForInlineAsmConstraint - Return register no and class for a C_Register
552 /// constraint.
553 std::pair<unsigned, const TargetRegisterClass*> BlackfinTargetLowering::
554 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
555   typedef std::pair<unsigned, const TargetRegisterClass*> Pair;
556   using namespace BF;
557
558   if (Constraint.size() != 1)
559     return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
560
561   switch (Constraint[0]) {
562     // Standard constraints
563   case 'r':
564     return Pair(0U, VT == MVT::i16 ? D16RegisterClass : DPRegisterClass);
565
566     // Blackfin-specific constraints
567   case 'a': return Pair(0U, PRegisterClass);
568   case 'd': return Pair(0U, DRegisterClass);
569   case 'e': return Pair(0U, AccuRegisterClass);
570   case 'A': return Pair(A0, AccuRegisterClass);
571   case 'B': return Pair(A1, AccuRegisterClass);
572   case 'b': return Pair(0U, IRegisterClass);
573   case 'v': return Pair(0U, BRegisterClass);
574   case 'f': return Pair(0U, MRegisterClass);
575   case 'C': return Pair(CC, JustCCRegisterClass);
576   case 'x': return Pair(0U, GRRegisterClass);
577   case 'w': return Pair(0U, ALLRegisterClass);
578   case 'Z': return Pair(P3, PRegisterClass);
579   case 'Y': return Pair(P1, PRegisterClass);
580   }
581
582   // Not implemented: q0-q7, qA. Use {R2} etc instead.
583   // Constraints z, D, W, c, t, u, k, and y use non-existing classes, defer to
584   // getRegClassForInlineAsmConstraint()
585
586   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
587 }
588
589 std::vector<unsigned> BlackfinTargetLowering::
590 getRegClassForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
591   using namespace BF;
592
593   if (Constraint.size() != 1)
594     return std::vector<unsigned>();
595
596   switch (Constraint[0]) {
597   case 'z': return make_vector<unsigned>(P0, P1, P2, 0);
598   case 'D': return make_vector<unsigned>(R0, R2, R4, R6, 0);
599   case 'W': return make_vector<unsigned>(R1, R3, R5, R7, 0);
600   case 'c': return make_vector<unsigned>(I0, I1, I2, I3,
601                                          B0, B1, B2, B3,
602                                          L0, L1, L2, L3, 0);
603   case 't': return make_vector<unsigned>(LT0, LT1, 0);
604   case 'u': return make_vector<unsigned>(LB0, LB1, 0);
605   case 'k': return make_vector<unsigned>(LC0, LC1, 0);
606   case 'y': return make_vector<unsigned>(RETS, RETN, RETI, RETX, RETE,
607                                          ASTAT, SEQSTAT, USP, 0);
608   }
609
610   return std::vector<unsigned>();
611 }
612
613 bool BlackfinTargetLowering::
614 isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
615   // The Blackfin target isn't yet aware of offsets.
616   return false;
617 }