LLVM API Documentation

HexagonISelLowering.cpp
Go to the documentation of this file.
00001 //===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the interfaces that Hexagon uses to lower LLVM code
00011 // into a selection DAG.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "HexagonISelLowering.h"
00016 #include "HexagonMachineFunctionInfo.h"
00017 #include "HexagonSubtarget.h"
00018 #include "HexagonTargetMachine.h"
00019 #include "HexagonTargetObjectFile.h"
00020 #include "llvm/CodeGen/CallingConvLower.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineFunction.h"
00023 #include "llvm/CodeGen/MachineInstrBuilder.h"
00024 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00025 #include "llvm/CodeGen/MachineRegisterInfo.h"
00026 #include "llvm/CodeGen/SelectionDAGISel.h"
00027 #include "llvm/CodeGen/ValueTypes.h"
00028 #include "llvm/IR/CallingConv.h"
00029 #include "llvm/IR/DerivedTypes.h"
00030 #include "llvm/IR/Function.h"
00031 #include "llvm/IR/GlobalAlias.h"
00032 #include "llvm/IR/GlobalVariable.h"
00033 #include "llvm/IR/InlineAsm.h"
00034 #include "llvm/IR/Intrinsics.h"
00035 #include "llvm/Support/CommandLine.h"
00036 #include "llvm/Support/Debug.h"
00037 #include "llvm/Support/ErrorHandling.h"
00038 #include "llvm/Support/raw_ostream.h"
00039 
00040 using namespace llvm;
00041 
00042 #define DEBUG_TYPE "hexagon-lowering"
00043 
00044 static cl::opt<bool>
00045 EmitJumpTables("hexagon-emit-jump-tables", cl::init(true), cl::Hidden,
00046                cl::desc("Control jump table emission on Hexagon target"));
00047 
00048 namespace {
00049 class HexagonCCState : public CCState {
00050   int NumNamedVarArgParams;
00051 
00052 public:
00053   HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
00054                  SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
00055                  int NumNamedVarArgParams)
00056       : CCState(CC, isVarArg, MF, locs, C),
00057         NumNamedVarArgParams(NumNamedVarArgParams) {}
00058 
00059   int getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
00060 };
00061 }
00062 
00063 // Implement calling convention for Hexagon.
00064 static bool
00065 CC_Hexagon(unsigned ValNo, MVT ValVT,
00066            MVT LocVT, CCValAssign::LocInfo LocInfo,
00067            ISD::ArgFlagsTy ArgFlags, CCState &State);
00068 
00069 static bool
00070 CC_Hexagon32(unsigned ValNo, MVT ValVT,
00071              MVT LocVT, CCValAssign::LocInfo LocInfo,
00072              ISD::ArgFlagsTy ArgFlags, CCState &State);
00073 
00074 static bool
00075 CC_Hexagon64(unsigned ValNo, MVT ValVT,
00076              MVT LocVT, CCValAssign::LocInfo LocInfo,
00077              ISD::ArgFlagsTy ArgFlags, CCState &State);
00078 
00079 static bool
00080 RetCC_Hexagon(unsigned ValNo, MVT ValVT,
00081               MVT LocVT, CCValAssign::LocInfo LocInfo,
00082               ISD::ArgFlagsTy ArgFlags, CCState &State);
00083 
00084 static bool
00085 RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
00086                 MVT LocVT, CCValAssign::LocInfo LocInfo,
00087                 ISD::ArgFlagsTy ArgFlags, CCState &State);
00088 
00089 static bool
00090 RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
00091                 MVT LocVT, CCValAssign::LocInfo LocInfo,
00092                 ISD::ArgFlagsTy ArgFlags, CCState &State);
00093 
00094 static bool
00095 CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
00096             MVT LocVT, CCValAssign::LocInfo LocInfo,
00097             ISD::ArgFlagsTy ArgFlags, CCState &State) {
00098   HexagonCCState &HState = static_cast<HexagonCCState &>(State);
00099 
00100   // NumNamedVarArgParams can not be zero for a VarArg function.
00101   assert((HState.getNumNamedVarArgParams() > 0) &&
00102          "NumNamedVarArgParams is not bigger than zero.");
00103 
00104   if ((int)ValNo < HState.getNumNamedVarArgParams()) {
00105     // Deal with named arguments.
00106     return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
00107   }
00108 
00109   // Deal with un-named arguments.
00110   unsigned ofst;
00111   if (ArgFlags.isByVal()) {
00112     // If pass-by-value, the size allocated on stack is decided
00113     // by ArgFlags.getByValSize(), not by the size of LocVT.
00114     assert ((ArgFlags.getByValSize() > 8) &&
00115             "ByValSize must be bigger than 8 bytes");
00116     ofst = State.AllocateStack(ArgFlags.getByValSize(), 4);
00117     State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
00118     return false;
00119   }
00120   if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
00121     LocVT = MVT::i32;
00122     ValVT = MVT::i32;
00123     if (ArgFlags.isSExt())
00124       LocInfo = CCValAssign::SExt;
00125     else if (ArgFlags.isZExt())
00126       LocInfo = CCValAssign::ZExt;
00127     else
00128       LocInfo = CCValAssign::AExt;
00129   }
00130   if (LocVT == MVT::i32 || LocVT == MVT::f32) {
00131     ofst = State.AllocateStack(4, 4);
00132     State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
00133     return false;
00134   }
00135   if (LocVT == MVT::i64 || LocVT == MVT::f64) {
00136     ofst = State.AllocateStack(8, 8);
00137     State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
00138     return false;
00139   }
00140   llvm_unreachable(nullptr);
00141 }
00142 
00143 
00144 static bool
00145 CC_Hexagon (unsigned ValNo, MVT ValVT,
00146             MVT LocVT, CCValAssign::LocInfo LocInfo,
00147             ISD::ArgFlagsTy ArgFlags, CCState &State) {
00148 
00149   if (ArgFlags.isByVal()) {
00150     // Passed on stack.
00151     assert ((ArgFlags.getByValSize() > 8) &&
00152             "ByValSize must be bigger than 8 bytes");
00153     unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 4);
00154     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
00155     return false;
00156   }
00157 
00158   if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
00159     LocVT = MVT::i32;
00160     ValVT = MVT::i32;
00161     if (ArgFlags.isSExt())
00162       LocInfo = CCValAssign::SExt;
00163     else if (ArgFlags.isZExt())
00164       LocInfo = CCValAssign::ZExt;
00165     else
00166       LocInfo = CCValAssign::AExt;
00167   }
00168 
00169   if (LocVT == MVT::i32 || LocVT == MVT::f32) {
00170     if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
00171       return false;
00172   }
00173 
00174   if (LocVT == MVT::i64 || LocVT == MVT::f64) {
00175     if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
00176       return false;
00177   }
00178 
00179   return true;  // CC didn't match.
00180 }
00181 
00182 
00183 static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
00184                          MVT LocVT, CCValAssign::LocInfo LocInfo,
00185                          ISD::ArgFlagsTy ArgFlags, CCState &State) {
00186 
00187   static const MCPhysReg RegList[] = {
00188     Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
00189     Hexagon::R5
00190   };
00191   if (unsigned Reg = State.AllocateReg(RegList, 6)) {
00192     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
00193     return false;
00194   }
00195 
00196   unsigned Offset = State.AllocateStack(4, 4);
00197   State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
00198   return false;
00199 }
00200 
00201 static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
00202                          MVT LocVT, CCValAssign::LocInfo LocInfo,
00203                          ISD::ArgFlagsTy ArgFlags, CCState &State) {
00204 
00205   if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
00206     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
00207     return false;
00208   }
00209 
00210   static const MCPhysReg RegList1[] = {
00211     Hexagon::D1, Hexagon::D2
00212   };
00213   static const MCPhysReg RegList2[] = {
00214     Hexagon::R1, Hexagon::R3
00215   };
00216   if (unsigned Reg = State.AllocateReg(RegList1, RegList2, 2)) {
00217     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
00218     return false;
00219   }
00220 
00221   unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
00222   State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
00223   return false;
00224 }
00225 
00226 static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
00227                           MVT LocVT, CCValAssign::LocInfo LocInfo,
00228                           ISD::ArgFlagsTy ArgFlags, CCState &State) {
00229 
00230 
00231   if (LocVT == MVT::i1 ||
00232       LocVT == MVT::i8 ||
00233       LocVT == MVT::i16) {
00234     LocVT = MVT::i32;
00235     ValVT = MVT::i32;
00236     if (ArgFlags.isSExt())
00237       LocInfo = CCValAssign::SExt;
00238     else if (ArgFlags.isZExt())
00239       LocInfo = CCValAssign::ZExt;
00240     else
00241       LocInfo = CCValAssign::AExt;
00242   }
00243 
00244   if (LocVT == MVT::i32 || LocVT == MVT::f32) {
00245     if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
00246     return false;
00247   }
00248 
00249   if (LocVT == MVT::i64 || LocVT == MVT::f64) {
00250     if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
00251     return false;
00252   }
00253 
00254   return true;  // CC didn't match.
00255 }
00256 
00257 static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
00258                             MVT LocVT, CCValAssign::LocInfo LocInfo,
00259                             ISD::ArgFlagsTy ArgFlags, CCState &State) {
00260 
00261   if (LocVT == MVT::i32 || LocVT == MVT::f32) {
00262     if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
00263       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
00264       return false;
00265     }
00266   }
00267 
00268   unsigned Offset = State.AllocateStack(4, 4);
00269   State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
00270   return false;
00271 }
00272 
00273 static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
00274                             MVT LocVT, CCValAssign::LocInfo LocInfo,
00275                             ISD::ArgFlagsTy ArgFlags, CCState &State) {
00276   if (LocVT == MVT::i64 || LocVT == MVT::f64) {
00277     if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
00278       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
00279       return false;
00280     }
00281   }
00282 
00283   unsigned Offset = State.AllocateStack(8, 8);
00284   State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
00285   return false;
00286 }
00287 
00288 SDValue
00289 HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
00290 const {
00291   return SDValue();
00292 }
00293 
00294 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
00295 /// by "Src" to address "Dst" of size "Size".  Alignment information is
00296 /// specified by the specific parameter attribute. The copy will be passed as
00297 /// a byval function parameter.  Sometimes what we are copying is the end of a
00298 /// larger object, the part that does not fit in registers.
00299 static SDValue
00300 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
00301                           ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
00302                           SDLoc dl) {
00303 
00304   SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
00305   return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
00306                        /*isVolatile=*/false, /*AlwaysInline=*/false,
00307                        MachinePointerInfo(), MachinePointerInfo());
00308 }
00309 
00310 
00311 // LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
00312 // passed by value, the function prototype is modified to return void and
00313 // the value is stored in memory pointed by a pointer passed by caller.
00314 SDValue
00315 HexagonTargetLowering::LowerReturn(SDValue Chain,
00316                                    CallingConv::ID CallConv, bool isVarArg,
00317                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
00318                                    const SmallVectorImpl<SDValue> &OutVals,
00319                                    SDLoc dl, SelectionDAG &DAG) const {
00320 
00321   // CCValAssign - represent the assignment of the return value to locations.
00322   SmallVector<CCValAssign, 16> RVLocs;
00323 
00324   // CCState - Info about the registers and stack slot.
00325   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
00326                  *DAG.getContext());
00327 
00328   // Analyze return values of ISD::RET
00329   CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
00330 
00331   SDValue Flag;
00332   SmallVector<SDValue, 4> RetOps(1, Chain);
00333 
00334   // Copy the result values into the output registers.
00335   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00336     CCValAssign &VA = RVLocs[i];
00337 
00338     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
00339 
00340     // Guarantee that all emitted copies are stuck together with flags.
00341     Flag = Chain.getValue(1);
00342     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
00343   }
00344 
00345   RetOps[0] = Chain;  // Update chain.
00346 
00347   // Add the flag if we have it.
00348   if (Flag.getNode())
00349     RetOps.push_back(Flag);
00350 
00351   return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
00352 }
00353 
00354 
00355 
00356 
00357 /// LowerCallResult - Lower the result values of an ISD::CALL into the
00358 /// appropriate copies out of appropriate physical registers.  This assumes that
00359 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
00360 /// being lowered. Returns a SDNode with the same number of values as the
00361 /// ISD::CALL.
00362 SDValue
00363 HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
00364                                        CallingConv::ID CallConv, bool isVarArg,
00365                                        const
00366                                        SmallVectorImpl<ISD::InputArg> &Ins,
00367                                        SDLoc dl, SelectionDAG &DAG,
00368                                        SmallVectorImpl<SDValue> &InVals,
00369                                        const SmallVectorImpl<SDValue> &OutVals,
00370                                        SDValue Callee) const {
00371 
00372   // Assign locations to each value returned by this call.
00373   SmallVector<CCValAssign, 16> RVLocs;
00374 
00375   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
00376                  *DAG.getContext());
00377 
00378   CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
00379 
00380   // Copy all of the result registers out of their specified physreg.
00381   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00382     Chain = DAG.getCopyFromReg(Chain, dl,
00383                                RVLocs[i].getLocReg(),
00384                                RVLocs[i].getValVT(), InFlag).getValue(1);
00385     InFlag = Chain.getValue(2);
00386     InVals.push_back(Chain.getValue(0));
00387   }
00388 
00389   return Chain;
00390 }
00391 
00392 /// LowerCall - Functions arguments are copied from virtual regs to
00393 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
00394 SDValue
00395 HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
00396                                  SmallVectorImpl<SDValue> &InVals) const {
00397   SelectionDAG &DAG                     = CLI.DAG;
00398   SDLoc &dl                             = CLI.DL;
00399   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
00400   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
00401   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
00402   SDValue Chain                         = CLI.Chain;
00403   SDValue Callee                        = CLI.Callee;
00404   bool &isTailCall                      = CLI.IsTailCall;
00405   CallingConv::ID CallConv              = CLI.CallConv;
00406   bool isVarArg                         = CLI.IsVarArg;
00407 
00408   bool IsStructRet    = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
00409 
00410   // Check for varargs.
00411   int NumNamedVarArgParams = -1;
00412   if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
00413   {
00414     const Function* CalleeFn = nullptr;
00415     Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
00416     if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
00417     {
00418       // If a function has zero args and is a vararg function, that's
00419       // disallowed so it must be an undeclared function.  Do not assume
00420       // varargs if the callee is undefined.
00421       if (CalleeFn->isVarArg() &&
00422           CalleeFn->getFunctionType()->getNumParams() != 0) {
00423         NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
00424       }
00425     }
00426   }
00427 
00428   // Analyze operands of the call, assigning locations to each operand.
00429   SmallVector<CCValAssign, 16> ArgLocs;
00430   HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
00431                         *DAG.getContext(), NumNamedVarArgParams);
00432 
00433   if (NumNamedVarArgParams > 0)
00434     CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
00435   else
00436     CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
00437 
00438 
00439   if(isTailCall) {
00440     bool StructAttrFlag =
00441       DAG.getMachineFunction().getFunction()->hasStructRetAttr();
00442     isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
00443                                                    isVarArg, IsStructRet,
00444                                                    StructAttrFlag,
00445                                                    Outs, OutVals, Ins, DAG);
00446     for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i){
00447       CCValAssign &VA = ArgLocs[i];
00448       if (VA.isMemLoc()) {
00449         isTailCall = false;
00450         break;
00451       }
00452     }
00453     if (isTailCall) {
00454       DEBUG(dbgs () << "Eligible for Tail Call\n");
00455     } else {
00456       DEBUG(dbgs () <<
00457             "Argument must be passed on stack. Not eligible for Tail Call\n");
00458     }
00459   }
00460   // Get a count of how many bytes are to be pushed on the stack.
00461   unsigned NumBytes = CCInfo.getNextStackOffset();
00462   SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
00463   SmallVector<SDValue, 8> MemOpChains;
00464 
00465   const HexagonRegisterInfo *QRI = static_cast<const HexagonRegisterInfo *>(
00466       DAG.getSubtarget().getRegisterInfo());
00467   SDValue StackPtr =
00468       DAG.getCopyFromReg(Chain, dl, QRI->getStackRegister(), getPointerTy());
00469 
00470   // Walk the register/memloc assignments, inserting copies/loads.
00471   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00472     CCValAssign &VA = ArgLocs[i];
00473     SDValue Arg = OutVals[i];
00474     ISD::ArgFlagsTy Flags = Outs[i].Flags;
00475 
00476     // Promote the value if needed.
00477     switch (VA.getLocInfo()) {
00478       default:
00479         // Loc info must be one of Full, SExt, ZExt, or AExt.
00480         llvm_unreachable("Unknown loc info!");
00481       case CCValAssign::Full:
00482         break;
00483       case CCValAssign::SExt:
00484         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
00485         break;
00486       case CCValAssign::ZExt:
00487         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
00488         break;
00489       case CCValAssign::AExt:
00490         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
00491         break;
00492     }
00493 
00494     if (VA.isMemLoc()) {
00495       unsigned LocMemOffset = VA.getLocMemOffset();
00496       SDValue PtrOff = DAG.getConstant(LocMemOffset, StackPtr.getValueType());
00497       PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
00498 
00499       if (Flags.isByVal()) {
00500         // The argument is a struct passed by value. According to LLVM, "Arg"
00501         // is is pointer.
00502         MemOpChains.push_back(CreateCopyOfByValArgument(Arg, PtrOff, Chain,
00503                                                         Flags, DAG, dl));
00504       } else {
00505         // The argument is not passed by value. "Arg" is a buildin type. It is
00506         // not a pointer.
00507         MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
00508                                            MachinePointerInfo(),false, false,
00509                                            0));
00510       }
00511       continue;
00512     }
00513 
00514     // Arguments that can be passed on register must be kept at RegsToPass
00515     // vector.
00516     if (VA.isRegLoc()) {
00517       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
00518     }
00519   }
00520 
00521   // Transform all store nodes into one single node because all store
00522   // nodes are independent of each other.
00523   if (!MemOpChains.empty()) {
00524     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
00525   }
00526 
00527   if (!isTailCall)
00528     Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes,
00529                                                         getPointerTy(), true),
00530                                  dl);
00531 
00532   // Build a sequence of copy-to-reg nodes chained together with token
00533   // chain and flag operands which copy the outgoing args into registers.
00534   // The InFlag in necessary since all emitted instructions must be
00535   // stuck together.
00536   SDValue InFlag;
00537   if (!isTailCall) {
00538     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
00539       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
00540                                RegsToPass[i].second, InFlag);
00541       InFlag = Chain.getValue(1);
00542     }
00543   }
00544 
00545   // For tail calls lower the arguments to the 'real' stack slot.
00546   if (isTailCall) {
00547     // Force all the incoming stack arguments to be loaded from the stack
00548     // before any new outgoing arguments are stored to the stack, because the
00549     // outgoing stack slots may alias the incoming argument stack slots, and
00550     // the alias isn't otherwise explicit. This is slightly more conservative
00551     // than necessary, because it means that each store effectively depends
00552     // on every argument instead of just those arguments it would clobber.
00553     //
00554     // Do not flag preceding copytoreg stuff together with the following stuff.
00555     InFlag = SDValue();
00556     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
00557       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
00558                                RegsToPass[i].second, InFlag);
00559       InFlag = Chain.getValue(1);
00560     }
00561     InFlag =SDValue();
00562   }
00563 
00564   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
00565   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
00566   // node so that legalize doesn't hack it.
00567   if (flag_aligned_memcpy) {
00568     const char *MemcpyName =
00569       "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
00570     Callee =
00571       DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
00572     flag_aligned_memcpy = false;
00573   } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
00574     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
00575   } else if (ExternalSymbolSDNode *S =
00576              dyn_cast<ExternalSymbolSDNode>(Callee)) {
00577     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
00578   }
00579 
00580   // Returns a chain & a flag for retval copy to use.
00581   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
00582   SmallVector<SDValue, 8> Ops;
00583   Ops.push_back(Chain);
00584   Ops.push_back(Callee);
00585 
00586   // Add argument registers to the end of the list so that they are
00587   // known live into the call.
00588   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
00589     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
00590                                   RegsToPass[i].second.getValueType()));
00591   }
00592 
00593   if (InFlag.getNode()) {
00594     Ops.push_back(InFlag);
00595   }
00596 
00597   if (isTailCall)
00598     return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
00599 
00600   Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
00601   InFlag = Chain.getValue(1);
00602 
00603   // Create the CALLSEQ_END node.
00604   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
00605                              DAG.getIntPtrConstant(0, true), InFlag, dl);
00606   InFlag = Chain.getValue(1);
00607 
00608   // Handle result values, copying them out of physregs into vregs that we
00609   // return.
00610   return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
00611                          InVals, OutVals, Callee);
00612 }
00613 
00614 static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
00615                                    bool isSEXTLoad, SDValue &Base,
00616                                    SDValue &Offset, bool &isInc,
00617                                    SelectionDAG &DAG) {
00618   if (Ptr->getOpcode() != ISD::ADD)
00619   return false;
00620 
00621   if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
00622     isInc = (Ptr->getOpcode() == ISD::ADD);
00623     Base = Ptr->getOperand(0);
00624     Offset = Ptr->getOperand(1);
00625     // Ensure that Offset is a constant.
00626     return (isa<ConstantSDNode>(Offset));
00627   }
00628 
00629   return false;
00630 }
00631 
00632 // TODO: Put this function along with the other isS* functions in
00633 // HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
00634 // functions defined in HexagonOperands.td.
00635 static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
00636   ConstantSDNode *N = cast<ConstantSDNode>(S);
00637 
00638   // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
00639   // field.
00640   int64_t v = (int64_t)N->getSExtValue();
00641   int64_t m = 0;
00642   if (ShiftAmount > 0) {
00643     m = v % ShiftAmount;
00644     v = v >> ShiftAmount;
00645   }
00646   return (v <= 7) && (v >= -8) && (m == 0);
00647 }
00648 
00649 /// getPostIndexedAddressParts - returns true by value, base pointer and
00650 /// offset pointer and addressing mode by reference if this node can be
00651 /// combined with a load / store to form a post-indexed load / store.
00652 bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
00653                                                        SDValue &Base,
00654                                                        SDValue &Offset,
00655                                                        ISD::MemIndexedMode &AM,
00656                                                        SelectionDAG &DAG) const
00657 {
00658   EVT VT;
00659   SDValue Ptr;
00660   bool isSEXTLoad = false;
00661 
00662   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
00663     VT  = LD->getMemoryVT();
00664     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
00665   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
00666     VT  = ST->getMemoryVT();
00667     if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
00668       return false;
00669     }
00670   } else {
00671     return false;
00672   }
00673 
00674   bool isInc = false;
00675   bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
00676                                         isInc, DAG);
00677   // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
00678   int ShiftAmount = VT.getSizeInBits() / 16;
00679   if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
00680     AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
00681     return true;
00682   }
00683 
00684   return false;
00685 }
00686 
00687 SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
00688                                               SelectionDAG &DAG) const {
00689   SDNode *Node = Op.getNode();
00690   MachineFunction &MF = DAG.getMachineFunction();
00691   HexagonMachineFunctionInfo *FuncInfo =
00692     MF.getInfo<HexagonMachineFunctionInfo>();
00693   switch (Node->getOpcode()) {
00694     case ISD::INLINEASM: {
00695       unsigned NumOps = Node->getNumOperands();
00696       if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
00697         --NumOps;  // Ignore the flag operand.
00698 
00699       for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
00700         if (FuncInfo->hasClobberLR())
00701           break;
00702         unsigned Flags =
00703           cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
00704         unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
00705         ++i;  // Skip the ID value.
00706 
00707         switch (InlineAsm::getKind(Flags)) {
00708         default: llvm_unreachable("Bad flags!");
00709           case InlineAsm::Kind_RegDef:
00710           case InlineAsm::Kind_RegUse:
00711           case InlineAsm::Kind_Imm:
00712           case InlineAsm::Kind_Clobber:
00713           case InlineAsm::Kind_Mem: {
00714             for (; NumVals; --NumVals, ++i) {}
00715             break;
00716           }
00717           case InlineAsm::Kind_RegDefEarlyClobber: {
00718             for (; NumVals; --NumVals, ++i) {
00719               unsigned Reg =
00720                 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
00721 
00722               // Check it to be lr
00723               const HexagonRegisterInfo *QRI =
00724                   static_cast<const HexagonRegisterInfo *>(
00725                       DAG.getSubtarget().getRegisterInfo());
00726               if (Reg == QRI->getRARegister()) {
00727                 FuncInfo->setHasClobberLR(true);
00728                 break;
00729               }
00730             }
00731             break;
00732           }
00733         }
00734       }
00735     }
00736   } // Node->getOpcode
00737   return Op;
00738 }
00739 
00740 
00741 //
00742 // Taken from the XCore backend.
00743 //
00744 SDValue HexagonTargetLowering::
00745 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
00746 {
00747   SDValue Chain = Op.getOperand(0);
00748   SDValue Table = Op.getOperand(1);
00749   SDValue Index = Op.getOperand(2);
00750   SDLoc dl(Op);
00751   JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
00752   unsigned JTI = JT->getIndex();
00753   MachineFunction &MF = DAG.getMachineFunction();
00754   const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
00755   SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
00756 
00757   // Mark all jump table targets as address taken.
00758   const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
00759   const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
00760   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
00761     MachineBasicBlock *MBB = JTBBs[i];
00762     MBB->setHasAddressTaken();
00763     // This line is needed to set the hasAddressTaken flag on the BasicBlock
00764     // object.
00765     BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
00766   }
00767 
00768   SDValue JumpTableBase = DAG.getNode(HexagonISD::WrapperJT, dl,
00769                                       getPointerTy(), TargetJT);
00770   SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
00771                                    DAG.getConstant(2, MVT::i32));
00772   SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
00773                                   ShiftIndex);
00774   SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
00775                                    MachinePointerInfo(), false, false, false,
00776                                    0);
00777   return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
00778 }
00779 
00780 
00781 SDValue
00782 HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
00783                                                SelectionDAG &DAG) const {
00784   SDValue Chain = Op.getOperand(0);
00785   SDValue Size = Op.getOperand(1);
00786   SDLoc dl(Op);
00787 
00788   unsigned SPReg = getStackPointerRegisterToSaveRestore();
00789 
00790   // Get a reference to the stack pointer.
00791   SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
00792 
00793   // Subtract the dynamic size from the actual stack size to
00794   // obtain the new stack size.
00795   SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
00796 
00797   //
00798   // For Hexagon, the outgoing memory arguments area should be on top of the
00799   // alloca area on the stack i.e., the outgoing memory arguments should be
00800   // at a lower address than the alloca area. Move the alloca area down the
00801   // stack by adding back the space reserved for outgoing arguments to SP
00802   // here.
00803   //
00804   // We do not know what the size of the outgoing args is at this point.
00805   // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
00806   // stack pointer. We patch this instruction with the correct, known
00807   // offset in emitPrologue().
00808   //
00809   // Use a placeholder immediate (zero) for now. This will be patched up
00810   // by emitPrologue().
00811   SDValue ArgAdjust = DAG.getNode(HexagonISD::ADJDYNALLOC, dl,
00812                                   MVT::i32,
00813                                   Sub,
00814                                   DAG.getConstant(0, MVT::i32));
00815 
00816   // The Sub result contains the new stack start address, so it
00817   // must be placed in the stack pointer register.
00818   const HexagonRegisterInfo *QRI = static_cast<const HexagonRegisterInfo *>(
00819       DAG.getSubtarget().getRegisterInfo());
00820   SDValue CopyChain = DAG.getCopyToReg(Chain, dl, QRI->getStackRegister(), Sub);
00821 
00822   SDValue Ops[2] = { ArgAdjust, CopyChain };
00823   return DAG.getMergeValues(Ops, dl);
00824 }
00825 
00826 SDValue
00827 HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
00828                                             CallingConv::ID CallConv,
00829                                             bool isVarArg,
00830                                             const
00831                                             SmallVectorImpl<ISD::InputArg> &Ins,
00832                                             SDLoc dl, SelectionDAG &DAG,
00833                                             SmallVectorImpl<SDValue> &InVals)
00834 const {
00835 
00836   MachineFunction &MF = DAG.getMachineFunction();
00837   MachineFrameInfo *MFI = MF.getFrameInfo();
00838   MachineRegisterInfo &RegInfo = MF.getRegInfo();
00839   HexagonMachineFunctionInfo *FuncInfo =
00840     MF.getInfo<HexagonMachineFunctionInfo>();
00841 
00842 
00843   // Assign locations to all of the incoming arguments.
00844   SmallVector<CCValAssign, 16> ArgLocs;
00845   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
00846                  *DAG.getContext());
00847 
00848   CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
00849 
00850   // For LLVM, in the case when returning a struct by value (>8byte),
00851   // the first argument is a pointer that points to the location on caller's
00852   // stack where the return value will be stored. For Hexagon, the location on
00853   // caller's stack is passed only when the struct size is smaller than (and
00854   // equal to) 8 bytes. If not, no address will be passed into callee and
00855   // callee return the result direclty through R0/R1.
00856 
00857   SmallVector<SDValue, 4> MemOps;
00858 
00859   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00860     CCValAssign &VA = ArgLocs[i];
00861     ISD::ArgFlagsTy Flags = Ins[i].Flags;
00862     unsigned ObjSize;
00863     unsigned StackLocation;
00864     int FI;
00865 
00866     if (   (VA.isRegLoc() && !Flags.isByVal())
00867         || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
00868       // Arguments passed in registers
00869       // 1. int, long long, ptr args that get allocated in register.
00870       // 2. Large struct that gets an register to put its address in.
00871       EVT RegVT = VA.getLocVT();
00872       if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
00873           RegVT == MVT::i32 || RegVT == MVT::f32) {
00874         unsigned VReg =
00875           RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
00876         RegInfo.addLiveIn(VA.getLocReg(), VReg);
00877         InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
00878       } else if (RegVT == MVT::i64) {
00879         unsigned VReg =
00880           RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
00881         RegInfo.addLiveIn(VA.getLocReg(), VReg);
00882         InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
00883       } else {
00884         assert (0);
00885       }
00886     } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
00887       assert (0 && "ByValSize must be bigger than 8 bytes");
00888     } else {
00889       // Sanity check.
00890       assert(VA.isMemLoc());
00891 
00892       if (Flags.isByVal()) {
00893         // If it's a byval parameter, then we need to compute the
00894         // "real" size, not the size of the pointer.
00895         ObjSize = Flags.getByValSize();
00896       } else {
00897         ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
00898       }
00899 
00900       StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
00901       // Create the frame index object for this incoming parameter...
00902       FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
00903 
00904       // Create the SelectionDAG nodes cordl, responding to a load
00905       // from this parameter.
00906       SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
00907 
00908       if (Flags.isByVal()) {
00909         // If it's a pass-by-value aggregate, then do not dereference the stack
00910         // location. Instead, we should generate a reference to the stack
00911         // location.
00912         InVals.push_back(FIN);
00913       } else {
00914         InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
00915                                      MachinePointerInfo(), false, false,
00916                                      false, 0));
00917       }
00918     }
00919   }
00920 
00921   if (!MemOps.empty())
00922     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
00923 
00924   if (isVarArg) {
00925     // This will point to the next argument passed via stack.
00926     int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
00927                                             HEXAGON_LRFP_SIZE +
00928                                             CCInfo.getNextStackOffset(),
00929                                             true);
00930     FuncInfo->setVarArgsFrameIndex(FrameIndex);
00931   }
00932 
00933   return Chain;
00934 }
00935 
00936 SDValue
00937 HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
00938   // VASTART stores the address of the VarArgsFrameIndex slot into the
00939   // memory location argument.
00940   MachineFunction &MF = DAG.getMachineFunction();
00941   HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
00942   SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
00943   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
00944   return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
00945                       Op.getOperand(1), MachinePointerInfo(SV), false,
00946                       false, 0);
00947 }
00948 
00949 SDValue
00950 HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
00951   EVT ValTy = Op.getValueType();
00952   SDLoc dl(Op);
00953   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
00954   SDValue Res;
00955   if (CP->isMachineConstantPoolEntry())
00956     Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
00957                                     CP->getAlignment());
00958   else
00959     Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
00960                                     CP->getAlignment());
00961   return DAG.getNode(HexagonISD::CONST32, dl, ValTy, Res);
00962 }
00963 
00964 SDValue
00965 HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
00966   const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
00967   MachineFunction &MF = DAG.getMachineFunction();
00968   MachineFrameInfo *MFI = MF.getFrameInfo();
00969   MFI->setReturnAddressIsTaken(true);
00970 
00971   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
00972     return SDValue();
00973 
00974   EVT VT = Op.getValueType();
00975   SDLoc dl(Op);
00976   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
00977   if (Depth) {
00978     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
00979     SDValue Offset = DAG.getConstant(4, MVT::i32);
00980     return DAG.getLoad(VT, dl, DAG.getEntryNode(),
00981                        DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
00982                        MachinePointerInfo(), false, false, false, 0);
00983   }
00984 
00985   // Return LR, which contains the return address. Mark it an implicit live-in.
00986   unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
00987   return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
00988 }
00989 
00990 SDValue
00991 HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
00992   const HexagonRegisterInfo *TRI = static_cast<const HexagonRegisterInfo *>(
00993       DAG.getSubtarget().getRegisterInfo());
00994   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
00995   MFI->setFrameAddressIsTaken(true);
00996 
00997   EVT VT = Op.getValueType();
00998   SDLoc dl(Op);
00999   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
01000   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
01001                                          TRI->getFrameRegister(), VT);
01002   while (Depth--)
01003     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
01004                             MachinePointerInfo(),
01005                             false, false, false, 0);
01006   return FrameAddr;
01007 }
01008 
01009 SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
01010                                                  SelectionDAG& DAG) const {
01011   SDLoc dl(Op);
01012   return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
01013 }
01014 
01015 
01016 SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
01017                                                   SelectionDAG &DAG) const {
01018   SDValue Result;
01019   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
01020   int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
01021   SDLoc dl(Op);
01022   Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
01023 
01024   const HexagonTargetObjectFile &TLOF =
01025       static_cast<const HexagonTargetObjectFile &>(getObjFileLowering());
01026   if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
01027     return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
01028   }
01029 
01030   return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
01031 }
01032 
01033 SDValue
01034 HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
01035   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
01036   SDValue BA_SD =  DAG.getTargetBlockAddress(BA, MVT::i32);
01037   SDLoc dl(Op);
01038   return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), BA_SD);
01039 }
01040 
01041 //===----------------------------------------------------------------------===//
01042 // TargetLowering Implementation
01043 //===----------------------------------------------------------------------===//
01044 
01045 HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &targetmachine)
01046     : TargetLowering(targetmachine, new HexagonTargetObjectFile()),
01047       TM(targetmachine) {
01048 
01049   const HexagonSubtarget &Subtarget = TM.getSubtarget<HexagonSubtarget>();
01050 
01051   // Set up the register classes.
01052   addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
01053   addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
01054 
01055   if (Subtarget.hasV5TOps()) {
01056     addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
01057     addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
01058   }
01059 
01060   addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
01061 
01062   computeRegisterProperties();
01063 
01064   // Align loop entry
01065   setPrefLoopAlignment(4);
01066 
01067   // Limits for inline expansion of memcpy/memmove
01068   MaxStoresPerMemcpy = 6;
01069   MaxStoresPerMemmove = 6;
01070 
01071   //
01072   // Library calls for unsupported operations
01073   //
01074 
01075   setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
01076   setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
01077 
01078   setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
01079   setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
01080 
01081   setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
01082   setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
01083 
01084   setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
01085   setOperationAction(ISD::SDIV, MVT::i32, Expand);
01086   setLibcallName(RTLIB::SREM_I32, "__hexagon_umodsi3");
01087   setOperationAction(ISD::SREM, MVT::i32, Expand);
01088 
01089   setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
01090   setOperationAction(ISD::SDIV, MVT::i64, Expand);
01091   setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
01092   setOperationAction(ISD::SREM, MVT::i64, Expand);
01093 
01094   setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
01095   setOperationAction(ISD::UDIV, MVT::i32, Expand);
01096 
01097   setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
01098   setOperationAction(ISD::UDIV, MVT::i64, Expand);
01099 
01100   setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
01101   setOperationAction(ISD::UREM, MVT::i32, Expand);
01102 
01103   setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
01104   setOperationAction(ISD::UREM, MVT::i64, Expand);
01105 
01106   setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
01107   setOperationAction(ISD::FDIV, MVT::f32, Expand);
01108 
01109   setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
01110   setOperationAction(ISD::FDIV, MVT::f64, Expand);
01111 
01112   setOperationAction(ISD::FSQRT, MVT::f32, Expand);
01113   setOperationAction(ISD::FSQRT, MVT::f64, Expand);
01114   setOperationAction(ISD::FSIN, MVT::f32, Expand);
01115   setOperationAction(ISD::FSIN, MVT::f64, Expand);
01116 
01117   if (Subtarget.hasV5TOps()) {
01118     // Hexagon V5 Support.
01119     setOperationAction(ISD::FADD, MVT::f32, Legal);
01120     setOperationAction(ISD::FADD, MVT::f64, Legal);
01121     setOperationAction(ISD::FP_EXTEND, MVT::f32, Legal);
01122     setCondCodeAction(ISD::SETOEQ, MVT::f32, Legal);
01123     setCondCodeAction(ISD::SETOEQ, MVT::f64, Legal);
01124     setCondCodeAction(ISD::SETUEQ, MVT::f32, Legal);
01125     setCondCodeAction(ISD::SETUEQ, MVT::f64, Legal);
01126 
01127     setCondCodeAction(ISD::SETOGE, MVT::f32, Legal);
01128     setCondCodeAction(ISD::SETOGE, MVT::f64, Legal);
01129     setCondCodeAction(ISD::SETUGE, MVT::f32, Legal);
01130     setCondCodeAction(ISD::SETUGE, MVT::f64, Legal);
01131 
01132     setCondCodeAction(ISD::SETOGT, MVT::f32, Legal);
01133     setCondCodeAction(ISD::SETOGT, MVT::f64, Legal);
01134     setCondCodeAction(ISD::SETUGT, MVT::f32, Legal);
01135     setCondCodeAction(ISD::SETUGT, MVT::f64, Legal);
01136 
01137     setCondCodeAction(ISD::SETOLE, MVT::f32, Legal);
01138     setCondCodeAction(ISD::SETOLE, MVT::f64, Legal);
01139     setCondCodeAction(ISD::SETOLT, MVT::f32, Legal);
01140     setCondCodeAction(ISD::SETOLT, MVT::f64, Legal);
01141 
01142     setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
01143     setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
01144 
01145     setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
01146     setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
01147     setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
01148     setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
01149 
01150     setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
01151     setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
01152     setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
01153     setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
01154 
01155     setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
01156     setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
01157     setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
01158     setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
01159 
01160     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
01161     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
01162     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
01163     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
01164 
01165     setOperationAction(ISD::FP_TO_UINT, MVT::i64, Legal);
01166     setOperationAction(ISD::FP_TO_SINT, MVT::i64, Legal);
01167     setOperationAction(ISD::UINT_TO_FP, MVT::i64, Legal);
01168     setOperationAction(ISD::SINT_TO_FP, MVT::i64, Legal);
01169 
01170     setOperationAction(ISD::FABS, MVT::f32, Legal);
01171     setOperationAction(ISD::FABS, MVT::f64, Expand);
01172 
01173     setOperationAction(ISD::FNEG, MVT::f32, Legal);
01174     setOperationAction(ISD::FNEG, MVT::f64, Expand);
01175   } else {
01176 
01177     // Expand fp<->uint.
01178     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Expand);
01179     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
01180 
01181     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
01182     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
01183 
01184     setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
01185     setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
01186 
01187     setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
01188     setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
01189 
01190     setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
01191     setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
01192 
01193     setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
01194     setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
01195 
01196     setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
01197     setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
01198 
01199     setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
01200     setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
01201 
01202     setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
01203     setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
01204 
01205     setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
01206     setOperationAction(ISD::FADD, MVT::f64, Expand);
01207 
01208     setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
01209     setOperationAction(ISD::FADD, MVT::f32, Expand);
01210 
01211     setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
01212     setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
01213 
01214     setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
01215     setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
01216 
01217     setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
01218     setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
01219 
01220     setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
01221     setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
01222 
01223     setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
01224     setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
01225 
01226     setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
01227     setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
01228 
01229     setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
01230     setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
01231 
01232     setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
01233     setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
01234 
01235     setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
01236     setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
01237 
01238     setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
01239     setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
01240 
01241     setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
01242     setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
01243 
01244     setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
01245     setCondCodeAction(ISD::SETOLT, MVT::f64, Expand);
01246 
01247     setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
01248     setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
01249 
01250     setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
01251     setOperationAction(ISD::FMUL, MVT::f64, Expand);
01252 
01253     setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
01254     setOperationAction(ISD::MUL, MVT::f32, Expand);
01255 
01256     setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
01257     setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
01258 
01259     setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
01260 
01261     setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
01262     setOperationAction(ISD::SUB, MVT::f64, Expand);
01263 
01264     setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
01265     setOperationAction(ISD::SUB, MVT::f32, Expand);
01266 
01267     setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
01268     setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
01269 
01270     setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
01271     setCondCodeAction(ISD::SETUO, MVT::f64, Expand);
01272 
01273     setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
01274     setCondCodeAction(ISD::SETO, MVT::f64, Expand);
01275 
01276     setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
01277     setCondCodeAction(ISD::SETO, MVT::f32, Expand);
01278 
01279     setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
01280     setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
01281 
01282     setOperationAction(ISD::FABS, MVT::f32, Expand);
01283     setOperationAction(ISD::FABS, MVT::f64, Expand);
01284     setOperationAction(ISD::FNEG, MVT::f32, Expand);
01285     setOperationAction(ISD::FNEG, MVT::f64, Expand);
01286   }
01287 
01288   setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
01289   setOperationAction(ISD::SREM, MVT::i32, Expand);
01290 
01291   setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
01292   setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
01293   setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
01294   setIndexedLoadAction(ISD::POST_INC, MVT::i64, Legal);
01295 
01296   setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal);
01297   setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal);
01298   setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
01299   setIndexedStoreAction(ISD::POST_INC, MVT::i64, Legal);
01300 
01301   setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
01302 
01303   // Turn FP extload into load/fextend.
01304   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
01305   // Hexagon has a i1 sign extending load.
01306   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Expand);
01307   // Turn FP truncstore into trunc + store.
01308   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
01309 
01310   // Custom legalize GlobalAddress nodes into CONST32.
01311   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
01312   setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
01313   setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
01314   // Truncate action?
01315   setOperationAction(ISD::TRUNCATE, MVT::i64, Expand);
01316 
01317   // Hexagon doesn't have sext_inreg, replace them with shl/sra.
01318   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
01319 
01320   // Hexagon has no REM or DIVREM operations.
01321   setOperationAction(ISD::UREM, MVT::i32, Expand);
01322   setOperationAction(ISD::SREM, MVT::i32, Expand);
01323   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
01324   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
01325   setOperationAction(ISD::SREM, MVT::i64, Expand);
01326   setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
01327   setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
01328 
01329   setOperationAction(ISD::BSWAP, MVT::i64, Expand);
01330 
01331   // Lower SELECT_CC to SETCC and SELECT.
01332   setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
01333   setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
01334   setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
01335 
01336   if (Subtarget.hasV5TOps()) {
01337 
01338     // We need to make the operation type of SELECT node to be Custom,
01339     // such that we don't go into the infinite loop of
01340     // select ->  setcc -> select_cc -> select loop.
01341     setOperationAction(ISD::SELECT, MVT::f32, Custom);
01342     setOperationAction(ISD::SELECT, MVT::f64, Custom);
01343 
01344     setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
01345     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
01346 
01347   } else {
01348 
01349     // Hexagon has no select or setcc: expand to SELECT_CC.
01350     setOperationAction(ISD::SELECT, MVT::f32, Expand);
01351     setOperationAction(ISD::SELECT, MVT::f64, Expand);
01352   }
01353 
01354   if (EmitJumpTables) {
01355     setOperationAction(ISD::BR_JT, MVT::Other, Custom);
01356   } else {
01357     setOperationAction(ISD::BR_JT, MVT::Other, Expand);
01358   }
01359   // Increase jump tables cutover to 5, was 4.
01360   setMinimumJumpTableEntries(5);
01361 
01362   setOperationAction(ISD::BR_CC, MVT::f32, Expand);
01363   setOperationAction(ISD::BR_CC, MVT::f64, Expand);
01364   setOperationAction(ISD::BR_CC, MVT::i1, Expand);
01365   setOperationAction(ISD::BR_CC, MVT::i32, Expand);
01366   setOperationAction(ISD::BR_CC, MVT::i64, Expand);
01367 
01368   setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
01369 
01370   setOperationAction(ISD::FSIN, MVT::f64, Expand);
01371   setOperationAction(ISD::FCOS, MVT::f64, Expand);
01372   setOperationAction(ISD::FREM, MVT::f64, Expand);
01373   setOperationAction(ISD::FSIN, MVT::f32, Expand);
01374   setOperationAction(ISD::FCOS, MVT::f32, Expand);
01375   setOperationAction(ISD::FREM, MVT::f32, Expand);
01376   setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
01377   setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
01378 
01379   // In V4, we have double word add/sub with carry. The problem with
01380   // modelling this instruction is that it produces 2 results - Rdd and Px.
01381   // To model update of Px, we will have to use Defs[p0..p3] which will
01382   // cause any predicate live range to spill. So, we pretend we dont't
01383   // have these instructions.
01384   setOperationAction(ISD::ADDE, MVT::i8, Expand);
01385   setOperationAction(ISD::ADDE, MVT::i16, Expand);
01386   setOperationAction(ISD::ADDE, MVT::i32, Expand);
01387   setOperationAction(ISD::ADDE, MVT::i64, Expand);
01388   setOperationAction(ISD::SUBE, MVT::i8, Expand);
01389   setOperationAction(ISD::SUBE, MVT::i16, Expand);
01390   setOperationAction(ISD::SUBE, MVT::i32, Expand);
01391   setOperationAction(ISD::SUBE, MVT::i64, Expand);
01392   setOperationAction(ISD::ADDC, MVT::i8, Expand);
01393   setOperationAction(ISD::ADDC, MVT::i16, Expand);
01394   setOperationAction(ISD::ADDC, MVT::i32, Expand);
01395   setOperationAction(ISD::ADDC, MVT::i64, Expand);
01396   setOperationAction(ISD::SUBC, MVT::i8, Expand);
01397   setOperationAction(ISD::SUBC, MVT::i16, Expand);
01398   setOperationAction(ISD::SUBC, MVT::i32, Expand);
01399   setOperationAction(ISD::SUBC, MVT::i64, Expand);
01400 
01401   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
01402   setOperationAction(ISD::CTPOP, MVT::i64, Expand);
01403   setOperationAction(ISD::CTTZ, MVT::i32, Expand);
01404   setOperationAction(ISD::CTTZ, MVT::i64, Expand);
01405   setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
01406   setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
01407   setOperationAction(ISD::CTLZ, MVT::i32, Expand);
01408   setOperationAction(ISD::CTLZ, MVT::i64, Expand);
01409   setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
01410   setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
01411   setOperationAction(ISD::ROTL, MVT::i32, Expand);
01412   setOperationAction(ISD::ROTR, MVT::i32, Expand);
01413   setOperationAction(ISD::BSWAP, MVT::i32, Expand);
01414   setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
01415   setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
01416   setOperationAction(ISD::FPOW, MVT::f64, Expand);
01417   setOperationAction(ISD::FPOW, MVT::f32, Expand);
01418 
01419   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
01420   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
01421   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
01422 
01423   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
01424   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
01425 
01426   setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
01427   setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
01428 
01429   setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
01430 
01431   if (Subtarget.isSubtargetV2()) {
01432     setExceptionPointerRegister(Hexagon::R20);
01433     setExceptionSelectorRegister(Hexagon::R21);
01434   } else {
01435     setExceptionPointerRegister(Hexagon::R0);
01436     setExceptionSelectorRegister(Hexagon::R1);
01437   }
01438 
01439   // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
01440   setOperationAction(ISD::VASTART, MVT::Other, Custom);
01441 
01442   // Use the default implementation.
01443   setOperationAction(ISD::VAARG, MVT::Other, Expand);
01444   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
01445   setOperationAction(ISD::VAEND, MVT::Other, Expand);
01446   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
01447   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
01448 
01449   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
01450   setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
01451 
01452   setMinFunctionAlignment(2);
01453 
01454   // Needed for DYNAMIC_STACKALLOC expansion.
01455   const HexagonRegisterInfo *QRI = static_cast<const HexagonRegisterInfo *>(
01456       TM.getSubtargetImpl()->getRegisterInfo());
01457   setStackPointerRegisterToSaveRestore(QRI->getStackRegister());
01458   setSchedulingPreference(Sched::VLIW);
01459 }
01460 
01461 const char*
01462 HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
01463   switch (Opcode) {
01464     default: return nullptr;
01465     case HexagonISD::CONST32:     return "HexagonISD::CONST32";
01466     case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
01467     case HexagonISD::CONST32_Int_Real: return "HexagonISD::CONST32_Int_Real";
01468     case HexagonISD::ADJDYNALLOC: return "HexagonISD::ADJDYNALLOC";
01469     case HexagonISD::CMPICC:      return "HexagonISD::CMPICC";
01470     case HexagonISD::CMPFCC:      return "HexagonISD::CMPFCC";
01471     case HexagonISD::BRICC:       return "HexagonISD::BRICC";
01472     case HexagonISD::BRFCC:       return "HexagonISD::BRFCC";
01473     case HexagonISD::SELECT_ICC:  return "HexagonISD::SELECT_ICC";
01474     case HexagonISD::SELECT_FCC:  return "HexagonISD::SELECT_FCC";
01475     case HexagonISD::Hi:          return "HexagonISD::Hi";
01476     case HexagonISD::Lo:          return "HexagonISD::Lo";
01477     case HexagonISD::FTOI:        return "HexagonISD::FTOI";
01478     case HexagonISD::ITOF:        return "HexagonISD::ITOF";
01479     case HexagonISD::CALL:        return "HexagonISD::CALL";
01480     case HexagonISD::RET_FLAG:    return "HexagonISD::RET_FLAG";
01481     case HexagonISD::BR_JT:       return "HexagonISD::BR_JT";
01482     case HexagonISD::TC_RETURN:   return "HexagonISD::TC_RETURN";
01483   case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
01484   }
01485 }
01486 
01487 bool
01488 HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
01489   EVT MTy1 = EVT::getEVT(Ty1);
01490   EVT MTy2 = EVT::getEVT(Ty2);
01491   if (!MTy1.isSimple() || !MTy2.isSimple()) {
01492     return false;
01493   }
01494   return ((MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32));
01495 }
01496 
01497 bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
01498   if (!VT1.isSimple() || !VT2.isSimple()) {
01499     return false;
01500   }
01501   return ((VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32));
01502 }
01503 
01504 bool
01505 HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
01506   // Assuming the caller does not have either a signext or zeroext modifier, and
01507   // only one value is accepted, any reasonable truncation is allowed.
01508   if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
01509     return false;
01510 
01511   // FIXME: in principle up to 64-bit could be made safe, but it would be very
01512   // fragile at the moment: any support for multiple value returns would be
01513   // liable to disallow tail calls involving i64 -> iN truncation in many cases.
01514   return Ty1->getPrimitiveSizeInBits() <= 32;
01515 }
01516 
01517 SDValue
01518 HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
01519   SDValue Chain     = Op.getOperand(0);
01520   SDValue Offset    = Op.getOperand(1);
01521   SDValue Handler   = Op.getOperand(2);
01522   SDLoc dl(Op);
01523 
01524   // Mark function as containing a call to EH_RETURN.
01525   HexagonMachineFunctionInfo *FuncInfo =
01526     DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
01527   FuncInfo->setHasEHReturn();
01528 
01529   unsigned OffsetReg = Hexagon::R28;
01530 
01531   SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(),
01532                                   DAG.getRegister(Hexagon::R30, getPointerTy()),
01533                                   DAG.getIntPtrConstant(4));
01534   Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
01535                        false, false, 0);
01536   Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
01537 
01538   // Not needed we already use it as explict input to EH_RETURN.
01539   // MF.getRegInfo().addLiveOut(OffsetReg);
01540 
01541   return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
01542 }
01543 
01544 SDValue
01545 HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
01546   switch (Op.getOpcode()) {
01547     default: llvm_unreachable("Should not custom lower this!");
01548     case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
01549     case ISD::EH_RETURN:          return LowerEH_RETURN(Op, DAG);
01550       // Frame & Return address.  Currently unimplemented.
01551     case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
01552     case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
01553     case ISD::GlobalTLSAddress:
01554                           llvm_unreachable("TLS not implemented for Hexagon.");
01555     case ISD::ATOMIC_FENCE:       return LowerATOMIC_FENCE(Op, DAG);
01556     case ISD::GlobalAddress:      return LowerGLOBALADDRESS(Op, DAG);
01557     case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
01558     case ISD::VASTART:            return LowerVASTART(Op, DAG);
01559     case ISD::BR_JT:              return LowerBR_JT(Op, DAG);
01560 
01561     case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
01562     case ISD::SELECT:             return Op;
01563     case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
01564     case ISD::INLINEASM:          return LowerINLINEASM(Op, DAG);
01565 
01566   }
01567 }
01568 
01569 
01570 
01571 //===----------------------------------------------------------------------===//
01572 //                           Hexagon Scheduler Hooks
01573 //===----------------------------------------------------------------------===//
01574 MachineBasicBlock *
01575 HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
01576                                                    MachineBasicBlock *BB)
01577 const {
01578   switch (MI->getOpcode()) {
01579     case Hexagon::ADJDYNALLOC: {
01580       MachineFunction *MF = BB->getParent();
01581       HexagonMachineFunctionInfo *FuncInfo =
01582         MF->getInfo<HexagonMachineFunctionInfo>();
01583       FuncInfo->addAllocaAdjustInst(MI);
01584       return BB;
01585     }
01586     default: llvm_unreachable("Unexpected instr type to insert");
01587   } // switch
01588 }
01589 
01590 //===----------------------------------------------------------------------===//
01591 // Inline Assembly Support
01592 //===----------------------------------------------------------------------===//
01593 
01594 std::pair<unsigned, const TargetRegisterClass*>
01595 HexagonTargetLowering::getRegForInlineAsmConstraint(const
01596                                                     std::string &Constraint,
01597                                                     MVT VT) const {
01598   if (Constraint.size() == 1) {
01599     switch (Constraint[0]) {
01600     case 'r':   // R0-R31
01601        switch (VT.SimpleTy) {
01602        default:
01603          llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
01604        case MVT::i32:
01605        case MVT::i16:
01606        case MVT::i8:
01607        case MVT::f32:
01608          return std::make_pair(0U, &Hexagon::IntRegsRegClass);
01609        case MVT::i64:
01610        case MVT::f64:
01611          return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
01612       }
01613     default:
01614       llvm_unreachable("Unknown asm register class");
01615     }
01616   }
01617 
01618   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
01619 }
01620 
01621 /// isFPImmLegal - Returns true if the target can instruction select the
01622 /// specified FP immediate natively. If false, the legalizer will
01623 /// materialize the FP immediate as a load from a constant pool.
01624 bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
01625   return TM.getSubtarget<HexagonSubtarget>().hasV5TOps();
01626 }
01627 
01628 /// isLegalAddressingMode - Return true if the addressing mode represented by
01629 /// AM is legal for this target, for a load/store of the specified type.
01630 bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
01631                                                   Type *Ty) const {
01632   // Allows a signed-extended 11-bit immediate field.
01633   if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1) {
01634     return false;
01635   }
01636 
01637   // No global is ever allowed as a base.
01638   if (AM.BaseGV) {
01639     return false;
01640   }
01641 
01642   int Scale = AM.Scale;
01643   if (Scale < 0) Scale = -Scale;
01644   switch (Scale) {
01645   case 0:  // No scale reg, "r+i", "r", or just "i".
01646     break;
01647   default: // No scaled addressing mode.
01648     return false;
01649   }
01650   return true;
01651 }
01652 
01653 /// isLegalICmpImmediate - Return true if the specified immediate is legal
01654 /// icmp immediate, that is the target has icmp instructions which can compare
01655 /// a register against the immediate without having to materialize the
01656 /// immediate into a register.
01657 bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
01658   return Imm >= -512 && Imm <= 511;
01659 }
01660 
01661 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
01662 /// for tail call optimization. Targets which want to do tail call
01663 /// optimization should implement this function.
01664 bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
01665                                  SDValue Callee,
01666                                  CallingConv::ID CalleeCC,
01667                                  bool isVarArg,
01668                                  bool isCalleeStructRet,
01669                                  bool isCallerStructRet,
01670                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
01671                                  const SmallVectorImpl<SDValue> &OutVals,
01672                                  const SmallVectorImpl<ISD::InputArg> &Ins,
01673                                  SelectionDAG& DAG) const {
01674   const Function *CallerF = DAG.getMachineFunction().getFunction();
01675   CallingConv::ID CallerCC = CallerF->getCallingConv();
01676   bool CCMatch = CallerCC == CalleeCC;
01677 
01678   // ***************************************************************************
01679   //  Look for obvious safe cases to perform tail call optimization that do not
01680   //  require ABI changes.
01681   // ***************************************************************************
01682 
01683   // If this is a tail call via a function pointer, then don't do it!
01684   if (!(dyn_cast<GlobalAddressSDNode>(Callee))
01685       && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
01686     return false;
01687   }
01688 
01689   // Do not optimize if the calling conventions do not match.
01690   if (!CCMatch)
01691     return false;
01692 
01693   // Do not tail call optimize vararg calls.
01694   if (isVarArg)
01695     return false;
01696 
01697   // Also avoid tail call optimization if either caller or callee uses struct
01698   // return semantics.
01699   if (isCalleeStructRet || isCallerStructRet)
01700     return false;
01701 
01702   // In addition to the cases above, we also disable Tail Call Optimization if
01703   // the calling convention code that at least one outgoing argument needs to
01704   // go on the stack. We cannot check that here because at this point that
01705   // information is not available.
01706   return true;
01707 }