1 //===-- HexagonISelLowering.cpp - Hexagon 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 interfaces that Hexagon uses to lower LLVM code
11 // into a selection DAG.
13 //===----------------------------------------------------------------------===//
15 #include "HexagonISelLowering.h"
16 #include "HexagonTargetMachine.h"
17 #include "HexagonMachineFunctionInfo.h"
18 #include "HexagonTargetObjectFile.h"
19 #include "HexagonSubtarget.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/InlineAsm.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/GlobalAlias.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/CallingConv.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/MachineJumpTableInfo.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/SelectionDAGISel.h"
34 #include "llvm/CodeGen/ValueTypes.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
42 const unsigned Hexagon_MAX_RET_SIZE = 64;
45 EmitJumpTables("hexagon-emit-jump-tables", cl::init(true), cl::Hidden,
46 cl::desc("Control jump table emission on Hexagon target"));
48 int NumNamedVarArgParams = -1;
50 // Implement calling convention for Hexagon.
52 CC_Hexagon(unsigned ValNo, MVT ValVT,
53 MVT LocVT, CCValAssign::LocInfo LocInfo,
54 ISD::ArgFlagsTy ArgFlags, CCState &State);
57 CC_Hexagon32(unsigned ValNo, MVT ValVT,
58 MVT LocVT, CCValAssign::LocInfo LocInfo,
59 ISD::ArgFlagsTy ArgFlags, CCState &State);
62 CC_Hexagon64(unsigned ValNo, MVT ValVT,
63 MVT LocVT, CCValAssign::LocInfo LocInfo,
64 ISD::ArgFlagsTy ArgFlags, CCState &State);
67 RetCC_Hexagon(unsigned ValNo, MVT ValVT,
68 MVT LocVT, CCValAssign::LocInfo LocInfo,
69 ISD::ArgFlagsTy ArgFlags, CCState &State);
72 RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
73 MVT LocVT, CCValAssign::LocInfo LocInfo,
74 ISD::ArgFlagsTy ArgFlags, CCState &State);
77 RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
78 MVT LocVT, CCValAssign::LocInfo LocInfo,
79 ISD::ArgFlagsTy ArgFlags, CCState &State);
82 CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
83 MVT LocVT, CCValAssign::LocInfo LocInfo,
84 ISD::ArgFlagsTy ArgFlags, CCState &State) {
86 // NumNamedVarArgParams can not be zero for a VarArg function.
87 assert ( (NumNamedVarArgParams > 0) &&
88 "NumNamedVarArgParams is not bigger than zero.");
90 if ( (int)ValNo < NumNamedVarArgParams ) {
91 // Deal with named arguments.
92 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
95 // Deal with un-named arguments.
97 if (ArgFlags.isByVal()) {
98 // If pass-by-value, the size allocated on stack is decided
99 // by ArgFlags.getByValSize(), not by the size of LocVT.
100 assert ((ArgFlags.getByValSize() > 8) &&
101 "ByValSize must be bigger than 8 bytes");
102 ofst = State.AllocateStack(ArgFlags.getByValSize(), 4);
103 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
106 if (LocVT == MVT::i32) {
107 ofst = State.AllocateStack(4, 4);
108 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
111 if (LocVT == MVT::i64) {
112 ofst = State.AllocateStack(8, 8);
113 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
121 CC_Hexagon (unsigned ValNo, MVT ValVT,
122 MVT LocVT, CCValAssign::LocInfo LocInfo,
123 ISD::ArgFlagsTy ArgFlags, CCState &State) {
125 if (ArgFlags.isByVal()) {
127 assert ((ArgFlags.getByValSize() > 8) &&
128 "ByValSize must be bigger than 8 bytes");
129 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 4);
130 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
134 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
137 if (ArgFlags.isSExt())
138 LocInfo = CCValAssign::SExt;
139 else if (ArgFlags.isZExt())
140 LocInfo = CCValAssign::ZExt;
142 LocInfo = CCValAssign::AExt;
145 if (LocVT == MVT::i32) {
146 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
150 if (LocVT == MVT::i64) {
151 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
155 return true; // CC didn't match.
159 static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
160 MVT LocVT, CCValAssign::LocInfo LocInfo,
161 ISD::ArgFlagsTy ArgFlags, CCState &State) {
163 static const uint16_t RegList[] = {
164 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
167 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
168 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
172 unsigned Offset = State.AllocateStack(4, 4);
173 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
177 static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
178 MVT LocVT, CCValAssign::LocInfo LocInfo,
179 ISD::ArgFlagsTy ArgFlags, CCState &State) {
181 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
182 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
186 static const uint16_t RegList1[] = {
187 Hexagon::D1, Hexagon::D2
189 static const uint16_t RegList2[] = {
190 Hexagon::R1, Hexagon::R3
192 if (unsigned Reg = State.AllocateReg(RegList1, RegList2, 2)) {
193 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
197 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
198 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
202 static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
203 MVT LocVT, CCValAssign::LocInfo LocInfo,
204 ISD::ArgFlagsTy ArgFlags, CCState &State) {
207 if (LocVT == MVT::i1 ||
212 if (ArgFlags.isSExt())
213 LocInfo = CCValAssign::SExt;
214 else if (ArgFlags.isZExt())
215 LocInfo = CCValAssign::ZExt;
217 LocInfo = CCValAssign::AExt;
220 if (LocVT == MVT::i32) {
221 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
225 if (LocVT == MVT::i64) {
226 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
230 return true; // CC didn't match.
233 static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
234 MVT LocVT, CCValAssign::LocInfo LocInfo,
235 ISD::ArgFlagsTy ArgFlags, CCState &State) {
237 if (LocVT == MVT::i32) {
238 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
239 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
244 unsigned Offset = State.AllocateStack(4, 4);
245 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
249 static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
250 MVT LocVT, CCValAssign::LocInfo LocInfo,
251 ISD::ArgFlagsTy ArgFlags, CCState &State) {
252 if (LocVT == MVT::i64) {
253 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
254 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
259 unsigned Offset = State.AllocateStack(8, 8);
260 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
265 HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
270 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
271 /// by "Src" to address "Dst" of size "Size". Alignment information is
272 /// specified by the specific parameter attribute. The copy will be passed as
273 /// a byval function parameter. Sometimes what we are copying is the end of a
274 /// larger object, the part that does not fit in registers.
276 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
277 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
280 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
281 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
282 /*isVolatile=*/false, /*AlwaysInline=*/false,
283 MachinePointerInfo(), MachinePointerInfo());
287 // LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
288 // passed by value, the function prototype is modified to return void and
289 // the value is stored in memory pointed by a pointer passed by caller.
291 HexagonTargetLowering::LowerReturn(SDValue Chain,
292 CallingConv::ID CallConv, bool isVarArg,
293 const SmallVectorImpl<ISD::OutputArg> &Outs,
294 const SmallVectorImpl<SDValue> &OutVals,
295 DebugLoc dl, SelectionDAG &DAG) const {
297 // CCValAssign - represent the assignment of the return value to locations.
298 SmallVector<CCValAssign, 16> RVLocs;
300 // CCState - Info about the registers and stack slot.
301 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
302 getTargetMachine(), RVLocs, *DAG.getContext());
304 // Analyze return values of ISD::RET
305 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
307 // If this is the first return lowered for this function, add the regs to the
308 // liveout set for the function.
309 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
310 for (unsigned i = 0; i != RVLocs.size(); ++i)
311 if (RVLocs[i].isRegLoc())
312 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
316 // Copy the result values into the output registers.
317 for (unsigned i = 0; i != RVLocs.size(); ++i) {
318 CCValAssign &VA = RVLocs[i];
320 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
322 // Guarantee that all emitted copies are stuck together with flags.
323 Flag = Chain.getValue(1);
327 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
329 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, Chain);
335 /// LowerCallResult - Lower the result values of an ISD::CALL into the
336 /// appropriate copies out of appropriate physical registers. This assumes that
337 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
338 /// being lowered. Returns a SDNode with the same number of values as the
341 HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
342 CallingConv::ID CallConv, bool isVarArg,
344 SmallVectorImpl<ISD::InputArg> &Ins,
345 DebugLoc dl, SelectionDAG &DAG,
346 SmallVectorImpl<SDValue> &InVals,
347 const SmallVectorImpl<SDValue> &OutVals,
348 SDValue Callee) const {
350 // Assign locations to each value returned by this call.
351 SmallVector<CCValAssign, 16> RVLocs;
353 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
354 getTargetMachine(), RVLocs, *DAG.getContext());
356 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
358 // Copy all of the result registers out of their specified physreg.
359 for (unsigned i = 0; i != RVLocs.size(); ++i) {
360 Chain = DAG.getCopyFromReg(Chain, dl,
361 RVLocs[i].getLocReg(),
362 RVLocs[i].getValVT(), InFlag).getValue(1);
363 InFlag = Chain.getValue(2);
364 InVals.push_back(Chain.getValue(0));
370 /// LowerCall - Functions arguments are copied from virtual regs to
371 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
373 HexagonTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
374 CallingConv::ID CallConv, bool isVarArg,
375 bool doesNotRet, bool &isTailCall,
376 const SmallVectorImpl<ISD::OutputArg> &Outs,
377 const SmallVectorImpl<SDValue> &OutVals,
378 const SmallVectorImpl<ISD::InputArg> &Ins,
379 DebugLoc dl, SelectionDAG &DAG,
380 SmallVectorImpl<SDValue> &InVals) const {
382 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
384 // Analyze operands of the call, assigning locations to each operand.
385 SmallVector<CCValAssign, 16> ArgLocs;
386 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
387 getTargetMachine(), ArgLocs, *DAG.getContext());
389 // Check for varargs.
390 NumNamedVarArgParams = -1;
391 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
393 const Function* CalleeFn = NULL;
394 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
395 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
397 // If a function has zero args and is a vararg function, that's
398 // disallowed so it must be an undeclared function. Do not assume
399 // varargs if the callee is undefined.
400 if (CalleeFn->isVarArg() &&
401 CalleeFn->getFunctionType()->getNumParams() != 0) {
402 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
407 if (NumNamedVarArgParams > 0)
408 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
410 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
414 bool StructAttrFlag =
415 DAG.getMachineFunction().getFunction()->hasStructRetAttr();
416 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
417 isVarArg, IsStructRet,
419 Outs, OutVals, Ins, DAG);
420 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i){
421 CCValAssign &VA = ArgLocs[i];
428 DEBUG(dbgs () << "Eligible for Tail Call\n");
431 "Argument must be passed on stack. Not eligible for Tail Call\n");
434 // Get a count of how many bytes are to be pushed on the stack.
435 unsigned NumBytes = CCInfo.getNextStackOffset();
436 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
437 SmallVector<SDValue, 8> MemOpChains;
440 DAG.getCopyFromReg(Chain, dl, TM.getRegisterInfo()->getStackRegister(),
443 // Walk the register/memloc assignments, inserting copies/loads.
444 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
445 CCValAssign &VA = ArgLocs[i];
446 SDValue Arg = OutVals[i];
447 ISD::ArgFlagsTy Flags = Outs[i].Flags;
449 // Promote the value if needed.
450 switch (VA.getLocInfo()) {
452 // Loc info must be one of Full, SExt, ZExt, or AExt.
453 llvm_unreachable("Unknown loc info!");
454 case CCValAssign::Full:
456 case CCValAssign::SExt:
457 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
459 case CCValAssign::ZExt:
460 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
462 case CCValAssign::AExt:
463 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
468 unsigned LocMemOffset = VA.getLocMemOffset();
469 SDValue PtrOff = DAG.getConstant(LocMemOffset, StackPtr.getValueType());
470 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
472 if (Flags.isByVal()) {
473 // The argument is a struct passed by value. According to LLVM, "Arg"
475 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, PtrOff, Chain,
478 // The argument is not passed by value. "Arg" is a buildin type. It is
480 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
481 MachinePointerInfo(),false, false,
487 // Arguments that can be passed on register must be kept at RegsToPass
490 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
494 // Transform all store nodes into one single node because all store
495 // nodes are independent of each other.
496 if (!MemOpChains.empty()) {
497 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &MemOpChains[0],
502 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes,
503 getPointerTy(), true));
505 // Build a sequence of copy-to-reg nodes chained together with token
506 // chain and flag operands which copy the outgoing args into registers.
507 // The InFlag in necessary since all emited instructions must be
511 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
512 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
513 RegsToPass[i].second, InFlag);
514 InFlag = Chain.getValue(1);
518 // For tail calls lower the arguments to the 'real' stack slot.
520 // Force all the incoming stack arguments to be loaded from the stack
521 // before any new outgoing arguments are stored to the stack, because the
522 // outgoing stack slots may alias the incoming argument stack slots, and
523 // the alias isn't otherwise explicit. This is slightly more conservative
524 // than necessary, because it means that each store effectively depends
525 // on every argument instead of just those arguments it would clobber.
527 // Do not flag preceeding copytoreg stuff together with the following stuff.
529 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
530 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
531 RegsToPass[i].second, InFlag);
532 InFlag = Chain.getValue(1);
537 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
538 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
539 // node so that legalize doesn't hack it.
540 if (flag_aligned_memcpy) {
541 const char *MemcpyName =
542 "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
544 DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
545 flag_aligned_memcpy = false;
546 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
547 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
548 } else if (ExternalSymbolSDNode *S =
549 dyn_cast<ExternalSymbolSDNode>(Callee)) {
550 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
553 // Returns a chain & a flag for retval copy to use.
554 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
555 SmallVector<SDValue, 8> Ops;
556 Ops.push_back(Chain);
557 Ops.push_back(Callee);
559 // Add argument registers to the end of the list so that they are
560 // known live into the call.
561 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
562 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
563 RegsToPass[i].second.getValueType()));
566 if (InFlag.getNode()) {
567 Ops.push_back(InFlag);
571 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
573 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
574 InFlag = Chain.getValue(1);
576 // Create the CALLSEQ_END node.
577 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
578 DAG.getIntPtrConstant(0, true), InFlag);
579 InFlag = Chain.getValue(1);
581 // Handle result values, copying them out of physregs into vregs that we
583 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
584 InVals, OutVals, Callee);
587 static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
588 bool isSEXTLoad, SDValue &Base,
589 SDValue &Offset, bool &isInc,
591 if (Ptr->getOpcode() != ISD::ADD)
594 if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
595 isInc = (Ptr->getOpcode() == ISD::ADD);
596 Base = Ptr->getOperand(0);
597 Offset = Ptr->getOperand(1);
598 // Ensure that Offset is a constant.
599 return (isa<ConstantSDNode>(Offset));
605 // TODO: Put this function along with the other isS* functions in
606 // HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
607 // functions defined in HexagonImmediates.td.
608 static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
609 ConstantSDNode *N = cast<ConstantSDNode>(S);
611 // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
613 int64_t v = (int64_t)N->getSExtValue();
615 if (ShiftAmount > 0) {
617 v = v >> ShiftAmount;
619 return (v <= 7) && (v >= -8) && (m == 0);
622 /// getPostIndexedAddressParts - returns true by value, base pointer and
623 /// offset pointer and addressing mode by reference if this node can be
624 /// combined with a load / store to form a post-indexed load / store.
625 bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
628 ISD::MemIndexedMode &AM,
629 SelectionDAG &DAG) const
633 bool isSEXTLoad = false;
635 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
636 VT = LD->getMemoryVT();
637 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
638 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
639 VT = ST->getMemoryVT();
640 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
648 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
650 // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
651 int ShiftAmount = VT.getSizeInBits() / 16;
652 if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
653 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
660 SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
661 SelectionDAG &DAG) const {
662 SDNode *Node = Op.getNode();
663 MachineFunction &MF = DAG.getMachineFunction();
664 HexagonMachineFunctionInfo *FuncInfo =
665 MF.getInfo<HexagonMachineFunctionInfo>();
666 switch (Node->getOpcode()) {
667 case ISD::INLINEASM: {
668 unsigned NumOps = Node->getNumOperands();
669 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
670 --NumOps; // Ignore the flag operand.
672 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
673 if (FuncInfo->hasClobberLR())
676 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
677 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
678 ++i; // Skip the ID value.
680 switch (InlineAsm::getKind(Flags)) {
681 default: llvm_unreachable("Bad flags!");
682 case InlineAsm::Kind_RegDef:
683 case InlineAsm::Kind_RegUse:
684 case InlineAsm::Kind_Imm:
685 case InlineAsm::Kind_Clobber:
686 case InlineAsm::Kind_Mem: {
687 for (; NumVals; --NumVals, ++i) {}
690 case InlineAsm::Kind_RegDefEarlyClobber: {
691 for (; NumVals; --NumVals, ++i) {
693 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
696 if (Reg == TM.getRegisterInfo()->getRARegister()) {
697 FuncInfo->setHasClobberLR(true);
712 // Taken from the XCore backend.
714 SDValue HexagonTargetLowering::
715 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
717 SDValue Chain = Op.getOperand(0);
718 SDValue Table = Op.getOperand(1);
719 SDValue Index = Op.getOperand(2);
720 DebugLoc dl = Op.getDebugLoc();
721 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
722 unsigned JTI = JT->getIndex();
723 MachineFunction &MF = DAG.getMachineFunction();
724 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
725 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
727 // Mark all jump table targets as address taken.
728 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
729 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
730 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
731 MachineBasicBlock *MBB = JTBBs[i];
732 MBB->setHasAddressTaken();
733 // This line is needed to set the hasAddressTaken flag on the BasicBlock
735 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
738 SDValue JumpTableBase = DAG.getNode(HexagonISD::WrapperJT, dl,
739 getPointerTy(), TargetJT);
740 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
741 DAG.getConstant(2, MVT::i32));
742 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
744 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
745 MachinePointerInfo(), false, false, false,
747 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
752 HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
753 SelectionDAG &DAG) const {
754 SDValue Chain = Op.getOperand(0);
755 SDValue Size = Op.getOperand(1);
756 DebugLoc dl = Op.getDebugLoc();
758 unsigned SPReg = getStackPointerRegisterToSaveRestore();
760 // Get a reference to the stack pointer.
761 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
763 // Subtract the dynamic size from the actual stack size to
764 // obtain the new stack size.
765 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
768 // For Hexagon, the outgoing memory arguments area should be on top of the
769 // alloca area on the stack i.e., the outgoing memory arguments should be
770 // at a lower address than the alloca area. Move the alloca area down the
771 // stack by adding back the space reserved for outgoing arguments to SP
774 // We do not know what the size of the outgoing args is at this point.
775 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
776 // stack pointer. We patch this instruction with the correct, known
777 // offset in emitPrologue().
779 // Use a placeholder immediate (zero) for now. This will be patched up
780 // by emitPrologue().
781 SDValue ArgAdjust = DAG.getNode(HexagonISD::ADJDYNALLOC, dl,
784 DAG.getConstant(0, MVT::i32));
786 // The Sub result contains the new stack start address, so it
787 // must be placed in the stack pointer register.
788 SDValue CopyChain = DAG.getCopyToReg(Chain, dl,
789 TM.getRegisterInfo()->getStackRegister(),
792 SDValue Ops[2] = { ArgAdjust, CopyChain };
793 return DAG.getMergeValues(Ops, 2, dl);
797 HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
798 CallingConv::ID CallConv,
801 SmallVectorImpl<ISD::InputArg> &Ins,
802 DebugLoc dl, SelectionDAG &DAG,
803 SmallVectorImpl<SDValue> &InVals)
806 MachineFunction &MF = DAG.getMachineFunction();
807 MachineFrameInfo *MFI = MF.getFrameInfo();
808 MachineRegisterInfo &RegInfo = MF.getRegInfo();
809 HexagonMachineFunctionInfo *FuncInfo =
810 MF.getInfo<HexagonMachineFunctionInfo>();
813 // Assign locations to all of the incoming arguments.
814 SmallVector<CCValAssign, 16> ArgLocs;
815 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
816 getTargetMachine(), ArgLocs, *DAG.getContext());
818 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
820 // For LLVM, in the case when returning a struct by value (>8byte),
821 // the first argument is a pointer that points to the location on caller's
822 // stack where the return value will be stored. For Hexagon, the location on
823 // caller's stack is passed only when the struct size is smaller than (and
824 // equal to) 8 bytes. If not, no address will be passed into callee and
825 // callee return the result direclty through R0/R1.
827 SmallVector<SDValue, 4> MemOps;
829 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
830 CCValAssign &VA = ArgLocs[i];
831 ISD::ArgFlagsTy Flags = Ins[i].Flags;
833 unsigned StackLocation;
836 if ( (VA.isRegLoc() && !Flags.isByVal())
837 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
838 // Arguments passed in registers
839 // 1. int, long long, ptr args that get allocated in register.
840 // 2. Large struct that gets an register to put its address in.
841 EVT RegVT = VA.getLocVT();
842 if (RegVT == MVT::i8 || RegVT == MVT::i16 || RegVT == MVT::i32) {
844 RegInfo.createVirtualRegister(Hexagon::IntRegsRegisterClass);
845 RegInfo.addLiveIn(VA.getLocReg(), VReg);
846 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
847 } else if (RegVT == MVT::i64) {
849 RegInfo.createVirtualRegister(Hexagon::DoubleRegsRegisterClass);
850 RegInfo.addLiveIn(VA.getLocReg(), VReg);
851 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
855 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
856 assert (0 && "ByValSize must be bigger than 8 bytes");
859 assert(VA.isMemLoc());
861 if (Flags.isByVal()) {
862 // If it's a byval parameter, then we need to compute the
863 // "real" size, not the size of the pointer.
864 ObjSize = Flags.getByValSize();
866 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
869 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
870 // Create the frame index object for this incoming parameter...
871 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
873 // Create the SelectionDAG nodes cordl, responding to a load
874 // from this parameter.
875 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
877 if (Flags.isByVal()) {
878 // If it's a pass-by-value aggregate, then do not dereference the stack
879 // location. Instead, we should generate a reference to the stack
881 InVals.push_back(FIN);
883 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
884 MachinePointerInfo(), false, false,
891 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &MemOps[0],
895 // This will point to the next argument passed via stack.
896 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
898 CCInfo.getNextStackOffset(),
900 FuncInfo->setVarArgsFrameIndex(FrameIndex);
907 HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
908 // VASTART stores the address of the VarArgsFrameIndex slot into the
909 // memory location argument.
910 MachineFunction &MF = DAG.getMachineFunction();
911 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
912 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
913 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
914 return DAG.getStore(Op.getOperand(0), Op.getDebugLoc(), Addr,
915 Op.getOperand(1), MachinePointerInfo(SV), false,
920 HexagonTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
921 SDNode* OpNode = Op.getNode();
923 SDValue Cond = DAG.getNode(ISD::SETCC, Op.getDebugLoc(), MVT::i1,
924 Op.getOperand(2), Op.getOperand(3),
926 return DAG.getNode(ISD::SELECT, Op.getDebugLoc(), OpNode->getValueType(0),
927 Cond, Op.getOperand(0),
932 HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
933 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
934 MachineFunction &MF = DAG.getMachineFunction();
935 MachineFrameInfo *MFI = MF.getFrameInfo();
936 MFI->setReturnAddressIsTaken(true);
938 EVT VT = Op.getValueType();
939 DebugLoc dl = Op.getDebugLoc();
940 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
942 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
943 SDValue Offset = DAG.getConstant(4, MVT::i32);
944 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
945 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
946 MachinePointerInfo(), false, false, false, 0);
949 // Return LR, which contains the return address. Mark it an implicit live-in.
950 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
951 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
955 HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
956 const HexagonRegisterInfo *TRI = TM.getRegisterInfo();
957 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
958 MFI->setFrameAddressIsTaken(true);
960 EVT VT = Op.getValueType();
961 DebugLoc dl = Op.getDebugLoc();
962 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
963 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
964 TRI->getFrameRegister(), VT);
966 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
967 MachinePointerInfo(),
968 false, false, false, 0);
973 SDValue HexagonTargetLowering::LowerMEMBARRIER(SDValue Op,
974 SelectionDAG& DAG) const {
975 DebugLoc dl = Op.getDebugLoc();
976 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
980 SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
981 SelectionDAG& DAG) const {
982 DebugLoc dl = Op.getDebugLoc();
983 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
987 SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
988 SelectionDAG &DAG) const {
990 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
991 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
992 DebugLoc dl = Op.getDebugLoc();
993 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
995 HexagonTargetObjectFile &TLOF =
996 (HexagonTargetObjectFile&)getObjFileLowering();
997 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
998 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
1001 return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
1004 //===----------------------------------------------------------------------===//
1005 // TargetLowering Implementation
1006 //===----------------------------------------------------------------------===//
1008 HexagonTargetLowering::HexagonTargetLowering(HexagonTargetMachine
1010 : TargetLowering(targetmachine, new HexagonTargetObjectFile()),
1013 // Set up the register classes.
1014 addRegisterClass(MVT::i32, Hexagon::IntRegsRegisterClass);
1015 addRegisterClass(MVT::i64, Hexagon::DoubleRegsRegisterClass);
1017 addRegisterClass(MVT::i1, Hexagon::PredRegsRegisterClass);
1019 computeRegisterProperties();
1022 setPrefLoopAlignment(4);
1024 // Limits for inline expansion of memcpy/memmove
1025 maxStoresPerMemcpy = 6;
1026 maxStoresPerMemmove = 6;
1029 // Library calls for unsupported operations
1031 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1033 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1034 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1035 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1036 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1037 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1038 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1039 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1041 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1042 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1043 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1045 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1046 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1047 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1049 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1050 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1051 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1052 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1053 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
1055 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1057 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1058 setOperationAction(ISD::SDIV, MVT::i32, Expand);
1059 setLibcallName(RTLIB::SREM_I32, "__hexagon_umodsi3");
1060 setOperationAction(ISD::SREM, MVT::i32, Expand);
1062 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1063 setOperationAction(ISD::SDIV, MVT::i64, Expand);
1064 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1065 setOperationAction(ISD::SREM, MVT::i64, Expand);
1067 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1068 setOperationAction(ISD::UDIV, MVT::i32, Expand);
1070 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1071 setOperationAction(ISD::UDIV, MVT::i64, Expand);
1073 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1074 setOperationAction(ISD::UREM, MVT::i32, Expand);
1076 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
1077 setOperationAction(ISD::UREM, MVT::i64, Expand);
1079 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1080 setOperationAction(ISD::FDIV, MVT::f32, Expand);
1082 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1083 setOperationAction(ISD::FDIV, MVT::f64, Expand);
1085 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1086 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1088 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1089 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1091 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1092 setOperationAction(ISD::FADD, MVT::f64, Expand);
1094 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1095 setOperationAction(ISD::FADD, MVT::f32, Expand);
1097 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1098 setOperationAction(ISD::FADD, MVT::f32, Expand);
1100 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1101 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
1103 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1104 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1106 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1107 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1109 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1110 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1112 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1113 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
1115 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1116 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
1118 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1119 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
1121 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1122 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
1124 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1125 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
1127 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1128 setCondCodeAction(ISD::SETOLT, MVT::f64, Expand);
1130 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1131 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
1133 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1134 setOperationAction(ISD::SREM, MVT::i32, Expand);
1136 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1137 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1139 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1140 setOperationAction(ISD::MUL, MVT::f32, Expand);
1142 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1143 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1145 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1148 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1149 setOperationAction(ISD::SUB, MVT::f64, Expand);
1151 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1152 setOperationAction(ISD::SUB, MVT::f32, Expand);
1154 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1155 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1157 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1158 setCondCodeAction(ISD::SETUO, MVT::f64, Expand);
1160 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
1161 setCondCodeAction(ISD::SETO, MVT::f64, Expand);
1163 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1164 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
1166 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1167 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
1169 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1170 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
1172 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
1173 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
1174 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
1175 setIndexedLoadAction(ISD::POST_INC, MVT::i64, Legal);
1177 setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal);
1178 setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal);
1179 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
1180 setIndexedStoreAction(ISD::POST_INC, MVT::i64, Legal);
1182 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
1184 // Turn FP extload into load/fextend.
1185 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
1186 // Hexagon has a i1 sign extending load.
1187 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Expand);
1188 // Turn FP truncstore into trunc + store.
1189 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1191 // Custom legalize GlobalAddress nodes into CONST32.
1192 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
1193 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1195 setOperationAction(ISD::TRUNCATE, MVT::i64, Expand);
1197 // Hexagon doesn't have sext_inreg, replace them with shl/sra.
1198 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
1200 // Hexagon has no REM or DIVREM operations.
1201 setOperationAction(ISD::UREM, MVT::i32, Expand);
1202 setOperationAction(ISD::SREM, MVT::i32, Expand);
1203 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1204 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1205 setOperationAction(ISD::SREM, MVT::i64, Expand);
1206 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1207 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1209 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
1211 // Expand fp<->uint.
1212 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
1213 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1215 // Hexagon has no select or setcc: expand to SELECT_CC.
1216 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1217 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1219 // Lower SELECT_CC to SETCC and SELECT.
1220 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1221 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
1222 // This is a workaround documented in DAGCombiner.cpp:2892 We don't
1223 // support SELECT_CC on every type.
1224 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
1226 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
1227 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1228 if (EmitJumpTables) {
1229 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
1231 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1234 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
1236 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
1237 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
1239 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1240 setOperationAction(ISD::FCOS , MVT::f64, Expand);
1241 setOperationAction(ISD::FREM , MVT::f64, Expand);
1242 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1243 setOperationAction(ISD::FCOS , MVT::f32, Expand);
1244 setOperationAction(ISD::FREM , MVT::f32, Expand);
1245 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1246 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1247 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1248 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1249 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
1250 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1251 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1252 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1253 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1254 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1255 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1256 setOperationAction(ISD::FPOW , MVT::f32, Expand);
1258 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1259 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1260 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
1262 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1263 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1265 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1266 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1268 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
1269 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
1270 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
1271 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
1273 setOperationAction(ISD::EH_RETURN, MVT::Other, Expand);
1275 if (TM.getSubtargetImpl()->isSubtargetV2()) {
1276 setExceptionPointerRegister(Hexagon::R20);
1277 setExceptionSelectorRegister(Hexagon::R21);
1279 setExceptionPointerRegister(Hexagon::R0);
1280 setExceptionSelectorRegister(Hexagon::R1);
1283 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1284 setOperationAction(ISD::VASTART , MVT::Other, Custom);
1286 // Use the default implementation.
1287 setOperationAction(ISD::VAARG , MVT::Other, Expand);
1288 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1289 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1290 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1291 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1294 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
1295 setOperationAction(ISD::INLINEASM , MVT::Other, Custom);
1297 setMinFunctionAlignment(2);
1299 // Needed for DYNAMIC_STACKALLOC expansion.
1300 unsigned StackRegister = TM.getRegisterInfo()->getStackRegister();
1301 setStackPointerRegisterToSaveRestore(StackRegister);
1302 setSchedulingPreference(Sched::VLIW);
1307 HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
1310 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1311 case HexagonISD::ADJDYNALLOC: return "HexagonISD::ADJDYNALLOC";
1312 case HexagonISD::CMPICC: return "HexagonISD::CMPICC";
1313 case HexagonISD::CMPFCC: return "HexagonISD::CMPFCC";
1314 case HexagonISD::BRICC: return "HexagonISD::BRICC";
1315 case HexagonISD::BRFCC: return "HexagonISD::BRFCC";
1316 case HexagonISD::SELECT_ICC: return "HexagonISD::SELECT_ICC";
1317 case HexagonISD::SELECT_FCC: return "HexagonISD::SELECT_FCC";
1318 case HexagonISD::Hi: return "HexagonISD::Hi";
1319 case HexagonISD::Lo: return "HexagonISD::Lo";
1320 case HexagonISD::FTOI: return "HexagonISD::FTOI";
1321 case HexagonISD::ITOF: return "HexagonISD::ITOF";
1322 case HexagonISD::CALL: return "HexagonISD::CALL";
1323 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
1324 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
1325 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
1330 HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
1331 EVT MTy1 = EVT::getEVT(Ty1);
1332 EVT MTy2 = EVT::getEVT(Ty2);
1333 if (!MTy1.isSimple() || !MTy2.isSimple()) {
1336 return ((MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32));
1339 bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1340 if (!VT1.isSimple() || !VT2.isSimple()) {
1343 return ((VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32));
1347 HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1348 switch (Op.getOpcode()) {
1349 default: llvm_unreachable("Should not custom lower this!");
1350 // Frame & Return address. Currently unimplemented.
1351 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1352 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
1353 case ISD::GlobalTLSAddress:
1354 llvm_unreachable("TLS not implemented for Hexagon.");
1355 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
1356 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
1357 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
1358 case ISD::VASTART: return LowerVASTART(Op, DAG);
1359 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1361 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
1362 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1363 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1364 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
1371 //===----------------------------------------------------------------------===//
1372 // Hexagon Scheduler Hooks
1373 //===----------------------------------------------------------------------===//
1375 HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1376 MachineBasicBlock *BB)
1378 switch (MI->getOpcode()) {
1379 case Hexagon::ADJDYNALLOC: {
1380 MachineFunction *MF = BB->getParent();
1381 HexagonMachineFunctionInfo *FuncInfo =
1382 MF->getInfo<HexagonMachineFunctionInfo>();
1383 FuncInfo->addAllocaAdjustInst(MI);
1386 default: llvm_unreachable("Unexpected instr type to insert");
1390 //===----------------------------------------------------------------------===//
1391 // Inline Assembly Support
1392 //===----------------------------------------------------------------------===//
1394 std::pair<unsigned, const TargetRegisterClass*>
1395 HexagonTargetLowering::getRegForInlineAsmConstraint(const
1396 std::string &Constraint,
1398 if (Constraint.size() == 1) {
1399 switch (Constraint[0]) {
1401 switch (VT.getSimpleVT().SimpleTy) {
1403 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
1407 return std::make_pair(0U, Hexagon::IntRegsRegisterClass);
1409 return std::make_pair(0U, Hexagon::DoubleRegsRegisterClass);
1412 llvm_unreachable("Unknown asm register class");
1416 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1419 /// isLegalAddressingMode - Return true if the addressing mode represented by
1420 /// AM is legal for this target, for a load/store of the specified type.
1421 bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1423 // Allows a signed-extended 11-bit immediate field.
1424 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1) {
1428 // No global is ever allowed as a base.
1433 int Scale = AM.Scale;
1434 if (Scale < 0) Scale = -Scale;
1436 case 0: // No scale reg, "r+i", "r", or just "i".
1438 default: // No scaled addressing mode.
1444 /// isLegalICmpImmediate - Return true if the specified immediate is legal
1445 /// icmp immediate, that is the target has icmp instructions which can compare
1446 /// a register against the immediate without having to materialize the
1447 /// immediate into a register.
1448 bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
1449 return Imm >= -512 && Imm <= 511;
1452 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
1453 /// for tail call optimization. Targets which want to do tail call
1454 /// optimization should implement this function.
1455 bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
1457 CallingConv::ID CalleeCC,
1459 bool isCalleeStructRet,
1460 bool isCallerStructRet,
1461 const SmallVectorImpl<ISD::OutputArg> &Outs,
1462 const SmallVectorImpl<SDValue> &OutVals,
1463 const SmallVectorImpl<ISD::InputArg> &Ins,
1464 SelectionDAG& DAG) const {
1465 const Function *CallerF = DAG.getMachineFunction().getFunction();
1466 CallingConv::ID CallerCC = CallerF->getCallingConv();
1467 bool CCMatch = CallerCC == CalleeCC;
1469 // ***************************************************************************
1470 // Look for obvious safe cases to perform tail call optimization that do not
1471 // require ABI changes.
1472 // ***************************************************************************
1474 // If this is a tail call via a function pointer, then don't do it!
1475 if (!(dyn_cast<GlobalAddressSDNode>(Callee))
1476 && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
1480 // Do not optimize if the calling conventions do not match.
1484 // Do not tail call optimize vararg calls.
1488 // Also avoid tail call optimization if either caller or callee uses struct
1489 // return semantics.
1490 if (isCalleeStructRet || isCallerStructRet)
1493 // In addition to the cases above, we also disable Tail Call Optimization if
1494 // the calling convention code that at least one outgoing argument needs to
1495 // go on the stack. We cannot check that here because at this point that
1496 // information is not available.