Split the init.trampoline intrinsic, which currently combines GCC's
[oota-llvm.git] / lib / Target / XCore / XCoreISelLowering.cpp
1 //===-- XCoreISelLowering.cpp - XCore 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 XCoreTargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "xcore-lower"
15
16 #include "XCoreISelLowering.h"
17 #include "XCoreMachineFunctionInfo.h"
18 #include "XCore.h"
19 #include "XCoreTargetObjectFile.h"
20 #include "XCoreTargetMachine.h"
21 #include "XCoreSubtarget.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/CallingConv.h"
26 #include "llvm/GlobalVariable.h"
27 #include "llvm/GlobalAlias.h"
28 #include "llvm/CodeGen/CallingConvLower.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineJumpTableInfo.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/CodeGen/SelectionDAGISel.h"
35 #include "llvm/CodeGen/ValueTypes.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/ADT/VectorExtras.h"
40 using namespace llvm;
41
42 const char *XCoreTargetLowering::
43 getTargetNodeName(unsigned Opcode) const
44 {
45   switch (Opcode)
46   {
47     case XCoreISD::BL                : return "XCoreISD::BL";
48     case XCoreISD::PCRelativeWrapper : return "XCoreISD::PCRelativeWrapper";
49     case XCoreISD::DPRelativeWrapper : return "XCoreISD::DPRelativeWrapper";
50     case XCoreISD::CPRelativeWrapper : return "XCoreISD::CPRelativeWrapper";
51     case XCoreISD::STWSP             : return "XCoreISD::STWSP";
52     case XCoreISD::RETSP             : return "XCoreISD::RETSP";
53     case XCoreISD::LADD              : return "XCoreISD::LADD";
54     case XCoreISD::LSUB              : return "XCoreISD::LSUB";
55     case XCoreISD::LMUL              : return "XCoreISD::LMUL";
56     case XCoreISD::MACCU             : return "XCoreISD::MACCU";
57     case XCoreISD::MACCS             : return "XCoreISD::MACCS";
58     case XCoreISD::BR_JT             : return "XCoreISD::BR_JT";
59     case XCoreISD::BR_JT32           : return "XCoreISD::BR_JT32";
60     default                          : return NULL;
61   }
62 }
63
64 XCoreTargetLowering::XCoreTargetLowering(XCoreTargetMachine &XTM)
65   : TargetLowering(XTM, new XCoreTargetObjectFile()),
66     TM(XTM),
67     Subtarget(*XTM.getSubtargetImpl()) {
68
69   // Set up the register classes.
70   addRegisterClass(MVT::i32, XCore::GRRegsRegisterClass);
71
72   // Compute derived properties from the register classes
73   computeRegisterProperties();
74
75   // Division is expensive
76   setIntDivIsCheap(false);
77
78   setStackPointerRegisterToSaveRestore(XCore::SP);
79
80   setSchedulingPreference(Sched::RegPressure);
81
82   // Use i32 for setcc operations results (slt, sgt, ...).
83   setBooleanContents(ZeroOrOneBooleanContent);
84
85   // XCore does not have the NodeTypes below.
86   setOperationAction(ISD::BR_CC,     MVT::Other, Expand);
87   setOperationAction(ISD::SELECT_CC, MVT::i32,   Custom);
88   setOperationAction(ISD::ADDC, MVT::i32, Expand);
89   setOperationAction(ISD::ADDE, MVT::i32, Expand);
90   setOperationAction(ISD::SUBC, MVT::i32, Expand);
91   setOperationAction(ISD::SUBE, MVT::i32, Expand);
92
93   // Stop the combiner recombining select and set_cc
94   setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
95
96   // 64bit
97   setOperationAction(ISD::ADD, MVT::i64, Custom);
98   setOperationAction(ISD::SUB, MVT::i64, Custom);
99   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
100   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
101   setOperationAction(ISD::MULHS, MVT::i32, Expand);
102   setOperationAction(ISD::MULHU, MVT::i32, Expand);
103   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
104   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
105   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
106
107   // Bit Manipulation
108   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
109   setOperationAction(ISD::ROTL , MVT::i32, Expand);
110   setOperationAction(ISD::ROTR , MVT::i32, Expand);
111
112   setOperationAction(ISD::TRAP, MVT::Other, Legal);
113
114   // Jump tables.
115   setOperationAction(ISD::BR_JT, MVT::Other, Custom);
116
117   setOperationAction(ISD::GlobalAddress, MVT::i32,   Custom);
118   setOperationAction(ISD::BlockAddress, MVT::i32 , Custom);
119
120   // Thread Local Storage
121   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
122
123   // Conversion of i64 -> double produces constantpool nodes
124   setOperationAction(ISD::ConstantPool, MVT::i32,   Custom);
125
126   // Loads
127   setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
128   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
129   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
130
131   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
132   setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
133
134   // Custom expand misaligned loads / stores.
135   setOperationAction(ISD::LOAD, MVT::i32, Custom);
136   setOperationAction(ISD::STORE, MVT::i32, Custom);
137
138   // Varargs
139   setOperationAction(ISD::VAEND, MVT::Other, Expand);
140   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
141   setOperationAction(ISD::VAARG, MVT::Other, Custom);
142   setOperationAction(ISD::VASTART, MVT::Other, Custom);
143
144   // Dynamic stack
145   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
146   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
147   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
148
149   // TRAMPOLINE is custom lowered.
150   setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
151   setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
152
153   maxStoresPerMemset = maxStoresPerMemsetOptSize = 4;
154   maxStoresPerMemmove = maxStoresPerMemmoveOptSize
155     = maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 2;
156
157   // We have target-specific dag combine patterns for the following nodes:
158   setTargetDAGCombine(ISD::STORE);
159   setTargetDAGCombine(ISD::ADD);
160
161   setMinFunctionAlignment(1);
162 }
163
164 SDValue XCoreTargetLowering::
165 LowerOperation(SDValue Op, SelectionDAG &DAG) const {
166   switch (Op.getOpcode())
167   {
168   case ISD::GlobalAddress:    return LowerGlobalAddress(Op, DAG);
169   case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
170   case ISD::BlockAddress:     return LowerBlockAddress(Op, DAG);
171   case ISD::ConstantPool:     return LowerConstantPool(Op, DAG);
172   case ISD::BR_JT:            return LowerBR_JT(Op, DAG);
173   case ISD::LOAD:             return LowerLOAD(Op, DAG);
174   case ISD::STORE:            return LowerSTORE(Op, DAG);
175   case ISD::SELECT_CC:        return LowerSELECT_CC(Op, DAG);
176   case ISD::VAARG:            return LowerVAARG(Op, DAG);
177   case ISD::VASTART:          return LowerVASTART(Op, DAG);
178   case ISD::SMUL_LOHI:        return LowerSMUL_LOHI(Op, DAG);
179   case ISD::UMUL_LOHI:        return LowerUMUL_LOHI(Op, DAG);
180   // FIXME: Remove these when LegalizeDAGTypes lands.
181   case ISD::ADD:
182   case ISD::SUB:              return ExpandADDSUB(Op.getNode(), DAG);
183   case ISD::FRAMEADDR:        return LowerFRAMEADDR(Op, DAG);
184   case ISD::INIT_TRAMPOLINE:  return LowerINIT_TRAMPOLINE(Op, DAG);
185   case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
186   default:
187     llvm_unreachable("unimplemented operand");
188     return SDValue();
189   }
190 }
191
192 /// ReplaceNodeResults - Replace the results of node with an illegal result
193 /// type with new values built out of custom code.
194 void XCoreTargetLowering::ReplaceNodeResults(SDNode *N,
195                                              SmallVectorImpl<SDValue>&Results,
196                                              SelectionDAG &DAG) const {
197   switch (N->getOpcode()) {
198   default:
199     llvm_unreachable("Don't know how to custom expand this!");
200     return;
201   case ISD::ADD:
202   case ISD::SUB:
203     Results.push_back(ExpandADDSUB(N, DAG));
204     return;
205   }
206 }
207
208 //===----------------------------------------------------------------------===//
209 //  Misc Lower Operation implementation
210 //===----------------------------------------------------------------------===//
211
212 SDValue XCoreTargetLowering::
213 LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
214 {
215   DebugLoc dl = Op.getDebugLoc();
216   SDValue Cond = DAG.getNode(ISD::SETCC, dl, MVT::i32, Op.getOperand(2),
217                              Op.getOperand(3), Op.getOperand(4));
218   return DAG.getNode(ISD::SELECT, dl, MVT::i32, Cond, Op.getOperand(0),
219                      Op.getOperand(1));
220 }
221
222 SDValue XCoreTargetLowering::
223 getGlobalAddressWrapper(SDValue GA, const GlobalValue *GV,
224                         SelectionDAG &DAG) const
225 {
226   // FIXME there is no actual debug info here
227   DebugLoc dl = GA.getDebugLoc();
228   if (isa<Function>(GV)) {
229     return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA);
230   }
231   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
232   if (!GVar) {
233     // If GV is an alias then use the aliasee to determine constness
234     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
235       GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
236   }
237   bool isConst = GVar && GVar->isConstant();
238   if (isConst) {
239     return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA);
240   }
241   return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA);
242 }
243
244 SDValue XCoreTargetLowering::
245 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const
246 {
247   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
248   SDValue GA = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(), MVT::i32);
249   return getGlobalAddressWrapper(GA, GV, DAG);
250 }
251
252 static inline SDValue BuildGetId(SelectionDAG &DAG, DebugLoc dl) {
253   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
254                      DAG.getConstant(Intrinsic::xcore_getid, MVT::i32));
255 }
256
257 static inline bool isZeroLengthArray(Type *Ty) {
258   ArrayType *AT = dyn_cast_or_null<ArrayType>(Ty);
259   return AT && (AT->getNumElements() == 0);
260 }
261
262 SDValue XCoreTargetLowering::
263 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
264 {
265   // FIXME there isn't really debug info here
266   DebugLoc dl = Op.getDebugLoc();
267   // transform to label + getid() * size
268   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
269   SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
270   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
271   if (!GVar) {
272     // If GV is an alias then use the aliasee to determine size
273     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
274       GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
275   }
276   if (! GVar) {
277     llvm_unreachable("Thread local object not a GlobalVariable?");
278     return SDValue();
279   }
280   Type *Ty = cast<PointerType>(GV->getType())->getElementType();
281   if (!Ty->isSized() || isZeroLengthArray(Ty)) {
282 #ifndef NDEBUG
283     errs() << "Size of thread local object " << GVar->getName()
284            << " is unknown\n";
285 #endif
286     llvm_unreachable(0);
287   }
288   SDValue base = getGlobalAddressWrapper(GA, GV, DAG);
289   const TargetData *TD = TM.getTargetData();
290   unsigned Size = TD->getTypeAllocSize(Ty);
291   SDValue offset = DAG.getNode(ISD::MUL, dl, MVT::i32, BuildGetId(DAG, dl),
292                        DAG.getConstant(Size, MVT::i32));
293   return DAG.getNode(ISD::ADD, dl, MVT::i32, base, offset);
294 }
295
296 SDValue XCoreTargetLowering::
297 LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const
298 {
299   DebugLoc DL = Op.getDebugLoc();
300
301   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
302   SDValue Result = DAG.getBlockAddress(BA, getPointerTy(), /*isTarget=*/true);
303
304   return DAG.getNode(XCoreISD::PCRelativeWrapper, DL, getPointerTy(), Result);
305 }
306
307 SDValue XCoreTargetLowering::
308 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
309 {
310   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
311   // FIXME there isn't really debug info here
312   DebugLoc dl = CP->getDebugLoc();
313   EVT PtrVT = Op.getValueType();
314   SDValue Res;
315   if (CP->isMachineConstantPoolEntry()) {
316     Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
317                                     CP->getAlignment());
318   } else {
319     Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
320                                     CP->getAlignment());
321   }
322   return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, Res);
323 }
324
325 unsigned XCoreTargetLowering::getJumpTableEncoding() const {
326   return MachineJumpTableInfo::EK_Inline;
327 }
328
329 SDValue XCoreTargetLowering::
330 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
331 {
332   SDValue Chain = Op.getOperand(0);
333   SDValue Table = Op.getOperand(1);
334   SDValue Index = Op.getOperand(2);
335   DebugLoc dl = Op.getDebugLoc();
336   JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
337   unsigned JTI = JT->getIndex();
338   MachineFunction &MF = DAG.getMachineFunction();
339   const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
340   SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
341
342   unsigned NumEntries = MJTI->getJumpTables()[JTI].MBBs.size();
343   if (NumEntries <= 32) {
344     return DAG.getNode(XCoreISD::BR_JT, dl, MVT::Other, Chain, TargetJT, Index);
345   }
346   assert((NumEntries >> 31) == 0);
347   SDValue ScaledIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
348                                     DAG.getConstant(1, MVT::i32));
349   return DAG.getNode(XCoreISD::BR_JT32, dl, MVT::Other, Chain, TargetJT,
350                      ScaledIndex);
351 }
352
353 static bool
354 IsWordAlignedBasePlusConstantOffset(SDValue Addr, SDValue &AlignedBase,
355                                     int64_t &Offset)
356 {
357   if (Addr.getOpcode() != ISD::ADD) {
358     return false;
359   }
360   ConstantSDNode *CN = 0;
361   if (!(CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
362     return false;
363   }
364   int64_t off = CN->getSExtValue();
365   const SDValue &Base = Addr.getOperand(0);
366   const SDValue *Root = &Base;
367   if (Base.getOpcode() == ISD::ADD &&
368       Base.getOperand(1).getOpcode() == ISD::SHL) {
369     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Base.getOperand(1)
370                                                       .getOperand(1));
371     if (CN && (CN->getSExtValue() >= 2)) {
372       Root = &Base.getOperand(0);
373     }
374   }
375   if (isa<FrameIndexSDNode>(*Root)) {
376     // All frame indicies are word aligned
377     AlignedBase = Base;
378     Offset = off;
379     return true;
380   }
381   if (Root->getOpcode() == XCoreISD::DPRelativeWrapper ||
382       Root->getOpcode() == XCoreISD::CPRelativeWrapper) {
383     // All dp / cp relative addresses are word aligned
384     AlignedBase = Base;
385     Offset = off;
386     return true;
387   }
388   return false;
389 }
390
391 SDValue XCoreTargetLowering::
392 LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
393   LoadSDNode *LD = cast<LoadSDNode>(Op);
394   assert(LD->getExtensionType() == ISD::NON_EXTLOAD &&
395          "Unexpected extension type");
396   assert(LD->getMemoryVT() == MVT::i32 && "Unexpected load EVT");
397   if (allowsUnalignedMemoryAccesses(LD->getMemoryVT()))
398     return SDValue();
399
400   unsigned ABIAlignment = getTargetData()->
401     getABITypeAlignment(LD->getMemoryVT().getTypeForEVT(*DAG.getContext()));
402   // Leave aligned load alone.
403   if (LD->getAlignment() >= ABIAlignment)
404     return SDValue();
405
406   SDValue Chain = LD->getChain();
407   SDValue BasePtr = LD->getBasePtr();
408   DebugLoc DL = Op.getDebugLoc();
409
410   SDValue Base;
411   int64_t Offset;
412   if (!LD->isVolatile() &&
413       IsWordAlignedBasePlusConstantOffset(BasePtr, Base, Offset)) {
414     if (Offset % 4 == 0) {
415       // We've managed to infer better alignment information than the load
416       // already has. Use an aligned load.
417       //
418       return DAG.getLoad(getPointerTy(), DL, Chain, BasePtr,
419                          MachinePointerInfo(),
420                          false, false, 0);
421     }
422     // Lower to
423     // ldw low, base[offset >> 2]
424     // ldw high, base[(offset >> 2) + 1]
425     // shr low_shifted, low, (offset & 0x3) * 8
426     // shl high_shifted, high, 32 - (offset & 0x3) * 8
427     // or result, low_shifted, high_shifted
428     SDValue LowOffset = DAG.getConstant(Offset & ~0x3, MVT::i32);
429     SDValue HighOffset = DAG.getConstant((Offset & ~0x3) + 4, MVT::i32);
430     SDValue LowShift = DAG.getConstant((Offset & 0x3) * 8, MVT::i32);
431     SDValue HighShift = DAG.getConstant(32 - (Offset & 0x3) * 8, MVT::i32);
432
433     SDValue LowAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base, LowOffset);
434     SDValue HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base, HighOffset);
435
436     SDValue Low = DAG.getLoad(getPointerTy(), DL, Chain,
437                               LowAddr, MachinePointerInfo(), false, false, 0);
438     SDValue High = DAG.getLoad(getPointerTy(), DL, Chain,
439                                HighAddr, MachinePointerInfo(), false, false, 0);
440     SDValue LowShifted = DAG.getNode(ISD::SRL, DL, MVT::i32, Low, LowShift);
441     SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High, HighShift);
442     SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, LowShifted, HighShifted);
443     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
444                              High.getValue(1));
445     SDValue Ops[] = { Result, Chain };
446     return DAG.getMergeValues(Ops, 2, DL);
447   }
448
449   if (LD->getAlignment() == 2) {
450     SDValue Low = DAG.getExtLoad(ISD::ZEXTLOAD, DL, MVT::i32, Chain,
451                                  BasePtr, LD->getPointerInfo(), MVT::i16,
452                                  LD->isVolatile(), LD->isNonTemporal(), 2);
453     SDValue HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
454                                    DAG.getConstant(2, MVT::i32));
455     SDValue High = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
456                                   HighAddr,
457                                   LD->getPointerInfo().getWithOffset(2),
458                                   MVT::i16, LD->isVolatile(),
459                                   LD->isNonTemporal(), 2);
460     SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High,
461                                       DAG.getConstant(16, MVT::i32));
462     SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Low, HighShifted);
463     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
464                              High.getValue(1));
465     SDValue Ops[] = { Result, Chain };
466     return DAG.getMergeValues(Ops, 2, DL);
467   }
468
469   // Lower to a call to __misaligned_load(BasePtr).
470   Type *IntPtrTy = getTargetData()->getIntPtrType(*DAG.getContext());
471   TargetLowering::ArgListTy Args;
472   TargetLowering::ArgListEntry Entry;
473
474   Entry.Ty = IntPtrTy;
475   Entry.Node = BasePtr;
476   Args.push_back(Entry);
477
478   std::pair<SDValue, SDValue> CallResult =
479         LowerCallTo(Chain, IntPtrTy, false, false,
480                     false, false, 0, CallingConv::C, false,
481                     /*isReturnValueUsed=*/true,
482                     DAG.getExternalSymbol("__misaligned_load", getPointerTy()),
483                     Args, DAG, DL);
484
485   SDValue Ops[] =
486     { CallResult.first, CallResult.second };
487
488   return DAG.getMergeValues(Ops, 2, DL);
489 }
490
491 SDValue XCoreTargetLowering::
492 LowerSTORE(SDValue Op, SelectionDAG &DAG) const
493 {
494   StoreSDNode *ST = cast<StoreSDNode>(Op);
495   assert(!ST->isTruncatingStore() && "Unexpected store type");
496   assert(ST->getMemoryVT() == MVT::i32 && "Unexpected store EVT");
497   if (allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
498     return SDValue();
499   }
500   unsigned ABIAlignment = getTargetData()->
501     getABITypeAlignment(ST->getMemoryVT().getTypeForEVT(*DAG.getContext()));
502   // Leave aligned store alone.
503   if (ST->getAlignment() >= ABIAlignment) {
504     return SDValue();
505   }
506   SDValue Chain = ST->getChain();
507   SDValue BasePtr = ST->getBasePtr();
508   SDValue Value = ST->getValue();
509   DebugLoc dl = Op.getDebugLoc();
510
511   if (ST->getAlignment() == 2) {
512     SDValue Low = Value;
513     SDValue High = DAG.getNode(ISD::SRL, dl, MVT::i32, Value,
514                                       DAG.getConstant(16, MVT::i32));
515     SDValue StoreLow = DAG.getTruncStore(Chain, dl, Low, BasePtr,
516                                          ST->getPointerInfo(), MVT::i16,
517                                          ST->isVolatile(), ST->isNonTemporal(),
518                                          2);
519     SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr,
520                                    DAG.getConstant(2, MVT::i32));
521     SDValue StoreHigh = DAG.getTruncStore(Chain, dl, High, HighAddr,
522                                           ST->getPointerInfo().getWithOffset(2),
523                                           MVT::i16, ST->isVolatile(),
524                                           ST->isNonTemporal(), 2);
525     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, StoreLow, StoreHigh);
526   }
527
528   // Lower to a call to __misaligned_store(BasePtr, Value).
529   Type *IntPtrTy = getTargetData()->getIntPtrType(*DAG.getContext());
530   TargetLowering::ArgListTy Args;
531   TargetLowering::ArgListEntry Entry;
532
533   Entry.Ty = IntPtrTy;
534   Entry.Node = BasePtr;
535   Args.push_back(Entry);
536
537   Entry.Node = Value;
538   Args.push_back(Entry);
539
540   std::pair<SDValue, SDValue> CallResult =
541         LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()), false, false,
542                     false, false, 0, CallingConv::C, false,
543                     /*isReturnValueUsed=*/true,
544                     DAG.getExternalSymbol("__misaligned_store", getPointerTy()),
545                     Args, DAG, dl);
546
547   return CallResult.second;
548 }
549
550 SDValue XCoreTargetLowering::
551 LowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
552 {
553   assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::SMUL_LOHI &&
554          "Unexpected operand to lower!");
555   DebugLoc dl = Op.getDebugLoc();
556   SDValue LHS = Op.getOperand(0);
557   SDValue RHS = Op.getOperand(1);
558   SDValue Zero = DAG.getConstant(0, MVT::i32);
559   SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
560                            DAG.getVTList(MVT::i32, MVT::i32), Zero, Zero,
561                            LHS, RHS);
562   SDValue Lo(Hi.getNode(), 1);
563   SDValue Ops[] = { Lo, Hi };
564   return DAG.getMergeValues(Ops, 2, dl);
565 }
566
567 SDValue XCoreTargetLowering::
568 LowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
569 {
570   assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::UMUL_LOHI &&
571          "Unexpected operand to lower!");
572   DebugLoc dl = Op.getDebugLoc();
573   SDValue LHS = Op.getOperand(0);
574   SDValue RHS = Op.getOperand(1);
575   SDValue Zero = DAG.getConstant(0, MVT::i32);
576   SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
577                            DAG.getVTList(MVT::i32, MVT::i32), LHS, RHS,
578                            Zero, Zero);
579   SDValue Lo(Hi.getNode(), 1);
580   SDValue Ops[] = { Lo, Hi };
581   return DAG.getMergeValues(Ops, 2, dl);
582 }
583
584 /// isADDADDMUL - Return whether Op is in a form that is equivalent to
585 /// add(add(mul(x,y),a),b). If requireIntermediatesHaveOneUse is true then
586 /// each intermediate result in the calculation must also have a single use.
587 /// If the Op is in the correct form the constituent parts are written to Mul0,
588 /// Mul1, Addend0 and Addend1.
589 static bool
590 isADDADDMUL(SDValue Op, SDValue &Mul0, SDValue &Mul1, SDValue &Addend0,
591             SDValue &Addend1, bool requireIntermediatesHaveOneUse)
592 {
593   if (Op.getOpcode() != ISD::ADD)
594     return false;
595   SDValue N0 = Op.getOperand(0);
596   SDValue N1 = Op.getOperand(1);
597   SDValue AddOp;
598   SDValue OtherOp;
599   if (N0.getOpcode() == ISD::ADD) {
600     AddOp = N0;
601     OtherOp = N1;
602   } else if (N1.getOpcode() == ISD::ADD) {
603     AddOp = N1;
604     OtherOp = N0;
605   } else {
606     return false;
607   }
608   if (requireIntermediatesHaveOneUse && !AddOp.hasOneUse())
609     return false;
610   if (OtherOp.getOpcode() == ISD::MUL) {
611     // add(add(a,b),mul(x,y))
612     if (requireIntermediatesHaveOneUse && !OtherOp.hasOneUse())
613       return false;
614     Mul0 = OtherOp.getOperand(0);
615     Mul1 = OtherOp.getOperand(1);
616     Addend0 = AddOp.getOperand(0);
617     Addend1 = AddOp.getOperand(1);
618     return true;
619   }
620   if (AddOp.getOperand(0).getOpcode() == ISD::MUL) {
621     // add(add(mul(x,y),a),b)
622     if (requireIntermediatesHaveOneUse && !AddOp.getOperand(0).hasOneUse())
623       return false;
624     Mul0 = AddOp.getOperand(0).getOperand(0);
625     Mul1 = AddOp.getOperand(0).getOperand(1);
626     Addend0 = AddOp.getOperand(1);
627     Addend1 = OtherOp;
628     return true;
629   }
630   if (AddOp.getOperand(1).getOpcode() == ISD::MUL) {
631     // add(add(a,mul(x,y)),b)
632     if (requireIntermediatesHaveOneUse && !AddOp.getOperand(1).hasOneUse())
633       return false;
634     Mul0 = AddOp.getOperand(1).getOperand(0);
635     Mul1 = AddOp.getOperand(1).getOperand(1);
636     Addend0 = AddOp.getOperand(0);
637     Addend1 = OtherOp;
638     return true;
639   }
640   return false;
641 }
642
643 SDValue XCoreTargetLowering::
644 TryExpandADDWithMul(SDNode *N, SelectionDAG &DAG) const
645 {
646   SDValue Mul;
647   SDValue Other;
648   if (N->getOperand(0).getOpcode() == ISD::MUL) {
649     Mul = N->getOperand(0);
650     Other = N->getOperand(1);
651   } else if (N->getOperand(1).getOpcode() == ISD::MUL) {
652     Mul = N->getOperand(1);
653     Other = N->getOperand(0);
654   } else {
655     return SDValue();
656   }
657   DebugLoc dl = N->getDebugLoc();
658   SDValue LL, RL, AddendL, AddendH;
659   LL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
660                    Mul.getOperand(0),  DAG.getConstant(0, MVT::i32));
661   RL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
662                    Mul.getOperand(1),  DAG.getConstant(0, MVT::i32));
663   AddendL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
664                         Other,  DAG.getConstant(0, MVT::i32));
665   AddendH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
666                         Other,  DAG.getConstant(1, MVT::i32));
667   APInt HighMask = APInt::getHighBitsSet(64, 32);
668   unsigned LHSSB = DAG.ComputeNumSignBits(Mul.getOperand(0));
669   unsigned RHSSB = DAG.ComputeNumSignBits(Mul.getOperand(1));
670   if (DAG.MaskedValueIsZero(Mul.getOperand(0), HighMask) &&
671       DAG.MaskedValueIsZero(Mul.getOperand(1), HighMask)) {
672     // The inputs are both zero-extended.
673     SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
674                              DAG.getVTList(MVT::i32, MVT::i32), AddendH,
675                              AddendL, LL, RL);
676     SDValue Lo(Hi.getNode(), 1);
677     return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
678   }
679   if (LHSSB > 32 && RHSSB > 32) {
680     // The inputs are both sign-extended.
681     SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
682                              DAG.getVTList(MVT::i32, MVT::i32), AddendH,
683                              AddendL, LL, RL);
684     SDValue Lo(Hi.getNode(), 1);
685     return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
686   }
687   SDValue LH, RH;
688   LH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
689                    Mul.getOperand(0),  DAG.getConstant(1, MVT::i32));
690   RH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
691                    Mul.getOperand(1),  DAG.getConstant(1, MVT::i32));
692   SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
693                            DAG.getVTList(MVT::i32, MVT::i32), AddendH,
694                            AddendL, LL, RL);
695   SDValue Lo(Hi.getNode(), 1);
696   RH = DAG.getNode(ISD::MUL, dl, MVT::i32, LL, RH);
697   LH = DAG.getNode(ISD::MUL, dl, MVT::i32, LH, RL);
698   Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, RH);
699   Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, LH);
700   return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
701 }
702
703 SDValue XCoreTargetLowering::
704 ExpandADDSUB(SDNode *N, SelectionDAG &DAG) const
705 {
706   assert(N->getValueType(0) == MVT::i64 &&
707          (N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&
708         "Unknown operand to lower!");
709
710   if (N->getOpcode() == ISD::ADD) {
711     SDValue Result = TryExpandADDWithMul(N, DAG);
712     if (Result.getNode() != 0)
713       return Result;
714   }
715
716   DebugLoc dl = N->getDebugLoc();
717
718   // Extract components
719   SDValue LHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
720                             N->getOperand(0),  DAG.getConstant(0, MVT::i32));
721   SDValue LHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
722                             N->getOperand(0),  DAG.getConstant(1, MVT::i32));
723   SDValue RHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
724                              N->getOperand(1), DAG.getConstant(0, MVT::i32));
725   SDValue RHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
726                              N->getOperand(1), DAG.getConstant(1, MVT::i32));
727
728   // Expand
729   unsigned Opcode = (N->getOpcode() == ISD::ADD) ? XCoreISD::LADD :
730                                                    XCoreISD::LSUB;
731   SDValue Zero = DAG.getConstant(0, MVT::i32);
732   SDValue Carry = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
733                                   LHSL, RHSL, Zero);
734   SDValue Lo(Carry.getNode(), 1);
735
736   SDValue Ignored = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
737                                   LHSH, RHSH, Carry);
738   SDValue Hi(Ignored.getNode(), 1);
739   // Merge the pieces
740   return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
741 }
742
743 SDValue XCoreTargetLowering::
744 LowerVAARG(SDValue Op, SelectionDAG &DAG) const
745 {
746   llvm_unreachable("unimplemented");
747   // FIX Arguments passed by reference need a extra dereference.
748   SDNode *Node = Op.getNode();
749   DebugLoc dl = Node->getDebugLoc();
750   const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
751   EVT VT = Node->getValueType(0);
752   SDValue VAList = DAG.getLoad(getPointerTy(), dl, Node->getOperand(0),
753                                Node->getOperand(1), MachinePointerInfo(V),
754                                false, false, 0);
755   // Increment the pointer, VAList, to the next vararg
756   SDValue Tmp3 = DAG.getNode(ISD::ADD, dl, getPointerTy(), VAList,
757                      DAG.getConstant(VT.getSizeInBits(),
758                                      getPointerTy()));
759   // Store the incremented VAList to the legalized pointer
760   Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Node->getOperand(1),
761                       MachinePointerInfo(V), false, false, 0);
762   // Load the actual argument out of the pointer VAList
763   return DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
764                      false, false, 0);
765 }
766
767 SDValue XCoreTargetLowering::
768 LowerVASTART(SDValue Op, SelectionDAG &DAG) const
769 {
770   DebugLoc dl = Op.getDebugLoc();
771   // vastart stores the address of the VarArgsFrameIndex slot into the
772   // memory location argument
773   MachineFunction &MF = DAG.getMachineFunction();
774   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
775   SDValue Addr = DAG.getFrameIndex(XFI->getVarArgsFrameIndex(), MVT::i32);
776   return DAG.getStore(Op.getOperand(0), dl, Addr, Op.getOperand(1),
777                       MachinePointerInfo(), false, false, 0);
778 }
779
780 SDValue XCoreTargetLowering::LowerFRAMEADDR(SDValue Op,
781                                             SelectionDAG &DAG) const {
782   DebugLoc dl = Op.getDebugLoc();
783   // Depths > 0 not supported yet!
784   if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
785     return SDValue();
786
787   MachineFunction &MF = DAG.getMachineFunction();
788   const TargetRegisterInfo *RegInfo = getTargetMachine().getRegisterInfo();
789   return DAG.getCopyFromReg(DAG.getEntryNode(), dl,
790                             RegInfo->getFrameRegister(MF), MVT::i32);
791 }
792
793 SDValue XCoreTargetLowering::
794 LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
795   return Op.getOperand(0);
796 }
797
798 SDValue XCoreTargetLowering::
799 LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
800   SDValue Chain = Op.getOperand(0);
801   SDValue Trmp = Op.getOperand(1); // trampoline
802   SDValue FPtr = Op.getOperand(2); // nested function
803   SDValue Nest = Op.getOperand(3); // 'nest' parameter value
804
805   const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
806
807   // .align 4
808   // LDAPF_u10 r11, nest
809   // LDW_2rus r11, r11[0]
810   // STWSP_ru6 r11, sp[0]
811   // LDAPF_u10 r11, fptr
812   // LDW_2rus r11, r11[0]
813   // BAU_1r r11
814   // nest:
815   // .word nest
816   // fptr:
817   // .word fptr
818   SDValue OutChains[5];
819
820   SDValue Addr = Trmp;
821
822   DebugLoc dl = Op.getDebugLoc();
823   OutChains[0] = DAG.getStore(Chain, dl, DAG.getConstant(0x0a3cd805, MVT::i32),
824                               Addr, MachinePointerInfo(TrmpAddr), false, false,
825                               0);
826
827   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
828                      DAG.getConstant(4, MVT::i32));
829   OutChains[1] = DAG.getStore(Chain, dl, DAG.getConstant(0xd80456c0, MVT::i32),
830                               Addr, MachinePointerInfo(TrmpAddr, 4), false,
831                               false, 0);
832
833   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
834                      DAG.getConstant(8, MVT::i32));
835   OutChains[2] = DAG.getStore(Chain, dl, DAG.getConstant(0x27fb0a3c, MVT::i32),
836                               Addr, MachinePointerInfo(TrmpAddr, 8), false,
837                               false, 0);
838
839   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
840                      DAG.getConstant(12, MVT::i32));
841   OutChains[3] = DAG.getStore(Chain, dl, Nest, Addr,
842                               MachinePointerInfo(TrmpAddr, 12), false, false,
843                               0);
844
845   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
846                      DAG.getConstant(16, MVT::i32));
847   OutChains[4] = DAG.getStore(Chain, dl, FPtr, Addr,
848                               MachinePointerInfo(TrmpAddr, 16), false, false,
849                               0);
850
851   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 5);
852 }
853
854 //===----------------------------------------------------------------------===//
855 //                      Calling Convention Implementation
856 //===----------------------------------------------------------------------===//
857
858 #include "XCoreGenCallingConv.inc"
859
860 //===----------------------------------------------------------------------===//
861 //                  Call Calling Convention Implementation
862 //===----------------------------------------------------------------------===//
863
864 /// XCore call implementation
865 SDValue
866 XCoreTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
867                                CallingConv::ID CallConv, bool isVarArg,
868                                bool &isTailCall,
869                                const SmallVectorImpl<ISD::OutputArg> &Outs,
870                                const SmallVectorImpl<SDValue> &OutVals,
871                                const SmallVectorImpl<ISD::InputArg> &Ins,
872                                DebugLoc dl, SelectionDAG &DAG,
873                                SmallVectorImpl<SDValue> &InVals) const {
874   // XCore target does not yet support tail call optimization.
875   isTailCall = false;
876
877   // For now, only CallingConv::C implemented
878   switch (CallConv)
879   {
880     default:
881       llvm_unreachable("Unsupported calling convention");
882     case CallingConv::Fast:
883     case CallingConv::C:
884       return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
885                             Outs, OutVals, Ins, dl, DAG, InVals);
886   }
887 }
888
889 /// LowerCCCCallTo - functions arguments are copied from virtual
890 /// regs to (physical regs)/(stack frame), CALLSEQ_START and
891 /// CALLSEQ_END are emitted.
892 /// TODO: isTailCall, sret.
893 SDValue
894 XCoreTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
895                                     CallingConv::ID CallConv, bool isVarArg,
896                                     bool isTailCall,
897                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
898                                     const SmallVectorImpl<SDValue> &OutVals,
899                                     const SmallVectorImpl<ISD::InputArg> &Ins,
900                                     DebugLoc dl, SelectionDAG &DAG,
901                                     SmallVectorImpl<SDValue> &InVals) const {
902
903   // Analyze operands of the call, assigning locations to each operand.
904   SmallVector<CCValAssign, 16> ArgLocs;
905   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
906                  getTargetMachine(), ArgLocs, *DAG.getContext());
907
908   // The ABI dictates there should be one stack slot available to the callee
909   // on function entry (for saving lr).
910   CCInfo.AllocateStack(4, 4);
911
912   CCInfo.AnalyzeCallOperands(Outs, CC_XCore);
913
914   // Get a count of how many bytes are to be pushed on the stack.
915   unsigned NumBytes = CCInfo.getNextStackOffset();
916
917   Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
918                                  getPointerTy(), true));
919
920   SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
921   SmallVector<SDValue, 12> MemOpChains;
922
923   // Walk the register/memloc assignments, inserting copies/loads.
924   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
925     CCValAssign &VA = ArgLocs[i];
926     SDValue Arg = OutVals[i];
927
928     // Promote the value if needed.
929     switch (VA.getLocInfo()) {
930       default: llvm_unreachable("Unknown loc info!");
931       case CCValAssign::Full: break;
932       case CCValAssign::SExt:
933         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
934         break;
935       case CCValAssign::ZExt:
936         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
937         break;
938       case CCValAssign::AExt:
939         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
940         break;
941     }
942
943     // Arguments that can be passed on register must be kept at
944     // RegsToPass vector
945     if (VA.isRegLoc()) {
946       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
947     } else {
948       assert(VA.isMemLoc());
949
950       int Offset = VA.getLocMemOffset();
951
952       MemOpChains.push_back(DAG.getNode(XCoreISD::STWSP, dl, MVT::Other,
953                                         Chain, Arg,
954                                         DAG.getConstant(Offset/4, MVT::i32)));
955     }
956   }
957
958   // Transform all store nodes into one single node because
959   // all store nodes are independent of each other.
960   if (!MemOpChains.empty())
961     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
962                         &MemOpChains[0], MemOpChains.size());
963
964   // Build a sequence of copy-to-reg nodes chained together with token
965   // chain and flag operands which copy the outgoing args into registers.
966   // The InFlag in necessary since all emitted instructions must be
967   // stuck together.
968   SDValue InFlag;
969   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
970     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
971                              RegsToPass[i].second, InFlag);
972     InFlag = Chain.getValue(1);
973   }
974
975   // If the callee is a GlobalAddress node (quite common, every direct call is)
976   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
977   // Likewise ExternalSymbol -> TargetExternalSymbol.
978   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
979     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
980   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
981     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
982
983   // XCoreBranchLink = #chain, #target_address, #opt_in_flags...
984   //             = Chain, Callee, Reg#1, Reg#2, ...
985   //
986   // Returns a chain & a flag for retval copy to use.
987   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
988   SmallVector<SDValue, 8> Ops;
989   Ops.push_back(Chain);
990   Ops.push_back(Callee);
991
992   // Add argument registers to the end of the list so that they are
993   // known live into the call.
994   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
995     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
996                                   RegsToPass[i].second.getValueType()));
997
998   if (InFlag.getNode())
999     Ops.push_back(InFlag);
1000
1001   Chain  = DAG.getNode(XCoreISD::BL, dl, NodeTys, &Ops[0], Ops.size());
1002   InFlag = Chain.getValue(1);
1003
1004   // Create the CALLSEQ_END node.
1005   Chain = DAG.getCALLSEQ_END(Chain,
1006                              DAG.getConstant(NumBytes, getPointerTy(), true),
1007                              DAG.getConstant(0, getPointerTy(), true),
1008                              InFlag);
1009   InFlag = Chain.getValue(1);
1010
1011   // Handle result values, copying them out of physregs into vregs that we
1012   // return.
1013   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1014                          Ins, dl, DAG, InVals);
1015 }
1016
1017 /// LowerCallResult - Lower the result values of a call into the
1018 /// appropriate copies out of appropriate physical registers.
1019 SDValue
1020 XCoreTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1021                                      CallingConv::ID CallConv, bool isVarArg,
1022                                      const SmallVectorImpl<ISD::InputArg> &Ins,
1023                                      DebugLoc dl, SelectionDAG &DAG,
1024                                      SmallVectorImpl<SDValue> &InVals) const {
1025
1026   // Assign locations to each value returned by this call.
1027   SmallVector<CCValAssign, 16> RVLocs;
1028   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1029                  getTargetMachine(), RVLocs, *DAG.getContext());
1030
1031   CCInfo.AnalyzeCallResult(Ins, RetCC_XCore);
1032
1033   // Copy all of the result registers out of their specified physreg.
1034   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1035     Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
1036                                  RVLocs[i].getValVT(), InFlag).getValue(1);
1037     InFlag = Chain.getValue(2);
1038     InVals.push_back(Chain.getValue(0));
1039   }
1040
1041   return Chain;
1042 }
1043
1044 //===----------------------------------------------------------------------===//
1045 //             Formal Arguments Calling Convention Implementation
1046 //===----------------------------------------------------------------------===//
1047
1048 /// XCore formal arguments implementation
1049 SDValue
1050 XCoreTargetLowering::LowerFormalArguments(SDValue Chain,
1051                                           CallingConv::ID CallConv,
1052                                           bool isVarArg,
1053                                       const SmallVectorImpl<ISD::InputArg> &Ins,
1054                                           DebugLoc dl,
1055                                           SelectionDAG &DAG,
1056                                           SmallVectorImpl<SDValue> &InVals)
1057                                             const {
1058   switch (CallConv)
1059   {
1060     default:
1061       llvm_unreachable("Unsupported calling convention");
1062     case CallingConv::C:
1063     case CallingConv::Fast:
1064       return LowerCCCArguments(Chain, CallConv, isVarArg,
1065                                Ins, dl, DAG, InVals);
1066   }
1067 }
1068
1069 /// LowerCCCArguments - transform physical registers into
1070 /// virtual registers and generate load operations for
1071 /// arguments places on the stack.
1072 /// TODO: sret
1073 SDValue
1074 XCoreTargetLowering::LowerCCCArguments(SDValue Chain,
1075                                        CallingConv::ID CallConv,
1076                                        bool isVarArg,
1077                                        const SmallVectorImpl<ISD::InputArg>
1078                                          &Ins,
1079                                        DebugLoc dl,
1080                                        SelectionDAG &DAG,
1081                                        SmallVectorImpl<SDValue> &InVals) const {
1082   MachineFunction &MF = DAG.getMachineFunction();
1083   MachineFrameInfo *MFI = MF.getFrameInfo();
1084   MachineRegisterInfo &RegInfo = MF.getRegInfo();
1085
1086   // Assign locations to all of the incoming arguments.
1087   SmallVector<CCValAssign, 16> ArgLocs;
1088   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1089                  getTargetMachine(), ArgLocs, *DAG.getContext());
1090
1091   CCInfo.AnalyzeFormalArguments(Ins, CC_XCore);
1092
1093   unsigned StackSlotSize = XCoreFrameLowering::stackSlotSize();
1094
1095   unsigned LRSaveSize = StackSlotSize;
1096
1097   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1098
1099     CCValAssign &VA = ArgLocs[i];
1100
1101     if (VA.isRegLoc()) {
1102       // Arguments passed in registers
1103       EVT RegVT = VA.getLocVT();
1104       switch (RegVT.getSimpleVT().SimpleTy) {
1105       default:
1106         {
1107 #ifndef NDEBUG
1108           errs() << "LowerFormalArguments Unhandled argument type: "
1109                  << RegVT.getSimpleVT().SimpleTy << "\n";
1110 #endif
1111           llvm_unreachable(0);
1112         }
1113       case MVT::i32:
1114         unsigned VReg = RegInfo.createVirtualRegister(
1115                           XCore::GRRegsRegisterClass);
1116         RegInfo.addLiveIn(VA.getLocReg(), VReg);
1117         InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1118       }
1119     } else {
1120       // sanity check
1121       assert(VA.isMemLoc());
1122       // Load the argument to a virtual register
1123       unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
1124       if (ObjSize > StackSlotSize) {
1125         errs() << "LowerFormalArguments Unhandled argument type: "
1126                << EVT(VA.getLocVT()).getEVTString()
1127                << "\n";
1128       }
1129       // Create the frame index object for this incoming parameter...
1130       int FI = MFI->CreateFixedObject(ObjSize,
1131                                       LRSaveSize + VA.getLocMemOffset(),
1132                                       true);
1133
1134       // Create the SelectionDAG nodes corresponding to a load
1135       //from this parameter
1136       SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1137       InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1138                                    MachinePointerInfo::getFixedStack(FI),
1139                                    false, false, 0));
1140     }
1141   }
1142
1143   if (isVarArg) {
1144     /* Argument registers */
1145     static const unsigned ArgRegs[] = {
1146       XCore::R0, XCore::R1, XCore::R2, XCore::R3
1147     };
1148     XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
1149     unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs,
1150                                                      array_lengthof(ArgRegs));
1151     if (FirstVAReg < array_lengthof(ArgRegs)) {
1152       SmallVector<SDValue, 4> MemOps;
1153       int offset = 0;
1154       // Save remaining registers, storing higher register numbers at a higher
1155       // address
1156       for (int i = array_lengthof(ArgRegs) - 1; i >= (int)FirstVAReg; --i) {
1157         // Create a stack slot
1158         int FI = MFI->CreateFixedObject(4, offset, true);
1159         if (i == (int)FirstVAReg) {
1160           XFI->setVarArgsFrameIndex(FI);
1161         }
1162         offset -= StackSlotSize;
1163         SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1164         // Move argument from phys reg -> virt reg
1165         unsigned VReg = RegInfo.createVirtualRegister(
1166                           XCore::GRRegsRegisterClass);
1167         RegInfo.addLiveIn(ArgRegs[i], VReg);
1168         SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
1169         // Move argument from virt reg -> stack
1170         SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1171                                      MachinePointerInfo(), false, false, 0);
1172         MemOps.push_back(Store);
1173       }
1174       if (!MemOps.empty())
1175         Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1176                             &MemOps[0], MemOps.size());
1177     } else {
1178       // This will point to the next argument passed via stack.
1179       XFI->setVarArgsFrameIndex(
1180         MFI->CreateFixedObject(4, LRSaveSize + CCInfo.getNextStackOffset(),
1181                                true));
1182     }
1183   }
1184
1185   return Chain;
1186 }
1187
1188 //===----------------------------------------------------------------------===//
1189 //               Return Value Calling Convention Implementation
1190 //===----------------------------------------------------------------------===//
1191
1192 bool XCoreTargetLowering::
1193 CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
1194                bool isVarArg,
1195                const SmallVectorImpl<ISD::OutputArg> &Outs,
1196                LLVMContext &Context) const {
1197   SmallVector<CCValAssign, 16> RVLocs;
1198   CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1199   return CCInfo.CheckReturn(Outs, RetCC_XCore);
1200 }
1201
1202 SDValue
1203 XCoreTargetLowering::LowerReturn(SDValue Chain,
1204                                  CallingConv::ID CallConv, bool isVarArg,
1205                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
1206                                  const SmallVectorImpl<SDValue> &OutVals,
1207                                  DebugLoc dl, SelectionDAG &DAG) const {
1208
1209   // CCValAssign - represent the assignment of
1210   // the return value to a location
1211   SmallVector<CCValAssign, 16> RVLocs;
1212
1213   // CCState - Info about the registers and stack slot.
1214   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1215                  getTargetMachine(), RVLocs, *DAG.getContext());
1216
1217   // Analyze return values.
1218   CCInfo.AnalyzeReturn(Outs, RetCC_XCore);
1219
1220   // If this is the first return lowered for this function, add
1221   // the regs to the liveout set for the function.
1222   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1223     for (unsigned i = 0; i != RVLocs.size(); ++i)
1224       if (RVLocs[i].isRegLoc())
1225         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1226   }
1227
1228   SDValue Flag;
1229
1230   // Copy the result values into the output registers.
1231   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1232     CCValAssign &VA = RVLocs[i];
1233     assert(VA.isRegLoc() && "Can only return in registers!");
1234
1235     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1236                              OutVals[i], Flag);
1237
1238     // guarantee that all emitted copies are
1239     // stuck together, avoiding something bad
1240     Flag = Chain.getValue(1);
1241   }
1242
1243   // Return on XCore is always a "retsp 0"
1244   if (Flag.getNode())
1245     return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other,
1246                        Chain, DAG.getConstant(0, MVT::i32), Flag);
1247   else // Return Void
1248     return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other,
1249                        Chain, DAG.getConstant(0, MVT::i32));
1250 }
1251
1252 //===----------------------------------------------------------------------===//
1253 //  Other Lowering Code
1254 //===----------------------------------------------------------------------===//
1255
1256 MachineBasicBlock *
1257 XCoreTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1258                                                  MachineBasicBlock *BB) const {
1259   const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1260   DebugLoc dl = MI->getDebugLoc();
1261   assert((MI->getOpcode() == XCore::SELECT_CC) &&
1262          "Unexpected instr type to insert");
1263
1264   // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1265   // control-flow pattern.  The incoming instruction knows the destination vreg
1266   // to set, the condition code register to branch on, the true/false values to
1267   // select between, and a branch opcode to use.
1268   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1269   MachineFunction::iterator It = BB;
1270   ++It;
1271
1272   //  thisMBB:
1273   //  ...
1274   //   TrueVal = ...
1275   //   cmpTY ccX, r1, r2
1276   //   bCC copy1MBB
1277   //   fallthrough --> copy0MBB
1278   MachineBasicBlock *thisMBB = BB;
1279   MachineFunction *F = BB->getParent();
1280   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1281   MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1282   F->insert(It, copy0MBB);
1283   F->insert(It, sinkMBB);
1284
1285   // Transfer the remainder of BB and its successor edges to sinkMBB.
1286   sinkMBB->splice(sinkMBB->begin(), BB,
1287                   llvm::next(MachineBasicBlock::iterator(MI)),
1288                   BB->end());
1289   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1290
1291   // Next, add the true and fallthrough blocks as its successors.
1292   BB->addSuccessor(copy0MBB);
1293   BB->addSuccessor(sinkMBB);
1294
1295   BuildMI(BB, dl, TII.get(XCore::BRFT_lru6))
1296     .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1297
1298   //  copy0MBB:
1299   //   %FalseValue = ...
1300   //   # fallthrough to sinkMBB
1301   BB = copy0MBB;
1302
1303   // Update machine-CFG edges
1304   BB->addSuccessor(sinkMBB);
1305
1306   //  sinkMBB:
1307   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1308   //  ...
1309   BB = sinkMBB;
1310   BuildMI(*BB, BB->begin(), dl,
1311           TII.get(XCore::PHI), MI->getOperand(0).getReg())
1312     .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1313     .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1314
1315   MI->eraseFromParent();   // The pseudo instruction is gone now.
1316   return BB;
1317 }
1318
1319 //===----------------------------------------------------------------------===//
1320 // Target Optimization Hooks
1321 //===----------------------------------------------------------------------===//
1322
1323 SDValue XCoreTargetLowering::PerformDAGCombine(SDNode *N,
1324                                              DAGCombinerInfo &DCI) const {
1325   SelectionDAG &DAG = DCI.DAG;
1326   DebugLoc dl = N->getDebugLoc();
1327   switch (N->getOpcode()) {
1328   default: break;
1329   case XCoreISD::LADD: {
1330     SDValue N0 = N->getOperand(0);
1331     SDValue N1 = N->getOperand(1);
1332     SDValue N2 = N->getOperand(2);
1333     ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1334     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1335     EVT VT = N0.getValueType();
1336
1337     // canonicalize constant to RHS
1338     if (N0C && !N1C)
1339       return DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N1, N0, N2);
1340
1341     // fold (ladd 0, 0, x) -> 0, x & 1
1342     if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
1343       SDValue Carry = DAG.getConstant(0, VT);
1344       SDValue Result = DAG.getNode(ISD::AND, dl, VT, N2,
1345                                    DAG.getConstant(1, VT));
1346       SDValue Ops [] = { Carry, Result };
1347       return DAG.getMergeValues(Ops, 2, dl);
1348     }
1349
1350     // fold (ladd x, 0, y) -> 0, add x, y iff carry is unused and y has only the
1351     // low bit set
1352     if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 0)) {
1353       APInt KnownZero, KnownOne;
1354       APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1355                                          VT.getSizeInBits() - 1);
1356       DAG.ComputeMaskedBits(N2, Mask, KnownZero, KnownOne);
1357       if (KnownZero == Mask) {
1358         SDValue Carry = DAG.getConstant(0, VT);
1359         SDValue Result = DAG.getNode(ISD::ADD, dl, VT, N0, N2);
1360         SDValue Ops [] = { Carry, Result };
1361         return DAG.getMergeValues(Ops, 2, dl);
1362       }
1363     }
1364   }
1365   break;
1366   case XCoreISD::LSUB: {
1367     SDValue N0 = N->getOperand(0);
1368     SDValue N1 = N->getOperand(1);
1369     SDValue N2 = N->getOperand(2);
1370     ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1371     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1372     EVT VT = N0.getValueType();
1373
1374     // fold (lsub 0, 0, x) -> x, -x iff x has only the low bit set
1375     if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
1376       APInt KnownZero, KnownOne;
1377       APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1378                                          VT.getSizeInBits() - 1);
1379       DAG.ComputeMaskedBits(N2, Mask, KnownZero, KnownOne);
1380       if (KnownZero == Mask) {
1381         SDValue Borrow = N2;
1382         SDValue Result = DAG.getNode(ISD::SUB, dl, VT,
1383                                      DAG.getConstant(0, VT), N2);
1384         SDValue Ops [] = { Borrow, Result };
1385         return DAG.getMergeValues(Ops, 2, dl);
1386       }
1387     }
1388
1389     // fold (lsub x, 0, y) -> 0, sub x, y iff borrow is unused and y has only the
1390     // low bit set
1391     if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 0)) {
1392       APInt KnownZero, KnownOne;
1393       APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1394                                          VT.getSizeInBits() - 1);
1395       DAG.ComputeMaskedBits(N2, Mask, KnownZero, KnownOne);
1396       if (KnownZero == Mask) {
1397         SDValue Borrow = DAG.getConstant(0, VT);
1398         SDValue Result = DAG.getNode(ISD::SUB, dl, VT, N0, N2);
1399         SDValue Ops [] = { Borrow, Result };
1400         return DAG.getMergeValues(Ops, 2, dl);
1401       }
1402     }
1403   }
1404   break;
1405   case XCoreISD::LMUL: {
1406     SDValue N0 = N->getOperand(0);
1407     SDValue N1 = N->getOperand(1);
1408     SDValue N2 = N->getOperand(2);
1409     SDValue N3 = N->getOperand(3);
1410     ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1411     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1412     EVT VT = N0.getValueType();
1413     // Canonicalize multiplicative constant to RHS. If both multiplicative
1414     // operands are constant canonicalize smallest to RHS.
1415     if ((N0C && !N1C) ||
1416         (N0C && N1C && N0C->getZExtValue() < N1C->getZExtValue()))
1417       return DAG.getNode(XCoreISD::LMUL, dl, DAG.getVTList(VT, VT), N1, N0, N2, N3);
1418
1419     // lmul(x, 0, a, b)
1420     if (N1C && N1C->isNullValue()) {
1421       // If the high result is unused fold to add(a, b)
1422       if (N->hasNUsesOfValue(0, 0)) {
1423         SDValue Lo = DAG.getNode(ISD::ADD, dl, VT, N2, N3);
1424         SDValue Ops [] = { Lo, Lo };
1425         return DAG.getMergeValues(Ops, 2, dl);
1426       }
1427       // Otherwise fold to ladd(a, b, 0)
1428       return DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N2, N3, N1);
1429     }
1430   }
1431   break;
1432   case ISD::ADD: {
1433     // Fold 32 bit expressions such as add(add(mul(x,y),a),b) ->
1434     // lmul(x, y, a, b). The high result of lmul will be ignored.
1435     // This is only profitable if the intermediate results are unused
1436     // elsewhere.
1437     SDValue Mul0, Mul1, Addend0, Addend1;
1438     if (N->getValueType(0) == MVT::i32 &&
1439         isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, true)) {
1440       SDValue Ignored = DAG.getNode(XCoreISD::LMUL, dl,
1441                                     DAG.getVTList(MVT::i32, MVT::i32), Mul0,
1442                                     Mul1, Addend0, Addend1);
1443       SDValue Result(Ignored.getNode(), 1);
1444       return Result;
1445     }
1446     APInt HighMask = APInt::getHighBitsSet(64, 32);
1447     // Fold 64 bit expression such as add(add(mul(x,y),a),b) ->
1448     // lmul(x, y, a, b) if all operands are zero-extended. We do this
1449     // before type legalization as it is messy to match the operands after
1450     // that.
1451     if (N->getValueType(0) == MVT::i64 &&
1452         isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, false) &&
1453         DAG.MaskedValueIsZero(Mul0, HighMask) &&
1454         DAG.MaskedValueIsZero(Mul1, HighMask) &&
1455         DAG.MaskedValueIsZero(Addend0, HighMask) &&
1456         DAG.MaskedValueIsZero(Addend1, HighMask)) {
1457       SDValue Mul0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1458                                   Mul0, DAG.getConstant(0, MVT::i32));
1459       SDValue Mul1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1460                                   Mul1, DAG.getConstant(0, MVT::i32));
1461       SDValue Addend0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1462                                      Addend0, DAG.getConstant(0, MVT::i32));
1463       SDValue Addend1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1464                                      Addend1, DAG.getConstant(0, MVT::i32));
1465       SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
1466                                DAG.getVTList(MVT::i32, MVT::i32), Mul0L, Mul1L,
1467                                Addend0L, Addend1L);
1468       SDValue Lo(Hi.getNode(), 1);
1469       return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
1470     }
1471   }
1472   break;
1473   case ISD::STORE: {
1474     // Replace unaligned store of unaligned load with memmove.
1475     StoreSDNode *ST  = cast<StoreSDNode>(N);
1476     if (!DCI.isBeforeLegalize() ||
1477         allowsUnalignedMemoryAccesses(ST->getMemoryVT()) ||
1478         ST->isVolatile() || ST->isIndexed()) {
1479       break;
1480     }
1481     SDValue Chain = ST->getChain();
1482
1483     unsigned StoreBits = ST->getMemoryVT().getStoreSizeInBits();
1484     if (StoreBits % 8) {
1485       break;
1486     }
1487     unsigned ABIAlignment = getTargetData()->getABITypeAlignment(
1488         ST->getMemoryVT().getTypeForEVT(*DCI.DAG.getContext()));
1489     unsigned Alignment = ST->getAlignment();
1490     if (Alignment >= ABIAlignment) {
1491       break;
1492     }
1493
1494     if (LoadSDNode *LD = dyn_cast<LoadSDNode>(ST->getValue())) {
1495       if (LD->hasNUsesOfValue(1, 0) && ST->getMemoryVT() == LD->getMemoryVT() &&
1496         LD->getAlignment() == Alignment &&
1497         !LD->isVolatile() && !LD->isIndexed() &&
1498         Chain.reachesChainWithoutSideEffects(SDValue(LD, 1))) {
1499         return DAG.getMemmove(Chain, dl, ST->getBasePtr(),
1500                               LD->getBasePtr(),
1501                               DAG.getConstant(StoreBits/8, MVT::i32),
1502                               Alignment, false, ST->getPointerInfo(),
1503                               LD->getPointerInfo());
1504       }
1505     }
1506     break;
1507   }
1508   }
1509   return SDValue();
1510 }
1511
1512 void XCoreTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1513                                                          const APInt &Mask,
1514                                                          APInt &KnownZero,
1515                                                          APInt &KnownOne,
1516                                                          const SelectionDAG &DAG,
1517                                                          unsigned Depth) const {
1518   KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
1519   switch (Op.getOpcode()) {
1520   default: break;
1521   case XCoreISD::LADD:
1522   case XCoreISD::LSUB:
1523     if (Op.getResNo() == 0) {
1524       // Top bits of carry / borrow are clear.
1525       KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
1526                                         Mask.getBitWidth() - 1);
1527       KnownZero &= Mask;
1528     }
1529     break;
1530   }
1531 }
1532
1533 //===----------------------------------------------------------------------===//
1534 //  Addressing mode description hooks
1535 //===----------------------------------------------------------------------===//
1536
1537 static inline bool isImmUs(int64_t val)
1538 {
1539   return (val >= 0 && val <= 11);
1540 }
1541
1542 static inline bool isImmUs2(int64_t val)
1543 {
1544   return (val%2 == 0 && isImmUs(val/2));
1545 }
1546
1547 static inline bool isImmUs4(int64_t val)
1548 {
1549   return (val%4 == 0 && isImmUs(val/4));
1550 }
1551
1552 /// isLegalAddressingMode - Return true if the addressing mode represented
1553 /// by AM is legal for this target, for a load/store of the specified type.
1554 bool
1555 XCoreTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1556                                               Type *Ty) const {
1557   if (Ty->getTypeID() == Type::VoidTyID)
1558     return AM.Scale == 0 && isImmUs(AM.BaseOffs) && isImmUs4(AM.BaseOffs);
1559
1560   const TargetData *TD = TM.getTargetData();
1561   unsigned Size = TD->getTypeAllocSize(Ty);
1562   if (AM.BaseGV) {
1563     return Size >= 4 && !AM.HasBaseReg && AM.Scale == 0 &&
1564                  AM.BaseOffs%4 == 0;
1565   }
1566
1567   switch (Size) {
1568   case 1:
1569     // reg + imm
1570     if (AM.Scale == 0) {
1571       return isImmUs(AM.BaseOffs);
1572     }
1573     // reg + reg
1574     return AM.Scale == 1 && AM.BaseOffs == 0;
1575   case 2:
1576   case 3:
1577     // reg + imm
1578     if (AM.Scale == 0) {
1579       return isImmUs2(AM.BaseOffs);
1580     }
1581     // reg + reg<<1
1582     return AM.Scale == 2 && AM.BaseOffs == 0;
1583   default:
1584     // reg + imm
1585     if (AM.Scale == 0) {
1586       return isImmUs4(AM.BaseOffs);
1587     }
1588     // reg + reg<<2
1589     return AM.Scale == 4 && AM.BaseOffs == 0;
1590   }
1591
1592   return false;
1593 }
1594
1595 //===----------------------------------------------------------------------===//
1596 //                           XCore Inline Assembly Support
1597 //===----------------------------------------------------------------------===//
1598
1599 std::pair<unsigned, const TargetRegisterClass*>
1600 XCoreTargetLowering::
1601 getRegForInlineAsmConstraint(const std::string &Constraint,
1602                              EVT VT) const {
1603   if (Constraint.size() == 1) {
1604     switch (Constraint[0]) {
1605     default : break;
1606     case 'r':
1607       return std::make_pair(0U, XCore::GRRegsRegisterClass);
1608     }
1609   }
1610   // Use the default implementation in TargetLowering to convert the register
1611   // constraint into a member of a register class.
1612   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1613 }