blob: a1a7c5e08ef874231deead1c9920f35bac06c515 [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
14#define DEBUG_TYPE "msp430-lower"
15
16#include "MSP430ISelLowering.h"
17#include "MSP430.h"
Anton Korobeynikovff4ab512009-12-07 02:28:10 +000018#include "MSP430MachineFunctionInfo.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000019#include "MSP430Subtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "MSP430TargetMachine.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000021#include "llvm/CodeGen/CallingConvLower.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000028#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#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"
Bill Wendlingdf7dd282014-01-05 01:47:20 +000035#include "llvm/IR/LLVMContext.h"
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000036#include "llvm/Support/CommandLine.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000037#include "llvm/Support/Debug.h"
Torok Edwinfa040022009-07-08 19:04:27 +000038#include "llvm/Support/ErrorHandling.h"
Chris Lattner317dbbc2009-08-23 07:05:07 +000039#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000040using namespace llvm;
41
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000042typedef enum {
43 NoHWMult,
44 HWMultIntr,
45 HWMultNoIntr
46} HWMultUseMode;
47
48static cl::opt<HWMultUseMode>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000049HWMultMode("msp430-hwmult-mode", cl::Hidden,
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000050 cl::desc("Hardware multiplier use mode"),
51 cl::init(HWMultNoIntr),
52 cl::values(
53 clEnumValN(NoHWMult, "no",
54 "Do not use hardware multiplier"),
55 clEnumValN(HWMultIntr, "interrupts",
56 "Assume hardware multiplier can be used inside interrupts"),
57 clEnumValN(HWMultNoIntr, "use",
58 "Assume hardware multiplier cannot be used inside interrupts"),
59 clEnumValEnd));
60
Anton Korobeynikov10138002009-05-03 12:57:15 +000061MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
Chris Lattner5e693ed2009-07-28 03:13:23 +000062 TargetLowering(tm, new TargetLoweringObjectFileELF()),
Benjamin Kramer628a39f2012-06-06 18:25:08 +000063 Subtarget(*tm.getSubtargetImpl()) {
Anton Korobeynikov10138002009-05-03 12:57:15 +000064
Micah Villmowcdfe20b2012-10-08 16:38:25 +000065 TD = getDataLayout();
Anton Korobeynikovff4ab512009-12-07 02:28:10 +000066
Anton Korobeynikov10138002009-05-03 12:57:15 +000067 // Set up the register classes.
Craig Topperc7242e02012-04-20 07:30:17 +000068 addRegisterClass(MVT::i8, &MSP430::GR8RegClass);
69 addRegisterClass(MVT::i16, &MSP430::GR16RegClass);
Anton Korobeynikov10138002009-05-03 12:57:15 +000070
71 // Compute derived properties from the register classes
72 computeRegisterProperties();
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +000073
Anton Korobeynikov55a085b2009-05-03 13:03:14 +000074 // Provide all sorts of operation actions
75
76 // Division is expensive
77 setIntDivIsCheap(false);
78
Anton Korobeynikov7212c152009-05-03 13:11:35 +000079 setStackPointerRegisterToSaveRestore(MSP430::SPW);
80 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sandsf2641e12011-09-06 19:07:46 +000081 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Anton Korobeynikov7212c152009-05-03 13:11:35 +000082
Anton Korobeynikovcf84ab52009-11-07 17:15:25 +000083 // We have post-incremented loads / stores.
Anton Korobeynikovd3c83192009-11-07 17:15:06 +000084 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
85 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
86
87 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
88 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
89 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
90 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Anton Korobeynikov31ecd232009-05-03 13:06:03 +000092
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000093 // We don't have any truncstores
Owen Anderson9f944592009-08-11 20:47:22 +000094 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000095
Owen Anderson9f944592009-08-11 20:47:22 +000096 setOperationAction(ISD::SRA, MVT::i8, Custom);
97 setOperationAction(ISD::SHL, MVT::i8, Custom);
98 setOperationAction(ISD::SRL, MVT::i8, Custom);
99 setOperationAction(ISD::SRA, MVT::i16, Custom);
100 setOperationAction(ISD::SHL, MVT::i16, Custom);
101 setOperationAction(ISD::SRL, MVT::i16, Custom);
102 setOperationAction(ISD::ROTL, MVT::i8, Expand);
103 setOperationAction(ISD::ROTR, MVT::i8, Expand);
104 setOperationAction(ISD::ROTL, MVT::i16, Expand);
105 setOperationAction(ISD::ROTR, MVT::i16, Expand);
106 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
107 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000108 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000109 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000110 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
111 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
112 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000113 setOperationAction(ISD::SETCC, MVT::i8, Custom);
114 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000115 setOperationAction(ISD::SELECT, MVT::i8, Expand);
116 setOperationAction(ISD::SELECT, MVT::i16, Expand);
117 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
118 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
119 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov271cdda2009-08-25 17:00:23 +0000120 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
121 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000122
Owen Anderson9f944592009-08-11 20:47:22 +0000123 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
124 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000125 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Expand);
126 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000127 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
128 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000129 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Expand);
130 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000131 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
132 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000133
Owen Anderson9f944592009-08-11 20:47:22 +0000134 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
135 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
136 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
137 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
138 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
139 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000140
Owen Anderson9f944592009-08-11 20:47:22 +0000141 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000142
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000143 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000144 setOperationAction(ISD::MUL, MVT::i8, Expand);
145 setOperationAction(ISD::MULHS, MVT::i8, Expand);
146 setOperationAction(ISD::MULHU, MVT::i8, Expand);
147 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
148 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000149 setOperationAction(ISD::MUL, MVT::i16, Expand);
150 setOperationAction(ISD::MULHS, MVT::i16, Expand);
151 setOperationAction(ISD::MULHU, MVT::i16, Expand);
152 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
153 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikoveb2152f2009-05-03 13:18:33 +0000154
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000155 setOperationAction(ISD::UDIV, MVT::i8, Expand);
156 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
157 setOperationAction(ISD::UREM, MVT::i8, Expand);
158 setOperationAction(ISD::SDIV, MVT::i8, Expand);
159 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
160 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000161 setOperationAction(ISD::UDIV, MVT::i16, Expand);
162 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
163 setOperationAction(ISD::UREM, MVT::i16, Expand);
164 setOperationAction(ISD::SDIV, MVT::i16, Expand);
165 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
166 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000167
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000168 // varargs support
169 setOperationAction(ISD::VASTART, MVT::Other, Custom);
170 setOperationAction(ISD::VAARG, MVT::Other, Expand);
171 setOperationAction(ISD::VAEND, MVT::Other, Expand);
172 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000173 setOperationAction(ISD::JumpTable, MVT::i16, Custom);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000174
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000175 // Libcalls names.
176 if (HWMultMode == HWMultIntr) {
177 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
178 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
179 } else if (HWMultMode == HWMultNoIntr) {
180 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
181 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
182 }
Eli Friedman2518f832011-05-06 20:34:06 +0000183
184 setMinFunctionAlignment(1);
185 setPrefFunctionAlignment(2);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000186}
187
Dan Gohman21cea8a2010-04-17 15:26:15 +0000188SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
189 SelectionDAG &DAG) const {
Anton Korobeynikov10138002009-05-03 12:57:15 +0000190 switch (Op.getOpcode()) {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000191 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000192 case ISD::SRL:
Anton Korobeynikov56135102009-05-03 13:07:31 +0000193 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000194 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000195 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000196 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000197 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000198 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
199 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000200 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000201 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
202 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000203 case ISD::VASTART: return LowerVASTART(Op, DAG);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000204 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000205 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000206 llvm_unreachable("unimplemented operand");
Anton Korobeynikov10138002009-05-03 12:57:15 +0000207 }
208}
209
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000210//===----------------------------------------------------------------------===//
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000211// MSP430 Inline Assembly Support
212//===----------------------------------------------------------------------===//
213
214/// getConstraintType - Given a constraint letter, return the type of
215/// constraint it is for this target.
216TargetLowering::ConstraintType
217MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
218 if (Constraint.size() == 1) {
219 switch (Constraint[0]) {
220 case 'r':
221 return C_RegisterClass;
222 default:
223 break;
224 }
225 }
226 return TargetLowering::getConstraintType(Constraint);
227}
228
229std::pair<unsigned, const TargetRegisterClass*>
230MSP430TargetLowering::
231getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +0000232 MVT VT) const {
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000233 if (Constraint.size() == 1) {
234 // GCC Constraint Letters
235 switch (Constraint[0]) {
236 default: break;
237 case 'r': // GENERAL_REGS
238 if (VT == MVT::i8)
Craig Topperc7242e02012-04-20 07:30:17 +0000239 return std::make_pair(0U, &MSP430::GR8RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000240
Craig Topperc7242e02012-04-20 07:30:17 +0000241 return std::make_pair(0U, &MSP430::GR16RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000242 }
243 }
244
245 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
246}
247
248//===----------------------------------------------------------------------===//
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000249// Calling Convention Implementation
250//===----------------------------------------------------------------------===//
251
Anton Korobeynikov10138002009-05-03 12:57:15 +0000252#include "MSP430GenCallingConv.inc"
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000253
Job Noormane9a1d4c2013-10-15 08:19:39 +0000254/// For each argument in a function store the number of pieces it is composed
255/// of.
256template<typename ArgT>
257static void ParseFunctionArgs(const SmallVectorImpl<ArgT> &Args,
258 SmallVectorImpl<unsigned> &Out) {
259 unsigned CurrentArgIndex = ~0U;
260 for (unsigned i = 0, e = Args.size(); i != e; i++) {
261 if (CurrentArgIndex == Args[i].OrigArgIndex) {
262 Out.back()++;
263 } else {
264 Out.push_back(1);
265 CurrentArgIndex++;
266 }
267 }
268}
269
270static void AnalyzeVarArgs(CCState &State,
271 const SmallVectorImpl<ISD::OutputArg> &Outs) {
272 State.AnalyzeCallOperands(Outs, CC_MSP430_AssignStack);
273}
274
275static void AnalyzeVarArgs(CCState &State,
276 const SmallVectorImpl<ISD::InputArg> &Ins) {
277 State.AnalyzeFormalArguments(Ins, CC_MSP430_AssignStack);
278}
279
280/// Analyze incoming and outgoing function arguments. We need custom C++ code
281/// to handle special constraints in the ABI like reversing the order of the
282/// pieces of splitted arguments. In addition, all pieces of a certain argument
283/// have to be passed either using registers or the stack but never mixing both.
284template<typename ArgT>
285static void AnalyzeArguments(CCState &State,
286 SmallVectorImpl<CCValAssign> &ArgLocs,
287 const SmallVectorImpl<ArgT> &Args) {
288 static const uint16_t RegList[] = {
289 MSP430::R15W, MSP430::R14W, MSP430::R13W, MSP430::R12W
290 };
291 static const unsigned NbRegs = array_lengthof(RegList);
292
293 if (State.isVarArg()) {
294 AnalyzeVarArgs(State, Args);
295 return;
296 }
297
298 SmallVector<unsigned, 4> ArgsParts;
299 ParseFunctionArgs(Args, ArgsParts);
300
301 unsigned RegsLeft = NbRegs;
302 bool UseStack = false;
303 unsigned ValNo = 0;
304
305 for (unsigned i = 0, e = ArgsParts.size(); i != e; i++) {
306 MVT ArgVT = Args[ValNo].VT;
307 ISD::ArgFlagsTy ArgFlags = Args[ValNo].Flags;
308 MVT LocVT = ArgVT;
309 CCValAssign::LocInfo LocInfo = CCValAssign::Full;
310
311 // Promote i8 to i16
312 if (LocVT == MVT::i8) {
313 LocVT = MVT::i16;
314 if (ArgFlags.isSExt())
315 LocInfo = CCValAssign::SExt;
316 else if (ArgFlags.isZExt())
317 LocInfo = CCValAssign::ZExt;
318 else
319 LocInfo = CCValAssign::AExt;
320 }
321
322 // Handle byval arguments
323 if (ArgFlags.isByVal()) {
324 State.HandleByVal(ValNo++, ArgVT, LocVT, LocInfo, 2, 2, ArgFlags);
325 continue;
326 }
327
328 unsigned Parts = ArgsParts[i];
329
330 if (!UseStack && Parts <= RegsLeft) {
331 unsigned FirstVal = ValNo;
332 for (unsigned j = 0; j < Parts; j++) {
333 unsigned Reg = State.AllocateReg(RegList, NbRegs);
334 State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo));
335 RegsLeft--;
336 }
337
338 // Reverse the order of the pieces to agree with the "big endian" format
339 // required in the calling convention ABI.
340 SmallVectorImpl<CCValAssign>::iterator B = ArgLocs.begin() + FirstVal;
341 std::reverse(B, B + Parts);
342 } else {
343 UseStack = true;
344 for (unsigned j = 0; j < Parts; j++)
345 CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State);
346 }
347 }
348}
349
350static void AnalyzeRetResult(CCState &State,
351 const SmallVectorImpl<ISD::InputArg> &Ins) {
352 State.AnalyzeCallResult(Ins, RetCC_MSP430);
353}
354
355static void AnalyzeRetResult(CCState &State,
356 const SmallVectorImpl<ISD::OutputArg> &Outs) {
357 State.AnalyzeReturn(Outs, RetCC_MSP430);
358}
359
360template<typename ArgT>
361static void AnalyzeReturnValues(CCState &State,
362 SmallVectorImpl<CCValAssign> &RVLocs,
363 const SmallVectorImpl<ArgT> &Args) {
364 AnalyzeRetResult(State, Args);
365
366 // Reverse splitted return values to get the "big endian" format required
367 // to agree with the calling convention ABI.
368 std::reverse(RVLocs.begin(), RVLocs.end());
369}
370
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000371SDValue
372MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000373 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000374 bool isVarArg,
375 const SmallVectorImpl<ISD::InputArg>
376 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000377 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000378 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000379 SmallVectorImpl<SDValue> &InVals)
380 const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000381
382 switch (CallConv) {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000383 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000384 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000385 case CallingConv::C:
386 case CallingConv::Fast:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000387 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000388 case CallingConv::MSP430_INTR:
David Blaikie46a9f012012-01-20 21:51:11 +0000389 if (Ins.empty())
390 return Chain;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000391 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000392 }
393}
394
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000395SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000396MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000397 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000398 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000399 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000400 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
401 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
402 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000403 SDValue Chain = CLI.Chain;
404 SDValue Callee = CLI.Callee;
405 bool &isTailCall = CLI.IsTailCall;
406 CallingConv::ID CallConv = CLI.CallConv;
407 bool isVarArg = CLI.IsVarArg;
408
Evan Cheng67a69dd2010-01-27 00:07:07 +0000409 // MSP430 target does not yet support tail call optimization.
410 isTailCall = false;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000411
412 switch (CallConv) {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000413 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000414 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000415 case CallingConv::Fast:
416 case CallingConv::C:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000417 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000418 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000419 case CallingConv::MSP430_INTR:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000420 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000421 }
422}
423
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000424/// LowerCCCArguments - transform physical registers into virtual registers and
425/// generate load operations for arguments places on the stack.
426// FIXME: struct return stuff
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000427SDValue
428MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000429 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000430 bool isVarArg,
431 const SmallVectorImpl<ISD::InputArg>
432 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000433 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000434 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000435 SmallVectorImpl<SDValue> &InVals)
436 const {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000437 MachineFunction &MF = DAG.getMachineFunction();
438 MachineFrameInfo *MFI = MF.getFrameInfo();
439 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000440 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000441
442 // Assign locations to all of the incoming arguments.
443 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000444 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000445 getTargetMachine(), ArgLocs, *DAG.getContext());
Job Noormane9a1d4c2013-10-15 08:19:39 +0000446 AnalyzeArguments(CCInfo, ArgLocs, Ins);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000447
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000448 // Create frame index for the start of the first vararg value
449 if (isVarArg) {
450 unsigned Offset = CCInfo.getNextStackOffset();
451 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, Offset, true));
452 }
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000453
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000454 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
455 CCValAssign &VA = ArgLocs[i];
456 if (VA.isRegLoc()) {
457 // Arguments passed in registers
Owen Anderson53aa7a92009-08-10 22:56:29 +0000458 EVT RegVT = VA.getLocVT();
Owen Anderson9f944592009-08-11 20:47:22 +0000459 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Andersonb2c80da2011-02-25 21:41:48 +0000460 default:
Torok Edwinfa040022009-07-08 19:04:27 +0000461 {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000462#ifndef NDEBUG
Chris Lattner317dbbc2009-08-23 07:05:07 +0000463 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson9f944592009-08-11 20:47:22 +0000464 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000465#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000466 llvm_unreachable(0);
Torok Edwinfa040022009-07-08 19:04:27 +0000467 }
Owen Anderson9f944592009-08-11 20:47:22 +0000468 case MVT::i16:
Craig Topperc7242e02012-04-20 07:30:17 +0000469 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000470 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000471 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000472
473 // If this is an 8-bit value, it is really passed promoted to 16
474 // bits. Insert an assert[sz]ext to capture this, then truncate to the
475 // right size.
476 if (VA.getLocInfo() == CCValAssign::SExt)
477 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
478 DAG.getValueType(VA.getValVT()));
479 else if (VA.getLocInfo() == CCValAssign::ZExt)
480 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
481 DAG.getValueType(VA.getValVT()));
482
483 if (VA.getLocInfo() != CCValAssign::Full)
484 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
485
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000486 InVals.push_back(ArgValue);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000487 }
488 } else {
489 // Sanity check
490 assert(VA.isMemLoc());
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000491
Anton Korobeynikov34148722012-11-21 17:23:03 +0000492 SDValue InVal;
493 ISD::ArgFlagsTy Flags = Ins[i].Flags;
494
495 if (Flags.isByVal()) {
496 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
497 VA.getLocMemOffset(), true);
498 InVal = DAG.getFrameIndex(FI, getPointerTy());
499 } else {
500 // Load the argument to a virtual register
501 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
502 if (ObjSize > 2) {
503 errs() << "LowerFormalArguments Unhandled argument type: "
504 << EVT(VA.getLocVT()).getEVTString()
505 << "\n";
506 }
507 // Create the frame index object for this incoming parameter...
508 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
509
510 // Create the SelectionDAG nodes corresponding to a load
511 //from this parameter
512 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
513 InVal = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
514 MachinePointerInfo::getFixedStack(FI),
515 false, false, false, 0);
516 }
517
518 InVals.push_back(InVal);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000519 }
520 }
521
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000522 return Chain;
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000523}
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000524
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000525SDValue
526MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000527 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000528 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000529 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000530 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000531
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000532 // CCValAssign - represent the assignment of the return value to a location
533 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000534
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000535 // ISRs cannot return any value.
David Blaikie46a9f012012-01-20 21:51:11 +0000536 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
Chris Lattner2104b8d2010-04-07 22:58:41 +0000537 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000538
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000539 // CCState - Info about the registers and stack slot.
Eric Christopher0713a9d2011-06-08 23:55:35 +0000540 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000541 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000542
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000543 // Analize return values.
Job Noormane9a1d4c2013-10-15 08:19:39 +0000544 AnalyzeReturnValues(CCInfo, RVLocs, Outs);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000545
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000546 SDValue Flag;
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000547 SmallVector<SDValue, 4> RetOps(1, Chain);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000548
549 // Copy the result values into the output registers.
550 for (unsigned i = 0; i != RVLocs.size(); ++i) {
551 CCValAssign &VA = RVLocs[i];
552 assert(VA.isRegLoc() && "Can only return in registers!");
553
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000554 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000555 OutVals[i], Flag);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000556
Anton Korobeynikovc10f98a2009-05-03 13:00:11 +0000557 // Guarantee that all emitted copies are stuck together,
558 // avoiding something bad.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000559 Flag = Chain.getValue(1);
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000560 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000561 }
562
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000563 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
564 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
565
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000566 RetOps[0] = Chain; // Update chain.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000567
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000568 // Add the flag if we have it.
569 if (Flag.getNode())
570 RetOps.push_back(Flag);
571
572 return DAG.getNode(Opc, dl, MVT::Other, &RetOps[0], RetOps.size());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000573}
574
Anton Korobeynikov56135102009-05-03 13:07:31 +0000575/// LowerCCCCallTo - functions arguments are copied from virtual regs to
576/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Job Noormana928e1d2013-07-15 14:25:26 +0000577// TODO: sret.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000578SDValue
579MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000580 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000581 bool isTailCall,
582 const SmallVectorImpl<ISD::OutputArg>
583 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000584 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000585 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000586 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000587 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000588 // Analyze operands of the call, assigning locations to each operand.
589 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000590 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000591 getTargetMachine(), ArgLocs, *DAG.getContext());
Job Noormane9a1d4c2013-10-15 08:19:39 +0000592 AnalyzeArguments(CCInfo, ArgLocs, Outs);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000593
594 // Get a count of how many bytes are to be pushed on the stack.
595 unsigned NumBytes = CCInfo.getNextStackOffset();
596
597 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
Andrew Trickad6d08a2013-05-29 22:03:55 +0000598 getPointerTy(), true),
599 dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000600
601 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
602 SmallVector<SDValue, 12> MemOpChains;
603 SDValue StackPtr;
604
605 // Walk the register/memloc assignments, inserting copies/loads.
606 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
607 CCValAssign &VA = ArgLocs[i];
608
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000609 SDValue Arg = OutVals[i];
Anton Korobeynikov56135102009-05-03 13:07:31 +0000610
611 // Promote the value if needed.
612 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000613 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000614 case CCValAssign::Full: break;
615 case CCValAssign::SExt:
616 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
617 break;
618 case CCValAssign::ZExt:
619 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
620 break;
621 case CCValAssign::AExt:
622 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
623 break;
624 }
625
626 // Arguments that can be passed on register must be kept at RegsToPass
627 // vector
628 if (VA.isRegLoc()) {
629 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
630 } else {
631 assert(VA.isMemLoc());
632
633 if (StackPtr.getNode() == 0)
634 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
635
636 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
637 StackPtr,
638 DAG.getIntPtrConstant(VA.getLocMemOffset()));
639
Anton Korobeynikov34148722012-11-21 17:23:03 +0000640 SDValue MemOp;
641 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000642
Anton Korobeynikov34148722012-11-21 17:23:03 +0000643 if (Flags.isByVal()) {
644 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16);
645 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
646 Flags.getByValAlign(),
647 /*isVolatile*/false,
648 /*AlwaysInline=*/true,
649 MachinePointerInfo(),
650 MachinePointerInfo());
651 } else {
652 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(),
653 false, false, 0);
654 }
655
656 MemOpChains.push_back(MemOp);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000657 }
658 }
659
660 // Transform all store nodes into one single node because all store nodes are
661 // independent of each other.
662 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +0000663 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov56135102009-05-03 13:07:31 +0000664 &MemOpChains[0], MemOpChains.size());
665
666 // Build a sequence of copy-to-reg nodes chained together with token chain and
667 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000668 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov56135102009-05-03 13:07:31 +0000669 SDValue InFlag;
670 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
671 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
672 RegsToPass[i].second, InFlag);
673 InFlag = Chain.getValue(1);
674 }
675
676 // If the callee is a GlobalAddress node (quite common, every direct call is)
677 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
678 // Likewise ExternalSymbol -> TargetExternalSymbol.
679 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patela3ca21b2010-07-06 22:08:15 +0000680 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000681 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson9f944592009-08-11 20:47:22 +0000682 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000683
684 // Returns a chain & a flag for retval copy to use.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000685 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000686 SmallVector<SDValue, 8> Ops;
687 Ops.push_back(Chain);
688 Ops.push_back(Callee);
689
690 // Add argument registers to the end of the list so that they are
691 // known live into the call.
692 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
693 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
694 RegsToPass[i].second.getValueType()));
695
696 if (InFlag.getNode())
697 Ops.push_back(InFlag);
698
699 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
700 InFlag = Chain.getValue(1);
701
702 // Create the CALLSEQ_END node.
703 Chain = DAG.getCALLSEQ_END(Chain,
704 DAG.getConstant(NumBytes, getPointerTy(), true),
705 DAG.getConstant(0, getPointerTy(), true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000706 InFlag, dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000707 InFlag = Chain.getValue(1);
708
709 // Handle result values, copying them out of physregs into vregs that we
710 // return.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000711 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
712 DAG, InVals);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000713}
714
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000715/// LowerCallResult - Lower the result values of a call into the
716/// appropriate copies out of appropriate physical registers.
717///
718SDValue
Anton Korobeynikov56135102009-05-03 13:07:31 +0000719MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000720 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000721 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000722 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000723 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000724
725 // Assign locations to each value returned by this call.
726 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000727 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000728 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000729
Job Noormane9a1d4c2013-10-15 08:19:39 +0000730 AnalyzeReturnValues(CCInfo, RVLocs, Ins);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000731
732 // Copy all of the result registers out of their specified physreg.
733 for (unsigned i = 0; i != RVLocs.size(); ++i) {
734 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
735 RVLocs[i].getValVT(), InFlag).getValue(1);
736 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000737 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov56135102009-05-03 13:07:31 +0000738 }
739
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000740 return Chain;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000741}
742
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000743SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000744 SelectionDAG &DAG) const {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000745 unsigned Opc = Op.getOpcode();
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000746 SDNode* N = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000747 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000748 SDLoc dl(N);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000749
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000750 // Expand non-constant shifts to loops:
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000751 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000752 switch (Opc) {
Craig Toppere55c5562012-02-07 02:50:20 +0000753 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000754 case ISD::SHL:
755 return DAG.getNode(MSP430ISD::SHL, dl,
756 VT, N->getOperand(0), N->getOperand(1));
757 case ISD::SRA:
758 return DAG.getNode(MSP430ISD::SRA, dl,
759 VT, N->getOperand(0), N->getOperand(1));
760 case ISD::SRL:
761 return DAG.getNode(MSP430ISD::SRL, dl,
762 VT, N->getOperand(0), N->getOperand(1));
763 }
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000764
765 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
766
767 // Expand the stuff into sequence of shifts.
768 // FIXME: for some shift amounts this might be done better!
769 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
770 SDValue Victim = N->getOperand(0);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000771
772 if (Opc == ISD::SRL && ShiftAmount) {
773 // Emit a special goodness here:
774 // srl A, 1 => clrc; rrc A
Anton Korobeynikovf3a6bc82009-05-03 13:16:37 +0000775 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000776 ShiftAmount -= 1;
777 }
778
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000779 while (ShiftAmount--)
Anton Korobeynikov6b5523a2009-05-17 10:15:22 +0000780 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000781 dl, VT, Victim);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000782
783 return Victim;
784}
785
Dan Gohman21cea8a2010-04-17 15:26:15 +0000786SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
787 SelectionDAG &DAG) const {
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000788 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
789 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
790
791 // Create the TargetGlobalAddress node, folding in the constant offset.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000792 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
Devang Patela3ca21b2010-07-06 22:08:15 +0000793 getPointerTy(), Offset);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000794 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op),
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000795 getPointerTy(), Result);
796}
797
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000798SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000799 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000800 SDLoc dl(Op);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000801 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
802 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
803
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000804 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000805}
806
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000807SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
808 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000809 SDLoc dl(Op);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000810 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liaoabb87d42012-09-12 21:43:09 +0000811 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000812
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000813 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000814}
815
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000816static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000817 ISD::CondCode CC,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000818 SDLoc dl, SelectionDAG &DAG) {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000819 // FIXME: Handle bittests someday
820 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
821
822 // FIXME: Handle jump negative someday
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000823 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000824 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000825 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov96272012009-05-03 13:12:06 +0000826 case ISD::SETEQ:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000827 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000828 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000829 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000830 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000831 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000832 break;
833 case ISD::SETNE:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000834 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000835 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000836 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000837 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000838 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000839 break;
840 case ISD::SETULE:
841 std::swap(LHS, RHS); // FALLTHROUGH
842 case ISD::SETUGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000843 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
844 // fold constant into instruction.
845 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
846 LHS = RHS;
847 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
848 TCC = MSP430CC::COND_LO;
849 break;
850 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000851 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikov96272012009-05-03 13:12:06 +0000852 break;
853 case ISD::SETUGT:
854 std::swap(LHS, RHS); // FALLTHROUGH
855 case ISD::SETULT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000856 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
857 // fold constant into instruction.
858 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
859 LHS = RHS;
860 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
861 TCC = MSP430CC::COND_HS;
862 break;
863 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000864 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikov96272012009-05-03 13:12:06 +0000865 break;
866 case ISD::SETLE:
867 std::swap(LHS, RHS); // FALLTHROUGH
868 case ISD::SETGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000869 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
870 // fold constant into instruction.
871 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
872 LHS = RHS;
873 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
874 TCC = MSP430CC::COND_L;
875 break;
876 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000877 TCC = MSP430CC::COND_GE;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000878 break;
879 case ISD::SETGT:
880 std::swap(LHS, RHS); // FALLTHROUGH
881 case ISD::SETLT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000882 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
883 // fold constant into instruction.
884 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
885 LHS = RHS;
886 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
887 TCC = MSP430CC::COND_GE;
888 break;
889 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000890 TCC = MSP430CC::COND_L;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000891 break;
892 }
893
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000894 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000895 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000896}
897
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000898
Dan Gohman21cea8a2010-04-17 15:26:15 +0000899SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000900 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000901 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
902 SDValue LHS = Op.getOperand(2);
903 SDValue RHS = Op.getOperand(3);
904 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000905 SDLoc dl (Op);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000906
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000907 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000908 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000909
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000910 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000911 Chain, Dest, TargetCC, Flag);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000912}
913
Dan Gohman21cea8a2010-04-17 15:26:15 +0000914SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000915 SDValue LHS = Op.getOperand(0);
916 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000917 SDLoc dl (Op);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000918
919 // If we are doing an AND and testing against zero, then the CMP
920 // will not be generated. The AND (or BIT) will generate the condition codes,
921 // but they are different from CMP.
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000922 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
923 // lowering & isel wouldn't diverge.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000924 bool andCC = false;
925 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
926 if (RHSC->isNullValue() && LHS.hasOneUse() &&
927 (LHS.getOpcode() == ISD::AND ||
928 (LHS.getOpcode() == ISD::TRUNCATE &&
929 LHS.getOperand(0).getOpcode() == ISD::AND))) {
930 andCC = true;
931 }
932 }
933 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
934 SDValue TargetCC;
935 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
936
937 // Get the condition codes directly from the status register, if its easy.
938 // Otherwise a branch will be generated. Note that the AND and BIT
939 // instructions generate different flags than CMP, the carry bit can be used
940 // for NE/EQ.
941 bool Invert = false;
942 bool Shift = false;
943 bool Convert = true;
944 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
945 default:
946 Convert = false;
947 break;
948 case MSP430CC::COND_HS:
949 // Res = SRW & 1, no processing is required
950 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000951 case MSP430CC::COND_LO:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000952 // Res = ~(SRW & 1)
953 Invert = true;
954 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000955 case MSP430CC::COND_NE:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000956 if (andCC) {
957 // C = ~Z, thus Res = SRW & 1, no processing is required
958 } else {
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000959 // Res = ~((SRW >> 1) & 1)
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000960 Shift = true;
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000961 Invert = true;
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000962 }
963 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000964 case MSP430CC::COND_E:
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000965 Shift = true;
966 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
967 // Res = (SRW >> 1) & 1 is 1 word shorter.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000968 break;
969 }
970 EVT VT = Op.getValueType();
971 SDValue One = DAG.getConstant(1, VT);
972 if (Convert) {
973 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000974 MVT::i16, Flag);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000975 if (Shift)
976 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
977 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
978 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
979 if (Invert)
980 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
981 return SR;
982 } else {
983 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000984 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000985 SmallVector<SDValue, 4> Ops;
986 Ops.push_back(One);
987 Ops.push_back(Zero);
988 Ops.push_back(TargetCC);
989 Ops.push_back(Flag);
990 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
991 }
992}
993
Dan Gohman21cea8a2010-04-17 15:26:15 +0000994SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
995 SelectionDAG &DAG) const {
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000996 SDValue LHS = Op.getOperand(0);
997 SDValue RHS = Op.getOperand(1);
998 SDValue TrueV = Op.getOperand(2);
999 SDValue FalseV = Op.getOperand(3);
1000 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001001 SDLoc dl (Op);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001002
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +00001003 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001004 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001005
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001006 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001007 SmallVector<SDValue, 4> Ops;
1008 Ops.push_back(TrueV);
1009 Ops.push_back(FalseV);
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +00001010 Ops.push_back(TargetCC);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001011 Ops.push_back(Flag);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001012
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001013 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001014}
1015
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001016SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001017 SelectionDAG &DAG) const {
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001018 SDValue Val = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001019 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001020 SDLoc dl(Op);
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001021
Owen Anderson9f944592009-08-11 20:47:22 +00001022 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikov29747e92009-05-03 13:17:49 +00001023
1024 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
1025 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
1026 DAG.getValueType(Val.getValueType()));
1027}
1028
Dan Gohman21cea8a2010-04-17 15:26:15 +00001029SDValue
1030MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001031 MachineFunction &MF = DAG.getMachineFunction();
1032 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
1033 int ReturnAddrIndex = FuncInfo->getRAIndex();
1034
1035 if (ReturnAddrIndex == 0) {
1036 // Set up a frame object for the return address.
Chandler Carruth5da3f052012-11-01 09:14:31 +00001037 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001038 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00001039 true);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001040 FuncInfo->setRAIndex(ReturnAddrIndex);
1041 }
1042
1043 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1044}
1045
Dan Gohman21cea8a2010-04-17 15:26:15 +00001046SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
1047 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00001048 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1049 MFI->setReturnAddressIsTaken(true);
1050
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001051 if (!isa<ConstantSDNode>(Op.getOperand(0))) {
1052 DAG.getContext()->emitError("argument to '__builtin_return_address' must "
1053 "be a constant integer");
1054 return SDValue();
1055 }
1056
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001057 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001058 SDLoc dl(Op);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001059
1060 if (Depth > 0) {
1061 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1062 SDValue Offset =
Chandler Carruth5da3f052012-11-01 09:14:31 +00001063 DAG.getConstant(TD->getPointerSize(), MVT::i16);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001064 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1065 DAG.getNode(ISD::ADD, dl, getPointerTy(),
1066 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001067 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001068 }
1069
1070 // Just load the return address.
1071 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
1072 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001073 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001074}
1075
Dan Gohman21cea8a2010-04-17 15:26:15 +00001076SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
1077 SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001078 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1079 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00001080
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001081 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001082 SDLoc dl(Op); // FIXME probably not meaningful
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001083 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1084 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1085 MSP430::FPW, VT);
1086 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00001087 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1088 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001089 false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001090 return FrameAddr;
1091}
1092
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001093SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
1094 SelectionDAG &DAG) const {
1095 MachineFunction &MF = DAG.getMachineFunction();
1096 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
1097
1098 // Frame index of first vararg argument
1099 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1100 getPointerTy());
1101 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1102
1103 // Create a store of the frame index to the location operand
Andrew Trickef9de2a2013-05-25 02:42:55 +00001104 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex,
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001105 Op.getOperand(1), MachinePointerInfo(SV),
1106 false, false, 0);
1107}
1108
Anton Korobeynikov82bedb12013-07-01 19:44:44 +00001109SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op,
1110 SelectionDAG &DAG) const {
1111 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1112 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikovfee796d2013-07-14 15:11:00 +00001113 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT),
1114 getPointerTy(), Result);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +00001115}
1116
Anton Korobeynikovd3c83192009-11-07 17:15:06 +00001117/// getPostIndexedAddressParts - returns true by value, base pointer and
1118/// offset pointer and addressing mode by reference if this node can be
1119/// combined with a load / store to form a post-indexed load / store.
1120bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1121 SDValue &Base,
1122 SDValue &Offset,
1123 ISD::MemIndexedMode &AM,
1124 SelectionDAG &DAG) const {
1125
1126 LoadSDNode *LD = cast<LoadSDNode>(N);
1127 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
1128 return false;
1129
1130 EVT VT = LD->getMemoryVT();
1131 if (VT != MVT::i8 && VT != MVT::i16)
1132 return false;
1133
1134 if (Op->getOpcode() != ISD::ADD)
1135 return false;
1136
1137 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1138 uint64_t RHSC = RHS->getZExtValue();
1139 if ((VT == MVT::i16 && RHSC != 2) ||
1140 (VT == MVT::i8 && RHSC != 1))
1141 return false;
1142
1143 Base = Op->getOperand(0);
1144 Offset = DAG.getConstant(RHSC, VT);
1145 AM = ISD::POST_INC;
1146 return true;
1147 }
1148
1149 return false;
1150}
1151
1152
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001153const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
1154 switch (Opcode) {
1155 default: return NULL;
1156 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov24a63162009-12-07 02:28:41 +00001157 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikov15a515b2009-05-03 13:03:33 +00001158 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikov61763b52009-05-03 13:16:17 +00001159 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1160 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovec3f0b32009-05-03 13:07:54 +00001161 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikovcfc97052009-05-03 13:08:33 +00001162 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001163 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikov96272012009-05-03 13:12:06 +00001164 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001165 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001166 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1167 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001168 }
1169}
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001170
Chris Lattner229907c2011-07-18 04:54:35 +00001171bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1172 Type *Ty2) const {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001173 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001174 return false;
1175
1176 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1177}
1178
1179bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1180 if (!VT1.isInteger() || !VT2.isInteger())
1181 return false;
1182
1183 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1184}
1185
Chris Lattner229907c2011-07-18 04:54:35 +00001186bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001187 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001188 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001189}
1190
1191bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1192 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1193 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1194}
1195
Eli Bendersky39e7c6e2012-12-18 18:21:29 +00001196bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1197 return isZExtFree(Val.getValueType(), VT2);
1198}
1199
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001200//===----------------------------------------------------------------------===//
1201// Other Lowering Code
1202//===----------------------------------------------------------------------===//
1203
1204MachineBasicBlock*
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001205MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001206 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001207 MachineFunction *F = BB->getParent();
1208 MachineRegisterInfo &RI = F->getRegInfo();
1209 DebugLoc dl = MI->getDebugLoc();
1210 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1211
1212 unsigned Opc;
1213 const TargetRegisterClass * RC;
1214 switch (MI->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00001215 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001216 case MSP430::Shl8:
1217 Opc = MSP430::SHL8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001218 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001219 break;
1220 case MSP430::Shl16:
1221 Opc = MSP430::SHL16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001222 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001223 break;
1224 case MSP430::Sra8:
1225 Opc = MSP430::SAR8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001226 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001227 break;
1228 case MSP430::Sra16:
1229 Opc = MSP430::SAR16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001230 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001231 break;
1232 case MSP430::Srl8:
1233 Opc = MSP430::SAR8r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001234 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001235 break;
1236 case MSP430::Srl16:
1237 Opc = MSP430::SAR16r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001238 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001239 break;
1240 }
1241
1242 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1243 MachineFunction::iterator I = BB;
1244 ++I;
1245
1246 // Create loop block
1247 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1248 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1249
1250 F->insert(I, LoopBB);
1251 F->insert(I, RemBB);
1252
1253 // Update machine-CFG edges by transferring all successors of the current
1254 // block to the block containing instructions after shift.
Dan Gohman34396292010-07-06 20:24:04 +00001255 RemBB->splice(RemBB->begin(), BB,
1256 llvm::next(MachineBasicBlock::iterator(MI)),
1257 BB->end());
1258 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001259
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001260 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1261 BB->addSuccessor(LoopBB);
1262 BB->addSuccessor(RemBB);
1263 LoopBB->addSuccessor(RemBB);
1264 LoopBB->addSuccessor(LoopBB);
1265
Craig Topperc7242e02012-04-20 07:30:17 +00001266 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1267 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001268 unsigned ShiftReg = RI.createVirtualRegister(RC);
1269 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1270 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1271 unsigned SrcReg = MI->getOperand(1).getReg();
1272 unsigned DstReg = MI->getOperand(0).getReg();
1273
1274 // BB:
1275 // cmp 0, N
1276 // je RemBB
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +00001277 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1278 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001279 BuildMI(BB, dl, TII.get(MSP430::JCC))
1280 .addMBB(RemBB)
1281 .addImm(MSP430CC::COND_E);
1282
1283 // LoopBB:
1284 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1285 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1286 // ShiftReg2 = shift ShiftReg
1287 // ShiftAmt2 = ShiftAmt - 1;
1288 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1289 .addReg(SrcReg).addMBB(BB)
1290 .addReg(ShiftReg2).addMBB(LoopBB);
1291 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1292 .addReg(ShiftAmtSrcReg).addMBB(BB)
1293 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1294 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1295 .addReg(ShiftReg);
1296 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1297 .addReg(ShiftAmtReg).addImm(1);
1298 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1299 .addMBB(LoopBB)
1300 .addImm(MSP430CC::COND_NE);
1301
1302 // RemBB:
1303 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman34396292010-07-06 20:24:04 +00001304 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001305 .addReg(SrcReg).addMBB(BB)
1306 .addReg(ShiftReg2).addMBB(LoopBB);
1307
Dan Gohman34396292010-07-06 20:24:04 +00001308 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001309 return RemBB;
1310}
1311
1312MachineBasicBlock*
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001313MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001314 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001315 unsigned Opc = MI->getOpcode();
1316
1317 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1318 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1319 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohman25c16532010-05-01 00:01:06 +00001320 return EmitShiftInstr(MI, BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001321
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001322 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1323 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001324
1325 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001326 "Unexpected instr type to insert");
1327
1328 // To "insert" a SELECT instruction, we actually have to insert the diamond
1329 // control-flow pattern. The incoming instruction knows the destination vreg
1330 // to set, the condition code register to branch on, the true/false values to
1331 // select between, and a branch opcode to use.
1332 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1333 MachineFunction::iterator I = BB;
1334 ++I;
1335
1336 // thisMBB:
1337 // ...
1338 // TrueVal = ...
1339 // cmpTY ccX, r1, r2
1340 // jCC copy1MBB
1341 // fallthrough --> copy0MBB
1342 MachineBasicBlock *thisMBB = BB;
1343 MachineFunction *F = BB->getParent();
1344 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1345 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001346 F->insert(I, copy0MBB);
1347 F->insert(I, copy1MBB);
1348 // Update machine-CFG edges by transferring all successors of the current
1349 // block to the new block which will contain the Phi node for the select.
Dan Gohman34396292010-07-06 20:24:04 +00001350 copy1MBB->splice(copy1MBB->begin(), BB,
1351 llvm::next(MachineBasicBlock::iterator(MI)),
1352 BB->end());
1353 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001354 // Next, add the true and fallthrough blocks as its successors.
1355 BB->addSuccessor(copy0MBB);
1356 BB->addSuccessor(copy1MBB);
1357
Dan Gohman34396292010-07-06 20:24:04 +00001358 BuildMI(BB, dl, TII.get(MSP430::JCC))
1359 .addMBB(copy1MBB)
1360 .addImm(MI->getOperand(3).getImm());
1361
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001362 // copy0MBB:
1363 // %FalseValue = ...
1364 // # fallthrough to copy1MBB
1365 BB = copy0MBB;
1366
1367 // Update machine-CFG edges
1368 BB->addSuccessor(copy1MBB);
1369
1370 // copy1MBB:
1371 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1372 // ...
1373 BB = copy1MBB;
Dan Gohman34396292010-07-06 20:24:04 +00001374 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001375 MI->getOperand(0).getReg())
1376 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1377 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1378
Dan Gohman34396292010-07-06 20:24:04 +00001379 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001380 return BB;
1381}