LLVM API Documentation
00001 //===-- MSP430ISelLowering.cpp - MSP430 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 MSP430TargetLowering class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "MSP430ISelLowering.h" 00015 #include "MSP430.h" 00016 #include "MSP430MachineFunctionInfo.h" 00017 #include "MSP430Subtarget.h" 00018 #include "MSP430TargetMachine.h" 00019 #include "llvm/CodeGen/CallingConvLower.h" 00020 #include "llvm/CodeGen/MachineFrameInfo.h" 00021 #include "llvm/CodeGen/MachineFunction.h" 00022 #include "llvm/CodeGen/MachineInstrBuilder.h" 00023 #include "llvm/CodeGen/MachineRegisterInfo.h" 00024 #include "llvm/CodeGen/SelectionDAGISel.h" 00025 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 00026 #include "llvm/CodeGen/ValueTypes.h" 00027 #include "llvm/IR/CallingConv.h" 00028 #include "llvm/IR/DerivedTypes.h" 00029 #include "llvm/IR/Function.h" 00030 #include "llvm/IR/GlobalAlias.h" 00031 #include "llvm/IR/GlobalVariable.h" 00032 #include "llvm/IR/Intrinsics.h" 00033 #include "llvm/Support/CommandLine.h" 00034 #include "llvm/Support/Debug.h" 00035 #include "llvm/Support/ErrorHandling.h" 00036 #include "llvm/Support/raw_ostream.h" 00037 using namespace llvm; 00038 00039 #define DEBUG_TYPE "msp430-lower" 00040 00041 typedef enum { 00042 NoHWMult, 00043 HWMultIntr, 00044 HWMultNoIntr 00045 } HWMultUseMode; 00046 00047 static cl::opt<HWMultUseMode> 00048 HWMultMode("msp430-hwmult-mode", cl::Hidden, 00049 cl::desc("Hardware multiplier use mode"), 00050 cl::init(HWMultNoIntr), 00051 cl::values( 00052 clEnumValN(NoHWMult, "no", 00053 "Do not use hardware multiplier"), 00054 clEnumValN(HWMultIntr, "interrupts", 00055 "Assume hardware multiplier can be used inside interrupts"), 00056 clEnumValN(HWMultNoIntr, "use", 00057 "Assume hardware multiplier cannot be used inside interrupts"), 00058 clEnumValEnd)); 00059 00060 MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM) 00061 : TargetLowering(TM, new TargetLoweringObjectFileELF()) { 00062 00063 // Set up the register classes. 00064 addRegisterClass(MVT::i8, &MSP430::GR8RegClass); 00065 addRegisterClass(MVT::i16, &MSP430::GR16RegClass); 00066 00067 // Compute derived properties from the register classes 00068 computeRegisterProperties(); 00069 00070 // Provide all sorts of operation actions 00071 00072 // Division is expensive 00073 setIntDivIsCheap(false); 00074 00075 setStackPointerRegisterToSaveRestore(MSP430::SP); 00076 setBooleanContents(ZeroOrOneBooleanContent); 00077 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct? 00078 00079 // We have post-incremented loads / stores. 00080 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal); 00081 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal); 00082 00083 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); 00084 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 00085 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); 00086 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand); 00087 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand); 00088 00089 // We don't have any truncstores 00090 setTruncStoreAction(MVT::i16, MVT::i8, Expand); 00091 00092 setOperationAction(ISD::SRA, MVT::i8, Custom); 00093 setOperationAction(ISD::SHL, MVT::i8, Custom); 00094 setOperationAction(ISD::SRL, MVT::i8, Custom); 00095 setOperationAction(ISD::SRA, MVT::i16, Custom); 00096 setOperationAction(ISD::SHL, MVT::i16, Custom); 00097 setOperationAction(ISD::SRL, MVT::i16, Custom); 00098 setOperationAction(ISD::ROTL, MVT::i8, Expand); 00099 setOperationAction(ISD::ROTR, MVT::i8, Expand); 00100 setOperationAction(ISD::ROTL, MVT::i16, Expand); 00101 setOperationAction(ISD::ROTR, MVT::i16, Expand); 00102 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom); 00103 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom); 00104 setOperationAction(ISD::BlockAddress, MVT::i16, Custom); 00105 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 00106 setOperationAction(ISD::BR_CC, MVT::i8, Custom); 00107 setOperationAction(ISD::BR_CC, MVT::i16, Custom); 00108 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 00109 setOperationAction(ISD::SETCC, MVT::i8, Custom); 00110 setOperationAction(ISD::SETCC, MVT::i16, Custom); 00111 setOperationAction(ISD::SELECT, MVT::i8, Expand); 00112 setOperationAction(ISD::SELECT, MVT::i16, Expand); 00113 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom); 00114 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom); 00115 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom); 00116 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand); 00117 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand); 00118 00119 setOperationAction(ISD::CTTZ, MVT::i8, Expand); 00120 setOperationAction(ISD::CTTZ, MVT::i16, Expand); 00121 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Expand); 00122 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand); 00123 setOperationAction(ISD::CTLZ, MVT::i8, Expand); 00124 setOperationAction(ISD::CTLZ, MVT::i16, Expand); 00125 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Expand); 00126 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Expand); 00127 setOperationAction(ISD::CTPOP, MVT::i8, Expand); 00128 setOperationAction(ISD::CTPOP, MVT::i16, Expand); 00129 00130 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand); 00131 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand); 00132 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand); 00133 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand); 00134 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand); 00135 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand); 00136 00137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 00138 00139 // FIXME: Implement efficiently multiplication by a constant 00140 setOperationAction(ISD::MUL, MVT::i8, Expand); 00141 setOperationAction(ISD::MULHS, MVT::i8, Expand); 00142 setOperationAction(ISD::MULHU, MVT::i8, Expand); 00143 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand); 00144 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand); 00145 setOperationAction(ISD::MUL, MVT::i16, Expand); 00146 setOperationAction(ISD::MULHS, MVT::i16, Expand); 00147 setOperationAction(ISD::MULHU, MVT::i16, Expand); 00148 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand); 00149 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand); 00150 00151 setOperationAction(ISD::UDIV, MVT::i8, Expand); 00152 setOperationAction(ISD::UDIVREM, MVT::i8, Expand); 00153 setOperationAction(ISD::UREM, MVT::i8, Expand); 00154 setOperationAction(ISD::SDIV, MVT::i8, Expand); 00155 setOperationAction(ISD::SDIVREM, MVT::i8, Expand); 00156 setOperationAction(ISD::SREM, MVT::i8, Expand); 00157 setOperationAction(ISD::UDIV, MVT::i16, Expand); 00158 setOperationAction(ISD::UDIVREM, MVT::i16, Expand); 00159 setOperationAction(ISD::UREM, MVT::i16, Expand); 00160 setOperationAction(ISD::SDIV, MVT::i16, Expand); 00161 setOperationAction(ISD::SDIVREM, MVT::i16, Expand); 00162 setOperationAction(ISD::SREM, MVT::i16, Expand); 00163 00164 // varargs support 00165 setOperationAction(ISD::VASTART, MVT::Other, Custom); 00166 setOperationAction(ISD::VAARG, MVT::Other, Expand); 00167 setOperationAction(ISD::VAEND, MVT::Other, Expand); 00168 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 00169 setOperationAction(ISD::JumpTable, MVT::i16, Custom); 00170 00171 // Libcalls names. 00172 if (HWMultMode == HWMultIntr) { 00173 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw"); 00174 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw"); 00175 } else if (HWMultMode == HWMultNoIntr) { 00176 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint"); 00177 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint"); 00178 } 00179 00180 setMinFunctionAlignment(1); 00181 setPrefFunctionAlignment(2); 00182 } 00183 00184 SDValue MSP430TargetLowering::LowerOperation(SDValue Op, 00185 SelectionDAG &DAG) const { 00186 switch (Op.getOpcode()) { 00187 case ISD::SHL: // FALLTHROUGH 00188 case ISD::SRL: 00189 case ISD::SRA: return LowerShifts(Op, DAG); 00190 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 00191 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 00192 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); 00193 case ISD::SETCC: return LowerSETCC(Op, DAG); 00194 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 00195 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 00196 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG); 00197 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 00198 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 00199 case ISD::VASTART: return LowerVASTART(Op, DAG); 00200 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 00201 default: 00202 llvm_unreachable("unimplemented operand"); 00203 } 00204 } 00205 00206 //===----------------------------------------------------------------------===// 00207 // MSP430 Inline Assembly Support 00208 //===----------------------------------------------------------------------===// 00209 00210 /// getConstraintType - Given a constraint letter, return the type of 00211 /// constraint it is for this target. 00212 TargetLowering::ConstraintType 00213 MSP430TargetLowering::getConstraintType(const std::string &Constraint) const { 00214 if (Constraint.size() == 1) { 00215 switch (Constraint[0]) { 00216 case 'r': 00217 return C_RegisterClass; 00218 default: 00219 break; 00220 } 00221 } 00222 return TargetLowering::getConstraintType(Constraint); 00223 } 00224 00225 std::pair<unsigned, const TargetRegisterClass*> 00226 MSP430TargetLowering:: 00227 getRegForInlineAsmConstraint(const std::string &Constraint, 00228 MVT VT) const { 00229 if (Constraint.size() == 1) { 00230 // GCC Constraint Letters 00231 switch (Constraint[0]) { 00232 default: break; 00233 case 'r': // GENERAL_REGS 00234 if (VT == MVT::i8) 00235 return std::make_pair(0U, &MSP430::GR8RegClass); 00236 00237 return std::make_pair(0U, &MSP430::GR16RegClass); 00238 } 00239 } 00240 00241 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 00242 } 00243 00244 //===----------------------------------------------------------------------===// 00245 // Calling Convention Implementation 00246 //===----------------------------------------------------------------------===// 00247 00248 #include "MSP430GenCallingConv.inc" 00249 00250 /// For each argument in a function store the number of pieces it is composed 00251 /// of. 00252 template<typename ArgT> 00253 static void ParseFunctionArgs(const SmallVectorImpl<ArgT> &Args, 00254 SmallVectorImpl<unsigned> &Out) { 00255 unsigned CurrentArgIndex = ~0U; 00256 for (unsigned i = 0, e = Args.size(); i != e; i++) { 00257 if (CurrentArgIndex == Args[i].OrigArgIndex) { 00258 Out.back()++; 00259 } else { 00260 Out.push_back(1); 00261 CurrentArgIndex++; 00262 } 00263 } 00264 } 00265 00266 static void AnalyzeVarArgs(CCState &State, 00267 const SmallVectorImpl<ISD::OutputArg> &Outs) { 00268 State.AnalyzeCallOperands(Outs, CC_MSP430_AssignStack); 00269 } 00270 00271 static void AnalyzeVarArgs(CCState &State, 00272 const SmallVectorImpl<ISD::InputArg> &Ins) { 00273 State.AnalyzeFormalArguments(Ins, CC_MSP430_AssignStack); 00274 } 00275 00276 /// Analyze incoming and outgoing function arguments. We need custom C++ code 00277 /// to handle special constraints in the ABI like reversing the order of the 00278 /// pieces of splitted arguments. In addition, all pieces of a certain argument 00279 /// have to be passed either using registers or the stack but never mixing both. 00280 template<typename ArgT> 00281 static void AnalyzeArguments(CCState &State, 00282 SmallVectorImpl<CCValAssign> &ArgLocs, 00283 const SmallVectorImpl<ArgT> &Args) { 00284 static const MCPhysReg RegList[] = { 00285 MSP430::R15, MSP430::R14, MSP430::R13, MSP430::R12 00286 }; 00287 static const unsigned NbRegs = array_lengthof(RegList); 00288 00289 if (State.isVarArg()) { 00290 AnalyzeVarArgs(State, Args); 00291 return; 00292 } 00293 00294 SmallVector<unsigned, 4> ArgsParts; 00295 ParseFunctionArgs(Args, ArgsParts); 00296 00297 unsigned RegsLeft = NbRegs; 00298 bool UseStack = false; 00299 unsigned ValNo = 0; 00300 00301 for (unsigned i = 0, e = ArgsParts.size(); i != e; i++) { 00302 MVT ArgVT = Args[ValNo].VT; 00303 ISD::ArgFlagsTy ArgFlags = Args[ValNo].Flags; 00304 MVT LocVT = ArgVT; 00305 CCValAssign::LocInfo LocInfo = CCValAssign::Full; 00306 00307 // Promote i8 to i16 00308 if (LocVT == MVT::i8) { 00309 LocVT = MVT::i16; 00310 if (ArgFlags.isSExt()) 00311 LocInfo = CCValAssign::SExt; 00312 else if (ArgFlags.isZExt()) 00313 LocInfo = CCValAssign::ZExt; 00314 else 00315 LocInfo = CCValAssign::AExt; 00316 } 00317 00318 // Handle byval arguments 00319 if (ArgFlags.isByVal()) { 00320 State.HandleByVal(ValNo++, ArgVT, LocVT, LocInfo, 2, 2, ArgFlags); 00321 continue; 00322 } 00323 00324 unsigned Parts = ArgsParts[i]; 00325 00326 if (!UseStack && Parts <= RegsLeft) { 00327 unsigned FirstVal = ValNo; 00328 for (unsigned j = 0; j < Parts; j++) { 00329 unsigned Reg = State.AllocateReg(RegList, NbRegs); 00330 State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo)); 00331 RegsLeft--; 00332 } 00333 00334 // Reverse the order of the pieces to agree with the "big endian" format 00335 // required in the calling convention ABI. 00336 SmallVectorImpl<CCValAssign>::iterator B = ArgLocs.begin() + FirstVal; 00337 std::reverse(B, B + Parts); 00338 } else { 00339 UseStack = true; 00340 for (unsigned j = 0; j < Parts; j++) 00341 CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State); 00342 } 00343 } 00344 } 00345 00346 static void AnalyzeRetResult(CCState &State, 00347 const SmallVectorImpl<ISD::InputArg> &Ins) { 00348 State.AnalyzeCallResult(Ins, RetCC_MSP430); 00349 } 00350 00351 static void AnalyzeRetResult(CCState &State, 00352 const SmallVectorImpl<ISD::OutputArg> &Outs) { 00353 State.AnalyzeReturn(Outs, RetCC_MSP430); 00354 } 00355 00356 template<typename ArgT> 00357 static void AnalyzeReturnValues(CCState &State, 00358 SmallVectorImpl<CCValAssign> &RVLocs, 00359 const SmallVectorImpl<ArgT> &Args) { 00360 AnalyzeRetResult(State, Args); 00361 00362 // Reverse splitted return values to get the "big endian" format required 00363 // to agree with the calling convention ABI. 00364 std::reverse(RVLocs.begin(), RVLocs.end()); 00365 } 00366 00367 SDValue 00368 MSP430TargetLowering::LowerFormalArguments(SDValue Chain, 00369 CallingConv::ID CallConv, 00370 bool isVarArg, 00371 const SmallVectorImpl<ISD::InputArg> 00372 &Ins, 00373 SDLoc dl, 00374 SelectionDAG &DAG, 00375 SmallVectorImpl<SDValue> &InVals) 00376 const { 00377 00378 switch (CallConv) { 00379 default: 00380 llvm_unreachable("Unsupported calling convention"); 00381 case CallingConv::C: 00382 case CallingConv::Fast: 00383 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals); 00384 case CallingConv::MSP430_INTR: 00385 if (Ins.empty()) 00386 return Chain; 00387 report_fatal_error("ISRs cannot have arguments"); 00388 } 00389 } 00390 00391 SDValue 00392 MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 00393 SmallVectorImpl<SDValue> &InVals) const { 00394 SelectionDAG &DAG = CLI.DAG; 00395 SDLoc &dl = CLI.DL; 00396 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 00397 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 00398 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 00399 SDValue Chain = CLI.Chain; 00400 SDValue Callee = CLI.Callee; 00401 bool &isTailCall = CLI.IsTailCall; 00402 CallingConv::ID CallConv = CLI.CallConv; 00403 bool isVarArg = CLI.IsVarArg; 00404 00405 // MSP430 target does not yet support tail call optimization. 00406 isTailCall = false; 00407 00408 switch (CallConv) { 00409 default: 00410 llvm_unreachable("Unsupported calling convention"); 00411 case CallingConv::Fast: 00412 case CallingConv::C: 00413 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall, 00414 Outs, OutVals, Ins, dl, DAG, InVals); 00415 case CallingConv::MSP430_INTR: 00416 report_fatal_error("ISRs cannot be called directly"); 00417 } 00418 } 00419 00420 /// LowerCCCArguments - transform physical registers into virtual registers and 00421 /// generate load operations for arguments places on the stack. 00422 // FIXME: struct return stuff 00423 SDValue 00424 MSP430TargetLowering::LowerCCCArguments(SDValue Chain, 00425 CallingConv::ID CallConv, 00426 bool isVarArg, 00427 const SmallVectorImpl<ISD::InputArg> 00428 &Ins, 00429 SDLoc dl, 00430 SelectionDAG &DAG, 00431 SmallVectorImpl<SDValue> &InVals) 00432 const { 00433 MachineFunction &MF = DAG.getMachineFunction(); 00434 MachineFrameInfo *MFI = MF.getFrameInfo(); 00435 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 00436 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>(); 00437 00438 // Assign locations to all of the incoming arguments. 00439 SmallVector<CCValAssign, 16> ArgLocs; 00440 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 00441 *DAG.getContext()); 00442 AnalyzeArguments(CCInfo, ArgLocs, Ins); 00443 00444 // Create frame index for the start of the first vararg value 00445 if (isVarArg) { 00446 unsigned Offset = CCInfo.getNextStackOffset(); 00447 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, Offset, true)); 00448 } 00449 00450 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 00451 CCValAssign &VA = ArgLocs[i]; 00452 if (VA.isRegLoc()) { 00453 // Arguments passed in registers 00454 EVT RegVT = VA.getLocVT(); 00455 switch (RegVT.getSimpleVT().SimpleTy) { 00456 default: 00457 { 00458 #ifndef NDEBUG 00459 errs() << "LowerFormalArguments Unhandled argument type: " 00460 << RegVT.getSimpleVT().SimpleTy << "\n"; 00461 #endif 00462 llvm_unreachable(nullptr); 00463 } 00464 case MVT::i16: 00465 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass); 00466 RegInfo.addLiveIn(VA.getLocReg(), VReg); 00467 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT); 00468 00469 // If this is an 8-bit value, it is really passed promoted to 16 00470 // bits. Insert an assert[sz]ext to capture this, then truncate to the 00471 // right size. 00472 if (VA.getLocInfo() == CCValAssign::SExt) 00473 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 00474 DAG.getValueType(VA.getValVT())); 00475 else if (VA.getLocInfo() == CCValAssign::ZExt) 00476 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 00477 DAG.getValueType(VA.getValVT())); 00478 00479 if (VA.getLocInfo() != CCValAssign::Full) 00480 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 00481 00482 InVals.push_back(ArgValue); 00483 } 00484 } else { 00485 // Sanity check 00486 assert(VA.isMemLoc()); 00487 00488 SDValue InVal; 00489 ISD::ArgFlagsTy Flags = Ins[i].Flags; 00490 00491 if (Flags.isByVal()) { 00492 int FI = MFI->CreateFixedObject(Flags.getByValSize(), 00493 VA.getLocMemOffset(), true); 00494 InVal = DAG.getFrameIndex(FI, getPointerTy()); 00495 } else { 00496 // Load the argument to a virtual register 00497 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8; 00498 if (ObjSize > 2) { 00499 errs() << "LowerFormalArguments Unhandled argument type: " 00500 << EVT(VA.getLocVT()).getEVTString() 00501 << "\n"; 00502 } 00503 // Create the frame index object for this incoming parameter... 00504 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true); 00505 00506 // Create the SelectionDAG nodes corresponding to a load 00507 //from this parameter 00508 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16); 00509 InVal = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, 00510 MachinePointerInfo::getFixedStack(FI), 00511 false, false, false, 0); 00512 } 00513 00514 InVals.push_back(InVal); 00515 } 00516 } 00517 00518 return Chain; 00519 } 00520 00521 SDValue 00522 MSP430TargetLowering::LowerReturn(SDValue Chain, 00523 CallingConv::ID CallConv, bool isVarArg, 00524 const SmallVectorImpl<ISD::OutputArg> &Outs, 00525 const SmallVectorImpl<SDValue> &OutVals, 00526 SDLoc dl, SelectionDAG &DAG) const { 00527 00528 // CCValAssign - represent the assignment of the return value to a location 00529 SmallVector<CCValAssign, 16> RVLocs; 00530 00531 // ISRs cannot return any value. 00532 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty()) 00533 report_fatal_error("ISRs cannot return any value"); 00534 00535 // CCState - Info about the registers and stack slot. 00536 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 00537 *DAG.getContext()); 00538 00539 // Analize return values. 00540 AnalyzeReturnValues(CCInfo, RVLocs, Outs); 00541 00542 SDValue Flag; 00543 SmallVector<SDValue, 4> RetOps(1, Chain); 00544 00545 // Copy the result values into the output registers. 00546 for (unsigned i = 0; i != RVLocs.size(); ++i) { 00547 CCValAssign &VA = RVLocs[i]; 00548 assert(VA.isRegLoc() && "Can only return in registers!"); 00549 00550 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 00551 OutVals[i], Flag); 00552 00553 // Guarantee that all emitted copies are stuck together, 00554 // avoiding something bad. 00555 Flag = Chain.getValue(1); 00556 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 00557 } 00558 00559 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ? 00560 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG); 00561 00562 RetOps[0] = Chain; // Update chain. 00563 00564 // Add the flag if we have it. 00565 if (Flag.getNode()) 00566 RetOps.push_back(Flag); 00567 00568 return DAG.getNode(Opc, dl, MVT::Other, RetOps); 00569 } 00570 00571 /// LowerCCCCallTo - functions arguments are copied from virtual regs to 00572 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. 00573 // TODO: sret. 00574 SDValue 00575 MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee, 00576 CallingConv::ID CallConv, bool isVarArg, 00577 bool isTailCall, 00578 const SmallVectorImpl<ISD::OutputArg> 00579 &Outs, 00580 const SmallVectorImpl<SDValue> &OutVals, 00581 const SmallVectorImpl<ISD::InputArg> &Ins, 00582 SDLoc dl, SelectionDAG &DAG, 00583 SmallVectorImpl<SDValue> &InVals) const { 00584 // Analyze operands of the call, assigning locations to each operand. 00585 SmallVector<CCValAssign, 16> ArgLocs; 00586 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 00587 *DAG.getContext()); 00588 AnalyzeArguments(CCInfo, ArgLocs, Outs); 00589 00590 // Get a count of how many bytes are to be pushed on the stack. 00591 unsigned NumBytes = CCInfo.getNextStackOffset(); 00592 00593 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes, 00594 getPointerTy(), true), 00595 dl); 00596 00597 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass; 00598 SmallVector<SDValue, 12> MemOpChains; 00599 SDValue StackPtr; 00600 00601 // Walk the register/memloc assignments, inserting copies/loads. 00602 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 00603 CCValAssign &VA = ArgLocs[i]; 00604 00605 SDValue Arg = OutVals[i]; 00606 00607 // Promote the value if needed. 00608 switch (VA.getLocInfo()) { 00609 default: llvm_unreachable("Unknown loc info!"); 00610 case CCValAssign::Full: break; 00611 case CCValAssign::SExt: 00612 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 00613 break; 00614 case CCValAssign::ZExt: 00615 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 00616 break; 00617 case CCValAssign::AExt: 00618 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 00619 break; 00620 } 00621 00622 // Arguments that can be passed on register must be kept at RegsToPass 00623 // vector 00624 if (VA.isRegLoc()) { 00625 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 00626 } else { 00627 assert(VA.isMemLoc()); 00628 00629 if (!StackPtr.getNode()) 00630 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SP, getPointerTy()); 00631 00632 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), 00633 StackPtr, 00634 DAG.getIntPtrConstant(VA.getLocMemOffset())); 00635 00636 SDValue MemOp; 00637 ISD::ArgFlagsTy Flags = Outs[i].Flags; 00638 00639 if (Flags.isByVal()) { 00640 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16); 00641 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode, 00642 Flags.getByValAlign(), 00643 /*isVolatile*/false, 00644 /*AlwaysInline=*/true, 00645 MachinePointerInfo(), 00646 MachinePointerInfo()); 00647 } else { 00648 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(), 00649 false, false, 0); 00650 } 00651 00652 MemOpChains.push_back(MemOp); 00653 } 00654 } 00655 00656 // Transform all store nodes into one single node because all store nodes are 00657 // independent of each other. 00658 if (!MemOpChains.empty()) 00659 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 00660 00661 // Build a sequence of copy-to-reg nodes chained together with token chain and 00662 // flag operands which copy the outgoing args into registers. The InFlag in 00663 // necessary since all emitted instructions must be stuck together. 00664 SDValue InFlag; 00665 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 00666 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 00667 RegsToPass[i].second, InFlag); 00668 InFlag = Chain.getValue(1); 00669 } 00670 00671 // If the callee is a GlobalAddress node (quite common, every direct call is) 00672 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 00673 // Likewise ExternalSymbol -> TargetExternalSymbol. 00674 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 00675 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16); 00676 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) 00677 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16); 00678 00679 // Returns a chain & a flag for retval copy to use. 00680 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 00681 SmallVector<SDValue, 8> Ops; 00682 Ops.push_back(Chain); 00683 Ops.push_back(Callee); 00684 00685 // Add argument registers to the end of the list so that they are 00686 // known live into the call. 00687 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 00688 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 00689 RegsToPass[i].second.getValueType())); 00690 00691 if (InFlag.getNode()) 00692 Ops.push_back(InFlag); 00693 00694 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, Ops); 00695 InFlag = Chain.getValue(1); 00696 00697 // Create the CALLSEQ_END node. 00698 Chain = DAG.getCALLSEQ_END(Chain, 00699 DAG.getConstant(NumBytes, getPointerTy(), true), 00700 DAG.getConstant(0, getPointerTy(), true), 00701 InFlag, dl); 00702 InFlag = Chain.getValue(1); 00703 00704 // Handle result values, copying them out of physregs into vregs that we 00705 // return. 00706 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, 00707 DAG, InVals); 00708 } 00709 00710 /// LowerCallResult - Lower the result values of a call into the 00711 /// appropriate copies out of appropriate physical registers. 00712 /// 00713 SDValue 00714 MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 00715 CallingConv::ID CallConv, bool isVarArg, 00716 const SmallVectorImpl<ISD::InputArg> &Ins, 00717 SDLoc dl, SelectionDAG &DAG, 00718 SmallVectorImpl<SDValue> &InVals) const { 00719 00720 // Assign locations to each value returned by this call. 00721 SmallVector<CCValAssign, 16> RVLocs; 00722 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 00723 *DAG.getContext()); 00724 00725 AnalyzeReturnValues(CCInfo, RVLocs, Ins); 00726 00727 // Copy all of the result registers out of their specified physreg. 00728 for (unsigned i = 0; i != RVLocs.size(); ++i) { 00729 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(), 00730 RVLocs[i].getValVT(), InFlag).getValue(1); 00731 InFlag = Chain.getValue(2); 00732 InVals.push_back(Chain.getValue(0)); 00733 } 00734 00735 return Chain; 00736 } 00737 00738 SDValue MSP430TargetLowering::LowerShifts(SDValue Op, 00739 SelectionDAG &DAG) const { 00740 unsigned Opc = Op.getOpcode(); 00741 SDNode* N = Op.getNode(); 00742 EVT VT = Op.getValueType(); 00743 SDLoc dl(N); 00744 00745 // Expand non-constant shifts to loops: 00746 if (!isa<ConstantSDNode>(N->getOperand(1))) 00747 switch (Opc) { 00748 default: llvm_unreachable("Invalid shift opcode!"); 00749 case ISD::SHL: 00750 return DAG.getNode(MSP430ISD::SHL, dl, 00751 VT, N->getOperand(0), N->getOperand(1)); 00752 case ISD::SRA: 00753 return DAG.getNode(MSP430ISD::SRA, dl, 00754 VT, N->getOperand(0), N->getOperand(1)); 00755 case ISD::SRL: 00756 return DAG.getNode(MSP430ISD::SRL, dl, 00757 VT, N->getOperand(0), N->getOperand(1)); 00758 } 00759 00760 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 00761 00762 // Expand the stuff into sequence of shifts. 00763 // FIXME: for some shift amounts this might be done better! 00764 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N 00765 SDValue Victim = N->getOperand(0); 00766 00767 if (Opc == ISD::SRL && ShiftAmount) { 00768 // Emit a special goodness here: 00769 // srl A, 1 => clrc; rrc A 00770 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim); 00771 ShiftAmount -= 1; 00772 } 00773 00774 while (ShiftAmount--) 00775 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA), 00776 dl, VT, Victim); 00777 00778 return Victim; 00779 } 00780 00781 SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, 00782 SelectionDAG &DAG) const { 00783 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 00784 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); 00785 00786 // Create the TargetGlobalAddress node, folding in the constant offset. 00787 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op), 00788 getPointerTy(), Offset); 00789 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op), 00790 getPointerTy(), Result); 00791 } 00792 00793 SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op, 00794 SelectionDAG &DAG) const { 00795 SDLoc dl(Op); 00796 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); 00797 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy()); 00798 00799 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result); 00800 } 00801 00802 SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op, 00803 SelectionDAG &DAG) const { 00804 SDLoc dl(Op); 00805 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 00806 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy()); 00807 00808 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result); 00809 } 00810 00811 static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC, 00812 ISD::CondCode CC, 00813 SDLoc dl, SelectionDAG &DAG) { 00814 // FIXME: Handle bittests someday 00815 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet"); 00816 00817 // FIXME: Handle jump negative someday 00818 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID; 00819 switch (CC) { 00820 default: llvm_unreachable("Invalid integer condition!"); 00821 case ISD::SETEQ: 00822 TCC = MSP430CC::COND_E; // aka COND_Z 00823 // Minor optimization: if LHS is a constant, swap operands, then the 00824 // constant can be folded into comparison. 00825 if (LHS.getOpcode() == ISD::Constant) 00826 std::swap(LHS, RHS); 00827 break; 00828 case ISD::SETNE: 00829 TCC = MSP430CC::COND_NE; // aka COND_NZ 00830 // Minor optimization: if LHS is a constant, swap operands, then the 00831 // constant can be folded into comparison. 00832 if (LHS.getOpcode() == ISD::Constant) 00833 std::swap(LHS, RHS); 00834 break; 00835 case ISD::SETULE: 00836 std::swap(LHS, RHS); // FALLTHROUGH 00837 case ISD::SETUGE: 00838 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to 00839 // fold constant into instruction. 00840 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 00841 LHS = RHS; 00842 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0)); 00843 TCC = MSP430CC::COND_LO; 00844 break; 00845 } 00846 TCC = MSP430CC::COND_HS; // aka COND_C 00847 break; 00848 case ISD::SETUGT: 00849 std::swap(LHS, RHS); // FALLTHROUGH 00850 case ISD::SETULT: 00851 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to 00852 // fold constant into instruction. 00853 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 00854 LHS = RHS; 00855 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0)); 00856 TCC = MSP430CC::COND_HS; 00857 break; 00858 } 00859 TCC = MSP430CC::COND_LO; // aka COND_NC 00860 break; 00861 case ISD::SETLE: 00862 std::swap(LHS, RHS); // FALLTHROUGH 00863 case ISD::SETGE: 00864 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to 00865 // fold constant into instruction. 00866 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 00867 LHS = RHS; 00868 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0)); 00869 TCC = MSP430CC::COND_L; 00870 break; 00871 } 00872 TCC = MSP430CC::COND_GE; 00873 break; 00874 case ISD::SETGT: 00875 std::swap(LHS, RHS); // FALLTHROUGH 00876 case ISD::SETLT: 00877 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to 00878 // fold constant into instruction. 00879 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 00880 LHS = RHS; 00881 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0)); 00882 TCC = MSP430CC::COND_GE; 00883 break; 00884 } 00885 TCC = MSP430CC::COND_L; 00886 break; 00887 } 00888 00889 TargetCC = DAG.getConstant(TCC, MVT::i8); 00890 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS); 00891 } 00892 00893 00894 SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 00895 SDValue Chain = Op.getOperand(0); 00896 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 00897 SDValue LHS = Op.getOperand(2); 00898 SDValue RHS = Op.getOperand(3); 00899 SDValue Dest = Op.getOperand(4); 00900 SDLoc dl (Op); 00901 00902 SDValue TargetCC; 00903 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); 00904 00905 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(), 00906 Chain, Dest, TargetCC, Flag); 00907 } 00908 00909 SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { 00910 SDValue LHS = Op.getOperand(0); 00911 SDValue RHS = Op.getOperand(1); 00912 SDLoc dl (Op); 00913 00914 // If we are doing an AND and testing against zero, then the CMP 00915 // will not be generated. The AND (or BIT) will generate the condition codes, 00916 // but they are different from CMP. 00917 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so 00918 // lowering & isel wouldn't diverge. 00919 bool andCC = false; 00920 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { 00921 if (RHSC->isNullValue() && LHS.hasOneUse() && 00922 (LHS.getOpcode() == ISD::AND || 00923 (LHS.getOpcode() == ISD::TRUNCATE && 00924 LHS.getOperand(0).getOpcode() == ISD::AND))) { 00925 andCC = true; 00926 } 00927 } 00928 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 00929 SDValue TargetCC; 00930 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); 00931 00932 // Get the condition codes directly from the status register, if its easy. 00933 // Otherwise a branch will be generated. Note that the AND and BIT 00934 // instructions generate different flags than CMP, the carry bit can be used 00935 // for NE/EQ. 00936 bool Invert = false; 00937 bool Shift = false; 00938 bool Convert = true; 00939 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) { 00940 default: 00941 Convert = false; 00942 break; 00943 case MSP430CC::COND_HS: 00944 // Res = SR & 1, no processing is required 00945 break; 00946 case MSP430CC::COND_LO: 00947 // Res = ~(SR & 1) 00948 Invert = true; 00949 break; 00950 case MSP430CC::COND_NE: 00951 if (andCC) { 00952 // C = ~Z, thus Res = SR & 1, no processing is required 00953 } else { 00954 // Res = ~((SR >> 1) & 1) 00955 Shift = true; 00956 Invert = true; 00957 } 00958 break; 00959 case MSP430CC::COND_E: 00960 Shift = true; 00961 // C = ~Z for AND instruction, thus we can put Res = ~(SR & 1), however, 00962 // Res = (SR >> 1) & 1 is 1 word shorter. 00963 break; 00964 } 00965 EVT VT = Op.getValueType(); 00966 SDValue One = DAG.getConstant(1, VT); 00967 if (Convert) { 00968 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SR, 00969 MVT::i16, Flag); 00970 if (Shift) 00971 // FIXME: somewhere this is turned into a SRL, lower it MSP specific? 00972 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One); 00973 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One); 00974 if (Invert) 00975 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One); 00976 return SR; 00977 } else { 00978 SDValue Zero = DAG.getConstant(0, VT); 00979 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 00980 SmallVector<SDValue, 4> Ops; 00981 Ops.push_back(One); 00982 Ops.push_back(Zero); 00983 Ops.push_back(TargetCC); 00984 Ops.push_back(Flag); 00985 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops); 00986 } 00987 } 00988 00989 SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op, 00990 SelectionDAG &DAG) const { 00991 SDValue LHS = Op.getOperand(0); 00992 SDValue RHS = Op.getOperand(1); 00993 SDValue TrueV = Op.getOperand(2); 00994 SDValue FalseV = Op.getOperand(3); 00995 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 00996 SDLoc dl (Op); 00997 00998 SDValue TargetCC; 00999 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); 01000 01001 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 01002 SmallVector<SDValue, 4> Ops; 01003 Ops.push_back(TrueV); 01004 Ops.push_back(FalseV); 01005 Ops.push_back(TargetCC); 01006 Ops.push_back(Flag); 01007 01008 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops); 01009 } 01010 01011 SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op, 01012 SelectionDAG &DAG) const { 01013 SDValue Val = Op.getOperand(0); 01014 EVT VT = Op.getValueType(); 01015 SDLoc dl(Op); 01016 01017 assert(VT == MVT::i16 && "Only support i16 for now!"); 01018 01019 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT, 01020 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val), 01021 DAG.getValueType(Val.getValueType())); 01022 } 01023 01024 SDValue 01025 MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const { 01026 MachineFunction &MF = DAG.getMachineFunction(); 01027 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>(); 01028 int ReturnAddrIndex = FuncInfo->getRAIndex(); 01029 01030 if (ReturnAddrIndex == 0) { 01031 // Set up a frame object for the return address. 01032 uint64_t SlotSize = getDataLayout()->getPointerSize(); 01033 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize, 01034 true); 01035 FuncInfo->setRAIndex(ReturnAddrIndex); 01036 } 01037 01038 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); 01039 } 01040 01041 SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op, 01042 SelectionDAG &DAG) const { 01043 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 01044 MFI->setReturnAddressIsTaken(true); 01045 01046 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 01047 return SDValue(); 01048 01049 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 01050 SDLoc dl(Op); 01051 01052 if (Depth > 0) { 01053 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 01054 SDValue Offset = 01055 DAG.getConstant(getDataLayout()->getPointerSize(), MVT::i16); 01056 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), 01057 DAG.getNode(ISD::ADD, dl, getPointerTy(), 01058 FrameAddr, Offset), 01059 MachinePointerInfo(), false, false, false, 0); 01060 } 01061 01062 // Just load the return address. 01063 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG); 01064 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), 01065 RetAddrFI, MachinePointerInfo(), false, false, false, 0); 01066 } 01067 01068 SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op, 01069 SelectionDAG &DAG) const { 01070 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 01071 MFI->setFrameAddressIsTaken(true); 01072 01073 EVT VT = Op.getValueType(); 01074 SDLoc dl(Op); // FIXME probably not meaningful 01075 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 01076 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, 01077 MSP430::FP, VT); 01078 while (Depth--) 01079 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 01080 MachinePointerInfo(), 01081 false, false, false, 0); 01082 return FrameAddr; 01083 } 01084 01085 SDValue MSP430TargetLowering::LowerVASTART(SDValue Op, 01086 SelectionDAG &DAG) const { 01087 MachineFunction &MF = DAG.getMachineFunction(); 01088 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>(); 01089 01090 // Frame index of first vararg argument 01091 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), 01092 getPointerTy()); 01093 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 01094 01095 // Create a store of the frame index to the location operand 01096 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex, 01097 Op.getOperand(1), MachinePointerInfo(SV), 01098 false, false, 0); 01099 } 01100 01101 SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op, 01102 SelectionDAG &DAG) const { 01103 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 01104 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy()); 01105 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT), 01106 getPointerTy(), Result); 01107 } 01108 01109 /// getPostIndexedAddressParts - returns true by value, base pointer and 01110 /// offset pointer and addressing mode by reference if this node can be 01111 /// combined with a load / store to form a post-indexed load / store. 01112 bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 01113 SDValue &Base, 01114 SDValue &Offset, 01115 ISD::MemIndexedMode &AM, 01116 SelectionDAG &DAG) const { 01117 01118 LoadSDNode *LD = cast<LoadSDNode>(N); 01119 if (LD->getExtensionType() != ISD::NON_EXTLOAD) 01120 return false; 01121 01122 EVT VT = LD->getMemoryVT(); 01123 if (VT != MVT::i8 && VT != MVT::i16) 01124 return false; 01125 01126 if (Op->getOpcode() != ISD::ADD) 01127 return false; 01128 01129 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) { 01130 uint64_t RHSC = RHS->getZExtValue(); 01131 if ((VT == MVT::i16 && RHSC != 2) || 01132 (VT == MVT::i8 && RHSC != 1)) 01133 return false; 01134 01135 Base = Op->getOperand(0); 01136 Offset = DAG.getConstant(RHSC, VT); 01137 AM = ISD::POST_INC; 01138 return true; 01139 } 01140 01141 return false; 01142 } 01143 01144 01145 const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const { 01146 switch (Opcode) { 01147 default: return nullptr; 01148 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG"; 01149 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG"; 01150 case MSP430ISD::RRA: return "MSP430ISD::RRA"; 01151 case MSP430ISD::RLA: return "MSP430ISD::RLA"; 01152 case MSP430ISD::RRC: return "MSP430ISD::RRC"; 01153 case MSP430ISD::CALL: return "MSP430ISD::CALL"; 01154 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper"; 01155 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC"; 01156 case MSP430ISD::CMP: return "MSP430ISD::CMP"; 01157 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC"; 01158 case MSP430ISD::SHL: return "MSP430ISD::SHL"; 01159 case MSP430ISD::SRA: return "MSP430ISD::SRA"; 01160 } 01161 } 01162 01163 bool MSP430TargetLowering::isTruncateFree(Type *Ty1, 01164 Type *Ty2) const { 01165 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 01166 return false; 01167 01168 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits()); 01169 } 01170 01171 bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { 01172 if (!VT1.isInteger() || !VT2.isInteger()) 01173 return false; 01174 01175 return (VT1.getSizeInBits() > VT2.getSizeInBits()); 01176 } 01177 01178 bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const { 01179 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers. 01180 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16); 01181 } 01182 01183 bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const { 01184 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers. 01185 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16; 01186 } 01187 01188 bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 01189 return isZExtFree(Val.getValueType(), VT2); 01190 } 01191 01192 //===----------------------------------------------------------------------===// 01193 // Other Lowering Code 01194 //===----------------------------------------------------------------------===// 01195 01196 MachineBasicBlock* 01197 MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI, 01198 MachineBasicBlock *BB) const { 01199 MachineFunction *F = BB->getParent(); 01200 MachineRegisterInfo &RI = F->getRegInfo(); 01201 DebugLoc dl = MI->getDebugLoc(); 01202 const TargetInstrInfo &TII = 01203 *getTargetMachine().getSubtargetImpl()->getInstrInfo(); 01204 01205 unsigned Opc; 01206 const TargetRegisterClass * RC; 01207 switch (MI->getOpcode()) { 01208 default: llvm_unreachable("Invalid shift opcode!"); 01209 case MSP430::Shl8: 01210 Opc = MSP430::SHL8r1; 01211 RC = &MSP430::GR8RegClass; 01212 break; 01213 case MSP430::Shl16: 01214 Opc = MSP430::SHL16r1; 01215 RC = &MSP430::GR16RegClass; 01216 break; 01217 case MSP430::Sra8: 01218 Opc = MSP430::SAR8r1; 01219 RC = &MSP430::GR8RegClass; 01220 break; 01221 case MSP430::Sra16: 01222 Opc = MSP430::SAR16r1; 01223 RC = &MSP430::GR16RegClass; 01224 break; 01225 case MSP430::Srl8: 01226 Opc = MSP430::SAR8r1c; 01227 RC = &MSP430::GR8RegClass; 01228 break; 01229 case MSP430::Srl16: 01230 Opc = MSP430::SAR16r1c; 01231 RC = &MSP430::GR16RegClass; 01232 break; 01233 } 01234 01235 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 01236 MachineFunction::iterator I = BB; 01237 ++I; 01238 01239 // Create loop block 01240 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB); 01241 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB); 01242 01243 F->insert(I, LoopBB); 01244 F->insert(I, RemBB); 01245 01246 // Update machine-CFG edges by transferring all successors of the current 01247 // block to the block containing instructions after shift. 01248 RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)), 01249 BB->end()); 01250 RemBB->transferSuccessorsAndUpdatePHIs(BB); 01251 01252 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB 01253 BB->addSuccessor(LoopBB); 01254 BB->addSuccessor(RemBB); 01255 LoopBB->addSuccessor(RemBB); 01256 LoopBB->addSuccessor(LoopBB); 01257 01258 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass); 01259 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass); 01260 unsigned ShiftReg = RI.createVirtualRegister(RC); 01261 unsigned ShiftReg2 = RI.createVirtualRegister(RC); 01262 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg(); 01263 unsigned SrcReg = MI->getOperand(1).getReg(); 01264 unsigned DstReg = MI->getOperand(0).getReg(); 01265 01266 // BB: 01267 // cmp 0, N 01268 // je RemBB 01269 BuildMI(BB, dl, TII.get(MSP430::CMP8ri)) 01270 .addReg(ShiftAmtSrcReg).addImm(0); 01271 BuildMI(BB, dl, TII.get(MSP430::JCC)) 01272 .addMBB(RemBB) 01273 .addImm(MSP430CC::COND_E); 01274 01275 // LoopBB: 01276 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB] 01277 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB] 01278 // ShiftReg2 = shift ShiftReg 01279 // ShiftAmt2 = ShiftAmt - 1; 01280 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg) 01281 .addReg(SrcReg).addMBB(BB) 01282 .addReg(ShiftReg2).addMBB(LoopBB); 01283 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg) 01284 .addReg(ShiftAmtSrcReg).addMBB(BB) 01285 .addReg(ShiftAmtReg2).addMBB(LoopBB); 01286 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2) 01287 .addReg(ShiftReg); 01288 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2) 01289 .addReg(ShiftAmtReg).addImm(1); 01290 BuildMI(LoopBB, dl, TII.get(MSP430::JCC)) 01291 .addMBB(LoopBB) 01292 .addImm(MSP430CC::COND_NE); 01293 01294 // RemBB: 01295 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB] 01296 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg) 01297 .addReg(SrcReg).addMBB(BB) 01298 .addReg(ShiftReg2).addMBB(LoopBB); 01299 01300 MI->eraseFromParent(); // The pseudo instruction is gone now. 01301 return RemBB; 01302 } 01303 01304 MachineBasicBlock* 01305 MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 01306 MachineBasicBlock *BB) const { 01307 unsigned Opc = MI->getOpcode(); 01308 01309 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 || 01310 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 || 01311 Opc == MSP430::Srl8 || Opc == MSP430::Srl16) 01312 return EmitShiftInstr(MI, BB); 01313 01314 const TargetInstrInfo &TII = 01315 *getTargetMachine().getSubtargetImpl()->getInstrInfo(); 01316 DebugLoc dl = MI->getDebugLoc(); 01317 01318 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) && 01319 "Unexpected instr type to insert"); 01320 01321 // To "insert" a SELECT instruction, we actually have to insert the diamond 01322 // control-flow pattern. The incoming instruction knows the destination vreg 01323 // to set, the condition code register to branch on, the true/false values to 01324 // select between, and a branch opcode to use. 01325 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 01326 MachineFunction::iterator I = BB; 01327 ++I; 01328 01329 // thisMBB: 01330 // ... 01331 // TrueVal = ... 01332 // cmpTY ccX, r1, r2 01333 // jCC copy1MBB 01334 // fallthrough --> copy0MBB 01335 MachineBasicBlock *thisMBB = BB; 01336 MachineFunction *F = BB->getParent(); 01337 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 01338 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB); 01339 F->insert(I, copy0MBB); 01340 F->insert(I, copy1MBB); 01341 // Update machine-CFG edges by transferring all successors of the current 01342 // block to the new block which will contain the Phi node for the select. 01343 copy1MBB->splice(copy1MBB->begin(), BB, 01344 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 01345 copy1MBB->transferSuccessorsAndUpdatePHIs(BB); 01346 // Next, add the true and fallthrough blocks as its successors. 01347 BB->addSuccessor(copy0MBB); 01348 BB->addSuccessor(copy1MBB); 01349 01350 BuildMI(BB, dl, TII.get(MSP430::JCC)) 01351 .addMBB(copy1MBB) 01352 .addImm(MI->getOperand(3).getImm()); 01353 01354 // copy0MBB: 01355 // %FalseValue = ... 01356 // # fallthrough to copy1MBB 01357 BB = copy0MBB; 01358 01359 // Update machine-CFG edges 01360 BB->addSuccessor(copy1MBB); 01361 01362 // copy1MBB: 01363 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 01364 // ... 01365 BB = copy1MBB; 01366 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI), 01367 MI->getOperand(0).getReg()) 01368 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB) 01369 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB); 01370 01371 MI->eraseFromParent(); // The pseudo instruction is gone now. 01372 return BB; 01373 }