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 "HexagonMachineFunctionInfo.h"
17 #include "HexagonSubtarget.h"
18 #include "HexagonTargetMachine.h"
19 #include "HexagonTargetObjectFile.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineJumpTableInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/CodeGen/ValueTypes.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/InlineAsm.h"
34 #include "llvm/IR/Intrinsics.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 #define DEBUG_TYPE "hexagon-lowering"
45 EmitJumpTables("hexagon-emit-jump-tables", cl::init(true), cl::Hidden,
46 cl::desc("Control jump table emission on Hexagon target"));
49 class HexagonCCState : public CCState {
50 int NumNamedVarArgParams;
53 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
54 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
55 int NumNamedVarArgParams)
56 : CCState(CC, isVarArg, MF, locs, C),
57 NumNamedVarArgParams(NumNamedVarArgParams) {}
59 int getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
63 // Implement calling convention for Hexagon.
65 CC_Hexagon(unsigned ValNo, MVT ValVT,
66 MVT LocVT, CCValAssign::LocInfo LocInfo,
67 ISD::ArgFlagsTy ArgFlags, CCState &State);
70 CC_Hexagon32(unsigned ValNo, MVT ValVT,
71 MVT LocVT, CCValAssign::LocInfo LocInfo,
72 ISD::ArgFlagsTy ArgFlags, CCState &State);
75 CC_Hexagon64(unsigned ValNo, MVT ValVT,
76 MVT LocVT, CCValAssign::LocInfo LocInfo,
77 ISD::ArgFlagsTy ArgFlags, CCState &State);
80 RetCC_Hexagon(unsigned ValNo, MVT ValVT,
81 MVT LocVT, CCValAssign::LocInfo LocInfo,
82 ISD::ArgFlagsTy ArgFlags, CCState &State);
85 RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
86 MVT LocVT, CCValAssign::LocInfo LocInfo,
87 ISD::ArgFlagsTy ArgFlags, CCState &State);
90 RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
91 MVT LocVT, CCValAssign::LocInfo LocInfo,
92 ISD::ArgFlagsTy ArgFlags, CCState &State);
95 CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
96 MVT LocVT, CCValAssign::LocInfo LocInfo,
97 ISD::ArgFlagsTy ArgFlags, CCState &State) {
98 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
100 // NumNamedVarArgParams can not be zero for a VarArg function.
101 assert((HState.getNumNamedVarArgParams() > 0) &&
102 "NumNamedVarArgParams is not bigger than zero.");
104 if ((int)ValNo < HState.getNumNamedVarArgParams()) {
105 // Deal with named arguments.
106 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
109 // Deal with un-named arguments.
111 if (ArgFlags.isByVal()) {
112 // If pass-by-value, the size allocated on stack is decided
113 // by ArgFlags.getByValSize(), not by the size of LocVT.
114 assert ((ArgFlags.getByValSize() > 8) &&
115 "ByValSize must be bigger than 8 bytes");
116 ofst = State.AllocateStack(ArgFlags.getByValSize(), 4);
117 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
120 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
123 if (ArgFlags.isSExt())
124 LocInfo = CCValAssign::SExt;
125 else if (ArgFlags.isZExt())
126 LocInfo = CCValAssign::ZExt;
128 LocInfo = CCValAssign::AExt;
130 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
131 ofst = State.AllocateStack(4, 4);
132 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
135 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
136 ofst = State.AllocateStack(8, 8);
137 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
140 llvm_unreachable(nullptr);
145 CC_Hexagon (unsigned ValNo, MVT ValVT,
146 MVT LocVT, CCValAssign::LocInfo LocInfo,
147 ISD::ArgFlagsTy ArgFlags, CCState &State) {
149 if (ArgFlags.isByVal()) {
151 assert ((ArgFlags.getByValSize() > 8) &&
152 "ByValSize must be bigger than 8 bytes");
153 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 4);
154 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
158 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
161 if (ArgFlags.isSExt())
162 LocInfo = CCValAssign::SExt;
163 else if (ArgFlags.isZExt())
164 LocInfo = CCValAssign::ZExt;
166 LocInfo = CCValAssign::AExt;
167 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
169 LocInfo = CCValAssign::BCvt;
170 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
172 LocInfo = CCValAssign::BCvt;
175 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
176 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
180 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
181 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
185 return true; // CC didn't match.
189 static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
190 MVT LocVT, CCValAssign::LocInfo LocInfo,
191 ISD::ArgFlagsTy ArgFlags, CCState &State) {
193 static const MCPhysReg RegList[] = {
194 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
197 if (unsigned Reg = State.AllocateReg(RegList)) {
198 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
202 unsigned Offset = State.AllocateStack(4, 4);
203 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
207 static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
208 MVT LocVT, CCValAssign::LocInfo LocInfo,
209 ISD::ArgFlagsTy ArgFlags, CCState &State) {
211 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
212 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
216 static const MCPhysReg RegList1[] = {
217 Hexagon::D1, Hexagon::D2
219 static const MCPhysReg RegList2[] = {
220 Hexagon::R1, Hexagon::R3
222 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
223 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
227 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
228 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
232 static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
233 MVT LocVT, CCValAssign::LocInfo LocInfo,
234 ISD::ArgFlagsTy ArgFlags, CCState &State) {
237 if (LocVT == MVT::i1 ||
242 if (ArgFlags.isSExt())
243 LocInfo = CCValAssign::SExt;
244 else if (ArgFlags.isZExt())
245 LocInfo = CCValAssign::ZExt;
247 LocInfo = CCValAssign::AExt;
248 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
250 LocInfo = CCValAssign::BCvt;
251 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
253 LocInfo = CCValAssign::BCvt;
256 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
257 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
261 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
262 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
266 return true; // CC didn't match.
269 static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
270 MVT LocVT, CCValAssign::LocInfo LocInfo,
271 ISD::ArgFlagsTy ArgFlags, CCState &State) {
273 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
274 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
275 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
280 unsigned Offset = State.AllocateStack(4, 4);
281 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
285 static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
286 MVT LocVT, CCValAssign::LocInfo LocInfo,
287 ISD::ArgFlagsTy ArgFlags, CCState &State) {
288 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
289 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
290 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
295 unsigned Offset = State.AllocateStack(8, 8);
296 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
301 HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
306 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
307 /// by "Src" to address "Dst" of size "Size". Alignment information is
308 /// specified by the specific parameter attribute. The copy will be passed as
309 /// a byval function parameter. Sometimes what we are copying is the end of a
310 /// larger object, the part that does not fit in registers.
312 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
313 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
316 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
317 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
318 /*isVolatile=*/false, /*AlwaysInline=*/false,
319 MachinePointerInfo(), MachinePointerInfo());
323 // LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
324 // passed by value, the function prototype is modified to return void and
325 // the value is stored in memory pointed by a pointer passed by caller.
327 HexagonTargetLowering::LowerReturn(SDValue Chain,
328 CallingConv::ID CallConv, bool isVarArg,
329 const SmallVectorImpl<ISD::OutputArg> &Outs,
330 const SmallVectorImpl<SDValue> &OutVals,
331 SDLoc dl, SelectionDAG &DAG) const {
333 // CCValAssign - represent the assignment of the return value to locations.
334 SmallVector<CCValAssign, 16> RVLocs;
336 // CCState - Info about the registers and stack slot.
337 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
340 // Analyze return values of ISD::RET
341 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
344 SmallVector<SDValue, 4> RetOps(1, Chain);
346 // Copy the result values into the output registers.
347 for (unsigned i = 0; i != RVLocs.size(); ++i) {
348 CCValAssign &VA = RVLocs[i];
350 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
352 // Guarantee that all emitted copies are stuck together with flags.
353 Flag = Chain.getValue(1);
354 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
357 RetOps[0] = Chain; // Update chain.
359 // Add the flag if we have it.
361 RetOps.push_back(Flag);
363 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
369 /// LowerCallResult - Lower the result values of an ISD::CALL into the
370 /// appropriate copies out of appropriate physical registers. This assumes that
371 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
372 /// being lowered. Returns a SDNode with the same number of values as the
375 HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
376 CallingConv::ID CallConv, bool isVarArg,
378 SmallVectorImpl<ISD::InputArg> &Ins,
379 SDLoc dl, SelectionDAG &DAG,
380 SmallVectorImpl<SDValue> &InVals,
381 const SmallVectorImpl<SDValue> &OutVals,
382 SDValue Callee) const {
384 // Assign locations to each value returned by this call.
385 SmallVector<CCValAssign, 16> RVLocs;
387 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
390 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
392 // Copy all of the result registers out of their specified physreg.
393 for (unsigned i = 0; i != RVLocs.size(); ++i) {
394 Chain = DAG.getCopyFromReg(Chain, dl,
395 RVLocs[i].getLocReg(),
396 RVLocs[i].getValVT(), InFlag).getValue(1);
397 InFlag = Chain.getValue(2);
398 InVals.push_back(Chain.getValue(0));
404 /// LowerCall - Functions arguments are copied from virtual regs to
405 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
407 HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
408 SmallVectorImpl<SDValue> &InVals) const {
409 SelectionDAG &DAG = CLI.DAG;
411 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
412 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
413 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
414 SDValue Chain = CLI.Chain;
415 SDValue Callee = CLI.Callee;
416 bool &isTailCall = CLI.IsTailCall;
417 CallingConv::ID CallConv = CLI.CallConv;
418 bool isVarArg = CLI.IsVarArg;
419 bool doesNotReturn = CLI.DoesNotReturn;
421 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
423 // Check for varargs.
424 int NumNamedVarArgParams = -1;
425 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
427 const Function* CalleeFn = nullptr;
428 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
429 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
431 // If a function has zero args and is a vararg function, that's
432 // disallowed so it must be an undeclared function. Do not assume
433 // varargs if the callee is undefined.
434 if (CalleeFn->isVarArg() &&
435 CalleeFn->getFunctionType()->getNumParams() != 0) {
436 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
441 // Analyze operands of the call, assigning locations to each operand.
442 SmallVector<CCValAssign, 16> ArgLocs;
443 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
444 *DAG.getContext(), NumNamedVarArgParams);
446 if (NumNamedVarArgParams > 0)
447 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
449 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
453 bool StructAttrFlag =
454 DAG.getMachineFunction().getFunction()->hasStructRetAttr();
455 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
456 isVarArg, IsStructRet,
458 Outs, OutVals, Ins, DAG);
459 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i){
460 CCValAssign &VA = ArgLocs[i];
467 DEBUG(dbgs () << "Eligible for Tail Call\n");
470 "Argument must be passed on stack. Not eligible for Tail Call\n");
473 // Get a count of how many bytes are to be pushed on the stack.
474 unsigned NumBytes = CCInfo.getNextStackOffset();
475 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
476 SmallVector<SDValue, 8> MemOpChains;
478 const HexagonRegisterInfo *QRI = Subtarget->getRegisterInfo();
480 DAG.getCopyFromReg(Chain, dl, QRI->getStackRegister(), getPointerTy());
482 // Walk the register/memloc assignments, inserting copies/loads.
483 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
484 CCValAssign &VA = ArgLocs[i];
485 SDValue Arg = OutVals[i];
486 ISD::ArgFlagsTy Flags = Outs[i].Flags;
488 // Promote the value if needed.
489 switch (VA.getLocInfo()) {
491 // Loc info must be one of Full, SExt, ZExt, or AExt.
492 llvm_unreachable("Unknown loc info!");
493 case CCValAssign::Full:
495 case CCValAssign::SExt:
496 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
498 case CCValAssign::ZExt:
499 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
501 case CCValAssign::AExt:
502 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
507 unsigned LocMemOffset = VA.getLocMemOffset();
508 SDValue PtrOff = DAG.getConstant(LocMemOffset, StackPtr.getValueType());
509 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
511 if (Flags.isByVal()) {
512 // The argument is a struct passed by value. According to LLVM, "Arg"
514 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, PtrOff, Chain,
517 // The argument is not passed by value. "Arg" is a buildin type. It is
519 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
520 MachinePointerInfo(),false, false,
526 // Arguments that can be passed on register must be kept at RegsToPass
529 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
533 // Transform all store nodes into one single node because all store
534 // nodes are independent of each other.
535 if (!MemOpChains.empty()) {
536 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
540 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes,
541 getPointerTy(), true),
544 // Build a sequence of copy-to-reg nodes chained together with token
545 // chain and flag operands which copy the outgoing args into registers.
546 // The InFlag in necessary since all emitted instructions must be
550 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
551 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
552 RegsToPass[i].second, InFlag);
553 InFlag = Chain.getValue(1);
557 // For tail calls lower the arguments to the 'real' stack slot.
559 // Force all the incoming stack arguments to be loaded from the stack
560 // before any new outgoing arguments are stored to the stack, because the
561 // outgoing stack slots may alias the incoming argument stack slots, and
562 // the alias isn't otherwise explicit. This is slightly more conservative
563 // than necessary, because it means that each store effectively depends
564 // on every argument instead of just those arguments it would clobber.
566 // Do not flag preceding copytoreg stuff together with the following stuff.
568 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
569 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
570 RegsToPass[i].second, InFlag);
571 InFlag = Chain.getValue(1);
576 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
577 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
578 // node so that legalize doesn't hack it.
579 if (flag_aligned_memcpy) {
580 const char *MemcpyName =
581 "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
583 DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
584 flag_aligned_memcpy = false;
585 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
586 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
587 } else if (ExternalSymbolSDNode *S =
588 dyn_cast<ExternalSymbolSDNode>(Callee)) {
589 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
592 // Returns a chain & a flag for retval copy to use.
593 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
594 SmallVector<SDValue, 8> Ops;
595 Ops.push_back(Chain);
596 Ops.push_back(Callee);
598 // Add argument registers to the end of the list so that they are
599 // known live into the call.
600 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
601 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
602 RegsToPass[i].second.getValueType()));
605 if (InFlag.getNode()) {
606 Ops.push_back(InFlag);
610 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
612 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
613 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
614 InFlag = Chain.getValue(1);
616 // Create the CALLSEQ_END node.
617 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
618 DAG.getIntPtrConstant(0, true), InFlag, dl);
619 InFlag = Chain.getValue(1);
621 // Handle result values, copying them out of physregs into vregs that we
623 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
624 InVals, OutVals, Callee);
627 static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
628 bool isSEXTLoad, SDValue &Base,
629 SDValue &Offset, bool &isInc,
631 if (Ptr->getOpcode() != ISD::ADD)
634 if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
635 isInc = (Ptr->getOpcode() == ISD::ADD);
636 Base = Ptr->getOperand(0);
637 Offset = Ptr->getOperand(1);
638 // Ensure that Offset is a constant.
639 return (isa<ConstantSDNode>(Offset));
645 // TODO: Put this function along with the other isS* functions in
646 // HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
647 // functions defined in HexagonOperands.td.
648 static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
649 ConstantSDNode *N = cast<ConstantSDNode>(S);
651 // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
653 int64_t v = (int64_t)N->getSExtValue();
655 if (ShiftAmount > 0) {
657 v = v >> ShiftAmount;
659 return (v <= 7) && (v >= -8) && (m == 0);
662 /// getPostIndexedAddressParts - returns true by value, base pointer and
663 /// offset pointer and addressing mode by reference if this node can be
664 /// combined with a load / store to form a post-indexed load / store.
665 bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
668 ISD::MemIndexedMode &AM,
669 SelectionDAG &DAG) const
673 bool isSEXTLoad = false;
675 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
676 VT = LD->getMemoryVT();
677 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
678 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
679 VT = ST->getMemoryVT();
680 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
688 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
690 // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
691 int ShiftAmount = VT.getSizeInBits() / 16;
692 if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
693 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
700 SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
701 SelectionDAG &DAG) const {
702 SDNode *Node = Op.getNode();
703 MachineFunction &MF = DAG.getMachineFunction();
704 HexagonMachineFunctionInfo *FuncInfo =
705 MF.getInfo<HexagonMachineFunctionInfo>();
706 switch (Node->getOpcode()) {
707 case ISD::INLINEASM: {
708 unsigned NumOps = Node->getNumOperands();
709 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
710 --NumOps; // Ignore the flag operand.
712 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
713 if (FuncInfo->hasClobberLR())
716 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
717 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
718 ++i; // Skip the ID value.
720 switch (InlineAsm::getKind(Flags)) {
721 default: llvm_unreachable("Bad flags!");
722 case InlineAsm::Kind_RegDef:
723 case InlineAsm::Kind_RegUse:
724 case InlineAsm::Kind_Imm:
725 case InlineAsm::Kind_Clobber:
726 case InlineAsm::Kind_Mem: {
727 for (; NumVals; --NumVals, ++i) {}
730 case InlineAsm::Kind_RegDefEarlyClobber: {
731 for (; NumVals; --NumVals, ++i) {
733 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
736 const HexagonRegisterInfo *QRI = Subtarget->getRegisterInfo();
737 if (Reg == QRI->getRARegister()) {
738 FuncInfo->setHasClobberLR(true);
753 // Taken from the XCore backend.
755 SDValue HexagonTargetLowering::
756 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
758 SDValue Chain = Op.getOperand(0);
759 SDValue Table = Op.getOperand(1);
760 SDValue Index = Op.getOperand(2);
762 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
763 unsigned JTI = JT->getIndex();
764 MachineFunction &MF = DAG.getMachineFunction();
765 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
766 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
768 // Mark all jump table targets as address taken.
769 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
770 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
771 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
772 MachineBasicBlock *MBB = JTBBs[i];
773 MBB->setHasAddressTaken();
774 // This line is needed to set the hasAddressTaken flag on the BasicBlock
776 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
779 SDValue JumpTableBase = DAG.getNode(HexagonISD::JT, dl,
780 getPointerTy(), TargetJT);
781 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
782 DAG.getConstant(2, MVT::i32));
783 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
785 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
786 MachinePointerInfo(), false, false, false,
788 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
793 HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
794 SelectionDAG &DAG) const {
795 SDValue Chain = Op.getOperand(0);
796 SDValue Size = Op.getOperand(1);
799 unsigned SPReg = getStackPointerRegisterToSaveRestore();
801 // Get a reference to the stack pointer.
802 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
804 // Subtract the dynamic size from the actual stack size to
805 // obtain the new stack size.
806 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
809 // For Hexagon, the outgoing memory arguments area should be on top of the
810 // alloca area on the stack i.e., the outgoing memory arguments should be
811 // at a lower address than the alloca area. Move the alloca area down the
812 // stack by adding back the space reserved for outgoing arguments to SP
815 // We do not know what the size of the outgoing args is at this point.
816 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
817 // stack pointer. We patch this instruction with the correct, known
818 // offset in emitPrologue().
820 // Use a placeholder immediate (zero) for now. This will be patched up
821 // by emitPrologue().
822 SDValue ArgAdjust = DAG.getNode(HexagonISD::ADJDYNALLOC, dl,
825 DAG.getConstant(0, MVT::i32));
827 // The Sub result contains the new stack start address, so it
828 // must be placed in the stack pointer register.
829 const HexagonRegisterInfo *QRI = Subtarget->getRegisterInfo();
830 SDValue CopyChain = DAG.getCopyToReg(Chain, dl, QRI->getStackRegister(), Sub);
832 SDValue Ops[2] = { ArgAdjust, CopyChain };
833 return DAG.getMergeValues(Ops, dl);
837 HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
838 CallingConv::ID CallConv,
841 SmallVectorImpl<ISD::InputArg> &Ins,
842 SDLoc dl, SelectionDAG &DAG,
843 SmallVectorImpl<SDValue> &InVals)
846 MachineFunction &MF = DAG.getMachineFunction();
847 MachineFrameInfo *MFI = MF.getFrameInfo();
848 MachineRegisterInfo &RegInfo = MF.getRegInfo();
849 HexagonMachineFunctionInfo *FuncInfo =
850 MF.getInfo<HexagonMachineFunctionInfo>();
853 // Assign locations to all of the incoming arguments.
854 SmallVector<CCValAssign, 16> ArgLocs;
855 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
858 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
860 // For LLVM, in the case when returning a struct by value (>8byte),
861 // the first argument is a pointer that points to the location on caller's
862 // stack where the return value will be stored. For Hexagon, the location on
863 // caller's stack is passed only when the struct size is smaller than (and
864 // equal to) 8 bytes. If not, no address will be passed into callee and
865 // callee return the result direclty through R0/R1.
867 SmallVector<SDValue, 4> MemOps;
869 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
870 CCValAssign &VA = ArgLocs[i];
871 ISD::ArgFlagsTy Flags = Ins[i].Flags;
873 unsigned StackLocation;
876 if ( (VA.isRegLoc() && !Flags.isByVal())
877 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
878 // Arguments passed in registers
879 // 1. int, long long, ptr args that get allocated in register.
880 // 2. Large struct that gets an register to put its address in.
881 EVT RegVT = VA.getLocVT();
882 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
883 RegVT == MVT::i32 || RegVT == MVT::f32) {
885 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
886 RegInfo.addLiveIn(VA.getLocReg(), VReg);
887 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
888 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
890 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
891 RegInfo.addLiveIn(VA.getLocReg(), VReg);
892 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
896 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
897 assert (0 && "ByValSize must be bigger than 8 bytes");
900 assert(VA.isMemLoc());
902 if (Flags.isByVal()) {
903 // If it's a byval parameter, then we need to compute the
904 // "real" size, not the size of the pointer.
905 ObjSize = Flags.getByValSize();
907 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
910 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
911 // Create the frame index object for this incoming parameter...
912 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
914 // Create the SelectionDAG nodes cordl, responding to a load
915 // from this parameter.
916 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
918 if (Flags.isByVal()) {
919 // If it's a pass-by-value aggregate, then do not dereference the stack
920 // location. Instead, we should generate a reference to the stack
922 InVals.push_back(FIN);
924 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
925 MachinePointerInfo(), false, false,
932 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
935 // This will point to the next argument passed via stack.
936 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
938 CCInfo.getNextStackOffset(),
940 FuncInfo->setVarArgsFrameIndex(FrameIndex);
947 HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
948 // VASTART stores the address of the VarArgsFrameIndex slot into the
949 // memory location argument.
950 MachineFunction &MF = DAG.getMachineFunction();
951 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
952 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
953 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
954 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
955 Op.getOperand(1), MachinePointerInfo(SV), false,
959 // Creates a SPLAT instruction for a constant value VAL.
960 static SDValue createSplat(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue Val) {
961 if (VT.getSimpleVT() == MVT::v4i8)
962 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
964 if (VT.getSimpleVT() == MVT::v4i16)
965 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
970 static bool isSExtFree(SDValue N) {
971 // A sign-extend of a truncate of a sign-extend is free.
972 if (N.getOpcode() == ISD::TRUNCATE &&
973 N.getOperand(0).getOpcode() == ISD::AssertSext)
975 // We have sign-extended loads.
976 if (N.getOpcode() == ISD::LOAD)
981 SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
983 SDValue InpVal = Op.getOperand(0);
984 if (isa<ConstantSDNode>(InpVal)) {
985 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
986 return DAG.getTargetConstant(countPopulation(V), MVT::i64);
988 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
989 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
992 SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
995 SDValue LHS = Op.getOperand(0);
996 SDValue RHS = Op.getOperand(1);
997 SDValue Cmp = Op.getOperand(2);
998 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1000 EVT VT = Op.getValueType();
1001 EVT LHSVT = LHS.getValueType();
1002 EVT RHSVT = RHS.getValueType();
1004 if (LHSVT == MVT::v2i16) {
1005 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1006 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1008 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1009 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1010 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1014 // Treat all other vector types as legal.
1018 // Equals and not equals should use sign-extend, not zero-extend, since
1019 // we can represent small negative values in the compare instructions.
1020 // The LLVM default is to use zero-extend arbitrarily in these cases.
1021 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1022 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1023 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1024 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1025 if (C && C->getAPIntValue().isNegative()) {
1026 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1027 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1028 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1029 LHS, RHS, Op.getOperand(2));
1031 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1032 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1033 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1034 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1035 LHS, RHS, Op.getOperand(2));
1041 SDValue HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG)
1043 SDValue PredOp = Op.getOperand(0);
1044 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1045 EVT OpVT = Op1.getValueType();
1048 if (OpVT == MVT::v2i16) {
1049 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1050 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1051 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1052 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1059 // Handle only specific vector loads.
1060 SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1061 EVT VT = Op.getValueType();
1063 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1064 SDValue Chain = LoadNode->getChain();
1065 SDValue Ptr = Op.getOperand(1);
1066 SDValue LoweredLoad;
1068 SDValue Base = LoadNode->getBasePtr();
1069 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1070 unsigned Alignment = LoadNode->getAlignment();
1073 if(Ext == ISD::NON_EXTLOAD)
1074 Ext = ISD::ZEXTLOAD;
1076 if (VT == MVT::v4i16) {
1077 if (Alignment == 2) {
1080 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1081 LoadNode->getPointerInfo(), MVT::i16,
1082 LoadNode->isVolatile(),
1083 LoadNode->isNonTemporal(),
1084 LoadNode->isInvariant(),
1087 SDValue Increment = DAG.getConstant(2, MVT::i32);
1088 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1089 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1090 LoadNode->getPointerInfo(), MVT::i16,
1091 LoadNode->isVolatile(),
1092 LoadNode->isNonTemporal(),
1093 LoadNode->isInvariant(),
1095 // SHL 16, then OR base and base+2.
1096 SDValue ShiftAmount = DAG.getConstant(16, MVT::i32);
1097 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1098 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1100 Increment = DAG.getConstant(4, MVT::i32);
1101 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1102 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1103 LoadNode->getPointerInfo(), MVT::i16,
1104 LoadNode->isVolatile(),
1105 LoadNode->isNonTemporal(),
1106 LoadNode->isInvariant(),
1109 Increment = DAG.getConstant(6, MVT::i32);
1110 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1111 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1112 LoadNode->getPointerInfo(), MVT::i16,
1113 LoadNode->isVolatile(),
1114 LoadNode->isNonTemporal(),
1115 LoadNode->isInvariant(),
1117 // SHL 16, then OR base+4 and base+6.
1118 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1119 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1120 // Combine to i64. This could be optimised out later if we can
1121 // affect reg allocation of this code.
1122 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1123 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1124 Loads[0].getValue(1), Loads[1].getValue(1),
1125 Loads[2].getValue(1), Loads[3].getValue(1));
1127 // Perform default type expansion.
1128 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1129 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1130 LoadNode->isInvariant(), LoadNode->getAlignment());
1131 LoadChain = Result.getValue(1);
1134 llvm_unreachable("Custom lowering unsupported load");
1136 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1137 // Since we pretend to lower a load, we need the original chain
1138 // info attached to the result.
1139 SDValue Ops[] = { Result, LoadChain };
1141 return DAG.getMergeValues(Ops, DL);
1146 HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1147 EVT ValTy = Op.getValueType();
1149 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1151 if (CP->isMachineConstantPoolEntry())
1152 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
1153 CP->getAlignment());
1155 Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
1156 CP->getAlignment());
1157 return DAG.getNode(HexagonISD::CONST32, dl, ValTy, Res);
1161 HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
1162 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
1163 MachineFunction &MF = DAG.getMachineFunction();
1164 MachineFrameInfo *MFI = MF.getFrameInfo();
1165 MFI->setReturnAddressIsTaken(true);
1167 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1170 EVT VT = Op.getValueType();
1172 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1174 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1175 SDValue Offset = DAG.getConstant(4, MVT::i32);
1176 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1177 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1178 MachinePointerInfo(), false, false, false, 0);
1181 // Return LR, which contains the return address. Mark it an implicit live-in.
1182 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
1183 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1187 HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1188 const HexagonRegisterInfo *TRI = Subtarget->getRegisterInfo();
1189 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1190 MFI->setFrameAddressIsTaken(true);
1192 EVT VT = Op.getValueType();
1194 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1195 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1196 TRI->getFrameRegister(), VT);
1198 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1199 MachinePointerInfo(),
1200 false, false, false, 0);
1204 SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1205 SelectionDAG& DAG) const {
1207 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1211 SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
1212 SelectionDAG &DAG) const {
1214 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1215 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
1217 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
1219 const HexagonTargetObjectFile *TLOF =
1220 static_cast<const HexagonTargetObjectFile *>(
1221 getTargetMachine().getObjFileLowering());
1222 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine())) {
1223 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
1226 return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
1229 // Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
1230 void HexagonTargetLowering::promoteLdStType(EVT VT, EVT PromotedLdStVT) {
1231 if (VT != PromotedLdStVT) {
1232 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
1233 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(),
1234 PromotedLdStVT.getSimpleVT());
1236 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
1237 AddPromotedToType(ISD::STORE, VT.getSimpleVT(),
1238 PromotedLdStVT.getSimpleVT());
1243 HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1244 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1245 SDValue BA_SD = DAG.getTargetBlockAddress(BA, MVT::i32);
1247 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), BA_SD);
1250 //===----------------------------------------------------------------------===//
1251 // TargetLowering Implementation
1252 //===----------------------------------------------------------------------===//
1254 HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
1255 const HexagonSubtarget &STI)
1256 : TargetLowering(TM), Subtarget(&STI) {
1258 // Set up the register classes.
1259 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1260 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1261 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1262 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1263 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
1264 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
1265 promoteLdStType(MVT::v4i8, MVT::i32);
1266 promoteLdStType(MVT::v2i16, MVT::i32);
1268 if (Subtarget->hasV5TOps()) {
1269 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1270 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1273 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1274 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1275 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1276 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
1277 promoteLdStType(MVT::v8i8, MVT::i64);
1279 // Custom lower v4i16 load only. Let v4i16 store to be
1280 // promoted for now.
1281 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1282 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1283 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1284 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1285 promoteLdStType(MVT::v2i32, MVT::i64);
1287 for (unsigned i = (unsigned) MVT::FIRST_VECTOR_VALUETYPE;
1288 i <= (unsigned) MVT::LAST_VECTOR_VALUETYPE; ++i) {
1289 MVT::SimpleValueType VT = (MVT::SimpleValueType) i;
1291 // Hexagon does not have support for the following operations,
1292 // so they need to be expanded.
1293 setOperationAction(ISD::SELECT, VT, Expand);
1294 setOperationAction(ISD::SDIV, VT, Expand);
1295 setOperationAction(ISD::SREM, VT, Expand);
1296 setOperationAction(ISD::UDIV, VT, Expand);
1297 setOperationAction(ISD::UREM, VT, Expand);
1298 setOperationAction(ISD::ROTL, VT, Expand);
1299 setOperationAction(ISD::ROTR, VT, Expand);
1300 setOperationAction(ISD::FDIV, VT, Expand);
1301 setOperationAction(ISD::FNEG, VT, Expand);
1302 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
1303 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
1304 setOperationAction(ISD::UDIVREM, VT, Expand);
1305 setOperationAction(ISD::SDIVREM, VT, Expand);
1306 setOperationAction(ISD::FPOW, VT, Expand);
1307 setOperationAction(ISD::CTPOP, VT, Expand);
1308 setOperationAction(ISD::CTLZ, VT, Expand);
1309 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
1310 setOperationAction(ISD::CTTZ, VT, Expand);
1311 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
1313 // Expand all any extend loads.
1314 for (unsigned j = (unsigned) MVT::FIRST_VECTOR_VALUETYPE;
1315 j <= (unsigned) MVT::LAST_VECTOR_VALUETYPE; ++j)
1316 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType) j, VT, Expand);
1318 // Expand all trunc stores.
1319 for (unsigned TargetVT = (unsigned) MVT::FIRST_VECTOR_VALUETYPE;
1320 TargetVT <= (unsigned) MVT::LAST_VECTOR_VALUETYPE; ++TargetVT)
1321 setTruncStoreAction(VT, (MVT::SimpleValueType) TargetVT, Expand);
1323 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
1324 setOperationAction(ISD::ConstantPool, VT, Expand);
1325 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
1326 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
1327 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
1328 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
1329 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Expand);
1330 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Expand);
1331 setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
1332 setOperationAction(ISD::SRA, VT, Custom);
1333 setOperationAction(ISD::SHL, VT, Custom);
1334 setOperationAction(ISD::SRL, VT, Custom);
1336 if (!isTypeLegal(VT))
1339 setOperationAction(ISD::ADD, VT, Legal);
1340 setOperationAction(ISD::SUB, VT, Legal);
1341 setOperationAction(ISD::MUL, VT, Legal);
1343 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1344 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1345 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1346 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
1347 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1348 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
1351 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1352 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1353 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
1354 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1356 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
1358 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1360 computeRegisterProperties(Subtarget->getRegisterInfo());
1363 setPrefLoopAlignment(4);
1365 // Limits for inline expansion of memcpy/memmove
1366 MaxStoresPerMemcpy = 6;
1367 MaxStoresPerMemmove = 6;
1370 // Library calls for unsupported operations
1373 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1374 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1376 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1377 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1379 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1380 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
1382 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1383 setOperationAction(ISD::SDIV, MVT::i32, Expand);
1384 setLibcallName(RTLIB::SREM_I32, "__hexagon_umodsi3");
1385 setOperationAction(ISD::SREM, MVT::i32, Expand);
1387 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1388 setOperationAction(ISD::SDIV, MVT::i64, Expand);
1389 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1390 setOperationAction(ISD::SREM, MVT::i64, Expand);
1392 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1393 setOperationAction(ISD::UDIV, MVT::i32, Expand);
1395 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1396 setOperationAction(ISD::UDIV, MVT::i64, Expand);
1398 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1399 setOperationAction(ISD::UREM, MVT::i32, Expand);
1401 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
1402 setOperationAction(ISD::UREM, MVT::i64, Expand);
1404 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1405 setOperationAction(ISD::FDIV, MVT::f32, Expand);
1407 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1408 setOperationAction(ISD::FDIV, MVT::f64, Expand);
1410 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1411 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1412 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1414 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
1415 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
1416 setOperationAction(ISD::FSIN, MVT::f32, Expand);
1417 setOperationAction(ISD::FSIN, MVT::f64, Expand);
1419 if (Subtarget->hasV5TOps()) {
1420 // Hexagon V5 Support.
1421 setOperationAction(ISD::FADD, MVT::f32, Legal);
1422 setOperationAction(ISD::FADD, MVT::f64, Expand);
1423 setOperationAction(ISD::FSUB, MVT::f32, Legal);
1424 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1425 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1426 setOperationAction(ISD::FP_EXTEND, MVT::f32, Legal);
1427 setCondCodeAction(ISD::SETOEQ, MVT::f32, Legal);
1428 setCondCodeAction(ISD::SETOEQ, MVT::f64, Legal);
1429 setCondCodeAction(ISD::SETUEQ, MVT::f32, Legal);
1430 setCondCodeAction(ISD::SETUEQ, MVT::f64, Legal);
1432 setCondCodeAction(ISD::SETOGE, MVT::f32, Legal);
1433 setCondCodeAction(ISD::SETOGE, MVT::f64, Legal);
1434 setCondCodeAction(ISD::SETUGE, MVT::f32, Legal);
1435 setCondCodeAction(ISD::SETUGE, MVT::f64, Legal);
1437 setCondCodeAction(ISD::SETOGT, MVT::f32, Legal);
1438 setCondCodeAction(ISD::SETOGT, MVT::f64, Legal);
1439 setCondCodeAction(ISD::SETUGT, MVT::f32, Legal);
1440 setCondCodeAction(ISD::SETUGT, MVT::f64, Legal);
1442 setCondCodeAction(ISD::SETOLE, MVT::f32, Legal);
1443 setCondCodeAction(ISD::SETOLE, MVT::f64, Legal);
1444 setCondCodeAction(ISD::SETOLT, MVT::f32, Legal);
1445 setCondCodeAction(ISD::SETOLT, MVT::f64, Legal);
1447 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
1448 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
1450 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1451 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1452 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1453 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1455 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1456 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1457 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1458 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1460 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1461 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1462 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1463 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
1465 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
1466 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
1467 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
1468 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
1470 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Legal);
1471 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Legal);
1472 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Legal);
1473 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Legal);
1475 setOperationAction(ISD::FABS, MVT::f32, Legal);
1476 setOperationAction(ISD::FABS, MVT::f64, Expand);
1478 setOperationAction(ISD::FNEG, MVT::f32, Legal);
1479 setOperationAction(ISD::FNEG, MVT::f64, Expand);
1482 // Expand fp<->uint.
1483 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Expand);
1484 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
1486 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1487 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1489 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1490 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1492 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1493 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1495 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1496 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1498 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1499 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1501 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1502 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1504 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1505 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1507 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1508 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1511 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1512 setOperationAction(ISD::FADD, MVT::f32, Expand);
1513 setOperationAction(ISD::FADD, MVT::f64, Expand);
1515 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1516 setOperationAction(ISD::FSUB, MVT::f32, Expand);
1517 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1519 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1520 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1522 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1523 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
1525 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1526 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
1528 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1529 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
1531 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1532 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
1534 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1535 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
1537 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1538 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
1540 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1541 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1543 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1544 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1546 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1547 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
1549 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1550 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
1552 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1553 setCondCodeAction(ISD::SETOLT, MVT::f64, Expand);
1555 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1556 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
1558 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1560 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1561 setOperationAction(ISD::MUL, MVT::f32, Expand);
1563 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1564 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1566 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1568 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1569 setOperationAction(ISD::SUB, MVT::f64, Expand);
1571 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1572 setOperationAction(ISD::SUB, MVT::f32, Expand);
1574 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1575 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1577 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1578 setCondCodeAction(ISD::SETUO, MVT::f64, Expand);
1580 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
1581 setCondCodeAction(ISD::SETO, MVT::f64, Expand);
1583 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1584 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
1586 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1587 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
1589 setOperationAction(ISD::FABS, MVT::f32, Expand);
1590 setOperationAction(ISD::FABS, MVT::f64, Expand);
1591 setOperationAction(ISD::FNEG, MVT::f32, Expand);
1592 setOperationAction(ISD::FNEG, MVT::f64, Expand);
1595 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1596 setOperationAction(ISD::SREM, MVT::i32, Expand);
1598 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
1599 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
1600 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
1601 setIndexedLoadAction(ISD::POST_INC, MVT::i64, Legal);
1603 setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal);
1604 setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal);
1605 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
1606 setIndexedStoreAction(ISD::POST_INC, MVT::i64, Legal);
1608 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
1610 // Turn FP extload into load/fextend.
1611 for (MVT VT : MVT::fp_valuetypes())
1612 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1614 // No extending loads from i32.
1615 for (MVT VT : MVT::integer_valuetypes()) {
1616 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1617 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1618 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1621 // Turn FP truncstore into trunc + store.
1622 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1624 // Custom legalize GlobalAddress nodes into CONST32.
1625 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
1626 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1627 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
1629 setOperationAction(ISD::TRUNCATE, MVT::i64, Expand);
1631 // Hexagon doesn't have sext_inreg, replace them with shl/sra.
1632 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1634 // Hexagon has no REM or DIVREM operations.
1635 setOperationAction(ISD::UREM, MVT::i32, Expand);
1636 setOperationAction(ISD::SREM, MVT::i32, Expand);
1637 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1638 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1639 setOperationAction(ISD::SREM, MVT::i64, Expand);
1640 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1641 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1643 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
1645 // Lower SELECT_CC to SETCC and SELECT.
1646 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
1647 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
1648 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
1650 if (Subtarget->hasV5TOps()) {
1652 // We need to make the operation type of SELECT node to be Custom,
1653 // such that we don't go into the infinite loop of
1654 // select -> setcc -> select_cc -> select loop.
1655 setOperationAction(ISD::SELECT, MVT::f32, Custom);
1656 setOperationAction(ISD::SELECT, MVT::f64, Custom);
1658 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
1659 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
1663 // Hexagon has no select or setcc: expand to SELECT_CC.
1664 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1665 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1668 // Hexagon needs to optimize cases with negative constants.
1669 setOperationAction(ISD::SETCC, MVT::i16, Custom);
1670 setOperationAction(ISD::SETCC, MVT::i8, Custom);
1672 if (EmitJumpTables) {
1673 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
1675 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1677 // Increase jump tables cutover to 5, was 4.
1678 setMinimumJumpTableEntries(5);
1680 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
1681 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
1682 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
1683 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
1684 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
1686 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
1688 setOperationAction(ISD::FSIN, MVT::f64, Expand);
1689 setOperationAction(ISD::FCOS, MVT::f64, Expand);
1690 setOperationAction(ISD::FREM, MVT::f64, Expand);
1691 setOperationAction(ISD::FSIN, MVT::f32, Expand);
1692 setOperationAction(ISD::FCOS, MVT::f32, Expand);
1693 setOperationAction(ISD::FREM, MVT::f32, Expand);
1694 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1695 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
1697 // In V4, we have double word add/sub with carry. The problem with
1698 // modelling this instruction is that it produces 2 results - Rdd and Px.
1699 // To model update of Px, we will have to use Defs[p0..p3] which will
1700 // cause any predicate live range to spill. So, we pretend we dont't
1701 // have these instructions.
1702 setOperationAction(ISD::ADDE, MVT::i8, Expand);
1703 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1704 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1705 setOperationAction(ISD::ADDE, MVT::i64, Expand);
1706 setOperationAction(ISD::SUBE, MVT::i8, Expand);
1707 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1708 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1709 setOperationAction(ISD::SUBE, MVT::i64, Expand);
1710 setOperationAction(ISD::ADDC, MVT::i8, Expand);
1711 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1712 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1713 setOperationAction(ISD::ADDC, MVT::i64, Expand);
1714 setOperationAction(ISD::SUBC, MVT::i8, Expand);
1715 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1716 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1717 setOperationAction(ISD::SUBC, MVT::i64, Expand);
1719 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1720 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1721 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
1722 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
1723 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1724 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1725 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
1726 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
1727 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
1728 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1730 setOperationAction(ISD::ROTL, MVT::i32, Expand);
1731 setOperationAction(ISD::ROTR, MVT::i32, Expand);
1732 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1733 setOperationAction(ISD::ROTL, MVT::i64, Expand);
1734 setOperationAction(ISD::ROTR, MVT::i64, Expand);
1735 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1736 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1737 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
1738 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
1740 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1741 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1742 setOperationAction(ISD::FPOW, MVT::f64, Expand);
1743 setOperationAction(ISD::FPOW, MVT::f32, Expand);
1745 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1746 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1747 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
1749 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1750 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1752 setOperationAction(ISD::MULHS, MVT::i64, Expand);
1753 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1754 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1756 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1758 setExceptionPointerRegister(Hexagon::R0);
1759 setExceptionSelectorRegister(Hexagon::R1);
1761 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1762 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1764 // Use the default implementation.
1765 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1766 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
1767 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1768 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1769 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1771 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1772 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
1774 setMinFunctionAlignment(2);
1776 // Needed for DYNAMIC_STACKALLOC expansion.
1777 const HexagonRegisterInfo *QRI = Subtarget->getRegisterInfo();
1778 setStackPointerRegisterToSaveRestore(QRI->getStackRegister());
1779 setSchedulingPreference(Sched::VLIW);
1783 HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
1785 default: return nullptr;
1786 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1787 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1788 case HexagonISD::CONST32_Int_Real: return "HexagonISD::CONST32_Int_Real";
1789 case HexagonISD::ADJDYNALLOC: return "HexagonISD::ADJDYNALLOC";
1790 case HexagonISD::CMPICC: return "HexagonISD::CMPICC";
1791 case HexagonISD::CMPFCC: return "HexagonISD::CMPFCC";
1792 case HexagonISD::BRICC: return "HexagonISD::BRICC";
1793 case HexagonISD::BRFCC: return "HexagonISD::BRFCC";
1794 case HexagonISD::SELECT_ICC: return "HexagonISD::SELECT_ICC";
1795 case HexagonISD::SELECT_FCC: return "HexagonISD::SELECT_FCC";
1796 case HexagonISD::Hi: return "HexagonISD::Hi";
1797 case HexagonISD::Lo: return "HexagonISD::Lo";
1798 case HexagonISD::JT: return "HexagonISD::JT";
1799 case HexagonISD::CP: return "HexagonISD::CP";
1800 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
1801 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1802 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
1803 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
1804 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
1805 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
1806 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
1807 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
1808 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
1809 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
1810 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
1811 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
1812 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
1813 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
1814 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
1815 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
1816 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
1817 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
1818 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
1819 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
1820 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
1821 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
1822 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
1823 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
1824 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
1825 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
1826 case HexagonISD::INSERT_ri: return "HexagonISD::INSERT_ri";
1827 case HexagonISD::INSERT_rd: return "HexagonISD::INSERT_rd";
1828 case HexagonISD::INSERT_riv: return "HexagonISD::INSERT_riv";
1829 case HexagonISD::INSERT_rdv: return "HexagonISD::INSERT_rdv";
1830 case HexagonISD::EXTRACTU_ri: return "HexagonISD::EXTRACTU_ri";
1831 case HexagonISD::EXTRACTU_rd: return "HexagonISD::EXTRACTU_rd";
1832 case HexagonISD::EXTRACTU_riv: return "HexagonISD::EXTRACTU_riv";
1833 case HexagonISD::EXTRACTU_rdv: return "HexagonISD::EXTRACTU_rdv";
1834 case HexagonISD::FTOI: return "HexagonISD::FTOI";
1835 case HexagonISD::ITOF: return "HexagonISD::ITOF";
1836 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
1837 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
1838 case HexagonISD::CALLR: return "HexagonISD::CALLR";
1839 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
1840 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
1841 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
1842 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
1847 HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
1848 EVT MTy1 = EVT::getEVT(Ty1);
1849 EVT MTy2 = EVT::getEVT(Ty2);
1850 if (!MTy1.isSimple() || !MTy2.isSimple()) {
1853 return ((MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32));
1856 bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1857 if (!VT1.isSimple() || !VT2.isSimple()) {
1860 return ((VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32));
1863 // shouldExpandBuildVectorWithShuffles
1864 // Should we expand the build vector with shuffles?
1866 HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1867 unsigned DefinedValues) const {
1869 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
1870 EVT EltVT = VT.getVectorElementType();
1871 int EltBits = EltVT.getSizeInBits();
1872 if ((EltBits != 8) && (EltBits != 16))
1875 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
1878 // LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
1879 // V2 are the two vectors to select data from, V3 is the permutation.
1880 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
1881 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
1882 SDValue V1 = Op.getOperand(0);
1883 SDValue V2 = Op.getOperand(1);
1885 EVT VT = Op.getValueType();
1887 if (V2.getOpcode() == ISD::UNDEF)
1890 if (SVN->isSplat()) {
1891 int Lane = SVN->getSplatIndex();
1892 if (Lane == -1) Lane = 0;
1894 // Test if V1 is a SCALAR_TO_VECTOR.
1895 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
1896 return createSplat(DAG, dl, VT, V1.getOperand(0));
1898 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
1899 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
1901 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
1902 !isa<ConstantSDNode>(V1.getOperand(0))) {
1903 bool IsScalarToVector = true;
1904 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
1905 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
1906 IsScalarToVector = false;
1909 if (IsScalarToVector)
1910 return createSplat(DAG, dl, VT, V1.getOperand(0));
1912 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, MVT::i32));
1915 // FIXME: We need to support more general vector shuffles. See
1916 // below the comment from the ARM backend that deals in the general
1917 // case with the vector shuffles. For now, let expand handle these.
1920 // If the shuffle is not directly supported and it has 4 elements, use
1921 // the PerfectShuffle-generated table to synthesize it from other shuffles.
1924 // If BUILD_VECTOR has same base element repeated several times,
1926 static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
1927 unsigned NElts = BVN->getNumOperands();
1928 SDValue V0 = BVN->getOperand(0);
1930 for (unsigned i = 1, e = NElts; i != e; ++i) {
1931 if (BVN->getOperand(i) != V0)
1937 // LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
1938 // <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
1939 // <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
1940 static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
1941 BuildVectorSDNode *BVN = 0;
1942 SDValue V1 = Op.getOperand(0);
1943 SDValue V2 = Op.getOperand(1);
1946 EVT VT = Op.getValueType();
1948 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
1949 isCommonSplatElement(BVN))
1951 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
1952 isCommonSplatElement(BVN))
1957 SDValue CommonSplat = BVN->getOperand(0);
1960 if (VT.getSimpleVT() == MVT::v4i16) {
1961 switch (Op.getOpcode()) {
1963 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
1966 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
1969 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
1974 } else if (VT.getSimpleVT() == MVT::v2i32) {
1975 switch (Op.getOpcode()) {
1977 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
1980 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
1983 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
1992 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
1996 HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
1997 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
1999 EVT VT = Op.getValueType();
2001 unsigned Size = VT.getSizeInBits();
2003 // A vector larger than 64 bits cannot be represented in Hexagon.
2004 // Expand will split the vector.
2008 APInt APSplatBits, APSplatUndef;
2009 unsigned SplatBitSize;
2011 unsigned NElts = BVN->getNumOperands();
2013 // Try to generate a SPLAT instruction.
2014 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
2015 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2016 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
2017 unsigned SplatBits = APSplatBits.getZExtValue();
2018 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
2019 (32 - SplatBitSize));
2020 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, MVT::i32));
2023 // Try to generate COMBINE to build v2i32 vectors.
2024 if (VT.getSimpleVT() == MVT::v2i32) {
2025 SDValue V0 = BVN->getOperand(0);
2026 SDValue V1 = BVN->getOperand(1);
2028 if (V0.getOpcode() == ISD::UNDEF)
2029 V0 = DAG.getConstant(0, MVT::i32);
2030 if (V1.getOpcode() == ISD::UNDEF)
2031 V1 = DAG.getConstant(0, MVT::i32);
2033 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
2034 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
2035 // If the element isn't a constant, it is in a register:
2036 // generate a COMBINE Register Register instruction.
2038 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2040 // If one of the operands is an 8 bit integer constant, generate
2041 // a COMBINE Immediate Immediate instruction.
2042 if (isInt<8>(C0->getSExtValue()) ||
2043 isInt<8>(C1->getSExtValue()))
2044 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
2047 // Try to generate a S2_packhl to build v2i16 vectors.
2048 if (VT.getSimpleVT() == MVT::v2i16) {
2049 for (unsigned i = 0, e = NElts; i != e; ++i) {
2050 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
2052 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
2053 // If the element isn't a constant, it is in a register:
2054 // generate a S2_packhl instruction.
2056 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
2057 BVN->getOperand(1), BVN->getOperand(0));
2059 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
2065 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
2066 // and insert_vector_elt for all the other cases.
2068 unsigned EltSize = Size / NElts;
2070 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
2071 bool HasNonConstantElements = false;
2073 for (unsigned i = 0, e = NElts; i != e; ++i) {
2074 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
2075 // combine, const64, etc. are Big Endian.
2076 unsigned OpIdx = NElts - i - 1;
2077 SDValue Operand = BVN->getOperand(OpIdx);
2078 if (Operand.getOpcode() == ISD::UNDEF)
2082 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2083 Val = Cst->getSExtValue();
2085 HasNonConstantElements = true;
2088 Res = (Res << EltSize) | Val;
2092 ConstVal = DAG.getConstant(Res, MVT::i64);
2094 ConstVal = DAG.getConstant(Res, MVT::i32);
2096 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2097 // ConstVal, the constant part of the vector.
2098 if (HasNonConstantElements) {
2099 EVT EltVT = VT.getVectorElementType();
2100 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), MVT::i64);
2101 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2102 DAG.getConstant(32, MVT::i64));
2104 for (unsigned i = 0, e = NElts; i != e; ++i) {
2105 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2107 unsigned OpIdx = NElts - i - 1;
2108 SDValue Operand = BVN->getOperand(OpIdx);
2109 if (dyn_cast<ConstantSDNode>(Operand))
2110 // This operand is already in ConstVal.
2113 if (VT.getSizeInBits() == 64 &&
2114 Operand.getValueType().getSizeInBits() == 32) {
2115 SDValue C = DAG.getConstant(0, MVT::i32);
2116 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2119 SDValue Idx = DAG.getConstant(OpIdx, MVT::i64);
2120 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2121 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2122 const SDValue Ops[] = {ConstVal, Operand, Combined};
2124 if (VT.getSizeInBits() == 32)
2125 ConstVal = DAG.getNode(HexagonISD::INSERT_riv, dl, MVT::i32, Ops);
2127 ConstVal = DAG.getNode(HexagonISD::INSERT_rdv, dl, MVT::i64, Ops);
2131 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2135 HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2136 SelectionDAG &DAG) const {
2138 EVT VT = Op.getValueType();
2139 unsigned NElts = Op.getNumOperands();
2140 SDValue Vec = Op.getOperand(0);
2141 EVT VecVT = Vec.getValueType();
2142 SDValue Width = DAG.getConstant(VecVT.getSizeInBits(), MVT::i64);
2143 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2144 DAG.getConstant(32, MVT::i64));
2145 SDValue ConstVal = DAG.getConstant(0, MVT::i64);
2147 ConstantSDNode *W = dyn_cast<ConstantSDNode>(Width);
2148 ConstantSDNode *S = dyn_cast<ConstantSDNode>(Shifted);
2150 if ((VecVT.getSimpleVT() == MVT::v2i16) && (NElts == 2) && W && S) {
2151 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2152 // We are trying to concat two v2i16 to a single v4i16.
2153 SDValue Vec0 = Op.getOperand(1);
2154 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2155 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2159 if ((VecVT.getSimpleVT() == MVT::v4i8) && (NElts == 2) && W && S) {
2160 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2161 // We are trying to concat two v4i8 to a single v8i8.
2162 SDValue Vec0 = Op.getOperand(1);
2163 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2164 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2168 for (unsigned i = 0, e = NElts; i != e; ++i) {
2169 unsigned OpIdx = NElts - i - 1;
2170 SDValue Operand = Op.getOperand(OpIdx);
2172 if (VT.getSizeInBits() == 64 &&
2173 Operand.getValueType().getSizeInBits() == 32) {
2174 SDValue C = DAG.getConstant(0, MVT::i32);
2175 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2178 SDValue Idx = DAG.getConstant(OpIdx, MVT::i64);
2179 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2180 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2181 const SDValue Ops[] = {ConstVal, Operand, Combined};
2183 if (VT.getSizeInBits() == 32)
2184 ConstVal = DAG.getNode(HexagonISD::INSERT_riv, dl, MVT::i32, Ops);
2186 ConstVal = DAG.getNode(HexagonISD::INSERT_rdv, dl, MVT::i64, Ops);
2189 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2193 HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2194 SelectionDAG &DAG) const {
2195 EVT VT = Op.getValueType();
2196 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2198 SDValue Idx = Op.getOperand(1);
2199 SDValue Vec = Op.getOperand(0);
2200 EVT VecVT = Vec.getValueType();
2201 EVT EltVT = VecVT.getVectorElementType();
2202 int EltSize = EltVT.getSizeInBits();
2203 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
2204 EltSize : VTN * EltSize, MVT::i64);
2206 // Constant element number.
2207 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Idx)) {
2208 SDValue Offset = DAG.getConstant(C->getZExtValue() * EltSize, MVT::i32);
2209 const SDValue Ops[] = {Vec, Width, Offset};
2211 ConstantSDNode *W = dyn_cast<ConstantSDNode>(Width);
2212 assert(W && "Non constant width in LowerEXTRACT_VECTOR");
2215 // For certain extracts, it is a simple _hi/_lo subreg.
2216 if (VecVT.getSimpleVT() == MVT::v2i32) {
2217 // v2i32 -> i32 vselect.
2218 if (C->getZExtValue() == 0)
2219 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
2221 else if (C->getZExtValue() == 1)
2222 N = DAG.getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
2225 llvm_unreachable("Bad offset");
2226 } else if ((VecVT.getSimpleVT() == MVT::v4i16) &&
2227 (W->getZExtValue() == 32)) {
2228 // v4i16 -> v2i16/i32 vselect.
2229 if (C->getZExtValue() == 0)
2230 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
2232 else if (C->getZExtValue() == 2)
2233 N = DAG.getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
2236 llvm_unreachable("Bad offset");
2237 } else if ((VecVT.getSimpleVT() == MVT::v8i8) &&
2238 (W->getZExtValue() == 32)) {
2239 // v8i8 -> v4i8/i32 vselect.
2240 if (C->getZExtValue() == 0)
2241 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
2243 else if (C->getZExtValue() == 4)
2244 N = DAG.getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
2247 llvm_unreachable("Bad offset");
2248 } else if (VecVT.getSizeInBits() == 32) {
2249 N = DAG.getNode(HexagonISD::EXTRACTU_ri, dl, MVT::i32, Ops);
2251 N = DAG.getNode(HexagonISD::EXTRACTU_rd, dl, MVT::i64, Ops);
2252 if (VT.getSizeInBits() == 32)
2253 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2256 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2259 // Variable element number.
2260 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
2261 DAG.getConstant(EltSize, MVT::i32));
2262 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2263 DAG.getConstant(32, MVT::i64));
2264 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2266 const SDValue Ops[] = {Vec, Combined};
2269 if (VecVT.getSizeInBits() == 32) {
2270 N = DAG.getNode(HexagonISD::EXTRACTU_riv, dl, MVT::i32, Ops);
2272 N = DAG.getNode(HexagonISD::EXTRACTU_rdv, dl, MVT::i64, Ops);
2273 if (VT.getSizeInBits() == 32)
2274 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2276 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2280 HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2281 SelectionDAG &DAG) const {
2282 EVT VT = Op.getValueType();
2283 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2285 SDValue Vec = Op.getOperand(0);
2286 SDValue Val = Op.getOperand(1);
2287 SDValue Idx = Op.getOperand(2);
2288 EVT VecVT = Vec.getValueType();
2289 EVT EltVT = VecVT.getVectorElementType();
2290 int EltSize = EltVT.getSizeInBits();
2291 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
2292 EltSize : VTN * EltSize, MVT::i64);
2294 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
2295 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, MVT::i32);
2296 const SDValue Ops[] = {Vec, Val, Width, Offset};
2299 if (VT.getSizeInBits() == 32)
2300 N = DAG.getNode(HexagonISD::INSERT_ri, dl, MVT::i32, Ops);
2302 N = DAG.getNode(HexagonISD::INSERT_rd, dl, MVT::i64, Ops);
2304 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2307 // Variable element number.
2308 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
2309 DAG.getConstant(EltSize, MVT::i32));
2310 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2311 DAG.getConstant(32, MVT::i64));
2312 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2314 if (VT.getSizeInBits() == 64 &&
2315 Val.getValueType().getSizeInBits() == 32) {
2316 SDValue C = DAG.getConstant(0, MVT::i32);
2317 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2320 const SDValue Ops[] = {Vec, Val, Combined};
2323 if (VT.getSizeInBits() == 32)
2324 N = DAG.getNode(HexagonISD::INSERT_riv, dl, MVT::i32, Ops);
2326 N = DAG.getNode(HexagonISD::INSERT_rdv, dl, MVT::i64, Ops);
2328 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2332 HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2333 // Assuming the caller does not have either a signext or zeroext modifier, and
2334 // only one value is accepted, any reasonable truncation is allowed.
2335 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2338 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2339 // fragile at the moment: any support for multiple value returns would be
2340 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2341 return Ty1->getPrimitiveSizeInBits() <= 32;
2345 HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2346 SDValue Chain = Op.getOperand(0);
2347 SDValue Offset = Op.getOperand(1);
2348 SDValue Handler = Op.getOperand(2);
2351 // Mark function as containing a call to EH_RETURN.
2352 HexagonMachineFunctionInfo *FuncInfo =
2353 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2354 FuncInfo->setHasEHReturn();
2356 unsigned OffsetReg = Hexagon::R28;
2358 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(),
2359 DAG.getRegister(Hexagon::R30, getPointerTy()),
2360 DAG.getIntPtrConstant(4));
2361 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2363 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2365 // Not needed we already use it as explict input to EH_RETURN.
2366 // MF.getRegInfo().addLiveOut(OffsetReg);
2368 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2372 HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
2373 switch (Op.getOpcode()) {
2374 default: llvm_unreachable("Should not custom lower this!");
2375 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2376 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2377 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2378 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2379 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2380 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2381 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2385 return LowerVECTOR_SHIFT(Op, DAG);
2386 case ISD::ConstantPool:
2387 return LowerConstantPool(Op, DAG);
2388 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2389 // Frame & Return address. Currently unimplemented.
2390 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2391 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2392 case ISD::GlobalTLSAddress:
2393 llvm_unreachable("TLS not implemented for Hexagon.");
2394 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2395 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2396 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2397 case ISD::VASTART: return LowerVASTART(Op, DAG);
2398 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
2399 // Custom lower some vector loads.
2400 case ISD::LOAD: return LowerLOAD(Op, DAG);
2402 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2403 case ISD::SELECT: return Op;
2404 case ISD::SETCC: return LowerSETCC(Op, DAG);
2405 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2406 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2407 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2408 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
2415 //===----------------------------------------------------------------------===//
2416 // Hexagon Scheduler Hooks
2417 //===----------------------------------------------------------------------===//
2419 HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2420 MachineBasicBlock *BB)
2422 switch (MI->getOpcode()) {
2423 case Hexagon::ADJDYNALLOC: {
2424 MachineFunction *MF = BB->getParent();
2425 HexagonMachineFunctionInfo *FuncInfo =
2426 MF->getInfo<HexagonMachineFunctionInfo>();
2427 FuncInfo->addAllocaAdjustInst(MI);
2430 default: llvm_unreachable("Unexpected instr type to insert");
2434 //===----------------------------------------------------------------------===//
2435 // Inline Assembly Support
2436 //===----------------------------------------------------------------------===//
2438 std::pair<unsigned, const TargetRegisterClass *>
2439 HexagonTargetLowering::getRegForInlineAsmConstraint(
2440 const TargetRegisterInfo *TRI, const std::string &Constraint,
2442 if (Constraint.size() == 1) {
2443 switch (Constraint[0]) {
2445 switch (VT.SimpleTy) {
2447 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2452 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
2455 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
2458 llvm_unreachable("Unknown asm register class");
2462 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2465 /// isFPImmLegal - Returns true if the target can instruction select the
2466 /// specified FP immediate natively. If false, the legalizer will
2467 /// materialize the FP immediate as a load from a constant pool.
2468 bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2469 return Subtarget->hasV5TOps();
2472 /// isLegalAddressingMode - Return true if the addressing mode represented by
2473 /// AM is legal for this target, for a load/store of the specified type.
2474 bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
2476 // Allows a signed-extended 11-bit immediate field.
2477 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1) {
2481 // No global is ever allowed as a base.
2486 int Scale = AM.Scale;
2487 if (Scale < 0) Scale = -Scale;
2489 case 0: // No scale reg, "r+i", "r", or just "i".
2491 default: // No scaled addressing mode.
2497 /// isLegalICmpImmediate - Return true if the specified immediate is legal
2498 /// icmp immediate, that is the target has icmp instructions which can compare
2499 /// a register against the immediate without having to materialize the
2500 /// immediate into a register.
2501 bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2502 return Imm >= -512 && Imm <= 511;
2505 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
2506 /// for tail call optimization. Targets which want to do tail call
2507 /// optimization should implement this function.
2508 bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2510 CallingConv::ID CalleeCC,
2512 bool isCalleeStructRet,
2513 bool isCallerStructRet,
2514 const SmallVectorImpl<ISD::OutputArg> &Outs,
2515 const SmallVectorImpl<SDValue> &OutVals,
2516 const SmallVectorImpl<ISD::InputArg> &Ins,
2517 SelectionDAG& DAG) const {
2518 const Function *CallerF = DAG.getMachineFunction().getFunction();
2519 CallingConv::ID CallerCC = CallerF->getCallingConv();
2520 bool CCMatch = CallerCC == CalleeCC;
2522 // ***************************************************************************
2523 // Look for obvious safe cases to perform tail call optimization that do not
2524 // require ABI changes.
2525 // ***************************************************************************
2527 // If this is a tail call via a function pointer, then don't do it!
2528 if (!(dyn_cast<GlobalAddressSDNode>(Callee))
2529 && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
2533 // Do not optimize if the calling conventions do not match.
2537 // Do not tail call optimize vararg calls.
2541 // Also avoid tail call optimization if either caller or callee uses struct
2542 // return semantics.
2543 if (isCalleeStructRet || isCallerStructRet)
2546 // In addition to the cases above, we also disable Tail Call Optimization if
2547 // the calling convention code that at least one outgoing argument needs to
2548 // go on the stack. We cannot check that here because at this point that
2549 // information is not available.
2553 // Return true when the given node fits in a positive half word.
2554 bool llvm::isPositiveHalfWord(SDNode *N) {
2555 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
2556 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
2559 switch (N->getOpcode()) {
2562 case ISD::SIGN_EXTEND_INREG: