xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the interfaces that Hexagon uses to lower LLVM code
100b57cec5SDimitry Andric // into a selection DAG.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "HexagonISelLowering.h"
150b57cec5SDimitry Andric #include "Hexagon.h"
160b57cec5SDimitry Andric #include "HexagonMachineFunctionInfo.h"
170b57cec5SDimitry Andric #include "HexagonRegisterInfo.h"
180b57cec5SDimitry Andric #include "HexagonSubtarget.h"
190b57cec5SDimitry Andric #include "HexagonTargetMachine.h"
200b57cec5SDimitry Andric #include "HexagonTargetObjectFile.h"
210b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
220b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
240b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/CallingConvLower.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetCallingConv.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
340b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
350b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
360b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
370b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
380b57cec5SDimitry Andric #include "llvm/IR/Function.h"
390b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
400b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
410b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
420b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
430b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
440b57cec5SDimitry Andric #include "llvm/IR/Module.h"
450b57cec5SDimitry Andric #include "llvm/IR/Type.h"
460b57cec5SDimitry Andric #include "llvm/IR/Value.h"
470b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
480b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
490b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
500b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
510b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
520b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
530b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
540b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
550b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
560b57cec5SDimitry Andric #include <algorithm>
570b57cec5SDimitry Andric #include <cassert>
580b57cec5SDimitry Andric #include <cstddef>
590b57cec5SDimitry Andric #include <cstdint>
600b57cec5SDimitry Andric #include <limits>
610b57cec5SDimitry Andric #include <utility>
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric using namespace llvm;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric #define DEBUG_TYPE "hexagon-lowering"
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
680b57cec5SDimitry Andric   cl::init(true), cl::Hidden,
690b57cec5SDimitry Andric   cl::desc("Control jump table emission on Hexagon target"));
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
720b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(false),
730b57cec5SDimitry Andric   cl::desc("Enable Hexagon SDNode scheduling"));
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric static cl::opt<bool> EnableFastMath("ffast-math",
760b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(false),
770b57cec5SDimitry Andric   cl::desc("Enable Fast Math processing"));
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
800b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(5),
810b57cec5SDimitry Andric   cl::desc("Set minimum jump tables"));
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
840b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(6),
850b57cec5SDimitry Andric   cl::desc("Max #stores to inline memcpy"));
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
880b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(4),
890b57cec5SDimitry Andric   cl::desc("Max #stores to inline memcpy"));
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
920b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(6),
930b57cec5SDimitry Andric   cl::desc("Max #stores to inline memmove"));
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
960b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(4),
970b57cec5SDimitry Andric   cl::desc("Max #stores to inline memmove"));
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
1000b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(8),
1010b57cec5SDimitry Andric   cl::desc("Max #stores to inline memset"));
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
1040b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::init(4),
1050b57cec5SDimitry Andric   cl::desc("Max #stores to inline memset"));
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric static cl::opt<bool> AlignLoads("hexagon-align-loads",
1080b57cec5SDimitry Andric   cl::Hidden, cl::init(false),
1090b57cec5SDimitry Andric   cl::desc("Rewrite unaligned loads as a pair of aligned loads"));
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric namespace {
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   class HexagonCCState : public CCState {
1150b57cec5SDimitry Andric     unsigned NumNamedVarArgParams = 0;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   public:
1180b57cec5SDimitry Andric     HexagonCCState(CallingConv::ID CC, bool IsVarArg, MachineFunction &MF,
1190b57cec5SDimitry Andric                    SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
1200b57cec5SDimitry Andric                    unsigned NumNamedArgs)
1210b57cec5SDimitry Andric         : CCState(CC, IsVarArg, MF, locs, C),
1220b57cec5SDimitry Andric           NumNamedVarArgParams(NumNamedArgs) {}
1230b57cec5SDimitry Andric     unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
1240b57cec5SDimitry Andric   };
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric } // end anonymous namespace
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric // Implement calling convention for Hexagon.
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric static bool CC_SkipOdd(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1320b57cec5SDimitry Andric                        CCValAssign::LocInfo &LocInfo,
1330b57cec5SDimitry Andric                        ISD::ArgFlagsTy &ArgFlags, CCState &State) {
1340b57cec5SDimitry Andric   static const MCPhysReg ArgRegs[] = {
1350b57cec5SDimitry Andric     Hexagon::R0, Hexagon::R1, Hexagon::R2,
1360b57cec5SDimitry Andric     Hexagon::R3, Hexagon::R4, Hexagon::R5
1370b57cec5SDimitry Andric   };
1380b57cec5SDimitry Andric   const unsigned NumArgRegs = array_lengthof(ArgRegs);
1390b57cec5SDimitry Andric   unsigned RegNum = State.getFirstUnallocated(ArgRegs);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   // RegNum is an index into ArgRegs: skip a register if RegNum is odd.
1420b57cec5SDimitry Andric   if (RegNum != NumArgRegs && RegNum % 2 == 1)
1430b57cec5SDimitry Andric     State.AllocateReg(ArgRegs[RegNum]);
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   // Always return false here, as this function only makes sure that the first
1460b57cec5SDimitry Andric   // unallocated register has an even register number and does not actually
1470b57cec5SDimitry Andric   // allocate a register for the current argument.
1480b57cec5SDimitry Andric   return false;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric #include "HexagonGenCallingConv.inc"
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric SDValue
1550b57cec5SDimitry Andric HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
1560b57cec5SDimitry Andric       const {
1570b57cec5SDimitry Andric   return SDValue();
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1610b57cec5SDimitry Andric /// by "Src" to address "Dst" of size "Size".  Alignment information is
1620b57cec5SDimitry Andric /// specified by the specific parameter attribute. The copy will be passed as
1630b57cec5SDimitry Andric /// a byval function parameter.  Sometimes what we are copying is the end of a
1640b57cec5SDimitry Andric /// larger object, the part that does not fit in registers.
1650b57cec5SDimitry Andric static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
1660b57cec5SDimitry Andric                                          SDValue Chain, ISD::ArgFlagsTy Flags,
1670b57cec5SDimitry Andric                                          SelectionDAG &DAG, const SDLoc &dl) {
1680b57cec5SDimitry Andric   SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
1690b57cec5SDimitry Andric   return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
1700b57cec5SDimitry Andric                        /*isVolatile=*/false, /*AlwaysInline=*/false,
1710b57cec5SDimitry Andric                        /*isTailCall=*/false,
1720b57cec5SDimitry Andric                        MachinePointerInfo(), MachinePointerInfo());
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric bool
1760b57cec5SDimitry Andric HexagonTargetLowering::CanLowerReturn(
1770b57cec5SDimitry Andric     CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
1780b57cec5SDimitry Andric     const SmallVectorImpl<ISD::OutputArg> &Outs,
1790b57cec5SDimitry Andric     LLVMContext &Context) const {
1800b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RVLocs;
1810b57cec5SDimitry Andric   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   if (MF.getSubtarget<HexagonSubtarget>().useHVXOps())
1840b57cec5SDimitry Andric     return CCInfo.CheckReturn(Outs, RetCC_Hexagon_HVX);
1850b57cec5SDimitry Andric   return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric // LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
1890b57cec5SDimitry Andric // passed by value, the function prototype is modified to return void and
1900b57cec5SDimitry Andric // the value is stored in memory pointed by a pointer passed by caller.
1910b57cec5SDimitry Andric SDValue
1920b57cec5SDimitry Andric HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1930b57cec5SDimitry Andric                                    bool IsVarArg,
1940b57cec5SDimitry Andric                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
1950b57cec5SDimitry Andric                                    const SmallVectorImpl<SDValue> &OutVals,
1960b57cec5SDimitry Andric                                    const SDLoc &dl, SelectionDAG &DAG) const {
1970b57cec5SDimitry Andric   // CCValAssign - represent the assignment of the return value to locations.
1980b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RVLocs;
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   // CCState - Info about the registers and stack slot.
2010b57cec5SDimitry Andric   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2020b57cec5SDimitry Andric                  *DAG.getContext());
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   // Analyze return values of ISD::RET
2050b57cec5SDimitry Andric   if (Subtarget.useHVXOps())
2060b57cec5SDimitry Andric     CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon_HVX);
2070b57cec5SDimitry Andric   else
2080b57cec5SDimitry Andric     CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   SDValue Flag;
2110b57cec5SDimitry Andric   SmallVector<SDValue, 4> RetOps(1, Chain);
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   // Copy the result values into the output registers.
2140b57cec5SDimitry Andric   for (unsigned i = 0; i != RVLocs.size(); ++i) {
2150b57cec5SDimitry Andric     CCValAssign &VA = RVLocs[i];
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric     // Guarantee that all emitted copies are stuck together with flags.
2200b57cec5SDimitry Andric     Flag = Chain.getValue(1);
2210b57cec5SDimitry Andric     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   RetOps[0] = Chain;  // Update chain.
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   // Add the flag if we have it.
2270b57cec5SDimitry Andric   if (Flag.getNode())
2280b57cec5SDimitry Andric     RetOps.push_back(Flag);
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
2340b57cec5SDimitry Andric   // If either no tail call or told not to tail call at all, don't.
2350b57cec5SDimitry Andric   auto Attr =
2360b57cec5SDimitry Andric       CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2370b57cec5SDimitry Andric   if (!CI->isTailCall() || Attr.getValueAsString() == "true")
2380b57cec5SDimitry Andric     return false;
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   return true;
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
243*8bcb0991SDimitry Andric Register HexagonTargetLowering::getRegisterByName(const char* RegName, EVT VT,
244*8bcb0991SDimitry Andric                                                   const MachineFunction &) const {
2450b57cec5SDimitry Andric   // Just support r19, the linux kernel uses it.
246*8bcb0991SDimitry Andric   Register Reg = StringSwitch<Register>(RegName)
2470b57cec5SDimitry Andric                      .Case("r19", Hexagon::R19)
248*8bcb0991SDimitry Andric                      .Default(Register());
2490b57cec5SDimitry Andric   if (Reg)
2500b57cec5SDimitry Andric     return Reg;
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   report_fatal_error("Invalid register name global variable");
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric /// LowerCallResult - Lower the result values of an ISD::CALL into the
2560b57cec5SDimitry Andric /// appropriate copies out of appropriate physical registers.  This assumes that
2570b57cec5SDimitry Andric /// Chain/Glue are the input chain/glue to use, and that TheCall is the call
2580b57cec5SDimitry Andric /// being lowered. Returns a SDNode with the same number of values as the
2590b57cec5SDimitry Andric /// ISD::CALL.
2600b57cec5SDimitry Andric SDValue HexagonTargetLowering::LowerCallResult(
2610b57cec5SDimitry Andric     SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool IsVarArg,
2620b57cec5SDimitry Andric     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
2630b57cec5SDimitry Andric     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
2640b57cec5SDimitry Andric     const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
2650b57cec5SDimitry Andric   // Assign locations to each value returned by this call.
2660b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RVLocs;
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2690b57cec5SDimitry Andric                  *DAG.getContext());
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   if (Subtarget.useHVXOps())
2720b57cec5SDimitry Andric     CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon_HVX);
2730b57cec5SDimitry Andric   else
2740b57cec5SDimitry Andric     CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   // Copy all of the result registers out of their specified physreg.
2770b57cec5SDimitry Andric   for (unsigned i = 0; i != RVLocs.size(); ++i) {
2780b57cec5SDimitry Andric     SDValue RetVal;
2790b57cec5SDimitry Andric     if (RVLocs[i].getValVT() == MVT::i1) {
2800b57cec5SDimitry Andric       // Return values of type MVT::i1 require special handling. The reason
2810b57cec5SDimitry Andric       // is that MVT::i1 is associated with the PredRegs register class, but
2820b57cec5SDimitry Andric       // values of that type are still returned in R0. Generate an explicit
2830b57cec5SDimitry Andric       // copy into a predicate register from R0, and treat the value of the
2840b57cec5SDimitry Andric       // predicate register as the call result.
2850b57cec5SDimitry Andric       auto &MRI = DAG.getMachineFunction().getRegInfo();
2860b57cec5SDimitry Andric       SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
2870b57cec5SDimitry Andric                                        MVT::i32, Glue);
2880b57cec5SDimitry Andric       // FR0 = (Value, Chain, Glue)
289*8bcb0991SDimitry Andric       Register PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
2900b57cec5SDimitry Andric       SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
2910b57cec5SDimitry Andric                                      FR0.getValue(0), FR0.getValue(2));
2920b57cec5SDimitry Andric       // TPR = (Chain, Glue)
2930b57cec5SDimitry Andric       // Don't glue this CopyFromReg, because it copies from a virtual
2940b57cec5SDimitry Andric       // register. If it is glued to the call, InstrEmitter will add it
2950b57cec5SDimitry Andric       // as an implicit def to the call (EmitMachineNode).
2960b57cec5SDimitry Andric       RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
2970b57cec5SDimitry Andric       Glue = TPR.getValue(1);
2980b57cec5SDimitry Andric       Chain = TPR.getValue(0);
2990b57cec5SDimitry Andric     } else {
3000b57cec5SDimitry Andric       RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
3010b57cec5SDimitry Andric                                   RVLocs[i].getValVT(), Glue);
3020b57cec5SDimitry Andric       Glue = RetVal.getValue(2);
3030b57cec5SDimitry Andric       Chain = RetVal.getValue(1);
3040b57cec5SDimitry Andric     }
3050b57cec5SDimitry Andric     InVals.push_back(RetVal.getValue(0));
3060b57cec5SDimitry Andric   }
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   return Chain;
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric /// LowerCall - Functions arguments are copied from virtual regs to
3120b57cec5SDimitry Andric /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
3130b57cec5SDimitry Andric SDValue
3140b57cec5SDimitry Andric HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3150b57cec5SDimitry Andric                                  SmallVectorImpl<SDValue> &InVals) const {
3160b57cec5SDimitry Andric   SelectionDAG &DAG                     = CLI.DAG;
3170b57cec5SDimitry Andric   SDLoc &dl                             = CLI.DL;
3180b57cec5SDimitry Andric   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3190b57cec5SDimitry Andric   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
3200b57cec5SDimitry Andric   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
3210b57cec5SDimitry Andric   SDValue Chain                         = CLI.Chain;
3220b57cec5SDimitry Andric   SDValue Callee                        = CLI.Callee;
3230b57cec5SDimitry Andric   CallingConv::ID CallConv              = CLI.CallConv;
3240b57cec5SDimitry Andric   bool IsVarArg                         = CLI.IsVarArg;
3250b57cec5SDimitry Andric   bool DoesNotReturn                    = CLI.DoesNotReturn;
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   bool IsStructRet    = Outs.empty() ? false : Outs[0].Flags.isSRet();
3280b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
3290b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
3300b57cec5SDimitry Andric   auto PtrVT = getPointerTy(MF.getDataLayout());
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   unsigned NumParams = CLI.CS.getInstruction()
3330b57cec5SDimitry Andric                         ? CLI.CS.getFunctionType()->getNumParams()
3340b57cec5SDimitry Andric                         : 0;
3350b57cec5SDimitry Andric   if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee))
3360b57cec5SDimitry Andric     Callee = DAG.getTargetGlobalAddress(GAN->getGlobal(), dl, MVT::i32);
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   // Analyze operands of the call, assigning locations to each operand.
3390b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> ArgLocs;
3400b57cec5SDimitry Andric   HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
3410b57cec5SDimitry Andric                         NumParams);
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   if (Subtarget.useHVXOps())
3440b57cec5SDimitry Andric     CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_HVX);
3450b57cec5SDimitry Andric   else
3460b57cec5SDimitry Andric     CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
3490b57cec5SDimitry Andric   if (Attr.getValueAsString() == "true")
3500b57cec5SDimitry Andric     CLI.IsTailCall = false;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   if (CLI.IsTailCall) {
3530b57cec5SDimitry Andric     bool StructAttrFlag = MF.getFunction().hasStructRetAttr();
3540b57cec5SDimitry Andric     CLI.IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
3550b57cec5SDimitry Andric                         IsVarArg, IsStructRet, StructAttrFlag, Outs,
3560b57cec5SDimitry Andric                         OutVals, Ins, DAG);
3570b57cec5SDimitry Andric     for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3580b57cec5SDimitry Andric       CCValAssign &VA = ArgLocs[i];
3590b57cec5SDimitry Andric       if (VA.isMemLoc()) {
3600b57cec5SDimitry Andric         CLI.IsTailCall = false;
3610b57cec5SDimitry Andric         break;
3620b57cec5SDimitry Andric       }
3630b57cec5SDimitry Andric     }
3640b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << (CLI.IsTailCall ? "Eligible for Tail Call\n"
3650b57cec5SDimitry Andric                                          : "Argument must be passed on stack. "
3660b57cec5SDimitry Andric                                            "Not eligible for Tail Call\n"));
3670b57cec5SDimitry Andric   }
3680b57cec5SDimitry Andric   // Get a count of how many bytes are to be pushed on the stack.
3690b57cec5SDimitry Andric   unsigned NumBytes = CCInfo.getNextStackOffset();
3700b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
3710b57cec5SDimitry Andric   SmallVector<SDValue, 8> MemOpChains;
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
3740b57cec5SDimitry Andric   SDValue StackPtr =
3750b57cec5SDimitry Andric       DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   bool NeedsArgAlign = false;
3780b57cec5SDimitry Andric   unsigned LargestAlignSeen = 0;
3790b57cec5SDimitry Andric   // Walk the register/memloc assignments, inserting copies/loads.
3800b57cec5SDimitry Andric   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3810b57cec5SDimitry Andric     CCValAssign &VA = ArgLocs[i];
3820b57cec5SDimitry Andric     SDValue Arg = OutVals[i];
3830b57cec5SDimitry Andric     ISD::ArgFlagsTy Flags = Outs[i].Flags;
3840b57cec5SDimitry Andric     // Record if we need > 8 byte alignment on an argument.
3850b57cec5SDimitry Andric     bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
3860b57cec5SDimitry Andric     NeedsArgAlign |= ArgAlign;
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric     // Promote the value if needed.
3890b57cec5SDimitry Andric     switch (VA.getLocInfo()) {
3900b57cec5SDimitry Andric       default:
3910b57cec5SDimitry Andric         // Loc info must be one of Full, BCvt, SExt, ZExt, or AExt.
3920b57cec5SDimitry Andric         llvm_unreachable("Unknown loc info!");
3930b57cec5SDimitry Andric       case CCValAssign::Full:
3940b57cec5SDimitry Andric         break;
3950b57cec5SDimitry Andric       case CCValAssign::BCvt:
3960b57cec5SDimitry Andric         Arg = DAG.getBitcast(VA.getLocVT(), Arg);
3970b57cec5SDimitry Andric         break;
3980b57cec5SDimitry Andric       case CCValAssign::SExt:
3990b57cec5SDimitry Andric         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4000b57cec5SDimitry Andric         break;
4010b57cec5SDimitry Andric       case CCValAssign::ZExt:
4020b57cec5SDimitry Andric         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4030b57cec5SDimitry Andric         break;
4040b57cec5SDimitry Andric       case CCValAssign::AExt:
4050b57cec5SDimitry Andric         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4060b57cec5SDimitry Andric         break;
4070b57cec5SDimitry Andric     }
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric     if (VA.isMemLoc()) {
4100b57cec5SDimitry Andric       unsigned LocMemOffset = VA.getLocMemOffset();
4110b57cec5SDimitry Andric       SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
4120b57cec5SDimitry Andric                                         StackPtr.getValueType());
4130b57cec5SDimitry Andric       MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
4140b57cec5SDimitry Andric       if (ArgAlign)
4150b57cec5SDimitry Andric         LargestAlignSeen = std::max(LargestAlignSeen,
4160b57cec5SDimitry Andric                                     VA.getLocVT().getStoreSizeInBits() >> 3);
4170b57cec5SDimitry Andric       if (Flags.isByVal()) {
4180b57cec5SDimitry Andric         // The argument is a struct passed by value. According to LLVM, "Arg"
4190b57cec5SDimitry Andric         // is a pointer.
4200b57cec5SDimitry Andric         MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
4210b57cec5SDimitry Andric                                                         Flags, DAG, dl));
4220b57cec5SDimitry Andric       } else {
4230b57cec5SDimitry Andric         MachinePointerInfo LocPI = MachinePointerInfo::getStack(
4240b57cec5SDimitry Andric             DAG.getMachineFunction(), LocMemOffset);
4250b57cec5SDimitry Andric         SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
4260b57cec5SDimitry Andric         MemOpChains.push_back(S);
4270b57cec5SDimitry Andric       }
4280b57cec5SDimitry Andric       continue;
4290b57cec5SDimitry Andric     }
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric     // Arguments that can be passed on register must be kept at RegsToPass
4320b57cec5SDimitry Andric     // vector.
4330b57cec5SDimitry Andric     if (VA.isRegLoc())
4340b57cec5SDimitry Andric       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4350b57cec5SDimitry Andric   }
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   if (NeedsArgAlign && Subtarget.hasV60Ops()) {
4380b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
4390b57cec5SDimitry Andric     unsigned VecAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
4400b57cec5SDimitry Andric     LargestAlignSeen = std::max(LargestAlignSeen, VecAlign);
4410b57cec5SDimitry Andric     MFI.ensureMaxAlignment(LargestAlignSeen);
4420b57cec5SDimitry Andric   }
4430b57cec5SDimitry Andric   // Transform all store nodes into one single node because all store
4440b57cec5SDimitry Andric   // nodes are independent of each other.
4450b57cec5SDimitry Andric   if (!MemOpChains.empty())
4460b57cec5SDimitry Andric     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   SDValue Glue;
4490b57cec5SDimitry Andric   if (!CLI.IsTailCall) {
4500b57cec5SDimitry Andric     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
4510b57cec5SDimitry Andric     Glue = Chain.getValue(1);
4520b57cec5SDimitry Andric   }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric   // Build a sequence of copy-to-reg nodes chained together with token
4550b57cec5SDimitry Andric   // chain and flag operands which copy the outgoing args into registers.
4560b57cec5SDimitry Andric   // The Glue is necessary since all emitted instructions must be
4570b57cec5SDimitry Andric   // stuck together.
4580b57cec5SDimitry Andric   if (!CLI.IsTailCall) {
4590b57cec5SDimitry Andric     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4600b57cec5SDimitry Andric       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4610b57cec5SDimitry Andric                                RegsToPass[i].second, Glue);
4620b57cec5SDimitry Andric       Glue = Chain.getValue(1);
4630b57cec5SDimitry Andric     }
4640b57cec5SDimitry Andric   } else {
4650b57cec5SDimitry Andric     // For tail calls lower the arguments to the 'real' stack slot.
4660b57cec5SDimitry Andric     //
4670b57cec5SDimitry Andric     // Force all the incoming stack arguments to be loaded from the stack
4680b57cec5SDimitry Andric     // before any new outgoing arguments are stored to the stack, because the
4690b57cec5SDimitry Andric     // outgoing stack slots may alias the incoming argument stack slots, and
4700b57cec5SDimitry Andric     // the alias isn't otherwise explicit. This is slightly more conservative
4710b57cec5SDimitry Andric     // than necessary, because it means that each store effectively depends
4720b57cec5SDimitry Andric     // on every argument instead of just those arguments it would clobber.
4730b57cec5SDimitry Andric     //
4740b57cec5SDimitry Andric     // Do not flag preceding copytoreg stuff together with the following stuff.
4750b57cec5SDimitry Andric     Glue = SDValue();
4760b57cec5SDimitry Andric     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4770b57cec5SDimitry Andric       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4780b57cec5SDimitry Andric                                RegsToPass[i].second, Glue);
4790b57cec5SDimitry Andric       Glue = Chain.getValue(1);
4800b57cec5SDimitry Andric     }
4810b57cec5SDimitry Andric     Glue = SDValue();
4820b57cec5SDimitry Andric   }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric   bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
4850b57cec5SDimitry Andric   unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
4880b57cec5SDimitry Andric   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
4890b57cec5SDimitry Andric   // node so that legalize doesn't hack it.
4900b57cec5SDimitry Andric   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
4910b57cec5SDimitry Andric     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
4920b57cec5SDimitry Andric   } else if (ExternalSymbolSDNode *S =
4930b57cec5SDimitry Andric              dyn_cast<ExternalSymbolSDNode>(Callee)) {
4940b57cec5SDimitry Andric     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
4950b57cec5SDimitry Andric   }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   // Returns a chain & a flag for retval copy to use.
4980b57cec5SDimitry Andric   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
4990b57cec5SDimitry Andric   SmallVector<SDValue, 8> Ops;
5000b57cec5SDimitry Andric   Ops.push_back(Chain);
5010b57cec5SDimitry Andric   Ops.push_back(Callee);
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   // Add argument registers to the end of the list so that they are
5040b57cec5SDimitry Andric   // known live into the call.
5050b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
5060b57cec5SDimitry Andric     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
5070b57cec5SDimitry Andric                                   RegsToPass[i].second.getValueType()));
5080b57cec5SDimitry Andric   }
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
5110b57cec5SDimitry Andric   assert(Mask && "Missing call preserved mask for calling convention");
5120b57cec5SDimitry Andric   Ops.push_back(DAG.getRegisterMask(Mask));
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric   if (Glue.getNode())
5150b57cec5SDimitry Andric     Ops.push_back(Glue);
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   if (CLI.IsTailCall) {
5180b57cec5SDimitry Andric     MFI.setHasTailCall();
5190b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
5200b57cec5SDimitry Andric   }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   // Set this here because we need to know this for "hasFP" in frame lowering.
5230b57cec5SDimitry Andric   // The target-independent code calls getFrameRegister before setting it, and
5240b57cec5SDimitry Andric   // getFrameRegister uses hasFP to determine whether the function has FP.
5250b57cec5SDimitry Andric   MFI.setHasCalls(true);
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
5280b57cec5SDimitry Andric   Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
5290b57cec5SDimitry Andric   Glue = Chain.getValue(1);
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric   // Create the CALLSEQ_END node.
5320b57cec5SDimitry Andric   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
5330b57cec5SDimitry Andric                              DAG.getIntPtrConstant(0, dl, true), Glue, dl);
5340b57cec5SDimitry Andric   Glue = Chain.getValue(1);
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   // Handle result values, copying them out of physregs into vregs that we
5370b57cec5SDimitry Andric   // return.
5380b57cec5SDimitry Andric   return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
5390b57cec5SDimitry Andric                          InVals, OutVals, Callee);
5400b57cec5SDimitry Andric }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric /// Returns true by value, base pointer and offset pointer and addressing
5430b57cec5SDimitry Andric /// mode by reference if this node can be combined with a load / store to
5440b57cec5SDimitry Andric /// form a post-indexed load / store.
5450b57cec5SDimitry Andric bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
5460b57cec5SDimitry Andric       SDValue &Base, SDValue &Offset, ISD::MemIndexedMode &AM,
5470b57cec5SDimitry Andric       SelectionDAG &DAG) const {
5480b57cec5SDimitry Andric   LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(N);
5490b57cec5SDimitry Andric   if (!LSN)
5500b57cec5SDimitry Andric     return false;
5510b57cec5SDimitry Andric   EVT VT = LSN->getMemoryVT();
5520b57cec5SDimitry Andric   if (!VT.isSimple())
5530b57cec5SDimitry Andric     return false;
5540b57cec5SDimitry Andric   bool IsLegalType = VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 ||
5550b57cec5SDimitry Andric                      VT == MVT::i64 || VT == MVT::f32 || VT == MVT::f64 ||
5560b57cec5SDimitry Andric                      VT == MVT::v2i16 || VT == MVT::v2i32 || VT == MVT::v4i8 ||
5570b57cec5SDimitry Andric                      VT == MVT::v4i16 || VT == MVT::v8i8 ||
5580b57cec5SDimitry Andric                      Subtarget.isHVXVectorType(VT.getSimpleVT());
5590b57cec5SDimitry Andric   if (!IsLegalType)
5600b57cec5SDimitry Andric     return false;
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   if (Op->getOpcode() != ISD::ADD)
5630b57cec5SDimitry Andric     return false;
5640b57cec5SDimitry Andric   Base = Op->getOperand(0);
5650b57cec5SDimitry Andric   Offset = Op->getOperand(1);
5660b57cec5SDimitry Andric   if (!isa<ConstantSDNode>(Offset.getNode()))
5670b57cec5SDimitry Andric     return false;
5680b57cec5SDimitry Andric   AM = ISD::POST_INC;
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric   int32_t V = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
5710b57cec5SDimitry Andric   return Subtarget.getInstrInfo()->isValidAutoIncImm(VT, V);
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric SDValue
5750b57cec5SDimitry Andric HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
5760b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
5770b57cec5SDimitry Andric   auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
5780b57cec5SDimitry Andric   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
5790b57cec5SDimitry Andric   unsigned LR = HRI.getRARegister();
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric   if ((Op.getOpcode() != ISD::INLINEASM &&
5820b57cec5SDimitry Andric        Op.getOpcode() != ISD::INLINEASM_BR) || HMFI.hasClobberLR())
5830b57cec5SDimitry Andric     return Op;
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric   unsigned NumOps = Op.getNumOperands();
5860b57cec5SDimitry Andric   if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
5870b57cec5SDimitry Andric     --NumOps;  // Ignore the flag operand.
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric   for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
5900b57cec5SDimitry Andric     unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
5910b57cec5SDimitry Andric     unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
5920b57cec5SDimitry Andric     ++i;  // Skip the ID value.
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric     switch (InlineAsm::getKind(Flags)) {
5950b57cec5SDimitry Andric       default:
5960b57cec5SDimitry Andric         llvm_unreachable("Bad flags!");
5970b57cec5SDimitry Andric       case InlineAsm::Kind_RegUse:
5980b57cec5SDimitry Andric       case InlineAsm::Kind_Imm:
5990b57cec5SDimitry Andric       case InlineAsm::Kind_Mem:
6000b57cec5SDimitry Andric         i += NumVals;
6010b57cec5SDimitry Andric         break;
6020b57cec5SDimitry Andric       case InlineAsm::Kind_Clobber:
6030b57cec5SDimitry Andric       case InlineAsm::Kind_RegDef:
6040b57cec5SDimitry Andric       case InlineAsm::Kind_RegDefEarlyClobber: {
6050b57cec5SDimitry Andric         for (; NumVals; --NumVals, ++i) {
6060b57cec5SDimitry Andric           unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
6070b57cec5SDimitry Andric           if (Reg != LR)
6080b57cec5SDimitry Andric             continue;
6090b57cec5SDimitry Andric           HMFI.setHasClobberLR(true);
6100b57cec5SDimitry Andric           return Op;
6110b57cec5SDimitry Andric         }
6120b57cec5SDimitry Andric         break;
6130b57cec5SDimitry Andric       }
6140b57cec5SDimitry Andric     }
6150b57cec5SDimitry Andric   }
6160b57cec5SDimitry Andric 
6170b57cec5SDimitry Andric   return Op;
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric // Need to transform ISD::PREFETCH into something that doesn't inherit
6210b57cec5SDimitry Andric // all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
6220b57cec5SDimitry Andric // SDNPMayStore.
6230b57cec5SDimitry Andric SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
6240b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
6250b57cec5SDimitry Andric   SDValue Chain = Op.getOperand(0);
6260b57cec5SDimitry Andric   SDValue Addr = Op.getOperand(1);
6270b57cec5SDimitry Andric   // Lower it to DCFETCH($reg, #0).  A "pat" will try to merge the offset in,
6280b57cec5SDimitry Andric   // if the "reg" is fed by an "add".
6290b57cec5SDimitry Andric   SDLoc DL(Op);
6300b57cec5SDimitry Andric   SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
6310b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric // Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
6350b57cec5SDimitry Andric // is marked as having side-effects, while the register read on Hexagon does
6360b57cec5SDimitry Andric // not have any. TableGen refuses to accept the direct pattern from that node
6370b57cec5SDimitry Andric // to the A4_tfrcpp.
6380b57cec5SDimitry Andric SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
6390b57cec5SDimitry Andric                                                      SelectionDAG &DAG) const {
6400b57cec5SDimitry Andric   SDValue Chain = Op.getOperand(0);
6410b57cec5SDimitry Andric   SDLoc dl(Op);
6420b57cec5SDimitry Andric   SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
6430b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
6470b57cec5SDimitry Andric       SelectionDAG &DAG) const {
6480b57cec5SDimitry Andric   SDValue Chain = Op.getOperand(0);
6490b57cec5SDimitry Andric   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6500b57cec5SDimitry Andric   // Lower the hexagon_prefetch builtin to DCFETCH, as above.
6510b57cec5SDimitry Andric   if (IntNo == Intrinsic::hexagon_prefetch) {
6520b57cec5SDimitry Andric     SDValue Addr = Op.getOperand(2);
6530b57cec5SDimitry Andric     SDLoc DL(Op);
6540b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
6550b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
6560b57cec5SDimitry Andric   }
6570b57cec5SDimitry Andric   return SDValue();
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric SDValue
6610b57cec5SDimitry Andric HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
6620b57cec5SDimitry Andric                                                SelectionDAG &DAG) const {
6630b57cec5SDimitry Andric   SDValue Chain = Op.getOperand(0);
6640b57cec5SDimitry Andric   SDValue Size = Op.getOperand(1);
6650b57cec5SDimitry Andric   SDValue Align = Op.getOperand(2);
6660b57cec5SDimitry Andric   SDLoc dl(Op);
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
6690b57cec5SDimitry Andric   assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric   unsigned A = AlignConst->getSExtValue();
6720b57cec5SDimitry Andric   auto &HFI = *Subtarget.getFrameLowering();
6730b57cec5SDimitry Andric   // "Zero" means natural stack alignment.
6740b57cec5SDimitry Andric   if (A == 0)
6750b57cec5SDimitry Andric     A = HFI.getStackAlignment();
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric   LLVM_DEBUG({
6780b57cec5SDimitry Andric     dbgs () << __func__ << " Align: " << A << " Size: ";
6790b57cec5SDimitry Andric     Size.getNode()->dump(&DAG);
6800b57cec5SDimitry Andric     dbgs() << "\n";
6810b57cec5SDimitry Andric   });
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   SDValue AC = DAG.getConstant(A, dl, MVT::i32);
6840b57cec5SDimitry Andric   SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
6850b57cec5SDimitry Andric   SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(Op, AA);
6880b57cec5SDimitry Andric   return AA;
6890b57cec5SDimitry Andric }
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric SDValue HexagonTargetLowering::LowerFormalArguments(
6920b57cec5SDimitry Andric     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
6930b57cec5SDimitry Andric     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
6940b57cec5SDimitry Andric     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
6950b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
6960b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
6970b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric   // Assign locations to all of the incoming arguments.
7000b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> ArgLocs;
7010b57cec5SDimitry Andric   HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
7020b57cec5SDimitry Andric                         MF.getFunction().getFunctionType()->getNumParams());
7030b57cec5SDimitry Andric 
7040b57cec5SDimitry Andric   if (Subtarget.useHVXOps())
7050b57cec5SDimitry Andric     CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon_HVX);
7060b57cec5SDimitry Andric   else
7070b57cec5SDimitry Andric     CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric   // For LLVM, in the case when returning a struct by value (>8byte),
7100b57cec5SDimitry Andric   // the first argument is a pointer that points to the location on caller's
7110b57cec5SDimitry Andric   // stack where the return value will be stored. For Hexagon, the location on
7120b57cec5SDimitry Andric   // caller's stack is passed only when the struct size is smaller than (and
7130b57cec5SDimitry Andric   // equal to) 8 bytes. If not, no address will be passed into callee and
7140b57cec5SDimitry Andric   // callee return the result direclty through R0/R1.
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
7190b57cec5SDimitry Andric     CCValAssign &VA = ArgLocs[i];
7200b57cec5SDimitry Andric     ISD::ArgFlagsTy Flags = Ins[i].Flags;
7210b57cec5SDimitry Andric     bool ByVal = Flags.isByVal();
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric     // Arguments passed in registers:
7240b57cec5SDimitry Andric     // 1. 32- and 64-bit values and HVX vectors are passed directly,
7250b57cec5SDimitry Andric     // 2. Large structs are passed via an address, and the address is
7260b57cec5SDimitry Andric     //    passed in a register.
7270b57cec5SDimitry Andric     if (VA.isRegLoc() && ByVal && Flags.getByValSize() <= 8)
7280b57cec5SDimitry Andric       llvm_unreachable("ByValSize must be bigger than 8 bytes");
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric     bool InReg = VA.isRegLoc() &&
7310b57cec5SDimitry Andric                  (!ByVal || (ByVal && Flags.getByValSize() > 8));
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric     if (InReg) {
7340b57cec5SDimitry Andric       MVT RegVT = VA.getLocVT();
7350b57cec5SDimitry Andric       if (VA.getLocInfo() == CCValAssign::BCvt)
7360b57cec5SDimitry Andric         RegVT = VA.getValVT();
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric       const TargetRegisterClass *RC = getRegClassFor(RegVT);
739*8bcb0991SDimitry Andric       Register VReg = MRI.createVirtualRegister(RC);
7400b57cec5SDimitry Andric       SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
7410b57cec5SDimitry Andric 
7420b57cec5SDimitry Andric       // Treat values of type MVT::i1 specially: they are passed in
7430b57cec5SDimitry Andric       // registers of type i32, but they need to remain as values of
7440b57cec5SDimitry Andric       // type i1 for consistency of the argument lowering.
7450b57cec5SDimitry Andric       if (VA.getValVT() == MVT::i1) {
7460b57cec5SDimitry Andric         assert(RegVT.getSizeInBits() <= 32);
7470b57cec5SDimitry Andric         SDValue T = DAG.getNode(ISD::AND, dl, RegVT,
7480b57cec5SDimitry Andric                                 Copy, DAG.getConstant(1, dl, RegVT));
7490b57cec5SDimitry Andric         Copy = DAG.getSetCC(dl, MVT::i1, T, DAG.getConstant(0, dl, RegVT),
7500b57cec5SDimitry Andric                             ISD::SETNE);
7510b57cec5SDimitry Andric       } else {
7520b57cec5SDimitry Andric #ifndef NDEBUG
7530b57cec5SDimitry Andric         unsigned RegSize = RegVT.getSizeInBits();
7540b57cec5SDimitry Andric         assert(RegSize == 32 || RegSize == 64 ||
7550b57cec5SDimitry Andric                Subtarget.isHVXVectorType(RegVT));
7560b57cec5SDimitry Andric #endif
7570b57cec5SDimitry Andric       }
7580b57cec5SDimitry Andric       InVals.push_back(Copy);
7590b57cec5SDimitry Andric       MRI.addLiveIn(VA.getLocReg(), VReg);
7600b57cec5SDimitry Andric     } else {
7610b57cec5SDimitry Andric       assert(VA.isMemLoc() && "Argument should be passed in memory");
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric       // If it's a byval parameter, then we need to compute the
7640b57cec5SDimitry Andric       // "real" size, not the size of the pointer.
7650b57cec5SDimitry Andric       unsigned ObjSize = Flags.isByVal()
7660b57cec5SDimitry Andric                             ? Flags.getByValSize()
7670b57cec5SDimitry Andric                             : VA.getLocVT().getStoreSizeInBits() / 8;
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric       // Create the frame index object for this incoming parameter.
7700b57cec5SDimitry Andric       int Offset = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
7710b57cec5SDimitry Andric       int FI = MFI.CreateFixedObject(ObjSize, Offset, true);
7720b57cec5SDimitry Andric       SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
7730b57cec5SDimitry Andric 
7740b57cec5SDimitry Andric       if (Flags.isByVal()) {
7750b57cec5SDimitry Andric         // If it's a pass-by-value aggregate, then do not dereference the stack
7760b57cec5SDimitry Andric         // location. Instead, we should generate a reference to the stack
7770b57cec5SDimitry Andric         // location.
7780b57cec5SDimitry Andric         InVals.push_back(FIN);
7790b57cec5SDimitry Andric       } else {
7800b57cec5SDimitry Andric         SDValue L = DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
7810b57cec5SDimitry Andric                                 MachinePointerInfo::getFixedStack(MF, FI, 0));
7820b57cec5SDimitry Andric         InVals.push_back(L);
7830b57cec5SDimitry Andric       }
7840b57cec5SDimitry Andric     }
7850b57cec5SDimitry Andric   }
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric   if (IsVarArg) {
7890b57cec5SDimitry Andric     // This will point to the next argument passed via stack.
7900b57cec5SDimitry Andric     int Offset = HEXAGON_LRFP_SIZE + CCInfo.getNextStackOffset();
7910b57cec5SDimitry Andric     int FI = MFI.CreateFixedObject(Hexagon_PointerSize, Offset, true);
7920b57cec5SDimitry Andric     HMFI.setVarArgsFrameIndex(FI);
7930b57cec5SDimitry Andric   }
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric   return Chain;
7960b57cec5SDimitry Andric }
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric SDValue
7990b57cec5SDimitry Andric HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
8000b57cec5SDimitry Andric   // VASTART stores the address of the VarArgsFrameIndex slot into the
8010b57cec5SDimitry Andric   // memory location argument.
8020b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
8030b57cec5SDimitry Andric   HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
8040b57cec5SDimitry Andric   SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
8050b57cec5SDimitry Andric   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8060b57cec5SDimitry Andric   return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
8070b57cec5SDimitry Andric                       MachinePointerInfo(SV));
8080b57cec5SDimitry Andric }
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
8110b57cec5SDimitry Andric   const SDLoc &dl(Op);
8120b57cec5SDimitry Andric   SDValue LHS = Op.getOperand(0);
8130b57cec5SDimitry Andric   SDValue RHS = Op.getOperand(1);
8140b57cec5SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8150b57cec5SDimitry Andric   MVT ResTy = ty(Op);
8160b57cec5SDimitry Andric   MVT OpTy = ty(LHS);
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric   if (OpTy == MVT::v2i16 || OpTy == MVT::v4i8) {
8190b57cec5SDimitry Andric     MVT ElemTy = OpTy.getVectorElementType();
8200b57cec5SDimitry Andric     assert(ElemTy.isScalarInteger());
8210b57cec5SDimitry Andric     MVT WideTy = MVT::getVectorVT(MVT::getIntegerVT(2*ElemTy.getSizeInBits()),
8220b57cec5SDimitry Andric                                   OpTy.getVectorNumElements());
8230b57cec5SDimitry Andric     return DAG.getSetCC(dl, ResTy,
8240b57cec5SDimitry Andric                         DAG.getSExtOrTrunc(LHS, SDLoc(LHS), WideTy),
8250b57cec5SDimitry Andric                         DAG.getSExtOrTrunc(RHS, SDLoc(RHS), WideTy), CC);
8260b57cec5SDimitry Andric   }
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric   // Treat all other vector types as legal.
8290b57cec5SDimitry Andric   if (ResTy.isVector())
8300b57cec5SDimitry Andric     return Op;
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric   // Comparisons of short integers should use sign-extend, not zero-extend,
8330b57cec5SDimitry Andric   // since we can represent small negative values in the compare instructions.
8340b57cec5SDimitry Andric   // The LLVM default is to use zero-extend arbitrarily in these cases.
8350b57cec5SDimitry Andric   auto isSExtFree = [this](SDValue N) {
8360b57cec5SDimitry Andric     switch (N.getOpcode()) {
8370b57cec5SDimitry Andric       case ISD::TRUNCATE: {
8380b57cec5SDimitry Andric         // A sign-extend of a truncate of a sign-extend is free.
8390b57cec5SDimitry Andric         SDValue Op = N.getOperand(0);
8400b57cec5SDimitry Andric         if (Op.getOpcode() != ISD::AssertSext)
8410b57cec5SDimitry Andric           return false;
8420b57cec5SDimitry Andric         EVT OrigTy = cast<VTSDNode>(Op.getOperand(1))->getVT();
8430b57cec5SDimitry Andric         unsigned ThisBW = ty(N).getSizeInBits();
8440b57cec5SDimitry Andric         unsigned OrigBW = OrigTy.getSizeInBits();
8450b57cec5SDimitry Andric         // The type that was sign-extended to get the AssertSext must be
8460b57cec5SDimitry Andric         // narrower than the type of N (so that N has still the same value
8470b57cec5SDimitry Andric         // as the original).
8480b57cec5SDimitry Andric         return ThisBW >= OrigBW;
8490b57cec5SDimitry Andric       }
8500b57cec5SDimitry Andric       case ISD::LOAD:
8510b57cec5SDimitry Andric         // We have sign-extended loads.
8520b57cec5SDimitry Andric         return true;
8530b57cec5SDimitry Andric     }
8540b57cec5SDimitry Andric     return false;
8550b57cec5SDimitry Andric   };
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric   if (OpTy == MVT::i8 || OpTy == MVT::i16) {
8580b57cec5SDimitry Andric     ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
8590b57cec5SDimitry Andric     bool IsNegative = C && C->getAPIntValue().isNegative();
8600b57cec5SDimitry Andric     if (IsNegative || isSExtFree(LHS) || isSExtFree(RHS))
8610b57cec5SDimitry Andric       return DAG.getSetCC(dl, ResTy,
8620b57cec5SDimitry Andric                           DAG.getSExtOrTrunc(LHS, SDLoc(LHS), MVT::i32),
8630b57cec5SDimitry Andric                           DAG.getSExtOrTrunc(RHS, SDLoc(RHS), MVT::i32), CC);
8640b57cec5SDimitry Andric   }
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric   return SDValue();
8670b57cec5SDimitry Andric }
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric SDValue
8700b57cec5SDimitry Andric HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
8710b57cec5SDimitry Andric   SDValue PredOp = Op.getOperand(0);
8720b57cec5SDimitry Andric   SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
873*8bcb0991SDimitry Andric   MVT OpTy = ty(Op1);
874*8bcb0991SDimitry Andric   const SDLoc &dl(Op);
8750b57cec5SDimitry Andric 
876*8bcb0991SDimitry Andric   if (OpTy == MVT::v2i16 || OpTy == MVT::v4i8) {
877*8bcb0991SDimitry Andric     MVT ElemTy = OpTy.getVectorElementType();
878*8bcb0991SDimitry Andric     assert(ElemTy.isScalarInteger());
879*8bcb0991SDimitry Andric     MVT WideTy = MVT::getVectorVT(MVT::getIntegerVT(2*ElemTy.getSizeInBits()),
880*8bcb0991SDimitry Andric                                   OpTy.getVectorNumElements());
881*8bcb0991SDimitry Andric     // Generate (trunc (select (_, sext, sext))).
882*8bcb0991SDimitry Andric     return DAG.getSExtOrTrunc(
883*8bcb0991SDimitry Andric               DAG.getSelect(dl, WideTy, PredOp,
884*8bcb0991SDimitry Andric                             DAG.getSExtOrTrunc(Op1, dl, WideTy),
885*8bcb0991SDimitry Andric                             DAG.getSExtOrTrunc(Op2, dl, WideTy)),
886*8bcb0991SDimitry Andric               dl, OpTy);
8870b57cec5SDimitry Andric   }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric   return SDValue();
8900b57cec5SDimitry Andric }
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric static Constant *convert_i1_to_i8(const Constant *ConstVal) {
8930b57cec5SDimitry Andric   SmallVector<Constant *, 128> NewConst;
8940b57cec5SDimitry Andric   const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
8950b57cec5SDimitry Andric   if (!CV)
8960b57cec5SDimitry Andric     return nullptr;
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric   LLVMContext &Ctx = ConstVal->getContext();
8990b57cec5SDimitry Andric   IRBuilder<> IRB(Ctx);
9000b57cec5SDimitry Andric   unsigned NumVectorElements = CV->getNumOperands();
9010b57cec5SDimitry Andric   assert(isPowerOf2_32(NumVectorElements) &&
9020b57cec5SDimitry Andric          "conversion only supported for pow2 VectorSize!");
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric   for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
9050b57cec5SDimitry Andric     uint8_t x = 0;
9060b57cec5SDimitry Andric     for (unsigned j = 0; j < 8; ++j) {
9070b57cec5SDimitry Andric       uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
9080b57cec5SDimitry Andric       x |= y << (7 - j);
9090b57cec5SDimitry Andric     }
9100b57cec5SDimitry Andric     assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
9110b57cec5SDimitry Andric     NewConst.push_back(IRB.getInt8(x));
9120b57cec5SDimitry Andric   }
9130b57cec5SDimitry Andric   return ConstantVector::get(NewConst);
9140b57cec5SDimitry Andric }
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric SDValue
9170b57cec5SDimitry Andric HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
9180b57cec5SDimitry Andric   EVT ValTy = Op.getValueType();
9190b57cec5SDimitry Andric   ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
9200b57cec5SDimitry Andric   Constant *CVal = nullptr;
9210b57cec5SDimitry Andric   bool isVTi1Type = false;
9220b57cec5SDimitry Andric   if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
9230b57cec5SDimitry Andric     Type *CValTy = ConstVal->getType();
9240b57cec5SDimitry Andric     if (CValTy->isVectorTy() &&
9250b57cec5SDimitry Andric         CValTy->getVectorElementType()->isIntegerTy(1)) {
9260b57cec5SDimitry Andric       CVal = convert_i1_to_i8(ConstVal);
9270b57cec5SDimitry Andric       isVTi1Type = (CVal != nullptr);
9280b57cec5SDimitry Andric     }
9290b57cec5SDimitry Andric   }
9300b57cec5SDimitry Andric   unsigned Align = CPN->getAlignment();
9310b57cec5SDimitry Andric   bool IsPositionIndependent = isPositionIndependent();
9320b57cec5SDimitry Andric   unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
9330b57cec5SDimitry Andric 
9340b57cec5SDimitry Andric   unsigned Offset = 0;
9350b57cec5SDimitry Andric   SDValue T;
9360b57cec5SDimitry Andric   if (CPN->isMachineConstantPoolEntry())
9370b57cec5SDimitry Andric     T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
9380b57cec5SDimitry Andric                                   TF);
9390b57cec5SDimitry Andric   else if (isVTi1Type)
9400b57cec5SDimitry Andric     T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
9410b57cec5SDimitry Andric   else
9420b57cec5SDimitry Andric     T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset, TF);
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric   assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
9450b57cec5SDimitry Andric          "Inconsistent target flag encountered");
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   if (IsPositionIndependent)
9480b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
9490b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
9500b57cec5SDimitry Andric }
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric SDValue
9530b57cec5SDimitry Andric HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
9540b57cec5SDimitry Andric   EVT VT = Op.getValueType();
9550b57cec5SDimitry Andric   int Idx = cast<JumpTableSDNode>(Op)->getIndex();
9560b57cec5SDimitry Andric   if (isPositionIndependent()) {
9570b57cec5SDimitry Andric     SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
9580b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
9590b57cec5SDimitry Andric   }
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric   SDValue T = DAG.getTargetJumpTable(Idx, VT);
9620b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
9630b57cec5SDimitry Andric }
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric SDValue
9660b57cec5SDimitry Andric HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
9670b57cec5SDimitry Andric   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
9680b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
9690b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
9700b57cec5SDimitry Andric   MFI.setReturnAddressIsTaken(true);
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
9730b57cec5SDimitry Andric     return SDValue();
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   EVT VT = Op.getValueType();
9760b57cec5SDimitry Andric   SDLoc dl(Op);
9770b57cec5SDimitry Andric   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9780b57cec5SDimitry Andric   if (Depth) {
9790b57cec5SDimitry Andric     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9800b57cec5SDimitry Andric     SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
9810b57cec5SDimitry Andric     return DAG.getLoad(VT, dl, DAG.getEntryNode(),
9820b57cec5SDimitry Andric                        DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
9830b57cec5SDimitry Andric                        MachinePointerInfo());
9840b57cec5SDimitry Andric   }
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   // Return LR, which contains the return address. Mark it an implicit live-in.
9870b57cec5SDimitry Andric   unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
9880b57cec5SDimitry Andric   return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
9890b57cec5SDimitry Andric }
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric SDValue
9920b57cec5SDimitry Andric HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
9930b57cec5SDimitry Andric   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
9940b57cec5SDimitry Andric   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
9950b57cec5SDimitry Andric   MFI.setFrameAddressIsTaken(true);
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   EVT VT = Op.getValueType();
9980b57cec5SDimitry Andric   SDLoc dl(Op);
9990b57cec5SDimitry Andric   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10000b57cec5SDimitry Andric   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10010b57cec5SDimitry Andric                                          HRI.getFrameRegister(), VT);
10020b57cec5SDimitry Andric   while (Depth--)
10030b57cec5SDimitry Andric     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10040b57cec5SDimitry Andric                             MachinePointerInfo());
10050b57cec5SDimitry Andric   return FrameAddr;
10060b57cec5SDimitry Andric }
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric SDValue
10090b57cec5SDimitry Andric HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
10100b57cec5SDimitry Andric   SDLoc dl(Op);
10110b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
10120b57cec5SDimitry Andric }
10130b57cec5SDimitry Andric 
10140b57cec5SDimitry Andric SDValue
10150b57cec5SDimitry Andric HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
10160b57cec5SDimitry Andric   SDLoc dl(Op);
10170b57cec5SDimitry Andric   auto *GAN = cast<GlobalAddressSDNode>(Op);
10180b57cec5SDimitry Andric   auto PtrVT = getPointerTy(DAG.getDataLayout());
10190b57cec5SDimitry Andric   auto *GV = GAN->getGlobal();
10200b57cec5SDimitry Andric   int64_t Offset = GAN->getOffset();
10210b57cec5SDimitry Andric 
10220b57cec5SDimitry Andric   auto &HLOF = *HTM.getObjFileLowering();
10230b57cec5SDimitry Andric   Reloc::Model RM = HTM.getRelocationModel();
10240b57cec5SDimitry Andric 
10250b57cec5SDimitry Andric   if (RM == Reloc::Static) {
10260b57cec5SDimitry Andric     SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
10270b57cec5SDimitry Andric     const GlobalObject *GO = GV->getBaseObject();
10280b57cec5SDimitry Andric     if (GO && Subtarget.useSmallData() && HLOF.isGlobalInSmallSection(GO, HTM))
10290b57cec5SDimitry Andric       return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
10300b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
10310b57cec5SDimitry Andric   }
10320b57cec5SDimitry Andric 
10330b57cec5SDimitry Andric   bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
10340b57cec5SDimitry Andric   if (UsePCRel) {
10350b57cec5SDimitry Andric     SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
10360b57cec5SDimitry Andric                                             HexagonII::MO_PCREL);
10370b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
10380b57cec5SDimitry Andric   }
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric   // Use GOT index.
10410b57cec5SDimitry Andric   SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
10420b57cec5SDimitry Andric   SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
10430b57cec5SDimitry Andric   SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
10440b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
10450b57cec5SDimitry Andric }
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric // Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
10480b57cec5SDimitry Andric SDValue
10490b57cec5SDimitry Andric HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
10500b57cec5SDimitry Andric   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
10510b57cec5SDimitry Andric   SDLoc dl(Op);
10520b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric   Reloc::Model RM = HTM.getRelocationModel();
10550b57cec5SDimitry Andric   if (RM == Reloc::Static) {
10560b57cec5SDimitry Andric     SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
10570b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
10580b57cec5SDimitry Andric   }
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric   SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
10610b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
10620b57cec5SDimitry Andric }
10630b57cec5SDimitry Andric 
10640b57cec5SDimitry Andric SDValue
10650b57cec5SDimitry Andric HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
10660b57cec5SDimitry Andric       const {
10670b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
10680b57cec5SDimitry Andric   SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
10690b57cec5SDimitry Andric                                                HexagonII::MO_PCREL);
10700b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
10710b57cec5SDimitry Andric }
10720b57cec5SDimitry Andric 
10730b57cec5SDimitry Andric SDValue
10740b57cec5SDimitry Andric HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
10750b57cec5SDimitry Andric       GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
10760b57cec5SDimitry Andric       unsigned char OperandFlags) const {
10770b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
10780b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
10790b57cec5SDimitry Andric   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10800b57cec5SDimitry Andric   SDLoc dl(GA);
10810b57cec5SDimitry Andric   SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
10820b57cec5SDimitry Andric                                            GA->getValueType(0),
10830b57cec5SDimitry Andric                                            GA->getOffset(),
10840b57cec5SDimitry Andric                                            OperandFlags);
10850b57cec5SDimitry Andric   // Create Operands for the call.The Operands should have the following:
10860b57cec5SDimitry Andric   // 1. Chain SDValue
10870b57cec5SDimitry Andric   // 2. Callee which in this case is the Global address value.
10880b57cec5SDimitry Andric   // 3. Registers live into the call.In this case its R0, as we
10890b57cec5SDimitry Andric   //    have just one argument to be passed.
10900b57cec5SDimitry Andric   // 4. Glue.
10910b57cec5SDimitry Andric   // Note: The order is important.
10920b57cec5SDimitry Andric 
10930b57cec5SDimitry Andric   const auto &HRI = *Subtarget.getRegisterInfo();
10940b57cec5SDimitry Andric   const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
10950b57cec5SDimitry Andric   assert(Mask && "Missing call preserved mask for calling convention");
10960b57cec5SDimitry Andric   SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
10970b57cec5SDimitry Andric                     DAG.getRegisterMask(Mask), Glue };
10980b57cec5SDimitry Andric   Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
10990b57cec5SDimitry Andric 
11000b57cec5SDimitry Andric   // Inform MFI that function has calls.
11010b57cec5SDimitry Andric   MFI.setAdjustsStack(true);
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric   Glue = Chain.getValue(1);
11040b57cec5SDimitry Andric   return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
11050b57cec5SDimitry Andric }
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric //
11080b57cec5SDimitry Andric // Lower using the intial executable model for TLS addresses
11090b57cec5SDimitry Andric //
11100b57cec5SDimitry Andric SDValue
11110b57cec5SDimitry Andric HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
11120b57cec5SDimitry Andric       SelectionDAG &DAG) const {
11130b57cec5SDimitry Andric   SDLoc dl(GA);
11140b57cec5SDimitry Andric   int64_t Offset = GA->getOffset();
11150b57cec5SDimitry Andric   auto PtrVT = getPointerTy(DAG.getDataLayout());
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric   // Get the thread pointer.
11180b57cec5SDimitry Andric   SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric   bool IsPositionIndependent = isPositionIndependent();
11210b57cec5SDimitry Andric   unsigned char TF =
11220b57cec5SDimitry Andric       IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric   // First generate the TLS symbol address
11250b57cec5SDimitry Andric   SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
11260b57cec5SDimitry Andric                                            Offset, TF);
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric   SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
11290b57cec5SDimitry Andric 
11300b57cec5SDimitry Andric   if (IsPositionIndependent) {
11310b57cec5SDimitry Andric     // Generate the GOT pointer in case of position independent code
11320b57cec5SDimitry Andric     SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
11330b57cec5SDimitry Andric 
11340b57cec5SDimitry Andric     // Add the TLS Symbol address to GOT pointer.This gives
11350b57cec5SDimitry Andric     // GOT relative relocation for the symbol.
11360b57cec5SDimitry Andric     Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
11370b57cec5SDimitry Andric   }
11380b57cec5SDimitry Andric 
11390b57cec5SDimitry Andric   // Load the offset value for TLS symbol.This offset is relative to
11400b57cec5SDimitry Andric   // thread pointer.
11410b57cec5SDimitry Andric   SDValue LoadOffset =
11420b57cec5SDimitry Andric       DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
11430b57cec5SDimitry Andric 
11440b57cec5SDimitry Andric   // Address of the thread local variable is the add of thread
11450b57cec5SDimitry Andric   // pointer and the offset of the variable.
11460b57cec5SDimitry Andric   return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
11470b57cec5SDimitry Andric }
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric //
11500b57cec5SDimitry Andric // Lower using the local executable model for TLS addresses
11510b57cec5SDimitry Andric //
11520b57cec5SDimitry Andric SDValue
11530b57cec5SDimitry Andric HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
11540b57cec5SDimitry Andric       SelectionDAG &DAG) const {
11550b57cec5SDimitry Andric   SDLoc dl(GA);
11560b57cec5SDimitry Andric   int64_t Offset = GA->getOffset();
11570b57cec5SDimitry Andric   auto PtrVT = getPointerTy(DAG.getDataLayout());
11580b57cec5SDimitry Andric 
11590b57cec5SDimitry Andric   // Get the thread pointer.
11600b57cec5SDimitry Andric   SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
11610b57cec5SDimitry Andric   // Generate the TLS symbol address
11620b57cec5SDimitry Andric   SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
11630b57cec5SDimitry Andric                                            HexagonII::MO_TPREL);
11640b57cec5SDimitry Andric   SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric   // Address of the thread local variable is the add of thread
11670b57cec5SDimitry Andric   // pointer and the offset of the variable.
11680b57cec5SDimitry Andric   return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
11690b57cec5SDimitry Andric }
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric //
11720b57cec5SDimitry Andric // Lower using the general dynamic model for TLS addresses
11730b57cec5SDimitry Andric //
11740b57cec5SDimitry Andric SDValue
11750b57cec5SDimitry Andric HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
11760b57cec5SDimitry Andric       SelectionDAG &DAG) const {
11770b57cec5SDimitry Andric   SDLoc dl(GA);
11780b57cec5SDimitry Andric   int64_t Offset = GA->getOffset();
11790b57cec5SDimitry Andric   auto PtrVT = getPointerTy(DAG.getDataLayout());
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric   // First generate the TLS symbol address
11820b57cec5SDimitry Andric   SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
11830b57cec5SDimitry Andric                                            HexagonII::MO_GDGOT);
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric   // Then, generate the GOT pointer
11860b57cec5SDimitry Andric   SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
11870b57cec5SDimitry Andric 
11880b57cec5SDimitry Andric   // Add the TLS symbol and the GOT pointer
11890b57cec5SDimitry Andric   SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
11900b57cec5SDimitry Andric   SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
11910b57cec5SDimitry Andric 
11920b57cec5SDimitry Andric   // Copy over the argument to R0
11930b57cec5SDimitry Andric   SDValue InFlag;
11940b57cec5SDimitry Andric   Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
11950b57cec5SDimitry Andric   InFlag = Chain.getValue(1);
11960b57cec5SDimitry Andric 
11970b57cec5SDimitry Andric   unsigned Flags =
11980b57cec5SDimitry Andric       static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
11990b57cec5SDimitry Andric           ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
12000b57cec5SDimitry Andric           : HexagonII::MO_GDPLT;
12010b57cec5SDimitry Andric 
12020b57cec5SDimitry Andric   return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
12030b57cec5SDimitry Andric                            Hexagon::R0, Flags);
12040b57cec5SDimitry Andric }
12050b57cec5SDimitry Andric 
12060b57cec5SDimitry Andric //
12070b57cec5SDimitry Andric // Lower TLS addresses.
12080b57cec5SDimitry Andric //
12090b57cec5SDimitry Andric // For now for dynamic models, we only support the general dynamic model.
12100b57cec5SDimitry Andric //
12110b57cec5SDimitry Andric SDValue
12120b57cec5SDimitry Andric HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
12130b57cec5SDimitry Andric       SelectionDAG &DAG) const {
12140b57cec5SDimitry Andric   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
12150b57cec5SDimitry Andric 
12160b57cec5SDimitry Andric   switch (HTM.getTLSModel(GA->getGlobal())) {
12170b57cec5SDimitry Andric     case TLSModel::GeneralDynamic:
12180b57cec5SDimitry Andric     case TLSModel::LocalDynamic:
12190b57cec5SDimitry Andric       return LowerToTLSGeneralDynamicModel(GA, DAG);
12200b57cec5SDimitry Andric     case TLSModel::InitialExec:
12210b57cec5SDimitry Andric       return LowerToTLSInitialExecModel(GA, DAG);
12220b57cec5SDimitry Andric     case TLSModel::LocalExec:
12230b57cec5SDimitry Andric       return LowerToTLSLocalExecModel(GA, DAG);
12240b57cec5SDimitry Andric   }
12250b57cec5SDimitry Andric   llvm_unreachable("Bogus TLS model");
12260b57cec5SDimitry Andric }
12270b57cec5SDimitry Andric 
12280b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12290b57cec5SDimitry Andric // TargetLowering Implementation
12300b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12310b57cec5SDimitry Andric 
12320b57cec5SDimitry Andric HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
12330b57cec5SDimitry Andric                                              const HexagonSubtarget &ST)
12340b57cec5SDimitry Andric     : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
12350b57cec5SDimitry Andric       Subtarget(ST) {
12360b57cec5SDimitry Andric   auto &HRI = *Subtarget.getRegisterInfo();
12370b57cec5SDimitry Andric 
1238*8bcb0991SDimitry Andric   setPrefLoopAlignment(Align(16));
1239*8bcb0991SDimitry Andric   setMinFunctionAlignment(Align(4));
1240*8bcb0991SDimitry Andric   setPrefFunctionAlignment(Align(16));
12410b57cec5SDimitry Andric   setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
12420b57cec5SDimitry Andric   setBooleanContents(TargetLoweringBase::UndefinedBooleanContent);
12430b57cec5SDimitry Andric   setBooleanVectorContents(TargetLoweringBase::UndefinedBooleanContent);
12440b57cec5SDimitry Andric 
12450b57cec5SDimitry Andric   setMaxAtomicSizeInBitsSupported(64);
12460b57cec5SDimitry Andric   setMinCmpXchgSizeInBits(32);
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric   if (EnableHexSDNodeSched)
12490b57cec5SDimitry Andric     setSchedulingPreference(Sched::VLIW);
12500b57cec5SDimitry Andric   else
12510b57cec5SDimitry Andric     setSchedulingPreference(Sched::Source);
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric   // Limits for inline expansion of memcpy/memmove
12540b57cec5SDimitry Andric   MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
12550b57cec5SDimitry Andric   MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
12560b57cec5SDimitry Andric   MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
12570b57cec5SDimitry Andric   MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
12580b57cec5SDimitry Andric   MaxStoresPerMemset = MaxStoresPerMemsetCL;
12590b57cec5SDimitry Andric   MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric   //
12620b57cec5SDimitry Andric   // Set up register classes.
12630b57cec5SDimitry Andric   //
12640b57cec5SDimitry Andric 
12650b57cec5SDimitry Andric   addRegisterClass(MVT::i1,    &Hexagon::PredRegsRegClass);
12660b57cec5SDimitry Andric   addRegisterClass(MVT::v2i1,  &Hexagon::PredRegsRegClass);  // bbbbaaaa
12670b57cec5SDimitry Andric   addRegisterClass(MVT::v4i1,  &Hexagon::PredRegsRegClass);  // ddccbbaa
12680b57cec5SDimitry Andric   addRegisterClass(MVT::v8i1,  &Hexagon::PredRegsRegClass);  // hgfedcba
12690b57cec5SDimitry Andric   addRegisterClass(MVT::i32,   &Hexagon::IntRegsRegClass);
12700b57cec5SDimitry Andric   addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
12710b57cec5SDimitry Andric   addRegisterClass(MVT::v4i8,  &Hexagon::IntRegsRegClass);
12720b57cec5SDimitry Andric   addRegisterClass(MVT::i64,   &Hexagon::DoubleRegsRegClass);
12730b57cec5SDimitry Andric   addRegisterClass(MVT::v8i8,  &Hexagon::DoubleRegsRegClass);
12740b57cec5SDimitry Andric   addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
12750b57cec5SDimitry Andric   addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric   addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
12780b57cec5SDimitry Andric   addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric   //
12810b57cec5SDimitry Andric   // Handling of scalar operations.
12820b57cec5SDimitry Andric   //
12830b57cec5SDimitry Andric   // All operations default to "legal", except:
12840b57cec5SDimitry Andric   // - indexed loads and stores (pre-/post-incremented),
12850b57cec5SDimitry Andric   // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
12860b57cec5SDimitry Andric   //   ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
12870b57cec5SDimitry Andric   //   FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
12880b57cec5SDimitry Andric   //   FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
12890b57cec5SDimitry Andric   // which default to "expand" for at least one type.
12900b57cec5SDimitry Andric 
12910b57cec5SDimitry Andric   // Misc operations.
12920b57cec5SDimitry Andric   setOperationAction(ISD::ConstantFP,           MVT::f32,   Legal);
12930b57cec5SDimitry Andric   setOperationAction(ISD::ConstantFP,           MVT::f64,   Legal);
12940b57cec5SDimitry Andric   setOperationAction(ISD::TRAP,                 MVT::Other, Legal);
12950b57cec5SDimitry Andric   setOperationAction(ISD::ConstantPool,         MVT::i32,   Custom);
12960b57cec5SDimitry Andric   setOperationAction(ISD::JumpTable,            MVT::i32,   Custom);
12970b57cec5SDimitry Andric   setOperationAction(ISD::BUILD_PAIR,           MVT::i64,   Expand);
12980b57cec5SDimitry Andric   setOperationAction(ISD::SIGN_EXTEND_INREG,    MVT::i1,    Expand);
12990b57cec5SDimitry Andric   setOperationAction(ISD::INLINEASM,            MVT::Other, Custom);
13000b57cec5SDimitry Andric   setOperationAction(ISD::INLINEASM_BR,         MVT::Other, Custom);
13010b57cec5SDimitry Andric   setOperationAction(ISD::PREFETCH,             MVT::Other, Custom);
13020b57cec5SDimitry Andric   setOperationAction(ISD::READCYCLECOUNTER,     MVT::i64,   Custom);
13030b57cec5SDimitry Andric   setOperationAction(ISD::INTRINSIC_VOID,       MVT::Other, Custom);
13040b57cec5SDimitry Andric   setOperationAction(ISD::EH_RETURN,            MVT::Other, Custom);
13050b57cec5SDimitry Andric   setOperationAction(ISD::GLOBAL_OFFSET_TABLE,  MVT::i32,   Custom);
13060b57cec5SDimitry Andric   setOperationAction(ISD::GlobalTLSAddress,     MVT::i32,   Custom);
13070b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_FENCE,         MVT::Other, Custom);
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric   // Custom legalize GlobalAddress nodes into CONST32.
13100b57cec5SDimitry Andric   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
13110b57cec5SDimitry Andric   setOperationAction(ISD::GlobalAddress, MVT::i8,  Custom);
13120b57cec5SDimitry Andric   setOperationAction(ISD::BlockAddress,  MVT::i32, Custom);
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric   // Hexagon needs to optimize cases with negative constants.
13150b57cec5SDimitry Andric   setOperationAction(ISD::SETCC, MVT::i8,    Custom);
13160b57cec5SDimitry Andric   setOperationAction(ISD::SETCC, MVT::i16,   Custom);
13170b57cec5SDimitry Andric   setOperationAction(ISD::SETCC, MVT::v4i8,  Custom);
13180b57cec5SDimitry Andric   setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric   // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
13210b57cec5SDimitry Andric   setOperationAction(ISD::VASTART, MVT::Other, Custom);
13220b57cec5SDimitry Andric   setOperationAction(ISD::VAEND,   MVT::Other, Expand);
13230b57cec5SDimitry Andric   setOperationAction(ISD::VAARG,   MVT::Other, Expand);
13240b57cec5SDimitry Andric   setOperationAction(ISD::VACOPY,  MVT::Other, Expand);
13250b57cec5SDimitry Andric 
13260b57cec5SDimitry Andric   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
13270b57cec5SDimitry Andric   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
13280b57cec5SDimitry Andric   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric   if (EmitJumpTables)
13310b57cec5SDimitry Andric     setMinimumJumpTableEntries(MinimumJumpTables);
13320b57cec5SDimitry Andric   else
13330b57cec5SDimitry Andric     setMinimumJumpTableEntries(std::numeric_limits<unsigned>::max());
13340b57cec5SDimitry Andric   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
13350b57cec5SDimitry Andric 
13360b57cec5SDimitry Andric   setOperationAction(ISD::ABS, MVT::i32, Legal);
13370b57cec5SDimitry Andric   setOperationAction(ISD::ABS, MVT::i64, Legal);
13380b57cec5SDimitry Andric 
13390b57cec5SDimitry Andric   // Hexagon has A4_addp_c and A4_subp_c that take and generate a carry bit,
13400b57cec5SDimitry Andric   // but they only operate on i64.
13410b57cec5SDimitry Andric   for (MVT VT : MVT::integer_valuetypes()) {
13420b57cec5SDimitry Andric     setOperationAction(ISD::UADDO,    VT, Custom);
13430b57cec5SDimitry Andric     setOperationAction(ISD::USUBO,    VT, Custom);
13440b57cec5SDimitry Andric     setOperationAction(ISD::SADDO,    VT, Expand);
13450b57cec5SDimitry Andric     setOperationAction(ISD::SSUBO,    VT, Expand);
13460b57cec5SDimitry Andric     setOperationAction(ISD::ADDCARRY, VT, Expand);
13470b57cec5SDimitry Andric     setOperationAction(ISD::SUBCARRY, VT, Expand);
13480b57cec5SDimitry Andric   }
13490b57cec5SDimitry Andric   setOperationAction(ISD::ADDCARRY, MVT::i64, Custom);
13500b57cec5SDimitry Andric   setOperationAction(ISD::SUBCARRY, MVT::i64, Custom);
13510b57cec5SDimitry Andric 
13520b57cec5SDimitry Andric   setOperationAction(ISD::CTLZ, MVT::i8,  Promote);
13530b57cec5SDimitry Andric   setOperationAction(ISD::CTLZ, MVT::i16, Promote);
13540b57cec5SDimitry Andric   setOperationAction(ISD::CTTZ, MVT::i8,  Promote);
13550b57cec5SDimitry Andric   setOperationAction(ISD::CTTZ, MVT::i16, Promote);
13560b57cec5SDimitry Andric 
13570b57cec5SDimitry Andric   // Popcount can count # of 1s in i64 but returns i32.
13580b57cec5SDimitry Andric   setOperationAction(ISD::CTPOP, MVT::i8,  Promote);
13590b57cec5SDimitry Andric   setOperationAction(ISD::CTPOP, MVT::i16, Promote);
13600b57cec5SDimitry Andric   setOperationAction(ISD::CTPOP, MVT::i32, Promote);
13610b57cec5SDimitry Andric   setOperationAction(ISD::CTPOP, MVT::i64, Legal);
13620b57cec5SDimitry Andric 
13630b57cec5SDimitry Andric   setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
13640b57cec5SDimitry Andric   setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
13650b57cec5SDimitry Andric   setOperationAction(ISD::BSWAP, MVT::i32, Legal);
13660b57cec5SDimitry Andric   setOperationAction(ISD::BSWAP, MVT::i64, Legal);
13670b57cec5SDimitry Andric 
13680b57cec5SDimitry Andric   setOperationAction(ISD::FSHL, MVT::i32, Legal);
13690b57cec5SDimitry Andric   setOperationAction(ISD::FSHL, MVT::i64, Legal);
13700b57cec5SDimitry Andric   setOperationAction(ISD::FSHR, MVT::i32, Legal);
13710b57cec5SDimitry Andric   setOperationAction(ISD::FSHR, MVT::i64, Legal);
13720b57cec5SDimitry Andric 
13730b57cec5SDimitry Andric   for (unsigned IntExpOp :
13740b57cec5SDimitry Andric        {ISD::SDIV,      ISD::UDIV,      ISD::SREM,      ISD::UREM,
13750b57cec5SDimitry Andric         ISD::SDIVREM,   ISD::UDIVREM,   ISD::ROTL,      ISD::ROTR,
13760b57cec5SDimitry Andric         ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
13770b57cec5SDimitry Andric         ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
13780b57cec5SDimitry Andric     for (MVT VT : MVT::integer_valuetypes())
13790b57cec5SDimitry Andric       setOperationAction(IntExpOp, VT, Expand);
13800b57cec5SDimitry Andric   }
13810b57cec5SDimitry Andric 
13820b57cec5SDimitry Andric   for (unsigned FPExpOp :
13830b57cec5SDimitry Andric        {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
13840b57cec5SDimitry Andric         ISD::FPOW, ISD::FCOPYSIGN}) {
13850b57cec5SDimitry Andric     for (MVT VT : MVT::fp_valuetypes())
13860b57cec5SDimitry Andric       setOperationAction(FPExpOp, VT, Expand);
13870b57cec5SDimitry Andric   }
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric   // No extending loads from i32.
13900b57cec5SDimitry Andric   for (MVT VT : MVT::integer_valuetypes()) {
13910b57cec5SDimitry Andric     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
13920b57cec5SDimitry Andric     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
13930b57cec5SDimitry Andric     setLoadExtAction(ISD::EXTLOAD,  VT, MVT::i32, Expand);
13940b57cec5SDimitry Andric   }
13950b57cec5SDimitry Andric   // Turn FP truncstore into trunc + store.
13960b57cec5SDimitry Andric   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
13970b57cec5SDimitry Andric   // Turn FP extload into load/fpextend.
13980b57cec5SDimitry Andric   for (MVT VT : MVT::fp_valuetypes())
13990b57cec5SDimitry Andric     setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric   // Expand BR_CC and SELECT_CC for all integer and fp types.
14020b57cec5SDimitry Andric   for (MVT VT : MVT::integer_valuetypes()) {
14030b57cec5SDimitry Andric     setOperationAction(ISD::BR_CC,     VT, Expand);
14040b57cec5SDimitry Andric     setOperationAction(ISD::SELECT_CC, VT, Expand);
14050b57cec5SDimitry Andric   }
14060b57cec5SDimitry Andric   for (MVT VT : MVT::fp_valuetypes()) {
14070b57cec5SDimitry Andric     setOperationAction(ISD::BR_CC,     VT, Expand);
14080b57cec5SDimitry Andric     setOperationAction(ISD::SELECT_CC, VT, Expand);
14090b57cec5SDimitry Andric   }
14100b57cec5SDimitry Andric   setOperationAction(ISD::BR_CC, MVT::Other, Expand);
14110b57cec5SDimitry Andric 
14120b57cec5SDimitry Andric   //
14130b57cec5SDimitry Andric   // Handling of vector operations.
14140b57cec5SDimitry Andric   //
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric   // Set the action for vector operations to "expand", then override it with
14170b57cec5SDimitry Andric   // either "custom" or "legal" for specific cases.
14180b57cec5SDimitry Andric   static const unsigned VectExpOps[] = {
14190b57cec5SDimitry Andric     // Integer arithmetic:
14200b57cec5SDimitry Andric     ISD::ADD,     ISD::SUB,     ISD::MUL,     ISD::SDIV,      ISD::UDIV,
14210b57cec5SDimitry Andric     ISD::SREM,    ISD::UREM,    ISD::SDIVREM, ISD::UDIVREM,   ISD::SADDO,
14220b57cec5SDimitry Andric     ISD::UADDO,   ISD::SSUBO,   ISD::USUBO,   ISD::SMUL_LOHI, ISD::UMUL_LOHI,
14230b57cec5SDimitry Andric     // Logical/bit:
14240b57cec5SDimitry Andric     ISD::AND,     ISD::OR,      ISD::XOR,     ISD::ROTL,    ISD::ROTR,
14250b57cec5SDimitry Andric     ISD::CTPOP,   ISD::CTLZ,    ISD::CTTZ,
14260b57cec5SDimitry Andric     // Floating point arithmetic/math functions:
14270b57cec5SDimitry Andric     ISD::FADD,    ISD::FSUB,    ISD::FMUL,    ISD::FMA,     ISD::FDIV,
14280b57cec5SDimitry Andric     ISD::FREM,    ISD::FNEG,    ISD::FABS,    ISD::FSQRT,   ISD::FSIN,
14290b57cec5SDimitry Andric     ISD::FCOS,    ISD::FPOW,    ISD::FLOG,    ISD::FLOG2,
14300b57cec5SDimitry Andric     ISD::FLOG10,  ISD::FEXP,    ISD::FEXP2,   ISD::FCEIL,   ISD::FTRUNC,
14310b57cec5SDimitry Andric     ISD::FRINT,   ISD::FNEARBYINT,            ISD::FROUND,  ISD::FFLOOR,
14320b57cec5SDimitry Andric     ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
14330b57cec5SDimitry Andric     // Misc:
14340b57cec5SDimitry Andric     ISD::BR_CC,   ISD::SELECT_CC,             ISD::ConstantPool,
14350b57cec5SDimitry Andric     // Vector:
14360b57cec5SDimitry Andric     ISD::BUILD_VECTOR,          ISD::SCALAR_TO_VECTOR,
14370b57cec5SDimitry Andric     ISD::EXTRACT_VECTOR_ELT,    ISD::INSERT_VECTOR_ELT,
14380b57cec5SDimitry Andric     ISD::EXTRACT_SUBVECTOR,     ISD::INSERT_SUBVECTOR,
14390b57cec5SDimitry Andric     ISD::CONCAT_VECTORS,        ISD::VECTOR_SHUFFLE
14400b57cec5SDimitry Andric   };
14410b57cec5SDimitry Andric 
1442*8bcb0991SDimitry Andric   for (MVT VT : MVT::fixedlen_vector_valuetypes()) {
14430b57cec5SDimitry Andric     for (unsigned VectExpOp : VectExpOps)
14440b57cec5SDimitry Andric       setOperationAction(VectExpOp, VT, Expand);
14450b57cec5SDimitry Andric 
14460b57cec5SDimitry Andric     // Expand all extending loads and truncating stores:
1447*8bcb0991SDimitry Andric     for (MVT TargetVT : MVT::fixedlen_vector_valuetypes()) {
14480b57cec5SDimitry Andric       if (TargetVT == VT)
14490b57cec5SDimitry Andric         continue;
14500b57cec5SDimitry Andric       setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
14510b57cec5SDimitry Andric       setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
14520b57cec5SDimitry Andric       setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
14530b57cec5SDimitry Andric       setTruncStoreAction(VT, TargetVT, Expand);
14540b57cec5SDimitry Andric     }
14550b57cec5SDimitry Andric 
14560b57cec5SDimitry Andric     // Normalize all inputs to SELECT to be vectors of i32.
14570b57cec5SDimitry Andric     if (VT.getVectorElementType() != MVT::i32) {
14580b57cec5SDimitry Andric       MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
14590b57cec5SDimitry Andric       setOperationAction(ISD::SELECT, VT, Promote);
14600b57cec5SDimitry Andric       AddPromotedToType(ISD::SELECT, VT, VT32);
14610b57cec5SDimitry Andric     }
14620b57cec5SDimitry Andric     setOperationAction(ISD::SRA, VT, Custom);
14630b57cec5SDimitry Andric     setOperationAction(ISD::SHL, VT, Custom);
14640b57cec5SDimitry Andric     setOperationAction(ISD::SRL, VT, Custom);
14650b57cec5SDimitry Andric   }
14660b57cec5SDimitry Andric 
14670b57cec5SDimitry Andric   // Extending loads from (native) vectors of i8 into (native) vectors of i16
14680b57cec5SDimitry Andric   // are legal.
14690b57cec5SDimitry Andric   setLoadExtAction(ISD::EXTLOAD,  MVT::v2i16, MVT::v2i8, Legal);
14700b57cec5SDimitry Andric   setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
14710b57cec5SDimitry Andric   setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
14720b57cec5SDimitry Andric   setLoadExtAction(ISD::EXTLOAD,  MVT::v4i16, MVT::v4i8, Legal);
14730b57cec5SDimitry Andric   setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
14740b57cec5SDimitry Andric   setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
14750b57cec5SDimitry Andric 
14760b57cec5SDimitry Andric   // Types natively supported:
14770b57cec5SDimitry Andric   for (MVT NativeVT : {MVT::v8i1, MVT::v4i1, MVT::v2i1, MVT::v4i8,
14780b57cec5SDimitry Andric                        MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
14790b57cec5SDimitry Andric     setOperationAction(ISD::BUILD_VECTOR,       NativeVT, Custom);
14800b57cec5SDimitry Andric     setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
14810b57cec5SDimitry Andric     setOperationAction(ISD::INSERT_VECTOR_ELT,  NativeVT, Custom);
14820b57cec5SDimitry Andric     setOperationAction(ISD::EXTRACT_SUBVECTOR,  NativeVT, Custom);
14830b57cec5SDimitry Andric     setOperationAction(ISD::INSERT_SUBVECTOR,   NativeVT, Custom);
14840b57cec5SDimitry Andric     setOperationAction(ISD::CONCAT_VECTORS,     NativeVT, Custom);
14850b57cec5SDimitry Andric 
14860b57cec5SDimitry Andric     setOperationAction(ISD::ADD, NativeVT, Legal);
14870b57cec5SDimitry Andric     setOperationAction(ISD::SUB, NativeVT, Legal);
14880b57cec5SDimitry Andric     setOperationAction(ISD::MUL, NativeVT, Legal);
14890b57cec5SDimitry Andric     setOperationAction(ISD::AND, NativeVT, Legal);
14900b57cec5SDimitry Andric     setOperationAction(ISD::OR,  NativeVT, Legal);
14910b57cec5SDimitry Andric     setOperationAction(ISD::XOR, NativeVT, Legal);
14920b57cec5SDimitry Andric   }
14930b57cec5SDimitry Andric 
14940b57cec5SDimitry Andric   // Custom lower unaligned loads.
14950b57cec5SDimitry Andric   // Also, for both loads and stores, verify the alignment of the address
14960b57cec5SDimitry Andric   // in case it is a compile-time constant. This is a usability feature to
14970b57cec5SDimitry Andric   // provide a meaningful error message to users.
14980b57cec5SDimitry Andric   for (MVT VT : {MVT::i16, MVT::i32, MVT::v4i8, MVT::i64, MVT::v8i8,
14990b57cec5SDimitry Andric                  MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
15000b57cec5SDimitry Andric     setOperationAction(ISD::LOAD,  VT, Custom);
15010b57cec5SDimitry Andric     setOperationAction(ISD::STORE, VT, Custom);
15020b57cec5SDimitry Andric   }
15030b57cec5SDimitry Andric 
1504*8bcb0991SDimitry Andric   for (MVT VT : {MVT::v2i16, MVT::v4i8, MVT::v8i8, MVT::v2i32, MVT::v4i16,
1505*8bcb0991SDimitry Andric                  MVT::v2i32}) {
1506*8bcb0991SDimitry Andric     setCondCodeAction(ISD::SETNE,  VT, Expand);
15070b57cec5SDimitry Andric     setCondCodeAction(ISD::SETLE,  VT, Expand);
1508*8bcb0991SDimitry Andric     setCondCodeAction(ISD::SETGE,  VT, Expand);
1509*8bcb0991SDimitry Andric     setCondCodeAction(ISD::SETLT,  VT, Expand);
15100b57cec5SDimitry Andric     setCondCodeAction(ISD::SETULE, VT, Expand);
1511*8bcb0991SDimitry Andric     setCondCodeAction(ISD::SETUGE, VT, Expand);
1512*8bcb0991SDimitry Andric     setCondCodeAction(ISD::SETULT, VT, Expand);
15130b57cec5SDimitry Andric   }
15140b57cec5SDimitry Andric 
15150b57cec5SDimitry Andric   // Custom-lower bitcasts from i8 to v8i1.
15160b57cec5SDimitry Andric   setOperationAction(ISD::BITCAST,        MVT::i8,    Custom);
15170b57cec5SDimitry Andric   setOperationAction(ISD::SETCC,          MVT::v2i16, Custom);
1518*8bcb0991SDimitry Andric   setOperationAction(ISD::VSELECT,        MVT::v4i8,  Custom);
15190b57cec5SDimitry Andric   setOperationAction(ISD::VSELECT,        MVT::v2i16, Custom);
15200b57cec5SDimitry Andric   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i8,  Custom);
15210b57cec5SDimitry Andric   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
15220b57cec5SDimitry Andric   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8,  Custom);
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric   // V5+.
15250b57cec5SDimitry Andric   setOperationAction(ISD::FMA,  MVT::f64, Expand);
15260b57cec5SDimitry Andric   setOperationAction(ISD::FADD, MVT::f64, Expand);
15270b57cec5SDimitry Andric   setOperationAction(ISD::FSUB, MVT::f64, Expand);
15280b57cec5SDimitry Andric   setOperationAction(ISD::FMUL, MVT::f64, Expand);
15290b57cec5SDimitry Andric 
15300b57cec5SDimitry Andric   setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
15310b57cec5SDimitry Andric   setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
15320b57cec5SDimitry Andric 
15330b57cec5SDimitry Andric   setOperationAction(ISD::FP_TO_UINT, MVT::i1,  Promote);
15340b57cec5SDimitry Andric   setOperationAction(ISD::FP_TO_UINT, MVT::i8,  Promote);
15350b57cec5SDimitry Andric   setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
15360b57cec5SDimitry Andric   setOperationAction(ISD::FP_TO_SINT, MVT::i1,  Promote);
15370b57cec5SDimitry Andric   setOperationAction(ISD::FP_TO_SINT, MVT::i8,  Promote);
15380b57cec5SDimitry Andric   setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
15390b57cec5SDimitry Andric   setOperationAction(ISD::UINT_TO_FP, MVT::i1,  Promote);
15400b57cec5SDimitry Andric   setOperationAction(ISD::UINT_TO_FP, MVT::i8,  Promote);
15410b57cec5SDimitry Andric   setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
15420b57cec5SDimitry Andric   setOperationAction(ISD::SINT_TO_FP, MVT::i1,  Promote);
15430b57cec5SDimitry Andric   setOperationAction(ISD::SINT_TO_FP, MVT::i8,  Promote);
15440b57cec5SDimitry Andric   setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric   // Handling of indexed loads/stores: default is "expand".
15470b57cec5SDimitry Andric   //
15480b57cec5SDimitry Andric   for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64, MVT::f32, MVT::f64,
15490b57cec5SDimitry Andric                  MVT::v2i16, MVT::v2i32, MVT::v4i8, MVT::v4i16, MVT::v8i8}) {
15500b57cec5SDimitry Andric     setIndexedLoadAction(ISD::POST_INC, VT, Legal);
15510b57cec5SDimitry Andric     setIndexedStoreAction(ISD::POST_INC, VT, Legal);
15520b57cec5SDimitry Andric   }
15530b57cec5SDimitry Andric 
15540b57cec5SDimitry Andric   // Subtarget-specific operation actions.
15550b57cec5SDimitry Andric   //
15560b57cec5SDimitry Andric   if (Subtarget.hasV60Ops()) {
15570b57cec5SDimitry Andric     setOperationAction(ISD::ROTL, MVT::i32, Legal);
15580b57cec5SDimitry Andric     setOperationAction(ISD::ROTL, MVT::i64, Legal);
15590b57cec5SDimitry Andric     setOperationAction(ISD::ROTR, MVT::i32, Legal);
15600b57cec5SDimitry Andric     setOperationAction(ISD::ROTR, MVT::i64, Legal);
15610b57cec5SDimitry Andric   }
15620b57cec5SDimitry Andric   if (Subtarget.hasV66Ops()) {
15630b57cec5SDimitry Andric     setOperationAction(ISD::FADD, MVT::f64, Legal);
15640b57cec5SDimitry Andric     setOperationAction(ISD::FSUB, MVT::f64, Legal);
15650b57cec5SDimitry Andric   }
15660b57cec5SDimitry Andric 
1567*8bcb0991SDimitry Andric   setTargetDAGCombine(ISD::VSELECT);
1568*8bcb0991SDimitry Andric 
15690b57cec5SDimitry Andric   if (Subtarget.useHVXOps())
15700b57cec5SDimitry Andric     initializeHVXLowering();
15710b57cec5SDimitry Andric 
15720b57cec5SDimitry Andric   computeRegisterProperties(&HRI);
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric   //
15750b57cec5SDimitry Andric   // Library calls for unsupported operations
15760b57cec5SDimitry Andric   //
15770b57cec5SDimitry Andric   bool FastMath  = EnableFastMath;
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric   setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
15800b57cec5SDimitry Andric   setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
15810b57cec5SDimitry Andric   setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
15820b57cec5SDimitry Andric   setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
15830b57cec5SDimitry Andric   setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
15840b57cec5SDimitry Andric   setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
15850b57cec5SDimitry Andric   setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
15860b57cec5SDimitry Andric   setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
15870b57cec5SDimitry Andric 
15880b57cec5SDimitry Andric   setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
15890b57cec5SDimitry Andric   setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
15900b57cec5SDimitry Andric   setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
15910b57cec5SDimitry Andric   setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
15920b57cec5SDimitry Andric   setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
15930b57cec5SDimitry Andric   setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
15940b57cec5SDimitry Andric 
15950b57cec5SDimitry Andric   // This is the only fast library function for sqrtd.
15960b57cec5SDimitry Andric   if (FastMath)
15970b57cec5SDimitry Andric     setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
15980b57cec5SDimitry Andric 
15990b57cec5SDimitry Andric   // Prefix is: nothing  for "slow-math",
16000b57cec5SDimitry Andric   //            "fast2_" for V5+ fast-math double-precision
16010b57cec5SDimitry Andric   // (actually, keep fast-math and fast-math2 separate for now)
16020b57cec5SDimitry Andric   if (FastMath) {
16030b57cec5SDimitry Andric     setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
16040b57cec5SDimitry Andric     setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
16050b57cec5SDimitry Andric     setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
16060b57cec5SDimitry Andric     setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
16070b57cec5SDimitry Andric     setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
16080b57cec5SDimitry Andric   } else {
16090b57cec5SDimitry Andric     setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
16100b57cec5SDimitry Andric     setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
16110b57cec5SDimitry Andric     setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
16120b57cec5SDimitry Andric     setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
16130b57cec5SDimitry Andric     setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
16140b57cec5SDimitry Andric   }
16150b57cec5SDimitry Andric 
16160b57cec5SDimitry Andric   if (FastMath)
16170b57cec5SDimitry Andric     setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
16180b57cec5SDimitry Andric   else
16190b57cec5SDimitry Andric     setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
16200b57cec5SDimitry Andric 
16210b57cec5SDimitry Andric   // These cause problems when the shift amount is non-constant.
16220b57cec5SDimitry Andric   setLibcallName(RTLIB::SHL_I128, nullptr);
16230b57cec5SDimitry Andric   setLibcallName(RTLIB::SRL_I128, nullptr);
16240b57cec5SDimitry Andric   setLibcallName(RTLIB::SRA_I128, nullptr);
16250b57cec5SDimitry Andric }
16260b57cec5SDimitry Andric 
16270b57cec5SDimitry Andric const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
16280b57cec5SDimitry Andric   switch ((HexagonISD::NodeType)Opcode) {
16290b57cec5SDimitry Andric   case HexagonISD::ADDC:          return "HexagonISD::ADDC";
16300b57cec5SDimitry Andric   case HexagonISD::SUBC:          return "HexagonISD::SUBC";
16310b57cec5SDimitry Andric   case HexagonISD::ALLOCA:        return "HexagonISD::ALLOCA";
16320b57cec5SDimitry Andric   case HexagonISD::AT_GOT:        return "HexagonISD::AT_GOT";
16330b57cec5SDimitry Andric   case HexagonISD::AT_PCREL:      return "HexagonISD::AT_PCREL";
16340b57cec5SDimitry Andric   case HexagonISD::BARRIER:       return "HexagonISD::BARRIER";
16350b57cec5SDimitry Andric   case HexagonISD::CALL:          return "HexagonISD::CALL";
16360b57cec5SDimitry Andric   case HexagonISD::CALLnr:        return "HexagonISD::CALLnr";
16370b57cec5SDimitry Andric   case HexagonISD::CALLR:         return "HexagonISD::CALLR";
16380b57cec5SDimitry Andric   case HexagonISD::COMBINE:       return "HexagonISD::COMBINE";
16390b57cec5SDimitry Andric   case HexagonISD::CONST32_GP:    return "HexagonISD::CONST32_GP";
16400b57cec5SDimitry Andric   case HexagonISD::CONST32:       return "HexagonISD::CONST32";
16410b57cec5SDimitry Andric   case HexagonISD::CP:            return "HexagonISD::CP";
16420b57cec5SDimitry Andric   case HexagonISD::DCFETCH:       return "HexagonISD::DCFETCH";
16430b57cec5SDimitry Andric   case HexagonISD::EH_RETURN:     return "HexagonISD::EH_RETURN";
16440b57cec5SDimitry Andric   case HexagonISD::TSTBIT:        return "HexagonISD::TSTBIT";
16450b57cec5SDimitry Andric   case HexagonISD::EXTRACTU:      return "HexagonISD::EXTRACTU";
16460b57cec5SDimitry Andric   case HexagonISD::INSERT:        return "HexagonISD::INSERT";
16470b57cec5SDimitry Andric   case HexagonISD::JT:            return "HexagonISD::JT";
16480b57cec5SDimitry Andric   case HexagonISD::RET_FLAG:      return "HexagonISD::RET_FLAG";
16490b57cec5SDimitry Andric   case HexagonISD::TC_RETURN:     return "HexagonISD::TC_RETURN";
16500b57cec5SDimitry Andric   case HexagonISD::VASL:          return "HexagonISD::VASL";
16510b57cec5SDimitry Andric   case HexagonISD::VASR:          return "HexagonISD::VASR";
16520b57cec5SDimitry Andric   case HexagonISD::VLSR:          return "HexagonISD::VLSR";
16530b57cec5SDimitry Andric   case HexagonISD::VSPLAT:        return "HexagonISD::VSPLAT";
16540b57cec5SDimitry Andric   case HexagonISD::VEXTRACTW:     return "HexagonISD::VEXTRACTW";
16550b57cec5SDimitry Andric   case HexagonISD::VINSERTW0:     return "HexagonISD::VINSERTW0";
16560b57cec5SDimitry Andric   case HexagonISD::VROR:          return "HexagonISD::VROR";
16570b57cec5SDimitry Andric   case HexagonISD::READCYCLE:     return "HexagonISD::READCYCLE";
1658*8bcb0991SDimitry Andric   case HexagonISD::PTRUE:         return "HexagonISD::PTRUE";
1659*8bcb0991SDimitry Andric   case HexagonISD::PFALSE:        return "HexagonISD::PFALSE";
16600b57cec5SDimitry Andric   case HexagonISD::VZERO:         return "HexagonISD::VZERO";
16610b57cec5SDimitry Andric   case HexagonISD::VSPLATW:       return "HexagonISD::VSPLATW";
16620b57cec5SDimitry Andric   case HexagonISD::D2P:           return "HexagonISD::D2P";
16630b57cec5SDimitry Andric   case HexagonISD::P2D:           return "HexagonISD::P2D";
16640b57cec5SDimitry Andric   case HexagonISD::V2Q:           return "HexagonISD::V2Q";
16650b57cec5SDimitry Andric   case HexagonISD::Q2V:           return "HexagonISD::Q2V";
16660b57cec5SDimitry Andric   case HexagonISD::QCAT:          return "HexagonISD::QCAT";
16670b57cec5SDimitry Andric   case HexagonISD::QTRUE:         return "HexagonISD::QTRUE";
16680b57cec5SDimitry Andric   case HexagonISD::QFALSE:        return "HexagonISD::QFALSE";
16690b57cec5SDimitry Andric   case HexagonISD::TYPECAST:      return "HexagonISD::TYPECAST";
16700b57cec5SDimitry Andric   case HexagonISD::VALIGN:        return "HexagonISD::VALIGN";
16710b57cec5SDimitry Andric   case HexagonISD::VALIGNADDR:    return "HexagonISD::VALIGNADDR";
16720b57cec5SDimitry Andric   case HexagonISD::OP_END:        break;
16730b57cec5SDimitry Andric   }
16740b57cec5SDimitry Andric   return nullptr;
16750b57cec5SDimitry Andric }
16760b57cec5SDimitry Andric 
16770b57cec5SDimitry Andric void
16780b57cec5SDimitry Andric HexagonTargetLowering::validateConstPtrAlignment(SDValue Ptr, const SDLoc &dl,
16790b57cec5SDimitry Andric       unsigned NeedAlign) const {
16800b57cec5SDimitry Andric   auto *CA = dyn_cast<ConstantSDNode>(Ptr);
16810b57cec5SDimitry Andric   if (!CA)
16820b57cec5SDimitry Andric     return;
16830b57cec5SDimitry Andric   unsigned Addr = CA->getZExtValue();
16840b57cec5SDimitry Andric   unsigned HaveAlign = Addr != 0 ? 1u << countTrailingZeros(Addr) : NeedAlign;
16850b57cec5SDimitry Andric   if (HaveAlign < NeedAlign) {
16860b57cec5SDimitry Andric     std::string ErrMsg;
16870b57cec5SDimitry Andric     raw_string_ostream O(ErrMsg);
16880b57cec5SDimitry Andric     O << "Misaligned constant address: " << format_hex(Addr, 10)
16890b57cec5SDimitry Andric       << " has alignment " << HaveAlign
16900b57cec5SDimitry Andric       << ", but the memory access requires " << NeedAlign;
16910b57cec5SDimitry Andric     if (DebugLoc DL = dl.getDebugLoc())
16920b57cec5SDimitry Andric       DL.print(O << ", at ");
16930b57cec5SDimitry Andric     report_fatal_error(O.str());
16940b57cec5SDimitry Andric   }
16950b57cec5SDimitry Andric }
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric // Bit-reverse Load Intrinsic: Check if the instruction is a bit reverse load
16980b57cec5SDimitry Andric // intrinsic.
16990b57cec5SDimitry Andric static bool isBrevLdIntrinsic(const Value *Inst) {
17000b57cec5SDimitry Andric   unsigned ID = cast<IntrinsicInst>(Inst)->getIntrinsicID();
17010b57cec5SDimitry Andric   return (ID == Intrinsic::hexagon_L2_loadrd_pbr ||
17020b57cec5SDimitry Andric           ID == Intrinsic::hexagon_L2_loadri_pbr ||
17030b57cec5SDimitry Andric           ID == Intrinsic::hexagon_L2_loadrh_pbr ||
17040b57cec5SDimitry Andric           ID == Intrinsic::hexagon_L2_loadruh_pbr ||
17050b57cec5SDimitry Andric           ID == Intrinsic::hexagon_L2_loadrb_pbr ||
17060b57cec5SDimitry Andric           ID == Intrinsic::hexagon_L2_loadrub_pbr);
17070b57cec5SDimitry Andric }
17080b57cec5SDimitry Andric 
17090b57cec5SDimitry Andric // Bit-reverse Load Intrinsic :Crawl up and figure out the object from previous
17100b57cec5SDimitry Andric // instruction. So far we only handle bitcast, extract value and bit reverse
17110b57cec5SDimitry Andric // load intrinsic instructions. Should we handle CGEP ?
17120b57cec5SDimitry Andric static Value *getBrevLdObject(Value *V) {
17130b57cec5SDimitry Andric   if (Operator::getOpcode(V) == Instruction::ExtractValue ||
17140b57cec5SDimitry Andric       Operator::getOpcode(V) == Instruction::BitCast)
17150b57cec5SDimitry Andric     V = cast<Operator>(V)->getOperand(0);
17160b57cec5SDimitry Andric   else if (isa<IntrinsicInst>(V) && isBrevLdIntrinsic(V))
17170b57cec5SDimitry Andric     V = cast<Instruction>(V)->getOperand(0);
17180b57cec5SDimitry Andric   return V;
17190b57cec5SDimitry Andric }
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric // Bit-reverse Load Intrinsic: For a PHI Node return either an incoming edge or
17220b57cec5SDimitry Andric // a back edge. If the back edge comes from the intrinsic itself, the incoming
17230b57cec5SDimitry Andric // edge is returned.
17240b57cec5SDimitry Andric static Value *returnEdge(const PHINode *PN, Value *IntrBaseVal) {
17250b57cec5SDimitry Andric   const BasicBlock *Parent = PN->getParent();
17260b57cec5SDimitry Andric   int Idx = -1;
17270b57cec5SDimitry Andric   for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
17280b57cec5SDimitry Andric     BasicBlock *Blk = PN->getIncomingBlock(i);
17290b57cec5SDimitry Andric     // Determine if the back edge is originated from intrinsic.
17300b57cec5SDimitry Andric     if (Blk == Parent) {
17310b57cec5SDimitry Andric       Value *BackEdgeVal = PN->getIncomingValue(i);
17320b57cec5SDimitry Andric       Value *BaseVal;
17330b57cec5SDimitry Andric       // Loop over till we return the same Value or we hit the IntrBaseVal.
17340b57cec5SDimitry Andric       do {
17350b57cec5SDimitry Andric         BaseVal = BackEdgeVal;
17360b57cec5SDimitry Andric         BackEdgeVal = getBrevLdObject(BackEdgeVal);
17370b57cec5SDimitry Andric       } while ((BaseVal != BackEdgeVal) && (IntrBaseVal != BackEdgeVal));
17380b57cec5SDimitry Andric       // If the getBrevLdObject returns IntrBaseVal, we should return the
17390b57cec5SDimitry Andric       // incoming edge.
17400b57cec5SDimitry Andric       if (IntrBaseVal == BackEdgeVal)
17410b57cec5SDimitry Andric         continue;
17420b57cec5SDimitry Andric       Idx = i;
17430b57cec5SDimitry Andric       break;
17440b57cec5SDimitry Andric     } else // Set the node to incoming edge.
17450b57cec5SDimitry Andric       Idx = i;
17460b57cec5SDimitry Andric   }
17470b57cec5SDimitry Andric   assert(Idx >= 0 && "Unexpected index to incoming argument in PHI");
17480b57cec5SDimitry Andric   return PN->getIncomingValue(Idx);
17490b57cec5SDimitry Andric }
17500b57cec5SDimitry Andric 
17510b57cec5SDimitry Andric // Bit-reverse Load Intrinsic: Figure out the underlying object the base
17520b57cec5SDimitry Andric // pointer points to, for the bit-reverse load intrinsic. Setting this to
17530b57cec5SDimitry Andric // memoperand might help alias analysis to figure out the dependencies.
17540b57cec5SDimitry Andric static Value *getUnderLyingObjectForBrevLdIntr(Value *V) {
17550b57cec5SDimitry Andric   Value *IntrBaseVal = V;
17560b57cec5SDimitry Andric   Value *BaseVal;
17570b57cec5SDimitry Andric   // Loop over till we return the same Value, implies we either figure out
17580b57cec5SDimitry Andric   // the object or we hit a PHI
17590b57cec5SDimitry Andric   do {
17600b57cec5SDimitry Andric     BaseVal = V;
17610b57cec5SDimitry Andric     V = getBrevLdObject(V);
17620b57cec5SDimitry Andric   } while (BaseVal != V);
17630b57cec5SDimitry Andric 
17640b57cec5SDimitry Andric   // Identify the object from PHINode.
17650b57cec5SDimitry Andric   if (const PHINode *PN = dyn_cast<PHINode>(V))
17660b57cec5SDimitry Andric     return returnEdge(PN, IntrBaseVal);
17670b57cec5SDimitry Andric   // For non PHI nodes, the object is the last value returned by getBrevLdObject
17680b57cec5SDimitry Andric   else
17690b57cec5SDimitry Andric     return V;
17700b57cec5SDimitry Andric }
17710b57cec5SDimitry Andric 
17720b57cec5SDimitry Andric /// Given an intrinsic, checks if on the target the intrinsic will need to map
17730b57cec5SDimitry Andric /// to a MemIntrinsicNode (touches memory). If this is the case, it returns
17740b57cec5SDimitry Andric /// true and store the intrinsic information into the IntrinsicInfo that was
17750b57cec5SDimitry Andric /// passed to the function.
17760b57cec5SDimitry Andric bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
17770b57cec5SDimitry Andric                                                const CallInst &I,
17780b57cec5SDimitry Andric                                                MachineFunction &MF,
17790b57cec5SDimitry Andric                                                unsigned Intrinsic) const {
17800b57cec5SDimitry Andric   switch (Intrinsic) {
17810b57cec5SDimitry Andric   case Intrinsic::hexagon_L2_loadrd_pbr:
17820b57cec5SDimitry Andric   case Intrinsic::hexagon_L2_loadri_pbr:
17830b57cec5SDimitry Andric   case Intrinsic::hexagon_L2_loadrh_pbr:
17840b57cec5SDimitry Andric   case Intrinsic::hexagon_L2_loadruh_pbr:
17850b57cec5SDimitry Andric   case Intrinsic::hexagon_L2_loadrb_pbr:
17860b57cec5SDimitry Andric   case Intrinsic::hexagon_L2_loadrub_pbr: {
17870b57cec5SDimitry Andric     Info.opc = ISD::INTRINSIC_W_CHAIN;
17880b57cec5SDimitry Andric     auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
17890b57cec5SDimitry Andric     auto &Cont = I.getCalledFunction()->getParent()->getContext();
17900b57cec5SDimitry Andric     // The intrinsic function call is of the form { ElTy, i8* }
17910b57cec5SDimitry Andric     // @llvm.hexagon.L2.loadXX.pbr(i8*, i32). The pointer and memory access type
17920b57cec5SDimitry Andric     // should be derived from ElTy.
17930b57cec5SDimitry Andric     Type *ElTy = I.getCalledFunction()->getReturnType()->getStructElementType(0);
17940b57cec5SDimitry Andric     Info.memVT = MVT::getVT(ElTy);
17950b57cec5SDimitry Andric     llvm::Value *BasePtrVal = I.getOperand(0);
17960b57cec5SDimitry Andric     Info.ptrVal = getUnderLyingObjectForBrevLdIntr(BasePtrVal);
17970b57cec5SDimitry Andric     // The offset value comes through Modifier register. For now, assume the
17980b57cec5SDimitry Andric     // offset is 0.
17990b57cec5SDimitry Andric     Info.offset = 0;
1800*8bcb0991SDimitry Andric     Info.align =
1801*8bcb0991SDimitry Andric         MaybeAlign(DL.getABITypeAlignment(Info.memVT.getTypeForEVT(Cont)));
18020b57cec5SDimitry Andric     Info.flags = MachineMemOperand::MOLoad;
18030b57cec5SDimitry Andric     return true;
18040b57cec5SDimitry Andric   }
18050b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermw:
18060b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermw_128B:
18070b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermh:
18080b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermh_128B:
18090b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermhw:
18100b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermhw_128B:
18110b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermwq:
18120b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermwq_128B:
18130b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermhq:
18140b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermhq_128B:
18150b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermhwq:
18160b57cec5SDimitry Andric   case Intrinsic::hexagon_V6_vgathermhwq_128B: {
18170b57cec5SDimitry Andric     const Module &M = *I.getParent()->getParent()->getParent();
18180b57cec5SDimitry Andric     Info.opc = ISD::INTRINSIC_W_CHAIN;
18190b57cec5SDimitry Andric     Type *VecTy = I.getArgOperand(1)->getType();
18200b57cec5SDimitry Andric     Info.memVT = MVT::getVT(VecTy);
18210b57cec5SDimitry Andric     Info.ptrVal = I.getArgOperand(0);
18220b57cec5SDimitry Andric     Info.offset = 0;
1823*8bcb0991SDimitry Andric     Info.align =
1824*8bcb0991SDimitry Andric         MaybeAlign(M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8);
18250b57cec5SDimitry Andric     Info.flags = MachineMemOperand::MOLoad |
18260b57cec5SDimitry Andric                  MachineMemOperand::MOStore |
18270b57cec5SDimitry Andric                  MachineMemOperand::MOVolatile;
18280b57cec5SDimitry Andric     return true;
18290b57cec5SDimitry Andric   }
18300b57cec5SDimitry Andric   default:
18310b57cec5SDimitry Andric     break;
18320b57cec5SDimitry Andric   }
18330b57cec5SDimitry Andric   return false;
18340b57cec5SDimitry Andric }
18350b57cec5SDimitry Andric 
1836*8bcb0991SDimitry Andric bool HexagonTargetLowering::hasBitTest(SDValue X, SDValue Y) const {
1837*8bcb0991SDimitry Andric   return X.getValueType().isScalarInteger(); // 'tstbit'
1838*8bcb0991SDimitry Andric }
1839*8bcb0991SDimitry Andric 
18400b57cec5SDimitry Andric bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
18410b57cec5SDimitry Andric   return isTruncateFree(EVT::getEVT(Ty1), EVT::getEVT(Ty2));
18420b57cec5SDimitry Andric }
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
18450b57cec5SDimitry Andric   if (!VT1.isSimple() || !VT2.isSimple())
18460b57cec5SDimitry Andric     return false;
18470b57cec5SDimitry Andric   return VT1.getSimpleVT() == MVT::i64 && VT2.getSimpleVT() == MVT::i32;
18480b57cec5SDimitry Andric }
18490b57cec5SDimitry Andric 
18500b57cec5SDimitry Andric bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
18510b57cec5SDimitry Andric   return isOperationLegalOrCustom(ISD::FMA, VT);
18520b57cec5SDimitry Andric }
18530b57cec5SDimitry Andric 
18540b57cec5SDimitry Andric // Should we expand the build vector with shuffles?
18550b57cec5SDimitry Andric bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
18560b57cec5SDimitry Andric       unsigned DefinedValues) const {
18570b57cec5SDimitry Andric   return false;
18580b57cec5SDimitry Andric }
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
18610b57cec5SDimitry Andric                                                EVT VT) const {
18620b57cec5SDimitry Andric   return true;
18630b57cec5SDimitry Andric }
18640b57cec5SDimitry Andric 
18650b57cec5SDimitry Andric TargetLoweringBase::LegalizeTypeAction
18660b57cec5SDimitry Andric HexagonTargetLowering::getPreferredVectorAction(MVT VT) const {
1867*8bcb0991SDimitry Andric   unsigned VecLen = VT.getVectorNumElements();
1868*8bcb0991SDimitry Andric   MVT ElemTy = VT.getVectorElementType();
1869*8bcb0991SDimitry Andric 
1870*8bcb0991SDimitry Andric   if (VecLen == 1 || VT.isScalableVector())
18710b57cec5SDimitry Andric     return TargetLoweringBase::TypeScalarizeVector;
18720b57cec5SDimitry Andric 
18730b57cec5SDimitry Andric   if (Subtarget.useHVXOps()) {
1874*8bcb0991SDimitry Andric     unsigned HwLen = Subtarget.getVectorLength();
18750b57cec5SDimitry Andric     // If the size of VT is at least half of the vector length,
18760b57cec5SDimitry Andric     // widen the vector. Note: the threshold was not selected in
18770b57cec5SDimitry Andric     // any scientific way.
18780b57cec5SDimitry Andric     ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
18790b57cec5SDimitry Andric     if (llvm::find(Tys, ElemTy) != Tys.end()) {
1880*8bcb0991SDimitry Andric       unsigned HwWidth = 8*HwLen;
18810b57cec5SDimitry Andric       unsigned VecWidth = VT.getSizeInBits();
18820b57cec5SDimitry Andric       if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
18830b57cec5SDimitry Andric         return TargetLoweringBase::TypeWidenVector;
18840b57cec5SDimitry Andric     }
1885*8bcb0991SDimitry Andric     // Split vectors of i1 that correspond to (byte) vector pairs.
1886*8bcb0991SDimitry Andric     if (ElemTy == MVT::i1 && VecLen == 2*HwLen)
1887*8bcb0991SDimitry Andric       return TargetLoweringBase::TypeSplitVector;
18880b57cec5SDimitry Andric   }
1889*8bcb0991SDimitry Andric 
1890*8bcb0991SDimitry Andric   // Always widen (remaining) vectors of i1.
1891*8bcb0991SDimitry Andric   if (ElemTy == MVT::i1)
1892*8bcb0991SDimitry Andric     return TargetLoweringBase::TypeWidenVector;
1893*8bcb0991SDimitry Andric 
18940b57cec5SDimitry Andric   return TargetLoweringBase::TypeSplitVector;
18950b57cec5SDimitry Andric }
18960b57cec5SDimitry Andric 
18970b57cec5SDimitry Andric std::pair<SDValue, int>
18980b57cec5SDimitry Andric HexagonTargetLowering::getBaseAndOffset(SDValue Addr) const {
18990b57cec5SDimitry Andric   if (Addr.getOpcode() == ISD::ADD) {
19000b57cec5SDimitry Andric     SDValue Op1 = Addr.getOperand(1);
19010b57cec5SDimitry Andric     if (auto *CN = dyn_cast<const ConstantSDNode>(Op1.getNode()))
19020b57cec5SDimitry Andric       return { Addr.getOperand(0), CN->getSExtValue() };
19030b57cec5SDimitry Andric   }
19040b57cec5SDimitry Andric   return { Addr, 0 };
19050b57cec5SDimitry Andric }
19060b57cec5SDimitry Andric 
19070b57cec5SDimitry Andric // Lower a vector shuffle (V1, V2, V3).  V1 and V2 are the two vectors
19080b57cec5SDimitry Andric // to select data from, V3 is the permutation.
19090b57cec5SDimitry Andric SDValue
19100b57cec5SDimitry Andric HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
19110b57cec5SDimitry Andric       const {
19120b57cec5SDimitry Andric   const auto *SVN = cast<ShuffleVectorSDNode>(Op);
19130b57cec5SDimitry Andric   ArrayRef<int> AM = SVN->getMask();
19140b57cec5SDimitry Andric   assert(AM.size() <= 8 && "Unexpected shuffle mask");
19150b57cec5SDimitry Andric   unsigned VecLen = AM.size();
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric   MVT VecTy = ty(Op);
19180b57cec5SDimitry Andric   assert(!Subtarget.isHVXVectorType(VecTy, true) &&
19190b57cec5SDimitry Andric          "HVX shuffles should be legal");
19200b57cec5SDimitry Andric   assert(VecTy.getSizeInBits() <= 64 && "Unexpected vector length");
19210b57cec5SDimitry Andric 
19220b57cec5SDimitry Andric   SDValue Op0 = Op.getOperand(0);
19230b57cec5SDimitry Andric   SDValue Op1 = Op.getOperand(1);
19240b57cec5SDimitry Andric   const SDLoc &dl(Op);
19250b57cec5SDimitry Andric 
19260b57cec5SDimitry Andric   // If the inputs are not the same as the output, bail. This is not an
19270b57cec5SDimitry Andric   // error situation, but complicates the handling and the default expansion
19280b57cec5SDimitry Andric   // (into BUILD_VECTOR) should be adequate.
19290b57cec5SDimitry Andric   if (ty(Op0) != VecTy || ty(Op1) != VecTy)
19300b57cec5SDimitry Andric     return SDValue();
19310b57cec5SDimitry Andric 
19320b57cec5SDimitry Andric   // Normalize the mask so that the first non-negative index comes from
19330b57cec5SDimitry Andric   // the first operand.
19340b57cec5SDimitry Andric   SmallVector<int,8> Mask(AM.begin(), AM.end());
19350b57cec5SDimitry Andric   unsigned F = llvm::find_if(AM, [](int M) { return M >= 0; }) - AM.data();
19360b57cec5SDimitry Andric   if (F == AM.size())
19370b57cec5SDimitry Andric     return DAG.getUNDEF(VecTy);
19380b57cec5SDimitry Andric   if (AM[F] >= int(VecLen)) {
19390b57cec5SDimitry Andric     ShuffleVectorSDNode::commuteMask(Mask);
19400b57cec5SDimitry Andric     std::swap(Op0, Op1);
19410b57cec5SDimitry Andric   }
19420b57cec5SDimitry Andric 
19430b57cec5SDimitry Andric   // Express the shuffle mask in terms of bytes.
19440b57cec5SDimitry Andric   SmallVector<int,8> ByteMask;
19450b57cec5SDimitry Andric   unsigned ElemBytes = VecTy.getVectorElementType().getSizeInBits() / 8;
19460b57cec5SDimitry Andric   for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
19470b57cec5SDimitry Andric     int M = Mask[i];
19480b57cec5SDimitry Andric     if (M < 0) {
19490b57cec5SDimitry Andric       for (unsigned j = 0; j != ElemBytes; ++j)
19500b57cec5SDimitry Andric         ByteMask.push_back(-1);
19510b57cec5SDimitry Andric     } else {
19520b57cec5SDimitry Andric       for (unsigned j = 0; j != ElemBytes; ++j)
19530b57cec5SDimitry Andric         ByteMask.push_back(M*ElemBytes + j);
19540b57cec5SDimitry Andric     }
19550b57cec5SDimitry Andric   }
19560b57cec5SDimitry Andric   assert(ByteMask.size() <= 8);
19570b57cec5SDimitry Andric 
19580b57cec5SDimitry Andric   // All non-undef (non-negative) indexes are well within [0..127], so they
19590b57cec5SDimitry Andric   // fit in a single byte. Build two 64-bit words:
19600b57cec5SDimitry Andric   // - MaskIdx where each byte is the corresponding index (for non-negative
19610b57cec5SDimitry Andric   //   indexes), and 0xFF for negative indexes, and
19620b57cec5SDimitry Andric   // - MaskUnd that has 0xFF for each negative index.
19630b57cec5SDimitry Andric   uint64_t MaskIdx = 0;
19640b57cec5SDimitry Andric   uint64_t MaskUnd = 0;
19650b57cec5SDimitry Andric   for (unsigned i = 0, e = ByteMask.size(); i != e; ++i) {
19660b57cec5SDimitry Andric     unsigned S = 8*i;
19670b57cec5SDimitry Andric     uint64_t M = ByteMask[i] & 0xFF;
19680b57cec5SDimitry Andric     if (M == 0xFF)
19690b57cec5SDimitry Andric       MaskUnd |= M << S;
19700b57cec5SDimitry Andric     MaskIdx |= M << S;
19710b57cec5SDimitry Andric   }
19720b57cec5SDimitry Andric 
19730b57cec5SDimitry Andric   if (ByteMask.size() == 4) {
19740b57cec5SDimitry Andric     // Identity.
19750b57cec5SDimitry Andric     if (MaskIdx == (0x03020100 | MaskUnd))
19760b57cec5SDimitry Andric       return Op0;
19770b57cec5SDimitry Andric     // Byte swap.
19780b57cec5SDimitry Andric     if (MaskIdx == (0x00010203 | MaskUnd)) {
19790b57cec5SDimitry Andric       SDValue T0 = DAG.getBitcast(MVT::i32, Op0);
19800b57cec5SDimitry Andric       SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i32, T0);
19810b57cec5SDimitry Andric       return DAG.getBitcast(VecTy, T1);
19820b57cec5SDimitry Andric     }
19830b57cec5SDimitry Andric 
19840b57cec5SDimitry Andric     // Byte packs.
19850b57cec5SDimitry Andric     SDValue Concat10 = DAG.getNode(HexagonISD::COMBINE, dl,
19860b57cec5SDimitry Andric                                    typeJoin({ty(Op1), ty(Op0)}), {Op1, Op0});
19870b57cec5SDimitry Andric     if (MaskIdx == (0x06040200 | MaskUnd))
19880b57cec5SDimitry Andric       return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat10}, DAG);
19890b57cec5SDimitry Andric     if (MaskIdx == (0x07050301 | MaskUnd))
19900b57cec5SDimitry Andric       return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat10}, DAG);
19910b57cec5SDimitry Andric 
19920b57cec5SDimitry Andric     SDValue Concat01 = DAG.getNode(HexagonISD::COMBINE, dl,
19930b57cec5SDimitry Andric                                    typeJoin({ty(Op0), ty(Op1)}), {Op0, Op1});
19940b57cec5SDimitry Andric     if (MaskIdx == (0x02000604 | MaskUnd))
19950b57cec5SDimitry Andric       return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat01}, DAG);
19960b57cec5SDimitry Andric     if (MaskIdx == (0x03010705 | MaskUnd))
19970b57cec5SDimitry Andric       return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat01}, DAG);
19980b57cec5SDimitry Andric   }
19990b57cec5SDimitry Andric 
20000b57cec5SDimitry Andric   if (ByteMask.size() == 8) {
20010b57cec5SDimitry Andric     // Identity.
20020b57cec5SDimitry Andric     if (MaskIdx == (0x0706050403020100ull | MaskUnd))
20030b57cec5SDimitry Andric       return Op0;
20040b57cec5SDimitry Andric     // Byte swap.
20050b57cec5SDimitry Andric     if (MaskIdx == (0x0001020304050607ull | MaskUnd)) {
20060b57cec5SDimitry Andric       SDValue T0 = DAG.getBitcast(MVT::i64, Op0);
20070b57cec5SDimitry Andric       SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i64, T0);
20080b57cec5SDimitry Andric       return DAG.getBitcast(VecTy, T1);
20090b57cec5SDimitry Andric     }
20100b57cec5SDimitry Andric 
20110b57cec5SDimitry Andric     // Halfword picks.
20120b57cec5SDimitry Andric     if (MaskIdx == (0x0d0c050409080100ull | MaskUnd))
20130b57cec5SDimitry Andric       return getInstr(Hexagon::S2_shuffeh, dl, VecTy, {Op1, Op0}, DAG);
20140b57cec5SDimitry Andric     if (MaskIdx == (0x0f0e07060b0a0302ull | MaskUnd))
20150b57cec5SDimitry Andric       return getInstr(Hexagon::S2_shuffoh, dl, VecTy, {Op1, Op0}, DAG);
20160b57cec5SDimitry Andric     if (MaskIdx == (0x0d0c090805040100ull | MaskUnd))
20170b57cec5SDimitry Andric       return getInstr(Hexagon::S2_vtrunewh, dl, VecTy, {Op1, Op0}, DAG);
20180b57cec5SDimitry Andric     if (MaskIdx == (0x0f0e0b0a07060302ull | MaskUnd))
20190b57cec5SDimitry Andric       return getInstr(Hexagon::S2_vtrunowh, dl, VecTy, {Op1, Op0}, DAG);
20200b57cec5SDimitry Andric     if (MaskIdx == (0x0706030205040100ull | MaskUnd)) {
20210b57cec5SDimitry Andric       VectorPair P = opSplit(Op0, dl, DAG);
20220b57cec5SDimitry Andric       return getInstr(Hexagon::S2_packhl, dl, VecTy, {P.second, P.first}, DAG);
20230b57cec5SDimitry Andric     }
20240b57cec5SDimitry Andric 
20250b57cec5SDimitry Andric     // Byte packs.
20260b57cec5SDimitry Andric     if (MaskIdx == (0x0e060c040a020800ull | MaskUnd))
20270b57cec5SDimitry Andric       return getInstr(Hexagon::S2_shuffeb, dl, VecTy, {Op1, Op0}, DAG);
20280b57cec5SDimitry Andric     if (MaskIdx == (0x0f070d050b030901ull | MaskUnd))
20290b57cec5SDimitry Andric       return getInstr(Hexagon::S2_shuffob, dl, VecTy, {Op1, Op0}, DAG);
20300b57cec5SDimitry Andric   }
20310b57cec5SDimitry Andric 
20320b57cec5SDimitry Andric   return SDValue();
20330b57cec5SDimitry Andric }
20340b57cec5SDimitry Andric 
20350b57cec5SDimitry Andric // Create a Hexagon-specific node for shifting a vector by an integer.
20360b57cec5SDimitry Andric SDValue
20370b57cec5SDimitry Andric HexagonTargetLowering::getVectorShiftByInt(SDValue Op, SelectionDAG &DAG)
20380b57cec5SDimitry Andric       const {
20390b57cec5SDimitry Andric   if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) {
20400b57cec5SDimitry Andric     if (SDValue S = BVN->getSplatValue()) {
20410b57cec5SDimitry Andric       unsigned NewOpc;
20420b57cec5SDimitry Andric       switch (Op.getOpcode()) {
20430b57cec5SDimitry Andric         case ISD::SHL:
20440b57cec5SDimitry Andric           NewOpc = HexagonISD::VASL;
20450b57cec5SDimitry Andric           break;
20460b57cec5SDimitry Andric         case ISD::SRA:
20470b57cec5SDimitry Andric           NewOpc = HexagonISD::VASR;
20480b57cec5SDimitry Andric           break;
20490b57cec5SDimitry Andric         case ISD::SRL:
20500b57cec5SDimitry Andric           NewOpc = HexagonISD::VLSR;
20510b57cec5SDimitry Andric           break;
20520b57cec5SDimitry Andric         default:
20530b57cec5SDimitry Andric           llvm_unreachable("Unexpected shift opcode");
20540b57cec5SDimitry Andric       }
20550b57cec5SDimitry Andric       return DAG.getNode(NewOpc, SDLoc(Op), ty(Op), Op.getOperand(0), S);
20560b57cec5SDimitry Andric     }
20570b57cec5SDimitry Andric   }
20580b57cec5SDimitry Andric 
20590b57cec5SDimitry Andric   return SDValue();
20600b57cec5SDimitry Andric }
20610b57cec5SDimitry Andric 
20620b57cec5SDimitry Andric SDValue
20630b57cec5SDimitry Andric HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
20640b57cec5SDimitry Andric   return getVectorShiftByInt(Op, DAG);
20650b57cec5SDimitry Andric }
20660b57cec5SDimitry Andric 
20670b57cec5SDimitry Andric SDValue
20680b57cec5SDimitry Andric HexagonTargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
20690b57cec5SDimitry Andric   if (isa<ConstantSDNode>(Op.getOperand(1).getNode()))
20700b57cec5SDimitry Andric     return Op;
20710b57cec5SDimitry Andric   return SDValue();
20720b57cec5SDimitry Andric }
20730b57cec5SDimitry Andric 
20740b57cec5SDimitry Andric SDValue
20750b57cec5SDimitry Andric HexagonTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
20760b57cec5SDimitry Andric   MVT ResTy = ty(Op);
20770b57cec5SDimitry Andric   SDValue InpV = Op.getOperand(0);
20780b57cec5SDimitry Andric   MVT InpTy = ty(InpV);
20790b57cec5SDimitry Andric   assert(ResTy.getSizeInBits() == InpTy.getSizeInBits());
20800b57cec5SDimitry Andric   const SDLoc &dl(Op);
20810b57cec5SDimitry Andric 
20820b57cec5SDimitry Andric   // Handle conversion from i8 to v8i1.
20830b57cec5SDimitry Andric   if (ResTy == MVT::v8i1) {
20840b57cec5SDimitry Andric     SDValue Sc = DAG.getBitcast(tyScalar(InpTy), InpV);
20850b57cec5SDimitry Andric     SDValue Ext = DAG.getZExtOrTrunc(Sc, dl, MVT::i32);
20860b57cec5SDimitry Andric     return getInstr(Hexagon::C2_tfrrp, dl, ResTy, Ext, DAG);
20870b57cec5SDimitry Andric   }
20880b57cec5SDimitry Andric 
20890b57cec5SDimitry Andric   return SDValue();
20900b57cec5SDimitry Andric }
20910b57cec5SDimitry Andric 
20920b57cec5SDimitry Andric bool
20930b57cec5SDimitry Andric HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
20940b57cec5SDimitry Andric       MVT VecTy, SelectionDAG &DAG,
20950b57cec5SDimitry Andric       MutableArrayRef<ConstantInt*> Consts) const {
20960b57cec5SDimitry Andric   MVT ElemTy = VecTy.getVectorElementType();
20970b57cec5SDimitry Andric   unsigned ElemWidth = ElemTy.getSizeInBits();
20980b57cec5SDimitry Andric   IntegerType *IntTy = IntegerType::get(*DAG.getContext(), ElemWidth);
20990b57cec5SDimitry Andric   bool AllConst = true;
21000b57cec5SDimitry Andric 
21010b57cec5SDimitry Andric   for (unsigned i = 0, e = Values.size(); i != e; ++i) {
21020b57cec5SDimitry Andric     SDValue V = Values[i];
21030b57cec5SDimitry Andric     if (V.isUndef()) {
21040b57cec5SDimitry Andric       Consts[i] = ConstantInt::get(IntTy, 0);
21050b57cec5SDimitry Andric       continue;
21060b57cec5SDimitry Andric     }
21070b57cec5SDimitry Andric     // Make sure to always cast to IntTy.
21080b57cec5SDimitry Andric     if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
21090b57cec5SDimitry Andric       const ConstantInt *CI = CN->getConstantIntValue();
21100b57cec5SDimitry Andric       Consts[i] = ConstantInt::get(IntTy, CI->getValue().getSExtValue());
21110b57cec5SDimitry Andric     } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
21120b57cec5SDimitry Andric       const ConstantFP *CF = CN->getConstantFPValue();
21130b57cec5SDimitry Andric       APInt A = CF->getValueAPF().bitcastToAPInt();
21140b57cec5SDimitry Andric       Consts[i] = ConstantInt::get(IntTy, A.getZExtValue());
21150b57cec5SDimitry Andric     } else {
21160b57cec5SDimitry Andric       AllConst = false;
21170b57cec5SDimitry Andric     }
21180b57cec5SDimitry Andric   }
21190b57cec5SDimitry Andric   return AllConst;
21200b57cec5SDimitry Andric }
21210b57cec5SDimitry Andric 
21220b57cec5SDimitry Andric SDValue
21230b57cec5SDimitry Andric HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
21240b57cec5SDimitry Andric                                      MVT VecTy, SelectionDAG &DAG) const {
21250b57cec5SDimitry Andric   MVT ElemTy = VecTy.getVectorElementType();
21260b57cec5SDimitry Andric   assert(VecTy.getVectorNumElements() == Elem.size());
21270b57cec5SDimitry Andric 
21280b57cec5SDimitry Andric   SmallVector<ConstantInt*,4> Consts(Elem.size());
21290b57cec5SDimitry Andric   bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
21300b57cec5SDimitry Andric 
21310b57cec5SDimitry Andric   unsigned First, Num = Elem.size();
21320b57cec5SDimitry Andric   for (First = 0; First != Num; ++First)
21330b57cec5SDimitry Andric     if (!isUndef(Elem[First]))
21340b57cec5SDimitry Andric       break;
21350b57cec5SDimitry Andric   if (First == Num)
21360b57cec5SDimitry Andric     return DAG.getUNDEF(VecTy);
21370b57cec5SDimitry Andric 
21380b57cec5SDimitry Andric   if (AllConst &&
21390b57cec5SDimitry Andric       llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
21400b57cec5SDimitry Andric     return getZero(dl, VecTy, DAG);
21410b57cec5SDimitry Andric 
21420b57cec5SDimitry Andric   if (ElemTy == MVT::i16) {
21430b57cec5SDimitry Andric     assert(Elem.size() == 2);
21440b57cec5SDimitry Andric     if (AllConst) {
21450b57cec5SDimitry Andric       uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
21460b57cec5SDimitry Andric                    Consts[1]->getZExtValue() << 16;
21470b57cec5SDimitry Andric       return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
21480b57cec5SDimitry Andric     }
21490b57cec5SDimitry Andric     SDValue N = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32,
21500b57cec5SDimitry Andric                          {Elem[1], Elem[0]}, DAG);
21510b57cec5SDimitry Andric     return DAG.getBitcast(MVT::v2i16, N);
21520b57cec5SDimitry Andric   }
21530b57cec5SDimitry Andric 
21540b57cec5SDimitry Andric   if (ElemTy == MVT::i8) {
21550b57cec5SDimitry Andric     // First try generating a constant.
21560b57cec5SDimitry Andric     if (AllConst) {
21570b57cec5SDimitry Andric       int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
21580b57cec5SDimitry Andric                   (Consts[1]->getZExtValue() & 0xFF) << 8 |
21590b57cec5SDimitry Andric                   (Consts[1]->getZExtValue() & 0xFF) << 16 |
21600b57cec5SDimitry Andric                   Consts[2]->getZExtValue() << 24;
21610b57cec5SDimitry Andric       return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
21620b57cec5SDimitry Andric     }
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric     // Then try splat.
21650b57cec5SDimitry Andric     bool IsSplat = true;
21660b57cec5SDimitry Andric     for (unsigned i = 0; i != Num; ++i) {
21670b57cec5SDimitry Andric       if (i == First)
21680b57cec5SDimitry Andric         continue;
21690b57cec5SDimitry Andric       if (Elem[i] == Elem[First] || isUndef(Elem[i]))
21700b57cec5SDimitry Andric         continue;
21710b57cec5SDimitry Andric       IsSplat = false;
21720b57cec5SDimitry Andric       break;
21730b57cec5SDimitry Andric     }
21740b57cec5SDimitry Andric     if (IsSplat) {
21750b57cec5SDimitry Andric       // Legalize the operand to VSPLAT.
21760b57cec5SDimitry Andric       SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
21770b57cec5SDimitry Andric       return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
21780b57cec5SDimitry Andric     }
21790b57cec5SDimitry Andric 
21800b57cec5SDimitry Andric     // Generate
21810b57cec5SDimitry Andric     //   (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
21820b57cec5SDimitry Andric     //   (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
21830b57cec5SDimitry Andric     assert(Elem.size() == 4);
21840b57cec5SDimitry Andric     SDValue Vs[4];
21850b57cec5SDimitry Andric     for (unsigned i = 0; i != 4; ++i) {
21860b57cec5SDimitry Andric       Vs[i] = DAG.getZExtOrTrunc(Elem[i], dl, MVT::i32);
21870b57cec5SDimitry Andric       Vs[i] = DAG.getZeroExtendInReg(Vs[i], dl, MVT::i8);
21880b57cec5SDimitry Andric     }
21890b57cec5SDimitry Andric     SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
21900b57cec5SDimitry Andric     SDValue T0 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[1], S8});
21910b57cec5SDimitry Andric     SDValue T1 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[3], S8});
21920b57cec5SDimitry Andric     SDValue B0 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[0], T0});
21930b57cec5SDimitry Andric     SDValue B1 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[2], T1});
21940b57cec5SDimitry Andric 
21950b57cec5SDimitry Andric     SDValue R = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32, {B1, B0}, DAG);
21960b57cec5SDimitry Andric     return DAG.getBitcast(MVT::v4i8, R);
21970b57cec5SDimitry Andric   }
21980b57cec5SDimitry Andric 
21990b57cec5SDimitry Andric #ifndef NDEBUG
22000b57cec5SDimitry Andric   dbgs() << "VecTy: " << EVT(VecTy).getEVTString() << '\n';
22010b57cec5SDimitry Andric #endif
22020b57cec5SDimitry Andric   llvm_unreachable("Unexpected vector element type");
22030b57cec5SDimitry Andric }
22040b57cec5SDimitry Andric 
22050b57cec5SDimitry Andric SDValue
22060b57cec5SDimitry Andric HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
22070b57cec5SDimitry Andric                                      MVT VecTy, SelectionDAG &DAG) const {
22080b57cec5SDimitry Andric   MVT ElemTy = VecTy.getVectorElementType();
22090b57cec5SDimitry Andric   assert(VecTy.getVectorNumElements() == Elem.size());
22100b57cec5SDimitry Andric 
22110b57cec5SDimitry Andric   SmallVector<ConstantInt*,8> Consts(Elem.size());
22120b57cec5SDimitry Andric   bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
22130b57cec5SDimitry Andric 
22140b57cec5SDimitry Andric   unsigned First, Num = Elem.size();
22150b57cec5SDimitry Andric   for (First = 0; First != Num; ++First)
22160b57cec5SDimitry Andric     if (!isUndef(Elem[First]))
22170b57cec5SDimitry Andric       break;
22180b57cec5SDimitry Andric   if (First == Num)
22190b57cec5SDimitry Andric     return DAG.getUNDEF(VecTy);
22200b57cec5SDimitry Andric 
22210b57cec5SDimitry Andric   if (AllConst &&
22220b57cec5SDimitry Andric       llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
22230b57cec5SDimitry Andric     return getZero(dl, VecTy, DAG);
22240b57cec5SDimitry Andric 
22250b57cec5SDimitry Andric   // First try splat if possible.
22260b57cec5SDimitry Andric   if (ElemTy == MVT::i16) {
22270b57cec5SDimitry Andric     bool IsSplat = true;
22280b57cec5SDimitry Andric     for (unsigned i = 0; i != Num; ++i) {
22290b57cec5SDimitry Andric       if (i == First)
22300b57cec5SDimitry Andric         continue;
22310b57cec5SDimitry Andric       if (Elem[i] == Elem[First] || isUndef(Elem[i]))
22320b57cec5SDimitry Andric         continue;
22330b57cec5SDimitry Andric       IsSplat = false;
22340b57cec5SDimitry Andric       break;
22350b57cec5SDimitry Andric     }
22360b57cec5SDimitry Andric     if (IsSplat) {
22370b57cec5SDimitry Andric       // Legalize the operand to VSPLAT.
22380b57cec5SDimitry Andric       SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
22390b57cec5SDimitry Andric       return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
22400b57cec5SDimitry Andric     }
22410b57cec5SDimitry Andric   }
22420b57cec5SDimitry Andric 
22430b57cec5SDimitry Andric   // Then try constant.
22440b57cec5SDimitry Andric   if (AllConst) {
22450b57cec5SDimitry Andric     uint64_t Val = 0;
22460b57cec5SDimitry Andric     unsigned W = ElemTy.getSizeInBits();
22470b57cec5SDimitry Andric     uint64_t Mask = (ElemTy == MVT::i8)  ? 0xFFull
22480b57cec5SDimitry Andric                   : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
22490b57cec5SDimitry Andric     for (unsigned i = 0; i != Num; ++i)
22500b57cec5SDimitry Andric       Val = (Val << W) | (Consts[Num-1-i]->getZExtValue() & Mask);
22510b57cec5SDimitry Andric     SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
22520b57cec5SDimitry Andric     return DAG.getBitcast(VecTy, V0);
22530b57cec5SDimitry Andric   }
22540b57cec5SDimitry Andric 
22550b57cec5SDimitry Andric   // Build two 32-bit vectors and concatenate.
22560b57cec5SDimitry Andric   MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
22570b57cec5SDimitry Andric   SDValue L = (ElemTy == MVT::i32)
22580b57cec5SDimitry Andric                 ? Elem[0]
22590b57cec5SDimitry Andric                 : buildVector32(Elem.take_front(Num/2), dl, HalfTy, DAG);
22600b57cec5SDimitry Andric   SDValue H = (ElemTy == MVT::i32)
22610b57cec5SDimitry Andric                 ? Elem[1]
22620b57cec5SDimitry Andric                 : buildVector32(Elem.drop_front(Num/2), dl, HalfTy, DAG);
22630b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
22640b57cec5SDimitry Andric }
22650b57cec5SDimitry Andric 
22660b57cec5SDimitry Andric SDValue
22670b57cec5SDimitry Andric HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
22680b57cec5SDimitry Andric                                      const SDLoc &dl, MVT ValTy, MVT ResTy,
22690b57cec5SDimitry Andric                                      SelectionDAG &DAG) const {
22700b57cec5SDimitry Andric   MVT VecTy = ty(VecV);
22710b57cec5SDimitry Andric   assert(!ValTy.isVector() ||
22720b57cec5SDimitry Andric          VecTy.getVectorElementType() == ValTy.getVectorElementType());
22730b57cec5SDimitry Andric   unsigned VecWidth = VecTy.getSizeInBits();
22740b57cec5SDimitry Andric   unsigned ValWidth = ValTy.getSizeInBits();
22750b57cec5SDimitry Andric   unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
22760b57cec5SDimitry Andric   assert((VecWidth % ElemWidth) == 0);
22770b57cec5SDimitry Andric   auto *IdxN = dyn_cast<ConstantSDNode>(IdxV);
22780b57cec5SDimitry Andric 
22790b57cec5SDimitry Andric   // Special case for v{8,4,2}i1 (the only boolean vectors legal in Hexagon
22800b57cec5SDimitry Andric   // without any coprocessors).
22810b57cec5SDimitry Andric   if (ElemWidth == 1) {
22820b57cec5SDimitry Andric     assert(VecWidth == VecTy.getVectorNumElements() && "Sanity failure");
22830b57cec5SDimitry Andric     assert(VecWidth == 8 || VecWidth == 4 || VecWidth == 2);
22840b57cec5SDimitry Andric     // Check if this is an extract of the lowest bit.
22850b57cec5SDimitry Andric     if (IdxN) {
22860b57cec5SDimitry Andric       // Extracting the lowest bit is a no-op, but it changes the type,
22870b57cec5SDimitry Andric       // so it must be kept as an operation to avoid errors related to
22880b57cec5SDimitry Andric       // type mismatches.
22890b57cec5SDimitry Andric       if (IdxN->isNullValue() && ValTy.getSizeInBits() == 1)
22900b57cec5SDimitry Andric         return DAG.getNode(HexagonISD::TYPECAST, dl, MVT::i1, VecV);
22910b57cec5SDimitry Andric     }
22920b57cec5SDimitry Andric 
22930b57cec5SDimitry Andric     // If the value extracted is a single bit, use tstbit.
22940b57cec5SDimitry Andric     if (ValWidth == 1) {
22950b57cec5SDimitry Andric       SDValue A0 = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32, {VecV}, DAG);
22960b57cec5SDimitry Andric       SDValue M0 = DAG.getConstant(8 / VecWidth, dl, MVT::i32);
22970b57cec5SDimitry Andric       SDValue I0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, M0);
22980b57cec5SDimitry Andric       return DAG.getNode(HexagonISD::TSTBIT, dl, MVT::i1, A0, I0);
22990b57cec5SDimitry Andric     }
23000b57cec5SDimitry Andric 
23010b57cec5SDimitry Andric     // Each bool vector (v2i1, v4i1, v8i1) always occupies 8 bits in
23020b57cec5SDimitry Andric     // a predicate register. The elements of the vector are repeated
23030b57cec5SDimitry Andric     // in the register (if necessary) so that the total number is 8.
23040b57cec5SDimitry Andric     // The extracted subvector will need to be expanded in such a way.
23050b57cec5SDimitry Andric     unsigned Scale = VecWidth / ValWidth;
23060b57cec5SDimitry Andric 
23070b57cec5SDimitry Andric     // Generate (p2d VecV) >> 8*Idx to move the interesting bytes to
23080b57cec5SDimitry Andric     // position 0.
23090b57cec5SDimitry Andric     assert(ty(IdxV) == MVT::i32);
23100b57cec5SDimitry Andric     unsigned VecRep = 8 / VecWidth;
23110b57cec5SDimitry Andric     SDValue S0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
23120b57cec5SDimitry Andric                              DAG.getConstant(8*VecRep, dl, MVT::i32));
23130b57cec5SDimitry Andric     SDValue T0 = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
23140b57cec5SDimitry Andric     SDValue T1 = DAG.getNode(ISD::SRL, dl, MVT::i64, T0, S0);
23150b57cec5SDimitry Andric     while (Scale > 1) {
23160b57cec5SDimitry Andric       // The longest possible subvector is at most 32 bits, so it is always
23170b57cec5SDimitry Andric       // contained in the low subregister.
23180b57cec5SDimitry Andric       T1 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, T1);
23190b57cec5SDimitry Andric       T1 = expandPredicate(T1, dl, DAG);
23200b57cec5SDimitry Andric       Scale /= 2;
23210b57cec5SDimitry Andric     }
23220b57cec5SDimitry Andric 
23230b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::D2P, dl, ResTy, T1);
23240b57cec5SDimitry Andric   }
23250b57cec5SDimitry Andric 
23260b57cec5SDimitry Andric   assert(VecWidth == 32 || VecWidth == 64);
23270b57cec5SDimitry Andric 
23280b57cec5SDimitry Andric   // Cast everything to scalar integer types.
23290b57cec5SDimitry Andric   MVT ScalarTy = tyScalar(VecTy);
23300b57cec5SDimitry Andric   VecV = DAG.getBitcast(ScalarTy, VecV);
23310b57cec5SDimitry Andric 
23320b57cec5SDimitry Andric   SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
23330b57cec5SDimitry Andric   SDValue ExtV;
23340b57cec5SDimitry Andric 
23350b57cec5SDimitry Andric   if (IdxN) {
23360b57cec5SDimitry Andric     unsigned Off = IdxN->getZExtValue() * ElemWidth;
23370b57cec5SDimitry Andric     if (VecWidth == 64 && ValWidth == 32) {
23380b57cec5SDimitry Andric       assert(Off == 0 || Off == 32);
23390b57cec5SDimitry Andric       unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
23400b57cec5SDimitry Andric       ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
23410b57cec5SDimitry Andric     } else if (Off == 0 && (ValWidth % 8) == 0) {
23420b57cec5SDimitry Andric       ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
23430b57cec5SDimitry Andric     } else {
23440b57cec5SDimitry Andric       SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
23450b57cec5SDimitry Andric       // The return type of EXTRACTU must be the same as the type of the
23460b57cec5SDimitry Andric       // input vector.
23470b57cec5SDimitry Andric       ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
23480b57cec5SDimitry Andric                          {VecV, WidthV, OffV});
23490b57cec5SDimitry Andric     }
23500b57cec5SDimitry Andric   } else {
23510b57cec5SDimitry Andric     if (ty(IdxV) != MVT::i32)
23520b57cec5SDimitry Andric       IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
23530b57cec5SDimitry Andric     SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
23540b57cec5SDimitry Andric                                DAG.getConstant(ElemWidth, dl, MVT::i32));
23550b57cec5SDimitry Andric     ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
23560b57cec5SDimitry Andric                        {VecV, WidthV, OffV});
23570b57cec5SDimitry Andric   }
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric   // Cast ExtV to the requested result type.
23600b57cec5SDimitry Andric   ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
23610b57cec5SDimitry Andric   ExtV = DAG.getBitcast(ResTy, ExtV);
23620b57cec5SDimitry Andric   return ExtV;
23630b57cec5SDimitry Andric }
23640b57cec5SDimitry Andric 
23650b57cec5SDimitry Andric SDValue
23660b57cec5SDimitry Andric HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
23670b57cec5SDimitry Andric                                     const SDLoc &dl, MVT ValTy,
23680b57cec5SDimitry Andric                                     SelectionDAG &DAG) const {
23690b57cec5SDimitry Andric   MVT VecTy = ty(VecV);
23700b57cec5SDimitry Andric   if (VecTy.getVectorElementType() == MVT::i1) {
23710b57cec5SDimitry Andric     MVT ValTy = ty(ValV);
23720b57cec5SDimitry Andric     assert(ValTy.getVectorElementType() == MVT::i1);
23730b57cec5SDimitry Andric     SDValue ValR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, ValV);
23740b57cec5SDimitry Andric     unsigned VecLen = VecTy.getVectorNumElements();
23750b57cec5SDimitry Andric     unsigned Scale = VecLen / ValTy.getVectorNumElements();
23760b57cec5SDimitry Andric     assert(Scale > 1);
23770b57cec5SDimitry Andric 
23780b57cec5SDimitry Andric     for (unsigned R = Scale; R > 1; R /= 2) {
23790b57cec5SDimitry Andric       ValR = contractPredicate(ValR, dl, DAG);
23800b57cec5SDimitry Andric       ValR = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
23810b57cec5SDimitry Andric                          DAG.getUNDEF(MVT::i32), ValR);
23820b57cec5SDimitry Andric     }
23830b57cec5SDimitry Andric     // The longest possible subvector is at most 32 bits, so it is always
23840b57cec5SDimitry Andric     // contained in the low subregister.
23850b57cec5SDimitry Andric     ValR = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, ValR);
23860b57cec5SDimitry Andric 
23870b57cec5SDimitry Andric     unsigned ValBytes = 64 / Scale;
23880b57cec5SDimitry Andric     SDValue Width = DAG.getConstant(ValBytes*8, dl, MVT::i32);
23890b57cec5SDimitry Andric     SDValue Idx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
23900b57cec5SDimitry Andric                               DAG.getConstant(8, dl, MVT::i32));
23910b57cec5SDimitry Andric     SDValue VecR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
23920b57cec5SDimitry Andric     SDValue Ins = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
23930b57cec5SDimitry Andric                               {VecR, ValR, Width, Idx});
23940b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::D2P, dl, VecTy, Ins);
23950b57cec5SDimitry Andric   }
23960b57cec5SDimitry Andric 
23970b57cec5SDimitry Andric   unsigned VecWidth = VecTy.getSizeInBits();
23980b57cec5SDimitry Andric   unsigned ValWidth = ValTy.getSizeInBits();
23990b57cec5SDimitry Andric   assert(VecWidth == 32 || VecWidth == 64);
24000b57cec5SDimitry Andric   assert((VecWidth % ValWidth) == 0);
24010b57cec5SDimitry Andric 
24020b57cec5SDimitry Andric   // Cast everything to scalar integer types.
24030b57cec5SDimitry Andric   MVT ScalarTy = MVT::getIntegerVT(VecWidth);
24040b57cec5SDimitry Andric   // The actual type of ValV may be different than ValTy (which is related
24050b57cec5SDimitry Andric   // to the vector type).
24060b57cec5SDimitry Andric   unsigned VW = ty(ValV).getSizeInBits();
24070b57cec5SDimitry Andric   ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
24080b57cec5SDimitry Andric   VecV = DAG.getBitcast(ScalarTy, VecV);
24090b57cec5SDimitry Andric   if (VW != VecWidth)
24100b57cec5SDimitry Andric     ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
24110b57cec5SDimitry Andric 
24120b57cec5SDimitry Andric   SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
24130b57cec5SDimitry Andric   SDValue InsV;
24140b57cec5SDimitry Andric 
24150b57cec5SDimitry Andric   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
24160b57cec5SDimitry Andric     unsigned W = C->getZExtValue() * ValWidth;
24170b57cec5SDimitry Andric     SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
24180b57cec5SDimitry Andric     InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
24190b57cec5SDimitry Andric                        {VecV, ValV, WidthV, OffV});
24200b57cec5SDimitry Andric   } else {
24210b57cec5SDimitry Andric     if (ty(IdxV) != MVT::i32)
24220b57cec5SDimitry Andric       IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
24230b57cec5SDimitry Andric     SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
24240b57cec5SDimitry Andric     InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
24250b57cec5SDimitry Andric                        {VecV, ValV, WidthV, OffV});
24260b57cec5SDimitry Andric   }
24270b57cec5SDimitry Andric 
24280b57cec5SDimitry Andric   return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
24290b57cec5SDimitry Andric }
24300b57cec5SDimitry Andric 
24310b57cec5SDimitry Andric SDValue
24320b57cec5SDimitry Andric HexagonTargetLowering::expandPredicate(SDValue Vec32, const SDLoc &dl,
24330b57cec5SDimitry Andric                                        SelectionDAG &DAG) const {
24340b57cec5SDimitry Andric   assert(ty(Vec32).getSizeInBits() == 32);
24350b57cec5SDimitry Andric   if (isUndef(Vec32))
24360b57cec5SDimitry Andric     return DAG.getUNDEF(MVT::i64);
24370b57cec5SDimitry Andric   return getInstr(Hexagon::S2_vsxtbh, dl, MVT::i64, {Vec32}, DAG);
24380b57cec5SDimitry Andric }
24390b57cec5SDimitry Andric 
24400b57cec5SDimitry Andric SDValue
24410b57cec5SDimitry Andric HexagonTargetLowering::contractPredicate(SDValue Vec64, const SDLoc &dl,
24420b57cec5SDimitry Andric                                          SelectionDAG &DAG) const {
24430b57cec5SDimitry Andric   assert(ty(Vec64).getSizeInBits() == 64);
24440b57cec5SDimitry Andric   if (isUndef(Vec64))
24450b57cec5SDimitry Andric     return DAG.getUNDEF(MVT::i32);
24460b57cec5SDimitry Andric   return getInstr(Hexagon::S2_vtrunehb, dl, MVT::i32, {Vec64}, DAG);
24470b57cec5SDimitry Andric }
24480b57cec5SDimitry Andric 
24490b57cec5SDimitry Andric SDValue
24500b57cec5SDimitry Andric HexagonTargetLowering::getZero(const SDLoc &dl, MVT Ty, SelectionDAG &DAG)
24510b57cec5SDimitry Andric       const {
24520b57cec5SDimitry Andric   if (Ty.isVector()) {
24530b57cec5SDimitry Andric     assert(Ty.isInteger() && "Only integer vectors are supported here");
24540b57cec5SDimitry Andric     unsigned W = Ty.getSizeInBits();
24550b57cec5SDimitry Andric     if (W <= 64)
24560b57cec5SDimitry Andric       return DAG.getBitcast(Ty, DAG.getConstant(0, dl, MVT::getIntegerVT(W)));
24570b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::VZERO, dl, Ty);
24580b57cec5SDimitry Andric   }
24590b57cec5SDimitry Andric 
24600b57cec5SDimitry Andric   if (Ty.isInteger())
24610b57cec5SDimitry Andric     return DAG.getConstant(0, dl, Ty);
24620b57cec5SDimitry Andric   if (Ty.isFloatingPoint())
24630b57cec5SDimitry Andric     return DAG.getConstantFP(0.0, dl, Ty);
24640b57cec5SDimitry Andric   llvm_unreachable("Invalid type for zero");
24650b57cec5SDimitry Andric }
24660b57cec5SDimitry Andric 
24670b57cec5SDimitry Andric SDValue
24680b57cec5SDimitry Andric HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
24690b57cec5SDimitry Andric   MVT VecTy = ty(Op);
24700b57cec5SDimitry Andric   unsigned BW = VecTy.getSizeInBits();
24710b57cec5SDimitry Andric   const SDLoc &dl(Op);
24720b57cec5SDimitry Andric   SmallVector<SDValue,8> Ops;
24730b57cec5SDimitry Andric   for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
24740b57cec5SDimitry Andric     Ops.push_back(Op.getOperand(i));
24750b57cec5SDimitry Andric 
24760b57cec5SDimitry Andric   if (BW == 32)
24770b57cec5SDimitry Andric     return buildVector32(Ops, dl, VecTy, DAG);
24780b57cec5SDimitry Andric   if (BW == 64)
24790b57cec5SDimitry Andric     return buildVector64(Ops, dl, VecTy, DAG);
24800b57cec5SDimitry Andric 
24810b57cec5SDimitry Andric   if (VecTy == MVT::v8i1 || VecTy == MVT::v4i1 || VecTy == MVT::v2i1) {
2482*8bcb0991SDimitry Andric     // Check if this is a special case or all-0 or all-1.
2483*8bcb0991SDimitry Andric     bool All0 = true, All1 = true;
2484*8bcb0991SDimitry Andric     for (SDValue P : Ops) {
2485*8bcb0991SDimitry Andric       auto *CN = dyn_cast<ConstantSDNode>(P.getNode());
2486*8bcb0991SDimitry Andric       if (CN == nullptr) {
2487*8bcb0991SDimitry Andric         All0 = All1 = false;
2488*8bcb0991SDimitry Andric         break;
2489*8bcb0991SDimitry Andric       }
2490*8bcb0991SDimitry Andric       uint32_t C = CN->getZExtValue();
2491*8bcb0991SDimitry Andric       All0 &= (C == 0);
2492*8bcb0991SDimitry Andric       All1 &= (C == 1);
2493*8bcb0991SDimitry Andric     }
2494*8bcb0991SDimitry Andric     if (All0)
2495*8bcb0991SDimitry Andric       return DAG.getNode(HexagonISD::PFALSE, dl, VecTy);
2496*8bcb0991SDimitry Andric     if (All1)
2497*8bcb0991SDimitry Andric       return DAG.getNode(HexagonISD::PTRUE, dl, VecTy);
2498*8bcb0991SDimitry Andric 
24990b57cec5SDimitry Andric     // For each i1 element in the resulting predicate register, put 1
25000b57cec5SDimitry Andric     // shifted by the index of the element into a general-purpose register,
25010b57cec5SDimitry Andric     // then or them together and transfer it back into a predicate register.
25020b57cec5SDimitry Andric     SDValue Rs[8];
25030b57cec5SDimitry Andric     SDValue Z = getZero(dl, MVT::i32, DAG);
25040b57cec5SDimitry Andric     // Always produce 8 bits, repeat inputs if necessary.
25050b57cec5SDimitry Andric     unsigned Rep = 8 / VecTy.getVectorNumElements();
25060b57cec5SDimitry Andric     for (unsigned i = 0; i != 8; ++i) {
25070b57cec5SDimitry Andric       SDValue S = DAG.getConstant(1ull << i, dl, MVT::i32);
25080b57cec5SDimitry Andric       Rs[i] = DAG.getSelect(dl, MVT::i32, Ops[i/Rep], S, Z);
25090b57cec5SDimitry Andric     }
25100b57cec5SDimitry Andric     for (ArrayRef<SDValue> A(Rs); A.size() != 1; A = A.drop_back(A.size()/2)) {
25110b57cec5SDimitry Andric       for (unsigned i = 0, e = A.size()/2; i != e; ++i)
25120b57cec5SDimitry Andric         Rs[i] = DAG.getNode(ISD::OR, dl, MVT::i32, Rs[2*i], Rs[2*i+1]);
25130b57cec5SDimitry Andric     }
25140b57cec5SDimitry Andric     // Move the value directly to a predicate register.
25150b57cec5SDimitry Andric     return getInstr(Hexagon::C2_tfrrp, dl, VecTy, {Rs[0]}, DAG);
25160b57cec5SDimitry Andric   }
25170b57cec5SDimitry Andric 
25180b57cec5SDimitry Andric   return SDValue();
25190b57cec5SDimitry Andric }
25200b57cec5SDimitry Andric 
25210b57cec5SDimitry Andric SDValue
25220b57cec5SDimitry Andric HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
25230b57cec5SDimitry Andric                                            SelectionDAG &DAG) const {
25240b57cec5SDimitry Andric   MVT VecTy = ty(Op);
25250b57cec5SDimitry Andric   const SDLoc &dl(Op);
25260b57cec5SDimitry Andric   if (VecTy.getSizeInBits() == 64) {
25270b57cec5SDimitry Andric     assert(Op.getNumOperands() == 2);
25280b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, Op.getOperand(1),
25290b57cec5SDimitry Andric                        Op.getOperand(0));
25300b57cec5SDimitry Andric   }
25310b57cec5SDimitry Andric 
25320b57cec5SDimitry Andric   MVT ElemTy = VecTy.getVectorElementType();
25330b57cec5SDimitry Andric   if (ElemTy == MVT::i1) {
25340b57cec5SDimitry Andric     assert(VecTy == MVT::v2i1 || VecTy == MVT::v4i1 || VecTy == MVT::v8i1);
25350b57cec5SDimitry Andric     MVT OpTy = ty(Op.getOperand(0));
25360b57cec5SDimitry Andric     // Scale is how many times the operands need to be contracted to match
25370b57cec5SDimitry Andric     // the representation in the target register.
25380b57cec5SDimitry Andric     unsigned Scale = VecTy.getVectorNumElements() / OpTy.getVectorNumElements();
25390b57cec5SDimitry Andric     assert(Scale == Op.getNumOperands() && Scale > 1);
25400b57cec5SDimitry Andric 
25410b57cec5SDimitry Andric     // First, convert all bool vectors to integers, then generate pairwise
25420b57cec5SDimitry Andric     // inserts to form values of doubled length. Up until there are only
25430b57cec5SDimitry Andric     // two values left to concatenate, all of these values will fit in a
25440b57cec5SDimitry Andric     // 32-bit integer, so keep them as i32 to use 32-bit inserts.
25450b57cec5SDimitry Andric     SmallVector<SDValue,4> Words[2];
25460b57cec5SDimitry Andric     unsigned IdxW = 0;
25470b57cec5SDimitry Andric 
25480b57cec5SDimitry Andric     for (SDValue P : Op.getNode()->op_values()) {
25490b57cec5SDimitry Andric       SDValue W = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, P);
25500b57cec5SDimitry Andric       for (unsigned R = Scale; R > 1; R /= 2) {
25510b57cec5SDimitry Andric         W = contractPredicate(W, dl, DAG);
25520b57cec5SDimitry Andric         W = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
25530b57cec5SDimitry Andric                         DAG.getUNDEF(MVT::i32), W);
25540b57cec5SDimitry Andric       }
25550b57cec5SDimitry Andric       W = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, W);
25560b57cec5SDimitry Andric       Words[IdxW].push_back(W);
25570b57cec5SDimitry Andric     }
25580b57cec5SDimitry Andric 
25590b57cec5SDimitry Andric     while (Scale > 2) {
25600b57cec5SDimitry Andric       SDValue WidthV = DAG.getConstant(64 / Scale, dl, MVT::i32);
25610b57cec5SDimitry Andric       Words[IdxW ^ 1].clear();
25620b57cec5SDimitry Andric 
25630b57cec5SDimitry Andric       for (unsigned i = 0, e = Words[IdxW].size(); i != e; i += 2) {
25640b57cec5SDimitry Andric         SDValue W0 = Words[IdxW][i], W1 = Words[IdxW][i+1];
25650b57cec5SDimitry Andric         // Insert W1 into W0 right next to the significant bits of W0.
25660b57cec5SDimitry Andric         SDValue T = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
25670b57cec5SDimitry Andric                                 {W0, W1, WidthV, WidthV});
25680b57cec5SDimitry Andric         Words[IdxW ^ 1].push_back(T);
25690b57cec5SDimitry Andric       }
25700b57cec5SDimitry Andric       IdxW ^= 1;
25710b57cec5SDimitry Andric       Scale /= 2;
25720b57cec5SDimitry Andric     }
25730b57cec5SDimitry Andric 
25740b57cec5SDimitry Andric     // Another sanity check. At this point there should only be two words
25750b57cec5SDimitry Andric     // left, and Scale should be 2.
25760b57cec5SDimitry Andric     assert(Scale == 2 && Words[IdxW].size() == 2);
25770b57cec5SDimitry Andric 
25780b57cec5SDimitry Andric     SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
25790b57cec5SDimitry Andric                              Words[IdxW][1], Words[IdxW][0]);
25800b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::D2P, dl, VecTy, WW);
25810b57cec5SDimitry Andric   }
25820b57cec5SDimitry Andric 
25830b57cec5SDimitry Andric   return SDValue();
25840b57cec5SDimitry Andric }
25850b57cec5SDimitry Andric 
25860b57cec5SDimitry Andric SDValue
25870b57cec5SDimitry Andric HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
25880b57cec5SDimitry Andric                                                SelectionDAG &DAG) const {
25890b57cec5SDimitry Andric   SDValue Vec = Op.getOperand(0);
25900b57cec5SDimitry Andric   MVT ElemTy = ty(Vec).getVectorElementType();
25910b57cec5SDimitry Andric   return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
25920b57cec5SDimitry Andric }
25930b57cec5SDimitry Andric 
25940b57cec5SDimitry Andric SDValue
25950b57cec5SDimitry Andric HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
25960b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
25970b57cec5SDimitry Andric   return extractVector(Op.getOperand(0), Op.getOperand(1), SDLoc(Op),
25980b57cec5SDimitry Andric                        ty(Op), ty(Op), DAG);
25990b57cec5SDimitry Andric }
26000b57cec5SDimitry Andric 
26010b57cec5SDimitry Andric SDValue
26020b57cec5SDimitry Andric HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
26030b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
26040b57cec5SDimitry Andric   return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
26050b57cec5SDimitry Andric                       SDLoc(Op), ty(Op).getVectorElementType(), DAG);
26060b57cec5SDimitry Andric }
26070b57cec5SDimitry Andric 
26080b57cec5SDimitry Andric SDValue
26090b57cec5SDimitry Andric HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
26100b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
26110b57cec5SDimitry Andric   SDValue ValV = Op.getOperand(1);
26120b57cec5SDimitry Andric   return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
26130b57cec5SDimitry Andric                       SDLoc(Op), ty(ValV), DAG);
26140b57cec5SDimitry Andric }
26150b57cec5SDimitry Andric 
26160b57cec5SDimitry Andric bool
26170b57cec5SDimitry Andric HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
26180b57cec5SDimitry Andric   // Assuming the caller does not have either a signext or zeroext modifier, and
26190b57cec5SDimitry Andric   // only one value is accepted, any reasonable truncation is allowed.
26200b57cec5SDimitry Andric   if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
26210b57cec5SDimitry Andric     return false;
26220b57cec5SDimitry Andric 
26230b57cec5SDimitry Andric   // FIXME: in principle up to 64-bit could be made safe, but it would be very
26240b57cec5SDimitry Andric   // fragile at the moment: any support for multiple value returns would be
26250b57cec5SDimitry Andric   // liable to disallow tail calls involving i64 -> iN truncation in many cases.
26260b57cec5SDimitry Andric   return Ty1->getPrimitiveSizeInBits() <= 32;
26270b57cec5SDimitry Andric }
26280b57cec5SDimitry Andric 
26290b57cec5SDimitry Andric SDValue
26300b57cec5SDimitry Andric HexagonTargetLowering::LowerLoad(SDValue Op, SelectionDAG &DAG) const {
26310b57cec5SDimitry Andric   LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
26320b57cec5SDimitry Andric   unsigned ClaimAlign = LN->getAlignment();
26330b57cec5SDimitry Andric   validateConstPtrAlignment(LN->getBasePtr(), SDLoc(Op), ClaimAlign);
26340b57cec5SDimitry Andric   // Call LowerUnalignedLoad for all loads, it recognizes loads that
26350b57cec5SDimitry Andric   // don't need extra aligning.
26360b57cec5SDimitry Andric   return LowerUnalignedLoad(Op, DAG);
26370b57cec5SDimitry Andric }
26380b57cec5SDimitry Andric 
26390b57cec5SDimitry Andric SDValue
26400b57cec5SDimitry Andric HexagonTargetLowering::LowerStore(SDValue Op, SelectionDAG &DAG) const {
26410b57cec5SDimitry Andric   StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
26420b57cec5SDimitry Andric   unsigned ClaimAlign = SN->getAlignment();
26430b57cec5SDimitry Andric   SDValue Ptr = SN->getBasePtr();
26440b57cec5SDimitry Andric   const SDLoc &dl(Op);
26450b57cec5SDimitry Andric   validateConstPtrAlignment(Ptr, dl, ClaimAlign);
26460b57cec5SDimitry Andric 
26470b57cec5SDimitry Andric   MVT StoreTy = SN->getMemoryVT().getSimpleVT();
26480b57cec5SDimitry Andric   unsigned NeedAlign = Subtarget.getTypeAlignment(StoreTy);
26490b57cec5SDimitry Andric   if (ClaimAlign < NeedAlign)
26500b57cec5SDimitry Andric     return expandUnalignedStore(SN, DAG);
26510b57cec5SDimitry Andric   return Op;
26520b57cec5SDimitry Andric }
26530b57cec5SDimitry Andric 
26540b57cec5SDimitry Andric SDValue
26550b57cec5SDimitry Andric HexagonTargetLowering::LowerUnalignedLoad(SDValue Op, SelectionDAG &DAG)
26560b57cec5SDimitry Andric       const {
26570b57cec5SDimitry Andric   LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
26580b57cec5SDimitry Andric   MVT LoadTy = ty(Op);
26590b57cec5SDimitry Andric   unsigned NeedAlign = Subtarget.getTypeAlignment(LoadTy);
26600b57cec5SDimitry Andric   unsigned HaveAlign = LN->getAlignment();
26610b57cec5SDimitry Andric   if (HaveAlign >= NeedAlign)
26620b57cec5SDimitry Andric     return Op;
26630b57cec5SDimitry Andric 
26640b57cec5SDimitry Andric   const SDLoc &dl(Op);
26650b57cec5SDimitry Andric   const DataLayout &DL = DAG.getDataLayout();
26660b57cec5SDimitry Andric   LLVMContext &Ctx = *DAG.getContext();
26670b57cec5SDimitry Andric 
26680b57cec5SDimitry Andric   // If the load aligning is disabled or the load can be broken up into two
26690b57cec5SDimitry Andric   // smaller legal loads, do the default (target-independent) expansion.
26700b57cec5SDimitry Andric   bool DoDefault = false;
26710b57cec5SDimitry Andric   // Handle it in the default way if this is an indexed load.
26720b57cec5SDimitry Andric   if (!LN->isUnindexed())
26730b57cec5SDimitry Andric     DoDefault = true;
26740b57cec5SDimitry Andric 
26750b57cec5SDimitry Andric   if (!AlignLoads) {
2676*8bcb0991SDimitry Andric     if (allowsMemoryAccessForAlignment(Ctx, DL, LN->getMemoryVT(),
2677*8bcb0991SDimitry Andric                                        *LN->getMemOperand()))
26780b57cec5SDimitry Andric       return Op;
26790b57cec5SDimitry Andric     DoDefault = true;
26800b57cec5SDimitry Andric   }
26810b57cec5SDimitry Andric   if (!DoDefault && (2 * HaveAlign) == NeedAlign) {
26820b57cec5SDimitry Andric     // The PartTy is the equivalent of "getLoadableTypeOfSize(HaveAlign)".
26830b57cec5SDimitry Andric     MVT PartTy = HaveAlign <= 8 ? MVT::getIntegerVT(8 * HaveAlign)
26840b57cec5SDimitry Andric                                 : MVT::getVectorVT(MVT::i8, HaveAlign);
2685*8bcb0991SDimitry Andric     DoDefault =
2686*8bcb0991SDimitry Andric         allowsMemoryAccessForAlignment(Ctx, DL, PartTy, *LN->getMemOperand());
26870b57cec5SDimitry Andric   }
26880b57cec5SDimitry Andric   if (DoDefault) {
26890b57cec5SDimitry Andric     std::pair<SDValue, SDValue> P = expandUnalignedLoad(LN, DAG);
26900b57cec5SDimitry Andric     return DAG.getMergeValues({P.first, P.second}, dl);
26910b57cec5SDimitry Andric   }
26920b57cec5SDimitry Andric 
26930b57cec5SDimitry Andric   // The code below generates two loads, both aligned as NeedAlign, and
26940b57cec5SDimitry Andric   // with the distance of NeedAlign between them. For that to cover the
26950b57cec5SDimitry Andric   // bits that need to be loaded (and without overlapping), the size of
26960b57cec5SDimitry Andric   // the loads should be equal to NeedAlign. This is true for all loadable
26970b57cec5SDimitry Andric   // types, but add an assertion in case something changes in the future.
26980b57cec5SDimitry Andric   assert(LoadTy.getSizeInBits() == 8*NeedAlign);
26990b57cec5SDimitry Andric 
27000b57cec5SDimitry Andric   unsigned LoadLen = NeedAlign;
27010b57cec5SDimitry Andric   SDValue Base = LN->getBasePtr();
27020b57cec5SDimitry Andric   SDValue Chain = LN->getChain();
27030b57cec5SDimitry Andric   auto BO = getBaseAndOffset(Base);
27040b57cec5SDimitry Andric   unsigned BaseOpc = BO.first.getOpcode();
27050b57cec5SDimitry Andric   if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % LoadLen == 0)
27060b57cec5SDimitry Andric     return Op;
27070b57cec5SDimitry Andric 
27080b57cec5SDimitry Andric   if (BO.second % LoadLen != 0) {
27090b57cec5SDimitry Andric     BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
27100b57cec5SDimitry Andric                            DAG.getConstant(BO.second % LoadLen, dl, MVT::i32));
27110b57cec5SDimitry Andric     BO.second -= BO.second % LoadLen;
27120b57cec5SDimitry Andric   }
27130b57cec5SDimitry Andric   SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
27140b57cec5SDimitry Andric       ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first,
27150b57cec5SDimitry Andric                     DAG.getConstant(NeedAlign, dl, MVT::i32))
27160b57cec5SDimitry Andric       : BO.first;
27170b57cec5SDimitry Andric   SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
27180b57cec5SDimitry Andric   SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+LoadLen, dl);
27190b57cec5SDimitry Andric 
27200b57cec5SDimitry Andric   MachineMemOperand *WideMMO = nullptr;
27210b57cec5SDimitry Andric   if (MachineMemOperand *MMO = LN->getMemOperand()) {
27220b57cec5SDimitry Andric     MachineFunction &MF = DAG.getMachineFunction();
27230b57cec5SDimitry Andric     WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
27240b57cec5SDimitry Andric                     2*LoadLen, LoadLen, MMO->getAAInfo(), MMO->getRanges(),
27250b57cec5SDimitry Andric                     MMO->getSyncScopeID(), MMO->getOrdering(),
27260b57cec5SDimitry Andric                     MMO->getFailureOrdering());
27270b57cec5SDimitry Andric   }
27280b57cec5SDimitry Andric 
27290b57cec5SDimitry Andric   SDValue Load0 = DAG.getLoad(LoadTy, dl, Chain, Base0, WideMMO);
27300b57cec5SDimitry Andric   SDValue Load1 = DAG.getLoad(LoadTy, dl, Chain, Base1, WideMMO);
27310b57cec5SDimitry Andric 
27320b57cec5SDimitry Andric   SDValue Aligned = DAG.getNode(HexagonISD::VALIGN, dl, LoadTy,
27330b57cec5SDimitry Andric                                 {Load1, Load0, BaseNoOff.getOperand(0)});
27340b57cec5SDimitry Andric   SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
27350b57cec5SDimitry Andric                                  Load0.getValue(1), Load1.getValue(1));
27360b57cec5SDimitry Andric   SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
27370b57cec5SDimitry Andric   return M;
27380b57cec5SDimitry Andric }
27390b57cec5SDimitry Andric 
27400b57cec5SDimitry Andric SDValue
27410b57cec5SDimitry Andric HexagonTargetLowering::LowerUAddSubO(SDValue Op, SelectionDAG &DAG) const {
27420b57cec5SDimitry Andric   SDValue X = Op.getOperand(0), Y = Op.getOperand(1);
27430b57cec5SDimitry Andric   auto *CY = dyn_cast<ConstantSDNode>(Y);
27440b57cec5SDimitry Andric   if (!CY)
27450b57cec5SDimitry Andric     return SDValue();
27460b57cec5SDimitry Andric 
27470b57cec5SDimitry Andric   const SDLoc &dl(Op);
27480b57cec5SDimitry Andric   SDVTList VTs = Op.getNode()->getVTList();
27490b57cec5SDimitry Andric   assert(VTs.NumVTs == 2);
27500b57cec5SDimitry Andric   assert(VTs.VTs[1] == MVT::i1);
27510b57cec5SDimitry Andric   unsigned Opc = Op.getOpcode();
27520b57cec5SDimitry Andric 
27530b57cec5SDimitry Andric   if (CY) {
27540b57cec5SDimitry Andric     uint32_t VY = CY->getZExtValue();
27550b57cec5SDimitry Andric     assert(VY != 0 && "This should have been folded");
27560b57cec5SDimitry Andric     // X +/- 1
27570b57cec5SDimitry Andric     if (VY != 1)
27580b57cec5SDimitry Andric       return SDValue();
27590b57cec5SDimitry Andric 
27600b57cec5SDimitry Andric     if (Opc == ISD::UADDO) {
27610b57cec5SDimitry Andric       SDValue Op = DAG.getNode(ISD::ADD, dl, VTs.VTs[0], {X, Y});
27620b57cec5SDimitry Andric       SDValue Ov = DAG.getSetCC(dl, MVT::i1, Op, getZero(dl, ty(Op), DAG),
27630b57cec5SDimitry Andric                                 ISD::SETEQ);
27640b57cec5SDimitry Andric       return DAG.getMergeValues({Op, Ov}, dl);
27650b57cec5SDimitry Andric     }
27660b57cec5SDimitry Andric     if (Opc == ISD::USUBO) {
27670b57cec5SDimitry Andric       SDValue Op = DAG.getNode(ISD::SUB, dl, VTs.VTs[0], {X, Y});
27680b57cec5SDimitry Andric       SDValue Ov = DAG.getSetCC(dl, MVT::i1, Op,
27690b57cec5SDimitry Andric                                 DAG.getConstant(-1, dl, ty(Op)), ISD::SETEQ);
27700b57cec5SDimitry Andric       return DAG.getMergeValues({Op, Ov}, dl);
27710b57cec5SDimitry Andric     }
27720b57cec5SDimitry Andric   }
27730b57cec5SDimitry Andric 
27740b57cec5SDimitry Andric   return SDValue();
27750b57cec5SDimitry Andric }
27760b57cec5SDimitry Andric 
27770b57cec5SDimitry Andric SDValue
27780b57cec5SDimitry Andric HexagonTargetLowering::LowerAddSubCarry(SDValue Op, SelectionDAG &DAG) const {
27790b57cec5SDimitry Andric   const SDLoc &dl(Op);
27800b57cec5SDimitry Andric   unsigned Opc = Op.getOpcode();
27810b57cec5SDimitry Andric   SDValue X = Op.getOperand(0), Y = Op.getOperand(1), C = Op.getOperand(2);
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric   if (Opc == ISD::ADDCARRY)
27840b57cec5SDimitry Andric     return DAG.getNode(HexagonISD::ADDC, dl, Op.getNode()->getVTList(),
27850b57cec5SDimitry Andric                        { X, Y, C });
27860b57cec5SDimitry Andric 
27870b57cec5SDimitry Andric   EVT CarryTy = C.getValueType();
27880b57cec5SDimitry Andric   SDValue SubC = DAG.getNode(HexagonISD::SUBC, dl, Op.getNode()->getVTList(),
27890b57cec5SDimitry Andric                              { X, Y, DAG.getLogicalNOT(dl, C, CarryTy) });
27900b57cec5SDimitry Andric   SDValue Out[] = { SubC.getValue(0),
27910b57cec5SDimitry Andric                     DAG.getLogicalNOT(dl, SubC.getValue(1), CarryTy) };
27920b57cec5SDimitry Andric   return DAG.getMergeValues(Out, dl);
27930b57cec5SDimitry Andric }
27940b57cec5SDimitry Andric 
27950b57cec5SDimitry Andric SDValue
27960b57cec5SDimitry Andric HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
27970b57cec5SDimitry Andric   SDValue Chain     = Op.getOperand(0);
27980b57cec5SDimitry Andric   SDValue Offset    = Op.getOperand(1);
27990b57cec5SDimitry Andric   SDValue Handler   = Op.getOperand(2);
28000b57cec5SDimitry Andric   SDLoc dl(Op);
28010b57cec5SDimitry Andric   auto PtrVT = getPointerTy(DAG.getDataLayout());
28020b57cec5SDimitry Andric 
28030b57cec5SDimitry Andric   // Mark function as containing a call to EH_RETURN.
28040b57cec5SDimitry Andric   HexagonMachineFunctionInfo *FuncInfo =
28050b57cec5SDimitry Andric     DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
28060b57cec5SDimitry Andric   FuncInfo->setHasEHReturn();
28070b57cec5SDimitry Andric 
28080b57cec5SDimitry Andric   unsigned OffsetReg = Hexagon::R28;
28090b57cec5SDimitry Andric 
28100b57cec5SDimitry Andric   SDValue StoreAddr =
28110b57cec5SDimitry Andric       DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
28120b57cec5SDimitry Andric                   DAG.getIntPtrConstant(4, dl));
28130b57cec5SDimitry Andric   Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
28140b57cec5SDimitry Andric   Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
28150b57cec5SDimitry Andric 
28160b57cec5SDimitry Andric   // Not needed we already use it as explict input to EH_RETURN.
28170b57cec5SDimitry Andric   // MF.getRegInfo().addLiveOut(OffsetReg);
28180b57cec5SDimitry Andric 
28190b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
28200b57cec5SDimitry Andric }
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric SDValue
28230b57cec5SDimitry Andric HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
28240b57cec5SDimitry Andric   unsigned Opc = Op.getOpcode();
28250b57cec5SDimitry Andric 
28260b57cec5SDimitry Andric   // Handle INLINEASM first.
28270b57cec5SDimitry Andric   if (Opc == ISD::INLINEASM || Opc == ISD::INLINEASM_BR)
28280b57cec5SDimitry Andric     return LowerINLINEASM(Op, DAG);
28290b57cec5SDimitry Andric 
28300b57cec5SDimitry Andric   if (isHvxOperation(Op)) {
28310b57cec5SDimitry Andric     // If HVX lowering returns nothing, try the default lowering.
28320b57cec5SDimitry Andric     if (SDValue V = LowerHvxOperation(Op, DAG))
28330b57cec5SDimitry Andric       return V;
28340b57cec5SDimitry Andric   }
28350b57cec5SDimitry Andric 
28360b57cec5SDimitry Andric   switch (Opc) {
28370b57cec5SDimitry Andric     default:
28380b57cec5SDimitry Andric #ifndef NDEBUG
28390b57cec5SDimitry Andric       Op.getNode()->dumpr(&DAG);
28400b57cec5SDimitry Andric       if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
28410b57cec5SDimitry Andric         errs() << "Error: check for a non-legal type in this operation\n";
28420b57cec5SDimitry Andric #endif
28430b57cec5SDimitry Andric       llvm_unreachable("Should not custom lower this!");
28440b57cec5SDimitry Andric     case ISD::CONCAT_VECTORS:       return LowerCONCAT_VECTORS(Op, DAG);
28450b57cec5SDimitry Andric     case ISD::INSERT_SUBVECTOR:     return LowerINSERT_SUBVECTOR(Op, DAG);
28460b57cec5SDimitry Andric     case ISD::INSERT_VECTOR_ELT:    return LowerINSERT_VECTOR_ELT(Op, DAG);
28470b57cec5SDimitry Andric     case ISD::EXTRACT_SUBVECTOR:    return LowerEXTRACT_SUBVECTOR(Op, DAG);
28480b57cec5SDimitry Andric     case ISD::EXTRACT_VECTOR_ELT:   return LowerEXTRACT_VECTOR_ELT(Op, DAG);
28490b57cec5SDimitry Andric     case ISD::BUILD_VECTOR:         return LowerBUILD_VECTOR(Op, DAG);
28500b57cec5SDimitry Andric     case ISD::VECTOR_SHUFFLE:       return LowerVECTOR_SHUFFLE(Op, DAG);
28510b57cec5SDimitry Andric     case ISD::BITCAST:              return LowerBITCAST(Op, DAG);
28520b57cec5SDimitry Andric     case ISD::LOAD:                 return LowerLoad(Op, DAG);
28530b57cec5SDimitry Andric     case ISD::STORE:                return LowerStore(Op, DAG);
28540b57cec5SDimitry Andric     case ISD::UADDO:
28550b57cec5SDimitry Andric     case ISD::USUBO:                return LowerUAddSubO(Op, DAG);
28560b57cec5SDimitry Andric     case ISD::ADDCARRY:
28570b57cec5SDimitry Andric     case ISD::SUBCARRY:             return LowerAddSubCarry(Op, DAG);
28580b57cec5SDimitry Andric     case ISD::SRA:
28590b57cec5SDimitry Andric     case ISD::SHL:
28600b57cec5SDimitry Andric     case ISD::SRL:                  return LowerVECTOR_SHIFT(Op, DAG);
28610b57cec5SDimitry Andric     case ISD::ROTL:                 return LowerROTL(Op, DAG);
28620b57cec5SDimitry Andric     case ISD::ConstantPool:         return LowerConstantPool(Op, DAG);
28630b57cec5SDimitry Andric     case ISD::JumpTable:            return LowerJumpTable(Op, DAG);
28640b57cec5SDimitry Andric     case ISD::EH_RETURN:            return LowerEH_RETURN(Op, DAG);
28650b57cec5SDimitry Andric     case ISD::RETURNADDR:           return LowerRETURNADDR(Op, DAG);
28660b57cec5SDimitry Andric     case ISD::FRAMEADDR:            return LowerFRAMEADDR(Op, DAG);
28670b57cec5SDimitry Andric     case ISD::GlobalTLSAddress:     return LowerGlobalTLSAddress(Op, DAG);
28680b57cec5SDimitry Andric     case ISD::ATOMIC_FENCE:         return LowerATOMIC_FENCE(Op, DAG);
28690b57cec5SDimitry Andric     case ISD::GlobalAddress:        return LowerGLOBALADDRESS(Op, DAG);
28700b57cec5SDimitry Andric     case ISD::BlockAddress:         return LowerBlockAddress(Op, DAG);
28710b57cec5SDimitry Andric     case ISD::GLOBAL_OFFSET_TABLE:  return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
28720b57cec5SDimitry Andric     case ISD::VASTART:              return LowerVASTART(Op, DAG);
28730b57cec5SDimitry Andric     case ISD::DYNAMIC_STACKALLOC:   return LowerDYNAMIC_STACKALLOC(Op, DAG);
28740b57cec5SDimitry Andric     case ISD::SETCC:                return LowerSETCC(Op, DAG);
28750b57cec5SDimitry Andric     case ISD::VSELECT:              return LowerVSELECT(Op, DAG);
28760b57cec5SDimitry Andric     case ISD::INTRINSIC_WO_CHAIN:   return LowerINTRINSIC_WO_CHAIN(Op, DAG);
28770b57cec5SDimitry Andric     case ISD::INTRINSIC_VOID:       return LowerINTRINSIC_VOID(Op, DAG);
28780b57cec5SDimitry Andric     case ISD::PREFETCH:             return LowerPREFETCH(Op, DAG);
28790b57cec5SDimitry Andric     case ISD::READCYCLECOUNTER:     return LowerREADCYCLECOUNTER(Op, DAG);
28800b57cec5SDimitry Andric       break;
28810b57cec5SDimitry Andric   }
28820b57cec5SDimitry Andric 
28830b57cec5SDimitry Andric   return SDValue();
28840b57cec5SDimitry Andric }
28850b57cec5SDimitry Andric 
28860b57cec5SDimitry Andric void
28870b57cec5SDimitry Andric HexagonTargetLowering::LowerOperationWrapper(SDNode *N,
28880b57cec5SDimitry Andric                                              SmallVectorImpl<SDValue> &Results,
28890b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
28900b57cec5SDimitry Andric   // We are only custom-lowering stores to verify the alignment of the
28910b57cec5SDimitry Andric   // address if it is a compile-time constant. Since a store can be modified
28920b57cec5SDimitry Andric   // during type-legalization (the value being stored may need legalization),
28930b57cec5SDimitry Andric   // return empty Results here to indicate that we don't really make any
28940b57cec5SDimitry Andric   // changes in the custom lowering.
28950b57cec5SDimitry Andric   if (N->getOpcode() != ISD::STORE)
28960b57cec5SDimitry Andric     return TargetLowering::LowerOperationWrapper(N, Results, DAG);
28970b57cec5SDimitry Andric }
28980b57cec5SDimitry Andric 
28990b57cec5SDimitry Andric void
29000b57cec5SDimitry Andric HexagonTargetLowering::ReplaceNodeResults(SDNode *N,
29010b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results,
29020b57cec5SDimitry Andric                                           SelectionDAG &DAG) const {
29030b57cec5SDimitry Andric   const SDLoc &dl(N);
29040b57cec5SDimitry Andric   switch (N->getOpcode()) {
29050b57cec5SDimitry Andric     case ISD::SRL:
29060b57cec5SDimitry Andric     case ISD::SRA:
29070b57cec5SDimitry Andric     case ISD::SHL:
29080b57cec5SDimitry Andric       return;
29090b57cec5SDimitry Andric     case ISD::BITCAST:
29100b57cec5SDimitry Andric       // Handle a bitcast from v8i1 to i8.
29110b57cec5SDimitry Andric       if (N->getValueType(0) == MVT::i8) {
29120b57cec5SDimitry Andric         SDValue P = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32,
29130b57cec5SDimitry Andric                              N->getOperand(0), DAG);
2914*8bcb0991SDimitry Andric         SDValue T = DAG.getAnyExtOrTrunc(P, dl, MVT::i8);
2915*8bcb0991SDimitry Andric         Results.push_back(T);
29160b57cec5SDimitry Andric       }
29170b57cec5SDimitry Andric       break;
29180b57cec5SDimitry Andric   }
29190b57cec5SDimitry Andric }
29200b57cec5SDimitry Andric 
2921*8bcb0991SDimitry Andric SDValue
2922*8bcb0991SDimitry Andric HexagonTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
2923*8bcb0991SDimitry Andric       const {
2924*8bcb0991SDimitry Andric   SDValue Op(N, 0);
2925*8bcb0991SDimitry Andric   if (isHvxOperation(Op)) {
2926*8bcb0991SDimitry Andric     if (SDValue V = PerformHvxDAGCombine(N, DCI))
2927*8bcb0991SDimitry Andric       return V;
2928*8bcb0991SDimitry Andric     return SDValue();
2929*8bcb0991SDimitry Andric   }
2930*8bcb0991SDimitry Andric 
2931*8bcb0991SDimitry Andric   const SDLoc &dl(Op);
2932*8bcb0991SDimitry Andric   unsigned Opc = Op.getOpcode();
2933*8bcb0991SDimitry Andric 
2934*8bcb0991SDimitry Andric   if (Opc == HexagonISD::P2D) {
2935*8bcb0991SDimitry Andric     SDValue P = Op.getOperand(0);
2936*8bcb0991SDimitry Andric     switch (P.getOpcode()) {
2937*8bcb0991SDimitry Andric       case HexagonISD::PTRUE:
2938*8bcb0991SDimitry Andric         return DCI.DAG.getConstant(-1, dl, ty(Op));
2939*8bcb0991SDimitry Andric       case HexagonISD::PFALSE:
2940*8bcb0991SDimitry Andric         return getZero(dl, ty(Op), DCI.DAG);
2941*8bcb0991SDimitry Andric       default:
2942*8bcb0991SDimitry Andric         break;
2943*8bcb0991SDimitry Andric     }
2944*8bcb0991SDimitry Andric   } else if (Opc == ISD::VSELECT) {
2945*8bcb0991SDimitry Andric     // This is pretty much duplicated in HexagonISelLoweringHVX...
2946*8bcb0991SDimitry Andric     //
2947*8bcb0991SDimitry Andric     // (vselect (xor x, ptrue), v0, v1) -> (vselect x, v1, v0)
2948*8bcb0991SDimitry Andric     SDValue Cond = Op.getOperand(0);
2949*8bcb0991SDimitry Andric     if (Cond->getOpcode() == ISD::XOR) {
2950*8bcb0991SDimitry Andric       SDValue C0 = Cond.getOperand(0), C1 = Cond.getOperand(1);
2951*8bcb0991SDimitry Andric       if (C1->getOpcode() == HexagonISD::PTRUE) {
2952*8bcb0991SDimitry Andric         SDValue VSel = DCI.DAG.getNode(ISD::VSELECT, dl, ty(Op), C0,
2953*8bcb0991SDimitry Andric                                        Op.getOperand(2), Op.getOperand(1));
2954*8bcb0991SDimitry Andric         return VSel;
2955*8bcb0991SDimitry Andric       }
2956*8bcb0991SDimitry Andric     }
2957*8bcb0991SDimitry Andric   }
2958*8bcb0991SDimitry Andric 
2959*8bcb0991SDimitry Andric   return SDValue();
2960*8bcb0991SDimitry Andric }
2961*8bcb0991SDimitry Andric 
29620b57cec5SDimitry Andric /// Returns relocation base for the given PIC jumptable.
29630b57cec5SDimitry Andric SDValue
29640b57cec5SDimitry Andric HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
29650b57cec5SDimitry Andric                                                 SelectionDAG &DAG) const {
29660b57cec5SDimitry Andric   int Idx = cast<JumpTableSDNode>(Table)->getIndex();
29670b57cec5SDimitry Andric   EVT VT = Table.getValueType();
29680b57cec5SDimitry Andric   SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
29690b57cec5SDimitry Andric   return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
29700b57cec5SDimitry Andric }
29710b57cec5SDimitry Andric 
29720b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
29730b57cec5SDimitry Andric // Inline Assembly Support
29740b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
29750b57cec5SDimitry Andric 
29760b57cec5SDimitry Andric TargetLowering::ConstraintType
29770b57cec5SDimitry Andric HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
29780b57cec5SDimitry Andric   if (Constraint.size() == 1) {
29790b57cec5SDimitry Andric     switch (Constraint[0]) {
29800b57cec5SDimitry Andric       case 'q':
29810b57cec5SDimitry Andric       case 'v':
29820b57cec5SDimitry Andric         if (Subtarget.useHVXOps())
29830b57cec5SDimitry Andric           return C_RegisterClass;
29840b57cec5SDimitry Andric         break;
29850b57cec5SDimitry Andric       case 'a':
29860b57cec5SDimitry Andric         return C_RegisterClass;
29870b57cec5SDimitry Andric       default:
29880b57cec5SDimitry Andric         break;
29890b57cec5SDimitry Andric     }
29900b57cec5SDimitry Andric   }
29910b57cec5SDimitry Andric   return TargetLowering::getConstraintType(Constraint);
29920b57cec5SDimitry Andric }
29930b57cec5SDimitry Andric 
29940b57cec5SDimitry Andric std::pair<unsigned, const TargetRegisterClass*>
29950b57cec5SDimitry Andric HexagonTargetLowering::getRegForInlineAsmConstraint(
29960b57cec5SDimitry Andric     const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
29970b57cec5SDimitry Andric 
29980b57cec5SDimitry Andric   if (Constraint.size() == 1) {
29990b57cec5SDimitry Andric     switch (Constraint[0]) {
30000b57cec5SDimitry Andric     case 'r':   // R0-R31
30010b57cec5SDimitry Andric       switch (VT.SimpleTy) {
30020b57cec5SDimitry Andric       default:
30030b57cec5SDimitry Andric         return {0u, nullptr};
30040b57cec5SDimitry Andric       case MVT::i1:
30050b57cec5SDimitry Andric       case MVT::i8:
30060b57cec5SDimitry Andric       case MVT::i16:
30070b57cec5SDimitry Andric       case MVT::i32:
30080b57cec5SDimitry Andric       case MVT::f32:
30090b57cec5SDimitry Andric         return {0u, &Hexagon::IntRegsRegClass};
30100b57cec5SDimitry Andric       case MVT::i64:
30110b57cec5SDimitry Andric       case MVT::f64:
30120b57cec5SDimitry Andric         return {0u, &Hexagon::DoubleRegsRegClass};
30130b57cec5SDimitry Andric       }
30140b57cec5SDimitry Andric       break;
30150b57cec5SDimitry Andric     case 'a': // M0-M1
30160b57cec5SDimitry Andric       if (VT != MVT::i32)
30170b57cec5SDimitry Andric         return {0u, nullptr};
30180b57cec5SDimitry Andric       return {0u, &Hexagon::ModRegsRegClass};
30190b57cec5SDimitry Andric     case 'q': // q0-q3
30200b57cec5SDimitry Andric       switch (VT.getSizeInBits()) {
30210b57cec5SDimitry Andric       default:
30220b57cec5SDimitry Andric         return {0u, nullptr};
30230b57cec5SDimitry Andric       case 512:
30240b57cec5SDimitry Andric       case 1024:
30250b57cec5SDimitry Andric         return {0u, &Hexagon::HvxQRRegClass};
30260b57cec5SDimitry Andric       }
30270b57cec5SDimitry Andric       break;
30280b57cec5SDimitry Andric     case 'v': // V0-V31
30290b57cec5SDimitry Andric       switch (VT.getSizeInBits()) {
30300b57cec5SDimitry Andric       default:
30310b57cec5SDimitry Andric         return {0u, nullptr};
30320b57cec5SDimitry Andric       case 512:
30330b57cec5SDimitry Andric         return {0u, &Hexagon::HvxVRRegClass};
30340b57cec5SDimitry Andric       case 1024:
30350b57cec5SDimitry Andric         if (Subtarget.hasV60Ops() && Subtarget.useHVX128BOps())
30360b57cec5SDimitry Andric           return {0u, &Hexagon::HvxVRRegClass};
30370b57cec5SDimitry Andric         return {0u, &Hexagon::HvxWRRegClass};
30380b57cec5SDimitry Andric       case 2048:
30390b57cec5SDimitry Andric         return {0u, &Hexagon::HvxWRRegClass};
30400b57cec5SDimitry Andric       }
30410b57cec5SDimitry Andric       break;
30420b57cec5SDimitry Andric     default:
30430b57cec5SDimitry Andric       return {0u, nullptr};
30440b57cec5SDimitry Andric     }
30450b57cec5SDimitry Andric   }
30460b57cec5SDimitry Andric 
30470b57cec5SDimitry Andric   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
30480b57cec5SDimitry Andric }
30490b57cec5SDimitry Andric 
30500b57cec5SDimitry Andric /// isFPImmLegal - Returns true if the target can instruction select the
30510b57cec5SDimitry Andric /// specified FP immediate natively. If false, the legalizer will
30520b57cec5SDimitry Andric /// materialize the FP immediate as a load from a constant pool.
30530b57cec5SDimitry Andric bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
30540b57cec5SDimitry Andric                                          bool ForCodeSize) const {
30550b57cec5SDimitry Andric   return true;
30560b57cec5SDimitry Andric }
30570b57cec5SDimitry Andric 
30580b57cec5SDimitry Andric /// isLegalAddressingMode - Return true if the addressing mode represented by
30590b57cec5SDimitry Andric /// AM is legal for this target, for a load/store of the specified type.
30600b57cec5SDimitry Andric bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
30610b57cec5SDimitry Andric                                                   const AddrMode &AM, Type *Ty,
30620b57cec5SDimitry Andric                                                   unsigned AS, Instruction *I) const {
30630b57cec5SDimitry Andric   if (Ty->isSized()) {
30640b57cec5SDimitry Andric     // When LSR detects uses of the same base address to access different
30650b57cec5SDimitry Andric     // types (e.g. unions), it will assume a conservative type for these
30660b57cec5SDimitry Andric     // uses:
30670b57cec5SDimitry Andric     //   LSR Use: Kind=Address of void in addrspace(4294967295), ...
30680b57cec5SDimitry Andric     // The type Ty passed here would then be "void". Skip the alignment
30690b57cec5SDimitry Andric     // checks, but do not return false right away, since that confuses
30700b57cec5SDimitry Andric     // LSR into crashing.
30710b57cec5SDimitry Andric     unsigned A = DL.getABITypeAlignment(Ty);
30720b57cec5SDimitry Andric     // The base offset must be a multiple of the alignment.
30730b57cec5SDimitry Andric     if ((AM.BaseOffs % A) != 0)
30740b57cec5SDimitry Andric       return false;
30750b57cec5SDimitry Andric     // The shifted offset must fit in 11 bits.
30760b57cec5SDimitry Andric     if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
30770b57cec5SDimitry Andric       return false;
30780b57cec5SDimitry Andric   }
30790b57cec5SDimitry Andric 
30800b57cec5SDimitry Andric   // No global is ever allowed as a base.
30810b57cec5SDimitry Andric   if (AM.BaseGV)
30820b57cec5SDimitry Andric     return false;
30830b57cec5SDimitry Andric 
30840b57cec5SDimitry Andric   int Scale = AM.Scale;
30850b57cec5SDimitry Andric   if (Scale < 0)
30860b57cec5SDimitry Andric     Scale = -Scale;
30870b57cec5SDimitry Andric   switch (Scale) {
30880b57cec5SDimitry Andric   case 0:  // No scale reg, "r+i", "r", or just "i".
30890b57cec5SDimitry Andric     break;
30900b57cec5SDimitry Andric   default: // No scaled addressing mode.
30910b57cec5SDimitry Andric     return false;
30920b57cec5SDimitry Andric   }
30930b57cec5SDimitry Andric   return true;
30940b57cec5SDimitry Andric }
30950b57cec5SDimitry Andric 
30960b57cec5SDimitry Andric /// Return true if folding a constant offset with the given GlobalAddress is
30970b57cec5SDimitry Andric /// legal.  It is frequently not legal in PIC relocation models.
30980b57cec5SDimitry Andric bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
30990b57cec5SDimitry Andric       const {
31000b57cec5SDimitry Andric   return HTM.getRelocationModel() == Reloc::Static;
31010b57cec5SDimitry Andric }
31020b57cec5SDimitry Andric 
31030b57cec5SDimitry Andric /// isLegalICmpImmediate - Return true if the specified immediate is legal
31040b57cec5SDimitry Andric /// icmp immediate, that is the target has icmp instructions which can compare
31050b57cec5SDimitry Andric /// a register against the immediate without having to materialize the
31060b57cec5SDimitry Andric /// immediate into a register.
31070b57cec5SDimitry Andric bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
31080b57cec5SDimitry Andric   return Imm >= -512 && Imm <= 511;
31090b57cec5SDimitry Andric }
31100b57cec5SDimitry Andric 
31110b57cec5SDimitry Andric /// IsEligibleForTailCallOptimization - Check whether the call is eligible
31120b57cec5SDimitry Andric /// for tail call optimization. Targets which want to do tail call
31130b57cec5SDimitry Andric /// optimization should implement this function.
31140b57cec5SDimitry Andric bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
31150b57cec5SDimitry Andric                                  SDValue Callee,
31160b57cec5SDimitry Andric                                  CallingConv::ID CalleeCC,
31170b57cec5SDimitry Andric                                  bool IsVarArg,
31180b57cec5SDimitry Andric                                  bool IsCalleeStructRet,
31190b57cec5SDimitry Andric                                  bool IsCallerStructRet,
31200b57cec5SDimitry Andric                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
31210b57cec5SDimitry Andric                                  const SmallVectorImpl<SDValue> &OutVals,
31220b57cec5SDimitry Andric                                  const SmallVectorImpl<ISD::InputArg> &Ins,
31230b57cec5SDimitry Andric                                  SelectionDAG& DAG) const {
31240b57cec5SDimitry Andric   const Function &CallerF = DAG.getMachineFunction().getFunction();
31250b57cec5SDimitry Andric   CallingConv::ID CallerCC = CallerF.getCallingConv();
31260b57cec5SDimitry Andric   bool CCMatch = CallerCC == CalleeCC;
31270b57cec5SDimitry Andric 
31280b57cec5SDimitry Andric   // ***************************************************************************
31290b57cec5SDimitry Andric   //  Look for obvious safe cases to perform tail call optimization that do not
31300b57cec5SDimitry Andric   //  require ABI changes.
31310b57cec5SDimitry Andric   // ***************************************************************************
31320b57cec5SDimitry Andric 
31330b57cec5SDimitry Andric   // If this is a tail call via a function pointer, then don't do it!
31340b57cec5SDimitry Andric   if (!isa<GlobalAddressSDNode>(Callee) &&
31350b57cec5SDimitry Andric       !isa<ExternalSymbolSDNode>(Callee)) {
31360b57cec5SDimitry Andric     return false;
31370b57cec5SDimitry Andric   }
31380b57cec5SDimitry Andric 
31390b57cec5SDimitry Andric   // Do not optimize if the calling conventions do not match and the conventions
31400b57cec5SDimitry Andric   // used are not C or Fast.
31410b57cec5SDimitry Andric   if (!CCMatch) {
31420b57cec5SDimitry Andric     bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
31430b57cec5SDimitry Andric     bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
31440b57cec5SDimitry Andric     // If R & E, then ok.
31450b57cec5SDimitry Andric     if (!R || !E)
31460b57cec5SDimitry Andric       return false;
31470b57cec5SDimitry Andric   }
31480b57cec5SDimitry Andric 
31490b57cec5SDimitry Andric   // Do not tail call optimize vararg calls.
31500b57cec5SDimitry Andric   if (IsVarArg)
31510b57cec5SDimitry Andric     return false;
31520b57cec5SDimitry Andric 
31530b57cec5SDimitry Andric   // Also avoid tail call optimization if either caller or callee uses struct
31540b57cec5SDimitry Andric   // return semantics.
31550b57cec5SDimitry Andric   if (IsCalleeStructRet || IsCallerStructRet)
31560b57cec5SDimitry Andric     return false;
31570b57cec5SDimitry Andric 
31580b57cec5SDimitry Andric   // In addition to the cases above, we also disable Tail Call Optimization if
31590b57cec5SDimitry Andric   // the calling convention code that at least one outgoing argument needs to
31600b57cec5SDimitry Andric   // go on the stack. We cannot check that here because at this point that
31610b57cec5SDimitry Andric   // information is not available.
31620b57cec5SDimitry Andric   return true;
31630b57cec5SDimitry Andric }
31640b57cec5SDimitry Andric 
31650b57cec5SDimitry Andric /// Returns the target specific optimal type for load and store operations as
31660b57cec5SDimitry Andric /// a result of memset, memcpy, and memmove lowering.
31670b57cec5SDimitry Andric ///
31680b57cec5SDimitry Andric /// If DstAlign is zero that means it's safe to destination alignment can
31690b57cec5SDimitry Andric /// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
31700b57cec5SDimitry Andric /// a need to check it against alignment requirement, probably because the
31710b57cec5SDimitry Andric /// source does not need to be loaded. If 'IsMemset' is true, that means it's
31720b57cec5SDimitry Andric /// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
31730b57cec5SDimitry Andric /// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
31740b57cec5SDimitry Andric /// does not need to be loaded.  It returns EVT::Other if the type should be
31750b57cec5SDimitry Andric /// determined using generic target-independent logic.
31760b57cec5SDimitry Andric EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
31770b57cec5SDimitry Andric       unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
31780b57cec5SDimitry Andric       bool MemcpyStrSrc, const AttributeList &FuncAttributes) const {
31790b57cec5SDimitry Andric 
31800b57cec5SDimitry Andric   auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
31810b57cec5SDimitry Andric     return (GivenA % MinA) == 0;
31820b57cec5SDimitry Andric   };
31830b57cec5SDimitry Andric 
31840b57cec5SDimitry Andric   if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
31850b57cec5SDimitry Andric     return MVT::i64;
31860b57cec5SDimitry Andric   if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
31870b57cec5SDimitry Andric     return MVT::i32;
31880b57cec5SDimitry Andric   if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
31890b57cec5SDimitry Andric     return MVT::i16;
31900b57cec5SDimitry Andric 
31910b57cec5SDimitry Andric   return MVT::Other;
31920b57cec5SDimitry Andric }
31930b57cec5SDimitry Andric 
31940b57cec5SDimitry Andric bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(
31950b57cec5SDimitry Andric     EVT VT, unsigned AS, unsigned Align, MachineMemOperand::Flags Flags,
31960b57cec5SDimitry Andric     bool *Fast) const {
31970b57cec5SDimitry Andric   if (Fast)
31980b57cec5SDimitry Andric     *Fast = false;
31990b57cec5SDimitry Andric   return Subtarget.isHVXVectorType(VT.getSimpleVT());
32000b57cec5SDimitry Andric }
32010b57cec5SDimitry Andric 
32020b57cec5SDimitry Andric std::pair<const TargetRegisterClass*, uint8_t>
32030b57cec5SDimitry Andric HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
32040b57cec5SDimitry Andric       MVT VT) const {
32050b57cec5SDimitry Andric   if (Subtarget.isHVXVectorType(VT, true)) {
32060b57cec5SDimitry Andric     unsigned BitWidth = VT.getSizeInBits();
32070b57cec5SDimitry Andric     unsigned VecWidth = Subtarget.getVectorLength() * 8;
32080b57cec5SDimitry Andric 
32090b57cec5SDimitry Andric     if (VT.getVectorElementType() == MVT::i1)
32100b57cec5SDimitry Andric       return std::make_pair(&Hexagon::HvxQRRegClass, 1);
32110b57cec5SDimitry Andric     if (BitWidth == VecWidth)
32120b57cec5SDimitry Andric       return std::make_pair(&Hexagon::HvxVRRegClass, 1);
32130b57cec5SDimitry Andric     assert(BitWidth == 2 * VecWidth);
32140b57cec5SDimitry Andric     return std::make_pair(&Hexagon::HvxWRRegClass, 1);
32150b57cec5SDimitry Andric   }
32160b57cec5SDimitry Andric 
32170b57cec5SDimitry Andric   return TargetLowering::findRepresentativeClass(TRI, VT);
32180b57cec5SDimitry Andric }
32190b57cec5SDimitry Andric 
32200b57cec5SDimitry Andric bool HexagonTargetLowering::shouldReduceLoadWidth(SDNode *Load,
32210b57cec5SDimitry Andric       ISD::LoadExtType ExtTy, EVT NewVT) const {
32220b57cec5SDimitry Andric   // TODO: This may be worth removing. Check regression tests for diffs.
32230b57cec5SDimitry Andric   if (!TargetLoweringBase::shouldReduceLoadWidth(Load, ExtTy, NewVT))
32240b57cec5SDimitry Andric     return false;
32250b57cec5SDimitry Andric 
32260b57cec5SDimitry Andric   auto *L = cast<LoadSDNode>(Load);
32270b57cec5SDimitry Andric   std::pair<SDValue,int> BO = getBaseAndOffset(L->getBasePtr());
32280b57cec5SDimitry Andric   // Small-data object, do not shrink.
32290b57cec5SDimitry Andric   if (BO.first.getOpcode() == HexagonISD::CONST32_GP)
32300b57cec5SDimitry Andric     return false;
32310b57cec5SDimitry Andric   if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(BO.first)) {
32320b57cec5SDimitry Andric     auto &HTM = static_cast<const HexagonTargetMachine&>(getTargetMachine());
32330b57cec5SDimitry Andric     const auto *GO = dyn_cast_or_null<const GlobalObject>(GA->getGlobal());
32340b57cec5SDimitry Andric     return !GO || !HTM.getObjFileLowering()->isGlobalInSmallSection(GO, HTM);
32350b57cec5SDimitry Andric   }
32360b57cec5SDimitry Andric   return true;
32370b57cec5SDimitry Andric }
32380b57cec5SDimitry Andric 
32390b57cec5SDimitry Andric Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
32400b57cec5SDimitry Andric       AtomicOrdering Ord) const {
32410b57cec5SDimitry Andric   BasicBlock *BB = Builder.GetInsertBlock();
32420b57cec5SDimitry Andric   Module *M = BB->getParent()->getParent();
32430b57cec5SDimitry Andric   auto PT = cast<PointerType>(Addr->getType());
32440b57cec5SDimitry Andric   Type *Ty = PT->getElementType();
32450b57cec5SDimitry Andric   unsigned SZ = Ty->getPrimitiveSizeInBits();
32460b57cec5SDimitry Andric   assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
32470b57cec5SDimitry Andric   Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
32480b57cec5SDimitry Andric                                    : Intrinsic::hexagon_L4_loadd_locked;
32490b57cec5SDimitry Andric   Function *Fn = Intrinsic::getDeclaration(M, IntID);
32500b57cec5SDimitry Andric 
32510b57cec5SDimitry Andric   PointerType *NewPtrTy
32520b57cec5SDimitry Andric     = Builder.getIntNTy(SZ)->getPointerTo(PT->getAddressSpace());
32530b57cec5SDimitry Andric   Addr = Builder.CreateBitCast(Addr, NewPtrTy);
32540b57cec5SDimitry Andric 
32550b57cec5SDimitry Andric   Value *Call = Builder.CreateCall(Fn, Addr, "larx");
32560b57cec5SDimitry Andric 
32570b57cec5SDimitry Andric   return Builder.CreateBitCast(Call, Ty);
32580b57cec5SDimitry Andric }
32590b57cec5SDimitry Andric 
32600b57cec5SDimitry Andric /// Perform a store-conditional operation to Addr. Return the status of the
32610b57cec5SDimitry Andric /// store. This should be 0 if the store succeeded, non-zero otherwise.
32620b57cec5SDimitry Andric Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
32630b57cec5SDimitry Andric       Value *Val, Value *Addr, AtomicOrdering Ord) const {
32640b57cec5SDimitry Andric   BasicBlock *BB = Builder.GetInsertBlock();
32650b57cec5SDimitry Andric   Module *M = BB->getParent()->getParent();
32660b57cec5SDimitry Andric   Type *Ty = Val->getType();
32670b57cec5SDimitry Andric   unsigned SZ = Ty->getPrimitiveSizeInBits();
32680b57cec5SDimitry Andric 
32690b57cec5SDimitry Andric   Type *CastTy = Builder.getIntNTy(SZ);
32700b57cec5SDimitry Andric   assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
32710b57cec5SDimitry Andric   Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
32720b57cec5SDimitry Andric                                    : Intrinsic::hexagon_S4_stored_locked;
32730b57cec5SDimitry Andric   Function *Fn = Intrinsic::getDeclaration(M, IntID);
32740b57cec5SDimitry Andric 
32750b57cec5SDimitry Andric   unsigned AS = Addr->getType()->getPointerAddressSpace();
32760b57cec5SDimitry Andric   Addr = Builder.CreateBitCast(Addr, CastTy->getPointerTo(AS));
32770b57cec5SDimitry Andric   Val = Builder.CreateBitCast(Val, CastTy);
32780b57cec5SDimitry Andric 
32790b57cec5SDimitry Andric   Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
32800b57cec5SDimitry Andric   Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
32810b57cec5SDimitry Andric   Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
32820b57cec5SDimitry Andric   return Ext;
32830b57cec5SDimitry Andric }
32840b57cec5SDimitry Andric 
32850b57cec5SDimitry Andric TargetLowering::AtomicExpansionKind
32860b57cec5SDimitry Andric HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
32870b57cec5SDimitry Andric   // Do not expand loads and stores that don't exceed 64 bits.
32880b57cec5SDimitry Andric   return LI->getType()->getPrimitiveSizeInBits() > 64
32890b57cec5SDimitry Andric              ? AtomicExpansionKind::LLOnly
32900b57cec5SDimitry Andric              : AtomicExpansionKind::None;
32910b57cec5SDimitry Andric }
32920b57cec5SDimitry Andric 
32930b57cec5SDimitry Andric bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
32940b57cec5SDimitry Andric   // Do not expand loads and stores that don't exceed 64 bits.
32950b57cec5SDimitry Andric   return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
32960b57cec5SDimitry Andric }
32970b57cec5SDimitry Andric 
32980b57cec5SDimitry Andric TargetLowering::AtomicExpansionKind
32990b57cec5SDimitry Andric HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
33000b57cec5SDimitry Andric     AtomicCmpXchgInst *AI) const {
33010b57cec5SDimitry Andric   const DataLayout &DL = AI->getModule()->getDataLayout();
33020b57cec5SDimitry Andric   unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
33030b57cec5SDimitry Andric   if (Size >= 4 && Size <= 8)
33040b57cec5SDimitry Andric     return AtomicExpansionKind::LLSC;
33050b57cec5SDimitry Andric   return AtomicExpansionKind::None;
33060b57cec5SDimitry Andric }
3307