XCoreISelLowering.cpp revision 203954
1193323Sed//===-- XCoreISelLowering.cpp - XCore DAG Lowering Implementation ------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements the XCoreTargetLowering class. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#define DEBUG_TYPE "xcore-lower" 15193323Sed 16193323Sed#include "XCoreISelLowering.h" 17193323Sed#include "XCoreMachineFunctionInfo.h" 18193323Sed#include "XCore.h" 19198090Srdivacky#include "XCoreTargetObjectFile.h" 20193323Sed#include "XCoreTargetMachine.h" 21193323Sed#include "XCoreSubtarget.h" 22193323Sed#include "llvm/DerivedTypes.h" 23193323Sed#include "llvm/Function.h" 24193323Sed#include "llvm/Intrinsics.h" 25193323Sed#include "llvm/CallingConv.h" 26193323Sed#include "llvm/GlobalVariable.h" 27193323Sed#include "llvm/GlobalAlias.h" 28193323Sed#include "llvm/CodeGen/CallingConvLower.h" 29193323Sed#include "llvm/CodeGen/MachineFrameInfo.h" 30193323Sed#include "llvm/CodeGen/MachineFunction.h" 31193323Sed#include "llvm/CodeGen/MachineInstrBuilder.h" 32193323Sed#include "llvm/CodeGen/MachineRegisterInfo.h" 33193323Sed#include "llvm/CodeGen/SelectionDAGISel.h" 34193323Sed#include "llvm/CodeGen/ValueTypes.h" 35193323Sed#include "llvm/Support/Debug.h" 36198090Srdivacky#include "llvm/Support/ErrorHandling.h" 37198090Srdivacky#include "llvm/Support/raw_ostream.h" 38193323Sed#include "llvm/ADT/VectorExtras.h" 39193323Sed#include <queue> 40193323Sed#include <set> 41193323Sedusing namespace llvm; 42193323Sed 43193323Sedconst char *XCoreTargetLowering:: 44193323SedgetTargetNodeName(unsigned Opcode) const 45193323Sed{ 46193323Sed switch (Opcode) 47193323Sed { 48193323Sed case XCoreISD::BL : return "XCoreISD::BL"; 49193323Sed case XCoreISD::PCRelativeWrapper : return "XCoreISD::PCRelativeWrapper"; 50193323Sed case XCoreISD::DPRelativeWrapper : return "XCoreISD::DPRelativeWrapper"; 51193323Sed case XCoreISD::CPRelativeWrapper : return "XCoreISD::CPRelativeWrapper"; 52193323Sed case XCoreISD::STWSP : return "XCoreISD::STWSP"; 53193323Sed case XCoreISD::RETSP : return "XCoreISD::RETSP"; 54198090Srdivacky case XCoreISD::LADD : return "XCoreISD::LADD"; 55198090Srdivacky case XCoreISD::LSUB : return "XCoreISD::LSUB"; 56193323Sed default : return NULL; 57193323Sed } 58193323Sed} 59193323Sed 60193323SedXCoreTargetLowering::XCoreTargetLowering(XCoreTargetMachine &XTM) 61198090Srdivacky : TargetLowering(XTM, new XCoreTargetObjectFile()), 62193323Sed TM(XTM), 63193323Sed Subtarget(*XTM.getSubtargetImpl()) { 64193323Sed 65193323Sed // Set up the register classes. 66193323Sed addRegisterClass(MVT::i32, XCore::GRRegsRegisterClass); 67193323Sed 68193323Sed // Compute derived properties from the register classes 69193323Sed computeRegisterProperties(); 70193323Sed 71193323Sed // Division is expensive 72193323Sed setIntDivIsCheap(false); 73193323Sed 74193323Sed setShiftAmountType(MVT::i32); 75193323Sed setStackPointerRegisterToSaveRestore(XCore::SP); 76193323Sed 77193323Sed setSchedulingPreference(SchedulingForRegPressure); 78193323Sed 79193323Sed // Use i32 for setcc operations results (slt, sgt, ...). 80193323Sed setBooleanContents(ZeroOrOneBooleanContent); 81193323Sed 82193323Sed // XCore does not have the NodeTypes below. 83193323Sed setOperationAction(ISD::BR_CC, MVT::Other, Expand); 84193323Sed setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 85193323Sed setOperationAction(ISD::ADDC, MVT::i32, Expand); 86193323Sed setOperationAction(ISD::ADDE, MVT::i32, Expand); 87193323Sed setOperationAction(ISD::SUBC, MVT::i32, Expand); 88193323Sed setOperationAction(ISD::SUBE, MVT::i32, Expand); 89193323Sed 90193323Sed // Stop the combiner recombining select and set_cc 91193323Sed setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); 92193323Sed 93193323Sed // 64bit 94198090Srdivacky setOperationAction(ISD::ADD, MVT::i64, Custom); 95198090Srdivacky setOperationAction(ISD::SUB, MVT::i64, Custom); 96193323Sed setOperationAction(ISD::MULHS, MVT::i32, Expand); 97193323Sed setOperationAction(ISD::MULHU, MVT::i32, Expand); 98193323Sed setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); 99193323Sed setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); 100193323Sed setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); 101193323Sed 102193323Sed // Bit Manipulation 103193323Sed setOperationAction(ISD::CTPOP, MVT::i32, Expand); 104193323Sed setOperationAction(ISD::ROTL , MVT::i32, Expand); 105193323Sed setOperationAction(ISD::ROTR , MVT::i32, Expand); 106193323Sed 107193323Sed setOperationAction(ISD::TRAP, MVT::Other, Legal); 108193323Sed 109193323Sed // Expand jump tables for now 110193323Sed setOperationAction(ISD::BR_JT, MVT::Other, Expand); 111193323Sed setOperationAction(ISD::JumpTable, MVT::i32, Custom); 112193323Sed 113193323Sed setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 114199511Srdivacky setOperationAction(ISD::BlockAddress, MVT::i32 , Custom); 115199511Srdivacky 116193323Sed // Thread Local Storage 117193323Sed setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 118193323Sed 119193323Sed // Conversion of i64 -> double produces constantpool nodes 120193323Sed setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 121193323Sed 122193323Sed // Loads 123193323Sed setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); 124193323Sed setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); 125193323Sed setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 126193323Sed 127193323Sed setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand); 128193323Sed setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand); 129198090Srdivacky 130198090Srdivacky // Custom expand misaligned loads / stores. 131198090Srdivacky setOperationAction(ISD::LOAD, MVT::i32, Custom); 132198090Srdivacky setOperationAction(ISD::STORE, MVT::i32, Custom); 133198090Srdivacky 134193323Sed // Varargs 135193323Sed setOperationAction(ISD::VAEND, MVT::Other, Expand); 136193323Sed setOperationAction(ISD::VACOPY, MVT::Other, Expand); 137193323Sed setOperationAction(ISD::VAARG, MVT::Other, Custom); 138193323Sed setOperationAction(ISD::VASTART, MVT::Other, Custom); 139193323Sed 140193323Sed // Dynamic stack 141193323Sed setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 142193323Sed setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 143193323Sed setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 144193323Sed 145198090Srdivacky maxStoresPerMemset = 4; 146198090Srdivacky maxStoresPerMemmove = maxStoresPerMemcpy = 2; 147198090Srdivacky 148198090Srdivacky // We have target-specific dag combine patterns for the following nodes: 149198090Srdivacky setTargetDAGCombine(ISD::STORE); 150193323Sed} 151193323Sed 152193323SedSDValue XCoreTargetLowering:: 153193323SedLowerOperation(SDValue Op, SelectionDAG &DAG) { 154193323Sed switch (Op.getOpcode()) 155193323Sed { 156193323Sed case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 157193323Sed case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 158199511Srdivacky case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 159193323Sed case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 160193323Sed case ISD::JumpTable: return LowerJumpTable(Op, DAG); 161198090Srdivacky case ISD::LOAD: return LowerLOAD(Op, DAG); 162198090Srdivacky case ISD::STORE: return LowerSTORE(Op, DAG); 163193323Sed case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 164193323Sed case ISD::VAARG: return LowerVAARG(Op, DAG); 165193323Sed case ISD::VASTART: return LowerVASTART(Op, DAG); 166193323Sed // FIXME: Remove these when LegalizeDAGTypes lands. 167193323Sed case ISD::ADD: 168193323Sed case ISD::SUB: return ExpandADDSUB(Op.getNode(), DAG); 169193323Sed case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 170193323Sed default: 171198090Srdivacky llvm_unreachable("unimplemented operand"); 172193323Sed return SDValue(); 173193323Sed } 174193323Sed} 175193323Sed 176193323Sed/// ReplaceNodeResults - Replace the results of node with an illegal result 177193323Sed/// type with new values built out of custom code. 178193323Sedvoid XCoreTargetLowering::ReplaceNodeResults(SDNode *N, 179193323Sed SmallVectorImpl<SDValue>&Results, 180193323Sed SelectionDAG &DAG) { 181193323Sed switch (N->getOpcode()) { 182193323Sed default: 183198090Srdivacky llvm_unreachable("Don't know how to custom expand this!"); 184193323Sed return; 185193323Sed case ISD::ADD: 186193323Sed case ISD::SUB: 187193323Sed Results.push_back(ExpandADDSUB(N, DAG)); 188193323Sed return; 189193323Sed } 190193323Sed} 191193323Sed 192195340Sed/// getFunctionAlignment - Return the Log2 alignment of this function. 193195340Sedunsigned XCoreTargetLowering:: 194195340SedgetFunctionAlignment(const Function *) const { 195195340Sed return 1; 196195340Sed} 197195340Sed 198193323Sed//===----------------------------------------------------------------------===// 199193323Sed// Misc Lower Operation implementation 200193323Sed//===----------------------------------------------------------------------===// 201193323Sed 202193323SedSDValue XCoreTargetLowering:: 203193323SedLowerSELECT_CC(SDValue Op, SelectionDAG &DAG) 204193323Sed{ 205193323Sed DebugLoc dl = Op.getDebugLoc(); 206193323Sed SDValue Cond = DAG.getNode(ISD::SETCC, dl, MVT::i32, Op.getOperand(2), 207193323Sed Op.getOperand(3), Op.getOperand(4)); 208193323Sed return DAG.getNode(ISD::SELECT, dl, MVT::i32, Cond, Op.getOperand(0), 209193323Sed Op.getOperand(1)); 210193323Sed} 211193323Sed 212193323SedSDValue XCoreTargetLowering:: 213193323SedgetGlobalAddressWrapper(SDValue GA, GlobalValue *GV, SelectionDAG &DAG) 214193323Sed{ 215193323Sed // FIXME there is no actual debug info here 216193323Sed DebugLoc dl = GA.getDebugLoc(); 217193323Sed if (isa<Function>(GV)) { 218193323Sed return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA); 219193323Sed } 220198090Srdivacky const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 221198090Srdivacky if (!GVar) { 222198090Srdivacky // If GV is an alias then use the aliasee to determine constness 223198090Srdivacky if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 224198090Srdivacky GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal()); 225198090Srdivacky } 226198090Srdivacky bool isConst = GVar && GVar->isConstant(); 227198090Srdivacky if (isConst) { 228198090Srdivacky return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA); 229198090Srdivacky } 230193323Sed return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA); 231193323Sed} 232193323Sed 233193323SedSDValue XCoreTargetLowering:: 234193323SedLowerGlobalAddress(SDValue Op, SelectionDAG &DAG) 235193323Sed{ 236193323Sed GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 237193323Sed SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32); 238193323Sed // If it's a debug information descriptor, don't mess with it. 239193323Sed if (DAG.isVerifiedDebugInfoDesc(Op)) 240193323Sed return GA; 241193323Sed return getGlobalAddressWrapper(GA, GV, DAG); 242193323Sed} 243193323Sed 244193323Sedstatic inline SDValue BuildGetId(SelectionDAG &DAG, DebugLoc dl) { 245193323Sed return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, 246193323Sed DAG.getConstant(Intrinsic::xcore_getid, MVT::i32)); 247193323Sed} 248193323Sed 249193323Sedstatic inline bool isZeroLengthArray(const Type *Ty) { 250193323Sed const ArrayType *AT = dyn_cast_or_null<ArrayType>(Ty); 251193323Sed return AT && (AT->getNumElements() == 0); 252193323Sed} 253193323Sed 254193323SedSDValue XCoreTargetLowering:: 255193323SedLowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) 256193323Sed{ 257193323Sed // FIXME there isn't really debug info here 258193323Sed DebugLoc dl = Op.getDebugLoc(); 259193323Sed // transform to label + getid() * size 260193323Sed GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 261193323Sed SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32); 262193323Sed const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 263193323Sed if (!GVar) { 264193323Sed // If GV is an alias then use the aliasee to determine size 265193323Sed if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 266193323Sed GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal()); 267193323Sed } 268193323Sed if (! GVar) { 269198090Srdivacky llvm_unreachable("Thread local object not a GlobalVariable?"); 270193323Sed return SDValue(); 271193323Sed } 272193323Sed const Type *Ty = cast<PointerType>(GV->getType())->getElementType(); 273193323Sed if (!Ty->isSized() || isZeroLengthArray(Ty)) { 274198090Srdivacky#ifndef NDEBUG 275198090Srdivacky errs() << "Size of thread local object " << GVar->getName() 276198090Srdivacky << " is unknown\n"; 277198090Srdivacky#endif 278198090Srdivacky llvm_unreachable(0); 279193323Sed } 280193323Sed SDValue base = getGlobalAddressWrapper(GA, GV, DAG); 281193323Sed const TargetData *TD = TM.getTargetData(); 282193323Sed unsigned Size = TD->getTypeAllocSize(Ty); 283193323Sed SDValue offset = DAG.getNode(ISD::MUL, dl, MVT::i32, BuildGetId(DAG, dl), 284193323Sed DAG.getConstant(Size, MVT::i32)); 285193323Sed return DAG.getNode(ISD::ADD, dl, MVT::i32, base, offset); 286193323Sed} 287193323Sed 288193323SedSDValue XCoreTargetLowering:: 289199511SrdivackyLowerBlockAddress(SDValue Op, SelectionDAG &DAG) 290199511Srdivacky{ 291199511Srdivacky DebugLoc DL = Op.getDebugLoc(); 292199511Srdivacky 293199511Srdivacky BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 294199989Srdivacky SDValue Result = DAG.getBlockAddress(BA, getPointerTy(), /*isTarget=*/true); 295199511Srdivacky 296199511Srdivacky return DAG.getNode(XCoreISD::PCRelativeWrapper, DL, getPointerTy(), Result); 297199511Srdivacky} 298199511Srdivacky 299199511SrdivackySDValue XCoreTargetLowering:: 300193323SedLowerConstantPool(SDValue Op, SelectionDAG &DAG) 301193323Sed{ 302193323Sed ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 303193323Sed // FIXME there isn't really debug info here 304193323Sed DebugLoc dl = CP->getDebugLoc(); 305198090Srdivacky EVT PtrVT = Op.getValueType(); 306198090Srdivacky SDValue Res; 307198090Srdivacky if (CP->isMachineConstantPoolEntry()) { 308198090Srdivacky Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 309198090Srdivacky CP->getAlignment()); 310193323Sed } else { 311198090Srdivacky Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 312198090Srdivacky CP->getAlignment()); 313193323Sed } 314198090Srdivacky return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, Res); 315193323Sed} 316193323Sed 317193323SedSDValue XCoreTargetLowering:: 318193323SedLowerJumpTable(SDValue Op, SelectionDAG &DAG) 319193323Sed{ 320193323Sed // FIXME there isn't really debug info here 321193323Sed DebugLoc dl = Op.getDebugLoc(); 322198090Srdivacky EVT PtrVT = Op.getValueType(); 323193323Sed JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 324193323Sed SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); 325193323Sed return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, JTI); 326193323Sed} 327193323Sed 328198090Srdivackystatic bool 329198090SrdivackyIsWordAlignedBasePlusConstantOffset(SDValue Addr, SDValue &AlignedBase, 330198090Srdivacky int64_t &Offset) 331198090Srdivacky{ 332198090Srdivacky if (Addr.getOpcode() != ISD::ADD) { 333198090Srdivacky return false; 334198090Srdivacky } 335198090Srdivacky ConstantSDNode *CN = 0; 336198090Srdivacky if (!(CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 337198090Srdivacky return false; 338198090Srdivacky } 339198090Srdivacky int64_t off = CN->getSExtValue(); 340198090Srdivacky const SDValue &Base = Addr.getOperand(0); 341198090Srdivacky const SDValue *Root = &Base; 342198090Srdivacky if (Base.getOpcode() == ISD::ADD && 343198090Srdivacky Base.getOperand(1).getOpcode() == ISD::SHL) { 344198090Srdivacky ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Base.getOperand(1) 345198090Srdivacky .getOperand(1)); 346198090Srdivacky if (CN && (CN->getSExtValue() >= 2)) { 347198090Srdivacky Root = &Base.getOperand(0); 348198090Srdivacky } 349198090Srdivacky } 350198090Srdivacky if (isa<FrameIndexSDNode>(*Root)) { 351198090Srdivacky // All frame indicies are word aligned 352198090Srdivacky AlignedBase = Base; 353198090Srdivacky Offset = off; 354198090Srdivacky return true; 355198090Srdivacky } 356198090Srdivacky if (Root->getOpcode() == XCoreISD::DPRelativeWrapper || 357198090Srdivacky Root->getOpcode() == XCoreISD::CPRelativeWrapper) { 358198090Srdivacky // All dp / cp relative addresses are word aligned 359198090Srdivacky AlignedBase = Base; 360198090Srdivacky Offset = off; 361198090Srdivacky return true; 362198090Srdivacky } 363198090Srdivacky return false; 364198090Srdivacky} 365198090Srdivacky 366193323SedSDValue XCoreTargetLowering:: 367198090SrdivackyLowerLOAD(SDValue Op, SelectionDAG &DAG) 368198090Srdivacky{ 369198090Srdivacky LoadSDNode *LD = cast<LoadSDNode>(Op); 370198090Srdivacky assert(LD->getExtensionType() == ISD::NON_EXTLOAD && 371198090Srdivacky "Unexpected extension type"); 372198090Srdivacky assert(LD->getMemoryVT() == MVT::i32 && "Unexpected load EVT"); 373198090Srdivacky if (allowsUnalignedMemoryAccesses(LD->getMemoryVT())) { 374198090Srdivacky return SDValue(); 375198090Srdivacky } 376198090Srdivacky unsigned ABIAlignment = getTargetData()-> 377198090Srdivacky getABITypeAlignment(LD->getMemoryVT().getTypeForEVT(*DAG.getContext())); 378198090Srdivacky // Leave aligned load alone. 379198090Srdivacky if (LD->getAlignment() >= ABIAlignment) { 380198090Srdivacky return SDValue(); 381198090Srdivacky } 382198090Srdivacky SDValue Chain = LD->getChain(); 383198090Srdivacky SDValue BasePtr = LD->getBasePtr(); 384198090Srdivacky DebugLoc dl = Op.getDebugLoc(); 385198090Srdivacky 386198090Srdivacky SDValue Base; 387198090Srdivacky int64_t Offset; 388198090Srdivacky if (!LD->isVolatile() && 389198090Srdivacky IsWordAlignedBasePlusConstantOffset(BasePtr, Base, Offset)) { 390198090Srdivacky if (Offset % 4 == 0) { 391198090Srdivacky // We've managed to infer better alignment information than the load 392198090Srdivacky // already has. Use an aligned load. 393203954Srdivacky // 394203954Srdivacky // FIXME: No new alignment information is actually passed here. 395203954Srdivacky // Should the offset really be 4? 396203954Srdivacky // 397203954Srdivacky return DAG.getLoad(getPointerTy(), dl, Chain, BasePtr, NULL, 4, 398203954Srdivacky false, false, 0); 399198090Srdivacky } 400198090Srdivacky // Lower to 401198090Srdivacky // ldw low, base[offset >> 2] 402198090Srdivacky // ldw high, base[(offset >> 2) + 1] 403198090Srdivacky // shr low_shifted, low, (offset & 0x3) * 8 404198090Srdivacky // shl high_shifted, high, 32 - (offset & 0x3) * 8 405198090Srdivacky // or result, low_shifted, high_shifted 406198090Srdivacky SDValue LowOffset = DAG.getConstant(Offset & ~0x3, MVT::i32); 407198090Srdivacky SDValue HighOffset = DAG.getConstant((Offset & ~0x3) + 4, MVT::i32); 408198090Srdivacky SDValue LowShift = DAG.getConstant((Offset & 0x3) * 8, MVT::i32); 409198090Srdivacky SDValue HighShift = DAG.getConstant(32 - (Offset & 0x3) * 8, MVT::i32); 410198090Srdivacky 411198090Srdivacky SDValue LowAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Base, LowOffset); 412198090Srdivacky SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Base, HighOffset); 413198090Srdivacky 414198090Srdivacky SDValue Low = DAG.getLoad(getPointerTy(), dl, Chain, 415203954Srdivacky LowAddr, NULL, 4, false, false, 0); 416198090Srdivacky SDValue High = DAG.getLoad(getPointerTy(), dl, Chain, 417203954Srdivacky HighAddr, NULL, 4, false, false, 0); 418198090Srdivacky SDValue LowShifted = DAG.getNode(ISD::SRL, dl, MVT::i32, Low, LowShift); 419198090Srdivacky SDValue HighShifted = DAG.getNode(ISD::SHL, dl, MVT::i32, High, HighShift); 420198090Srdivacky SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, LowShifted, HighShifted); 421198090Srdivacky Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Low.getValue(1), 422198090Srdivacky High.getValue(1)); 423198090Srdivacky SDValue Ops[] = { Result, Chain }; 424198090Srdivacky return DAG.getMergeValues(Ops, 2, dl); 425198090Srdivacky } 426198090Srdivacky 427198090Srdivacky if (LD->getAlignment() == 2) { 428198090Srdivacky int SVOffset = LD->getSrcValueOffset(); 429198090Srdivacky SDValue Low = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain, 430198090Srdivacky BasePtr, LD->getSrcValue(), SVOffset, MVT::i16, 431203954Srdivacky LD->isVolatile(), LD->isNonTemporal(), 2); 432198090Srdivacky SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr, 433198090Srdivacky DAG.getConstant(2, MVT::i32)); 434198090Srdivacky SDValue High = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::i32, Chain, 435198090Srdivacky HighAddr, LD->getSrcValue(), SVOffset + 2, 436203954Srdivacky MVT::i16, LD->isVolatile(), 437203954Srdivacky LD->isNonTemporal(), 2); 438198090Srdivacky SDValue HighShifted = DAG.getNode(ISD::SHL, dl, MVT::i32, High, 439198090Srdivacky DAG.getConstant(16, MVT::i32)); 440198090Srdivacky SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, Low, HighShifted); 441198090Srdivacky Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Low.getValue(1), 442198090Srdivacky High.getValue(1)); 443198090Srdivacky SDValue Ops[] = { Result, Chain }; 444198090Srdivacky return DAG.getMergeValues(Ops, 2, dl); 445198090Srdivacky } 446198090Srdivacky 447198090Srdivacky // Lower to a call to __misaligned_load(BasePtr). 448198090Srdivacky const Type *IntPtrTy = getTargetData()->getIntPtrType(*DAG.getContext()); 449198090Srdivacky TargetLowering::ArgListTy Args; 450198090Srdivacky TargetLowering::ArgListEntry Entry; 451198090Srdivacky 452198090Srdivacky Entry.Ty = IntPtrTy; 453198090Srdivacky Entry.Node = BasePtr; 454198090Srdivacky Args.push_back(Entry); 455198090Srdivacky 456198090Srdivacky std::pair<SDValue, SDValue> CallResult = 457198090Srdivacky LowerCallTo(Chain, IntPtrTy, false, false, 458198090Srdivacky false, false, 0, CallingConv::C, false, 459198090Srdivacky /*isReturnValueUsed=*/true, 460198090Srdivacky DAG.getExternalSymbol("__misaligned_load", getPointerTy()), 461201360Srdivacky Args, DAG, dl, DAG.GetOrdering(Chain.getNode())); 462198090Srdivacky 463198090Srdivacky SDValue Ops[] = 464198090Srdivacky { CallResult.first, CallResult.second }; 465198090Srdivacky 466198090Srdivacky return DAG.getMergeValues(Ops, 2, dl); 467198090Srdivacky} 468198090Srdivacky 469198090SrdivackySDValue XCoreTargetLowering:: 470198090SrdivackyLowerSTORE(SDValue Op, SelectionDAG &DAG) 471198090Srdivacky{ 472198090Srdivacky StoreSDNode *ST = cast<StoreSDNode>(Op); 473198090Srdivacky assert(!ST->isTruncatingStore() && "Unexpected store type"); 474198090Srdivacky assert(ST->getMemoryVT() == MVT::i32 && "Unexpected store EVT"); 475198090Srdivacky if (allowsUnalignedMemoryAccesses(ST->getMemoryVT())) { 476198090Srdivacky return SDValue(); 477198090Srdivacky } 478198090Srdivacky unsigned ABIAlignment = getTargetData()-> 479198090Srdivacky getABITypeAlignment(ST->getMemoryVT().getTypeForEVT(*DAG.getContext())); 480198090Srdivacky // Leave aligned store alone. 481198090Srdivacky if (ST->getAlignment() >= ABIAlignment) { 482198090Srdivacky return SDValue(); 483198090Srdivacky } 484198090Srdivacky SDValue Chain = ST->getChain(); 485198090Srdivacky SDValue BasePtr = ST->getBasePtr(); 486198090Srdivacky SDValue Value = ST->getValue(); 487198090Srdivacky DebugLoc dl = Op.getDebugLoc(); 488198090Srdivacky 489198090Srdivacky if (ST->getAlignment() == 2) { 490198090Srdivacky int SVOffset = ST->getSrcValueOffset(); 491198090Srdivacky SDValue Low = Value; 492198090Srdivacky SDValue High = DAG.getNode(ISD::SRL, dl, MVT::i32, Value, 493198090Srdivacky DAG.getConstant(16, MVT::i32)); 494198090Srdivacky SDValue StoreLow = DAG.getTruncStore(Chain, dl, Low, BasePtr, 495198090Srdivacky ST->getSrcValue(), SVOffset, MVT::i16, 496203954Srdivacky ST->isVolatile(), ST->isNonTemporal(), 497203954Srdivacky 2); 498198090Srdivacky SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr, 499198090Srdivacky DAG.getConstant(2, MVT::i32)); 500198090Srdivacky SDValue StoreHigh = DAG.getTruncStore(Chain, dl, High, HighAddr, 501198090Srdivacky ST->getSrcValue(), SVOffset + 2, 502203954Srdivacky MVT::i16, ST->isVolatile(), 503203954Srdivacky ST->isNonTemporal(), 2); 504198090Srdivacky return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, StoreLow, StoreHigh); 505198090Srdivacky } 506198090Srdivacky 507198090Srdivacky // Lower to a call to __misaligned_store(BasePtr, Value). 508198090Srdivacky const Type *IntPtrTy = getTargetData()->getIntPtrType(*DAG.getContext()); 509198090Srdivacky TargetLowering::ArgListTy Args; 510198090Srdivacky TargetLowering::ArgListEntry Entry; 511198090Srdivacky 512198090Srdivacky Entry.Ty = IntPtrTy; 513198090Srdivacky Entry.Node = BasePtr; 514198090Srdivacky Args.push_back(Entry); 515198090Srdivacky 516198090Srdivacky Entry.Node = Value; 517198090Srdivacky Args.push_back(Entry); 518198090Srdivacky 519198090Srdivacky std::pair<SDValue, SDValue> CallResult = 520198090Srdivacky LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()), false, false, 521198090Srdivacky false, false, 0, CallingConv::C, false, 522198090Srdivacky /*isReturnValueUsed=*/true, 523198090Srdivacky DAG.getExternalSymbol("__misaligned_store", getPointerTy()), 524201360Srdivacky Args, DAG, dl, DAG.GetOrdering(Chain.getNode())); 525198090Srdivacky 526198090Srdivacky return CallResult.second; 527198090Srdivacky} 528198090Srdivacky 529198090SrdivackySDValue XCoreTargetLowering:: 530193323SedExpandADDSUB(SDNode *N, SelectionDAG &DAG) 531193323Sed{ 532193323Sed assert(N->getValueType(0) == MVT::i64 && 533193323Sed (N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) && 534193323Sed "Unknown operand to lower!"); 535193323Sed DebugLoc dl = N->getDebugLoc(); 536193323Sed 537193323Sed // Extract components 538193323Sed SDValue LHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 539193323Sed N->getOperand(0), DAG.getConstant(0, MVT::i32)); 540193323Sed SDValue LHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 541193323Sed N->getOperand(0), DAG.getConstant(1, MVT::i32)); 542193323Sed SDValue RHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 543193323Sed N->getOperand(1), DAG.getConstant(0, MVT::i32)); 544193323Sed SDValue RHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 545193323Sed N->getOperand(1), DAG.getConstant(1, MVT::i32)); 546193323Sed 547193323Sed // Expand 548193323Sed unsigned Opcode = (N->getOpcode() == ISD::ADD) ? XCoreISD::LADD : 549193323Sed XCoreISD::LSUB; 550193323Sed SDValue Zero = DAG.getConstant(0, MVT::i32); 551193323Sed SDValue Carry = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32), 552193323Sed LHSL, RHSL, Zero); 553193323Sed SDValue Lo(Carry.getNode(), 1); 554193323Sed 555193323Sed SDValue Ignored = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32), 556193323Sed LHSH, RHSH, Carry); 557193323Sed SDValue Hi(Ignored.getNode(), 1); 558193323Sed // Merge the pieces 559193323Sed return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 560193323Sed} 561193323Sed 562193323SedSDValue XCoreTargetLowering:: 563193323SedLowerVAARG(SDValue Op, SelectionDAG &DAG) 564193323Sed{ 565198090Srdivacky llvm_unreachable("unimplemented"); 566193323Sed // FIX Arguments passed by reference need a extra dereference. 567193323Sed SDNode *Node = Op.getNode(); 568193323Sed DebugLoc dl = Node->getDebugLoc(); 569193323Sed const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); 570198090Srdivacky EVT VT = Node->getValueType(0); 571193323Sed SDValue VAList = DAG.getLoad(getPointerTy(), dl, Node->getOperand(0), 572203954Srdivacky Node->getOperand(1), V, 0, false, false, 0); 573193323Sed // Increment the pointer, VAList, to the next vararg 574193323Sed SDValue Tmp3 = DAG.getNode(ISD::ADD, dl, getPointerTy(), VAList, 575193323Sed DAG.getConstant(VT.getSizeInBits(), 576193323Sed getPointerTy())); 577193323Sed // Store the incremented VAList to the legalized pointer 578203954Srdivacky Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Node->getOperand(1), V, 0, 579203954Srdivacky false, false, 0); 580193323Sed // Load the actual argument out of the pointer VAList 581203954Srdivacky return DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0, false, false, 0); 582193323Sed} 583193323Sed 584193323SedSDValue XCoreTargetLowering:: 585193323SedLowerVASTART(SDValue Op, SelectionDAG &DAG) 586193323Sed{ 587193323Sed DebugLoc dl = Op.getDebugLoc(); 588193323Sed // vastart stores the address of the VarArgsFrameIndex slot into the 589193323Sed // memory location argument 590193323Sed MachineFunction &MF = DAG.getMachineFunction(); 591193323Sed XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>(); 592193323Sed SDValue Addr = DAG.getFrameIndex(XFI->getVarArgsFrameIndex(), MVT::i32); 593193323Sed const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 594203954Srdivacky return DAG.getStore(Op.getOperand(0), dl, Addr, Op.getOperand(1), SV, 0, 595203954Srdivacky false, false, 0); 596193323Sed} 597193323Sed 598193323SedSDValue XCoreTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) { 599193323Sed DebugLoc dl = Op.getDebugLoc(); 600193323Sed // Depths > 0 not supported yet! 601193323Sed if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0) 602193323Sed return SDValue(); 603193323Sed 604193323Sed MachineFunction &MF = DAG.getMachineFunction(); 605193323Sed const TargetRegisterInfo *RegInfo = getTargetMachine().getRegisterInfo(); 606193323Sed return DAG.getCopyFromReg(DAG.getEntryNode(), dl, 607193323Sed RegInfo->getFrameRegister(MF), MVT::i32); 608193323Sed} 609193323Sed 610193323Sed//===----------------------------------------------------------------------===// 611193323Sed// Calling Convention Implementation 612193323Sed//===----------------------------------------------------------------------===// 613193323Sed 614193323Sed#include "XCoreGenCallingConv.inc" 615193323Sed 616193323Sed//===----------------------------------------------------------------------===// 617198090Srdivacky// Call Calling Convention Implementation 618193323Sed//===----------------------------------------------------------------------===// 619193323Sed 620198090Srdivacky/// XCore call implementation 621198090SrdivackySDValue 622198090SrdivackyXCoreTargetLowering::LowerCall(SDValue Chain, SDValue Callee, 623198090Srdivacky CallingConv::ID CallConv, bool isVarArg, 624203954Srdivacky bool &isTailCall, 625198090Srdivacky const SmallVectorImpl<ISD::OutputArg> &Outs, 626198090Srdivacky const SmallVectorImpl<ISD::InputArg> &Ins, 627198090Srdivacky DebugLoc dl, SelectionDAG &DAG, 628198090Srdivacky SmallVectorImpl<SDValue> &InVals) { 629203954Srdivacky // XCore target does not yet support tail call optimization. 630203954Srdivacky isTailCall = false; 631198090Srdivacky 632193323Sed // For now, only CallingConv::C implemented 633198090Srdivacky switch (CallConv) 634193323Sed { 635193323Sed default: 636198090Srdivacky llvm_unreachable("Unsupported calling convention"); 637193323Sed case CallingConv::Fast: 638193323Sed case CallingConv::C: 639198090Srdivacky return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall, 640198090Srdivacky Outs, Ins, dl, DAG, InVals); 641193323Sed } 642193323Sed} 643193323Sed 644193323Sed/// LowerCCCCallTo - functions arguments are copied from virtual 645193323Sed/// regs to (physical regs)/(stack frame), CALLSEQ_START and 646193323Sed/// CALLSEQ_END are emitted. 647193323Sed/// TODO: isTailCall, sret. 648198090SrdivackySDValue 649198090SrdivackyXCoreTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee, 650198090Srdivacky CallingConv::ID CallConv, bool isVarArg, 651198090Srdivacky bool isTailCall, 652198090Srdivacky const SmallVectorImpl<ISD::OutputArg> &Outs, 653198090Srdivacky const SmallVectorImpl<ISD::InputArg> &Ins, 654198090Srdivacky DebugLoc dl, SelectionDAG &DAG, 655198090Srdivacky SmallVectorImpl<SDValue> &InVals) { 656193323Sed 657193323Sed // Analyze operands of the call, assigning locations to each operand. 658193323Sed SmallVector<CCValAssign, 16> ArgLocs; 659198090Srdivacky CCState CCInfo(CallConv, isVarArg, getTargetMachine(), 660198090Srdivacky ArgLocs, *DAG.getContext()); 661193323Sed 662193323Sed // The ABI dictates there should be one stack slot available to the callee 663193323Sed // on function entry (for saving lr). 664193323Sed CCInfo.AllocateStack(4, 4); 665193323Sed 666198090Srdivacky CCInfo.AnalyzeCallOperands(Outs, CC_XCore); 667193323Sed 668193323Sed // Get a count of how many bytes are to be pushed on the stack. 669193323Sed unsigned NumBytes = CCInfo.getNextStackOffset(); 670193323Sed 671193323Sed Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, 672193323Sed getPointerTy(), true)); 673193323Sed 674193323Sed SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass; 675193323Sed SmallVector<SDValue, 12> MemOpChains; 676193323Sed 677193323Sed // Walk the register/memloc assignments, inserting copies/loads. 678193323Sed for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 679193323Sed CCValAssign &VA = ArgLocs[i]; 680198090Srdivacky SDValue Arg = Outs[i].Val; 681193323Sed 682193323Sed // Promote the value if needed. 683193323Sed switch (VA.getLocInfo()) { 684198090Srdivacky default: llvm_unreachable("Unknown loc info!"); 685193323Sed case CCValAssign::Full: break; 686193323Sed case CCValAssign::SExt: 687193323Sed Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 688193323Sed break; 689193323Sed case CCValAssign::ZExt: 690193323Sed Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 691193323Sed break; 692193323Sed case CCValAssign::AExt: 693193323Sed Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 694193323Sed break; 695193323Sed } 696193323Sed 697193323Sed // Arguments that can be passed on register must be kept at 698193323Sed // RegsToPass vector 699193323Sed if (VA.isRegLoc()) { 700193323Sed RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 701193323Sed } else { 702193323Sed assert(VA.isMemLoc()); 703193323Sed 704193323Sed int Offset = VA.getLocMemOffset(); 705193323Sed 706193323Sed MemOpChains.push_back(DAG.getNode(XCoreISD::STWSP, dl, MVT::Other, 707193323Sed Chain, Arg, 708193323Sed DAG.getConstant(Offset/4, MVT::i32))); 709193323Sed } 710193323Sed } 711193323Sed 712193323Sed // Transform all store nodes into one single node because 713193323Sed // all store nodes are independent of each other. 714193323Sed if (!MemOpChains.empty()) 715193323Sed Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 716193323Sed &MemOpChains[0], MemOpChains.size()); 717193323Sed 718193323Sed // Build a sequence of copy-to-reg nodes chained together with token 719193323Sed // chain and flag operands which copy the outgoing args into registers. 720193323Sed // The InFlag in necessary since all emited instructions must be 721193323Sed // stuck together. 722193323Sed SDValue InFlag; 723193323Sed for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 724193323Sed Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 725193323Sed RegsToPass[i].second, InFlag); 726193323Sed InFlag = Chain.getValue(1); 727193323Sed } 728193323Sed 729193323Sed // If the callee is a GlobalAddress node (quite common, every direct call is) 730193323Sed // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 731193323Sed // Likewise ExternalSymbol -> TargetExternalSymbol. 732193323Sed if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 733193323Sed Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32); 734193323Sed else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) 735193323Sed Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32); 736193323Sed 737193323Sed // XCoreBranchLink = #chain, #target_address, #opt_in_flags... 738193323Sed // = Chain, Callee, Reg#1, Reg#2, ... 739193323Sed // 740193323Sed // Returns a chain & a flag for retval copy to use. 741193323Sed SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 742193323Sed SmallVector<SDValue, 8> Ops; 743193323Sed Ops.push_back(Chain); 744193323Sed Ops.push_back(Callee); 745193323Sed 746193323Sed // Add argument registers to the end of the list so that they are 747193323Sed // known live into the call. 748193323Sed for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 749193323Sed Ops.push_back(DAG.getRegister(RegsToPass[i].first, 750193323Sed RegsToPass[i].second.getValueType())); 751193323Sed 752193323Sed if (InFlag.getNode()) 753193323Sed Ops.push_back(InFlag); 754193323Sed 755193323Sed Chain = DAG.getNode(XCoreISD::BL, dl, NodeTys, &Ops[0], Ops.size()); 756193323Sed InFlag = Chain.getValue(1); 757193323Sed 758193323Sed // Create the CALLSEQ_END node. 759193323Sed Chain = DAG.getCALLSEQ_END(Chain, 760193323Sed DAG.getConstant(NumBytes, getPointerTy(), true), 761193323Sed DAG.getConstant(0, getPointerTy(), true), 762193323Sed InFlag); 763193323Sed InFlag = Chain.getValue(1); 764193323Sed 765193323Sed // Handle result values, copying them out of physregs into vregs that we 766193323Sed // return. 767198090Srdivacky return LowerCallResult(Chain, InFlag, CallConv, isVarArg, 768198090Srdivacky Ins, dl, DAG, InVals); 769193323Sed} 770193323Sed 771198090Srdivacky/// LowerCallResult - Lower the result values of a call into the 772198090Srdivacky/// appropriate copies out of appropriate physical registers. 773198090SrdivackySDValue 774198090SrdivackyXCoreTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 775198090Srdivacky CallingConv::ID CallConv, bool isVarArg, 776198090Srdivacky const SmallVectorImpl<ISD::InputArg> &Ins, 777198090Srdivacky DebugLoc dl, SelectionDAG &DAG, 778198090Srdivacky SmallVectorImpl<SDValue> &InVals) { 779193323Sed 780193323Sed // Assign locations to each value returned by this call. 781193323Sed SmallVector<CCValAssign, 16> RVLocs; 782198090Srdivacky CCState CCInfo(CallConv, isVarArg, getTargetMachine(), 783198090Srdivacky RVLocs, *DAG.getContext()); 784193323Sed 785198090Srdivacky CCInfo.AnalyzeCallResult(Ins, RetCC_XCore); 786193323Sed 787193323Sed // Copy all of the result registers out of their specified physreg. 788193323Sed for (unsigned i = 0; i != RVLocs.size(); ++i) { 789193323Sed Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(), 790193323Sed RVLocs[i].getValVT(), InFlag).getValue(1); 791193323Sed InFlag = Chain.getValue(2); 792198090Srdivacky InVals.push_back(Chain.getValue(0)); 793193323Sed } 794193323Sed 795198090Srdivacky return Chain; 796193323Sed} 797193323Sed 798193323Sed//===----------------------------------------------------------------------===// 799198090Srdivacky// Formal Arguments Calling Convention Implementation 800193323Sed//===----------------------------------------------------------------------===// 801193323Sed 802198090Srdivacky/// XCore formal arguments implementation 803198090SrdivackySDValue 804198090SrdivackyXCoreTargetLowering::LowerFormalArguments(SDValue Chain, 805198090Srdivacky CallingConv::ID CallConv, 806198090Srdivacky bool isVarArg, 807198090Srdivacky const SmallVectorImpl<ISD::InputArg> &Ins, 808198090Srdivacky DebugLoc dl, 809198090Srdivacky SelectionDAG &DAG, 810198090Srdivacky SmallVectorImpl<SDValue> &InVals) { 811198090Srdivacky switch (CallConv) 812193323Sed { 813193323Sed default: 814198090Srdivacky llvm_unreachable("Unsupported calling convention"); 815193323Sed case CallingConv::C: 816193323Sed case CallingConv::Fast: 817198090Srdivacky return LowerCCCArguments(Chain, CallConv, isVarArg, 818198090Srdivacky Ins, dl, DAG, InVals); 819193323Sed } 820193323Sed} 821193323Sed 822193323Sed/// LowerCCCArguments - transform physical registers into 823193323Sed/// virtual registers and generate load operations for 824193323Sed/// arguments places on the stack. 825193323Sed/// TODO: sret 826198090SrdivackySDValue 827198090SrdivackyXCoreTargetLowering::LowerCCCArguments(SDValue Chain, 828198090Srdivacky CallingConv::ID CallConv, 829198090Srdivacky bool isVarArg, 830198090Srdivacky const SmallVectorImpl<ISD::InputArg> 831198090Srdivacky &Ins, 832198090Srdivacky DebugLoc dl, 833198090Srdivacky SelectionDAG &DAG, 834198090Srdivacky SmallVectorImpl<SDValue> &InVals) { 835193323Sed MachineFunction &MF = DAG.getMachineFunction(); 836193323Sed MachineFrameInfo *MFI = MF.getFrameInfo(); 837193323Sed MachineRegisterInfo &RegInfo = MF.getRegInfo(); 838193323Sed 839193323Sed // Assign locations to all of the incoming arguments. 840193323Sed SmallVector<CCValAssign, 16> ArgLocs; 841198090Srdivacky CCState CCInfo(CallConv, isVarArg, getTargetMachine(), 842198090Srdivacky ArgLocs, *DAG.getContext()); 843193323Sed 844198090Srdivacky CCInfo.AnalyzeFormalArguments(Ins, CC_XCore); 845193323Sed 846193323Sed unsigned StackSlotSize = XCoreFrameInfo::stackSlotSize(); 847193323Sed 848193323Sed unsigned LRSaveSize = StackSlotSize; 849193323Sed 850193323Sed for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 851193323Sed 852193323Sed CCValAssign &VA = ArgLocs[i]; 853193323Sed 854193323Sed if (VA.isRegLoc()) { 855193323Sed // Arguments passed in registers 856198090Srdivacky EVT RegVT = VA.getLocVT(); 857198090Srdivacky switch (RegVT.getSimpleVT().SimpleTy) { 858193323Sed default: 859198090Srdivacky { 860198090Srdivacky#ifndef NDEBUG 861198090Srdivacky errs() << "LowerFormalArguments Unhandled argument type: " 862198090Srdivacky << RegVT.getSimpleVT().SimpleTy << "\n"; 863198090Srdivacky#endif 864198090Srdivacky llvm_unreachable(0); 865198090Srdivacky } 866193323Sed case MVT::i32: 867193323Sed unsigned VReg = RegInfo.createVirtualRegister( 868193323Sed XCore::GRRegsRegisterClass); 869193323Sed RegInfo.addLiveIn(VA.getLocReg(), VReg); 870198090Srdivacky InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT)); 871193323Sed } 872193323Sed } else { 873193323Sed // sanity check 874193323Sed assert(VA.isMemLoc()); 875193323Sed // Load the argument to a virtual register 876193323Sed unsigned ObjSize = VA.getLocVT().getSizeInBits()/8; 877193323Sed if (ObjSize > StackSlotSize) { 878198090Srdivacky errs() << "LowerFormalArguments Unhandled argument type: " 879198090Srdivacky << (unsigned)VA.getLocVT().getSimpleVT().SimpleTy 880198090Srdivacky << "\n"; 881193323Sed } 882193323Sed // Create the frame index object for this incoming parameter... 883193323Sed int FI = MFI->CreateFixedObject(ObjSize, 884199481Srdivacky LRSaveSize + VA.getLocMemOffset(), 885199481Srdivacky true, false); 886193323Sed 887193323Sed // Create the SelectionDAG nodes corresponding to a load 888193323Sed //from this parameter 889193323Sed SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); 890203954Srdivacky InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, NULL, 0, 891203954Srdivacky false, false, 0)); 892193323Sed } 893193323Sed } 894193323Sed 895193323Sed if (isVarArg) { 896193323Sed /* Argument registers */ 897193323Sed static const unsigned ArgRegs[] = { 898193323Sed XCore::R0, XCore::R1, XCore::R2, XCore::R3 899193323Sed }; 900193323Sed XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>(); 901193323Sed unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs, 902193323Sed array_lengthof(ArgRegs)); 903193323Sed if (FirstVAReg < array_lengthof(ArgRegs)) { 904193323Sed SmallVector<SDValue, 4> MemOps; 905193323Sed int offset = 0; 906193323Sed // Save remaining registers, storing higher register numbers at a higher 907193323Sed // address 908193323Sed for (unsigned i = array_lengthof(ArgRegs) - 1; i >= FirstVAReg; --i) { 909193323Sed // Create a stack slot 910199481Srdivacky int FI = MFI->CreateFixedObject(4, offset, true, false); 911193323Sed if (i == FirstVAReg) { 912193323Sed XFI->setVarArgsFrameIndex(FI); 913193323Sed } 914193323Sed offset -= StackSlotSize; 915193323Sed SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); 916193323Sed // Move argument from phys reg -> virt reg 917193323Sed unsigned VReg = RegInfo.createVirtualRegister( 918193323Sed XCore::GRRegsRegisterClass); 919193323Sed RegInfo.addLiveIn(ArgRegs[i], VReg); 920198090Srdivacky SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 921193323Sed // Move argument from virt reg -> stack 922203954Srdivacky SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0, 923203954Srdivacky false, false, 0); 924193323Sed MemOps.push_back(Store); 925193323Sed } 926193323Sed if (!MemOps.empty()) 927198090Srdivacky Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 928198090Srdivacky &MemOps[0], MemOps.size()); 929193323Sed } else { 930193323Sed // This will point to the next argument passed via stack. 931193323Sed XFI->setVarArgsFrameIndex( 932199481Srdivacky MFI->CreateFixedObject(4, LRSaveSize + CCInfo.getNextStackOffset(), 933199481Srdivacky true, false)); 934193323Sed } 935193323Sed } 936193323Sed 937198090Srdivacky return Chain; 938193323Sed} 939193323Sed 940193323Sed//===----------------------------------------------------------------------===// 941193323Sed// Return Value Calling Convention Implementation 942193323Sed//===----------------------------------------------------------------------===// 943193323Sed 944199481Srdivackybool XCoreTargetLowering:: 945199481SrdivackyCanLowerReturn(CallingConv::ID CallConv, bool isVarArg, 946199481Srdivacky const SmallVectorImpl<EVT> &OutTys, 947199481Srdivacky const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags, 948199481Srdivacky SelectionDAG &DAG) { 949199481Srdivacky SmallVector<CCValAssign, 16> RVLocs; 950199481Srdivacky CCState CCInfo(CallConv, isVarArg, getTargetMachine(), 951199481Srdivacky RVLocs, *DAG.getContext()); 952199481Srdivacky return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_XCore); 953199481Srdivacky} 954199481Srdivacky 955198090SrdivackySDValue 956198090SrdivackyXCoreTargetLowering::LowerReturn(SDValue Chain, 957198090Srdivacky CallingConv::ID CallConv, bool isVarArg, 958198090Srdivacky const SmallVectorImpl<ISD::OutputArg> &Outs, 959198090Srdivacky DebugLoc dl, SelectionDAG &DAG) { 960198090Srdivacky 961193323Sed // CCValAssign - represent the assignment of 962193323Sed // the return value to a location 963193323Sed SmallVector<CCValAssign, 16> RVLocs; 964193323Sed 965193323Sed // CCState - Info about the registers and stack slot. 966198090Srdivacky CCState CCInfo(CallConv, isVarArg, getTargetMachine(), 967198090Srdivacky RVLocs, *DAG.getContext()); 968193323Sed 969198090Srdivacky // Analize return values. 970198090Srdivacky CCInfo.AnalyzeReturn(Outs, RetCC_XCore); 971193323Sed 972193323Sed // If this is the first return lowered for this function, add 973193323Sed // the regs to the liveout set for the function. 974193323Sed if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { 975193323Sed for (unsigned i = 0; i != RVLocs.size(); ++i) 976193323Sed if (RVLocs[i].isRegLoc()) 977193323Sed DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); 978193323Sed } 979193323Sed 980193323Sed SDValue Flag; 981193323Sed 982193323Sed // Copy the result values into the output registers. 983193323Sed for (unsigned i = 0; i != RVLocs.size(); ++i) { 984193323Sed CCValAssign &VA = RVLocs[i]; 985193323Sed assert(VA.isRegLoc() && "Can only return in registers!"); 986193323Sed 987193323Sed Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 988198090Srdivacky Outs[i].Val, Flag); 989193323Sed 990193323Sed // guarantee that all emitted copies are 991193323Sed // stuck together, avoiding something bad 992193323Sed Flag = Chain.getValue(1); 993193323Sed } 994193323Sed 995193323Sed // Return on XCore is always a "retsp 0" 996193323Sed if (Flag.getNode()) 997193323Sed return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other, 998193323Sed Chain, DAG.getConstant(0, MVT::i32), Flag); 999193323Sed else // Return Void 1000193323Sed return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other, 1001193323Sed Chain, DAG.getConstant(0, MVT::i32)); 1002193323Sed} 1003193323Sed 1004193323Sed//===----------------------------------------------------------------------===// 1005193323Sed// Other Lowering Code 1006193323Sed//===----------------------------------------------------------------------===// 1007193323Sed 1008193323SedMachineBasicBlock * 1009193323SedXCoreTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 1010198090Srdivacky MachineBasicBlock *BB, 1011198090Srdivacky DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const { 1012193323Sed const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo(); 1013193323Sed DebugLoc dl = MI->getDebugLoc(); 1014193323Sed assert((MI->getOpcode() == XCore::SELECT_CC) && 1015193323Sed "Unexpected instr type to insert"); 1016193323Sed 1017193323Sed // To "insert" a SELECT_CC instruction, we actually have to insert the diamond 1018193323Sed // control-flow pattern. The incoming instruction knows the destination vreg 1019193323Sed // to set, the condition code register to branch on, the true/false values to 1020193323Sed // select between, and a branch opcode to use. 1021193323Sed const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1022193323Sed MachineFunction::iterator It = BB; 1023193323Sed ++It; 1024193323Sed 1025193323Sed // thisMBB: 1026193323Sed // ... 1027193323Sed // TrueVal = ... 1028193323Sed // cmpTY ccX, r1, r2 1029193323Sed // bCC copy1MBB 1030193323Sed // fallthrough --> copy0MBB 1031193323Sed MachineBasicBlock *thisMBB = BB; 1032193323Sed MachineFunction *F = BB->getParent(); 1033193323Sed MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 1034193323Sed MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 1035193323Sed BuildMI(BB, dl, TII.get(XCore::BRFT_lru6)) 1036193323Sed .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB); 1037193323Sed F->insert(It, copy0MBB); 1038193323Sed F->insert(It, sinkMBB); 1039198090Srdivacky // Update machine-CFG edges by first adding all successors of the current 1040193323Sed // block to the new block which will contain the Phi node for the select. 1041198090Srdivacky // Also inform sdisel of the edge changes. 1042198090Srdivacky for (MachineBasicBlock::succ_iterator I = BB->succ_begin(), 1043198090Srdivacky E = BB->succ_end(); I != E; ++I) { 1044198090Srdivacky EM->insert(std::make_pair(*I, sinkMBB)); 1045198090Srdivacky sinkMBB->addSuccessor(*I); 1046198090Srdivacky } 1047198090Srdivacky // Next, remove all successors of the current block, and add the true 1048198090Srdivacky // and fallthrough blocks as its successors. 1049198090Srdivacky while (!BB->succ_empty()) 1050198090Srdivacky BB->removeSuccessor(BB->succ_begin()); 1051193323Sed // Next, add the true and fallthrough blocks as its successors. 1052193323Sed BB->addSuccessor(copy0MBB); 1053193323Sed BB->addSuccessor(sinkMBB); 1054193323Sed 1055193323Sed // copy0MBB: 1056193323Sed // %FalseValue = ... 1057193323Sed // # fallthrough to sinkMBB 1058193323Sed BB = copy0MBB; 1059193323Sed 1060193323Sed // Update machine-CFG edges 1061193323Sed BB->addSuccessor(sinkMBB); 1062193323Sed 1063193323Sed // sinkMBB: 1064193323Sed // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 1065193323Sed // ... 1066193323Sed BB = sinkMBB; 1067193323Sed BuildMI(BB, dl, TII.get(XCore::PHI), MI->getOperand(0).getReg()) 1068193323Sed .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB) 1069193323Sed .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 1070193323Sed 1071193323Sed F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. 1072193323Sed return BB; 1073193323Sed} 1074193323Sed 1075193323Sed//===----------------------------------------------------------------------===// 1076198090Srdivacky// Target Optimization Hooks 1077198090Srdivacky//===----------------------------------------------------------------------===// 1078198090Srdivacky 1079198090SrdivackySDValue XCoreTargetLowering::PerformDAGCombine(SDNode *N, 1080198090Srdivacky DAGCombinerInfo &DCI) const { 1081198090Srdivacky SelectionDAG &DAG = DCI.DAG; 1082198090Srdivacky DebugLoc dl = N->getDebugLoc(); 1083198090Srdivacky switch (N->getOpcode()) { 1084198090Srdivacky default: break; 1085198090Srdivacky case ISD::STORE: { 1086198090Srdivacky // Replace unaligned store of unaligned load with memmove. 1087198090Srdivacky StoreSDNode *ST = cast<StoreSDNode>(N); 1088198090Srdivacky if (!DCI.isBeforeLegalize() || 1089198090Srdivacky allowsUnalignedMemoryAccesses(ST->getMemoryVT()) || 1090198090Srdivacky ST->isVolatile() || ST->isIndexed()) { 1091198090Srdivacky break; 1092198090Srdivacky } 1093198090Srdivacky SDValue Chain = ST->getChain(); 1094198090Srdivacky 1095198090Srdivacky unsigned StoreBits = ST->getMemoryVT().getStoreSizeInBits(); 1096198090Srdivacky if (StoreBits % 8) { 1097198090Srdivacky break; 1098198090Srdivacky } 1099198090Srdivacky unsigned ABIAlignment = getTargetData()->getABITypeAlignment( 1100198090Srdivacky ST->getMemoryVT().getTypeForEVT(*DCI.DAG.getContext())); 1101198090Srdivacky unsigned Alignment = ST->getAlignment(); 1102198090Srdivacky if (Alignment >= ABIAlignment) { 1103198090Srdivacky break; 1104198090Srdivacky } 1105198090Srdivacky 1106198090Srdivacky if (LoadSDNode *LD = dyn_cast<LoadSDNode>(ST->getValue())) { 1107198090Srdivacky if (LD->hasNUsesOfValue(1, 0) && ST->getMemoryVT() == LD->getMemoryVT() && 1108198090Srdivacky LD->getAlignment() == Alignment && 1109198090Srdivacky !LD->isVolatile() && !LD->isIndexed() && 1110198090Srdivacky Chain.reachesChainWithoutSideEffects(SDValue(LD, 1))) { 1111198090Srdivacky return DAG.getMemmove(Chain, dl, ST->getBasePtr(), 1112198090Srdivacky LD->getBasePtr(), 1113198090Srdivacky DAG.getConstant(StoreBits/8, MVT::i32), 1114198090Srdivacky Alignment, ST->getSrcValue(), 1115198090Srdivacky ST->getSrcValueOffset(), LD->getSrcValue(), 1116198090Srdivacky LD->getSrcValueOffset()); 1117198090Srdivacky } 1118198090Srdivacky } 1119198090Srdivacky break; 1120198090Srdivacky } 1121198090Srdivacky } 1122198090Srdivacky return SDValue(); 1123198090Srdivacky} 1124198090Srdivacky 1125198090Srdivacky//===----------------------------------------------------------------------===// 1126193323Sed// Addressing mode description hooks 1127193323Sed//===----------------------------------------------------------------------===// 1128193323Sed 1129193323Sedstatic inline bool isImmUs(int64_t val) 1130193323Sed{ 1131193323Sed return (val >= 0 && val <= 11); 1132193323Sed} 1133193323Sed 1134193323Sedstatic inline bool isImmUs2(int64_t val) 1135193323Sed{ 1136193323Sed return (val%2 == 0 && isImmUs(val/2)); 1137193323Sed} 1138193323Sed 1139193323Sedstatic inline bool isImmUs4(int64_t val) 1140193323Sed{ 1141193323Sed return (val%4 == 0 && isImmUs(val/4)); 1142193323Sed} 1143193323Sed 1144193323Sed/// isLegalAddressingMode - Return true if the addressing mode represented 1145193323Sed/// by AM is legal for this target, for a load/store of the specified type. 1146193323Sedbool 1147193323SedXCoreTargetLowering::isLegalAddressingMode(const AddrMode &AM, 1148193323Sed const Type *Ty) const { 1149198090Srdivacky // Be conservative with void 1150198090Srdivacky // FIXME: Can we be more aggressive? 1151198090Srdivacky if (Ty->getTypeID() == Type::VoidTyID) 1152198090Srdivacky return false; 1153198090Srdivacky 1154198090Srdivacky const TargetData *TD = TM.getTargetData(); 1155198090Srdivacky unsigned Size = TD->getTypeAllocSize(Ty); 1156193323Sed if (AM.BaseGV) { 1157198090Srdivacky return Size >= 4 && !AM.HasBaseReg && AM.Scale == 0 && 1158193323Sed AM.BaseOffs%4 == 0; 1159193323Sed } 1160193323Sed 1161198090Srdivacky switch (Size) { 1162198090Srdivacky case 1: 1163193323Sed // reg + imm 1164193323Sed if (AM.Scale == 0) { 1165193323Sed return isImmUs(AM.BaseOffs); 1166193323Sed } 1167198090Srdivacky // reg + reg 1168193323Sed return AM.Scale == 1 && AM.BaseOffs == 0; 1169198090Srdivacky case 2: 1170198090Srdivacky case 3: 1171193323Sed // reg + imm 1172193323Sed if (AM.Scale == 0) { 1173193323Sed return isImmUs2(AM.BaseOffs); 1174193323Sed } 1175198090Srdivacky // reg + reg<<1 1176193323Sed return AM.Scale == 2 && AM.BaseOffs == 0; 1177198090Srdivacky default: 1178193323Sed // reg + imm 1179193323Sed if (AM.Scale == 0) { 1180193323Sed return isImmUs4(AM.BaseOffs); 1181193323Sed } 1182193323Sed // reg + reg<<2 1183193323Sed return AM.Scale == 4 && AM.BaseOffs == 0; 1184193323Sed } 1185193323Sed 1186193323Sed return false; 1187193323Sed} 1188193323Sed 1189193323Sed//===----------------------------------------------------------------------===// 1190193323Sed// XCore Inline Assembly Support 1191193323Sed//===----------------------------------------------------------------------===// 1192193323Sed 1193193323Sedstd::vector<unsigned> XCoreTargetLowering:: 1194193323SedgetRegClassForInlineAsmConstraint(const std::string &Constraint, 1195198090Srdivacky EVT VT) const 1196193323Sed{ 1197193323Sed if (Constraint.size() != 1) 1198193323Sed return std::vector<unsigned>(); 1199193323Sed 1200193323Sed switch (Constraint[0]) { 1201193323Sed default : break; 1202193323Sed case 'r': 1203193323Sed return make_vector<unsigned>(XCore::R0, XCore::R1, XCore::R2, 1204193323Sed XCore::R3, XCore::R4, XCore::R5, 1205193323Sed XCore::R6, XCore::R7, XCore::R8, 1206193323Sed XCore::R9, XCore::R10, XCore::R11, 0); 1207193323Sed break; 1208193323Sed } 1209193323Sed return std::vector<unsigned>(); 1210193323Sed} 1211