LLVM API Documentation

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