Reapply 91184 with fixes and an addition to the testcase to cover the problem
[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/Target/TargetLoweringObjectFile.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) {
143   DebugLoc DL = Op.getDebugLoc();
144   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, SelectionDAG &DAG) {
151   DebugLoc DL = Op.getDebugLoc();
152   int JTI = cast<JumpTableSDNode>(Op)->getIndex();
153
154   Op = DAG.getTargetJumpTable(JTI, MVT::i32);
155   return DAG.getNode(BFISD::Wrapper, DL, MVT::i32, Op);
156 }
157
158 SDValue
159 BlackfinTargetLowering::LowerFormalArguments(SDValue Chain,
160                                              CallingConv::ID CallConv, bool isVarArg,
161                                             const SmallVectorImpl<ISD::InputArg>
162                                                &Ins,
163                                              DebugLoc dl, SelectionDAG &DAG,
164                                              SmallVectorImpl<SDValue> &InVals) {
165
166   MachineFunction &MF = DAG.getMachineFunction();
167   MachineFrameInfo *MFI = MF.getFrameInfo();
168
169   SmallVector<CCValAssign, 16> ArgLocs;
170   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
171                  ArgLocs, *DAG.getContext());
172   CCInfo.AllocateStack(12, 4);  // ABI requires 12 bytes stack space
173   CCInfo.AnalyzeFormalArguments(Ins, CC_Blackfin);
174
175   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
176     CCValAssign &VA = ArgLocs[i];
177
178     if (VA.isRegLoc()) {
179       EVT RegVT = VA.getLocVT();
180       TargetRegisterClass *RC = VA.getLocReg() == BF::P0 ?
181         BF::PRegisterClass : BF::DRegisterClass;
182       assert(RC->contains(VA.getLocReg()) && "Unexpected regclass in CCState");
183       assert(RC->hasType(RegVT) && "Unexpected regclass in CCState");
184
185       unsigned Reg = MF.getRegInfo().createVirtualRegister(RC);
186       MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg);
187       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
188
189       // If this is an 8 or 16-bit value, it is really passed promoted to 32
190       // bits.  Insert an assert[sz]ext to capture this, then truncate to the
191       // right size.
192       if (VA.getLocInfo() == CCValAssign::SExt)
193         ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
194                                DAG.getValueType(VA.getValVT()));
195       else if (VA.getLocInfo() == CCValAssign::ZExt)
196         ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
197                                DAG.getValueType(VA.getValVT()));
198
199       if (VA.getLocInfo() != CCValAssign::Full)
200         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
201
202       InVals.push_back(ArgValue);
203     } else {
204       assert(VA.isMemLoc() && "CCValAssign must be RegLoc or MemLoc");
205       unsigned ObjSize = VA.getLocVT().getStoreSize();
206       int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(),
207                                       true, false);
208       SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
209       InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
210     }
211   }
212
213   return Chain;
214 }
215
216 SDValue
217 BlackfinTargetLowering::LowerReturn(SDValue Chain,
218                                     CallingConv::ID CallConv, bool isVarArg,
219                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
220                                     DebugLoc dl, SelectionDAG &DAG) {
221
222   // CCValAssign - represent the assignment of the return value to locations.
223   SmallVector<CCValAssign, 16> RVLocs;
224
225   // CCState - Info about the registers and stack slot.
226   CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
227                  RVLocs, *DAG.getContext());
228
229   // Analize return values.
230   CCInfo.AnalyzeReturn(Outs, RetCC_Blackfin);
231
232   // If this is the first return lowered for this function, add the regs to the
233   // liveout set for the function.
234   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
235     for (unsigned i = 0; i != RVLocs.size(); ++i)
236       DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
237   }
238
239   SDValue Flag;
240
241   // Copy the result values into the output registers.
242   for (unsigned i = 0; i != RVLocs.size(); ++i) {
243     CCValAssign &VA = RVLocs[i];
244     assert(VA.isRegLoc() && "Can only return in registers!");
245     SDValue Opi = Outs[i].Val;
246
247     // Expand to i32 if necessary
248     switch (VA.getLocInfo()) {
249     default: llvm_unreachable("Unknown loc info!");
250     case CCValAssign::Full: break;
251     case CCValAssign::SExt:
252       Opi = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Opi);
253       break;
254     case CCValAssign::ZExt:
255       Opi = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Opi);
256       break;
257     case CCValAssign::AExt:
258       Opi = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Opi);
259       break;
260     }
261     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Opi, SDValue());
262     // Guarantee that all emitted copies are stuck together with flags.
263     Flag = Chain.getValue(1);
264   }
265
266   if (Flag.getNode()) {
267     return DAG.getNode(BFISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
268   } else {
269     return DAG.getNode(BFISD::RET_FLAG, dl, MVT::Other, Chain);
270   }
271 }
272
273 SDValue
274 BlackfinTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
275                                   CallingConv::ID CallConv, bool isVarArg,
276                                   bool isTailCall,
277                                   const SmallVectorImpl<ISD::OutputArg> &Outs,
278                                   const SmallVectorImpl<ISD::InputArg> &Ins,
279                                   DebugLoc dl, SelectionDAG &DAG,
280                                   SmallVectorImpl<SDValue> &InVals) {
281
282   // Analyze operands of the call, assigning locations to each operand.
283   SmallVector<CCValAssign, 16> ArgLocs;
284   CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs,
285                  *DAG.getContext());
286   CCInfo.AllocateStack(12, 4);  // ABI requires 12 bytes stack space
287   CCInfo.AnalyzeCallOperands(Outs, CC_Blackfin);
288
289   // Get the size of the outgoing arguments stack space requirement.
290   unsigned ArgsSize = CCInfo.getNextStackOffset();
291
292   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
293   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
294   SmallVector<SDValue, 8> MemOpChains;
295
296   // Walk the register/memloc assignments, inserting copies/loads.
297   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
298     CCValAssign &VA = ArgLocs[i];
299     SDValue Arg = Outs[i].Val;
300
301     // Promote the value if needed.
302     switch (VA.getLocInfo()) {
303     default: llvm_unreachable("Unknown loc info!");
304     case CCValAssign::Full: break;
305     case CCValAssign::SExt:
306       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
307       break;
308     case CCValAssign::ZExt:
309       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
310       break;
311     case CCValAssign::AExt:
312       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
313       break;
314     }
315
316     // Arguments that can be passed on register must be kept at
317     // RegsToPass vector
318     if (VA.isRegLoc()) {
319       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
320     } else {
321       assert(VA.isMemLoc() && "CCValAssign must be RegLoc or MemLoc");
322       int Offset = VA.getLocMemOffset();
323       assert(Offset%4 == 0 && "Unaligned LocMemOffset");
324       assert(VA.getLocVT()==MVT::i32 && "Illegal CCValAssign type");
325       SDValue SPN = DAG.getCopyFromReg(Chain, dl, BF::SP, MVT::i32);
326       SDValue OffsetN = DAG.getIntPtrConstant(Offset);
327       OffsetN = DAG.getNode(ISD::ADD, dl, MVT::i32, SPN, OffsetN);
328       MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, OffsetN,
329                                          PseudoSourceValue::getStack(),
330                                          Offset));
331     }
332   }
333
334   // Transform all store nodes into one single node because
335   // all store nodes are independent of each other.
336   if (!MemOpChains.empty())
337     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
338                         &MemOpChains[0], MemOpChains.size());
339
340   // Build a sequence of copy-to-reg nodes chained together with token
341   // chain and flag operands which copy the outgoing args into registers.
342   // The InFlag in necessary since all emited instructions must be
343   // stuck together.
344   SDValue InFlag;
345   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
346     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
347                              RegsToPass[i].second, InFlag);
348     InFlag = Chain.getValue(1);
349   }
350
351   // If the callee is a GlobalAddress node (quite common, every direct call is)
352   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
353   // Likewise ExternalSymbol -> TargetExternalSymbol.
354   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
355     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
356   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
357     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
358
359   std::vector<EVT> NodeTys;
360   NodeTys.push_back(MVT::Other);   // Returns a chain
361   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
362   SDValue Ops[] = { Chain, Callee, InFlag };
363   Chain = DAG.getNode(BFISD::CALL, dl, NodeTys, Ops,
364                       InFlag.getNode() ? 3 : 2);
365   InFlag = Chain.getValue(1);
366
367   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
368                              DAG.getIntPtrConstant(0, true), InFlag);
369   InFlag = Chain.getValue(1);
370
371   // Assign locations to each value returned by this call.
372   SmallVector<CCValAssign, 16> RVLocs;
373   CCState RVInfo(CallConv, isVarArg, DAG.getTarget(), RVLocs,
374                  *DAG.getContext());
375
376   RVInfo.AnalyzeCallResult(Ins, RetCC_Blackfin);
377
378   // Copy all of the result registers out of their specified physreg.
379   for (unsigned i = 0; i != RVLocs.size(); ++i) {
380     CCValAssign &RV = RVLocs[i];
381     unsigned Reg = RV.getLocReg();
382
383     Chain = DAG.getCopyFromReg(Chain, dl, Reg,
384                                RVLocs[i].getLocVT(), InFlag);
385     SDValue Val = Chain.getValue(0);
386     InFlag = Chain.getValue(2);
387     Chain = Chain.getValue(1);
388
389     // Callee is responsible for extending any i16 return values.
390     switch (RV.getLocInfo()) {
391     case CCValAssign::SExt:
392       Val = DAG.getNode(ISD::AssertSext, dl, RV.getLocVT(), Val,
393                         DAG.getValueType(RV.getValVT()));
394       break;
395     case CCValAssign::ZExt:
396       Val = DAG.getNode(ISD::AssertZext, dl, RV.getLocVT(), Val,
397                         DAG.getValueType(RV.getValVT()));
398       break;
399     default:
400       break;
401     }
402
403     // Truncate to valtype
404     if (RV.getLocInfo() != CCValAssign::Full)
405       Val = DAG.getNode(ISD::TRUNCATE, dl, RV.getValVT(), Val);
406     InVals.push_back(Val);
407   }
408
409   return Chain;
410 }
411
412 // Expansion of ADDE / SUBE. This is a bit involved since blackfin doesn't have
413 // add-with-carry instructions.
414 SDValue BlackfinTargetLowering::LowerADDE(SDValue Op, SelectionDAG &DAG) {
415   // Operands: lhs, rhs, carry-in (AC0 flag)
416   // Results: sum, carry-out (AC0 flag)
417   DebugLoc dl = Op.getDebugLoc();
418
419   unsigned Opcode = Op.getOpcode()==ISD::ADDE ? BF::ADD : BF::SUB;
420
421   // zext incoming carry flag in AC0 to 32 bits
422   SDNode* CarryIn = DAG.getMachineNode(BF::MOVE_cc_ac0, dl, MVT::i32,
423                                        /* flag= */ Op.getOperand(2));
424   CarryIn = DAG.getMachineNode(BF::MOVECC_zext, dl, MVT::i32,
425                                SDValue(CarryIn, 0));
426
427   // Add operands, produce sum and carry flag
428   SDNode *Sum = DAG.getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
429                                    Op.getOperand(0), Op.getOperand(1));
430
431   // Store intermediate carry from Sum
432   SDNode* Carry1 = DAG.getMachineNode(BF::MOVE_cc_ac0, dl, MVT::i32,
433                                       /* flag= */ SDValue(Sum, 1));
434
435   // Add incoming carry, again producing an output flag
436   Sum = DAG.getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
437                            SDValue(Sum, 0), SDValue(CarryIn, 0));
438
439   // Update AC0 with the intermediate carry, producing a flag.
440   SDNode *CarryOut = DAG.getMachineNode(BF::OR_ac0_cc, dl, MVT::Flag,
441                                         SDValue(Carry1, 0));
442
443   // Compose (i32, flag) pair
444   SDValue ops[2] = { SDValue(Sum, 0), SDValue(CarryOut, 0) };
445   return DAG.getMergeValues(ops, 2, dl);
446 }
447
448 SDValue BlackfinTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
449   switch (Op.getOpcode()) {
450   default:
451     Op.getNode()->dump();
452     llvm_unreachable("Should not custom lower this!");
453   case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
454   case ISD::GlobalTLSAddress:
455     llvm_unreachable("TLS not implemented for Blackfin.");
456   case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
457     // Frame & Return address.  Currently unimplemented
458   case ISD::FRAMEADDR:          return SDValue();
459   case ISD::RETURNADDR:         return SDValue();
460   case ISD::ADDE:
461   case ISD::SUBE:               return LowerADDE(Op, DAG);
462   }
463 }
464
465 void
466 BlackfinTargetLowering::ReplaceNodeResults(SDNode *N,
467                                            SmallVectorImpl<SDValue> &Results,
468                                            SelectionDAG &DAG) {
469   DebugLoc dl = N->getDebugLoc();
470   switch (N->getOpcode()) {
471   default:
472     llvm_unreachable("Do not know how to custom type legalize this operation!");
473     return;
474   case ISD::READCYCLECOUNTER: {
475     // The low part of the cycle counter is in CYCLES, the high part in
476     // CYCLES2. Reading CYCLES will latch the value of CYCLES2, so we must read
477     // CYCLES2 last.
478     SDValue TheChain = N->getOperand(0);
479     SDValue lo = DAG.getCopyFromReg(TheChain, dl, BF::CYCLES, MVT::i32);
480     SDValue hi = DAG.getCopyFromReg(lo.getValue(1), dl, BF::CYCLES2, MVT::i32);
481     // Use a buildpair to merge the two 32-bit values into a 64-bit one.
482     Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, lo, hi));
483     // Outgoing chain. If we were to use the chain from lo instead, it would be
484     // possible to entirely eliminate the CYCLES2 read in (i32 (trunc
485     // readcyclecounter)). Unfortunately this could possibly delay the CYCLES2
486     // read beyond the next CYCLES read, leading to invalid results.
487     Results.push_back(hi.getValue(1));
488     return;
489   }
490   }
491 }
492
493 /// getFunctionAlignment - Return the Log2 alignment of this function.
494 unsigned BlackfinTargetLowering::getFunctionAlignment(const Function *F) const {
495   return 2;
496 }
497
498 //===----------------------------------------------------------------------===//
499 //                         Blackfin Inline Assembly Support
500 //===----------------------------------------------------------------------===//
501
502 /// getConstraintType - Given a constraint letter, return the type of
503 /// constraint it is for this target.
504 BlackfinTargetLowering::ConstraintType
505 BlackfinTargetLowering::getConstraintType(const std::string &Constraint) const {
506   if (Constraint.size() != 1)
507     return TargetLowering::getConstraintType(Constraint);
508
509   switch (Constraint[0]) {
510     // Standard constraints
511   case 'r':
512     return C_RegisterClass;
513
514     // Blackfin-specific constraints
515   case 'a':
516   case 'd':
517   case 'z':
518   case 'D':
519   case 'W':
520   case 'e':
521   case 'b':
522   case 'v':
523   case 'f':
524   case 'c':
525   case 't':
526   case 'u':
527   case 'k':
528   case 'x':
529   case 'y':
530   case 'w':
531     return C_RegisterClass;
532   case 'A':
533   case 'B':
534   case 'C':
535   case 'Z':
536   case 'Y':
537     return C_Register;
538   }
539
540   // Not implemented: q0-q7, qA. Use {R2} etc instead
541
542   return TargetLowering::getConstraintType(Constraint);
543 }
544
545 /// getRegForInlineAsmConstraint - Return register no and class for a C_Register
546 /// constraint.
547 std::pair<unsigned, const TargetRegisterClass*> BlackfinTargetLowering::
548 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
549   typedef std::pair<unsigned, const TargetRegisterClass*> Pair;
550   using namespace BF;
551
552   if (Constraint.size() != 1)
553     return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
554
555   switch (Constraint[0]) {
556     // Standard constraints
557   case 'r':
558     return Pair(0U, VT == MVT::i16 ? D16RegisterClass : DPRegisterClass);
559
560     // Blackfin-specific constraints
561   case 'a': return Pair(0U, PRegisterClass);
562   case 'd': return Pair(0U, DRegisterClass);
563   case 'e': return Pair(0U, AccuRegisterClass);
564   case 'A': return Pair(A0, AccuRegisterClass);
565   case 'B': return Pair(A1, AccuRegisterClass);
566   case 'b': return Pair(0U, IRegisterClass);
567   case 'v': return Pair(0U, BRegisterClass);
568   case 'f': return Pair(0U, MRegisterClass);
569   case 'C': return Pair(CC, JustCCRegisterClass);
570   case 'x': return Pair(0U, GRRegisterClass);
571   case 'w': return Pair(0U, ALLRegisterClass);
572   case 'Z': return Pair(P3, PRegisterClass);
573   case 'Y': return Pair(P1, PRegisterClass);
574   }
575
576   // Not implemented: q0-q7, qA. Use {R2} etc instead.
577   // Constraints z, D, W, c, t, u, k, and y use non-existing classes, defer to
578   // getRegClassForInlineAsmConstraint()
579
580   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
581 }
582
583 std::vector<unsigned> BlackfinTargetLowering::
584 getRegClassForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
585   using namespace BF;
586
587   if (Constraint.size() != 1)
588     return std::vector<unsigned>();
589
590   switch (Constraint[0]) {
591   case 'z': return make_vector<unsigned>(P0, P1, P2, 0);
592   case 'D': return make_vector<unsigned>(R0, R2, R4, R6, 0);
593   case 'W': return make_vector<unsigned>(R1, R3, R5, R7, 0);
594   case 'c': return make_vector<unsigned>(I0, I1, I2, I3,
595                                          B0, B1, B2, B3,
596                                          L0, L1, L2, L3, 0);
597   case 't': return make_vector<unsigned>(LT0, LT1, 0);
598   case 'u': return make_vector<unsigned>(LB0, LB1, 0);
599   case 'k': return make_vector<unsigned>(LC0, LC1, 0);
600   case 'y': return make_vector<unsigned>(RETS, RETN, RETI, RETX, RETE,
601                                          ASTAT, SEQSTAT, USP, 0);
602   }
603
604   return std::vector<unsigned>();
605 }
606
607 bool BlackfinTargetLowering::
608 isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
609   // The Blackfin target isn't yet aware of offsets.
610   return false;
611 }