LLVM API Documentation
00001 //===-- AlphaISelLowering.cpp - Alpha 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 AlphaISelLowering class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "AlphaISelLowering.h" 00015 #include "AlphaTargetMachine.h" 00016 #include "llvm/CodeGen/CallingConvLower.h" 00017 #include "llvm/CodeGen/MachineFrameInfo.h" 00018 #include "llvm/CodeGen/MachineFunction.h" 00019 #include "llvm/CodeGen/MachineInstrBuilder.h" 00020 #include "llvm/CodeGen/MachineRegisterInfo.h" 00021 #include "llvm/CodeGen/SelectionDAG.h" 00022 #include "llvm/CodeGen/MachineRegisterInfo.h" 00023 #include "llvm/CodeGen/PseudoSourceValue.h" 00024 #include "llvm/Target/TargetLoweringObjectFile.h" 00025 #include "llvm/Constants.h" 00026 #include "llvm/Function.h" 00027 #include "llvm/Module.h" 00028 #include "llvm/Intrinsics.h" 00029 #include "llvm/Support/CommandLine.h" 00030 #include "llvm/Support/ErrorHandling.h" 00031 #include "llvm/Support/raw_ostream.h" 00032 using namespace llvm; 00033 00034 /// AddLiveIn - This helper function adds the specified physical register to the 00035 /// MachineFunction as a live in value. It also creates a corresponding virtual 00036 /// register for it. 00037 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, 00038 TargetRegisterClass *RC) { 00039 assert(RC->contains(PReg) && "Not the correct regclass!"); 00040 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC); 00041 MF.getRegInfo().addLiveIn(PReg, VReg); 00042 return VReg; 00043 } 00044 00045 AlphaTargetLowering::AlphaTargetLowering(TargetMachine &TM) 00046 : TargetLowering(TM, new TargetLoweringObjectFileELF()) { 00047 // Set up the TargetLowering object. 00048 //I am having problems with shr n i8 1 00049 setShiftAmountType(MVT::i64); 00050 setBooleanContents(ZeroOrOneBooleanContent); 00051 00052 setUsesGlobalOffsetTable(true); 00053 00054 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass); 00055 addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass); 00056 addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass); 00057 00058 // We want to custom lower some of our intrinsics. 00059 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 00060 00061 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); 00062 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 00063 00064 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); 00065 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand); 00066 00067 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 00068 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand); 00069 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand); 00070 00071 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 00072 00073 // setOperationAction(ISD::BRIND, MVT::Other, Expand); 00074 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 00075 setOperationAction(ISD::BR_CC, MVT::Other, Expand); 00076 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); 00077 00078 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 00079 00080 setOperationAction(ISD::FREM, MVT::f32, Expand); 00081 setOperationAction(ISD::FREM, MVT::f64, Expand); 00082 00083 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 00084 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 00085 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); 00086 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 00087 00088 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) { 00089 setOperationAction(ISD::CTPOP , MVT::i64 , Expand); 00090 setOperationAction(ISD::CTTZ , MVT::i64 , Expand); 00091 setOperationAction(ISD::CTLZ , MVT::i64 , Expand); 00092 } 00093 setOperationAction(ISD::BSWAP , MVT::i64, Expand); 00094 setOperationAction(ISD::ROTL , MVT::i64, Expand); 00095 setOperationAction(ISD::ROTR , MVT::i64, Expand); 00096 00097 setOperationAction(ISD::SREM , MVT::i64, Custom); 00098 setOperationAction(ISD::UREM , MVT::i64, Custom); 00099 setOperationAction(ISD::SDIV , MVT::i64, Custom); 00100 setOperationAction(ISD::UDIV , MVT::i64, Custom); 00101 00102 setOperationAction(ISD::ADDC , MVT::i64, Expand); 00103 setOperationAction(ISD::ADDE , MVT::i64, Expand); 00104 setOperationAction(ISD::SUBC , MVT::i64, Expand); 00105 setOperationAction(ISD::SUBE , MVT::i64, Expand); 00106 00107 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); 00108 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); 00109 00110 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); 00111 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); 00112 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); 00113 00114 // We don't support sin/cos/sqrt/pow 00115 setOperationAction(ISD::FSIN , MVT::f64, Expand); 00116 setOperationAction(ISD::FCOS , MVT::f64, Expand); 00117 setOperationAction(ISD::FSIN , MVT::f32, Expand); 00118 setOperationAction(ISD::FCOS , MVT::f32, Expand); 00119 00120 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 00121 setOperationAction(ISD::FSQRT, MVT::f32, Expand); 00122 00123 setOperationAction(ISD::FPOW , MVT::f32, Expand); 00124 setOperationAction(ISD::FPOW , MVT::f64, Expand); 00125 00126 setOperationAction(ISD::SETCC, MVT::f32, Promote); 00127 00128 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Promote); 00129 00130 // We don't have line number support yet. 00131 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand); 00132 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); 00133 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand); 00134 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); 00135 00136 // Not implemented yet. 00137 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 00138 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 00139 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); 00140 00141 // We want to legalize GlobalAddress and ConstantPool and 00142 // ExternalSymbols nodes into the appropriate instructions to 00143 // materialize the address. 00144 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 00145 setOperationAction(ISD::ConstantPool, MVT::i64, Custom); 00146 setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom); 00147 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 00148 00149 setOperationAction(ISD::VASTART, MVT::Other, Custom); 00150 setOperationAction(ISD::VAEND, MVT::Other, Expand); 00151 setOperationAction(ISD::VACOPY, MVT::Other, Custom); 00152 setOperationAction(ISD::VAARG, MVT::Other, Custom); 00153 setOperationAction(ISD::VAARG, MVT::i32, Custom); 00154 00155 setOperationAction(ISD::JumpTable, MVT::i64, Custom); 00156 setOperationAction(ISD::JumpTable, MVT::i32, Custom); 00157 00158 setStackPointerRegisterToSaveRestore(Alpha::R30); 00159 00160 addLegalFPImmediate(APFloat(+0.0)); //F31 00161 addLegalFPImmediate(APFloat(+0.0f)); //F31 00162 addLegalFPImmediate(APFloat(-0.0)); //-F31 00163 addLegalFPImmediate(APFloat(-0.0f)); //-F31 00164 00165 setJumpBufSize(272); 00166 setJumpBufAlignment(16); 00167 00168 computeRegisterProperties(); 00169 } 00170 00171 MVT::SimpleValueType AlphaTargetLowering::getSetCCResultType(EVT VT) const { 00172 return MVT::i64; 00173 } 00174 00175 const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const { 00176 switch (Opcode) { 00177 default: return 0; 00178 case AlphaISD::CVTQT_: return "Alpha::CVTQT_"; 00179 case AlphaISD::CVTQS_: return "Alpha::CVTQS_"; 00180 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_"; 00181 case AlphaISD::GPRelHi: return "Alpha::GPRelHi"; 00182 case AlphaISD::GPRelLo: return "Alpha::GPRelLo"; 00183 case AlphaISD::RelLit: return "Alpha::RelLit"; 00184 case AlphaISD::GlobalRetAddr: return "Alpha::GlobalRetAddr"; 00185 case AlphaISD::CALL: return "Alpha::CALL"; 00186 case AlphaISD::DivCall: return "Alpha::DivCall"; 00187 case AlphaISD::RET_FLAG: return "Alpha::RET_FLAG"; 00188 case AlphaISD::COND_BRANCH_I: return "Alpha::COND_BRANCH_I"; 00189 case AlphaISD::COND_BRANCH_F: return "Alpha::COND_BRANCH_F"; 00190 } 00191 } 00192 00193 /// getFunctionAlignment - Return the Log2 alignment of this function. 00194 unsigned AlphaTargetLowering::getFunctionAlignment(const Function *F) const { 00195 return 4; 00196 } 00197 00198 static SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) { 00199 EVT PtrVT = Op.getValueType(); 00200 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 00201 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); 00202 SDValue Zero = DAG.getConstant(0, PtrVT); 00203 // FIXME there isn't really any debug info here 00204 DebugLoc dl = Op.getDebugLoc(); 00205 00206 SDValue Hi = DAG.getNode(AlphaISD::GPRelHi, dl, MVT::i64, JTI, 00207 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64)); 00208 SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, JTI, Hi); 00209 return Lo; 00210 } 00211 00212 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/ 00213 //AA-PY8AC-TET1_html/callCH3.html#BLOCK21 00214 00215 //For now, just use variable size stack frame format 00216 00217 //In a standard call, the first six items are passed in registers $16 00218 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details 00219 //of argument-to-register correspondence.) The remaining items are 00220 //collected in a memory argument list that is a naturally aligned 00221 //array of quadwords. In a standard call, this list, if present, must 00222 //be passed at 0(SP). 00223 //7 ... n 0(SP) ... (n-7)*8(SP) 00224 00225 // //#define FP $15 00226 // //#define RA $26 00227 // //#define PV $27 00228 // //#define GP $29 00229 // //#define SP $30 00230 00231 #include "AlphaGenCallingConv.inc" 00232 00233 SDValue 00234 AlphaTargetLowering::LowerCall(SDValue Chain, SDValue Callee, 00235 unsigned CallConv, bool isVarArg, 00236 bool isTailCall, 00237 const SmallVectorImpl<ISD::OutputArg> &Outs, 00238 const SmallVectorImpl<ISD::InputArg> &Ins, 00239 DebugLoc dl, SelectionDAG &DAG, 00240 SmallVectorImpl<SDValue> &InVals) { 00241 00242 // Analyze operands of the call, assigning locations to each operand. 00243 SmallVector<CCValAssign, 16> ArgLocs; 00244 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), 00245 ArgLocs, *DAG.getContext()); 00246 00247 CCInfo.AnalyzeCallOperands(Outs, CC_Alpha); 00248 00249 // Get a count of how many bytes are to be pushed on the stack. 00250 unsigned NumBytes = CCInfo.getNextStackOffset(); 00251 00252 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, 00253 getPointerTy(), true)); 00254 00255 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass; 00256 SmallVector<SDValue, 12> MemOpChains; 00257 SDValue StackPtr; 00258 00259 // Walk the register/memloc assignments, inserting copies/loads. 00260 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 00261 CCValAssign &VA = ArgLocs[i]; 00262 00263 SDValue Arg = Outs[i].Val; 00264 00265 // Promote the value if needed. 00266 switch (VA.getLocInfo()) { 00267 default: assert(0 && "Unknown loc info!"); 00268 case CCValAssign::Full: break; 00269 case CCValAssign::SExt: 00270 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 00271 break; 00272 case CCValAssign::ZExt: 00273 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 00274 break; 00275 case CCValAssign::AExt: 00276 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 00277 break; 00278 } 00279 00280 // Arguments that can be passed on register must be kept at RegsToPass 00281 // vector 00282 if (VA.isRegLoc()) { 00283 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 00284 } else { 00285 assert(VA.isMemLoc()); 00286 00287 if (StackPtr.getNode() == 0) 00288 StackPtr = DAG.getCopyFromReg(Chain, dl, Alpha::R30, MVT::i64); 00289 00290 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), 00291 StackPtr, 00292 DAG.getIntPtrConstant(VA.getLocMemOffset())); 00293 00294 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, 00295 PseudoSourceValue::getStack(), 0)); 00296 } 00297 } 00298 00299 // Transform all store nodes into one single node because all store nodes are 00300 // independent of each other. 00301 if (!MemOpChains.empty()) 00302 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00303 &MemOpChains[0], MemOpChains.size()); 00304 00305 // Build a sequence of copy-to-reg nodes chained together with token chain and 00306 // flag operands which copy the outgoing args into registers. The InFlag in 00307 // necessary since all emited instructions must be stuck together. 00308 SDValue InFlag; 00309 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 00310 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 00311 RegsToPass[i].second, InFlag); 00312 InFlag = Chain.getValue(1); 00313 } 00314 00315 // Returns a chain & a flag for retval copy to use. 00316 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 00317 SmallVector<SDValue, 8> Ops; 00318 Ops.push_back(Chain); 00319 Ops.push_back(Callee); 00320 00321 // Add argument registers to the end of the list so that they are 00322 // known live into the call. 00323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 00324 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 00325 RegsToPass[i].second.getValueType())); 00326 00327 if (InFlag.getNode()) 00328 Ops.push_back(InFlag); 00329 00330 Chain = DAG.getNode(AlphaISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); 00331 InFlag = Chain.getValue(1); 00332 00333 // Create the CALLSEQ_END node. 00334 Chain = DAG.getCALLSEQ_END(Chain, 00335 DAG.getConstant(NumBytes, getPointerTy(), true), 00336 DAG.getConstant(0, getPointerTy(), true), 00337 InFlag); 00338 InFlag = Chain.getValue(1); 00339 00340 // Handle result values, copying them out of physregs into vregs that we 00341 // return. 00342 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, 00343 Ins, dl, DAG, InVals); 00344 } 00345 00346 /// LowerCallResult - Lower the result values of a call into the 00347 /// appropriate copies out of appropriate physical registers. 00348 /// 00349 SDValue 00350 AlphaTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 00351 unsigned CallConv, bool isVarArg, 00352 const SmallVectorImpl<ISD::InputArg> &Ins, 00353 DebugLoc dl, SelectionDAG &DAG, 00354 SmallVectorImpl<SDValue> &InVals) { 00355 00356 // Assign locations to each value returned by this call. 00357 SmallVector<CCValAssign, 16> RVLocs; 00358 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs, 00359 *DAG.getContext()); 00360 00361 CCInfo.AnalyzeCallResult(Ins, RetCC_Alpha); 00362 00363 // Copy all of the result registers out of their specified physreg. 00364 for (unsigned i = 0; i != RVLocs.size(); ++i) { 00365 CCValAssign &VA = RVLocs[i]; 00366 00367 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), 00368 VA.getLocVT(), InFlag).getValue(1); 00369 SDValue RetValue = Chain.getValue(0); 00370 InFlag = Chain.getValue(2); 00371 00372 // If this is an 8/16/32-bit value, it is really passed promoted to 64 00373 // bits. Insert an assert[sz]ext to capture this, then truncate to the 00374 // right size. 00375 if (VA.getLocInfo() == CCValAssign::SExt) 00376 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue, 00377 DAG.getValueType(VA.getValVT())); 00378 else if (VA.getLocInfo() == CCValAssign::ZExt) 00379 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue, 00380 DAG.getValueType(VA.getValVT())); 00381 00382 if (VA.getLocInfo() != CCValAssign::Full) 00383 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue); 00384 00385 InVals.push_back(RetValue); 00386 } 00387 00388 return Chain; 00389 } 00390 00391 SDValue 00392 AlphaTargetLowering::LowerFormalArguments(SDValue Chain, 00393 unsigned CallConv, bool isVarArg, 00394 const SmallVectorImpl<ISD::InputArg> 00395 &Ins, 00396 DebugLoc dl, SelectionDAG &DAG, 00397 SmallVectorImpl<SDValue> &InVals) { 00398 00399 MachineFunction &MF = DAG.getMachineFunction(); 00400 MachineFrameInfo *MFI = MF.getFrameInfo(); 00401 00402 unsigned args_int[] = { 00403 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21}; 00404 unsigned args_float[] = { 00405 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21}; 00406 00407 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { 00408 SDValue argt; 00409 EVT ObjectVT = Ins[ArgNo].VT; 00410 SDValue ArgVal; 00411 00412 if (ArgNo < 6) { 00413 switch (ObjectVT.getSimpleVT().SimpleTy) { 00414 default: 00415 assert(false && "Invalid value type!"); 00416 case MVT::f64: 00417 args_float[ArgNo] = AddLiveIn(MF, args_float[ArgNo], 00418 &Alpha::F8RCRegClass); 00419 ArgVal = DAG.getCopyFromReg(Chain, dl, args_float[ArgNo], ObjectVT); 00420 break; 00421 case MVT::f32: 00422 args_float[ArgNo] = AddLiveIn(MF, args_float[ArgNo], 00423 &Alpha::F4RCRegClass); 00424 ArgVal = DAG.getCopyFromReg(Chain, dl, args_float[ArgNo], ObjectVT); 00425 break; 00426 case MVT::i64: 00427 args_int[ArgNo] = AddLiveIn(MF, args_int[ArgNo], 00428 &Alpha::GPRCRegClass); 00429 ArgVal = DAG.getCopyFromReg(Chain, dl, args_int[ArgNo], MVT::i64); 00430 break; 00431 } 00432 } else { //more args 00433 // Create the frame index object for this incoming parameter... 00434 int FI = MFI->CreateFixedObject(8, 8 * (ArgNo - 6)); 00435 00436 // Create the SelectionDAG nodes corresponding to a load 00437 //from this parameter 00438 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64); 00439 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, NULL, 0); 00440 } 00441 InVals.push_back(ArgVal); 00442 } 00443 00444 // If the functions takes variable number of arguments, copy all regs to stack 00445 if (isVarArg) { 00446 VarArgsOffset = Ins.size() * 8; 00447 std::vector<SDValue> LS; 00448 for (int i = 0; i < 6; ++i) { 00449 if (TargetRegisterInfo::isPhysicalRegister(args_int[i])) 00450 args_int[i] = AddLiveIn(MF, args_int[i], &Alpha::GPRCRegClass); 00451 SDValue argt = DAG.getCopyFromReg(Chain, dl, args_int[i], MVT::i64); 00452 int FI = MFI->CreateFixedObject(8, -8 * (6 - i)); 00453 if (i == 0) VarArgsBase = FI; 00454 SDValue SDFI = DAG.getFrameIndex(FI, MVT::i64); 00455 LS.push_back(DAG.getStore(Chain, dl, argt, SDFI, NULL, 0)); 00456 00457 if (TargetRegisterInfo::isPhysicalRegister(args_float[i])) 00458 args_float[i] = AddLiveIn(MF, args_float[i], &Alpha::F8RCRegClass); 00459 argt = DAG.getCopyFromReg(Chain, dl, args_float[i], MVT::f64); 00460 FI = MFI->CreateFixedObject(8, - 8 * (12 - i)); 00461 SDFI = DAG.getFrameIndex(FI, MVT::i64); 00462 LS.push_back(DAG.getStore(Chain, dl, argt, SDFI, NULL, 0)); 00463 } 00464 00465 //Set up a token factor with all the stack traffic 00466 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &LS[0], LS.size()); 00467 } 00468 00469 return Chain; 00470 } 00471 00472 SDValue 00473 AlphaTargetLowering::LowerReturn(SDValue Chain, 00474 unsigned CallConv, bool isVarArg, 00475 const SmallVectorImpl<ISD::OutputArg> &Outs, 00476 DebugLoc dl, SelectionDAG &DAG) { 00477 00478 SDValue Copy = DAG.getCopyToReg(Chain, dl, Alpha::R26, 00479 DAG.getNode(AlphaISD::GlobalRetAddr, 00480 DebugLoc::getUnknownLoc(), 00481 MVT::i64), 00482 SDValue()); 00483 switch (Outs.size()) { 00484 default: 00485 llvm_unreachable("Do not know how to return this many arguments!"); 00486 case 0: 00487 break; 00488 //return SDValue(); // ret void is legal 00489 case 1: { 00490 EVT ArgVT = Outs[0].Val.getValueType(); 00491 unsigned ArgReg; 00492 if (ArgVT.isInteger()) 00493 ArgReg = Alpha::R0; 00494 else { 00495 assert(ArgVT.isFloatingPoint()); 00496 ArgReg = Alpha::F0; 00497 } 00498 Copy = DAG.getCopyToReg(Copy, dl, ArgReg, 00499 Outs[0].Val, Copy.getValue(1)); 00500 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) 00501 DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg); 00502 break; 00503 } 00504 case 2: { 00505 EVT ArgVT = Outs[0].Val.getValueType(); 00506 unsigned ArgReg1, ArgReg2; 00507 if (ArgVT.isInteger()) { 00508 ArgReg1 = Alpha::R0; 00509 ArgReg2 = Alpha::R1; 00510 } else { 00511 assert(ArgVT.isFloatingPoint()); 00512 ArgReg1 = Alpha::F0; 00513 ArgReg2 = Alpha::F1; 00514 } 00515 Copy = DAG.getCopyToReg(Copy, dl, ArgReg1, 00516 Outs[0].Val, Copy.getValue(1)); 00517 if (std::find(DAG.getMachineFunction().getRegInfo().liveout_begin(), 00518 DAG.getMachineFunction().getRegInfo().liveout_end(), ArgReg1) 00519 == DAG.getMachineFunction().getRegInfo().liveout_end()) 00520 DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg1); 00521 Copy = DAG.getCopyToReg(Copy, dl, ArgReg2, 00522 Outs[1].Val, Copy.getValue(1)); 00523 if (std::find(DAG.getMachineFunction().getRegInfo().liveout_begin(), 00524 DAG.getMachineFunction().getRegInfo().liveout_end(), ArgReg2) 00525 == DAG.getMachineFunction().getRegInfo().liveout_end()) 00526 DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg2); 00527 break; 00528 } 00529 } 00530 return DAG.getNode(AlphaISD::RET_FLAG, dl, 00531 MVT::Other, Copy, Copy.getValue(1)); 00532 } 00533 00534 void AlphaTargetLowering::LowerVAARG(SDNode *N, SDValue &Chain, 00535 SDValue &DataPtr, SelectionDAG &DAG) { 00536 Chain = N->getOperand(0); 00537 SDValue VAListP = N->getOperand(1); 00538 const Value *VAListS = cast<SrcValueSDNode>(N->getOperand(2))->getValue(); 00539 DebugLoc dl = N->getDebugLoc(); 00540 00541 SDValue Base = DAG.getLoad(MVT::i64, dl, Chain, VAListP, VAListS, 0); 00542 SDValue Tmp = DAG.getNode(ISD::ADD, dl, MVT::i64, VAListP, 00543 DAG.getConstant(8, MVT::i64)); 00544 SDValue Offset = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Base.getValue(1), 00545 Tmp, NULL, 0, MVT::i32); 00546 DataPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Base, Offset); 00547 if (N->getValueType(0).isFloatingPoint()) 00548 { 00549 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr 00550 SDValue FPDataPtr = DAG.getNode(ISD::SUB, dl, MVT::i64, DataPtr, 00551 DAG.getConstant(8*6, MVT::i64)); 00552 SDValue CC = DAG.getSetCC(dl, MVT::i64, Offset, 00553 DAG.getConstant(8*6, MVT::i64), ISD::SETLT); 00554 DataPtr = DAG.getNode(ISD::SELECT, dl, MVT::i64, CC, FPDataPtr, DataPtr); 00555 } 00556 00557 SDValue NewOffset = DAG.getNode(ISD::ADD, dl, MVT::i64, Offset, 00558 DAG.getConstant(8, MVT::i64)); 00559 Chain = DAG.getTruncStore(Offset.getValue(1), dl, NewOffset, Tmp, NULL, 0, 00560 MVT::i32); 00561 } 00562 00563 /// LowerOperation - Provide custom lowering hooks for some operations. 00564 /// 00565 SDValue AlphaTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { 00566 DebugLoc dl = Op.getDebugLoc(); 00567 switch (Op.getOpcode()) { 00568 default: llvm_unreachable("Wasn't expecting to be able to lower this!"); 00569 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 00570 00571 case ISD::INTRINSIC_WO_CHAIN: { 00572 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 00573 switch (IntNo) { 00574 default: break; // Don't custom lower most intrinsics. 00575 case Intrinsic::alpha_umulh: 00576 return DAG.getNode(ISD::MULHU, dl, MVT::i64, 00577 Op.getOperand(1), Op.getOperand(2)); 00578 } 00579 } 00580 00581 case ISD::SRL_PARTS: { 00582 SDValue ShOpLo = Op.getOperand(0); 00583 SDValue ShOpHi = Op.getOperand(1); 00584 SDValue ShAmt = Op.getOperand(2); 00585 SDValue bm = DAG.getNode(ISD::SUB, dl, MVT::i64, 00586 DAG.getConstant(64, MVT::i64), ShAmt); 00587 SDValue BMCC = DAG.getSetCC(dl, MVT::i64, bm, 00588 DAG.getConstant(0, MVT::i64), ISD::SETLE); 00589 // if 64 - shAmt <= 0 00590 SDValue Hi_Neg = DAG.getConstant(0, MVT::i64); 00591 SDValue ShAmt_Neg = DAG.getNode(ISD::SUB, dl, MVT::i64, 00592 DAG.getConstant(0, MVT::i64), bm); 00593 SDValue Lo_Neg = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpHi, ShAmt_Neg); 00594 // else 00595 SDValue carries = DAG.getNode(ISD::SHL, dl, MVT::i64, ShOpHi, bm); 00596 SDValue Hi_Pos = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpHi, ShAmt); 00597 SDValue Lo_Pos = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpLo, ShAmt); 00598 Lo_Pos = DAG.getNode(ISD::OR, dl, MVT::i64, Lo_Pos, carries); 00599 // Merge 00600 SDValue Hi = DAG.getNode(ISD::SELECT, dl, MVT::i64, BMCC, Hi_Neg, Hi_Pos); 00601 SDValue Lo = DAG.getNode(ISD::SELECT, dl, MVT::i64, BMCC, Lo_Neg, Lo_Pos); 00602 SDValue Ops[2] = { Lo, Hi }; 00603 return DAG.getMergeValues(Ops, 2, dl); 00604 } 00605 // case ISD::SRA_PARTS: 00606 00607 // case ISD::SHL_PARTS: 00608 00609 00610 case ISD::SINT_TO_FP: { 00611 assert(Op.getOperand(0).getValueType() == MVT::i64 && 00612 "Unhandled SINT_TO_FP type in custom expander!"); 00613 SDValue LD; 00614 bool isDouble = Op.getValueType() == MVT::f64; 00615 LD = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op.getOperand(0)); 00616 SDValue FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_, dl, 00617 isDouble?MVT::f64:MVT::f32, LD); 00618 return FP; 00619 } 00620 case ISD::FP_TO_SINT: { 00621 bool isDouble = Op.getOperand(0).getValueType() == MVT::f64; 00622 SDValue src = Op.getOperand(0); 00623 00624 if (!isDouble) //Promote 00625 src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, src); 00626 00627 src = DAG.getNode(AlphaISD::CVTTQ_, dl, MVT::f64, src); 00628 00629 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, src); 00630 } 00631 case ISD::ConstantPool: { 00632 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 00633 Constant *C = CP->getConstVal(); 00634 SDValue CPI = DAG.getTargetConstantPool(C, MVT::i64, CP->getAlignment()); 00635 // FIXME there isn't really any debug info here 00636 00637 SDValue Hi = DAG.getNode(AlphaISD::GPRelHi, dl, MVT::i64, CPI, 00638 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64)); 00639 SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, CPI, Hi); 00640 return Lo; 00641 } 00642 case ISD::GlobalTLSAddress: 00643 llvm_unreachable("TLS not implemented for Alpha."); 00644 case ISD::GlobalAddress: { 00645 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op); 00646 GlobalValue *GV = GSDN->getGlobal(); 00647 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset()); 00648 // FIXME there isn't really any debug info here 00649 00650 // if (!GV->hasWeakLinkage() && !GV->isDeclaration() && !GV->hasLinkOnceLinkage()) { 00651 if (GV->hasLocalLinkage()) { 00652 SDValue Hi = DAG.getNode(AlphaISD::GPRelHi, dl, MVT::i64, GA, 00653 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64)); 00654 SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, GA, Hi); 00655 return Lo; 00656 } else 00657 return DAG.getNode(AlphaISD::RelLit, dl, MVT::i64, GA, 00658 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64)); 00659 } 00660 case ISD::ExternalSymbol: { 00661 return DAG.getNode(AlphaISD::RelLit, dl, MVT::i64, 00662 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op) 00663 ->getSymbol(), MVT::i64), 00664 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64)); 00665 } 00666 00667 case ISD::UREM: 00668 case ISD::SREM: 00669 //Expand only on constant case 00670 if (Op.getOperand(1).getOpcode() == ISD::Constant) { 00671 EVT VT = Op.getNode()->getValueType(0); 00672 SDValue Tmp1 = Op.getNode()->getOpcode() == ISD::UREM ? 00673 BuildUDIV(Op.getNode(), DAG, NULL) : 00674 BuildSDIV(Op.getNode(), DAG, NULL); 00675 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Op.getOperand(1)); 00676 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Op.getOperand(0), Tmp1); 00677 return Tmp1; 00678 } 00679 //fall through 00680 case ISD::SDIV: 00681 case ISD::UDIV: 00682 if (Op.getValueType().isInteger()) { 00683 if (Op.getOperand(1).getOpcode() == ISD::Constant) 00684 return Op.getOpcode() == ISD::SDIV ? BuildSDIV(Op.getNode(), DAG, NULL) 00685 : BuildUDIV(Op.getNode(), DAG, NULL); 00686 const char* opstr = 0; 00687 switch (Op.getOpcode()) { 00688 case ISD::UREM: opstr = "__remqu"; break; 00689 case ISD::SREM: opstr = "__remq"; break; 00690 case ISD::UDIV: opstr = "__divqu"; break; 00691 case ISD::SDIV: opstr = "__divq"; break; 00692 } 00693 SDValue Tmp1 = Op.getOperand(0), 00694 Tmp2 = Op.getOperand(1), 00695 Addr = DAG.getExternalSymbol(opstr, MVT::i64); 00696 return DAG.getNode(AlphaISD::DivCall, dl, MVT::i64, Addr, Tmp1, Tmp2); 00697 } 00698 break; 00699 00700 case ISD::VAARG: { 00701 SDValue Chain, DataPtr; 00702 LowerVAARG(Op.getNode(), Chain, DataPtr, DAG); 00703 00704 SDValue Result; 00705 if (Op.getValueType() == MVT::i32) 00706 Result = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Chain, DataPtr, 00707 NULL, 0, MVT::i32); 00708 else 00709 Result = DAG.getLoad(Op.getValueType(), dl, Chain, DataPtr, NULL, 0); 00710 return Result; 00711 } 00712 case ISD::VACOPY: { 00713 SDValue Chain = Op.getOperand(0); 00714 SDValue DestP = Op.getOperand(1); 00715 SDValue SrcP = Op.getOperand(2); 00716 const Value *DestS = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); 00717 const Value *SrcS = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); 00718 00719 SDValue Val = DAG.getLoad(getPointerTy(), dl, Chain, SrcP, SrcS, 0); 00720 SDValue Result = DAG.getStore(Val.getValue(1), dl, Val, DestP, DestS, 0); 00721 SDValue NP = DAG.getNode(ISD::ADD, dl, MVT::i64, SrcP, 00722 DAG.getConstant(8, MVT::i64)); 00723 Val = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Result, 00724 NP, NULL,0, MVT::i32); 00725 SDValue NPD = DAG.getNode(ISD::ADD, dl, MVT::i64, DestP, 00726 DAG.getConstant(8, MVT::i64)); 00727 return DAG.getTruncStore(Val.getValue(1), dl, Val, NPD, NULL, 0, MVT::i32); 00728 } 00729 case ISD::VASTART: { 00730 SDValue Chain = Op.getOperand(0); 00731 SDValue VAListP = Op.getOperand(1); 00732 const Value *VAListS = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 00733 00734 // vastart stores the address of the VarArgsBase and VarArgsOffset 00735 SDValue FR = DAG.getFrameIndex(VarArgsBase, MVT::i64); 00736 SDValue S1 = DAG.getStore(Chain, dl, FR, VAListP, VAListS, 0); 00737 SDValue SA2 = DAG.getNode(ISD::ADD, dl, MVT::i64, VAListP, 00738 DAG.getConstant(8, MVT::i64)); 00739 return DAG.getTruncStore(S1, dl, DAG.getConstant(VarArgsOffset, MVT::i64), 00740 SA2, NULL, 0, MVT::i32); 00741 } 00742 case ISD::RETURNADDR: 00743 return DAG.getNode(AlphaISD::GlobalRetAddr, DebugLoc::getUnknownLoc(), 00744 MVT::i64); 00745 //FIXME: implement 00746 case ISD::FRAMEADDR: break; 00747 } 00748 00749 return SDValue(); 00750 } 00751 00752 void AlphaTargetLowering::ReplaceNodeResults(SDNode *N, 00753 SmallVectorImpl<SDValue>&Results, 00754 SelectionDAG &DAG) { 00755 DebugLoc dl = N->getDebugLoc(); 00756 assert(N->getValueType(0) == MVT::i32 && 00757 N->getOpcode() == ISD::VAARG && 00758 "Unknown node to custom promote!"); 00759 00760 SDValue Chain, DataPtr; 00761 LowerVAARG(N, Chain, DataPtr, DAG); 00762 SDValue Res = DAG.getLoad(N->getValueType(0), dl, Chain, DataPtr, NULL, 0); 00763 Results.push_back(Res); 00764 Results.push_back(SDValue(Res.getNode(), 1)); 00765 } 00766 00767 00768 //Inline Asm 00769 00770 /// getConstraintType - Given a constraint letter, return the type of 00771 /// constraint it is for this target. 00772 AlphaTargetLowering::ConstraintType 00773 AlphaTargetLowering::getConstraintType(const std::string &Constraint) const { 00774 if (Constraint.size() == 1) { 00775 switch (Constraint[0]) { 00776 default: break; 00777 case 'f': 00778 case 'r': 00779 return C_RegisterClass; 00780 } 00781 } 00782 return TargetLowering::getConstraintType(Constraint); 00783 } 00784 00785 std::vector<unsigned> AlphaTargetLowering:: 00786 getRegClassForInlineAsmConstraint(const std::string &Constraint, 00787 EVT VT) const { 00788 if (Constraint.size() == 1) { 00789 switch (Constraint[0]) { 00790 default: break; // Unknown constriant letter 00791 case 'f': 00792 return make_vector<unsigned>(Alpha::F0 , Alpha::F1 , Alpha::F2 , 00793 Alpha::F3 , Alpha::F4 , Alpha::F5 , 00794 Alpha::F6 , Alpha::F7 , Alpha::F8 , 00795 Alpha::F9 , Alpha::F10, Alpha::F11, 00796 Alpha::F12, Alpha::F13, Alpha::F14, 00797 Alpha::F15, Alpha::F16, Alpha::F17, 00798 Alpha::F18, Alpha::F19, Alpha::F20, 00799 Alpha::F21, Alpha::F22, Alpha::F23, 00800 Alpha::F24, Alpha::F25, Alpha::F26, 00801 Alpha::F27, Alpha::F28, Alpha::F29, 00802 Alpha::F30, Alpha::F31, 0); 00803 case 'r': 00804 return make_vector<unsigned>(Alpha::R0 , Alpha::R1 , Alpha::R2 , 00805 Alpha::R3 , Alpha::R4 , Alpha::R5 , 00806 Alpha::R6 , Alpha::R7 , Alpha::R8 , 00807 Alpha::R9 , Alpha::R10, Alpha::R11, 00808 Alpha::R12, Alpha::R13, Alpha::R14, 00809 Alpha::R15, Alpha::R16, Alpha::R17, 00810 Alpha::R18, Alpha::R19, Alpha::R20, 00811 Alpha::R21, Alpha::R22, Alpha::R23, 00812 Alpha::R24, Alpha::R25, Alpha::R26, 00813 Alpha::R27, Alpha::R28, Alpha::R29, 00814 Alpha::R30, Alpha::R31, 0); 00815 } 00816 } 00817 00818 return std::vector<unsigned>(); 00819 } 00820 //===----------------------------------------------------------------------===// 00821 // Other Lowering Code 00822 //===----------------------------------------------------------------------===// 00823 00824 MachineBasicBlock * 00825 AlphaTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 00826 MachineBasicBlock *BB) const { 00827 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 00828 assert((MI->getOpcode() == Alpha::CAS32 || 00829 MI->getOpcode() == Alpha::CAS64 || 00830 MI->getOpcode() == Alpha::LAS32 || 00831 MI->getOpcode() == Alpha::LAS64 || 00832 MI->getOpcode() == Alpha::SWAP32 || 00833 MI->getOpcode() == Alpha::SWAP64) && 00834 "Unexpected instr type to insert"); 00835 00836 bool is32 = MI->getOpcode() == Alpha::CAS32 || 00837 MI->getOpcode() == Alpha::LAS32 || 00838 MI->getOpcode() == Alpha::SWAP32; 00839 00840 //Load locked store conditional for atomic ops take on the same form 00841 //start: 00842 //ll 00843 //do stuff (maybe branch to exit) 00844 //sc 00845 //test sc and maybe branck to start 00846 //exit: 00847 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 00848 DebugLoc dl = MI->getDebugLoc(); 00849 MachineFunction::iterator It = BB; 00850 ++It; 00851 00852 MachineBasicBlock *thisMBB = BB; 00853 MachineFunction *F = BB->getParent(); 00854 MachineBasicBlock *llscMBB = F->CreateMachineBasicBlock(LLVM_BB); 00855 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 00856 00857 sinkMBB->transferSuccessors(thisMBB); 00858 00859 F->insert(It, llscMBB); 00860 F->insert(It, sinkMBB); 00861 00862 BuildMI(thisMBB, dl, TII->get(Alpha::BR)).addMBB(llscMBB); 00863 00864 unsigned reg_res = MI->getOperand(0).getReg(), 00865 reg_ptr = MI->getOperand(1).getReg(), 00866 reg_v2 = MI->getOperand(2).getReg(), 00867 reg_store = F->getRegInfo().createVirtualRegister(&Alpha::GPRCRegClass); 00868 00869 BuildMI(llscMBB, dl, TII->get(is32 ? Alpha::LDL_L : Alpha::LDQ_L), 00870 reg_res).addImm(0).addReg(reg_ptr); 00871 switch (MI->getOpcode()) { 00872 case Alpha::CAS32: 00873 case Alpha::CAS64: { 00874 unsigned reg_cmp 00875 = F->getRegInfo().createVirtualRegister(&Alpha::GPRCRegClass); 00876 BuildMI(llscMBB, dl, TII->get(Alpha::CMPEQ), reg_cmp) 00877 .addReg(reg_v2).addReg(reg_res); 00878 BuildMI(llscMBB, dl, TII->get(Alpha::BEQ)) 00879 .addImm(0).addReg(reg_cmp).addMBB(sinkMBB); 00880 BuildMI(llscMBB, dl, TII->get(Alpha::BISr), reg_store) 00881 .addReg(Alpha::R31).addReg(MI->getOperand(3).getReg()); 00882 break; 00883 } 00884 case Alpha::LAS32: 00885 case Alpha::LAS64: { 00886 BuildMI(llscMBB, dl,TII->get(is32 ? Alpha::ADDLr : Alpha::ADDQr), reg_store) 00887 .addReg(reg_res).addReg(reg_v2); 00888 break; 00889 } 00890 case Alpha::SWAP32: 00891 case Alpha::SWAP64: { 00892 BuildMI(llscMBB, dl, TII->get(Alpha::BISr), reg_store) 00893 .addReg(reg_v2).addReg(reg_v2); 00894 break; 00895 } 00896 } 00897 BuildMI(llscMBB, dl, TII->get(is32 ? Alpha::STL_C : Alpha::STQ_C), reg_store) 00898 .addReg(reg_store).addImm(0).addReg(reg_ptr); 00899 BuildMI(llscMBB, dl, TII->get(Alpha::BEQ)) 00900 .addImm(0).addReg(reg_store).addMBB(llscMBB); 00901 BuildMI(llscMBB, dl, TII->get(Alpha::BR)).addMBB(sinkMBB); 00902 00903 thisMBB->addSuccessor(llscMBB); 00904 llscMBB->addSuccessor(llscMBB); 00905 llscMBB->addSuccessor(sinkMBB); 00906 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. 00907 00908 return sinkMBB; 00909 } 00910 00911 bool 00912 AlphaTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 00913 // The Alpha target isn't yet aware of offsets. 00914 return false; 00915 }