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"
17 #include "XCoreMachineFunctionInfo.h"
19 #include "XCoreTargetMachine.h"
20 #include "XCoreSubtarget.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/CallingConv.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/GlobalAlias.h"
27 #include "llvm/CodeGen/CallingConvLower.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/CodeGen/ValueTypes.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/ADT/VectorExtras.h"
40 const char *XCoreTargetLowering::
41 getTargetNodeName(unsigned Opcode) const
45 case XCoreISD::BL : return "XCoreISD::BL";
46 case XCoreISD::PCRelativeWrapper : return "XCoreISD::PCRelativeWrapper";
47 case XCoreISD::DPRelativeWrapper : return "XCoreISD::DPRelativeWrapper";
48 case XCoreISD::CPRelativeWrapper : return "XCoreISD::CPRelativeWrapper";
49 case XCoreISD::STWSP : return "XCoreISD::STWSP";
50 case XCoreISD::RETSP : return "XCoreISD::RETSP";
51 default : return NULL;
55 XCoreTargetLowering::XCoreTargetLowering(XCoreTargetMachine &XTM)
56 : TargetLowering(XTM),
58 Subtarget(*XTM.getSubtargetImpl()) {
60 // Set up the register classes.
61 addRegisterClass(MVT::i32, XCore::GRRegsRegisterClass);
63 // Compute derived properties from the register classes
64 computeRegisterProperties();
66 // Division is expensive
67 setIntDivIsCheap(false);
69 setShiftAmountType(MVT::i32);
71 setShiftAmountFlavor(Extend);
72 setStackPointerRegisterToSaveRestore(XCore::SP);
74 setSchedulingPreference(SchedulingForRegPressure);
76 // Use i32 for setcc operations results (slt, sgt, ...).
77 setBooleanContents(ZeroOrOneBooleanContent);
79 // XCore does not have the NodeTypes below.
80 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
81 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
82 setOperationAction(ISD::ADDC, MVT::i32, Expand);
83 setOperationAction(ISD::ADDE, MVT::i32, Expand);
84 setOperationAction(ISD::SUBC, MVT::i32, Expand);
85 setOperationAction(ISD::SUBE, MVT::i32, Expand);
87 // Stop the combiner recombining select and set_cc
88 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
91 if (!Subtarget.isXS1A()) {
92 setOperationAction(ISD::ADD, MVT::i64, Custom);
93 setOperationAction(ISD::SUB, MVT::i64, Custom);
95 if (Subtarget.isXS1A()) {
96 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
98 setOperationAction(ISD::MULHS, MVT::i32, Expand);
99 setOperationAction(ISD::MULHU, MVT::i32, Expand);
100 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
101 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
102 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
105 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
106 setOperationAction(ISD::ROTL , MVT::i32, Expand);
107 setOperationAction(ISD::ROTR , MVT::i32, Expand);
109 setOperationAction(ISD::TRAP, MVT::Other, Legal);
111 // Expand jump tables for now
112 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
113 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
115 // RET must be custom lowered, to meet ABI requirements
116 setOperationAction(ISD::RET, MVT::Other, Custom);
118 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
120 // Thread Local Storage
121 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
123 // Conversion of i64 -> double produces constantpool nodes
124 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
127 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
128 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
129 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
131 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
132 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
135 setOperationAction(ISD::VAEND, MVT::Other, Expand);
136 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
137 setOperationAction(ISD::VAARG, MVT::Other, Custom);
138 setOperationAction(ISD::VASTART, MVT::Other, Custom);
141 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
142 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
143 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
146 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
147 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
150 SDValue XCoreTargetLowering::
151 LowerOperation(SDValue Op, SelectionDAG &DAG) {
152 switch (Op.getOpcode())
154 case ISD::CALL: return LowerCALL(Op, DAG);
155 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
156 case ISD::RET: return LowerRET(Op, DAG);
157 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
158 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
159 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
160 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
161 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
162 case ISD::VAARG: return LowerVAARG(Op, DAG);
163 case ISD::VASTART: return LowerVASTART(Op, DAG);
164 // FIXME: Remove these when LegalizeDAGTypes lands.
166 case ISD::SUB: return ExpandADDSUB(Op.getNode(), DAG);
167 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
169 assert(0 && "unimplemented operand");
174 /// ReplaceNodeResults - Replace the results of node with an illegal result
175 /// type with new values built out of custom code.
176 void XCoreTargetLowering::ReplaceNodeResults(SDNode *N,
177 SmallVectorImpl<SDValue>&Results,
179 switch (N->getOpcode()) {
181 assert(0 && "Don't know how to custom expand this!");
185 Results.push_back(ExpandADDSUB(N, DAG));
190 //===----------------------------------------------------------------------===//
191 // Misc Lower Operation implementation
192 //===----------------------------------------------------------------------===//
194 SDValue XCoreTargetLowering::
195 LowerSELECT_CC(SDValue Op, SelectionDAG &DAG)
197 SDValue Cond = DAG.getNode(ISD::SETCC, MVT::i32, Op.getOperand(2),
198 Op.getOperand(3), Op.getOperand(4));
199 return DAG.getNode(ISD::SELECT, MVT::i32, Cond, Op.getOperand(0),
203 SDValue XCoreTargetLowering::
204 getGlobalAddressWrapper(SDValue GA, GlobalValue *GV, SelectionDAG &DAG)
206 if (isa<Function>(GV)) {
207 return DAG.getNode(XCoreISD::PCRelativeWrapper, MVT::i32, GA);
208 } else if (!Subtarget.isXS1A()) {
209 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
211 // If GV is an alias then use the aliasee to determine constness
212 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
213 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
215 bool isConst = GVar && GVar->isConstant();
217 return DAG.getNode(XCoreISD::CPRelativeWrapper, MVT::i32, GA);
220 return DAG.getNode(XCoreISD::DPRelativeWrapper, MVT::i32, GA);
223 SDValue XCoreTargetLowering::
224 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
226 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
227 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
228 // If it's a debug information descriptor, don't mess with it.
229 if (DAG.isVerifiedDebugInfoDesc(Op))
231 return getGlobalAddressWrapper(GA, GV, DAG);
234 static inline SDValue BuildGetId(SelectionDAG &DAG) {
235 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, MVT::i32,
236 DAG.getConstant(Intrinsic::xcore_getid, MVT::i32));
239 static inline bool isZeroLengthArray(const Type *Ty) {
240 const ArrayType *AT = dyn_cast_or_null<ArrayType>(Ty);
241 return AT && (AT->getNumElements() == 0);
244 SDValue XCoreTargetLowering::
245 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
247 // transform to label + getid() * size
248 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
249 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
250 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
252 // If GV is an alias then use the aliasee to determine size
253 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
254 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
257 assert(0 && "Thread local object not a GlobalVariable?");
260 const Type *Ty = cast<PointerType>(GV->getType())->getElementType();
261 if (!Ty->isSized() || isZeroLengthArray(Ty)) {
262 cerr << "Size of thread local object " << GVar->getName()
266 SDValue base = getGlobalAddressWrapper(GA, GV, DAG);
267 const TargetData *TD = TM.getTargetData();
268 unsigned Size = TD->getTypePaddedSize(Ty);
269 SDValue offset = DAG.getNode(ISD::MUL, MVT::i32, BuildGetId(DAG),
270 DAG.getConstant(Size, MVT::i32));
271 return DAG.getNode(ISD::ADD, MVT::i32, base, offset);
274 SDValue XCoreTargetLowering::
275 LowerConstantPool(SDValue Op, SelectionDAG &DAG)
277 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
278 if (Subtarget.isXS1A()) {
279 assert(0 && "Lowering of constant pool unimplemented");
282 MVT PtrVT = Op.getValueType();
284 if (CP->isMachineConstantPoolEntry()) {
285 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
288 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
291 return DAG.getNode(XCoreISD::CPRelativeWrapper, MVT::i32, Res);
295 SDValue XCoreTargetLowering::
296 LowerJumpTable(SDValue Op, SelectionDAG &DAG)
298 MVT PtrVT = Op.getValueType();
299 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
300 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
301 return DAG.getNode(XCoreISD::DPRelativeWrapper, MVT::i32, JTI);
304 SDValue XCoreTargetLowering::
305 ExpandADDSUB(SDNode *N, SelectionDAG &DAG)
307 assert(N->getValueType(0) == MVT::i64 &&
308 (N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&
309 "Unknown operand to lower!");
310 assert(!Subtarget.isXS1A() && "Cannot custom lower ADD/SUB on xs1a");
311 DebugLoc dl = N->getDebugLoc();
313 // Extract components
314 SDValue LHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
315 N->getOperand(0), DAG.getConstant(0, MVT::i32));
316 SDValue LHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
317 N->getOperand(0), DAG.getConstant(1, MVT::i32));
318 SDValue RHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
319 N->getOperand(1), DAG.getConstant(0, MVT::i32));
320 SDValue RHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
321 N->getOperand(1), DAG.getConstant(1, MVT::i32));
324 unsigned Opcode = (N->getOpcode() == ISD::ADD) ? XCoreISD::LADD :
326 SDValue Zero = DAG.getConstant(0, MVT::i32);
327 SDValue Carry = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
329 SDValue Lo(Carry.getNode(), 1);
331 SDValue Ignored = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
333 SDValue Hi(Ignored.getNode(), 1);
335 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
338 SDValue XCoreTargetLowering::
339 LowerVAARG(SDValue Op, SelectionDAG &DAG)
341 assert(0 && "unimplemented");
342 // FIX Arguments passed by reference need a extra dereference.
343 SDNode *Node = Op.getNode();
344 DebugLoc dl = Node->getDebugLoc();
345 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
346 MVT VT = Node->getValueType(0);
347 SDValue VAList = DAG.getLoad(getPointerTy(), dl, Node->getOperand(0),
348 Node->getOperand(1), V, 0);
349 // Increment the pointer, VAList, to the next vararg
350 SDValue Tmp3 = DAG.getNode(ISD::ADD, dl, getPointerTy(), VAList,
351 DAG.getConstant(VT.getSizeInBits(),
353 // Store the incremented VAList to the legalized pointer
354 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Node->getOperand(1), V, 0);
355 // Load the actual argument out of the pointer VAList
356 return DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
359 SDValue XCoreTargetLowering::
360 LowerVASTART(SDValue Op, SelectionDAG &DAG)
362 DebugLoc dl = Op.getDebugLoc();
363 // vastart stores the address of the VarArgsFrameIndex slot into the
364 // memory location argument
365 MachineFunction &MF = DAG.getMachineFunction();
366 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
367 SDValue Addr = DAG.getFrameIndex(XFI->getVarArgsFrameIndex(), MVT::i32);
368 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
369 return DAG.getStore(Op.getOperand(0), dl, Addr, Op.getOperand(1), SV, 0);
372 SDValue XCoreTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
373 DebugLoc dl = Op.getDebugLoc();
374 // Depths > 0 not supported yet!
375 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
378 MachineFunction &MF = DAG.getMachineFunction();
379 const TargetRegisterInfo *RegInfo = getTargetMachine().getRegisterInfo();
380 return DAG.getCopyFromReg(DAG.getEntryNode(), dl,
381 RegInfo->getFrameRegister(MF), MVT::i32);
384 //===----------------------------------------------------------------------===//
385 // Calling Convention Implementation
387 // The lower operations present on calling convention works on this order:
388 // LowerCALL (virt regs --> phys regs, virt regs --> stack)
389 // LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
390 // LowerRET (virt regs --> phys regs)
391 // LowerCALL (phys regs --> virt regs)
393 //===----------------------------------------------------------------------===//
395 #include "XCoreGenCallingConv.inc"
397 //===----------------------------------------------------------------------===//
398 // CALL Calling Convention Implementation
399 //===----------------------------------------------------------------------===//
401 /// XCore custom CALL implementation
402 SDValue XCoreTargetLowering::
403 LowerCALL(SDValue Op, SelectionDAG &DAG)
405 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
406 unsigned CallingConv = TheCall->getCallingConv();
407 // For now, only CallingConv::C implemented
411 assert(0 && "Unsupported calling convention");
412 case CallingConv::Fast:
414 return LowerCCCCallTo(Op, DAG, CallingConv);
418 /// LowerCCCCallTo - functions arguments are copied from virtual
419 /// regs to (physical regs)/(stack frame), CALLSEQ_START and
420 /// CALLSEQ_END are emitted.
421 /// TODO: isTailCall, sret.
422 SDValue XCoreTargetLowering::
423 LowerCCCCallTo(SDValue Op, SelectionDAG &DAG, unsigned CC)
425 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
426 SDValue Chain = TheCall->getChain();
427 SDValue Callee = TheCall->getCallee();
428 bool isVarArg = TheCall->isVarArg();
429 DebugLoc dl = Op.getDebugLoc();
431 // Analyze operands of the call, assigning locations to each operand.
432 SmallVector<CCValAssign, 16> ArgLocs;
433 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
435 // The ABI dictates there should be one stack slot available to the callee
436 // on function entry (for saving lr).
437 CCInfo.AllocateStack(4, 4);
439 CCInfo.AnalyzeCallOperands(TheCall, CC_XCore);
441 // Get a count of how many bytes are to be pushed on the stack.
442 unsigned NumBytes = CCInfo.getNextStackOffset();
444 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
445 getPointerTy(), true));
447 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
448 SmallVector<SDValue, 12> MemOpChains;
450 // Walk the register/memloc assignments, inserting copies/loads.
451 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
452 CCValAssign &VA = ArgLocs[i];
454 // Arguments start after the 5 first operands of ISD::CALL
455 SDValue Arg = TheCall->getArg(i);
457 // Promote the value if needed.
458 switch (VA.getLocInfo()) {
459 default: assert(0 && "Unknown loc info!");
460 case CCValAssign::Full: break;
461 case CCValAssign::SExt:
462 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
464 case CCValAssign::ZExt:
465 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
467 case CCValAssign::AExt:
468 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
472 // Arguments that can be passed on register must be kept at
475 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
477 assert(VA.isMemLoc());
479 int Offset = VA.getLocMemOffset();
481 MemOpChains.push_back(DAG.getNode(XCoreISD::STWSP, dl, MVT::Other,
483 DAG.getConstant(Offset/4, MVT::i32)));
487 // Transform all store nodes into one single node because
488 // all store nodes are independent of each other.
489 if (!MemOpChains.empty())
490 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
491 &MemOpChains[0], MemOpChains.size());
493 // Build a sequence of copy-to-reg nodes chained together with token
494 // chain and flag operands which copy the outgoing args into registers.
495 // The InFlag in necessary since all emited instructions must be
498 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
499 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
500 RegsToPass[i].second, InFlag);
501 InFlag = Chain.getValue(1);
504 // If the callee is a GlobalAddress node (quite common, every direct call is)
505 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
506 // Likewise ExternalSymbol -> TargetExternalSymbol.
507 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
508 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
509 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
510 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
512 // XCoreBranchLink = #chain, #target_address, #opt_in_flags...
513 // = Chain, Callee, Reg#1, Reg#2, ...
515 // Returns a chain & a flag for retval copy to use.
516 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
517 SmallVector<SDValue, 8> Ops;
518 Ops.push_back(Chain);
519 Ops.push_back(Callee);
521 // Add argument registers to the end of the list so that they are
522 // known live into the call.
523 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
524 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
525 RegsToPass[i].second.getValueType()));
527 if (InFlag.getNode())
528 Ops.push_back(InFlag);
530 Chain = DAG.getNode(XCoreISD::BL, dl, NodeTys, &Ops[0], Ops.size());
531 InFlag = Chain.getValue(1);
533 // Create the CALLSEQ_END node.
534 Chain = DAG.getCALLSEQ_END(Chain,
535 DAG.getConstant(NumBytes, getPointerTy(), true),
536 DAG.getConstant(0, getPointerTy(), true),
538 InFlag = Chain.getValue(1);
540 // Handle result values, copying them out of physregs into vregs that we
542 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
546 /// LowerCallResult - Lower the result values of an ISD::CALL into the
547 /// appropriate copies out of appropriate physical registers. This assumes that
548 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
549 /// being lowered. Returns a SDNode with the same number of values as the
551 SDNode *XCoreTargetLowering::
552 LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
553 unsigned CallingConv, SelectionDAG &DAG) {
554 bool isVarArg = TheCall->isVarArg();
555 DebugLoc dl = TheCall->getDebugLoc();
557 // Assign locations to each value returned by this call.
558 SmallVector<CCValAssign, 16> RVLocs;
559 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
561 CCInfo.AnalyzeCallResult(TheCall, RetCC_XCore);
562 SmallVector<SDValue, 8> ResultVals;
564 // Copy all of the result registers out of their specified physreg.
565 for (unsigned i = 0; i != RVLocs.size(); ++i) {
566 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
567 RVLocs[i].getValVT(), InFlag).getValue(1);
568 InFlag = Chain.getValue(2);
569 ResultVals.push_back(Chain.getValue(0));
572 ResultVals.push_back(Chain);
574 // Merge everything together with a MERGE_VALUES node.
575 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
576 &ResultVals[0], ResultVals.size()).getNode();
579 //===----------------------------------------------------------------------===//
580 // FORMAL_ARGUMENTS Calling Convention Implementation
581 //===----------------------------------------------------------------------===//
583 /// XCore custom FORMAL_ARGUMENTS implementation
584 SDValue XCoreTargetLowering::
585 LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG)
587 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
591 assert(0 && "Unsupported calling convention");
593 case CallingConv::Fast:
594 return LowerCCCArguments(Op, DAG);
598 /// LowerCCCArguments - transform physical registers into
599 /// virtual registers and generate load operations for
600 /// arguments places on the stack.
602 SDValue XCoreTargetLowering::
603 LowerCCCArguments(SDValue Op, SelectionDAG &DAG)
605 MachineFunction &MF = DAG.getMachineFunction();
606 MachineFrameInfo *MFI = MF.getFrameInfo();
607 MachineRegisterInfo &RegInfo = MF.getRegInfo();
608 SDValue Root = Op.getOperand(0);
609 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
610 unsigned CC = MF.getFunction()->getCallingConv();
611 DebugLoc dl = Op.getDebugLoc();
613 // Assign locations to all of the incoming arguments.
614 SmallVector<CCValAssign, 16> ArgLocs;
615 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
617 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_XCore);
619 unsigned StackSlotSize = XCoreFrameInfo::stackSlotSize();
621 SmallVector<SDValue, 16> ArgValues;
623 unsigned LRSaveSize = StackSlotSize;
625 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
627 CCValAssign &VA = ArgLocs[i];
630 // Arguments passed in registers
631 MVT RegVT = VA.getLocVT();
632 switch (RegVT.getSimpleVT()) {
634 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
635 << RegVT.getSimpleVT()
639 unsigned VReg = RegInfo.createVirtualRegister(
640 XCore::GRRegsRegisterClass);
641 RegInfo.addLiveIn(VA.getLocReg(), VReg);
642 ArgValues.push_back(DAG.getCopyFromReg(Root, dl, VReg, RegVT));
646 assert(VA.isMemLoc());
647 // Load the argument to a virtual register
648 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
649 if (ObjSize > StackSlotSize) {
650 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
651 << VA.getLocVT().getSimpleVT()
654 // Create the frame index object for this incoming parameter...
655 int FI = MFI->CreateFixedObject(ObjSize,
656 LRSaveSize + VA.getLocMemOffset());
658 // Create the SelectionDAG nodes corresponding to a load
659 //from this parameter
660 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
661 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN, NULL, 0));
666 /* Argument registers */
667 static const unsigned ArgRegs[] = {
668 XCore::R0, XCore::R1, XCore::R2, XCore::R3
670 XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
671 unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs,
672 array_lengthof(ArgRegs));
673 if (FirstVAReg < array_lengthof(ArgRegs)) {
674 SmallVector<SDValue, 4> MemOps;
676 // Save remaining registers, storing higher register numbers at a higher
678 for (unsigned i = array_lengthof(ArgRegs) - 1; i >= FirstVAReg; --i) {
679 // Create a stack slot
680 int FI = MFI->CreateFixedObject(4, offset);
681 if (i == FirstVAReg) {
682 XFI->setVarArgsFrameIndex(FI);
684 offset -= StackSlotSize;
685 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
686 // Move argument from phys reg -> virt reg
687 unsigned VReg = RegInfo.createVirtualRegister(
688 XCore::GRRegsRegisterClass);
689 RegInfo.addLiveIn(ArgRegs[i], VReg);
690 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
691 // Move argument from virt reg -> stack
692 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
693 MemOps.push_back(Store);
696 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
697 &MemOps[0], MemOps.size());
699 // This will point to the next argument passed via stack.
700 XFI->setVarArgsFrameIndex(
701 MFI->CreateFixedObject(4, LRSaveSize + CCInfo.getNextStackOffset()));
705 ArgValues.push_back(Root);
707 // Return the new list of results.
708 std::vector<MVT> RetVT(Op.getNode()->value_begin(),
709 Op.getNode()->value_end());
710 return DAG.getNode(ISD::MERGE_VALUES, dl, RetVT,
711 &ArgValues[0], ArgValues.size());
714 //===----------------------------------------------------------------------===//
715 // Return Value Calling Convention Implementation
716 //===----------------------------------------------------------------------===//
718 SDValue XCoreTargetLowering::
719 LowerRET(SDValue Op, SelectionDAG &DAG)
721 // CCValAssign - represent the assignment of
722 // the return value to a location
723 SmallVector<CCValAssign, 16> RVLocs;
724 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
725 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
726 DebugLoc dl = Op.getDebugLoc();
728 // CCState - Info about the registers and stack slot.
729 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
731 // Analize return values of ISD::RET
732 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_XCore);
734 // If this is the first return lowered for this function, add
735 // the regs to the liveout set for the function.
736 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
737 for (unsigned i = 0; i != RVLocs.size(); ++i)
738 if (RVLocs[i].isRegLoc())
739 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
742 // The chain is always operand #0
743 SDValue Chain = Op.getOperand(0);
746 // Copy the result values into the output registers.
747 for (unsigned i = 0; i != RVLocs.size(); ++i) {
748 CCValAssign &VA = RVLocs[i];
749 assert(VA.isRegLoc() && "Can only return in registers!");
751 // ISD::RET => ret chain, (regnum1,val1), ...
752 // So i*2+1 index only the regnums
753 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
754 Op.getOperand(i*2+1), Flag);
756 // guarantee that all emitted copies are
757 // stuck together, avoiding something bad
758 Flag = Chain.getValue(1);
761 // Return on XCore is always a "retsp 0"
763 return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other,
764 Chain, DAG.getConstant(0, MVT::i32), Flag);
766 return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other,
767 Chain, DAG.getConstant(0, MVT::i32));
770 //===----------------------------------------------------------------------===//
771 // Other Lowering Code
772 //===----------------------------------------------------------------------===//
775 XCoreTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
776 MachineBasicBlock *BB) {
777 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
778 assert((MI->getOpcode() == XCore::SELECT_CC) &&
779 "Unexpected instr type to insert");
781 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
782 // control-flow pattern. The incoming instruction knows the destination vreg
783 // to set, the condition code register to branch on, the true/false values to
784 // select between, and a branch opcode to use.
785 const BasicBlock *LLVM_BB = BB->getBasicBlock();
786 MachineFunction::iterator It = BB;
794 // fallthrough --> copy0MBB
795 MachineBasicBlock *thisMBB = BB;
796 MachineFunction *F = BB->getParent();
797 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
798 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
799 BuildMI(BB, TII.get(XCore::BRFT_lru6))
800 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
801 F->insert(It, copy0MBB);
802 F->insert(It, sinkMBB);
803 // Update machine-CFG edges by transferring all successors of the current
804 // block to the new block which will contain the Phi node for the select.
805 sinkMBB->transferSuccessors(BB);
806 // Next, add the true and fallthrough blocks as its successors.
807 BB->addSuccessor(copy0MBB);
808 BB->addSuccessor(sinkMBB);
812 // # fallthrough to sinkMBB
815 // Update machine-CFG edges
816 BB->addSuccessor(sinkMBB);
819 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
822 BuildMI(BB, TII.get(XCore::PHI), MI->getOperand(0).getReg())
823 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
824 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
826 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
830 //===----------------------------------------------------------------------===//
831 // Addressing mode description hooks
832 //===----------------------------------------------------------------------===//
834 static inline bool isImmUs(int64_t val)
836 return (val >= 0 && val <= 11);
839 static inline bool isImmUs2(int64_t val)
841 return (val%2 == 0 && isImmUs(val/2));
844 static inline bool isImmUs4(int64_t val)
846 return (val%4 == 0 && isImmUs(val/4));
849 /// isLegalAddressingMode - Return true if the addressing mode represented
850 /// by AM is legal for this target, for a load/store of the specified type.
852 XCoreTargetLowering::isLegalAddressingMode(const AddrMode &AM,
853 const Type *Ty) const {
854 MVT VT = getValueType(Ty, true);
855 // Get expected value type after legalization
856 switch (VT.getSimpleVT()) {
857 // Legal load / stores
866 // Everything else is lowered to words
872 return VT == MVT::i32 && !AM.HasBaseReg && AM.Scale == 0 &&
876 switch (VT.getSimpleVT()) {
882 return isImmUs(AM.BaseOffs);
884 return AM.Scale == 1 && AM.BaseOffs == 0;
888 return isImmUs2(AM.BaseOffs);
890 return AM.Scale == 2 && AM.BaseOffs == 0;
894 return isImmUs4(AM.BaseOffs);
897 return AM.Scale == 4 && AM.BaseOffs == 0;
903 //===----------------------------------------------------------------------===//
904 // XCore Inline Assembly Support
905 //===----------------------------------------------------------------------===//
907 std::vector<unsigned> XCoreTargetLowering::
908 getRegClassForInlineAsmConstraint(const std::string &Constraint,
911 if (Constraint.size() != 1)
912 return std::vector<unsigned>();
914 switch (Constraint[0]) {
917 return make_vector<unsigned>(XCore::R0, XCore::R1, XCore::R2,
918 XCore::R3, XCore::R4, XCore::R5,
919 XCore::R6, XCore::R7, XCore::R8,
920 XCore::R9, XCore::R10, XCore::R11, 0);
923 return std::vector<unsigned>();