LLVM API Documentation

XCoreISelLowering.cpp
Go to the documentation of this file.
00001 //===-- XCoreISelLowering.cpp - XCore 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 XCoreTargetLowering class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "XCoreISelLowering.h"
00015 #include "XCore.h"
00016 #include "XCoreMachineFunctionInfo.h"
00017 #include "XCoreSubtarget.h"
00018 #include "XCoreTargetMachine.h"
00019 #include "XCoreTargetObjectFile.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/Constants.h"
00030 #include "llvm/IR/DerivedTypes.h"
00031 #include "llvm/IR/Function.h"
00032 #include "llvm/IR/GlobalAlias.h"
00033 #include "llvm/IR/GlobalVariable.h"
00034 #include "llvm/IR/Intrinsics.h"
00035 #include "llvm/Support/Debug.h"
00036 #include "llvm/Support/ErrorHandling.h"
00037 #include "llvm/Support/raw_ostream.h"
00038 #include <algorithm>
00039 
00040 using namespace llvm;
00041 
00042 #define DEBUG_TYPE "xcore-lower"
00043 
00044 const char *XCoreTargetLowering::
00045 getTargetNodeName(unsigned Opcode) const
00046 {
00047   switch (Opcode)
00048   {
00049     case XCoreISD::BL                : return "XCoreISD::BL";
00050     case XCoreISD::PCRelativeWrapper : return "XCoreISD::PCRelativeWrapper";
00051     case XCoreISD::DPRelativeWrapper : return "XCoreISD::DPRelativeWrapper";
00052     case XCoreISD::CPRelativeWrapper : return "XCoreISD::CPRelativeWrapper";
00053     case XCoreISD::LDWSP             : return "XCoreISD::LDWSP";
00054     case XCoreISD::STWSP             : return "XCoreISD::STWSP";
00055     case XCoreISD::RETSP             : return "XCoreISD::RETSP";
00056     case XCoreISD::LADD              : return "XCoreISD::LADD";
00057     case XCoreISD::LSUB              : return "XCoreISD::LSUB";
00058     case XCoreISD::LMUL              : return "XCoreISD::LMUL";
00059     case XCoreISD::MACCU             : return "XCoreISD::MACCU";
00060     case XCoreISD::MACCS             : return "XCoreISD::MACCS";
00061     case XCoreISD::CRC8              : return "XCoreISD::CRC8";
00062     case XCoreISD::BR_JT             : return "XCoreISD::BR_JT";
00063     case XCoreISD::BR_JT32           : return "XCoreISD::BR_JT32";
00064     case XCoreISD::FRAME_TO_ARGS_OFFSET : return "XCoreISD::FRAME_TO_ARGS_OFFSET";
00065     case XCoreISD::EH_RETURN         : return "XCoreISD::EH_RETURN";
00066     case XCoreISD::MEMBARRIER        : return "XCoreISD::MEMBARRIER";
00067     default                          : return nullptr;
00068   }
00069 }
00070 
00071 XCoreTargetLowering::XCoreTargetLowering(const TargetMachine &TM)
00072     : TargetLowering(TM, new XCoreTargetObjectFile()), TM(TM),
00073       Subtarget(TM.getSubtarget<XCoreSubtarget>()) {
00074 
00075   // Set up the register classes.
00076   addRegisterClass(MVT::i32, &XCore::GRRegsRegClass);
00077 
00078   // Compute derived properties from the register classes
00079   computeRegisterProperties();
00080 
00081   // Division is expensive
00082   setIntDivIsCheap(false);
00083 
00084   setStackPointerRegisterToSaveRestore(XCore::SP);
00085 
00086   setSchedulingPreference(Sched::Source);
00087 
00088   // Use i32 for setcc operations results (slt, sgt, ...).
00089   setBooleanContents(ZeroOrOneBooleanContent);
00090   setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
00091 
00092   // XCore does not have the NodeTypes below.
00093   setOperationAction(ISD::BR_CC,     MVT::i32,   Expand);
00094   setOperationAction(ISD::SELECT_CC, MVT::i32,   Expand);
00095   setOperationAction(ISD::ADDC, MVT::i32, Expand);
00096   setOperationAction(ISD::ADDE, MVT::i32, Expand);
00097   setOperationAction(ISD::SUBC, MVT::i32, Expand);
00098   setOperationAction(ISD::SUBE, MVT::i32, Expand);
00099 
00100   // 64bit
00101   setOperationAction(ISD::ADD, MVT::i64, Custom);
00102   setOperationAction(ISD::SUB, MVT::i64, Custom);
00103   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
00104   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
00105   setOperationAction(ISD::MULHS, MVT::i32, Expand);
00106   setOperationAction(ISD::MULHU, MVT::i32, Expand);
00107   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
00108   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
00109   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
00110 
00111   // Bit Manipulation
00112   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
00113   setOperationAction(ISD::ROTL , MVT::i32, Expand);
00114   setOperationAction(ISD::ROTR , MVT::i32, Expand);
00115   setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
00116   setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
00117 
00118   setOperationAction(ISD::TRAP, MVT::Other, Legal);
00119 
00120   // Jump tables.
00121   setOperationAction(ISD::BR_JT, MVT::Other, Custom);
00122 
00123   setOperationAction(ISD::GlobalAddress, MVT::i32,   Custom);
00124   setOperationAction(ISD::BlockAddress, MVT::i32 , Custom);
00125 
00126   // Conversion of i64 -> double produces constantpool nodes
00127   setOperationAction(ISD::ConstantPool, MVT::i32,   Custom);
00128 
00129   // Loads
00130   setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
00131   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
00132   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
00133 
00134   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
00135   setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
00136 
00137   // Custom expand misaligned loads / stores.
00138   setOperationAction(ISD::LOAD, MVT::i32, Custom);
00139   setOperationAction(ISD::STORE, MVT::i32, Custom);
00140 
00141   // Varargs
00142   setOperationAction(ISD::VAEND, MVT::Other, Expand);
00143   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
00144   setOperationAction(ISD::VAARG, MVT::Other, Custom);
00145   setOperationAction(ISD::VASTART, MVT::Other, Custom);
00146 
00147   // Dynamic stack
00148   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
00149   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
00150   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
00151 
00152   // Exception handling
00153   setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
00154   setExceptionPointerRegister(XCore::R0);
00155   setExceptionSelectorRegister(XCore::R1);
00156   setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
00157 
00158   // Atomic operations
00159   // We request a fence for ATOMIC_* instructions, to reduce them to Monotonic.
00160   // As we are always Sequential Consistent, an ATOMIC_FENCE becomes a no OP.
00161   setInsertFencesForAtomic(true);
00162   setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
00163   setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
00164   setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
00165 
00166   // TRAMPOLINE is custom lowered.
00167   setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
00168   setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
00169 
00170   // We want to custom lower some of our intrinsics.
00171   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
00172 
00173   MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 4;
00174   MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize
00175     = MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 2;
00176 
00177   // We have target-specific dag combine patterns for the following nodes:
00178   setTargetDAGCombine(ISD::STORE);
00179   setTargetDAGCombine(ISD::ADD);
00180   setTargetDAGCombine(ISD::INTRINSIC_VOID);
00181   setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
00182 
00183   setMinFunctionAlignment(1);
00184   setPrefFunctionAlignment(2);
00185 }
00186 
00187 bool XCoreTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
00188   if (Val.getOpcode() != ISD::LOAD)
00189     return false;
00190 
00191   EVT VT1 = Val.getValueType();
00192   if (!VT1.isSimple() || !VT1.isInteger() ||
00193       !VT2.isSimple() || !VT2.isInteger())
00194     return false;
00195 
00196   switch (VT1.getSimpleVT().SimpleTy) {
00197   default: break;
00198   case MVT::i8:
00199     return true;
00200   }
00201 
00202   return false;
00203 }
00204 
00205 SDValue XCoreTargetLowering::
00206 LowerOperation(SDValue Op, SelectionDAG &DAG) const {
00207   switch (Op.getOpcode())
00208   {
00209   case ISD::EH_RETURN:          return LowerEH_RETURN(Op, DAG);
00210   case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
00211   case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
00212   case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
00213   case ISD::BR_JT:              return LowerBR_JT(Op, DAG);
00214   case ISD::LOAD:               return LowerLOAD(Op, DAG);
00215   case ISD::STORE:              return LowerSTORE(Op, DAG);
00216   case ISD::VAARG:              return LowerVAARG(Op, DAG);
00217   case ISD::VASTART:            return LowerVASTART(Op, DAG);
00218   case ISD::SMUL_LOHI:          return LowerSMUL_LOHI(Op, DAG);
00219   case ISD::UMUL_LOHI:          return LowerUMUL_LOHI(Op, DAG);
00220   // FIXME: Remove these when LegalizeDAGTypes lands.
00221   case ISD::ADD:
00222   case ISD::SUB:                return ExpandADDSUB(Op.getNode(), DAG);
00223   case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
00224   case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
00225   case ISD::FRAME_TO_ARGS_OFFSET: return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
00226   case ISD::INIT_TRAMPOLINE:    return LowerINIT_TRAMPOLINE(Op, DAG);
00227   case ISD::ADJUST_TRAMPOLINE:  return LowerADJUST_TRAMPOLINE(Op, DAG);
00228   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
00229   case ISD::ATOMIC_FENCE:       return LowerATOMIC_FENCE(Op, DAG);
00230   case ISD::ATOMIC_LOAD:        return LowerATOMIC_LOAD(Op, DAG);
00231   case ISD::ATOMIC_STORE:       return LowerATOMIC_STORE(Op, DAG);
00232   default:
00233     llvm_unreachable("unimplemented operand");
00234   }
00235 }
00236 
00237 /// ReplaceNodeResults - Replace the results of node with an illegal result
00238 /// type with new values built out of custom code.
00239 void XCoreTargetLowering::ReplaceNodeResults(SDNode *N,
00240                                              SmallVectorImpl<SDValue>&Results,
00241                                              SelectionDAG &DAG) const {
00242   switch (N->getOpcode()) {
00243   default:
00244     llvm_unreachable("Don't know how to custom expand this!");
00245   case ISD::ADD:
00246   case ISD::SUB:
00247     Results.push_back(ExpandADDSUB(N, DAG));
00248     return;
00249   }
00250 }
00251 
00252 //===----------------------------------------------------------------------===//
00253 //  Misc Lower Operation implementation
00254 //===----------------------------------------------------------------------===//
00255 
00256 SDValue XCoreTargetLowering::getGlobalAddressWrapper(SDValue GA,
00257                                                      const GlobalValue *GV,
00258                                                      SelectionDAG &DAG) const {
00259   // FIXME there is no actual debug info here
00260   SDLoc dl(GA);
00261 
00262   if (GV->getType()->getElementType()->isFunctionTy())
00263     return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA);
00264 
00265   const auto *GVar = dyn_cast<GlobalVariable>(GV);
00266   if ((GV->hasSection() && StringRef(GV->getSection()).startswith(".cp.")) ||
00267       (GVar && GVar->isConstant() && GV->hasLocalLinkage()))
00268     return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA);
00269 
00270   return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA);
00271 }
00272 
00273 static bool IsSmallObject(const GlobalValue *GV, const XCoreTargetLowering &XTL) {
00274   if (XTL.getTargetMachine().getCodeModel() == CodeModel::Small)
00275     return true;
00276 
00277   Type *ObjType = GV->getType()->getPointerElementType();
00278   if (!ObjType->isSized())
00279     return false;
00280 
00281   unsigned ObjSize = XTL.getDataLayout()->getTypeAllocSize(ObjType);
00282   return ObjSize < CodeModelLargeSize && ObjSize != 0;
00283 }
00284 
00285 SDValue XCoreTargetLowering::
00286 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const
00287 {
00288   const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
00289   const GlobalValue *GV = GN->getGlobal();
00290   SDLoc DL(GN);
00291   int64_t Offset = GN->getOffset();
00292   if (IsSmallObject(GV, *this)) {
00293     // We can only fold positive offsets that are a multiple of the word size.
00294     int64_t FoldedOffset = std::max(Offset & ~3, (int64_t)0);
00295     SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, FoldedOffset);
00296     GA = getGlobalAddressWrapper(GA, GV, DAG);
00297     // Handle the rest of the offset.
00298     if (Offset != FoldedOffset) {
00299       SDValue Remaining = DAG.getConstant(Offset - FoldedOffset, MVT::i32);
00300       GA = DAG.getNode(ISD::ADD, DL, MVT::i32, GA, Remaining);
00301     }
00302     return GA;
00303   } else {
00304     // Ideally we would not fold in offset with an index <= 11.
00305     Type *Ty = Type::getInt8PtrTy(*DAG.getContext());
00306     Constant *GA = ConstantExpr::getBitCast(const_cast<GlobalValue*>(GV), Ty);
00307     Ty = Type::getInt32Ty(*DAG.getContext());
00308     Constant *Idx = ConstantInt::get(Ty, Offset);
00309     Constant *GAI = ConstantExpr::getGetElementPtr(GA, Idx);
00310     SDValue CP = DAG.getConstantPool(GAI, MVT::i32);
00311     return DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), CP,
00312                        MachinePointerInfo(), false, false, false, 0);
00313   }
00314 }
00315 
00316 SDValue XCoreTargetLowering::
00317 LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const
00318 {
00319   SDLoc DL(Op);
00320 
00321   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
00322   SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
00323 
00324   return DAG.getNode(XCoreISD::PCRelativeWrapper, DL, getPointerTy(), Result);
00325 }
00326 
00327 SDValue XCoreTargetLowering::
00328 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
00329 {
00330   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
00331   // FIXME there isn't really debug info here
00332   SDLoc dl(CP);
00333   EVT PtrVT = Op.getValueType();
00334   SDValue Res;
00335   if (CP->isMachineConstantPoolEntry()) {
00336     Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
00337                                     CP->getAlignment(), CP->getOffset());
00338   } else {
00339     Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
00340                                     CP->getAlignment(), CP->getOffset());
00341   }
00342   return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, Res);
00343 }
00344 
00345 unsigned XCoreTargetLowering::getJumpTableEncoding() const {
00346   return MachineJumpTableInfo::EK_Inline;
00347 }
00348 
00349 SDValue XCoreTargetLowering::
00350 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
00351 {
00352   SDValue Chain = Op.getOperand(0);
00353   SDValue Table = Op.getOperand(1);
00354   SDValue Index = Op.getOperand(2);
00355   SDLoc dl(Op);
00356   JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
00357   unsigned JTI = JT->getIndex();
00358   MachineFunction &MF = DAG.getMachineFunction();
00359   const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
00360   SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
00361 
00362   unsigned NumEntries = MJTI->getJumpTables()[JTI].MBBs.size();
00363   if (NumEntries <= 32) {
00364     return DAG.getNode(XCoreISD::BR_JT, dl, MVT::Other, Chain, TargetJT, Index);
00365   }
00366   assert((NumEntries >> 31) == 0);
00367   SDValue ScaledIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
00368                                     DAG.getConstant(1, MVT::i32));
00369   return DAG.getNode(XCoreISD::BR_JT32, dl, MVT::Other, Chain, TargetJT,
00370                      ScaledIndex);
00371 }
00372 
00373 SDValue XCoreTargetLowering::
00374 lowerLoadWordFromAlignedBasePlusOffset(SDLoc DL, SDValue Chain, SDValue Base,
00375                                        int64_t Offset, SelectionDAG &DAG) const
00376 {
00377   if ((Offset & 0x3) == 0) {
00378     return DAG.getLoad(getPointerTy(), DL, Chain, Base, MachinePointerInfo(),
00379                        false, false, false, 0);
00380   }
00381   // Lower to pair of consecutive word aligned loads plus some bit shifting.
00382   int32_t HighOffset = RoundUpToAlignment(Offset, 4);
00383   int32_t LowOffset = HighOffset - 4;
00384   SDValue LowAddr, HighAddr;
00385   if (GlobalAddressSDNode *GASD =
00386         dyn_cast<GlobalAddressSDNode>(Base.getNode())) {
00387     LowAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
00388                                    LowOffset);
00389     HighAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
00390                                     HighOffset);
00391   } else {
00392     LowAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
00393                           DAG.getConstant(LowOffset, MVT::i32));
00394     HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
00395                            DAG.getConstant(HighOffset, MVT::i32));
00396   }
00397   SDValue LowShift = DAG.getConstant((Offset - LowOffset) * 8, MVT::i32);
00398   SDValue HighShift = DAG.getConstant((HighOffset - Offset) * 8, MVT::i32);
00399 
00400   SDValue Low = DAG.getLoad(getPointerTy(), DL, Chain,
00401                             LowAddr, MachinePointerInfo(),
00402                             false, false, false, 0);
00403   SDValue High = DAG.getLoad(getPointerTy(), DL, Chain,
00404                              HighAddr, MachinePointerInfo(),
00405                              false, false, false, 0);
00406   SDValue LowShifted = DAG.getNode(ISD::SRL, DL, MVT::i32, Low, LowShift);
00407   SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High, HighShift);
00408   SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, LowShifted, HighShifted);
00409   Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
00410                       High.getValue(1));
00411   SDValue Ops[] = { Result, Chain };
00412   return DAG.getMergeValues(Ops, DL);
00413 }
00414 
00415 static bool isWordAligned(SDValue Value, SelectionDAG &DAG)
00416 {
00417   APInt KnownZero, KnownOne;
00418   DAG.computeKnownBits(Value, KnownZero, KnownOne);
00419   return KnownZero.countTrailingOnes() >= 2;
00420 }
00421 
00422 SDValue XCoreTargetLowering::
00423 LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
00424   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
00425   LoadSDNode *LD = cast<LoadSDNode>(Op);
00426   assert(LD->getExtensionType() == ISD::NON_EXTLOAD &&
00427          "Unexpected extension type");
00428   assert(LD->getMemoryVT() == MVT::i32 && "Unexpected load EVT");
00429   if (allowsMisalignedMemoryAccesses(LD->getMemoryVT(),
00430                                      LD->getAddressSpace(),
00431                                      LD->getAlignment()))
00432     return SDValue();
00433 
00434   unsigned ABIAlignment = getDataLayout()->
00435     getABITypeAlignment(LD->getMemoryVT().getTypeForEVT(*DAG.getContext()));
00436   // Leave aligned load alone.
00437   if (LD->getAlignment() >= ABIAlignment)
00438     return SDValue();
00439 
00440   SDValue Chain = LD->getChain();
00441   SDValue BasePtr = LD->getBasePtr();
00442   SDLoc DL(Op);
00443 
00444   if (!LD->isVolatile()) {
00445     const GlobalValue *GV;
00446     int64_t Offset = 0;
00447     if (DAG.isBaseWithConstantOffset(BasePtr) &&
00448         isWordAligned(BasePtr->getOperand(0), DAG)) {
00449       SDValue NewBasePtr = BasePtr->getOperand(0);
00450       Offset = cast<ConstantSDNode>(BasePtr->getOperand(1))->getSExtValue();
00451       return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
00452                                                     Offset, DAG);
00453     }
00454     if (TLI.isGAPlusOffset(BasePtr.getNode(), GV, Offset) &&
00455         MinAlign(GV->getAlignment(), 4) == 4) {
00456       SDValue NewBasePtr = DAG.getGlobalAddress(GV, DL,
00457                                                 BasePtr->getValueType(0));
00458       return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
00459                                                     Offset, DAG);
00460     }
00461   }
00462 
00463   if (LD->getAlignment() == 2) {
00464     SDValue Low = DAG.getExtLoad(ISD::ZEXTLOAD, DL, MVT::i32, Chain,
00465                                  BasePtr, LD->getPointerInfo(), MVT::i16,
00466                                  LD->isVolatile(), LD->isNonTemporal(),
00467                                  LD->isInvariant(), 2);
00468     SDValue HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
00469                                    DAG.getConstant(2, MVT::i32));
00470     SDValue High = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
00471                                   HighAddr,
00472                                   LD->getPointerInfo().getWithOffset(2),
00473                                   MVT::i16, LD->isVolatile(),
00474                                   LD->isNonTemporal(), LD->isInvariant(), 2);
00475     SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High,
00476                                       DAG.getConstant(16, MVT::i32));
00477     SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Low, HighShifted);
00478     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
00479                              High.getValue(1));
00480     SDValue Ops[] = { Result, Chain };
00481     return DAG.getMergeValues(Ops, DL);
00482   }
00483 
00484   // Lower to a call to __misaligned_load(BasePtr).
00485   Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
00486   TargetLowering::ArgListTy Args;
00487   TargetLowering::ArgListEntry Entry;
00488 
00489   Entry.Ty = IntPtrTy;
00490   Entry.Node = BasePtr;
00491   Args.push_back(Entry);
00492 
00493   TargetLowering::CallLoweringInfo CLI(DAG);
00494   CLI.setDebugLoc(DL).setChain(Chain)
00495     .setCallee(CallingConv::C, IntPtrTy,
00496                DAG.getExternalSymbol("__misaligned_load", getPointerTy()),
00497                std::move(Args), 0);
00498 
00499   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
00500   SDValue Ops[] = { CallResult.first, CallResult.second };
00501   return DAG.getMergeValues(Ops, DL);
00502 }
00503 
00504 SDValue XCoreTargetLowering::
00505 LowerSTORE(SDValue Op, SelectionDAG &DAG) const
00506 {
00507   StoreSDNode *ST = cast<StoreSDNode>(Op);
00508   assert(!ST->isTruncatingStore() && "Unexpected store type");
00509   assert(ST->getMemoryVT() == MVT::i32 && "Unexpected store EVT");
00510   if (allowsMisalignedMemoryAccesses(ST->getMemoryVT(),
00511                                      ST->getAddressSpace(),
00512                                      ST->getAlignment())) {
00513     return SDValue();
00514   }
00515   unsigned ABIAlignment = getDataLayout()->
00516     getABITypeAlignment(ST->getMemoryVT().getTypeForEVT(*DAG.getContext()));
00517   // Leave aligned store alone.
00518   if (ST->getAlignment() >= ABIAlignment) {
00519     return SDValue();
00520   }
00521   SDValue Chain = ST->getChain();
00522   SDValue BasePtr = ST->getBasePtr();
00523   SDValue Value = ST->getValue();
00524   SDLoc dl(Op);
00525 
00526   if (ST->getAlignment() == 2) {
00527     SDValue Low = Value;
00528     SDValue High = DAG.getNode(ISD::SRL, dl, MVT::i32, Value,
00529                                       DAG.getConstant(16, MVT::i32));
00530     SDValue StoreLow = DAG.getTruncStore(Chain, dl, Low, BasePtr,
00531                                          ST->getPointerInfo(), MVT::i16,
00532                                          ST->isVolatile(), ST->isNonTemporal(),
00533                                          2);
00534     SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr,
00535                                    DAG.getConstant(2, MVT::i32));
00536     SDValue StoreHigh = DAG.getTruncStore(Chain, dl, High, HighAddr,
00537                                           ST->getPointerInfo().getWithOffset(2),
00538                                           MVT::i16, ST->isVolatile(),
00539                                           ST->isNonTemporal(), 2);
00540     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, StoreLow, StoreHigh);
00541   }
00542 
00543   // Lower to a call to __misaligned_store(BasePtr, Value).
00544   Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
00545   TargetLowering::ArgListTy Args;
00546   TargetLowering::ArgListEntry Entry;
00547 
00548   Entry.Ty = IntPtrTy;
00549   Entry.Node = BasePtr;
00550   Args.push_back(Entry);
00551 
00552   Entry.Node = Value;
00553   Args.push_back(Entry);
00554 
00555   TargetLowering::CallLoweringInfo CLI(DAG);
00556   CLI.setDebugLoc(dl).setChain(Chain)
00557     .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
00558                DAG.getExternalSymbol("__misaligned_store", getPointerTy()),
00559                std::move(Args), 0);
00560 
00561   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
00562   return CallResult.second;
00563 }
00564 
00565 SDValue XCoreTargetLowering::
00566 LowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
00567 {
00568   assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::SMUL_LOHI &&
00569          "Unexpected operand to lower!");
00570   SDLoc dl(Op);
00571   SDValue LHS = Op.getOperand(0);
00572   SDValue RHS = Op.getOperand(1);
00573   SDValue Zero = DAG.getConstant(0, MVT::i32);
00574   SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
00575                            DAG.getVTList(MVT::i32, MVT::i32), Zero, Zero,
00576                            LHS, RHS);
00577   SDValue Lo(Hi.getNode(), 1);
00578   SDValue Ops[] = { Lo, Hi };
00579   return DAG.getMergeValues(Ops, dl);
00580 }
00581 
00582 SDValue XCoreTargetLowering::
00583 LowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
00584 {
00585   assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::UMUL_LOHI &&
00586          "Unexpected operand to lower!");
00587   SDLoc dl(Op);
00588   SDValue LHS = Op.getOperand(0);
00589   SDValue RHS = Op.getOperand(1);
00590   SDValue Zero = DAG.getConstant(0, MVT::i32);
00591   SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
00592                            DAG.getVTList(MVT::i32, MVT::i32), LHS, RHS,
00593                            Zero, Zero);
00594   SDValue Lo(Hi.getNode(), 1);
00595   SDValue Ops[] = { Lo, Hi };
00596   return DAG.getMergeValues(Ops, dl);
00597 }
00598 
00599 /// isADDADDMUL - Return whether Op is in a form that is equivalent to
00600 /// add(add(mul(x,y),a),b). If requireIntermediatesHaveOneUse is true then
00601 /// each intermediate result in the calculation must also have a single use.
00602 /// If the Op is in the correct form the constituent parts are written to Mul0,
00603 /// Mul1, Addend0 and Addend1.
00604 static bool
00605 isADDADDMUL(SDValue Op, SDValue &Mul0, SDValue &Mul1, SDValue &Addend0,
00606             SDValue &Addend1, bool requireIntermediatesHaveOneUse)
00607 {
00608   if (Op.getOpcode() != ISD::ADD)
00609     return false;
00610   SDValue N0 = Op.getOperand(0);
00611   SDValue N1 = Op.getOperand(1);
00612   SDValue AddOp;
00613   SDValue OtherOp;
00614   if (N0.getOpcode() == ISD::ADD) {
00615     AddOp = N0;
00616     OtherOp = N1;
00617   } else if (N1.getOpcode() == ISD::ADD) {
00618     AddOp = N1;
00619     OtherOp = N0;
00620   } else {
00621     return false;
00622   }
00623   if (requireIntermediatesHaveOneUse && !AddOp.hasOneUse())
00624     return false;
00625   if (OtherOp.getOpcode() == ISD::MUL) {
00626     // add(add(a,b),mul(x,y))
00627     if (requireIntermediatesHaveOneUse && !OtherOp.hasOneUse())
00628       return false;
00629     Mul0 = OtherOp.getOperand(0);
00630     Mul1 = OtherOp.getOperand(1);
00631     Addend0 = AddOp.getOperand(0);
00632     Addend1 = AddOp.getOperand(1);
00633     return true;
00634   }
00635   if (AddOp.getOperand(0).getOpcode() == ISD::MUL) {
00636     // add(add(mul(x,y),a),b)
00637     if (requireIntermediatesHaveOneUse && !AddOp.getOperand(0).hasOneUse())
00638       return false;
00639     Mul0 = AddOp.getOperand(0).getOperand(0);
00640     Mul1 = AddOp.getOperand(0).getOperand(1);
00641     Addend0 = AddOp.getOperand(1);
00642     Addend1 = OtherOp;
00643     return true;
00644   }
00645   if (AddOp.getOperand(1).getOpcode() == ISD::MUL) {
00646     // add(add(a,mul(x,y)),b)
00647     if (requireIntermediatesHaveOneUse && !AddOp.getOperand(1).hasOneUse())
00648       return false;
00649     Mul0 = AddOp.getOperand(1).getOperand(0);
00650     Mul1 = AddOp.getOperand(1).getOperand(1);
00651     Addend0 = AddOp.getOperand(0);
00652     Addend1 = OtherOp;
00653     return true;
00654   }
00655   return false;
00656 }
00657 
00658 SDValue XCoreTargetLowering::
00659 TryExpandADDWithMul(SDNode *N, SelectionDAG &DAG) const
00660 {
00661   SDValue Mul;
00662   SDValue Other;
00663   if (N->getOperand(0).getOpcode() == ISD::MUL) {
00664     Mul = N->getOperand(0);
00665     Other = N->getOperand(1);
00666   } else if (N->getOperand(1).getOpcode() == ISD::MUL) {
00667     Mul = N->getOperand(1);
00668     Other = N->getOperand(0);
00669   } else {
00670     return SDValue();
00671   }
00672   SDLoc dl(N);
00673   SDValue LL, RL, AddendL, AddendH;
00674   LL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00675                    Mul.getOperand(0),  DAG.getConstant(0, MVT::i32));
00676   RL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00677                    Mul.getOperand(1),  DAG.getConstant(0, MVT::i32));
00678   AddendL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00679                         Other,  DAG.getConstant(0, MVT::i32));
00680   AddendH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00681                         Other,  DAG.getConstant(1, MVT::i32));
00682   APInt HighMask = APInt::getHighBitsSet(64, 32);
00683   unsigned LHSSB = DAG.ComputeNumSignBits(Mul.getOperand(0));
00684   unsigned RHSSB = DAG.ComputeNumSignBits(Mul.getOperand(1));
00685   if (DAG.MaskedValueIsZero(Mul.getOperand(0), HighMask) &&
00686       DAG.MaskedValueIsZero(Mul.getOperand(1), HighMask)) {
00687     // The inputs are both zero-extended.
00688     SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
00689                              DAG.getVTList(MVT::i32, MVT::i32), AddendH,
00690                              AddendL, LL, RL);
00691     SDValue Lo(Hi.getNode(), 1);
00692     return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
00693   }
00694   if (LHSSB > 32 && RHSSB > 32) {
00695     // The inputs are both sign-extended.
00696     SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
00697                              DAG.getVTList(MVT::i32, MVT::i32), AddendH,
00698                              AddendL, LL, RL);
00699     SDValue Lo(Hi.getNode(), 1);
00700     return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
00701   }
00702   SDValue LH, RH;
00703   LH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00704                    Mul.getOperand(0),  DAG.getConstant(1, MVT::i32));
00705   RH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00706                    Mul.getOperand(1),  DAG.getConstant(1, MVT::i32));
00707   SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
00708                            DAG.getVTList(MVT::i32, MVT::i32), AddendH,
00709                            AddendL, LL, RL);
00710   SDValue Lo(Hi.getNode(), 1);
00711   RH = DAG.getNode(ISD::MUL, dl, MVT::i32, LL, RH);
00712   LH = DAG.getNode(ISD::MUL, dl, MVT::i32, LH, RL);
00713   Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, RH);
00714   Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, LH);
00715   return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
00716 }
00717 
00718 SDValue XCoreTargetLowering::
00719 ExpandADDSUB(SDNode *N, SelectionDAG &DAG) const
00720 {
00721   assert(N->getValueType(0) == MVT::i64 &&
00722          (N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&
00723         "Unknown operand to lower!");
00724 
00725   if (N->getOpcode() == ISD::ADD) {
00726     SDValue Result = TryExpandADDWithMul(N, DAG);
00727     if (Result.getNode())
00728       return Result;
00729   }
00730 
00731   SDLoc dl(N);
00732 
00733   // Extract components
00734   SDValue LHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00735                             N->getOperand(0),  DAG.getConstant(0, MVT::i32));
00736   SDValue LHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00737                             N->getOperand(0),  DAG.getConstant(1, MVT::i32));
00738   SDValue RHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00739                              N->getOperand(1), DAG.getConstant(0, MVT::i32));
00740   SDValue RHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
00741                              N->getOperand(1), DAG.getConstant(1, MVT::i32));
00742 
00743   // Expand
00744   unsigned Opcode = (N->getOpcode() == ISD::ADD) ? XCoreISD::LADD :
00745                                                    XCoreISD::LSUB;
00746   SDValue Zero = DAG.getConstant(0, MVT::i32);
00747   SDValue Lo = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
00748                            LHSL, RHSL, Zero);
00749   SDValue Carry(Lo.getNode(), 1);
00750 
00751   SDValue Hi = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
00752                            LHSH, RHSH, Carry);
00753   SDValue Ignored(Hi.getNode(), 1);
00754   // Merge the pieces
00755   return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
00756 }
00757 
00758 SDValue XCoreTargetLowering::
00759 LowerVAARG(SDValue Op, SelectionDAG &DAG) const
00760 {
00761   // Whist llvm does not support aggregate varargs we can ignore
00762   // the possibility of the ValueType being an implicit byVal vararg.
00763   SDNode *Node = Op.getNode();
00764   EVT VT = Node->getValueType(0); // not an aggregate
00765   SDValue InChain = Node->getOperand(0);
00766   SDValue VAListPtr = Node->getOperand(1);
00767   EVT PtrVT = VAListPtr.getValueType();
00768   const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
00769   SDLoc dl(Node);
00770   SDValue VAList = DAG.getLoad(PtrVT, dl, InChain,
00771                                VAListPtr, MachinePointerInfo(SV),
00772                                false, false, false, 0);
00773   // Increment the pointer, VAList, to the next vararg
00774   SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAList,
00775                                 DAG.getIntPtrConstant(VT.getSizeInBits() / 8));
00776   // Store the incremented VAList to the legalized pointer
00777   InChain = DAG.getStore(VAList.getValue(1), dl, nextPtr, VAListPtr,
00778                          MachinePointerInfo(SV), false, false, 0);
00779   // Load the actual argument out of the pointer VAList
00780   return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
00781                      false, false, false, 0);
00782 }
00783 
00784 SDValue XCoreTargetLowering::
00785 LowerVASTART(SDValue Op, SelectionDAG &DAG) const
00786 {
00787   SDLoc dl(Op);
00788   // vastart stores the address of the VarArgsFrameIndex slot into the
00789   // memory location argument
00790   MachineFunction &MF = DAG.getMachineFunction();
00791   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
00792   SDValue Addr = DAG.getFrameIndex(XFI->getVarArgsFrameIndex(), MVT::i32);
00793   return DAG.getStore(Op.getOperand(0), dl, Addr, Op.getOperand(1),
00794                       MachinePointerInfo(), false, false, 0);
00795 }
00796 
00797 SDValue XCoreTargetLowering::LowerFRAMEADDR(SDValue Op,
00798                                             SelectionDAG &DAG) const {
00799   // This nodes represent llvm.frameaddress on the DAG.
00800   // It takes one operand, the index of the frame address to return.
00801   // An index of zero corresponds to the current function's frame address.
00802   // An index of one to the parent's frame address, and so on.
00803   // Depths > 0 not supported yet!
00804   if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
00805     return SDValue();
00806 
00807   MachineFunction &MF = DAG.getMachineFunction();
00808   const TargetRegisterInfo *RegInfo =
00809       getTargetMachine().getSubtargetImpl()->getRegisterInfo();
00810   return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op),
00811                             RegInfo->getFrameRegister(MF), MVT::i32);
00812 }
00813 
00814 SDValue XCoreTargetLowering::
00815 LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
00816   // This nodes represent llvm.returnaddress on the DAG.
00817   // It takes one operand, the index of the return address to return.
00818   // An index of zero corresponds to the current function's return address.
00819   // An index of one to the parent's return address, and so on.
00820   // Depths > 0 not supported yet!
00821   if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
00822     return SDValue();
00823 
00824   MachineFunction &MF = DAG.getMachineFunction();
00825   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
00826   int FI = XFI->createLRSpillSlot(MF);
00827   SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
00828   return DAG.getLoad(getPointerTy(), SDLoc(Op), DAG.getEntryNode(), FIN,
00829                      MachinePointerInfo::getFixedStack(FI), false, false,
00830                      false, 0);
00831 }
00832 
00833 SDValue XCoreTargetLowering::
00834 LowerFRAME_TO_ARGS_OFFSET(SDValue Op, SelectionDAG &DAG) const {
00835   // This node represents offset from frame pointer to first on-stack argument.
00836   // This is needed for correct stack adjustment during unwind.
00837   // However, we don't know the offset until after the frame has be finalised.
00838   // This is done during the XCoreFTAOElim pass.
00839   return DAG.getNode(XCoreISD::FRAME_TO_ARGS_OFFSET, SDLoc(Op), MVT::i32);
00840 }
00841 
00842 SDValue XCoreTargetLowering::
00843 LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
00844   // OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER)
00845   // This node represents 'eh_return' gcc dwarf builtin, which is used to
00846   // return from exception. The general meaning is: adjust stack by OFFSET and
00847   // pass execution to HANDLER.
00848   MachineFunction &MF = DAG.getMachineFunction();
00849   SDValue Chain     = Op.getOperand(0);
00850   SDValue Offset    = Op.getOperand(1);
00851   SDValue Handler   = Op.getOperand(2);
00852   SDLoc dl(Op);
00853 
00854   // Absolute SP = (FP + FrameToArgs) + Offset
00855   const TargetRegisterInfo *RegInfo =
00856       getTargetMachine().getSubtargetImpl()->getRegisterInfo();
00857   SDValue Stack = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
00858                             RegInfo->getFrameRegister(MF), MVT::i32);
00859   SDValue FrameToArgs = DAG.getNode(XCoreISD::FRAME_TO_ARGS_OFFSET, dl,
00860                                     MVT::i32);
00861   Stack = DAG.getNode(ISD::ADD, dl, MVT::i32, Stack, FrameToArgs);
00862   Stack = DAG.getNode(ISD::ADD, dl, MVT::i32, Stack, Offset);
00863 
00864   // R0=ExceptionPointerRegister R1=ExceptionSelectorRegister
00865   // which leaves 2 caller saved registers, R2 & R3 for us to use.
00866   unsigned StackReg = XCore::R2;
00867   unsigned HandlerReg = XCore::R3;
00868 
00869   SDValue OutChains[] = {
00870     DAG.getCopyToReg(Chain, dl, StackReg, Stack),
00871     DAG.getCopyToReg(Chain, dl, HandlerReg, Handler)
00872   };
00873 
00874   Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
00875 
00876   return DAG.getNode(XCoreISD::EH_RETURN, dl, MVT::Other, Chain,
00877                      DAG.getRegister(StackReg, MVT::i32),
00878                      DAG.getRegister(HandlerReg, MVT::i32));
00879 
00880 }
00881 
00882 SDValue XCoreTargetLowering::
00883 LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
00884   return Op.getOperand(0);
00885 }
00886 
00887 SDValue XCoreTargetLowering::
00888 LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
00889   SDValue Chain = Op.getOperand(0);
00890   SDValue Trmp = Op.getOperand(1); // trampoline
00891   SDValue FPtr = Op.getOperand(2); // nested function
00892   SDValue Nest = Op.getOperand(3); // 'nest' parameter value
00893 
00894   const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
00895 
00896   // .align 4
00897   // LDAPF_u10 r11, nest
00898   // LDW_2rus r11, r11[0]
00899   // STWSP_ru6 r11, sp[0]
00900   // LDAPF_u10 r11, fptr
00901   // LDW_2rus r11, r11[0]
00902   // BAU_1r r11
00903   // nest:
00904   // .word nest
00905   // fptr:
00906   // .word fptr
00907   SDValue OutChains[5];
00908 
00909   SDValue Addr = Trmp;
00910 
00911   SDLoc dl(Op);
00912   OutChains[0] = DAG.getStore(Chain, dl, DAG.getConstant(0x0a3cd805, MVT::i32),
00913                               Addr, MachinePointerInfo(TrmpAddr), false, false,
00914                               0);
00915 
00916   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
00917                      DAG.getConstant(4, MVT::i32));
00918   OutChains[1] = DAG.getStore(Chain, dl, DAG.getConstant(0xd80456c0, MVT::i32),
00919                               Addr, MachinePointerInfo(TrmpAddr, 4), false,
00920                               false, 0);
00921 
00922   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
00923                      DAG.getConstant(8, MVT::i32));
00924   OutChains[2] = DAG.getStore(Chain, dl, DAG.getConstant(0x27fb0a3c, MVT::i32),
00925                               Addr, MachinePointerInfo(TrmpAddr, 8), false,
00926                               false, 0);
00927 
00928   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
00929                      DAG.getConstant(12, MVT::i32));
00930   OutChains[3] = DAG.getStore(Chain, dl, Nest, Addr,
00931                               MachinePointerInfo(TrmpAddr, 12), false, false,
00932                               0);
00933 
00934   Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
00935                      DAG.getConstant(16, MVT::i32));
00936   OutChains[4] = DAG.getStore(Chain, dl, FPtr, Addr,
00937                               MachinePointerInfo(TrmpAddr, 16), false, false,
00938                               0);
00939 
00940   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
00941 }
00942 
00943 SDValue XCoreTargetLowering::
00944 LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
00945   SDLoc DL(Op);
00946   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
00947   switch (IntNo) {
00948     case Intrinsic::xcore_crc8:
00949       EVT VT = Op.getValueType();
00950       SDValue Data =
00951         DAG.getNode(XCoreISD::CRC8, DL, DAG.getVTList(VT, VT),
00952                     Op.getOperand(1), Op.getOperand(2) , Op.getOperand(3));
00953       SDValue Crc(Data.getNode(), 1);
00954       SDValue Results[] = { Crc, Data };
00955       return DAG.getMergeValues(Results, DL);
00956   }
00957   return SDValue();
00958 }
00959 
00960 SDValue XCoreTargetLowering::
00961 LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG) const {
00962   SDLoc DL(Op);
00963   return DAG.getNode(XCoreISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
00964 }
00965 
00966 SDValue XCoreTargetLowering::
00967 LowerATOMIC_LOAD(SDValue Op, SelectionDAG &DAG) const {
00968   AtomicSDNode *N = cast<AtomicSDNode>(Op);
00969   assert(N->getOpcode() == ISD::ATOMIC_LOAD && "Bad Atomic OP");
00970   assert(N->getOrdering() <= Monotonic &&
00971          "setInsertFencesForAtomic(true) and yet greater than Monotonic");
00972   if (N->getMemoryVT() == MVT::i32) {
00973     if (N->getAlignment() < 4)
00974       report_fatal_error("atomic load must be aligned");
00975     return DAG.getLoad(getPointerTy(), SDLoc(Op), N->getChain(),
00976                        N->getBasePtr(), N->getPointerInfo(),
00977                        N->isVolatile(), N->isNonTemporal(),
00978                        N->isInvariant(), N->getAlignment(),
00979                        N->getAAInfo(), N->getRanges());
00980   }
00981   if (N->getMemoryVT() == MVT::i16) {
00982     if (N->getAlignment() < 2)
00983       report_fatal_error("atomic load must be aligned");
00984     return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), MVT::i32, N->getChain(),
00985                           N->getBasePtr(), N->getPointerInfo(), MVT::i16,
00986                           N->isVolatile(), N->isNonTemporal(),
00987                           N->isInvariant(), N->getAlignment(), N->getAAInfo());
00988   }
00989   if (N->getMemoryVT() == MVT::i8)
00990     return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), MVT::i32, N->getChain(),
00991                           N->getBasePtr(), N->getPointerInfo(), MVT::i8,
00992                           N->isVolatile(), N->isNonTemporal(),
00993                           N->isInvariant(), N->getAlignment(), N->getAAInfo());
00994   return SDValue();
00995 }
00996 
00997 SDValue XCoreTargetLowering::
00998 LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) const {
00999   AtomicSDNode *N = cast<AtomicSDNode>(Op);
01000   assert(N->getOpcode() == ISD::ATOMIC_STORE && "Bad Atomic OP");
01001   assert(N->getOrdering() <= Monotonic &&
01002          "setInsertFencesForAtomic(true) and yet greater than Monotonic");
01003   if (N->getMemoryVT() == MVT::i32) {
01004     if (N->getAlignment() < 4)
01005       report_fatal_error("atomic store must be aligned");
01006     return DAG.getStore(N->getChain(), SDLoc(Op), N->getVal(),
01007                         N->getBasePtr(), N->getPointerInfo(),
01008                         N->isVolatile(), N->isNonTemporal(),
01009                         N->getAlignment(), N->getAAInfo());
01010   }
01011   if (N->getMemoryVT() == MVT::i16) {
01012     if (N->getAlignment() < 2)
01013       report_fatal_error("atomic store must be aligned");
01014     return DAG.getTruncStore(N->getChain(), SDLoc(Op), N->getVal(),
01015                              N->getBasePtr(), N->getPointerInfo(), MVT::i16,
01016                              N->isVolatile(), N->isNonTemporal(),
01017                              N->getAlignment(), N->getAAInfo());
01018   }
01019   if (N->getMemoryVT() == MVT::i8)
01020     return DAG.getTruncStore(N->getChain(), SDLoc(Op), N->getVal(),
01021                              N->getBasePtr(), N->getPointerInfo(), MVT::i8,
01022                              N->isVolatile(), N->isNonTemporal(),
01023                              N->getAlignment(), N->getAAInfo());
01024   return SDValue();
01025 }
01026 
01027 //===----------------------------------------------------------------------===//
01028 //                      Calling Convention Implementation
01029 //===----------------------------------------------------------------------===//
01030 
01031 #include "XCoreGenCallingConv.inc"
01032 
01033 //===----------------------------------------------------------------------===//
01034 //                  Call Calling Convention Implementation
01035 //===----------------------------------------------------------------------===//
01036 
01037 /// XCore call implementation
01038 SDValue
01039 XCoreTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
01040                                SmallVectorImpl<SDValue> &InVals) const {
01041   SelectionDAG &DAG                     = CLI.DAG;
01042   SDLoc &dl                             = CLI.DL;
01043   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
01044   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
01045   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
01046   SDValue Chain                         = CLI.Chain;
01047   SDValue Callee                        = CLI.Callee;
01048   bool &isTailCall                      = CLI.IsTailCall;
01049   CallingConv::ID CallConv              = CLI.CallConv;
01050   bool isVarArg                         = CLI.IsVarArg;
01051 
01052   // XCore target does not yet support tail call optimization.
01053   isTailCall = false;
01054 
01055   // For now, only CallingConv::C implemented
01056   switch (CallConv)
01057   {
01058     default:
01059       llvm_unreachable("Unsupported calling convention");
01060     case CallingConv::Fast:
01061     case CallingConv::C:
01062       return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
01063                             Outs, OutVals, Ins, dl, DAG, InVals);
01064   }
01065 }
01066 
01067 /// LowerCallResult - Lower the result values of a call into the
01068 /// appropriate copies out of appropriate physical registers / memory locations.
01069 static SDValue
01070 LowerCallResult(SDValue Chain, SDValue InFlag,
01071                 const SmallVectorImpl<CCValAssign> &RVLocs,
01072                 SDLoc dl, SelectionDAG &DAG,
01073                 SmallVectorImpl<SDValue> &InVals) {
01074   SmallVector<std::pair<int, unsigned>, 4> ResultMemLocs;
01075   // Copy results out of physical registers.
01076   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
01077     const CCValAssign &VA = RVLocs[i];
01078     if (VA.isRegLoc()) {
01079       Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getValVT(),
01080                                  InFlag).getValue(1);
01081       InFlag = Chain.getValue(2);
01082       InVals.push_back(Chain.getValue(0));
01083     } else {
01084       assert(VA.isMemLoc());
01085       ResultMemLocs.push_back(std::make_pair(VA.getLocMemOffset(),
01086                                              InVals.size()));
01087       // Reserve space for this result.
01088       InVals.push_back(SDValue());
01089     }
01090   }
01091 
01092   // Copy results out of memory.
01093   SmallVector<SDValue, 4> MemOpChains;
01094   for (unsigned i = 0, e = ResultMemLocs.size(); i != e; ++i) {
01095     int offset = ResultMemLocs[i].first;
01096     unsigned index = ResultMemLocs[i].second;
01097     SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
01098     SDValue Ops[] = { Chain, DAG.getConstant(offset / 4, MVT::i32) };
01099     SDValue load = DAG.getNode(XCoreISD::LDWSP, dl, VTs, Ops);
01100     InVals[index] = load;
01101     MemOpChains.push_back(load.getValue(1));
01102   }
01103 
01104   // Transform all loads nodes into one single node because
01105   // all load nodes are independent of each other.
01106   if (!MemOpChains.empty())
01107     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
01108 
01109   return Chain;
01110 }
01111 
01112 /// LowerCCCCallTo - functions arguments are copied from virtual
01113 /// regs to (physical regs)/(stack frame), CALLSEQ_START and
01114 /// CALLSEQ_END are emitted.
01115 /// TODO: isTailCall, sret.
01116 SDValue
01117 XCoreTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
01118                                     CallingConv::ID CallConv, bool isVarArg,
01119                                     bool isTailCall,
01120                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
01121                                     const SmallVectorImpl<SDValue> &OutVals,
01122                                     const SmallVectorImpl<ISD::InputArg> &Ins,
01123                                     SDLoc dl, SelectionDAG &DAG,
01124                                     SmallVectorImpl<SDValue> &InVals) const {
01125 
01126   // Analyze operands of the call, assigning locations to each operand.
01127   SmallVector<CCValAssign, 16> ArgLocs;
01128   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
01129                  *DAG.getContext());
01130 
01131   // The ABI dictates there should be one stack slot available to the callee
01132   // on function entry (for saving lr).
01133   CCInfo.AllocateStack(4, 4);
01134 
01135   CCInfo.AnalyzeCallOperands(Outs, CC_XCore);
01136 
01137   SmallVector<CCValAssign, 16> RVLocs;
01138   // Analyze return values to determine the number of bytes of stack required.
01139   CCState RetCCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
01140                     *DAG.getContext());
01141   RetCCInfo.AllocateStack(CCInfo.getNextStackOffset(), 4);
01142   RetCCInfo.AnalyzeCallResult(Ins, RetCC_XCore);
01143 
01144   // Get a count of how many bytes are to be pushed on the stack.
01145   unsigned NumBytes = RetCCInfo.getNextStackOffset();
01146 
01147   Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
01148                                  getPointerTy(), true), dl);
01149 
01150   SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
01151   SmallVector<SDValue, 12> MemOpChains;
01152 
01153   // Walk the register/memloc assignments, inserting copies/loads.
01154   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
01155     CCValAssign &VA = ArgLocs[i];
01156     SDValue Arg = OutVals[i];
01157 
01158     // Promote the value if needed.
01159     switch (VA.getLocInfo()) {
01160       default: llvm_unreachable("Unknown loc info!");
01161       case CCValAssign::Full: break;
01162       case CCValAssign::SExt:
01163         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
01164         break;
01165       case CCValAssign::ZExt:
01166         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
01167         break;
01168       case CCValAssign::AExt:
01169         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
01170         break;
01171     }
01172 
01173     // Arguments that can be passed on register must be kept at
01174     // RegsToPass vector
01175     if (VA.isRegLoc()) {
01176       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
01177     } else {
01178       assert(VA.isMemLoc());
01179 
01180       int Offset = VA.getLocMemOffset();
01181 
01182       MemOpChains.push_back(DAG.getNode(XCoreISD::STWSP, dl, MVT::Other,
01183                                         Chain, Arg,
01184                                         DAG.getConstant(Offset/4, MVT::i32)));
01185     }
01186   }
01187 
01188   // Transform all store nodes into one single node because
01189   // all store nodes are independent of each other.
01190   if (!MemOpChains.empty())
01191     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
01192 
01193   // Build a sequence of copy-to-reg nodes chained together with token
01194   // chain and flag operands which copy the outgoing args into registers.
01195   // The InFlag in necessary since all emitted instructions must be
01196   // stuck together.
01197   SDValue InFlag;
01198   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
01199     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
01200                              RegsToPass[i].second, InFlag);
01201     InFlag = Chain.getValue(1);
01202   }
01203 
01204   // If the callee is a GlobalAddress node (quite common, every direct call is)
01205   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
01206   // Likewise ExternalSymbol -> TargetExternalSymbol.
01207   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
01208     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
01209   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
01210     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
01211 
01212   // XCoreBranchLink = #chain, #target_address, #opt_in_flags...
01213   //             = Chain, Callee, Reg#1, Reg#2, ...
01214   //
01215   // Returns a chain & a flag for retval copy to use.
01216   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
01217   SmallVector<SDValue, 8> Ops;
01218   Ops.push_back(Chain);
01219   Ops.push_back(Callee);
01220 
01221   // Add argument registers to the end of the list so that they are
01222   // known live into the call.
01223   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
01224     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
01225                                   RegsToPass[i].second.getValueType()));
01226 
01227   if (InFlag.getNode())
01228     Ops.push_back(InFlag);
01229 
01230   Chain  = DAG.getNode(XCoreISD::BL, dl, NodeTys, Ops);
01231   InFlag = Chain.getValue(1);
01232 
01233   // Create the CALLSEQ_END node.
01234   Chain = DAG.getCALLSEQ_END(Chain,
01235                              DAG.getConstant(NumBytes, getPointerTy(), true),
01236                              DAG.getConstant(0, getPointerTy(), true),
01237                              InFlag, dl);
01238   InFlag = Chain.getValue(1);
01239 
01240   // Handle result values, copying them out of physregs into vregs that we
01241   // return.
01242   return LowerCallResult(Chain, InFlag, RVLocs, dl, DAG, InVals);
01243 }
01244 
01245 //===----------------------------------------------------------------------===//
01246 //             Formal Arguments Calling Convention Implementation
01247 //===----------------------------------------------------------------------===//
01248 
01249 namespace {
01250   struct ArgDataPair { SDValue SDV; ISD::ArgFlagsTy Flags; };
01251 }
01252 
01253 /// XCore formal arguments implementation
01254 SDValue
01255 XCoreTargetLowering::LowerFormalArguments(SDValue Chain,
01256                                           CallingConv::ID CallConv,
01257                                           bool isVarArg,
01258                                       const SmallVectorImpl<ISD::InputArg> &Ins,
01259                                           SDLoc dl,
01260                                           SelectionDAG &DAG,
01261                                           SmallVectorImpl<SDValue> &InVals)
01262                                             const {
01263   switch (CallConv)
01264   {
01265     default:
01266       llvm_unreachable("Unsupported calling convention");
01267     case CallingConv::C:
01268     case CallingConv::Fast:
01269       return LowerCCCArguments(Chain, CallConv, isVarArg,
01270                                Ins, dl, DAG, InVals);
01271   }
01272 }
01273 
01274 /// LowerCCCArguments - transform physical registers into
01275 /// virtual registers and generate load operations for
01276 /// arguments places on the stack.
01277 /// TODO: sret
01278 SDValue
01279 XCoreTargetLowering::LowerCCCArguments(SDValue Chain,
01280                                        CallingConv::ID CallConv,
01281                                        bool isVarArg,
01282                                        const SmallVectorImpl<ISD::InputArg>
01283                                          &Ins,
01284                                        SDLoc dl,
01285                                        SelectionDAG &DAG,
01286                                        SmallVectorImpl<SDValue> &InVals) const {
01287   MachineFunction &MF = DAG.getMachineFunction();
01288   MachineFrameInfo *MFI = MF.getFrameInfo();
01289   MachineRegisterInfo &RegInfo = MF.getRegInfo();
01290   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
01291 
01292   // Assign locations to all of the incoming arguments.
01293   SmallVector<CCValAssign, 16> ArgLocs;
01294   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
01295                  *DAG.getContext());
01296 
01297   CCInfo.AnalyzeFormalArguments(Ins, CC_XCore);
01298 
01299   unsigned StackSlotSize = XCoreFrameLowering::stackSlotSize();
01300 
01301   unsigned LRSaveSize = StackSlotSize;
01302 
01303   if (!isVarArg)
01304     XFI->setReturnStackOffset(CCInfo.getNextStackOffset() + LRSaveSize);
01305 
01306   // All getCopyFromReg ops must precede any getMemcpys to prevent the
01307   // scheduler clobbering a register before it has been copied.
01308   // The stages are:
01309   // 1. CopyFromReg (and load) arg & vararg registers.
01310   // 2. Chain CopyFromReg nodes into a TokenFactor.
01311   // 3. Memcpy 'byVal' args & push final InVals.
01312   // 4. Chain mem ops nodes into a TokenFactor.
01313   SmallVector<SDValue, 4> CFRegNode;
01314   SmallVector<ArgDataPair, 4> ArgData;
01315   SmallVector<SDValue, 4> MemOps;
01316 
01317   // 1a. CopyFromReg (and load) arg registers.
01318   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
01319 
01320     CCValAssign &VA = ArgLocs[i];
01321     SDValue ArgIn;
01322 
01323     if (VA.isRegLoc()) {
01324       // Arguments passed in registers
01325       EVT RegVT = VA.getLocVT();
01326       switch (RegVT.getSimpleVT().SimpleTy) {
01327       default:
01328         {
01329 #ifndef NDEBUG
01330           errs() << "LowerFormalArguments Unhandled argument type: "
01331                  << RegVT.getSimpleVT().SimpleTy << "\n";
01332 #endif
01333           llvm_unreachable(nullptr);
01334         }
01335       case MVT::i32:
01336         unsigned VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
01337         RegInfo.addLiveIn(VA.getLocReg(), VReg);
01338         ArgIn = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
01339         CFRegNode.push_back(ArgIn.getValue(ArgIn->getNumValues() - 1));
01340       }
01341     } else {
01342       // sanity check
01343       assert(VA.isMemLoc());
01344       // Load the argument to a virtual register
01345       unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
01346       if (ObjSize > StackSlotSize) {
01347         errs() << "LowerFormalArguments Unhandled argument type: "
01348                << EVT(VA.getLocVT()).getEVTString()
01349                << "\n";
01350       }
01351       // Create the frame index object for this incoming parameter...
01352       int FI = MFI->CreateFixedObject(ObjSize,
01353                                       LRSaveSize + VA.getLocMemOffset(),
01354                                       true);
01355 
01356       // Create the SelectionDAG nodes corresponding to a load
01357       //from this parameter
01358       SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
01359       ArgIn = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
01360                           MachinePointerInfo::getFixedStack(FI),
01361                           false, false, false, 0);
01362     }
01363     const ArgDataPair ADP = { ArgIn, Ins[i].Flags };
01364     ArgData.push_back(ADP);
01365   }
01366 
01367   // 1b. CopyFromReg vararg registers.
01368   if (isVarArg) {
01369     // Argument registers
01370     static const MCPhysReg ArgRegs[] = {
01371       XCore::R0, XCore::R1, XCore::R2, XCore::R3
01372     };
01373     XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
01374     unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs,
01375                                                      array_lengthof(ArgRegs));
01376     if (FirstVAReg < array_lengthof(ArgRegs)) {
01377       int offset = 0;
01378       // Save remaining registers, storing higher register numbers at a higher
01379       // address
01380       for (int i = array_lengthof(ArgRegs) - 1; i >= (int)FirstVAReg; --i) {
01381         // Create a stack slot
01382         int FI = MFI->CreateFixedObject(4, offset, true);
01383         if (i == (int)FirstVAReg) {
01384           XFI->setVarArgsFrameIndex(FI);
01385         }
01386         offset -= StackSlotSize;
01387         SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
01388         // Move argument from phys reg -> virt reg
01389         unsigned VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
01390         RegInfo.addLiveIn(ArgRegs[i], VReg);
01391         SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
01392         CFRegNode.push_back(Val.getValue(Val->getNumValues() - 1));
01393         // Move argument from virt reg -> stack
01394         SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
01395                                      MachinePointerInfo(), false, false, 0);
01396         MemOps.push_back(Store);
01397       }
01398     } else {
01399       // This will point to the next argument passed via stack.
01400       XFI->setVarArgsFrameIndex(
01401         MFI->CreateFixedObject(4, LRSaveSize + CCInfo.getNextStackOffset(),
01402                                true));
01403     }
01404   }
01405 
01406   // 2. chain CopyFromReg nodes into a TokenFactor.
01407   if (!CFRegNode.empty())
01408     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, CFRegNode);
01409 
01410   // 3. Memcpy 'byVal' args & push final InVals.
01411   // Aggregates passed "byVal" need to be copied by the callee.
01412   // The callee will use a pointer to this copy, rather than the original
01413   // pointer.
01414   for (SmallVectorImpl<ArgDataPair>::const_iterator ArgDI = ArgData.begin(),
01415                                                     ArgDE = ArgData.end();
01416        ArgDI != ArgDE; ++ArgDI) {
01417     if (ArgDI->Flags.isByVal() && ArgDI->Flags.getByValSize()) {
01418       unsigned Size = ArgDI->Flags.getByValSize();
01419       unsigned Align = std::max(StackSlotSize, ArgDI->Flags.getByValAlign());
01420       // Create a new object on the stack and copy the pointee into it.
01421       int FI = MFI->CreateStackObject(Size, Align, false);
01422       SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
01423       InVals.push_back(FIN);
01424       MemOps.push_back(DAG.getMemcpy(Chain, dl, FIN, ArgDI->SDV,
01425                                      DAG.getConstant(Size, MVT::i32),
01426                                      Align, false, false,
01427                                      MachinePointerInfo(),
01428                                      MachinePointerInfo()));
01429     } else {
01430       InVals.push_back(ArgDI->SDV);
01431     }
01432   }
01433 
01434   // 4, chain mem ops nodes into a TokenFactor.
01435   if (!MemOps.empty()) {
01436     MemOps.push_back(Chain);
01437     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
01438   }
01439 
01440   return Chain;
01441 }
01442 
01443 //===----------------------------------------------------------------------===//
01444 //               Return Value Calling Convention Implementation
01445 //===----------------------------------------------------------------------===//
01446 
01447 bool XCoreTargetLowering::
01448 CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
01449                bool isVarArg,
01450                const SmallVectorImpl<ISD::OutputArg> &Outs,
01451                LLVMContext &Context) const {
01452   SmallVector<CCValAssign, 16> RVLocs;
01453   CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
01454   if (!CCInfo.CheckReturn(Outs, RetCC_XCore))
01455     return false;
01456   if (CCInfo.getNextStackOffset() != 0 && isVarArg)
01457     return false;
01458   return true;
01459 }
01460 
01461 SDValue
01462 XCoreTargetLowering::LowerReturn(SDValue Chain,
01463                                  CallingConv::ID CallConv, bool isVarArg,
01464                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
01465                                  const SmallVectorImpl<SDValue> &OutVals,
01466                                  SDLoc dl, SelectionDAG &DAG) const {
01467 
01468   XCoreFunctionInfo *XFI =
01469     DAG.getMachineFunction().getInfo<XCoreFunctionInfo>();
01470   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
01471 
01472   // CCValAssign - represent the assignment of
01473   // the return value to a location
01474   SmallVector<CCValAssign, 16> RVLocs;
01475 
01476   // CCState - Info about the registers and stack slot.
01477   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
01478                  *DAG.getContext());
01479 
01480   // Analyze return values.
01481   if (!isVarArg)
01482     CCInfo.AllocateStack(XFI->getReturnStackOffset(), 4);
01483 
01484   CCInfo.AnalyzeReturn(Outs, RetCC_XCore);
01485 
01486   SDValue Flag;
01487   SmallVector<SDValue, 4> RetOps(1, Chain);
01488 
01489   // Return on XCore is always a "retsp 0"
01490   RetOps.push_back(DAG.getConstant(0, MVT::i32));
01491 
01492   SmallVector<SDValue, 4> MemOpChains;
01493   // Handle return values that must be copied to memory.
01494   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
01495     CCValAssign &VA = RVLocs[i];
01496     if (VA.isRegLoc())
01497       continue;
01498     assert(VA.isMemLoc());
01499     if (isVarArg) {
01500       report_fatal_error("Can't return value from vararg function in memory");
01501     }
01502 
01503     int Offset = VA.getLocMemOffset();
01504     unsigned ObjSize = VA.getLocVT().getSizeInBits() / 8;
01505     // Create the frame index object for the memory location.
01506     int FI = MFI->CreateFixedObject(ObjSize, Offset, false);
01507 
01508     // Create a SelectionDAG node corresponding to a store
01509     // to this memory location.
01510     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
01511     MemOpChains.push_back(DAG.getStore(Chain, dl, OutVals[i], FIN,
01512                           MachinePointerInfo::getFixedStack(FI), false, false,
01513                           0));
01514   }
01515 
01516   // Transform all store nodes into one single node because
01517   // all stores are independent of each other.
01518   if (!MemOpChains.empty())
01519     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
01520 
01521   // Now handle return values copied to registers.
01522   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
01523     CCValAssign &VA = RVLocs[i];
01524     if (!VA.isRegLoc())
01525       continue;
01526     // Copy the result values into the output registers.
01527     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
01528 
01529     // guarantee that all emitted copies are
01530     // stuck together, avoiding something bad
01531     Flag = Chain.getValue(1);
01532     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
01533   }
01534 
01535   RetOps[0] = Chain;  // Update chain.
01536 
01537   // Add the flag if we have it.
01538   if (Flag.getNode())
01539     RetOps.push_back(Flag);
01540 
01541   return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other, RetOps);
01542 }
01543 
01544 //===----------------------------------------------------------------------===//
01545 //  Other Lowering Code
01546 //===----------------------------------------------------------------------===//
01547 
01548 MachineBasicBlock *
01549 XCoreTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
01550                                                  MachineBasicBlock *BB) const {
01551   const TargetInstrInfo &TII =
01552       *getTargetMachine().getSubtargetImpl()->getInstrInfo();
01553   DebugLoc dl = MI->getDebugLoc();
01554   assert((MI->getOpcode() == XCore::SELECT_CC) &&
01555          "Unexpected instr type to insert");
01556 
01557   // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
01558   // control-flow pattern.  The incoming instruction knows the destination vreg
01559   // to set, the condition code register to branch on, the true/false values to
01560   // select between, and a branch opcode to use.
01561   const BasicBlock *LLVM_BB = BB->getBasicBlock();
01562   MachineFunction::iterator It = BB;
01563   ++It;
01564 
01565   //  thisMBB:
01566   //  ...
01567   //   TrueVal = ...
01568   //   cmpTY ccX, r1, r2
01569   //   bCC copy1MBB
01570   //   fallthrough --> copy0MBB
01571   MachineBasicBlock *thisMBB = BB;
01572   MachineFunction *F = BB->getParent();
01573   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
01574   MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
01575   F->insert(It, copy0MBB);
01576   F->insert(It, sinkMBB);
01577 
01578   // Transfer the remainder of BB and its successor edges to sinkMBB.
01579   sinkMBB->splice(sinkMBB->begin(), BB,
01580                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
01581   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
01582 
01583   // Next, add the true and fallthrough blocks as its successors.
01584   BB->addSuccessor(copy0MBB);
01585   BB->addSuccessor(sinkMBB);
01586 
01587   BuildMI(BB, dl, TII.get(XCore::BRFT_lru6))
01588     .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
01589 
01590   //  copy0MBB:
01591   //   %FalseValue = ...
01592   //   # fallthrough to sinkMBB
01593   BB = copy0MBB;
01594 
01595   // Update machine-CFG edges
01596   BB->addSuccessor(sinkMBB);
01597 
01598   //  sinkMBB:
01599   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
01600   //  ...
01601   BB = sinkMBB;
01602   BuildMI(*BB, BB->begin(), dl,
01603           TII.get(XCore::PHI), MI->getOperand(0).getReg())
01604     .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
01605     .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
01606 
01607   MI->eraseFromParent();   // The pseudo instruction is gone now.
01608   return BB;
01609 }
01610 
01611 //===----------------------------------------------------------------------===//
01612 // Target Optimization Hooks
01613 //===----------------------------------------------------------------------===//
01614 
01615 SDValue XCoreTargetLowering::PerformDAGCombine(SDNode *N,
01616                                              DAGCombinerInfo &DCI) const {
01617   SelectionDAG &DAG = DCI.DAG;
01618   SDLoc dl(N);
01619   switch (N->getOpcode()) {
01620   default: break;
01621   case ISD::INTRINSIC_VOID:
01622     switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
01623     case Intrinsic::xcore_outt:
01624     case Intrinsic::xcore_outct:
01625     case Intrinsic::xcore_chkct: {
01626       SDValue OutVal = N->getOperand(3);
01627       // These instructions ignore the high bits.
01628       if (OutVal.hasOneUse()) {
01629         unsigned BitWidth = OutVal.getValueSizeInBits();
01630         APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 8);
01631         APInt KnownZero, KnownOne;
01632         TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
01633                                               !DCI.isBeforeLegalizeOps());
01634         const TargetLowering &TLI = DAG.getTargetLoweringInfo();
01635         if (TLO.ShrinkDemandedConstant(OutVal, DemandedMask) ||
01636             TLI.SimplifyDemandedBits(OutVal, DemandedMask, KnownZero, KnownOne,
01637                                      TLO))
01638           DCI.CommitTargetLoweringOpt(TLO);
01639       }
01640       break;
01641     }
01642     case Intrinsic::xcore_setpt: {
01643       SDValue Time = N->getOperand(3);
01644       // This instruction ignores the high bits.
01645       if (Time.hasOneUse()) {
01646         unsigned BitWidth = Time.getValueSizeInBits();
01647         APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16);
01648         APInt KnownZero, KnownOne;
01649         TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
01650                                               !DCI.isBeforeLegalizeOps());
01651         const TargetLowering &TLI = DAG.getTargetLoweringInfo();
01652         if (TLO.ShrinkDemandedConstant(Time, DemandedMask) ||
01653             TLI.SimplifyDemandedBits(Time, DemandedMask, KnownZero, KnownOne,
01654                                      TLO))
01655           DCI.CommitTargetLoweringOpt(TLO);
01656       }
01657       break;
01658     }
01659     }
01660     break;
01661   case XCoreISD::LADD: {
01662     SDValue N0 = N->getOperand(0);
01663     SDValue N1 = N->getOperand(1);
01664     SDValue N2 = N->getOperand(2);
01665     ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
01666     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
01667     EVT VT = N0.getValueType();
01668 
01669     // canonicalize constant to RHS
01670     if (N0C && !N1C)
01671       return DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N1, N0, N2);
01672 
01673     // fold (ladd 0, 0, x) -> 0, x & 1
01674     if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
01675       SDValue Carry = DAG.getConstant(0, VT);
01676       SDValue Result = DAG.getNode(ISD::AND, dl, VT, N2,
01677                                    DAG.getConstant(1, VT));
01678       SDValue Ops[] = { Result, Carry };
01679       return DAG.getMergeValues(Ops, dl);
01680     }
01681 
01682     // fold (ladd x, 0, y) -> 0, add x, y iff carry is unused and y has only the
01683     // low bit set
01684     if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
01685       APInt KnownZero, KnownOne;
01686       APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
01687                                          VT.getSizeInBits() - 1);
01688       DAG.computeKnownBits(N2, KnownZero, KnownOne);
01689       if ((KnownZero & Mask) == Mask) {
01690         SDValue Carry = DAG.getConstant(0, VT);
01691         SDValue Result = DAG.getNode(ISD::ADD, dl, VT, N0, N2);
01692         SDValue Ops[] = { Result, Carry };
01693         return DAG.getMergeValues(Ops, dl);
01694       }
01695     }
01696   }
01697   break;
01698   case XCoreISD::LSUB: {
01699     SDValue N0 = N->getOperand(0);
01700     SDValue N1 = N->getOperand(1);
01701     SDValue N2 = N->getOperand(2);
01702     ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
01703     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
01704     EVT VT = N0.getValueType();
01705 
01706     // fold (lsub 0, 0, x) -> x, -x iff x has only the low bit set
01707     if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
01708       APInt KnownZero, KnownOne;
01709       APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
01710                                          VT.getSizeInBits() - 1);
01711       DAG.computeKnownBits(N2, KnownZero, KnownOne);
01712       if ((KnownZero & Mask) == Mask) {
01713         SDValue Borrow = N2;
01714         SDValue Result = DAG.getNode(ISD::SUB, dl, VT,
01715                                      DAG.getConstant(0, VT), N2);
01716         SDValue Ops[] = { Result, Borrow };
01717         return DAG.getMergeValues(Ops, dl);
01718       }
01719     }
01720 
01721     // fold (lsub x, 0, y) -> 0, sub x, y iff borrow is unused and y has only the
01722     // low bit set
01723     if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
01724       APInt KnownZero, KnownOne;
01725       APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
01726                                          VT.getSizeInBits() - 1);
01727       DAG.computeKnownBits(N2, KnownZero, KnownOne);
01728       if ((KnownZero & Mask) == Mask) {
01729         SDValue Borrow = DAG.getConstant(0, VT);
01730         SDValue Result = DAG.getNode(ISD::SUB, dl, VT, N0, N2);
01731         SDValue Ops[] = { Result, Borrow };
01732         return DAG.getMergeValues(Ops, dl);
01733       }
01734     }
01735   }
01736   break;
01737   case XCoreISD::LMUL: {
01738     SDValue N0 = N->getOperand(0);
01739     SDValue N1 = N->getOperand(1);
01740     SDValue N2 = N->getOperand(2);
01741     SDValue N3 = N->getOperand(3);
01742     ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
01743     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
01744     EVT VT = N0.getValueType();
01745     // Canonicalize multiplicative constant to RHS. If both multiplicative
01746     // operands are constant canonicalize smallest to RHS.
01747     if ((N0C && !N1C) ||
01748         (N0C && N1C && N0C->getZExtValue() < N1C->getZExtValue()))
01749       return DAG.getNode(XCoreISD::LMUL, dl, DAG.getVTList(VT, VT),
01750                          N1, N0, N2, N3);
01751 
01752     // lmul(x, 0, a, b)
01753     if (N1C && N1C->isNullValue()) {
01754       // If the high result is unused fold to add(a, b)
01755       if (N->hasNUsesOfValue(0, 0)) {
01756         SDValue Lo = DAG.getNode(ISD::ADD, dl, VT, N2, N3);
01757         SDValue Ops[] = { Lo, Lo };
01758         return DAG.getMergeValues(Ops, dl);
01759       }
01760       // Otherwise fold to ladd(a, b, 0)
01761       SDValue Result =
01762         DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N2, N3, N1);
01763       SDValue Carry(Result.getNode(), 1);
01764       SDValue Ops[] = { Carry, Result };
01765       return DAG.getMergeValues(Ops, dl);
01766     }
01767   }
01768   break;
01769   case ISD::ADD: {
01770     // Fold 32 bit expressions such as add(add(mul(x,y),a),b) ->
01771     // lmul(x, y, a, b). The high result of lmul will be ignored.
01772     // This is only profitable if the intermediate results are unused
01773     // elsewhere.
01774     SDValue Mul0, Mul1, Addend0, Addend1;
01775     if (N->getValueType(0) == MVT::i32 &&
01776         isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, true)) {
01777       SDValue Ignored = DAG.getNode(XCoreISD::LMUL, dl,
01778                                     DAG.getVTList(MVT::i32, MVT::i32), Mul0,
01779                                     Mul1, Addend0, Addend1);
01780       SDValue Result(Ignored.getNode(), 1);
01781       return Result;
01782     }
01783     APInt HighMask = APInt::getHighBitsSet(64, 32);
01784     // Fold 64 bit expression such as add(add(mul(x,y),a),b) ->
01785     // lmul(x, y, a, b) if all operands are zero-extended. We do this
01786     // before type legalization as it is messy to match the operands after
01787     // that.
01788     if (N->getValueType(0) == MVT::i64 &&
01789         isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, false) &&
01790         DAG.MaskedValueIsZero(Mul0, HighMask) &&
01791         DAG.MaskedValueIsZero(Mul1, HighMask) &&
01792         DAG.MaskedValueIsZero(Addend0, HighMask) &&
01793         DAG.MaskedValueIsZero(Addend1, HighMask)) {
01794       SDValue Mul0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
01795                                   Mul0, DAG.getConstant(0, MVT::i32));
01796       SDValue Mul1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
01797                                   Mul1, DAG.getConstant(0, MVT::i32));
01798       SDValue Addend0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
01799                                      Addend0, DAG.getConstant(0, MVT::i32));
01800       SDValue Addend1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
01801                                      Addend1, DAG.getConstant(0, MVT::i32));
01802       SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
01803                                DAG.getVTList(MVT::i32, MVT::i32), Mul0L, Mul1L,
01804                                Addend0L, Addend1L);
01805       SDValue Lo(Hi.getNode(), 1);
01806       return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
01807     }
01808   }
01809   break;
01810   case ISD::STORE: {
01811     // Replace unaligned store of unaligned load with memmove.
01812     StoreSDNode *ST  = cast<StoreSDNode>(N);
01813     if (!DCI.isBeforeLegalize() ||
01814         allowsMisalignedMemoryAccesses(ST->getMemoryVT(),
01815                                        ST->getAddressSpace(),
01816                                        ST->getAlignment()) ||
01817         ST->isVolatile() || ST->isIndexed()) {
01818       break;
01819     }
01820     SDValue Chain = ST->getChain();
01821 
01822     unsigned StoreBits = ST->getMemoryVT().getStoreSizeInBits();
01823     if (StoreBits % 8) {
01824       break;
01825     }
01826     unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(
01827         ST->getMemoryVT().getTypeForEVT(*DCI.DAG.getContext()));
01828     unsigned Alignment = ST->getAlignment();
01829     if (Alignment >= ABIAlignment) {
01830       break;
01831     }
01832 
01833     if (LoadSDNode *LD = dyn_cast<LoadSDNode>(ST->getValue())) {
01834       if (LD->hasNUsesOfValue(1, 0) && ST->getMemoryVT() == LD->getMemoryVT() &&
01835         LD->getAlignment() == Alignment &&
01836         !LD->isVolatile() && !LD->isIndexed() &&
01837         Chain.reachesChainWithoutSideEffects(SDValue(LD, 1))) {
01838         return DAG.getMemmove(Chain, dl, ST->getBasePtr(),
01839                               LD->getBasePtr(),
01840                               DAG.getConstant(StoreBits/8, MVT::i32),
01841                               Alignment, false, ST->getPointerInfo(),
01842                               LD->getPointerInfo());
01843       }
01844     }
01845     break;
01846   }
01847   }
01848   return SDValue();
01849 }
01850 
01851 void XCoreTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
01852                                                         APInt &KnownZero,
01853                                                         APInt &KnownOne,
01854                                                         const SelectionDAG &DAG,
01855                                                         unsigned Depth) const {
01856   KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
01857   switch (Op.getOpcode()) {
01858   default: break;
01859   case XCoreISD::LADD:
01860   case XCoreISD::LSUB:
01861     if (Op.getResNo() == 1) {
01862       // Top bits of carry / borrow are clear.
01863       KnownZero = APInt::getHighBitsSet(KnownZero.getBitWidth(),
01864                                         KnownZero.getBitWidth() - 1);
01865     }
01866     break;
01867   case ISD::INTRINSIC_W_CHAIN:
01868     {
01869       unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
01870       switch (IntNo) {
01871       case Intrinsic::xcore_getts:
01872         // High bits are known to be zero.
01873         KnownZero = APInt::getHighBitsSet(KnownZero.getBitWidth(),
01874                                           KnownZero.getBitWidth() - 16);
01875         break;
01876       case Intrinsic::xcore_int:
01877       case Intrinsic::xcore_inct:
01878         // High bits are known to be zero.
01879         KnownZero = APInt::getHighBitsSet(KnownZero.getBitWidth(),
01880                                           KnownZero.getBitWidth() - 8);
01881         break;
01882       case Intrinsic::xcore_testct:
01883         // Result is either 0 or 1.
01884         KnownZero = APInt::getHighBitsSet(KnownZero.getBitWidth(),
01885                                           KnownZero.getBitWidth() - 1);
01886         break;
01887       case Intrinsic::xcore_testwct:
01888         // Result is in the range 0 - 4.
01889         KnownZero = APInt::getHighBitsSet(KnownZero.getBitWidth(),
01890                                           KnownZero.getBitWidth() - 3);
01891         break;
01892       }
01893     }
01894     break;
01895   }
01896 }
01897 
01898 //===----------------------------------------------------------------------===//
01899 //  Addressing mode description hooks
01900 //===----------------------------------------------------------------------===//
01901 
01902 static inline bool isImmUs(int64_t val)
01903 {
01904   return (val >= 0 && val <= 11);
01905 }
01906 
01907 static inline bool isImmUs2(int64_t val)
01908 {
01909   return (val%2 == 0 && isImmUs(val/2));
01910 }
01911 
01912 static inline bool isImmUs4(int64_t val)
01913 {
01914   return (val%4 == 0 && isImmUs(val/4));
01915 }
01916 
01917 /// isLegalAddressingMode - Return true if the addressing mode represented
01918 /// by AM is legal for this target, for a load/store of the specified type.
01919 bool
01920 XCoreTargetLowering::isLegalAddressingMode(const AddrMode &AM,
01921                                               Type *Ty) const {
01922   if (Ty->getTypeID() == Type::VoidTyID)
01923     return AM.Scale == 0 && isImmUs(AM.BaseOffs) && isImmUs4(AM.BaseOffs);
01924 
01925   const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
01926   unsigned Size = TD->getTypeAllocSize(Ty);
01927   if (AM.BaseGV) {
01928     return Size >= 4 && !AM.HasBaseReg && AM.Scale == 0 &&
01929                  AM.BaseOffs%4 == 0;
01930   }
01931 
01932   switch (Size) {
01933   case 1:
01934     // reg + imm
01935     if (AM.Scale == 0) {
01936       return isImmUs(AM.BaseOffs);
01937     }
01938     // reg + reg
01939     return AM.Scale == 1 && AM.BaseOffs == 0;
01940   case 2:
01941   case 3:
01942     // reg + imm
01943     if (AM.Scale == 0) {
01944       return isImmUs2(AM.BaseOffs);
01945     }
01946     // reg + reg<<1
01947     return AM.Scale == 2 && AM.BaseOffs == 0;
01948   default:
01949     // reg + imm
01950     if (AM.Scale == 0) {
01951       return isImmUs4(AM.BaseOffs);
01952     }
01953     // reg + reg<<2
01954     return AM.Scale == 4 && AM.BaseOffs == 0;
01955   }
01956 }
01957 
01958 //===----------------------------------------------------------------------===//
01959 //                           XCore Inline Assembly Support
01960 //===----------------------------------------------------------------------===//
01961 
01962 std::pair<unsigned, const TargetRegisterClass*>
01963 XCoreTargetLowering::
01964 getRegForInlineAsmConstraint(const std::string &Constraint,
01965                              MVT VT) const {
01966   if (Constraint.size() == 1) {
01967     switch (Constraint[0]) {
01968     default : break;
01969     case 'r':
01970       return std::make_pair(0U, &XCore::GRRegsRegClass);
01971     }
01972   }
01973   // Use the default implementation in TargetLowering to convert the register
01974   // constraint into a member of a register class.
01975   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
01976 }