blob: 04bb6d041cf5e4643cbf4030a4c08904997f9925 [file] [log] [blame]
Anton Korobeynikov10138002009-05-03 12:57:15 +00001//===-- 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
Anton Korobeynikov10138002009-05-03 12:57:15 +000014#include "MSP430ISelLowering.h"
15#include "MSP430.h"
Anton Korobeynikovff4ab512009-12-07 02:28:10 +000016#include "MSP430MachineFunctionInfo.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000017#include "MSP430Subtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "MSP430TargetMachine.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000019#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"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000025#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000026#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#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"
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000033#include "llvm/Support/CommandLine.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000034#include "llvm/Support/Debug.h"
Torok Edwinfa040022009-07-08 19:04:27 +000035#include "llvm/Support/ErrorHandling.h"
Chris Lattner317dbbc2009-08-23 07:05:07 +000036#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000037using namespace llvm;
38
Chandler Carruth84e68b22014-04-22 02:41:26 +000039#define DEBUG_TYPE "msp430-lower"
40
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000041typedef enum {
42 NoHWMult,
43 HWMultIntr,
44 HWMultNoIntr
45} HWMultUseMode;
46
47static cl::opt<HWMultUseMode>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000048HWMultMode("msp430-hwmult-mode", cl::Hidden,
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000049 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
Eric Christopherdc13b212014-06-27 00:37:59 +000060MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM)
Aditya Nandakumar30531552014-11-13 21:29:21 +000061 : TargetLowering(TM) {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +000062
Anton Korobeynikov10138002009-05-03 12:57:15 +000063 // Set up the register classes.
Craig Topperc7242e02012-04-20 07:30:17 +000064 addRegisterClass(MVT::i8, &MSP430::GR8RegClass);
65 addRegisterClass(MVT::i16, &MSP430::GR16RegClass);
Anton Korobeynikov10138002009-05-03 12:57:15 +000066
67 // Compute derived properties from the register classes
68 computeRegisterProperties();
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +000069
Anton Korobeynikov55a085b2009-05-03 13:03:14 +000070 // Provide all sorts of operation actions
71
72 // Division is expensive
73 setIntDivIsCheap(false);
74
Job Noormaneb19aea2014-09-10 06:58:14 +000075 setStackPointerRegisterToSaveRestore(MSP430::SP);
Anton Korobeynikov7212c152009-05-03 13:11:35 +000076 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sandsf2641e12011-09-06 19:07:46 +000077 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Anton Korobeynikov7212c152009-05-03 13:11:35 +000078
Anton Korobeynikovcf84ab52009-11-07 17:15:25 +000079 // We have post-incremented loads / stores.
Anton Korobeynikovd3c83192009-11-07 17:15:06 +000080 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
81 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
82
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000083 for (MVT VT : MVT::integer_valuetypes()) {
84 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
85 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
86 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
87 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
88 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Expand);
89 }
Anton Korobeynikov31ecd232009-05-03 13:06:03 +000090
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000091 // We don't have any truncstores
Owen Anderson9f944592009-08-11 20:47:22 +000092 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000093
Owen Anderson9f944592009-08-11 20:47:22 +000094 setOperationAction(ISD::SRA, MVT::i8, Custom);
95 setOperationAction(ISD::SHL, MVT::i8, Custom);
96 setOperationAction(ISD::SRL, MVT::i8, Custom);
97 setOperationAction(ISD::SRA, MVT::i16, Custom);
98 setOperationAction(ISD::SHL, MVT::i16, Custom);
99 setOperationAction(ISD::SRL, MVT::i16, Custom);
100 setOperationAction(ISD::ROTL, MVT::i8, Expand);
101 setOperationAction(ISD::ROTR, MVT::i8, Expand);
102 setOperationAction(ISD::ROTL, MVT::i16, Expand);
103 setOperationAction(ISD::ROTR, MVT::i16, Expand);
104 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
105 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000106 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000107 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000108 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
109 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
110 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000111 setOperationAction(ISD::SETCC, MVT::i8, Custom);
112 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000113 setOperationAction(ISD::SELECT, MVT::i8, Expand);
114 setOperationAction(ISD::SELECT, MVT::i16, Expand);
115 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
116 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
117 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov271cdda2009-08-25 17:00:23 +0000118 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
119 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000120
Owen Anderson9f944592009-08-11 20:47:22 +0000121 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
122 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000123 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Expand);
124 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000125 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
126 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000127 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Expand);
128 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000129 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
130 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000131
Owen Anderson9f944592009-08-11 20:47:22 +0000132 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
133 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
134 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
135 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
136 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
137 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000138
Owen Anderson9f944592009-08-11 20:47:22 +0000139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000140
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000141 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000142 setOperationAction(ISD::MUL, MVT::i8, Expand);
143 setOperationAction(ISD::MULHS, MVT::i8, Expand);
144 setOperationAction(ISD::MULHU, MVT::i8, Expand);
145 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
146 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000147 setOperationAction(ISD::MUL, MVT::i16, Expand);
148 setOperationAction(ISD::MULHS, MVT::i16, Expand);
149 setOperationAction(ISD::MULHU, MVT::i16, Expand);
150 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
151 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikoveb2152f2009-05-03 13:18:33 +0000152
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000153 setOperationAction(ISD::UDIV, MVT::i8, Expand);
154 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
155 setOperationAction(ISD::UREM, MVT::i8, Expand);
156 setOperationAction(ISD::SDIV, MVT::i8, Expand);
157 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
158 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 setOperationAction(ISD::UDIV, MVT::i16, Expand);
160 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
161 setOperationAction(ISD::UREM, MVT::i16, Expand);
162 setOperationAction(ISD::SDIV, MVT::i16, Expand);
163 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
164 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000165
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000166 // varargs support
167 setOperationAction(ISD::VASTART, MVT::Other, Custom);
168 setOperationAction(ISD::VAARG, MVT::Other, Expand);
169 setOperationAction(ISD::VAEND, MVT::Other, Expand);
170 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000171 setOperationAction(ISD::JumpTable, MVT::i16, Custom);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000172
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000173 // Libcalls names.
174 if (HWMultMode == HWMultIntr) {
175 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
176 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
177 } else if (HWMultMode == HWMultNoIntr) {
178 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
179 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
180 }
Eli Friedman2518f832011-05-06 20:34:06 +0000181
182 setMinFunctionAlignment(1);
183 setPrefFunctionAlignment(2);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000184}
185
Dan Gohman21cea8a2010-04-17 15:26:15 +0000186SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
187 SelectionDAG &DAG) const {
Anton Korobeynikov10138002009-05-03 12:57:15 +0000188 switch (Op.getOpcode()) {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000189 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000190 case ISD::SRL:
Anton Korobeynikov56135102009-05-03 13:07:31 +0000191 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000192 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000193 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000194 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000195 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000196 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
197 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000198 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000199 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
200 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000201 case ISD::VASTART: return LowerVASTART(Op, DAG);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000202 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000203 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000204 llvm_unreachable("unimplemented operand");
Anton Korobeynikov10138002009-05-03 12:57:15 +0000205 }
206}
207
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000208//===----------------------------------------------------------------------===//
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000209// MSP430 Inline Assembly Support
210//===----------------------------------------------------------------------===//
211
212/// getConstraintType - Given a constraint letter, return the type of
213/// constraint it is for this target.
214TargetLowering::ConstraintType
215MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
216 if (Constraint.size() == 1) {
217 switch (Constraint[0]) {
218 case 'r':
219 return C_RegisterClass;
220 default:
221 break;
222 }
223 }
224 return TargetLowering::getConstraintType(Constraint);
225}
226
227std::pair<unsigned, const TargetRegisterClass*>
228MSP430TargetLowering::
229getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +0000230 MVT VT) const {
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000231 if (Constraint.size() == 1) {
232 // GCC Constraint Letters
233 switch (Constraint[0]) {
234 default: break;
235 case 'r': // GENERAL_REGS
236 if (VT == MVT::i8)
Craig Topperc7242e02012-04-20 07:30:17 +0000237 return std::make_pair(0U, &MSP430::GR8RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000238
Craig Topperc7242e02012-04-20 07:30:17 +0000239 return std::make_pair(0U, &MSP430::GR16RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000240 }
241 }
242
243 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
244}
245
246//===----------------------------------------------------------------------===//
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000247// Calling Convention Implementation
248//===----------------------------------------------------------------------===//
249
Anton Korobeynikov10138002009-05-03 12:57:15 +0000250#include "MSP430GenCallingConv.inc"
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000251
Job Noormane9a1d4c2013-10-15 08:19:39 +0000252/// For each argument in a function store the number of pieces it is composed
253/// of.
254template<typename ArgT>
255static void ParseFunctionArgs(const SmallVectorImpl<ArgT> &Args,
256 SmallVectorImpl<unsigned> &Out) {
257 unsigned CurrentArgIndex = ~0U;
258 for (unsigned i = 0, e = Args.size(); i != e; i++) {
259 if (CurrentArgIndex == Args[i].OrigArgIndex) {
260 Out.back()++;
261 } else {
262 Out.push_back(1);
263 CurrentArgIndex++;
264 }
265 }
266}
267
268static void AnalyzeVarArgs(CCState &State,
269 const SmallVectorImpl<ISD::OutputArg> &Outs) {
270 State.AnalyzeCallOperands(Outs, CC_MSP430_AssignStack);
271}
272
273static void AnalyzeVarArgs(CCState &State,
274 const SmallVectorImpl<ISD::InputArg> &Ins) {
275 State.AnalyzeFormalArguments(Ins, CC_MSP430_AssignStack);
276}
277
278/// Analyze incoming and outgoing function arguments. We need custom C++ code
279/// to handle special constraints in the ABI like reversing the order of the
280/// pieces of splitted arguments. In addition, all pieces of a certain argument
281/// have to be passed either using registers or the stack but never mixing both.
282template<typename ArgT>
283static void AnalyzeArguments(CCState &State,
284 SmallVectorImpl<CCValAssign> &ArgLocs,
285 const SmallVectorImpl<ArgT> &Args) {
Craig Topper840beec2014-04-04 05:16:06 +0000286 static const MCPhysReg RegList[] = {
Job Noormaneb19aea2014-09-10 06:58:14 +0000287 MSP430::R15, MSP430::R14, MSP430::R13, MSP430::R12
Job Noormane9a1d4c2013-10-15 08:19:39 +0000288 };
289 static const unsigned NbRegs = array_lengthof(RegList);
290
291 if (State.isVarArg()) {
292 AnalyzeVarArgs(State, Args);
293 return;
294 }
295
296 SmallVector<unsigned, 4> ArgsParts;
297 ParseFunctionArgs(Args, ArgsParts);
298
299 unsigned RegsLeft = NbRegs;
300 bool UseStack = false;
301 unsigned ValNo = 0;
302
303 for (unsigned i = 0, e = ArgsParts.size(); i != e; i++) {
304 MVT ArgVT = Args[ValNo].VT;
305 ISD::ArgFlagsTy ArgFlags = Args[ValNo].Flags;
306 MVT LocVT = ArgVT;
307 CCValAssign::LocInfo LocInfo = CCValAssign::Full;
308
309 // Promote i8 to i16
310 if (LocVT == MVT::i8) {
311 LocVT = MVT::i16;
312 if (ArgFlags.isSExt())
313 LocInfo = CCValAssign::SExt;
314 else if (ArgFlags.isZExt())
315 LocInfo = CCValAssign::ZExt;
316 else
317 LocInfo = CCValAssign::AExt;
318 }
319
320 // Handle byval arguments
321 if (ArgFlags.isByVal()) {
322 State.HandleByVal(ValNo++, ArgVT, LocVT, LocInfo, 2, 2, ArgFlags);
323 continue;
324 }
325
326 unsigned Parts = ArgsParts[i];
327
328 if (!UseStack && Parts <= RegsLeft) {
329 unsigned FirstVal = ValNo;
330 for (unsigned j = 0; j < Parts; j++) {
331 unsigned Reg = State.AllocateReg(RegList, NbRegs);
332 State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo));
333 RegsLeft--;
334 }
335
336 // Reverse the order of the pieces to agree with the "big endian" format
337 // required in the calling convention ABI.
338 SmallVectorImpl<CCValAssign>::iterator B = ArgLocs.begin() + FirstVal;
339 std::reverse(B, B + Parts);
340 } else {
341 UseStack = true;
342 for (unsigned j = 0; j < Parts; j++)
343 CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State);
344 }
345 }
346}
347
348static void AnalyzeRetResult(CCState &State,
349 const SmallVectorImpl<ISD::InputArg> &Ins) {
350 State.AnalyzeCallResult(Ins, RetCC_MSP430);
351}
352
353static void AnalyzeRetResult(CCState &State,
354 const SmallVectorImpl<ISD::OutputArg> &Outs) {
355 State.AnalyzeReturn(Outs, RetCC_MSP430);
356}
357
358template<typename ArgT>
359static void AnalyzeReturnValues(CCState &State,
360 SmallVectorImpl<CCValAssign> &RVLocs,
361 const SmallVectorImpl<ArgT> &Args) {
362 AnalyzeRetResult(State, Args);
363
364 // Reverse splitted return values to get the "big endian" format required
365 // to agree with the calling convention ABI.
366 std::reverse(RVLocs.begin(), RVLocs.end());
367}
368
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000369SDValue
370MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000371 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000372 bool isVarArg,
373 const SmallVectorImpl<ISD::InputArg>
374 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000375 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000376 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000377 SmallVectorImpl<SDValue> &InVals)
378 const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000379
380 switch (CallConv) {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000381 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000382 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000383 case CallingConv::C:
384 case CallingConv::Fast:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000385 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000386 case CallingConv::MSP430_INTR:
David Blaikie46a9f012012-01-20 21:51:11 +0000387 if (Ins.empty())
388 return Chain;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000389 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000390 }
391}
392
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000393SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000394MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000395 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000396 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000397 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000398 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
399 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
400 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000401 SDValue Chain = CLI.Chain;
402 SDValue Callee = CLI.Callee;
403 bool &isTailCall = CLI.IsTailCall;
404 CallingConv::ID CallConv = CLI.CallConv;
405 bool isVarArg = CLI.IsVarArg;
406
Evan Cheng67a69dd2010-01-27 00:07:07 +0000407 // MSP430 target does not yet support tail call optimization.
408 isTailCall = false;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000409
410 switch (CallConv) {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000411 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000412 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000413 case CallingConv::Fast:
414 case CallingConv::C:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000415 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000416 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000417 case CallingConv::MSP430_INTR:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000418 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000419 }
420}
421
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000422/// LowerCCCArguments - transform physical registers into virtual registers and
423/// generate load operations for arguments places on the stack.
424// FIXME: struct return stuff
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000425SDValue
426MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000427 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000428 bool isVarArg,
429 const SmallVectorImpl<ISD::InputArg>
430 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000431 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000432 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000433 SmallVectorImpl<SDValue> &InVals)
434 const {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000435 MachineFunction &MF = DAG.getMachineFunction();
436 MachineFrameInfo *MFI = MF.getFrameInfo();
437 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000438 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000439
440 // Assign locations to all of the incoming arguments.
441 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000442 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
443 *DAG.getContext());
Job Noormane9a1d4c2013-10-15 08:19:39 +0000444 AnalyzeArguments(CCInfo, ArgLocs, Ins);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000445
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000446 // Create frame index for the start of the first vararg value
447 if (isVarArg) {
448 unsigned Offset = CCInfo.getNextStackOffset();
449 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, Offset, true));
450 }
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000451
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000452 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
453 CCValAssign &VA = ArgLocs[i];
454 if (VA.isRegLoc()) {
455 // Arguments passed in registers
Owen Anderson53aa7a92009-08-10 22:56:29 +0000456 EVT RegVT = VA.getLocVT();
Owen Anderson9f944592009-08-11 20:47:22 +0000457 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Andersonb2c80da2011-02-25 21:41:48 +0000458 default:
Torok Edwinfa040022009-07-08 19:04:27 +0000459 {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000460#ifndef NDEBUG
Chris Lattner317dbbc2009-08-23 07:05:07 +0000461 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson9f944592009-08-11 20:47:22 +0000462 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000463#endif
Craig Toppere73658d2014-04-28 04:05:08 +0000464 llvm_unreachable(nullptr);
Torok Edwinfa040022009-07-08 19:04:27 +0000465 }
Owen Anderson9f944592009-08-11 20:47:22 +0000466 case MVT::i16:
Craig Topperc7242e02012-04-20 07:30:17 +0000467 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000468 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000469 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000470
471 // If this is an 8-bit value, it is really passed promoted to 16
472 // bits. Insert an assert[sz]ext to capture this, then truncate to the
473 // right size.
474 if (VA.getLocInfo() == CCValAssign::SExt)
475 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
476 DAG.getValueType(VA.getValVT()));
477 else if (VA.getLocInfo() == CCValAssign::ZExt)
478 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
479 DAG.getValueType(VA.getValVT()));
480
481 if (VA.getLocInfo() != CCValAssign::Full)
482 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
483
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000484 InVals.push_back(ArgValue);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000485 }
486 } else {
487 // Sanity check
488 assert(VA.isMemLoc());
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000489
Anton Korobeynikov34148722012-11-21 17:23:03 +0000490 SDValue InVal;
491 ISD::ArgFlagsTy Flags = Ins[i].Flags;
492
493 if (Flags.isByVal()) {
494 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
495 VA.getLocMemOffset(), true);
496 InVal = DAG.getFrameIndex(FI, getPointerTy());
497 } else {
498 // Load the argument to a virtual register
499 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
500 if (ObjSize > 2) {
501 errs() << "LowerFormalArguments Unhandled argument type: "
502 << EVT(VA.getLocVT()).getEVTString()
503 << "\n";
504 }
505 // Create the frame index object for this incoming parameter...
506 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
507
508 // Create the SelectionDAG nodes corresponding to a load
509 //from this parameter
510 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
511 InVal = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
512 MachinePointerInfo::getFixedStack(FI),
513 false, false, false, 0);
514 }
515
516 InVals.push_back(InVal);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000517 }
518 }
519
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000520 return Chain;
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000521}
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000522
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000523SDValue
524MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000525 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000526 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000527 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000528 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000529
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000530 // CCValAssign - represent the assignment of the return value to a location
531 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000532
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000533 // ISRs cannot return any value.
David Blaikie46a9f012012-01-20 21:51:11 +0000534 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
Chris Lattner2104b8d2010-04-07 22:58:41 +0000535 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000536
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000537 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000538 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
539 *DAG.getContext());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000540
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000541 // Analize return values.
Job Noormane9a1d4c2013-10-15 08:19:39 +0000542 AnalyzeReturnValues(CCInfo, RVLocs, Outs);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000543
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000544 SDValue Flag;
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000545 SmallVector<SDValue, 4> RetOps(1, Chain);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000546
547 // Copy the result values into the output registers.
548 for (unsigned i = 0; i != RVLocs.size(); ++i) {
549 CCValAssign &VA = RVLocs[i];
550 assert(VA.isRegLoc() && "Can only return in registers!");
551
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000552 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000553 OutVals[i], Flag);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000554
Anton Korobeynikovc10f98a2009-05-03 13:00:11 +0000555 // Guarantee that all emitted copies are stuck together,
556 // avoiding something bad.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000557 Flag = Chain.getValue(1);
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000558 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000559 }
560
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000561 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
562 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
563
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000564 RetOps[0] = Chain; // Update chain.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000565
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000566 // Add the flag if we have it.
567 if (Flag.getNode())
568 RetOps.push_back(Flag);
569
Craig Topper48d114b2014-04-26 18:35:24 +0000570 return DAG.getNode(Opc, dl, MVT::Other, RetOps);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000571}
572
Anton Korobeynikov56135102009-05-03 13:07:31 +0000573/// LowerCCCCallTo - functions arguments are copied from virtual regs to
574/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Job Noormana928e1d2013-07-15 14:25:26 +0000575// TODO: sret.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000576SDValue
577MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000578 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000579 bool isTailCall,
580 const SmallVectorImpl<ISD::OutputArg>
581 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000582 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000583 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000584 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000585 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000586 // Analyze operands of the call, assigning locations to each operand.
587 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000588 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
589 *DAG.getContext());
Job Noormane9a1d4c2013-10-15 08:19:39 +0000590 AnalyzeArguments(CCInfo, ArgLocs, Outs);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000591
592 // Get a count of how many bytes are to be pushed on the stack.
593 unsigned NumBytes = CCInfo.getNextStackOffset();
594
595 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
Andrew Trickad6d08a2013-05-29 22:03:55 +0000596 getPointerTy(), true),
597 dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000598
599 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
600 SmallVector<SDValue, 12> MemOpChains;
601 SDValue StackPtr;
602
603 // Walk the register/memloc assignments, inserting copies/loads.
604 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
605 CCValAssign &VA = ArgLocs[i];
606
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000607 SDValue Arg = OutVals[i];
Anton Korobeynikov56135102009-05-03 13:07:31 +0000608
609 // Promote the value if needed.
610 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000611 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000612 case CCValAssign::Full: break;
613 case CCValAssign::SExt:
614 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
615 break;
616 case CCValAssign::ZExt:
617 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
618 break;
619 case CCValAssign::AExt:
620 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
621 break;
622 }
623
624 // Arguments that can be passed on register must be kept at RegsToPass
625 // vector
626 if (VA.isRegLoc()) {
627 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
628 } else {
629 assert(VA.isMemLoc());
630
Craig Topper062a2ba2014-04-25 05:30:21 +0000631 if (!StackPtr.getNode())
Job Noormaneb19aea2014-09-10 06:58:14 +0000632 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SP, getPointerTy());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000633
634 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
635 StackPtr,
636 DAG.getIntPtrConstant(VA.getLocMemOffset()));
637
Anton Korobeynikov34148722012-11-21 17:23:03 +0000638 SDValue MemOp;
639 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000640
Anton Korobeynikov34148722012-11-21 17:23:03 +0000641 if (Flags.isByVal()) {
642 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16);
643 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
644 Flags.getByValAlign(),
645 /*isVolatile*/false,
646 /*AlwaysInline=*/true,
647 MachinePointerInfo(),
648 MachinePointerInfo());
649 } else {
650 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(),
651 false, false, 0);
652 }
653
654 MemOpChains.push_back(MemOp);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000655 }
656 }
657
658 // Transform all store nodes into one single node because all store nodes are
659 // independent of each other.
660 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000661 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000662
663 // Build a sequence of copy-to-reg nodes chained together with token chain and
664 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000665 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov56135102009-05-03 13:07:31 +0000666 SDValue InFlag;
667 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
668 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
669 RegsToPass[i].second, InFlag);
670 InFlag = Chain.getValue(1);
671 }
672
673 // If the callee is a GlobalAddress node (quite common, every direct call is)
674 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
675 // Likewise ExternalSymbol -> TargetExternalSymbol.
676 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patela3ca21b2010-07-06 22:08:15 +0000677 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000678 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson9f944592009-08-11 20:47:22 +0000679 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000680
681 // Returns a chain & a flag for retval copy to use.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000682 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000683 SmallVector<SDValue, 8> Ops;
684 Ops.push_back(Chain);
685 Ops.push_back(Callee);
686
687 // Add argument registers to the end of the list so that they are
688 // known live into the call.
689 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
690 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
691 RegsToPass[i].second.getValueType()));
692
693 if (InFlag.getNode())
694 Ops.push_back(InFlag);
695
Craig Topper48d114b2014-04-26 18:35:24 +0000696 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, Ops);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000697 InFlag = Chain.getValue(1);
698
699 // Create the CALLSEQ_END node.
700 Chain = DAG.getCALLSEQ_END(Chain,
701 DAG.getConstant(NumBytes, getPointerTy(), true),
702 DAG.getConstant(0, getPointerTy(), true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000703 InFlag, dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000704 InFlag = Chain.getValue(1);
705
706 // Handle result values, copying them out of physregs into vregs that we
707 // return.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000708 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
709 DAG, InVals);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000710}
711
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000712/// LowerCallResult - Lower the result values of a call into the
713/// appropriate copies out of appropriate physical registers.
714///
715SDValue
Anton Korobeynikov56135102009-05-03 13:07:31 +0000716MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000717 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000718 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000719 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000720 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000721
722 // Assign locations to each value returned by this call.
723 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000724 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
725 *DAG.getContext());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000726
Job Noormane9a1d4c2013-10-15 08:19:39 +0000727 AnalyzeReturnValues(CCInfo, RVLocs, Ins);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000728
729 // Copy all of the result registers out of their specified physreg.
730 for (unsigned i = 0; i != RVLocs.size(); ++i) {
731 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
732 RVLocs[i].getValVT(), InFlag).getValue(1);
733 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000734 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov56135102009-05-03 13:07:31 +0000735 }
736
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000737 return Chain;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000738}
739
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000740SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000741 SelectionDAG &DAG) const {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000742 unsigned Opc = Op.getOpcode();
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000743 SDNode* N = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000744 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000745 SDLoc dl(N);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000746
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000747 // Expand non-constant shifts to loops:
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000748 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000749 switch (Opc) {
Craig Toppere55c5562012-02-07 02:50:20 +0000750 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000751 case ISD::SHL:
752 return DAG.getNode(MSP430ISD::SHL, dl,
753 VT, N->getOperand(0), N->getOperand(1));
754 case ISD::SRA:
755 return DAG.getNode(MSP430ISD::SRA, dl,
756 VT, N->getOperand(0), N->getOperand(1));
757 case ISD::SRL:
758 return DAG.getNode(MSP430ISD::SRL, dl,
759 VT, N->getOperand(0), N->getOperand(1));
760 }
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000761
762 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
763
764 // Expand the stuff into sequence of shifts.
765 // FIXME: for some shift amounts this might be done better!
766 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
767 SDValue Victim = N->getOperand(0);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000768
769 if (Opc == ISD::SRL && ShiftAmount) {
770 // Emit a special goodness here:
771 // srl A, 1 => clrc; rrc A
Anton Korobeynikovf3a6bc82009-05-03 13:16:37 +0000772 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000773 ShiftAmount -= 1;
774 }
775
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000776 while (ShiftAmount--)
Anton Korobeynikov6b5523a2009-05-17 10:15:22 +0000777 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000778 dl, VT, Victim);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000779
780 return Victim;
781}
782
Dan Gohman21cea8a2010-04-17 15:26:15 +0000783SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
784 SelectionDAG &DAG) const {
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000785 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
786 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
787
788 // Create the TargetGlobalAddress node, folding in the constant offset.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000789 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
Devang Patela3ca21b2010-07-06 22:08:15 +0000790 getPointerTy(), Offset);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000791 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op),
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000792 getPointerTy(), Result);
793}
794
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000795SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000796 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000797 SDLoc dl(Op);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000798 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
799 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
800
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000801 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000802}
803
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000804SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
805 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000806 SDLoc dl(Op);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000807 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liaoabb87d42012-09-12 21:43:09 +0000808 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000809
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000810 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000811}
812
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000813static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000814 ISD::CondCode CC,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000815 SDLoc dl, SelectionDAG &DAG) {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000816 // FIXME: Handle bittests someday
817 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
818
819 // FIXME: Handle jump negative someday
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000820 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000821 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000822 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov96272012009-05-03 13:12:06 +0000823 case ISD::SETEQ:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000824 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000825 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000826 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000827 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000828 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000829 break;
830 case ISD::SETNE:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000831 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000832 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000833 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000834 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000835 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000836 break;
837 case ISD::SETULE:
838 std::swap(LHS, RHS); // FALLTHROUGH
839 case ISD::SETUGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000840 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
841 // fold constant into instruction.
842 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
843 LHS = RHS;
844 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
845 TCC = MSP430CC::COND_LO;
846 break;
847 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000848 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikov96272012009-05-03 13:12:06 +0000849 break;
850 case ISD::SETUGT:
851 std::swap(LHS, RHS); // FALLTHROUGH
852 case ISD::SETULT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000853 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
854 // fold constant into instruction.
855 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
856 LHS = RHS;
857 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
858 TCC = MSP430CC::COND_HS;
859 break;
860 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000861 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikov96272012009-05-03 13:12:06 +0000862 break;
863 case ISD::SETLE:
864 std::swap(LHS, RHS); // FALLTHROUGH
865 case ISD::SETGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000866 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
867 // fold constant into instruction.
868 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
869 LHS = RHS;
870 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
871 TCC = MSP430CC::COND_L;
872 break;
873 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000874 TCC = MSP430CC::COND_GE;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000875 break;
876 case ISD::SETGT:
877 std::swap(LHS, RHS); // FALLTHROUGH
878 case ISD::SETLT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000879 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
880 // fold constant into instruction.
881 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
882 LHS = RHS;
883 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
884 TCC = MSP430CC::COND_GE;
885 break;
886 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000887 TCC = MSP430CC::COND_L;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000888 break;
889 }
890
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000891 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000892 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000893}
894
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000895
Dan Gohman21cea8a2010-04-17 15:26:15 +0000896SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000897 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000898 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
899 SDValue LHS = Op.getOperand(2);
900 SDValue RHS = Op.getOperand(3);
901 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000902 SDLoc dl (Op);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000903
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000904 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000905 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000906
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000907 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000908 Chain, Dest, TargetCC, Flag);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000909}
910
Dan Gohman21cea8a2010-04-17 15:26:15 +0000911SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000912 SDValue LHS = Op.getOperand(0);
913 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000914 SDLoc dl (Op);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000915
916 // If we are doing an AND and testing against zero, then the CMP
917 // will not be generated. The AND (or BIT) will generate the condition codes,
918 // but they are different from CMP.
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000919 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
920 // lowering & isel wouldn't diverge.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000921 bool andCC = false;
922 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
923 if (RHSC->isNullValue() && LHS.hasOneUse() &&
924 (LHS.getOpcode() == ISD::AND ||
925 (LHS.getOpcode() == ISD::TRUNCATE &&
926 LHS.getOperand(0).getOpcode() == ISD::AND))) {
927 andCC = true;
928 }
929 }
930 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
931 SDValue TargetCC;
932 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
933
934 // Get the condition codes directly from the status register, if its easy.
935 // Otherwise a branch will be generated. Note that the AND and BIT
936 // instructions generate different flags than CMP, the carry bit can be used
937 // for NE/EQ.
938 bool Invert = false;
939 bool Shift = false;
940 bool Convert = true;
941 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
942 default:
943 Convert = false;
944 break;
945 case MSP430CC::COND_HS:
Job Noormaneb19aea2014-09-10 06:58:14 +0000946 // Res = SR & 1, no processing is required
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000947 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000948 case MSP430CC::COND_LO:
Job Noormaneb19aea2014-09-10 06:58:14 +0000949 // Res = ~(SR & 1)
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000950 Invert = true;
951 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000952 case MSP430CC::COND_NE:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000953 if (andCC) {
Job Noormaneb19aea2014-09-10 06:58:14 +0000954 // C = ~Z, thus Res = SR & 1, no processing is required
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000955 } else {
Job Noormaneb19aea2014-09-10 06:58:14 +0000956 // Res = ~((SR >> 1) & 1)
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000957 Shift = true;
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000958 Invert = true;
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000959 }
960 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000961 case MSP430CC::COND_E:
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000962 Shift = true;
Job Noormaneb19aea2014-09-10 06:58:14 +0000963 // C = ~Z for AND instruction, thus we can put Res = ~(SR & 1), however,
964 // Res = (SR >> 1) & 1 is 1 word shorter.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000965 break;
966 }
967 EVT VT = Op.getValueType();
968 SDValue One = DAG.getConstant(1, VT);
969 if (Convert) {
Job Noormaneb19aea2014-09-10 06:58:14 +0000970 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SR,
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000971 MVT::i16, Flag);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000972 if (Shift)
973 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
974 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
975 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
976 if (Invert)
977 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
978 return SR;
979 } else {
980 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000981 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000982 SmallVector<SDValue, 4> Ops;
983 Ops.push_back(One);
984 Ops.push_back(Zero);
985 Ops.push_back(TargetCC);
986 Ops.push_back(Flag);
Craig Topper48d114b2014-04-26 18:35:24 +0000987 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000988 }
989}
990
Dan Gohman21cea8a2010-04-17 15:26:15 +0000991SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
992 SelectionDAG &DAG) const {
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000993 SDValue LHS = Op.getOperand(0);
994 SDValue RHS = Op.getOperand(1);
995 SDValue TrueV = Op.getOperand(2);
996 SDValue FalseV = Op.getOperand(3);
997 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000998 SDLoc dl (Op);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000999
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +00001000 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001001 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001002
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001003 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001004 SmallVector<SDValue, 4> Ops;
1005 Ops.push_back(TrueV);
1006 Ops.push_back(FalseV);
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +00001007 Ops.push_back(TargetCC);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001008 Ops.push_back(Flag);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001009
Craig Topper48d114b2014-04-26 18:35:24 +00001010 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001011}
1012
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001013SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001014 SelectionDAG &DAG) const {
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001015 SDValue Val = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001016 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001017 SDLoc dl(Op);
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001018
Owen Anderson9f944592009-08-11 20:47:22 +00001019 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001020
1021 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
1022 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
1023 DAG.getValueType(Val.getValueType()));
1024}
1025
Dan Gohman21cea8a2010-04-17 15:26:15 +00001026SDValue
1027MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001028 MachineFunction &MF = DAG.getMachineFunction();
1029 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
1030 int ReturnAddrIndex = FuncInfo->getRAIndex();
1031
1032 if (ReturnAddrIndex == 0) {
1033 // Set up a frame object for the return address.
Eric Christopherdc13b212014-06-27 00:37:59 +00001034 uint64_t SlotSize = getDataLayout()->getPointerSize();
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001035 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00001036 true);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001037 FuncInfo->setRAIndex(ReturnAddrIndex);
1038 }
1039
1040 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1041}
1042
Dan Gohman21cea8a2010-04-17 15:26:15 +00001043SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
1044 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00001045 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1046 MFI->setReturnAddressIsTaken(true);
1047
Bill Wendling908bf812014-01-06 00:43:20 +00001048 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001049 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001050
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001051 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001052 SDLoc dl(Op);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001053
1054 if (Depth > 0) {
1055 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1056 SDValue Offset =
Eric Christopherdc13b212014-06-27 00:37:59 +00001057 DAG.getConstant(getDataLayout()->getPointerSize(), MVT::i16);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001058 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1059 DAG.getNode(ISD::ADD, dl, getPointerTy(),
1060 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001061 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001062 }
1063
1064 // Just load the return address.
1065 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
1066 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001067 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001068}
1069
Dan Gohman21cea8a2010-04-17 15:26:15 +00001070SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
1071 SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001072 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1073 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00001074
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001075 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001076 SDLoc dl(Op); // FIXME probably not meaningful
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001077 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1078 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Job Noormaneb19aea2014-09-10 06:58:14 +00001079 MSP430::FP, VT);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001080 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00001081 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1082 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001083 false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001084 return FrameAddr;
1085}
1086
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001087SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
1088 SelectionDAG &DAG) const {
1089 MachineFunction &MF = DAG.getMachineFunction();
1090 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
1091
1092 // Frame index of first vararg argument
1093 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1094 getPointerTy());
1095 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1096
1097 // Create a store of the frame index to the location operand
Andrew Trickef9de2a2013-05-25 02:42:55 +00001098 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex,
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001099 Op.getOperand(1), MachinePointerInfo(SV),
1100 false, false, 0);
1101}
1102
Anton Korobeynikov82bedb12013-07-01 19:44:44 +00001103SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op,
1104 SelectionDAG &DAG) const {
1105 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1106 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikovfee796d2013-07-14 15:11:00 +00001107 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT),
1108 getPointerTy(), Result);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +00001109}
1110
Anton Korobeynikovd3c83192009-11-07 17:15:06 +00001111/// getPostIndexedAddressParts - returns true by value, base pointer and
1112/// offset pointer and addressing mode by reference if this node can be
1113/// combined with a load / store to form a post-indexed load / store.
1114bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1115 SDValue &Base,
1116 SDValue &Offset,
1117 ISD::MemIndexedMode &AM,
1118 SelectionDAG &DAG) const {
1119
1120 LoadSDNode *LD = cast<LoadSDNode>(N);
1121 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
1122 return false;
1123
1124 EVT VT = LD->getMemoryVT();
1125 if (VT != MVT::i8 && VT != MVT::i16)
1126 return false;
1127
1128 if (Op->getOpcode() != ISD::ADD)
1129 return false;
1130
1131 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1132 uint64_t RHSC = RHS->getZExtValue();
1133 if ((VT == MVT::i16 && RHSC != 2) ||
1134 (VT == MVT::i8 && RHSC != 1))
1135 return false;
1136
1137 Base = Op->getOperand(0);
1138 Offset = DAG.getConstant(RHSC, VT);
1139 AM = ISD::POST_INC;
1140 return true;
1141 }
1142
1143 return false;
1144}
1145
1146
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001147const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
1148 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001149 default: return nullptr;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001150 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov24a63162009-12-07 02:28:41 +00001151 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikov15a515b2009-05-03 13:03:33 +00001152 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikov61763b52009-05-03 13:16:17 +00001153 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1154 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovec3f0b32009-05-03 13:07:54 +00001155 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikovcfc97052009-05-03 13:08:33 +00001156 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001157 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikov96272012009-05-03 13:12:06 +00001158 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001159 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001160 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1161 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001162 }
1163}
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001164
Chris Lattner229907c2011-07-18 04:54:35 +00001165bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1166 Type *Ty2) const {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001167 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001168 return false;
1169
1170 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1171}
1172
1173bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1174 if (!VT1.isInteger() || !VT2.isInteger())
1175 return false;
1176
1177 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1178}
1179
Chris Lattner229907c2011-07-18 04:54:35 +00001180bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001181 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001182 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001183}
1184
1185bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1186 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1187 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1188}
1189
Eli Bendersky39e7c6e2012-12-18 18:21:29 +00001190bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1191 return isZExtFree(Val.getValueType(), VT2);
1192}
1193
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001194//===----------------------------------------------------------------------===//
1195// Other Lowering Code
1196//===----------------------------------------------------------------------===//
1197
1198MachineBasicBlock*
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001199MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001200 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001201 MachineFunction *F = BB->getParent();
1202 MachineRegisterInfo &RI = F->getRegInfo();
1203 DebugLoc dl = MI->getDebugLoc();
Eric Christopherd9134482014-08-04 21:25:23 +00001204 const TargetInstrInfo &TII =
1205 *getTargetMachine().getSubtargetImpl()->getInstrInfo();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001206
1207 unsigned Opc;
1208 const TargetRegisterClass * RC;
1209 switch (MI->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00001210 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001211 case MSP430::Shl8:
1212 Opc = MSP430::SHL8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001213 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001214 break;
1215 case MSP430::Shl16:
1216 Opc = MSP430::SHL16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001217 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001218 break;
1219 case MSP430::Sra8:
1220 Opc = MSP430::SAR8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001221 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001222 break;
1223 case MSP430::Sra16:
1224 Opc = MSP430::SAR16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001225 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001226 break;
1227 case MSP430::Srl8:
1228 Opc = MSP430::SAR8r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001229 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001230 break;
1231 case MSP430::Srl16:
1232 Opc = MSP430::SAR16r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001233 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001234 break;
1235 }
1236
1237 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1238 MachineFunction::iterator I = BB;
1239 ++I;
1240
1241 // Create loop block
1242 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1243 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1244
1245 F->insert(I, LoopBB);
1246 F->insert(I, RemBB);
1247
1248 // Update machine-CFG edges by transferring all successors of the current
1249 // block to the block containing instructions after shift.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001250 RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00001251 BB->end());
1252 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001253
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001254 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1255 BB->addSuccessor(LoopBB);
1256 BB->addSuccessor(RemBB);
1257 LoopBB->addSuccessor(RemBB);
1258 LoopBB->addSuccessor(LoopBB);
1259
Craig Topperc7242e02012-04-20 07:30:17 +00001260 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1261 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001262 unsigned ShiftReg = RI.createVirtualRegister(RC);
1263 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1264 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1265 unsigned SrcReg = MI->getOperand(1).getReg();
1266 unsigned DstReg = MI->getOperand(0).getReg();
1267
1268 // BB:
1269 // cmp 0, N
1270 // je RemBB
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +00001271 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1272 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001273 BuildMI(BB, dl, TII.get(MSP430::JCC))
1274 .addMBB(RemBB)
1275 .addImm(MSP430CC::COND_E);
1276
1277 // LoopBB:
1278 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1279 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1280 // ShiftReg2 = shift ShiftReg
1281 // ShiftAmt2 = ShiftAmt - 1;
1282 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1283 .addReg(SrcReg).addMBB(BB)
1284 .addReg(ShiftReg2).addMBB(LoopBB);
1285 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1286 .addReg(ShiftAmtSrcReg).addMBB(BB)
1287 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1288 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1289 .addReg(ShiftReg);
1290 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1291 .addReg(ShiftAmtReg).addImm(1);
1292 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1293 .addMBB(LoopBB)
1294 .addImm(MSP430CC::COND_NE);
1295
1296 // RemBB:
1297 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman34396292010-07-06 20:24:04 +00001298 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001299 .addReg(SrcReg).addMBB(BB)
1300 .addReg(ShiftReg2).addMBB(LoopBB);
1301
Dan Gohman34396292010-07-06 20:24:04 +00001302 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001303 return RemBB;
1304}
1305
1306MachineBasicBlock*
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001307MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001308 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001309 unsigned Opc = MI->getOpcode();
1310
1311 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1312 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1313 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohman25c16532010-05-01 00:01:06 +00001314 return EmitShiftInstr(MI, BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001315
Eric Christopherd9134482014-08-04 21:25:23 +00001316 const TargetInstrInfo &TII =
1317 *getTargetMachine().getSubtargetImpl()->getInstrInfo();
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001318 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001319
1320 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001321 "Unexpected instr type to insert");
1322
1323 // To "insert" a SELECT instruction, we actually have to insert the diamond
1324 // control-flow pattern. The incoming instruction knows the destination vreg
1325 // to set, the condition code register to branch on, the true/false values to
1326 // select between, and a branch opcode to use.
1327 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1328 MachineFunction::iterator I = BB;
1329 ++I;
1330
1331 // thisMBB:
1332 // ...
1333 // TrueVal = ...
1334 // cmpTY ccX, r1, r2
1335 // jCC copy1MBB
1336 // fallthrough --> copy0MBB
1337 MachineBasicBlock *thisMBB = BB;
1338 MachineFunction *F = BB->getParent();
1339 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1340 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001341 F->insert(I, copy0MBB);
1342 F->insert(I, copy1MBB);
1343 // Update machine-CFG edges by transferring all successors of the current
1344 // block to the new block which will contain the Phi node for the select.
Dan Gohman34396292010-07-06 20:24:04 +00001345 copy1MBB->splice(copy1MBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001346 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00001347 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001348 // Next, add the true and fallthrough blocks as its successors.
1349 BB->addSuccessor(copy0MBB);
1350 BB->addSuccessor(copy1MBB);
1351
Dan Gohman34396292010-07-06 20:24:04 +00001352 BuildMI(BB, dl, TII.get(MSP430::JCC))
1353 .addMBB(copy1MBB)
1354 .addImm(MI->getOperand(3).getImm());
1355
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001356 // copy0MBB:
1357 // %FalseValue = ...
1358 // # fallthrough to copy1MBB
1359 BB = copy0MBB;
1360
1361 // Update machine-CFG edges
1362 BB->addSuccessor(copy1MBB);
1363
1364 // copy1MBB:
1365 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1366 // ...
1367 BB = copy1MBB;
Dan Gohman34396292010-07-06 20:24:04 +00001368 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001369 MI->getOperand(0).getReg())
1370 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1371 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1372
Dan Gohman34396292010-07-06 20:24:04 +00001373 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001374 return BB;
1375}