1 //===-- XCoreISelLowering.cpp - XCore DAG Lowering Implementation ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the XCoreTargetLowering class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "xcore-lower"
16 #include "XCoreISelLowering.h"
18 #include "XCoreMachineFunctionInfo.h"
19 #include "XCoreSubtarget.h"
20 #include "XCoreTargetMachine.h"
21 #include "XCoreTargetObjectFile.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineJumpTableInfo.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAGISel.h"
29 #include "llvm/CodeGen/ValueTypes.h"
30 #include "llvm/IR/CallingConv.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GlobalAlias.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
43 const char *XCoreTargetLowering::
44 getTargetNodeName(unsigned Opcode) const
48 case XCoreISD::BL : return "XCoreISD::BL";
49 case XCoreISD::PCRelativeWrapper : return "XCoreISD::PCRelativeWrapper";
50 case XCoreISD::DPRelativeWrapper : return "XCoreISD::DPRelativeWrapper";
51 case XCoreISD::CPRelativeWrapper : return "XCoreISD::CPRelativeWrapper";
52 case XCoreISD::STWSP : return "XCoreISD::STWSP";
53 case XCoreISD::RETSP : return "XCoreISD::RETSP";
54 case XCoreISD::LADD : return "XCoreISD::LADD";
55 case XCoreISD::LSUB : return "XCoreISD::LSUB";
56 case XCoreISD::LMUL : return "XCoreISD::LMUL";
57 case XCoreISD::MACCU : return "XCoreISD::MACCU";
58 case XCoreISD::MACCS : return "XCoreISD::MACCS";
59 case XCoreISD::CRC8 : return "XCoreISD::CRC8";
60 case XCoreISD::BR_JT : return "XCoreISD::BR_JT";
61 case XCoreISD::BR_JT32 : return "XCoreISD::BR_JT32";
62 default : return NULL;
66 XCoreTargetLowering::XCoreTargetLowering(XCoreTargetMachine &XTM)
67 : TargetLowering(XTM, new XCoreTargetObjectFile()),
69 Subtarget(*XTM.getSubtargetImpl()) {
71 // Set up the register classes.
72 addRegisterClass(MVT::i32, &XCore::GRRegsRegClass);
74 // Compute derived properties from the register classes
75 computeRegisterProperties();
77 // Division is expensive
78 setIntDivIsCheap(false);
80 setStackPointerRegisterToSaveRestore(XCore::SP);
82 setSchedulingPreference(Sched::RegPressure);
84 // Use i32 for setcc operations results (slt, sgt, ...).
85 setBooleanContents(ZeroOrOneBooleanContent);
86 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
88 // XCore does not have the NodeTypes below.
89 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
90 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
91 setOperationAction(ISD::ADDC, MVT::i32, Expand);
92 setOperationAction(ISD::ADDE, MVT::i32, Expand);
93 setOperationAction(ISD::SUBC, MVT::i32, Expand);
94 setOperationAction(ISD::SUBE, MVT::i32, Expand);
96 // Stop the combiner recombining select and set_cc
97 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
100 setOperationAction(ISD::ADD, MVT::i64, Custom);
101 setOperationAction(ISD::SUB, MVT::i64, Custom);
102 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
103 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
104 setOperationAction(ISD::MULHS, MVT::i32, Expand);
105 setOperationAction(ISD::MULHU, MVT::i32, Expand);
106 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
107 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
108 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
111 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
112 setOperationAction(ISD::ROTL , MVT::i32, Expand);
113 setOperationAction(ISD::ROTR , MVT::i32, Expand);
114 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
115 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
117 setOperationAction(ISD::TRAP, MVT::Other, Legal);
120 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
122 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
123 setOperationAction(ISD::BlockAddress, MVT::i32 , Custom);
125 // Conversion of i64 -> double produces constantpool nodes
126 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
129 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
130 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
131 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
133 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
134 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
136 // Custom expand misaligned loads / stores.
137 setOperationAction(ISD::LOAD, MVT::i32, Custom);
138 setOperationAction(ISD::STORE, MVT::i32, Custom);
141 setOperationAction(ISD::VAEND, MVT::Other, Expand);
142 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
143 setOperationAction(ISD::VAARG, MVT::Other, Custom);
144 setOperationAction(ISD::VASTART, MVT::Other, Custom);
147 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
148 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
149 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
151 // TRAMPOLINE is custom lowered.
152 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
153 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
155 // We want to custom lower some of our intrinsics.
156 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
158 MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 4;
159 MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize
160 = MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 2;
162 // We have target-specific dag combine patterns for the following nodes:
163 setTargetDAGCombine(ISD::STORE);
164 setTargetDAGCombine(ISD::ADD);
166 setMinFunctionAlignment(1);
169 SDValue XCoreTargetLowering::
170 LowerOperation(SDValue Op, SelectionDAG &DAG) const {
171 switch (Op.getOpcode())
173 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
174 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
175 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
176 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
177 case ISD::LOAD: return LowerLOAD(Op, DAG);
178 case ISD::STORE: return LowerSTORE(Op, DAG);
179 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
180 case ISD::VAARG: return LowerVAARG(Op, DAG);
181 case ISD::VASTART: return LowerVASTART(Op, DAG);
182 case ISD::SMUL_LOHI: return LowerSMUL_LOHI(Op, DAG);
183 case ISD::UMUL_LOHI: return LowerUMUL_LOHI(Op, DAG);
184 // FIXME: Remove these when LegalizeDAGTypes lands.
186 case ISD::SUB: return ExpandADDSUB(Op.getNode(), DAG);
187 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
188 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
189 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
190 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
192 llvm_unreachable("unimplemented operand");
196 /// ReplaceNodeResults - Replace the results of node with an illegal result
197 /// type with new values built out of custom code.
198 void XCoreTargetLowering::ReplaceNodeResults(SDNode *N,
199 SmallVectorImpl<SDValue>&Results,
200 SelectionDAG &DAG) const {
201 switch (N->getOpcode()) {
203 llvm_unreachable("Don't know how to custom expand this!");
206 Results.push_back(ExpandADDSUB(N, DAG));
211 //===----------------------------------------------------------------------===//
212 // Misc Lower Operation implementation
213 //===----------------------------------------------------------------------===//
215 SDValue XCoreTargetLowering::
216 LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
219 SDValue Cond = DAG.getNode(ISD::SETCC, dl, MVT::i32, Op.getOperand(2),
220 Op.getOperand(3), Op.getOperand(4));
221 return DAG.getNode(ISD::SELECT, dl, MVT::i32, Cond, Op.getOperand(0),
225 SDValue XCoreTargetLowering::
226 getGlobalAddressWrapper(SDValue GA, const GlobalValue *GV,
227 SelectionDAG &DAG) const
229 // FIXME there is no actual debug info here
231 const GlobalValue *UnderlyingGV = GV;
232 // If GV is an alias then use the aliasee to determine the wrapper type
233 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
234 UnderlyingGV = GA->resolveAliasedGlobal();
235 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(UnderlyingGV)) {
236 if (GVar->isConstant())
237 return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA);
238 return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA);
240 return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA);
243 SDValue XCoreTargetLowering::
244 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const
247 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
248 const GlobalValue *GV = GN->getGlobal();
249 int64_t Offset = GN->getOffset();
250 // We can only fold positive offsets that are a multiple of the word size.
251 int64_t FoldedOffset = std::max(Offset & ~3, (int64_t)0);
252 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, FoldedOffset);
253 GA = getGlobalAddressWrapper(GA, GV, DAG);
254 // Handle the rest of the offset.
255 if (Offset != FoldedOffset) {
256 SDValue Remaining = DAG.getConstant(Offset - FoldedOffset, MVT::i32);
257 GA = DAG.getNode(ISD::ADD, DL, MVT::i32, GA, Remaining);
262 static inline SDValue BuildGetId(SelectionDAG &DAG, SDLoc dl) {
263 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
264 DAG.getConstant(Intrinsic::xcore_getid, MVT::i32));
267 SDValue XCoreTargetLowering::
268 LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const
272 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
273 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
275 return DAG.getNode(XCoreISD::PCRelativeWrapper, DL, getPointerTy(), Result);
278 SDValue XCoreTargetLowering::
279 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
281 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
282 // FIXME there isn't really debug info here
284 EVT PtrVT = Op.getValueType();
286 if (CP->isMachineConstantPoolEntry()) {
287 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
290 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
293 return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, Res);
296 unsigned XCoreTargetLowering::getJumpTableEncoding() const {
297 return MachineJumpTableInfo::EK_Inline;
300 SDValue XCoreTargetLowering::
301 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
303 SDValue Chain = Op.getOperand(0);
304 SDValue Table = Op.getOperand(1);
305 SDValue Index = Op.getOperand(2);
307 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
308 unsigned JTI = JT->getIndex();
309 MachineFunction &MF = DAG.getMachineFunction();
310 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
311 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
313 unsigned NumEntries = MJTI->getJumpTables()[JTI].MBBs.size();
314 if (NumEntries <= 32) {
315 return DAG.getNode(XCoreISD::BR_JT, dl, MVT::Other, Chain, TargetJT, Index);
317 assert((NumEntries >> 31) == 0);
318 SDValue ScaledIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
319 DAG.getConstant(1, MVT::i32));
320 return DAG.getNode(XCoreISD::BR_JT32, dl, MVT::Other, Chain, TargetJT,
324 SDValue XCoreTargetLowering::
325 lowerLoadWordFromAlignedBasePlusOffset(SDLoc DL, SDValue Chain, SDValue Base,
326 int64_t Offset, SelectionDAG &DAG) const
328 if ((Offset & 0x3) == 0) {
329 return DAG.getLoad(getPointerTy(), DL, Chain, Base, MachinePointerInfo(),
330 false, false, false, 0);
332 // Lower to pair of consecutive word aligned loads plus some bit shifting.
333 int32_t HighOffset = RoundUpToAlignment(Offset, 4);
334 int32_t LowOffset = HighOffset - 4;
335 SDValue LowAddr, HighAddr;
336 if (GlobalAddressSDNode *GASD =
337 dyn_cast<GlobalAddressSDNode>(Base.getNode())) {
338 LowAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
340 HighAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
343 LowAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
344 DAG.getConstant(LowOffset, MVT::i32));
345 HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
346 DAG.getConstant(HighOffset, MVT::i32));
348 SDValue LowShift = DAG.getConstant((Offset - LowOffset) * 8, MVT::i32);
349 SDValue HighShift = DAG.getConstant((HighOffset - Offset) * 8, MVT::i32);
351 SDValue Low = DAG.getLoad(getPointerTy(), DL, Chain,
352 LowAddr, MachinePointerInfo(),
353 false, false, false, 0);
354 SDValue High = DAG.getLoad(getPointerTy(), DL, Chain,
355 HighAddr, MachinePointerInfo(),
356 false, false, false, 0);
357 SDValue LowShifted = DAG.getNode(ISD::SRL, DL, MVT::i32, Low, LowShift);
358 SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High, HighShift);
359 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, LowShifted, HighShifted);
360 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
362 SDValue Ops[] = { Result, Chain };
363 return DAG.getMergeValues(Ops, 2, DL);
366 static bool isWordAligned(SDValue Value, SelectionDAG &DAG)
368 APInt KnownZero, KnownOne;
369 DAG.ComputeMaskedBits(Value, KnownZero, KnownOne);
370 return KnownZero.countTrailingOnes() >= 2;
373 SDValue XCoreTargetLowering::
374 LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
375 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
376 LoadSDNode *LD = cast<LoadSDNode>(Op);
377 assert(LD->getExtensionType() == ISD::NON_EXTLOAD &&
378 "Unexpected extension type");
379 assert(LD->getMemoryVT() == MVT::i32 && "Unexpected load EVT");
380 if (allowsUnalignedMemoryAccesses(LD->getMemoryVT()))
383 unsigned ABIAlignment = getDataLayout()->
384 getABITypeAlignment(LD->getMemoryVT().getTypeForEVT(*DAG.getContext()));
385 // Leave aligned load alone.
386 if (LD->getAlignment() >= ABIAlignment)
389 SDValue Chain = LD->getChain();
390 SDValue BasePtr = LD->getBasePtr();
393 if (!LD->isVolatile()) {
394 const GlobalValue *GV;
396 if (DAG.isBaseWithConstantOffset(BasePtr) &&
397 isWordAligned(BasePtr->getOperand(0), DAG)) {
398 SDValue NewBasePtr = BasePtr->getOperand(0);
399 Offset = cast<ConstantSDNode>(BasePtr->getOperand(1))->getSExtValue();
400 return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
403 if (TLI.isGAPlusOffset(BasePtr.getNode(), GV, Offset) &&
404 MinAlign(GV->getAlignment(), 4) == 4) {
405 SDValue NewBasePtr = DAG.getGlobalAddress(GV, DL,
406 BasePtr->getValueType(0));
407 return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
412 if (LD->getAlignment() == 2) {
413 SDValue Low = DAG.getExtLoad(ISD::ZEXTLOAD, DL, MVT::i32, Chain,
414 BasePtr, LD->getPointerInfo(), MVT::i16,
415 LD->isVolatile(), LD->isNonTemporal(), 2);
416 SDValue HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
417 DAG.getConstant(2, MVT::i32));
418 SDValue High = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
420 LD->getPointerInfo().getWithOffset(2),
421 MVT::i16, LD->isVolatile(),
422 LD->isNonTemporal(), 2);
423 SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High,
424 DAG.getConstant(16, MVT::i32));
425 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Low, HighShifted);
426 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
428 SDValue Ops[] = { Result, Chain };
429 return DAG.getMergeValues(Ops, 2, DL);
432 // Lower to a call to __misaligned_load(BasePtr).
433 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
434 TargetLowering::ArgListTy Args;
435 TargetLowering::ArgListEntry Entry;
438 Entry.Node = BasePtr;
439 Args.push_back(Entry);
441 TargetLowering::CallLoweringInfo CLI(Chain, IntPtrTy, false, false,
442 false, false, 0, CallingConv::C, /*isTailCall=*/false,
443 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
444 DAG.getExternalSymbol("__misaligned_load", getPointerTy()),
446 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
449 { CallResult.first, CallResult.second };
451 return DAG.getMergeValues(Ops, 2, DL);
454 SDValue XCoreTargetLowering::
455 LowerSTORE(SDValue Op, SelectionDAG &DAG) const
457 StoreSDNode *ST = cast<StoreSDNode>(Op);
458 assert(!ST->isTruncatingStore() && "Unexpected store type");
459 assert(ST->getMemoryVT() == MVT::i32 && "Unexpected store EVT");
460 if (allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
463 unsigned ABIAlignment = getDataLayout()->
464 getABITypeAlignment(ST->getMemoryVT().getTypeForEVT(*DAG.getContext()));
465 // Leave aligned store alone.
466 if (ST->getAlignment() >= ABIAlignment) {
469 SDValue Chain = ST->getChain();
470 SDValue BasePtr = ST->getBasePtr();
471 SDValue Value = ST->getValue();
474 if (ST->getAlignment() == 2) {
476 SDValue High = DAG.getNode(ISD::SRL, dl, MVT::i32, Value,
477 DAG.getConstant(16, MVT::i32));
478 SDValue StoreLow = DAG.getTruncStore(Chain, dl, Low, BasePtr,
479 ST->getPointerInfo(), MVT::i16,
480 ST->isVolatile(), ST->isNonTemporal(),
482 SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr,
483 DAG.getConstant(2, MVT::i32));
484 SDValue StoreHigh = DAG.getTruncStore(Chain, dl, High, HighAddr,
485 ST->getPointerInfo().getWithOffset(2),
486 MVT::i16, ST->isVolatile(),
487 ST->isNonTemporal(), 2);
488 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, StoreLow, StoreHigh);
491 // Lower to a call to __misaligned_store(BasePtr, Value).
492 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
493 TargetLowering::ArgListTy Args;
494 TargetLowering::ArgListEntry Entry;
497 Entry.Node = BasePtr;
498 Args.push_back(Entry);
501 Args.push_back(Entry);
503 TargetLowering::CallLoweringInfo CLI(Chain,
504 Type::getVoidTy(*DAG.getContext()), false, false,
505 false, false, 0, CallingConv::C, /*isTailCall=*/false,
506 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
507 DAG.getExternalSymbol("__misaligned_store", getPointerTy()),
509 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
511 return CallResult.second;
514 SDValue XCoreTargetLowering::
515 LowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
517 assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::SMUL_LOHI &&
518 "Unexpected operand to lower!");
520 SDValue LHS = Op.getOperand(0);
521 SDValue RHS = Op.getOperand(1);
522 SDValue Zero = DAG.getConstant(0, MVT::i32);
523 SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
524 DAG.getVTList(MVT::i32, MVT::i32), Zero, Zero,
526 SDValue Lo(Hi.getNode(), 1);
527 SDValue Ops[] = { Lo, Hi };
528 return DAG.getMergeValues(Ops, 2, dl);
531 SDValue XCoreTargetLowering::
532 LowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
534 assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::UMUL_LOHI &&
535 "Unexpected operand to lower!");
537 SDValue LHS = Op.getOperand(0);
538 SDValue RHS = Op.getOperand(1);
539 SDValue Zero = DAG.getConstant(0, MVT::i32);
540 SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
541 DAG.getVTList(MVT::i32, MVT::i32), LHS, RHS,
543 SDValue Lo(Hi.getNode(), 1);
544 SDValue Ops[] = { Lo, Hi };
545 return DAG.getMergeValues(Ops, 2, dl);
548 /// isADDADDMUL - Return whether Op is in a form that is equivalent to
549 /// add(add(mul(x,y),a),b). If requireIntermediatesHaveOneUse is true then
550 /// each intermediate result in the calculation must also have a single use.
551 /// If the Op is in the correct form the constituent parts are written to Mul0,
552 /// Mul1, Addend0 and Addend1.
554 isADDADDMUL(SDValue Op, SDValue &Mul0, SDValue &Mul1, SDValue &Addend0,
555 SDValue &Addend1, bool requireIntermediatesHaveOneUse)
557 if (Op.getOpcode() != ISD::ADD)
559 SDValue N0 = Op.getOperand(0);
560 SDValue N1 = Op.getOperand(1);
563 if (N0.getOpcode() == ISD::ADD) {
566 } else if (N1.getOpcode() == ISD::ADD) {
572 if (requireIntermediatesHaveOneUse && !AddOp.hasOneUse())
574 if (OtherOp.getOpcode() == ISD::MUL) {
575 // add(add(a,b),mul(x,y))
576 if (requireIntermediatesHaveOneUse && !OtherOp.hasOneUse())
578 Mul0 = OtherOp.getOperand(0);
579 Mul1 = OtherOp.getOperand(1);
580 Addend0 = AddOp.getOperand(0);
581 Addend1 = AddOp.getOperand(1);
584 if (AddOp.getOperand(0).getOpcode() == ISD::MUL) {
585 // add(add(mul(x,y),a),b)
586 if (requireIntermediatesHaveOneUse && !AddOp.getOperand(0).hasOneUse())
588 Mul0 = AddOp.getOperand(0).getOperand(0);
589 Mul1 = AddOp.getOperand(0).getOperand(1);
590 Addend0 = AddOp.getOperand(1);
594 if (AddOp.getOperand(1).getOpcode() == ISD::MUL) {
595 // add(add(a,mul(x,y)),b)
596 if (requireIntermediatesHaveOneUse && !AddOp.getOperand(1).hasOneUse())
598 Mul0 = AddOp.getOperand(1).getOperand(0);
599 Mul1 = AddOp.getOperand(1).getOperand(1);
600 Addend0 = AddOp.getOperand(0);
607 SDValue XCoreTargetLowering::
608 TryExpandADDWithMul(SDNode *N, SelectionDAG &DAG) const
612 if (N->getOperand(0).getOpcode() == ISD::MUL) {
613 Mul = N->getOperand(0);
614 Other = N->getOperand(1);
615 } else if (N->getOperand(1).getOpcode() == ISD::MUL) {
616 Mul = N->getOperand(1);
617 Other = N->getOperand(0);
622 SDValue LL, RL, AddendL, AddendH;
623 LL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
624 Mul.getOperand(0), DAG.getConstant(0, MVT::i32));
625 RL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
626 Mul.getOperand(1), DAG.getConstant(0, MVT::i32));
627 AddendL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
628 Other, DAG.getConstant(0, MVT::i32));
629 AddendH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
630 Other, DAG.getConstant(1, MVT::i32));
631 APInt HighMask = APInt::getHighBitsSet(64, 32);
632 unsigned LHSSB = DAG.ComputeNumSignBits(Mul.getOperand(0));
633 unsigned RHSSB = DAG.ComputeNumSignBits(Mul.getOperand(1));
634 if (DAG.MaskedValueIsZero(Mul.getOperand(0), HighMask) &&
635 DAG.MaskedValueIsZero(Mul.getOperand(1), HighMask)) {
636 // The inputs are both zero-extended.
637 SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
638 DAG.getVTList(MVT::i32, MVT::i32), AddendH,
640 SDValue Lo(Hi.getNode(), 1);
641 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
643 if (LHSSB > 32 && RHSSB > 32) {
644 // The inputs are both sign-extended.
645 SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
646 DAG.getVTList(MVT::i32, MVT::i32), AddendH,
648 SDValue Lo(Hi.getNode(), 1);
649 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
652 LH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
653 Mul.getOperand(0), DAG.getConstant(1, MVT::i32));
654 RH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
655 Mul.getOperand(1), DAG.getConstant(1, MVT::i32));
656 SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
657 DAG.getVTList(MVT::i32, MVT::i32), AddendH,
659 SDValue Lo(Hi.getNode(), 1);
660 RH = DAG.getNode(ISD::MUL, dl, MVT::i32, LL, RH);
661 LH = DAG.getNode(ISD::MUL, dl, MVT::i32, LH, RL);
662 Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, RH);
663 Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, LH);
664 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
667 SDValue XCoreTargetLowering::
668 ExpandADDSUB(SDNode *N, SelectionDAG &DAG) const
670 assert(N->getValueType(0) == MVT::i64 &&
671 (N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&
672 "Unknown operand to lower!");
674 if (N->getOpcode() == ISD::ADD) {
675 SDValue Result = TryExpandADDWithMul(N, DAG);
676 if (Result.getNode() != 0)
682 // Extract components
683 SDValue LHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
684 N->getOperand(0), DAG.getConstant(0, MVT::i32));
685 SDValue LHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
686 N->getOperand(0), DAG.getConstant(1, MVT::i32));
687 SDValue RHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
688 N->getOperand(1), DAG.getConstant(0, MVT::i32));
689 SDValue RHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
690 N->getOperand(1), DAG.getConstant(1, MVT::i32));
693 unsigned Opcode = (N->getOpcode() == ISD::ADD) ? XCoreISD::LADD :
695 SDValue Zero = DAG.getConstant(0, MVT::i32);
696 SDValue Lo = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
698 SDValue Carry(Lo.getNode(), 1);
700 SDValue Hi = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
702 SDValue Ignored(Hi.getNode(), 1);
704 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
707 SDValue XCoreTargetLowering::
708 LowerVAARG(SDValue Op, SelectionDAG &DAG) const
710 llvm_unreachable("unimplemented");
711 // FIXME Arguments passed by reference need a extra dereference.
712 SDNode *Node = Op.getNode();
714 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
715 EVT VT = Node->getValueType(0);
716 SDValue VAList = DAG.getLoad(getPointerTy(), dl, Node->getOperand(0),
717 Node->getOperand(1), MachinePointerInfo(V),
718 false, false, false, 0);
719 // Increment the pointer, VAList, to the next vararg
720 SDValue Tmp3 = DAG.getNode(ISD::ADD, dl, getPointerTy(), VAList,
721 DAG.getConstant(VT.getSizeInBits(),
723 // Store the incremented VAList to the legalized pointer
724 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Node->getOperand(1),
725 MachinePointerInfo(V), false, false, 0);
726 // Load the actual argument out of the pointer VAList
727 return DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
728 false, false, false, 0);
731 SDValue XCoreTargetLowering::
732 LowerVASTART(SDValue Op, SelectionDAG &DAG) const
735 // vastart stores the address of the VarArgsFrameIndex slot into the
736 // memory location argument
737 MachineFunction &MF = DAG.getMachineFunction();
738 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
739 SDValue Addr = DAG.getFrameIndex(XFI->getVarArgsFrameIndex(), MVT::i32);
740 return DAG.getStore(Op.getOperand(0), dl, Addr, Op.getOperand(1),
741 MachinePointerInfo(), false, false, 0);
744 SDValue XCoreTargetLowering::LowerFRAMEADDR(SDValue Op,
745 SelectionDAG &DAG) const {
747 // Depths > 0 not supported yet!
748 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
751 MachineFunction &MF = DAG.getMachineFunction();
752 const TargetRegisterInfo *RegInfo = getTargetMachine().getRegisterInfo();
753 return DAG.getCopyFromReg(DAG.getEntryNode(), dl,
754 RegInfo->getFrameRegister(MF), MVT::i32);
757 SDValue XCoreTargetLowering::
758 LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
759 return Op.getOperand(0);
762 SDValue XCoreTargetLowering::
763 LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
764 SDValue Chain = Op.getOperand(0);
765 SDValue Trmp = Op.getOperand(1); // trampoline
766 SDValue FPtr = Op.getOperand(2); // nested function
767 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
769 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
772 // LDAPF_u10 r11, nest
773 // LDW_2rus r11, r11[0]
774 // STWSP_ru6 r11, sp[0]
775 // LDAPF_u10 r11, fptr
776 // LDW_2rus r11, r11[0]
782 SDValue OutChains[5];
787 OutChains[0] = DAG.getStore(Chain, dl, DAG.getConstant(0x0a3cd805, MVT::i32),
788 Addr, MachinePointerInfo(TrmpAddr), false, false,
791 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
792 DAG.getConstant(4, MVT::i32));
793 OutChains[1] = DAG.getStore(Chain, dl, DAG.getConstant(0xd80456c0, MVT::i32),
794 Addr, MachinePointerInfo(TrmpAddr, 4), false,
797 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
798 DAG.getConstant(8, MVT::i32));
799 OutChains[2] = DAG.getStore(Chain, dl, DAG.getConstant(0x27fb0a3c, MVT::i32),
800 Addr, MachinePointerInfo(TrmpAddr, 8), false,
803 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
804 DAG.getConstant(12, MVT::i32));
805 OutChains[3] = DAG.getStore(Chain, dl, Nest, Addr,
806 MachinePointerInfo(TrmpAddr, 12), false, false,
809 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
810 DAG.getConstant(16, MVT::i32));
811 OutChains[4] = DAG.getStore(Chain, dl, FPtr, Addr,
812 MachinePointerInfo(TrmpAddr, 16), false, false,
815 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 5);
818 SDValue XCoreTargetLowering::
819 LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
821 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
823 case Intrinsic::xcore_crc8:
824 EVT VT = Op.getValueType();
826 DAG.getNode(XCoreISD::CRC8, DL, DAG.getVTList(VT, VT),
827 Op.getOperand(1), Op.getOperand(2) , Op.getOperand(3));
828 SDValue Crc(Data.getNode(), 1);
829 SDValue Results[] = { Crc, Data };
830 return DAG.getMergeValues(Results, 2, DL);
835 //===----------------------------------------------------------------------===//
836 // Calling Convention Implementation
837 //===----------------------------------------------------------------------===//
839 #include "XCoreGenCallingConv.inc"
841 //===----------------------------------------------------------------------===//
842 // Call Calling Convention Implementation
843 //===----------------------------------------------------------------------===//
845 /// XCore call implementation
847 XCoreTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
848 SmallVectorImpl<SDValue> &InVals) const {
849 SelectionDAG &DAG = CLI.DAG;
851 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
852 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
853 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
854 SDValue Chain = CLI.Chain;
855 SDValue Callee = CLI.Callee;
856 bool &isTailCall = CLI.IsTailCall;
857 CallingConv::ID CallConv = CLI.CallConv;
858 bool isVarArg = CLI.IsVarArg;
860 // XCore target does not yet support tail call optimization.
863 // For now, only CallingConv::C implemented
867 llvm_unreachable("Unsupported calling convention");
868 case CallingConv::Fast:
870 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
871 Outs, OutVals, Ins, dl, DAG, InVals);
875 /// LowerCCCCallTo - functions arguments are copied from virtual
876 /// regs to (physical regs)/(stack frame), CALLSEQ_START and
877 /// CALLSEQ_END are emitted.
878 /// TODO: isTailCall, sret.
880 XCoreTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
881 CallingConv::ID CallConv, bool isVarArg,
883 const SmallVectorImpl<ISD::OutputArg> &Outs,
884 const SmallVectorImpl<SDValue> &OutVals,
885 const SmallVectorImpl<ISD::InputArg> &Ins,
886 SDLoc dl, SelectionDAG &DAG,
887 SmallVectorImpl<SDValue> &InVals) const {
889 // Analyze operands of the call, assigning locations to each operand.
890 SmallVector<CCValAssign, 16> ArgLocs;
891 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
892 getTargetMachine(), ArgLocs, *DAG.getContext());
894 // The ABI dictates there should be one stack slot available to the callee
895 // on function entry (for saving lr).
896 CCInfo.AllocateStack(4, 4);
898 CCInfo.AnalyzeCallOperands(Outs, CC_XCore);
900 // Get a count of how many bytes are to be pushed on the stack.
901 unsigned NumBytes = CCInfo.getNextStackOffset();
903 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
904 getPointerTy(), true), dl);
906 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
907 SmallVector<SDValue, 12> MemOpChains;
909 // Walk the register/memloc assignments, inserting copies/loads.
910 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
911 CCValAssign &VA = ArgLocs[i];
912 SDValue Arg = OutVals[i];
914 // Promote the value if needed.
915 switch (VA.getLocInfo()) {
916 default: llvm_unreachable("Unknown loc info!");
917 case CCValAssign::Full: break;
918 case CCValAssign::SExt:
919 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
921 case CCValAssign::ZExt:
922 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
924 case CCValAssign::AExt:
925 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
929 // Arguments that can be passed on register must be kept at
932 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
934 assert(VA.isMemLoc());
936 int Offset = VA.getLocMemOffset();
938 MemOpChains.push_back(DAG.getNode(XCoreISD::STWSP, dl, MVT::Other,
940 DAG.getConstant(Offset/4, MVT::i32)));
944 // Transform all store nodes into one single node because
945 // all store nodes are independent of each other.
946 if (!MemOpChains.empty())
947 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
948 &MemOpChains[0], MemOpChains.size());
950 // Build a sequence of copy-to-reg nodes chained together with token
951 // chain and flag operands which copy the outgoing args into registers.
952 // The InFlag in necessary since all emitted instructions must be
955 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
956 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
957 RegsToPass[i].second, InFlag);
958 InFlag = Chain.getValue(1);
961 // If the callee is a GlobalAddress node (quite common, every direct call is)
962 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
963 // Likewise ExternalSymbol -> TargetExternalSymbol.
964 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
965 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
966 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
967 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
969 // XCoreBranchLink = #chain, #target_address, #opt_in_flags...
970 // = Chain, Callee, Reg#1, Reg#2, ...
972 // Returns a chain & a flag for retval copy to use.
973 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
974 SmallVector<SDValue, 8> Ops;
975 Ops.push_back(Chain);
976 Ops.push_back(Callee);
978 // Add argument registers to the end of the list so that they are
979 // known live into the call.
980 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
981 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
982 RegsToPass[i].second.getValueType()));
984 if (InFlag.getNode())
985 Ops.push_back(InFlag);
987 Chain = DAG.getNode(XCoreISD::BL, dl, NodeTys, &Ops[0], Ops.size());
988 InFlag = Chain.getValue(1);
990 // Create the CALLSEQ_END node.
991 Chain = DAG.getCALLSEQ_END(Chain,
992 DAG.getConstant(NumBytes, getPointerTy(), true),
993 DAG.getConstant(0, getPointerTy(), true),
995 InFlag = Chain.getValue(1);
997 // Handle result values, copying them out of physregs into vregs that we
999 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1000 Ins, dl, DAG, InVals);
1003 /// LowerCallResult - Lower the result values of a call into the
1004 /// appropriate copies out of appropriate physical registers.
1006 XCoreTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1007 CallingConv::ID CallConv, bool isVarArg,
1008 const SmallVectorImpl<ISD::InputArg> &Ins,
1009 SDLoc dl, SelectionDAG &DAG,
1010 SmallVectorImpl<SDValue> &InVals) const {
1012 // Assign locations to each value returned by this call.
1013 SmallVector<CCValAssign, 16> RVLocs;
1014 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1015 getTargetMachine(), RVLocs, *DAG.getContext());
1017 CCInfo.AnalyzeCallResult(Ins, RetCC_XCore);
1019 // Copy all of the result registers out of their specified physreg.
1020 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1021 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
1022 RVLocs[i].getValVT(), InFlag).getValue(1);
1023 InFlag = Chain.getValue(2);
1024 InVals.push_back(Chain.getValue(0));
1030 //===----------------------------------------------------------------------===//
1031 // Formal Arguments Calling Convention Implementation
1032 //===----------------------------------------------------------------------===//
1034 /// XCore formal arguments implementation
1036 XCoreTargetLowering::LowerFormalArguments(SDValue Chain,
1037 CallingConv::ID CallConv,
1039 const SmallVectorImpl<ISD::InputArg> &Ins,
1042 SmallVectorImpl<SDValue> &InVals)
1047 llvm_unreachable("Unsupported calling convention");
1048 case CallingConv::C:
1049 case CallingConv::Fast:
1050 return LowerCCCArguments(Chain, CallConv, isVarArg,
1051 Ins, dl, DAG, InVals);
1055 /// LowerCCCArguments - transform physical registers into
1056 /// virtual registers and generate load operations for
1057 /// arguments places on the stack.
1060 XCoreTargetLowering::LowerCCCArguments(SDValue Chain,
1061 CallingConv::ID CallConv,
1063 const SmallVectorImpl<ISD::InputArg>
1067 SmallVectorImpl<SDValue> &InVals) const {
1068 MachineFunction &MF = DAG.getMachineFunction();
1069 MachineFrameInfo *MFI = MF.getFrameInfo();
1070 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1072 // Assign locations to all of the incoming arguments.
1073 SmallVector<CCValAssign, 16> ArgLocs;
1074 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1075 getTargetMachine(), ArgLocs, *DAG.getContext());
1077 CCInfo.AnalyzeFormalArguments(Ins, CC_XCore);
1079 unsigned StackSlotSize = XCoreFrameLowering::stackSlotSize();
1081 unsigned LRSaveSize = StackSlotSize;
1083 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1085 CCValAssign &VA = ArgLocs[i];
1087 if (VA.isRegLoc()) {
1088 // Arguments passed in registers
1089 EVT RegVT = VA.getLocVT();
1090 switch (RegVT.getSimpleVT().SimpleTy) {
1094 errs() << "LowerFormalArguments Unhandled argument type: "
1095 << RegVT.getSimpleVT().SimpleTy << "\n";
1097 llvm_unreachable(0);
1100 unsigned VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
1101 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1102 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
1106 assert(VA.isMemLoc());
1107 // Load the argument to a virtual register
1108 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
1109 if (ObjSize > StackSlotSize) {
1110 errs() << "LowerFormalArguments Unhandled argument type: "
1111 << EVT(VA.getLocVT()).getEVTString()
1114 // Create the frame index object for this incoming parameter...
1115 int FI = MFI->CreateFixedObject(ObjSize,
1116 LRSaveSize + VA.getLocMemOffset(),
1119 // Create the SelectionDAG nodes corresponding to a load
1120 //from this parameter
1121 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1122 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1123 MachinePointerInfo::getFixedStack(FI),
1124 false, false, false, 0));
1129 /* Argument registers */
1130 static const uint16_t ArgRegs[] = {
1131 XCore::R0, XCore::R1, XCore::R2, XCore::R3
1133 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
1134 unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs,
1135 array_lengthof(ArgRegs));
1136 if (FirstVAReg < array_lengthof(ArgRegs)) {
1137 SmallVector<SDValue, 4> MemOps;
1139 // Save remaining registers, storing higher register numbers at a higher
1141 for (int i = array_lengthof(ArgRegs) - 1; i >= (int)FirstVAReg; --i) {
1142 // Create a stack slot
1143 int FI = MFI->CreateFixedObject(4, offset, true);
1144 if (i == (int)FirstVAReg) {
1145 XFI->setVarArgsFrameIndex(FI);
1147 offset -= StackSlotSize;
1148 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1149 // Move argument from phys reg -> virt reg
1150 unsigned VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
1151 RegInfo.addLiveIn(ArgRegs[i], VReg);
1152 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
1153 // Move argument from virt reg -> stack
1154 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1155 MachinePointerInfo(), false, false, 0);
1156 MemOps.push_back(Store);
1158 if (!MemOps.empty())
1159 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1160 &MemOps[0], MemOps.size());
1162 // This will point to the next argument passed via stack.
1163 XFI->setVarArgsFrameIndex(
1164 MFI->CreateFixedObject(4, LRSaveSize + CCInfo.getNextStackOffset(),
1172 //===----------------------------------------------------------------------===//
1173 // Return Value Calling Convention Implementation
1174 //===----------------------------------------------------------------------===//
1176 bool XCoreTargetLowering::
1177 CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
1179 const SmallVectorImpl<ISD::OutputArg> &Outs,
1180 LLVMContext &Context) const {
1181 SmallVector<CCValAssign, 16> RVLocs;
1182 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1183 return CCInfo.CheckReturn(Outs, RetCC_XCore);
1187 XCoreTargetLowering::LowerReturn(SDValue Chain,
1188 CallingConv::ID CallConv, bool isVarArg,
1189 const SmallVectorImpl<ISD::OutputArg> &Outs,
1190 const SmallVectorImpl<SDValue> &OutVals,
1191 SDLoc dl, SelectionDAG &DAG) const {
1193 // CCValAssign - represent the assignment of
1194 // the return value to a location
1195 SmallVector<CCValAssign, 16> RVLocs;
1197 // CCState - Info about the registers and stack slot.
1198 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1199 getTargetMachine(), RVLocs, *DAG.getContext());
1201 // Analyze return values.
1202 CCInfo.AnalyzeReturn(Outs, RetCC_XCore);
1205 SmallVector<SDValue, 4> RetOps(1, Chain);
1207 // Return on XCore is always a "retsp 0"
1208 RetOps.push_back(DAG.getConstant(0, MVT::i32));
1210 // Copy the result values into the output registers.
1211 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1212 CCValAssign &VA = RVLocs[i];
1213 assert(VA.isRegLoc() && "Can only return in registers!");
1215 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1218 // guarantee that all emitted copies are
1219 // stuck together, avoiding something bad
1220 Flag = Chain.getValue(1);
1221 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1224 RetOps[0] = Chain; // Update chain.
1226 // Add the flag if we have it.
1228 RetOps.push_back(Flag);
1230 return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other,
1231 &RetOps[0], RetOps.size());
1234 //===----------------------------------------------------------------------===//
1235 // Other Lowering Code
1236 //===----------------------------------------------------------------------===//
1239 XCoreTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1240 MachineBasicBlock *BB) const {
1241 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1242 DebugLoc dl = MI->getDebugLoc();
1243 assert((MI->getOpcode() == XCore::SELECT_CC) &&
1244 "Unexpected instr type to insert");
1246 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1247 // control-flow pattern. The incoming instruction knows the destination vreg
1248 // to set, the condition code register to branch on, the true/false values to
1249 // select between, and a branch opcode to use.
1250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1251 MachineFunction::iterator It = BB;
1257 // cmpTY ccX, r1, r2
1259 // fallthrough --> copy0MBB
1260 MachineBasicBlock *thisMBB = BB;
1261 MachineFunction *F = BB->getParent();
1262 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1263 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1264 F->insert(It, copy0MBB);
1265 F->insert(It, sinkMBB);
1267 // Transfer the remainder of BB and its successor edges to sinkMBB.
1268 sinkMBB->splice(sinkMBB->begin(), BB,
1269 llvm::next(MachineBasicBlock::iterator(MI)),
1271 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1273 // Next, add the true and fallthrough blocks as its successors.
1274 BB->addSuccessor(copy0MBB);
1275 BB->addSuccessor(sinkMBB);
1277 BuildMI(BB, dl, TII.get(XCore::BRFT_lru6))
1278 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1281 // %FalseValue = ...
1282 // # fallthrough to sinkMBB
1285 // Update machine-CFG edges
1286 BB->addSuccessor(sinkMBB);
1289 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1292 BuildMI(*BB, BB->begin(), dl,
1293 TII.get(XCore::PHI), MI->getOperand(0).getReg())
1294 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1295 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1297 MI->eraseFromParent(); // The pseudo instruction is gone now.
1301 //===----------------------------------------------------------------------===//
1302 // Target Optimization Hooks
1303 //===----------------------------------------------------------------------===//
1305 SDValue XCoreTargetLowering::PerformDAGCombine(SDNode *N,
1306 DAGCombinerInfo &DCI) const {
1307 SelectionDAG &DAG = DCI.DAG;
1309 switch (N->getOpcode()) {
1311 case XCoreISD::LADD: {
1312 SDValue N0 = N->getOperand(0);
1313 SDValue N1 = N->getOperand(1);
1314 SDValue N2 = N->getOperand(2);
1315 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1316 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1317 EVT VT = N0.getValueType();
1319 // canonicalize constant to RHS
1321 return DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N1, N0, N2);
1323 // fold (ladd 0, 0, x) -> 0, x & 1
1324 if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
1325 SDValue Carry = DAG.getConstant(0, VT);
1326 SDValue Result = DAG.getNode(ISD::AND, dl, VT, N2,
1327 DAG.getConstant(1, VT));
1328 SDValue Ops[] = { Result, Carry };
1329 return DAG.getMergeValues(Ops, 2, dl);
1332 // fold (ladd x, 0, y) -> 0, add x, y iff carry is unused and y has only the
1334 if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
1335 APInt KnownZero, KnownOne;
1336 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1337 VT.getSizeInBits() - 1);
1338 DAG.ComputeMaskedBits(N2, KnownZero, KnownOne);
1339 if ((KnownZero & Mask) == Mask) {
1340 SDValue Carry = DAG.getConstant(0, VT);
1341 SDValue Result = DAG.getNode(ISD::ADD, dl, VT, N0, N2);
1342 SDValue Ops[] = { Result, Carry };
1343 return DAG.getMergeValues(Ops, 2, dl);
1348 case XCoreISD::LSUB: {
1349 SDValue N0 = N->getOperand(0);
1350 SDValue N1 = N->getOperand(1);
1351 SDValue N2 = N->getOperand(2);
1352 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1353 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1354 EVT VT = N0.getValueType();
1356 // fold (lsub 0, 0, x) -> x, -x iff x has only the low bit set
1357 if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
1358 APInt KnownZero, KnownOne;
1359 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1360 VT.getSizeInBits() - 1);
1361 DAG.ComputeMaskedBits(N2, KnownZero, KnownOne);
1362 if ((KnownZero & Mask) == Mask) {
1363 SDValue Borrow = N2;
1364 SDValue Result = DAG.getNode(ISD::SUB, dl, VT,
1365 DAG.getConstant(0, VT), N2);
1366 SDValue Ops[] = { Result, Borrow };
1367 return DAG.getMergeValues(Ops, 2, dl);
1371 // fold (lsub x, 0, y) -> 0, sub x, y iff borrow is unused and y has only the
1373 if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
1374 APInt KnownZero, KnownOne;
1375 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
1376 VT.getSizeInBits() - 1);
1377 DAG.ComputeMaskedBits(N2, KnownZero, KnownOne);
1378 if ((KnownZero & Mask) == Mask) {
1379 SDValue Borrow = DAG.getConstant(0, VT);
1380 SDValue Result = DAG.getNode(ISD::SUB, dl, VT, N0, N2);
1381 SDValue Ops[] = { Result, Borrow };
1382 return DAG.getMergeValues(Ops, 2, dl);
1387 case XCoreISD::LMUL: {
1388 SDValue N0 = N->getOperand(0);
1389 SDValue N1 = N->getOperand(1);
1390 SDValue N2 = N->getOperand(2);
1391 SDValue N3 = N->getOperand(3);
1392 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1394 EVT VT = N0.getValueType();
1395 // Canonicalize multiplicative constant to RHS. If both multiplicative
1396 // operands are constant canonicalize smallest to RHS.
1397 if ((N0C && !N1C) ||
1398 (N0C && N1C && N0C->getZExtValue() < N1C->getZExtValue()))
1399 return DAG.getNode(XCoreISD::LMUL, dl, DAG.getVTList(VT, VT),
1403 if (N1C && N1C->isNullValue()) {
1404 // If the high result is unused fold to add(a, b)
1405 if (N->hasNUsesOfValue(0, 0)) {
1406 SDValue Lo = DAG.getNode(ISD::ADD, dl, VT, N2, N3);
1407 SDValue Ops[] = { Lo, Lo };
1408 return DAG.getMergeValues(Ops, 2, dl);
1410 // Otherwise fold to ladd(a, b, 0)
1412 DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N2, N3, N1);
1413 SDValue Carry(Result.getNode(), 1);
1414 SDValue Ops[] = { Carry, Result };
1415 return DAG.getMergeValues(Ops, 2, dl);
1420 // Fold 32 bit expressions such as add(add(mul(x,y),a),b) ->
1421 // lmul(x, y, a, b). The high result of lmul will be ignored.
1422 // This is only profitable if the intermediate results are unused
1424 SDValue Mul0, Mul1, Addend0, Addend1;
1425 if (N->getValueType(0) == MVT::i32 &&
1426 isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, true)) {
1427 SDValue Ignored = DAG.getNode(XCoreISD::LMUL, dl,
1428 DAG.getVTList(MVT::i32, MVT::i32), Mul0,
1429 Mul1, Addend0, Addend1);
1430 SDValue Result(Ignored.getNode(), 1);
1433 APInt HighMask = APInt::getHighBitsSet(64, 32);
1434 // Fold 64 bit expression such as add(add(mul(x,y),a),b) ->
1435 // lmul(x, y, a, b) if all operands are zero-extended. We do this
1436 // before type legalization as it is messy to match the operands after
1438 if (N->getValueType(0) == MVT::i64 &&
1439 isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, false) &&
1440 DAG.MaskedValueIsZero(Mul0, HighMask) &&
1441 DAG.MaskedValueIsZero(Mul1, HighMask) &&
1442 DAG.MaskedValueIsZero(Addend0, HighMask) &&
1443 DAG.MaskedValueIsZero(Addend1, HighMask)) {
1444 SDValue Mul0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1445 Mul0, DAG.getConstant(0, MVT::i32));
1446 SDValue Mul1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1447 Mul1, DAG.getConstant(0, MVT::i32));
1448 SDValue Addend0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1449 Addend0, DAG.getConstant(0, MVT::i32));
1450 SDValue Addend1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
1451 Addend1, DAG.getConstant(0, MVT::i32));
1452 SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
1453 DAG.getVTList(MVT::i32, MVT::i32), Mul0L, Mul1L,
1454 Addend0L, Addend1L);
1455 SDValue Lo(Hi.getNode(), 1);
1456 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
1461 // Replace unaligned store of unaligned load with memmove.
1462 StoreSDNode *ST = cast<StoreSDNode>(N);
1463 if (!DCI.isBeforeLegalize() ||
1464 allowsUnalignedMemoryAccesses(ST->getMemoryVT()) ||
1465 ST->isVolatile() || ST->isIndexed()) {
1468 SDValue Chain = ST->getChain();
1470 unsigned StoreBits = ST->getMemoryVT().getStoreSizeInBits();
1471 if (StoreBits % 8) {
1474 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(
1475 ST->getMemoryVT().getTypeForEVT(*DCI.DAG.getContext()));
1476 unsigned Alignment = ST->getAlignment();
1477 if (Alignment >= ABIAlignment) {
1481 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(ST->getValue())) {
1482 if (LD->hasNUsesOfValue(1, 0) && ST->getMemoryVT() == LD->getMemoryVT() &&
1483 LD->getAlignment() == Alignment &&
1484 !LD->isVolatile() && !LD->isIndexed() &&
1485 Chain.reachesChainWithoutSideEffects(SDValue(LD, 1))) {
1486 return DAG.getMemmove(Chain, dl, ST->getBasePtr(),
1488 DAG.getConstant(StoreBits/8, MVT::i32),
1489 Alignment, false, ST->getPointerInfo(),
1490 LD->getPointerInfo());
1499 void XCoreTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1502 const SelectionDAG &DAG,
1503 unsigned Depth) const {
1504 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
1505 switch (Op.getOpcode()) {
1507 case XCoreISD::LADD:
1508 case XCoreISD::LSUB:
1509 if (Op.getResNo() == 1) {
1510 // Top bits of carry / borrow are clear.
1511 KnownZero = APInt::getHighBitsSet(KnownZero.getBitWidth(),
1512 KnownZero.getBitWidth() - 1);
1518 //===----------------------------------------------------------------------===//
1519 // Addressing mode description hooks
1520 //===----------------------------------------------------------------------===//
1522 static inline bool isImmUs(int64_t val)
1524 return (val >= 0 && val <= 11);
1527 static inline bool isImmUs2(int64_t val)
1529 return (val%2 == 0 && isImmUs(val/2));
1532 static inline bool isImmUs4(int64_t val)
1534 return (val%4 == 0 && isImmUs(val/4));
1537 /// isLegalAddressingMode - Return true if the addressing mode represented
1538 /// by AM is legal for this target, for a load/store of the specified type.
1540 XCoreTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1542 if (Ty->getTypeID() == Type::VoidTyID)
1543 return AM.Scale == 0 && isImmUs(AM.BaseOffs) && isImmUs4(AM.BaseOffs);
1545 const DataLayout *TD = TM.getDataLayout();
1546 unsigned Size = TD->getTypeAllocSize(Ty);
1548 return Size >= 4 && !AM.HasBaseReg && AM.Scale == 0 &&
1555 if (AM.Scale == 0) {
1556 return isImmUs(AM.BaseOffs);
1559 return AM.Scale == 1 && AM.BaseOffs == 0;
1563 if (AM.Scale == 0) {
1564 return isImmUs2(AM.BaseOffs);
1567 return AM.Scale == 2 && AM.BaseOffs == 0;
1570 if (AM.Scale == 0) {
1571 return isImmUs4(AM.BaseOffs);
1574 return AM.Scale == 4 && AM.BaseOffs == 0;
1578 //===----------------------------------------------------------------------===//
1579 // XCore Inline Assembly Support
1580 //===----------------------------------------------------------------------===//
1582 std::pair<unsigned, const TargetRegisterClass*>
1583 XCoreTargetLowering::
1584 getRegForInlineAsmConstraint(const std::string &Constraint,
1586 if (Constraint.size() == 1) {
1587 switch (Constraint[0]) {
1590 return std::make_pair(0U, &XCore::GRRegsRegClass);
1593 // Use the default implementation in TargetLowering to convert the register
1594 // constraint into a member of a register class.
1595 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);