1 //===-- MSP430ISelLowering.cpp - MSP430 DAG Lowering Implementation ------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the MSP430TargetLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MSP430ISelLowering.h" 15 #include "MSP430.h" 16 #include "MSP430MachineFunctionInfo.h" 17 #include "MSP430Subtarget.h" 18 #include "MSP430TargetMachine.h" 19 #include "llvm/CodeGen/CallingConvLower.h" 20 #include "llvm/CodeGen/MachineFrameInfo.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineInstrBuilder.h" 23 #include "llvm/CodeGen/MachineRegisterInfo.h" 24 #include "llvm/CodeGen/SelectionDAGISel.h" 25 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 26 #include "llvm/CodeGen/ValueTypes.h" 27 #include "llvm/IR/CallingConv.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/Function.h" 30 #include "llvm/IR/GlobalAlias.h" 31 #include "llvm/IR/GlobalVariable.h" 32 #include "llvm/IR/Intrinsics.h" 33 #include "llvm/Support/CommandLine.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/raw_ostream.h" 37 using namespace llvm; 38 39 #define DEBUG_TYPE "msp430-lower" 40 41 typedef enum { 42 NoHWMult, 43 HWMultIntr, 44 HWMultNoIntr 45 } HWMultUseMode; 46 47 static cl::opt<HWMultUseMode> 48 HWMultMode("msp430-hwmult-mode", cl::Hidden, 49 cl::desc("Hardware multiplier use mode"), 50 cl::init(HWMultNoIntr), 51 cl::values( 52 clEnumValN(NoHWMult, "no", 53 "Do not use hardware multiplier"), 54 clEnumValN(HWMultIntr, "interrupts", 55 "Assume hardware multiplier can be used inside interrupts"), 56 clEnumValN(HWMultNoIntr, "use", 57 "Assume hardware multiplier cannot be used inside interrupts"), 58 clEnumValEnd)); 59 60 MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM, 61 const MSP430Subtarget &STI) 62 : TargetLowering(TM) { 63 64 // Set up the register classes. 65 addRegisterClass(MVT::i8, &MSP430::GR8RegClass); 66 addRegisterClass(MVT::i16, &MSP430::GR16RegClass); 67 68 // Compute derived properties from the register classes 69 computeRegisterProperties(STI.getRegisterInfo()); 70 71 // Provide all sorts of operation actions 72 setStackPointerRegisterToSaveRestore(MSP430::SP); 73 setBooleanContents(ZeroOrOneBooleanContent); 74 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct? 75 76 // We have post-incremented loads / stores. 77 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal); 78 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal); 79 80 for (MVT VT : MVT::integer_valuetypes()) { 81 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 82 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 83 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 84 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand); 85 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Expand); 86 } 87 88 // We don't have any truncstores 89 setTruncStoreAction(MVT::i16, MVT::i8, Expand); 90 91 setOperationAction(ISD::SRA, MVT::i8, Custom); 92 setOperationAction(ISD::SHL, MVT::i8, Custom); 93 setOperationAction(ISD::SRL, MVT::i8, Custom); 94 setOperationAction(ISD::SRA, MVT::i16, Custom); 95 setOperationAction(ISD::SHL, MVT::i16, Custom); 96 setOperationAction(ISD::SRL, MVT::i16, Custom); 97 setOperationAction(ISD::ROTL, MVT::i8, Expand); 98 setOperationAction(ISD::ROTR, MVT::i8, Expand); 99 setOperationAction(ISD::ROTL, MVT::i16, Expand); 100 setOperationAction(ISD::ROTR, MVT::i16, Expand); 101 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom); 102 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom); 103 setOperationAction(ISD::BlockAddress, MVT::i16, Custom); 104 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 105 setOperationAction(ISD::BR_CC, MVT::i8, Custom); 106 setOperationAction(ISD::BR_CC, MVT::i16, Custom); 107 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 108 setOperationAction(ISD::SETCC, MVT::i8, Custom); 109 setOperationAction(ISD::SETCC, MVT::i16, Custom); 110 setOperationAction(ISD::SELECT, MVT::i8, Expand); 111 setOperationAction(ISD::SELECT, MVT::i16, Expand); 112 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom); 113 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom); 114 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom); 115 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand); 116 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand); 117 118 setOperationAction(ISD::CTTZ, MVT::i8, Expand); 119 setOperationAction(ISD::CTTZ, MVT::i16, Expand); 120 setOperationAction(ISD::CTLZ, MVT::i8, Expand); 121 setOperationAction(ISD::CTLZ, MVT::i16, Expand); 122 setOperationAction(ISD::CTPOP, MVT::i8, Expand); 123 setOperationAction(ISD::CTPOP, MVT::i16, Expand); 124 125 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand); 126 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand); 127 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand); 128 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand); 129 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand); 130 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand); 131 132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 133 134 // FIXME: Implement efficiently multiplication by a constant 135 setOperationAction(ISD::MUL, MVT::i8, Expand); 136 setOperationAction(ISD::MULHS, MVT::i8, Expand); 137 setOperationAction(ISD::MULHU, MVT::i8, Expand); 138 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand); 139 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand); 140 setOperationAction(ISD::MUL, MVT::i16, Expand); 141 setOperationAction(ISD::MULHS, MVT::i16, Expand); 142 setOperationAction(ISD::MULHU, MVT::i16, Expand); 143 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand); 144 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand); 145 146 setOperationAction(ISD::UDIV, MVT::i8, Expand); 147 setOperationAction(ISD::UDIVREM, MVT::i8, Expand); 148 setOperationAction(ISD::UREM, MVT::i8, Expand); 149 setOperationAction(ISD::SDIV, MVT::i8, Expand); 150 setOperationAction(ISD::SDIVREM, MVT::i8, Expand); 151 setOperationAction(ISD::SREM, MVT::i8, Expand); 152 setOperationAction(ISD::UDIV, MVT::i16, Expand); 153 setOperationAction(ISD::UDIVREM, MVT::i16, Expand); 154 setOperationAction(ISD::UREM, MVT::i16, Expand); 155 setOperationAction(ISD::SDIV, MVT::i16, Expand); 156 setOperationAction(ISD::SDIVREM, MVT::i16, Expand); 157 setOperationAction(ISD::SREM, MVT::i16, Expand); 158 159 // varargs support 160 setOperationAction(ISD::VASTART, MVT::Other, Custom); 161 setOperationAction(ISD::VAARG, MVT::Other, Expand); 162 setOperationAction(ISD::VAEND, MVT::Other, Expand); 163 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 164 setOperationAction(ISD::JumpTable, MVT::i16, Custom); 165 166 // Libcalls names. 167 if (HWMultMode == HWMultIntr) { 168 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw"); 169 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw"); 170 } else if (HWMultMode == HWMultNoIntr) { 171 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint"); 172 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint"); 173 } 174 175 setMinFunctionAlignment(1); 176 setPrefFunctionAlignment(2); 177 } 178 179 SDValue MSP430TargetLowering::LowerOperation(SDValue Op, 180 SelectionDAG &DAG) const { 181 switch (Op.getOpcode()) { 182 case ISD::SHL: // FALLTHROUGH 183 case ISD::SRL: 184 case ISD::SRA: return LowerShifts(Op, DAG); 185 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 186 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 187 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); 188 case ISD::SETCC: return LowerSETCC(Op, DAG); 189 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 190 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 191 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG); 192 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 193 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 194 case ISD::VASTART: return LowerVASTART(Op, DAG); 195 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 196 default: 197 llvm_unreachable("unimplemented operand"); 198 } 199 } 200 201 //===----------------------------------------------------------------------===// 202 // MSP430 Inline Assembly Support 203 //===----------------------------------------------------------------------===// 204 205 /// getConstraintType - Given a constraint letter, return the type of 206 /// constraint it is for this target. 207 TargetLowering::ConstraintType 208 MSP430TargetLowering::getConstraintType(StringRef Constraint) const { 209 if (Constraint.size() == 1) { 210 switch (Constraint[0]) { 211 case 'r': 212 return C_RegisterClass; 213 default: 214 break; 215 } 216 } 217 return TargetLowering::getConstraintType(Constraint); 218 } 219 220 std::pair<unsigned, const TargetRegisterClass *> 221 MSP430TargetLowering::getRegForInlineAsmConstraint( 222 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 223 if (Constraint.size() == 1) { 224 // GCC Constraint Letters 225 switch (Constraint[0]) { 226 default: break; 227 case 'r': // GENERAL_REGS 228 if (VT == MVT::i8) 229 return std::make_pair(0U, &MSP430::GR8RegClass); 230 231 return std::make_pair(0U, &MSP430::GR16RegClass); 232 } 233 } 234 235 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 236 } 237 238 //===----------------------------------------------------------------------===// 239 // Calling Convention Implementation 240 //===----------------------------------------------------------------------===// 241 242 #include "MSP430GenCallingConv.inc" 243 244 /// For each argument in a function store the number of pieces it is composed 245 /// of. 246 template<typename ArgT> 247 static void ParseFunctionArgs(const SmallVectorImpl<ArgT> &Args, 248 SmallVectorImpl<unsigned> &Out) { 249 unsigned CurrentArgIndex = ~0U; 250 for (unsigned i = 0, e = Args.size(); i != e; i++) { 251 if (CurrentArgIndex == Args[i].OrigArgIndex) { 252 Out.back()++; 253 } else { 254 Out.push_back(1); 255 CurrentArgIndex++; 256 } 257 } 258 } 259 260 static void AnalyzeVarArgs(CCState &State, 261 const SmallVectorImpl<ISD::OutputArg> &Outs) { 262 State.AnalyzeCallOperands(Outs, CC_MSP430_AssignStack); 263 } 264 265 static void AnalyzeVarArgs(CCState &State, 266 const SmallVectorImpl<ISD::InputArg> &Ins) { 267 State.AnalyzeFormalArguments(Ins, CC_MSP430_AssignStack); 268 } 269 270 /// Analyze incoming and outgoing function arguments. We need custom C++ code 271 /// to handle special constraints in the ABI like reversing the order of the 272 /// pieces of splitted arguments. In addition, all pieces of a certain argument 273 /// have to be passed either using registers or the stack but never mixing both. 274 template<typename ArgT> 275 static void AnalyzeArguments(CCState &State, 276 SmallVectorImpl<CCValAssign> &ArgLocs, 277 const SmallVectorImpl<ArgT> &Args) { 278 static const MCPhysReg RegList[] = { 279 MSP430::R15, MSP430::R14, MSP430::R13, MSP430::R12 280 }; 281 static const unsigned NbRegs = array_lengthof(RegList); 282 283 if (State.isVarArg()) { 284 AnalyzeVarArgs(State, Args); 285 return; 286 } 287 288 SmallVector<unsigned, 4> ArgsParts; 289 ParseFunctionArgs(Args, ArgsParts); 290 291 unsigned RegsLeft = NbRegs; 292 bool UseStack = false; 293 unsigned ValNo = 0; 294 295 for (unsigned i = 0, e = ArgsParts.size(); i != e; i++) { 296 MVT ArgVT = Args[ValNo].VT; 297 ISD::ArgFlagsTy ArgFlags = Args[ValNo].Flags; 298 MVT LocVT = ArgVT; 299 CCValAssign::LocInfo LocInfo = CCValAssign::Full; 300 301 // Promote i8 to i16 302 if (LocVT == MVT::i8) { 303 LocVT = MVT::i16; 304 if (ArgFlags.isSExt()) 305 LocInfo = CCValAssign::SExt; 306 else if (ArgFlags.isZExt()) 307 LocInfo = CCValAssign::ZExt; 308 else 309 LocInfo = CCValAssign::AExt; 310 } 311 312 // Handle byval arguments 313 if (ArgFlags.isByVal()) { 314 State.HandleByVal(ValNo++, ArgVT, LocVT, LocInfo, 2, 2, ArgFlags); 315 continue; 316 } 317 318 unsigned Parts = ArgsParts[i]; 319 320 if (!UseStack && Parts <= RegsLeft) { 321 unsigned FirstVal = ValNo; 322 for (unsigned j = 0; j < Parts; j++) { 323 unsigned Reg = State.AllocateReg(RegList); 324 State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo)); 325 RegsLeft--; 326 } 327 328 // Reverse the order of the pieces to agree with the "big endian" format 329 // required in the calling convention ABI. 330 SmallVectorImpl<CCValAssign>::iterator B = ArgLocs.begin() + FirstVal; 331 std::reverse(B, B + Parts); 332 } else { 333 UseStack = true; 334 for (unsigned j = 0; j < Parts; j++) 335 CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State); 336 } 337 } 338 } 339 340 static void AnalyzeRetResult(CCState &State, 341 const SmallVectorImpl<ISD::InputArg> &Ins) { 342 State.AnalyzeCallResult(Ins, RetCC_MSP430); 343 } 344 345 static void AnalyzeRetResult(CCState &State, 346 const SmallVectorImpl<ISD::OutputArg> &Outs) { 347 State.AnalyzeReturn(Outs, RetCC_MSP430); 348 } 349 350 template<typename ArgT> 351 static void AnalyzeReturnValues(CCState &State, 352 SmallVectorImpl<CCValAssign> &RVLocs, 353 const SmallVectorImpl<ArgT> &Args) { 354 AnalyzeRetResult(State, Args); 355 356 // Reverse splitted return values to get the "big endian" format required 357 // to agree with the calling convention ABI. 358 std::reverse(RVLocs.begin(), RVLocs.end()); 359 } 360 361 SDValue 362 MSP430TargetLowering::LowerFormalArguments(SDValue Chain, 363 CallingConv::ID CallConv, 364 bool isVarArg, 365 const SmallVectorImpl<ISD::InputArg> 366 &Ins, 367 SDLoc dl, 368 SelectionDAG &DAG, 369 SmallVectorImpl<SDValue> &InVals) 370 const { 371 372 switch (CallConv) { 373 default: 374 llvm_unreachable("Unsupported calling convention"); 375 case CallingConv::C: 376 case CallingConv::Fast: 377 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals); 378 case CallingConv::MSP430_INTR: 379 if (Ins.empty()) 380 return Chain; 381 report_fatal_error("ISRs cannot have arguments"); 382 } 383 } 384 385 SDValue 386 MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 387 SmallVectorImpl<SDValue> &InVals) const { 388 SelectionDAG &DAG = CLI.DAG; 389 SDLoc &dl = CLI.DL; 390 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 391 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 392 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 393 SDValue Chain = CLI.Chain; 394 SDValue Callee = CLI.Callee; 395 bool &isTailCall = CLI.IsTailCall; 396 CallingConv::ID CallConv = CLI.CallConv; 397 bool isVarArg = CLI.IsVarArg; 398 399 // MSP430 target does not yet support tail call optimization. 400 isTailCall = false; 401 402 switch (CallConv) { 403 default: 404 llvm_unreachable("Unsupported calling convention"); 405 case CallingConv::Fast: 406 case CallingConv::C: 407 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall, 408 Outs, OutVals, Ins, dl, DAG, InVals); 409 case CallingConv::MSP430_INTR: 410 report_fatal_error("ISRs cannot be called directly"); 411 } 412 } 413 414 /// LowerCCCArguments - transform physical registers into virtual registers and 415 /// generate load operations for arguments places on the stack. 416 // FIXME: struct return stuff 417 SDValue 418 MSP430TargetLowering::LowerCCCArguments(SDValue Chain, 419 CallingConv::ID CallConv, 420 bool isVarArg, 421 const SmallVectorImpl<ISD::InputArg> 422 &Ins, 423 SDLoc dl, 424 SelectionDAG &DAG, 425 SmallVectorImpl<SDValue> &InVals) 426 const { 427 MachineFunction &MF = DAG.getMachineFunction(); 428 MachineFrameInfo *MFI = MF.getFrameInfo(); 429 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 430 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>(); 431 432 // Assign locations to all of the incoming arguments. 433 SmallVector<CCValAssign, 16> ArgLocs; 434 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 435 *DAG.getContext()); 436 AnalyzeArguments(CCInfo, ArgLocs, Ins); 437 438 // Create frame index for the start of the first vararg value 439 if (isVarArg) { 440 unsigned Offset = CCInfo.getNextStackOffset(); 441 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, Offset, true)); 442 } 443 444 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 445 CCValAssign &VA = ArgLocs[i]; 446 if (VA.isRegLoc()) { 447 // Arguments passed in registers 448 EVT RegVT = VA.getLocVT(); 449 switch (RegVT.getSimpleVT().SimpleTy) { 450 default: 451 { 452 #ifndef NDEBUG 453 errs() << "LowerFormalArguments Unhandled argument type: " 454 << RegVT.getEVTString() << "\n"; 455 #endif 456 llvm_unreachable(nullptr); 457 } 458 case MVT::i16: 459 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass); 460 RegInfo.addLiveIn(VA.getLocReg(), VReg); 461 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT); 462 463 // If this is an 8-bit value, it is really passed promoted to 16 464 // bits. Insert an assert[sz]ext to capture this, then truncate to the 465 // right size. 466 if (VA.getLocInfo() == CCValAssign::SExt) 467 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 468 DAG.getValueType(VA.getValVT())); 469 else if (VA.getLocInfo() == CCValAssign::ZExt) 470 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 471 DAG.getValueType(VA.getValVT())); 472 473 if (VA.getLocInfo() != CCValAssign::Full) 474 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 475 476 InVals.push_back(ArgValue); 477 } 478 } else { 479 // Sanity check 480 assert(VA.isMemLoc()); 481 482 SDValue InVal; 483 ISD::ArgFlagsTy Flags = Ins[i].Flags; 484 485 if (Flags.isByVal()) { 486 int FI = MFI->CreateFixedObject(Flags.getByValSize(), 487 VA.getLocMemOffset(), true); 488 InVal = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 489 } else { 490 // Load the argument to a virtual register 491 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8; 492 if (ObjSize > 2) { 493 errs() << "LowerFormalArguments Unhandled argument type: " 494 << EVT(VA.getLocVT()).getEVTString() 495 << "\n"; 496 } 497 // Create the frame index object for this incoming parameter... 498 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true); 499 500 // Create the SelectionDAG nodes corresponding to a load 501 //from this parameter 502 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16); 503 InVal = DAG.getLoad( 504 VA.getLocVT(), dl, Chain, FIN, 505 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), 506 false, false, false, 0); 507 } 508 509 InVals.push_back(InVal); 510 } 511 } 512 513 return Chain; 514 } 515 516 SDValue 517 MSP430TargetLowering::LowerReturn(SDValue Chain, 518 CallingConv::ID CallConv, bool isVarArg, 519 const SmallVectorImpl<ISD::OutputArg> &Outs, 520 const SmallVectorImpl<SDValue> &OutVals, 521 SDLoc dl, SelectionDAG &DAG) const { 522 523 // CCValAssign - represent the assignment of the return value to a location 524 SmallVector<CCValAssign, 16> RVLocs; 525 526 // ISRs cannot return any value. 527 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty()) 528 report_fatal_error("ISRs cannot return any value"); 529 530 // CCState - Info about the registers and stack slot. 531 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 532 *DAG.getContext()); 533 534 // Analize return values. 535 AnalyzeReturnValues(CCInfo, RVLocs, Outs); 536 537 SDValue Flag; 538 SmallVector<SDValue, 4> RetOps(1, Chain); 539 540 // Copy the result values into the output registers. 541 for (unsigned i = 0; i != RVLocs.size(); ++i) { 542 CCValAssign &VA = RVLocs[i]; 543 assert(VA.isRegLoc() && "Can only return in registers!"); 544 545 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 546 OutVals[i], Flag); 547 548 // Guarantee that all emitted copies are stuck together, 549 // avoiding something bad. 550 Flag = Chain.getValue(1); 551 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 552 } 553 554 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ? 555 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG); 556 557 RetOps[0] = Chain; // Update chain. 558 559 // Add the flag if we have it. 560 if (Flag.getNode()) 561 RetOps.push_back(Flag); 562 563 return DAG.getNode(Opc, dl, MVT::Other, RetOps); 564 } 565 566 /// LowerCCCCallTo - functions arguments are copied from virtual regs to 567 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. 568 // TODO: sret. 569 SDValue 570 MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee, 571 CallingConv::ID CallConv, bool isVarArg, 572 bool isTailCall, 573 const SmallVectorImpl<ISD::OutputArg> 574 &Outs, 575 const SmallVectorImpl<SDValue> &OutVals, 576 const SmallVectorImpl<ISD::InputArg> &Ins, 577 SDLoc dl, SelectionDAG &DAG, 578 SmallVectorImpl<SDValue> &InVals) const { 579 // Analyze operands of the call, assigning locations to each operand. 580 SmallVector<CCValAssign, 16> ArgLocs; 581 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 582 *DAG.getContext()); 583 AnalyzeArguments(CCInfo, ArgLocs, Outs); 584 585 // Get a count of how many bytes are to be pushed on the stack. 586 unsigned NumBytes = CCInfo.getNextStackOffset(); 587 auto PtrVT = getPointerTy(DAG.getDataLayout()); 588 589 Chain = DAG.getCALLSEQ_START(Chain, 590 DAG.getConstant(NumBytes, dl, PtrVT, true), dl); 591 592 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass; 593 SmallVector<SDValue, 12> MemOpChains; 594 SDValue StackPtr; 595 596 // Walk the register/memloc assignments, inserting copies/loads. 597 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 598 CCValAssign &VA = ArgLocs[i]; 599 600 SDValue Arg = OutVals[i]; 601 602 // Promote the value if needed. 603 switch (VA.getLocInfo()) { 604 default: llvm_unreachable("Unknown loc info!"); 605 case CCValAssign::Full: break; 606 case CCValAssign::SExt: 607 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 608 break; 609 case CCValAssign::ZExt: 610 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 611 break; 612 case CCValAssign::AExt: 613 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 614 break; 615 } 616 617 // Arguments that can be passed on register must be kept at RegsToPass 618 // vector 619 if (VA.isRegLoc()) { 620 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 621 } else { 622 assert(VA.isMemLoc()); 623 624 if (!StackPtr.getNode()) 625 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SP, PtrVT); 626 627 SDValue PtrOff = 628 DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, 629 DAG.getIntPtrConstant(VA.getLocMemOffset(), dl)); 630 631 SDValue MemOp; 632 ISD::ArgFlagsTy Flags = Outs[i].Flags; 633 634 if (Flags.isByVal()) { 635 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i16); 636 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode, 637 Flags.getByValAlign(), 638 /*isVolatile*/false, 639 /*AlwaysInline=*/true, 640 /*isTailCall=*/false, 641 MachinePointerInfo(), 642 MachinePointerInfo()); 643 } else { 644 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(), 645 false, false, 0); 646 } 647 648 MemOpChains.push_back(MemOp); 649 } 650 } 651 652 // Transform all store nodes into one single node because all store nodes are 653 // independent of each other. 654 if (!MemOpChains.empty()) 655 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 656 657 // Build a sequence of copy-to-reg nodes chained together with token chain and 658 // flag operands which copy the outgoing args into registers. The InFlag in 659 // necessary since all emitted instructions must be stuck together. 660 SDValue InFlag; 661 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 662 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 663 RegsToPass[i].second, InFlag); 664 InFlag = Chain.getValue(1); 665 } 666 667 // If the callee is a GlobalAddress node (quite common, every direct call is) 668 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 669 // Likewise ExternalSymbol -> TargetExternalSymbol. 670 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 671 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16); 672 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) 673 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16); 674 675 // Returns a chain & a flag for retval copy to use. 676 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 677 SmallVector<SDValue, 8> Ops; 678 Ops.push_back(Chain); 679 Ops.push_back(Callee); 680 681 // Add argument registers to the end of the list so that they are 682 // known live into the call. 683 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 684 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 685 RegsToPass[i].second.getValueType())); 686 687 if (InFlag.getNode()) 688 Ops.push_back(InFlag); 689 690 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, Ops); 691 InFlag = Chain.getValue(1); 692 693 // Create the CALLSEQ_END node. 694 Chain = DAG.getCALLSEQ_END(Chain, DAG.getConstant(NumBytes, dl, PtrVT, true), 695 DAG.getConstant(0, dl, PtrVT, true), InFlag, dl); 696 InFlag = Chain.getValue(1); 697 698 // Handle result values, copying them out of physregs into vregs that we 699 // return. 700 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, 701 DAG, InVals); 702 } 703 704 /// LowerCallResult - Lower the result values of a call into the 705 /// appropriate copies out of appropriate physical registers. 706 /// 707 SDValue 708 MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 709 CallingConv::ID CallConv, bool isVarArg, 710 const SmallVectorImpl<ISD::InputArg> &Ins, 711 SDLoc dl, SelectionDAG &DAG, 712 SmallVectorImpl<SDValue> &InVals) const { 713 714 // Assign locations to each value returned by this call. 715 SmallVector<CCValAssign, 16> RVLocs; 716 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 717 *DAG.getContext()); 718 719 AnalyzeReturnValues(CCInfo, RVLocs, Ins); 720 721 // Copy all of the result registers out of their specified physreg. 722 for (unsigned i = 0; i != RVLocs.size(); ++i) { 723 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(), 724 RVLocs[i].getValVT(), InFlag).getValue(1); 725 InFlag = Chain.getValue(2); 726 InVals.push_back(Chain.getValue(0)); 727 } 728 729 return Chain; 730 } 731 732 SDValue MSP430TargetLowering::LowerShifts(SDValue Op, 733 SelectionDAG &DAG) const { 734 unsigned Opc = Op.getOpcode(); 735 SDNode* N = Op.getNode(); 736 EVT VT = Op.getValueType(); 737 SDLoc dl(N); 738 739 // Expand non-constant shifts to loops: 740 if (!isa<ConstantSDNode>(N->getOperand(1))) 741 switch (Opc) { 742 default: llvm_unreachable("Invalid shift opcode!"); 743 case ISD::SHL: 744 return DAG.getNode(MSP430ISD::SHL, dl, 745 VT, N->getOperand(0), N->getOperand(1)); 746 case ISD::SRA: 747 return DAG.getNode(MSP430ISD::SRA, dl, 748 VT, N->getOperand(0), N->getOperand(1)); 749 case ISD::SRL: 750 return DAG.getNode(MSP430ISD::SRL, dl, 751 VT, N->getOperand(0), N->getOperand(1)); 752 } 753 754 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 755 756 // Expand the stuff into sequence of shifts. 757 // FIXME: for some shift amounts this might be done better! 758 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N 759 SDValue Victim = N->getOperand(0); 760 761 if (Opc == ISD::SRL && ShiftAmount) { 762 // Emit a special goodness here: 763 // srl A, 1 => clrc; rrc A 764 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim); 765 ShiftAmount -= 1; 766 } 767 768 while (ShiftAmount--) 769 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA), 770 dl, VT, Victim); 771 772 return Victim; 773 } 774 775 SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, 776 SelectionDAG &DAG) const { 777 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 778 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); 779 auto PtrVT = getPointerTy(DAG.getDataLayout()); 780 781 // Create the TargetGlobalAddress node, folding in the constant offset. 782 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op), PtrVT, Offset); 783 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op), PtrVT, Result); 784 } 785 786 SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op, 787 SelectionDAG &DAG) const { 788 SDLoc dl(Op); 789 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); 790 auto PtrVT = getPointerTy(DAG.getDataLayout()); 791 SDValue Result = DAG.getTargetExternalSymbol(Sym, PtrVT); 792 793 return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result); 794 } 795 796 SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op, 797 SelectionDAG &DAG) const { 798 SDLoc dl(Op); 799 auto PtrVT = getPointerTy(DAG.getDataLayout()); 800 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 801 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT); 802 803 return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result); 804 } 805 806 static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC, 807 ISD::CondCode CC, 808 SDLoc dl, SelectionDAG &DAG) { 809 // FIXME: Handle bittests someday 810 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet"); 811 812 // FIXME: Handle jump negative someday 813 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID; 814 switch (CC) { 815 default: llvm_unreachable("Invalid integer condition!"); 816 case ISD::SETEQ: 817 TCC = MSP430CC::COND_E; // aka COND_Z 818 // Minor optimization: if LHS is a constant, swap operands, then the 819 // constant can be folded into comparison. 820 if (LHS.getOpcode() == ISD::Constant) 821 std::swap(LHS, RHS); 822 break; 823 case ISD::SETNE: 824 TCC = MSP430CC::COND_NE; // aka COND_NZ 825 // Minor optimization: if LHS is a constant, swap operands, then the 826 // constant can be folded into comparison. 827 if (LHS.getOpcode() == ISD::Constant) 828 std::swap(LHS, RHS); 829 break; 830 case ISD::SETULE: 831 std::swap(LHS, RHS); // FALLTHROUGH 832 case ISD::SETUGE: 833 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to 834 // fold constant into instruction. 835 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 836 LHS = RHS; 837 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0)); 838 TCC = MSP430CC::COND_LO; 839 break; 840 } 841 TCC = MSP430CC::COND_HS; // aka COND_C 842 break; 843 case ISD::SETUGT: 844 std::swap(LHS, RHS); // FALLTHROUGH 845 case ISD::SETULT: 846 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to 847 // fold constant into instruction. 848 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 849 LHS = RHS; 850 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0)); 851 TCC = MSP430CC::COND_HS; 852 break; 853 } 854 TCC = MSP430CC::COND_LO; // aka COND_NC 855 break; 856 case ISD::SETLE: 857 std::swap(LHS, RHS); // FALLTHROUGH 858 case ISD::SETGE: 859 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to 860 // fold constant into instruction. 861 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 862 LHS = RHS; 863 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0)); 864 TCC = MSP430CC::COND_L; 865 break; 866 } 867 TCC = MSP430CC::COND_GE; 868 break; 869 case ISD::SETGT: 870 std::swap(LHS, RHS); // FALLTHROUGH 871 case ISD::SETLT: 872 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to 873 // fold constant into instruction. 874 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) { 875 LHS = RHS; 876 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0)); 877 TCC = MSP430CC::COND_GE; 878 break; 879 } 880 TCC = MSP430CC::COND_L; 881 break; 882 } 883 884 TargetCC = DAG.getConstant(TCC, dl, MVT::i8); 885 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS); 886 } 887 888 889 SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 890 SDValue Chain = Op.getOperand(0); 891 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 892 SDValue LHS = Op.getOperand(2); 893 SDValue RHS = Op.getOperand(3); 894 SDValue Dest = Op.getOperand(4); 895 SDLoc dl (Op); 896 897 SDValue TargetCC; 898 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); 899 900 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(), 901 Chain, Dest, TargetCC, Flag); 902 } 903 904 SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { 905 SDValue LHS = Op.getOperand(0); 906 SDValue RHS = Op.getOperand(1); 907 SDLoc dl (Op); 908 909 // If we are doing an AND and testing against zero, then the CMP 910 // will not be generated. The AND (or BIT) will generate the condition codes, 911 // but they are different from CMP. 912 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so 913 // lowering & isel wouldn't diverge. 914 bool andCC = false; 915 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { 916 if (RHSC->isNullValue() && LHS.hasOneUse() && 917 (LHS.getOpcode() == ISD::AND || 918 (LHS.getOpcode() == ISD::TRUNCATE && 919 LHS.getOperand(0).getOpcode() == ISD::AND))) { 920 andCC = true; 921 } 922 } 923 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 924 SDValue TargetCC; 925 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); 926 927 // Get the condition codes directly from the status register, if its easy. 928 // Otherwise a branch will be generated. Note that the AND and BIT 929 // instructions generate different flags than CMP, the carry bit can be used 930 // for NE/EQ. 931 bool Invert = false; 932 bool Shift = false; 933 bool Convert = true; 934 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) { 935 default: 936 Convert = false; 937 break; 938 case MSP430CC::COND_HS: 939 // Res = SR & 1, no processing is required 940 break; 941 case MSP430CC::COND_LO: 942 // Res = ~(SR & 1) 943 Invert = true; 944 break; 945 case MSP430CC::COND_NE: 946 if (andCC) { 947 // C = ~Z, thus Res = SR & 1, no processing is required 948 } else { 949 // Res = ~((SR >> 1) & 1) 950 Shift = true; 951 Invert = true; 952 } 953 break; 954 case MSP430CC::COND_E: 955 Shift = true; 956 // C = ~Z for AND instruction, thus we can put Res = ~(SR & 1), however, 957 // Res = (SR >> 1) & 1 is 1 word shorter. 958 break; 959 } 960 EVT VT = Op.getValueType(); 961 SDValue One = DAG.getConstant(1, dl, VT); 962 if (Convert) { 963 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SR, 964 MVT::i16, Flag); 965 if (Shift) 966 // FIXME: somewhere this is turned into a SRL, lower it MSP specific? 967 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One); 968 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One); 969 if (Invert) 970 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One); 971 return SR; 972 } else { 973 SDValue Zero = DAG.getConstant(0, dl, VT); 974 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 975 SDValue Ops[] = {One, Zero, TargetCC, Flag}; 976 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops); 977 } 978 } 979 980 SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op, 981 SelectionDAG &DAG) const { 982 SDValue LHS = Op.getOperand(0); 983 SDValue RHS = Op.getOperand(1); 984 SDValue TrueV = Op.getOperand(2); 985 SDValue FalseV = Op.getOperand(3); 986 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 987 SDLoc dl (Op); 988 989 SDValue TargetCC; 990 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); 991 992 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 993 SDValue Ops[] = {TrueV, FalseV, TargetCC, Flag}; 994 995 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops); 996 } 997 998 SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op, 999 SelectionDAG &DAG) const { 1000 SDValue Val = Op.getOperand(0); 1001 EVT VT = Op.getValueType(); 1002 SDLoc dl(Op); 1003 1004 assert(VT == MVT::i16 && "Only support i16 for now!"); 1005 1006 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT, 1007 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val), 1008 DAG.getValueType(Val.getValueType())); 1009 } 1010 1011 SDValue 1012 MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const { 1013 MachineFunction &MF = DAG.getMachineFunction(); 1014 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>(); 1015 int ReturnAddrIndex = FuncInfo->getRAIndex(); 1016 auto PtrVT = getPointerTy(MF.getDataLayout()); 1017 1018 if (ReturnAddrIndex == 0) { 1019 // Set up a frame object for the return address. 1020 uint64_t SlotSize = MF.getDataLayout().getPointerSize(); 1021 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize, 1022 true); 1023 FuncInfo->setRAIndex(ReturnAddrIndex); 1024 } 1025 1026 return DAG.getFrameIndex(ReturnAddrIndex, PtrVT); 1027 } 1028 1029 SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op, 1030 SelectionDAG &DAG) const { 1031 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 1032 MFI->setReturnAddressIsTaken(true); 1033 1034 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 1035 return SDValue(); 1036 1037 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 1038 SDLoc dl(Op); 1039 auto PtrVT = getPointerTy(DAG.getDataLayout()); 1040 1041 if (Depth > 0) { 1042 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 1043 SDValue Offset = 1044 DAG.getConstant(DAG.getDataLayout().getPointerSize(), dl, MVT::i16); 1045 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 1046 DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset), 1047 MachinePointerInfo(), false, false, false, 0); 1048 } 1049 1050 // Just load the return address. 1051 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG); 1052 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI, 1053 MachinePointerInfo(), false, false, false, 0); 1054 } 1055 1056 SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op, 1057 SelectionDAG &DAG) const { 1058 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 1059 MFI->setFrameAddressIsTaken(true); 1060 1061 EVT VT = Op.getValueType(); 1062 SDLoc dl(Op); // FIXME probably not meaningful 1063 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 1064 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, 1065 MSP430::FP, VT); 1066 while (Depth--) 1067 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 1068 MachinePointerInfo(), 1069 false, false, false, 0); 1070 return FrameAddr; 1071 } 1072 1073 SDValue MSP430TargetLowering::LowerVASTART(SDValue Op, 1074 SelectionDAG &DAG) const { 1075 MachineFunction &MF = DAG.getMachineFunction(); 1076 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>(); 1077 auto PtrVT = getPointerTy(DAG.getDataLayout()); 1078 1079 // Frame index of first vararg argument 1080 SDValue FrameIndex = 1081 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 1082 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 1083 1084 // Create a store of the frame index to the location operand 1085 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex, 1086 Op.getOperand(1), MachinePointerInfo(SV), 1087 false, false, 0); 1088 } 1089 1090 SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op, 1091 SelectionDAG &DAG) const { 1092 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 1093 auto PtrVT = getPointerTy(DAG.getDataLayout()); 1094 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); 1095 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT), PtrVT, Result); 1096 } 1097 1098 /// getPostIndexedAddressParts - returns true by value, base pointer and 1099 /// offset pointer and addressing mode by reference if this node can be 1100 /// combined with a load / store to form a post-indexed load / store. 1101 bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 1102 SDValue &Base, 1103 SDValue &Offset, 1104 ISD::MemIndexedMode &AM, 1105 SelectionDAG &DAG) const { 1106 1107 LoadSDNode *LD = cast<LoadSDNode>(N); 1108 if (LD->getExtensionType() != ISD::NON_EXTLOAD) 1109 return false; 1110 1111 EVT VT = LD->getMemoryVT(); 1112 if (VT != MVT::i8 && VT != MVT::i16) 1113 return false; 1114 1115 if (Op->getOpcode() != ISD::ADD) 1116 return false; 1117 1118 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) { 1119 uint64_t RHSC = RHS->getZExtValue(); 1120 if ((VT == MVT::i16 && RHSC != 2) || 1121 (VT == MVT::i8 && RHSC != 1)) 1122 return false; 1123 1124 Base = Op->getOperand(0); 1125 Offset = DAG.getConstant(RHSC, SDLoc(N), VT); 1126 AM = ISD::POST_INC; 1127 return true; 1128 } 1129 1130 return false; 1131 } 1132 1133 1134 const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const { 1135 switch ((MSP430ISD::NodeType)Opcode) { 1136 case MSP430ISD::FIRST_NUMBER: break; 1137 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG"; 1138 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG"; 1139 case MSP430ISD::RRA: return "MSP430ISD::RRA"; 1140 case MSP430ISD::RLA: return "MSP430ISD::RLA"; 1141 case MSP430ISD::RRC: return "MSP430ISD::RRC"; 1142 case MSP430ISD::CALL: return "MSP430ISD::CALL"; 1143 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper"; 1144 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC"; 1145 case MSP430ISD::CMP: return "MSP430ISD::CMP"; 1146 case MSP430ISD::SETCC: return "MSP430ISD::SETCC"; 1147 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC"; 1148 case MSP430ISD::SHL: return "MSP430ISD::SHL"; 1149 case MSP430ISD::SRA: return "MSP430ISD::SRA"; 1150 case MSP430ISD::SRL: return "MSP430ISD::SRL"; 1151 } 1152 return nullptr; 1153 } 1154 1155 bool MSP430TargetLowering::isTruncateFree(Type *Ty1, 1156 Type *Ty2) const { 1157 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 1158 return false; 1159 1160 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits()); 1161 } 1162 1163 bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { 1164 if (!VT1.isInteger() || !VT2.isInteger()) 1165 return false; 1166 1167 return (VT1.getSizeInBits() > VT2.getSizeInBits()); 1168 } 1169 1170 bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const { 1171 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers. 1172 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16); 1173 } 1174 1175 bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const { 1176 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers. 1177 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16; 1178 } 1179 1180 bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 1181 return isZExtFree(Val.getValueType(), VT2); 1182 } 1183 1184 //===----------------------------------------------------------------------===// 1185 // Other Lowering Code 1186 //===----------------------------------------------------------------------===// 1187 1188 MachineBasicBlock* 1189 MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI, 1190 MachineBasicBlock *BB) const { 1191 MachineFunction *F = BB->getParent(); 1192 MachineRegisterInfo &RI = F->getRegInfo(); 1193 DebugLoc dl = MI->getDebugLoc(); 1194 const TargetInstrInfo &TII = *F->getSubtarget().getInstrInfo(); 1195 1196 unsigned Opc; 1197 const TargetRegisterClass * RC; 1198 switch (MI->getOpcode()) { 1199 default: llvm_unreachable("Invalid shift opcode!"); 1200 case MSP430::Shl8: 1201 Opc = MSP430::SHL8r1; 1202 RC = &MSP430::GR8RegClass; 1203 break; 1204 case MSP430::Shl16: 1205 Opc = MSP430::SHL16r1; 1206 RC = &MSP430::GR16RegClass; 1207 break; 1208 case MSP430::Sra8: 1209 Opc = MSP430::SAR8r1; 1210 RC = &MSP430::GR8RegClass; 1211 break; 1212 case MSP430::Sra16: 1213 Opc = MSP430::SAR16r1; 1214 RC = &MSP430::GR16RegClass; 1215 break; 1216 case MSP430::Srl8: 1217 Opc = MSP430::SAR8r1c; 1218 RC = &MSP430::GR8RegClass; 1219 break; 1220 case MSP430::Srl16: 1221 Opc = MSP430::SAR16r1c; 1222 RC = &MSP430::GR16RegClass; 1223 break; 1224 } 1225 1226 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1227 MachineFunction::iterator I = ++BB->getIterator(); 1228 1229 // Create loop block 1230 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB); 1231 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB); 1232 1233 F->insert(I, LoopBB); 1234 F->insert(I, RemBB); 1235 1236 // Update machine-CFG edges by transferring all successors of the current 1237 // block to the block containing instructions after shift. 1238 RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)), 1239 BB->end()); 1240 RemBB->transferSuccessorsAndUpdatePHIs(BB); 1241 1242 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB 1243 BB->addSuccessor(LoopBB); 1244 BB->addSuccessor(RemBB); 1245 LoopBB->addSuccessor(RemBB); 1246 LoopBB->addSuccessor(LoopBB); 1247 1248 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass); 1249 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass); 1250 unsigned ShiftReg = RI.createVirtualRegister(RC); 1251 unsigned ShiftReg2 = RI.createVirtualRegister(RC); 1252 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg(); 1253 unsigned SrcReg = MI->getOperand(1).getReg(); 1254 unsigned DstReg = MI->getOperand(0).getReg(); 1255 1256 // BB: 1257 // cmp 0, N 1258 // je RemBB 1259 BuildMI(BB, dl, TII.get(MSP430::CMP8ri)) 1260 .addReg(ShiftAmtSrcReg).addImm(0); 1261 BuildMI(BB, dl, TII.get(MSP430::JCC)) 1262 .addMBB(RemBB) 1263 .addImm(MSP430CC::COND_E); 1264 1265 // LoopBB: 1266 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB] 1267 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB] 1268 // ShiftReg2 = shift ShiftReg 1269 // ShiftAmt2 = ShiftAmt - 1; 1270 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg) 1271 .addReg(SrcReg).addMBB(BB) 1272 .addReg(ShiftReg2).addMBB(LoopBB); 1273 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg) 1274 .addReg(ShiftAmtSrcReg).addMBB(BB) 1275 .addReg(ShiftAmtReg2).addMBB(LoopBB); 1276 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2) 1277 .addReg(ShiftReg); 1278 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2) 1279 .addReg(ShiftAmtReg).addImm(1); 1280 BuildMI(LoopBB, dl, TII.get(MSP430::JCC)) 1281 .addMBB(LoopBB) 1282 .addImm(MSP430CC::COND_NE); 1283 1284 // RemBB: 1285 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB] 1286 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg) 1287 .addReg(SrcReg).addMBB(BB) 1288 .addReg(ShiftReg2).addMBB(LoopBB); 1289 1290 MI->eraseFromParent(); // The pseudo instruction is gone now. 1291 return RemBB; 1292 } 1293 1294 MachineBasicBlock* 1295 MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 1296 MachineBasicBlock *BB) const { 1297 unsigned Opc = MI->getOpcode(); 1298 1299 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 || 1300 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 || 1301 Opc == MSP430::Srl8 || Opc == MSP430::Srl16) 1302 return EmitShiftInstr(MI, BB); 1303 1304 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo(); 1305 DebugLoc dl = MI->getDebugLoc(); 1306 1307 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) && 1308 "Unexpected instr type to insert"); 1309 1310 // To "insert" a SELECT instruction, we actually have to insert the diamond 1311 // control-flow pattern. The incoming instruction knows the destination vreg 1312 // to set, the condition code register to branch on, the true/false values to 1313 // select between, and a branch opcode to use. 1314 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1315 MachineFunction::iterator I = ++BB->getIterator(); 1316 1317 // thisMBB: 1318 // ... 1319 // TrueVal = ... 1320 // cmpTY ccX, r1, r2 1321 // jCC copy1MBB 1322 // fallthrough --> copy0MBB 1323 MachineBasicBlock *thisMBB = BB; 1324 MachineFunction *F = BB->getParent(); 1325 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 1326 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB); 1327 F->insert(I, copy0MBB); 1328 F->insert(I, copy1MBB); 1329 // Update machine-CFG edges by transferring all successors of the current 1330 // block to the new block which will contain the Phi node for the select. 1331 copy1MBB->splice(copy1MBB->begin(), BB, 1332 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 1333 copy1MBB->transferSuccessorsAndUpdatePHIs(BB); 1334 // Next, add the true and fallthrough blocks as its successors. 1335 BB->addSuccessor(copy0MBB); 1336 BB->addSuccessor(copy1MBB); 1337 1338 BuildMI(BB, dl, TII.get(MSP430::JCC)) 1339 .addMBB(copy1MBB) 1340 .addImm(MI->getOperand(3).getImm()); 1341 1342 // copy0MBB: 1343 // %FalseValue = ... 1344 // # fallthrough to copy1MBB 1345 BB = copy0MBB; 1346 1347 // Update machine-CFG edges 1348 BB->addSuccessor(copy1MBB); 1349 1350 // copy1MBB: 1351 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 1352 // ... 1353 BB = copy1MBB; 1354 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI), 1355 MI->getOperand(0).getReg()) 1356 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB) 1357 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB); 1358 1359 MI->eraseFromParent(); // The pseudo instruction is gone now. 1360 return BB; 1361 } 1362