blob: 4305f60b15e4ab0128c1d82fa25ee7c0d7939228 [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",
Mehdi Amini732afdd2016-10-08 19:41:06 +000057 "Assume hardware multiplier cannot be used inside interrupts")));
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000058
Eric Christopher23a3a7c2015-02-26 00:00:24 +000059MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM,
60 const MSP430Subtarget &STI)
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
Eric Christopher23a3a7c2015-02-26 00:00:24 +000068 computeRegisterProperties(STI.getRegisterInfo());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +000069
Anton Korobeynikov55a085b2009-05-03 13:03:14 +000070 // Provide all sorts of operation actions
Job Noormaneb19aea2014-09-10 06:58:14 +000071 setStackPointerRegisterToSaveRestore(MSP430::SP);
Anton Korobeynikov7212c152009-05-03 13:11:35 +000072 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sandsf2641e12011-09-06 19:07:46 +000073 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Anton Korobeynikov7212c152009-05-03 13:11:35 +000074
Anton Korobeynikovcf84ab52009-11-07 17:15:25 +000075 // We have post-incremented loads / stores.
Anton Korobeynikovd3c83192009-11-07 17:15:06 +000076 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
77 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
78
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000079 for (MVT VT : MVT::integer_valuetypes()) {
80 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
81 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
82 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
83 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
84 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Expand);
85 }
Anton Korobeynikov31ecd232009-05-03 13:06:03 +000086
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000087 // We don't have any truncstores
Owen Anderson9f944592009-08-11 20:47:22 +000088 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000089
Owen Anderson9f944592009-08-11 20:47:22 +000090 setOperationAction(ISD::SRA, MVT::i8, Custom);
91 setOperationAction(ISD::SHL, MVT::i8, Custom);
92 setOperationAction(ISD::SRL, MVT::i8, Custom);
93 setOperationAction(ISD::SRA, MVT::i16, Custom);
94 setOperationAction(ISD::SHL, MVT::i16, Custom);
95 setOperationAction(ISD::SRL, MVT::i16, Custom);
96 setOperationAction(ISD::ROTL, MVT::i8, Expand);
97 setOperationAction(ISD::ROTR, MVT::i8, Expand);
98 setOperationAction(ISD::ROTL, MVT::i16, Expand);
99 setOperationAction(ISD::ROTR, MVT::i16, Expand);
100 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
101 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000102 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000103 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000104 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
105 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
106 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000107 setOperationAction(ISD::SETCC, MVT::i8, Custom);
108 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000109 setOperationAction(ISD::SELECT, MVT::i8, Expand);
110 setOperationAction(ISD::SELECT, MVT::i16, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
112 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
113 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov271cdda2009-08-25 17:00:23 +0000114 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
115 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000116
Owen Anderson9f944592009-08-11 20:47:22 +0000117 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
118 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
119 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
120 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
121 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
122 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000123
Owen Anderson9f944592009-08-11 20:47:22 +0000124 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
125 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
126 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
127 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
129 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000130
Owen Anderson9f944592009-08-11 20:47:22 +0000131 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000132
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000133 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000134 setOperationAction(ISD::MUL, MVT::i8, Expand);
135 setOperationAction(ISD::MULHS, MVT::i8, Expand);
136 setOperationAction(ISD::MULHU, MVT::i8, Expand);
137 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
138 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000139 setOperationAction(ISD::MUL, MVT::i16, Expand);
140 setOperationAction(ISD::MULHS, MVT::i16, Expand);
141 setOperationAction(ISD::MULHU, MVT::i16, Expand);
142 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
143 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikoveb2152f2009-05-03 13:18:33 +0000144
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000145 setOperationAction(ISD::UDIV, MVT::i8, Expand);
146 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
147 setOperationAction(ISD::UREM, MVT::i8, Expand);
148 setOperationAction(ISD::SDIV, MVT::i8, Expand);
149 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
150 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000151 setOperationAction(ISD::UDIV, MVT::i16, Expand);
152 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
153 setOperationAction(ISD::UREM, MVT::i16, Expand);
154 setOperationAction(ISD::SDIV, MVT::i16, Expand);
155 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
156 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000157
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000158 // varargs support
159 setOperationAction(ISD::VASTART, MVT::Other, Custom);
160 setOperationAction(ISD::VAARG, MVT::Other, Expand);
161 setOperationAction(ISD::VAEND, MVT::Other, Expand);
162 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000163 setOperationAction(ISD::JumpTable, MVT::i16, Custom);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000164
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000165 // Libcalls names.
166 if (HWMultMode == HWMultIntr) {
167 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
168 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
169 } else if (HWMultMode == HWMultNoIntr) {
170 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
171 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
172 }
Eli Friedman2518f832011-05-06 20:34:06 +0000173
174 setMinFunctionAlignment(1);
175 setPrefFunctionAlignment(2);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000176}
177
Dan Gohman21cea8a2010-04-17 15:26:15 +0000178SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
179 SelectionDAG &DAG) const {
Anton Korobeynikov10138002009-05-03 12:57:15 +0000180 switch (Op.getOpcode()) {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000181 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000182 case ISD::SRL:
Anton Korobeynikov56135102009-05-03 13:07:31 +0000183 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000184 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000185 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000186 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000187 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000188 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
189 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000190 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000191 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
192 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000193 case ISD::VASTART: return LowerVASTART(Op, DAG);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000194 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000195 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000196 llvm_unreachable("unimplemented operand");
Anton Korobeynikov10138002009-05-03 12:57:15 +0000197 }
198}
199
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000200//===----------------------------------------------------------------------===//
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000201// MSP430 Inline Assembly Support
202//===----------------------------------------------------------------------===//
203
204/// getConstraintType - Given a constraint letter, return the type of
205/// constraint it is for this target.
206TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000207MSP430TargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000208 if (Constraint.size() == 1) {
209 switch (Constraint[0]) {
210 case 'r':
211 return C_RegisterClass;
212 default:
213 break;
214 }
215 }
216 return TargetLowering::getConstraintType(Constraint);
217}
218
Eric Christopher11e4df72015-02-26 22:38:43 +0000219std::pair<unsigned, const TargetRegisterClass *>
220MSP430TargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000221 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000222 if (Constraint.size() == 1) {
223 // GCC Constraint Letters
224 switch (Constraint[0]) {
225 default: break;
226 case 'r': // GENERAL_REGS
227 if (VT == MVT::i8)
Craig Topperc7242e02012-04-20 07:30:17 +0000228 return std::make_pair(0U, &MSP430::GR8RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000229
Craig Topperc7242e02012-04-20 07:30:17 +0000230 return std::make_pair(0U, &MSP430::GR16RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000231 }
232 }
233
Eric Christopher11e4df72015-02-26 22:38:43 +0000234 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000235}
236
237//===----------------------------------------------------------------------===//
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000238// Calling Convention Implementation
239//===----------------------------------------------------------------------===//
240
Anton Korobeynikov10138002009-05-03 12:57:15 +0000241#include "MSP430GenCallingConv.inc"
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000242
Job Noormane9a1d4c2013-10-15 08:19:39 +0000243/// For each argument in a function store the number of pieces it is composed
244/// of.
245template<typename ArgT>
246static void ParseFunctionArgs(const SmallVectorImpl<ArgT> &Args,
247 SmallVectorImpl<unsigned> &Out) {
248 unsigned CurrentArgIndex = ~0U;
249 for (unsigned i = 0, e = Args.size(); i != e; i++) {
250 if (CurrentArgIndex == Args[i].OrigArgIndex) {
251 Out.back()++;
252 } else {
253 Out.push_back(1);
254 CurrentArgIndex++;
255 }
256 }
257}
258
259static void AnalyzeVarArgs(CCState &State,
260 const SmallVectorImpl<ISD::OutputArg> &Outs) {
261 State.AnalyzeCallOperands(Outs, CC_MSP430_AssignStack);
262}
263
264static void AnalyzeVarArgs(CCState &State,
265 const SmallVectorImpl<ISD::InputArg> &Ins) {
266 State.AnalyzeFormalArguments(Ins, CC_MSP430_AssignStack);
267}
268
269/// Analyze incoming and outgoing function arguments. We need custom C++ code
270/// to handle special constraints in the ABI like reversing the order of the
271/// pieces of splitted arguments. In addition, all pieces of a certain argument
272/// have to be passed either using registers or the stack but never mixing both.
273template<typename ArgT>
274static void AnalyzeArguments(CCState &State,
275 SmallVectorImpl<CCValAssign> &ArgLocs,
276 const SmallVectorImpl<ArgT> &Args) {
Craig Topper840beec2014-04-04 05:16:06 +0000277 static const MCPhysReg RegList[] = {
Job Noormaneb19aea2014-09-10 06:58:14 +0000278 MSP430::R15, MSP430::R14, MSP430::R13, MSP430::R12
Job Noormane9a1d4c2013-10-15 08:19:39 +0000279 };
280 static const unsigned NbRegs = array_lengthof(RegList);
281
282 if (State.isVarArg()) {
283 AnalyzeVarArgs(State, Args);
284 return;
285 }
286
287 SmallVector<unsigned, 4> ArgsParts;
288 ParseFunctionArgs(Args, ArgsParts);
289
290 unsigned RegsLeft = NbRegs;
291 bool UseStack = false;
292 unsigned ValNo = 0;
293
294 for (unsigned i = 0, e = ArgsParts.size(); i != e; i++) {
295 MVT ArgVT = Args[ValNo].VT;
296 ISD::ArgFlagsTy ArgFlags = Args[ValNo].Flags;
297 MVT LocVT = ArgVT;
298 CCValAssign::LocInfo LocInfo = CCValAssign::Full;
299
300 // Promote i8 to i16
301 if (LocVT == MVT::i8) {
302 LocVT = MVT::i16;
303 if (ArgFlags.isSExt())
304 LocInfo = CCValAssign::SExt;
305 else if (ArgFlags.isZExt())
306 LocInfo = CCValAssign::ZExt;
307 else
308 LocInfo = CCValAssign::AExt;
309 }
310
311 // Handle byval arguments
312 if (ArgFlags.isByVal()) {
313 State.HandleByVal(ValNo++, ArgVT, LocVT, LocInfo, 2, 2, ArgFlags);
314 continue;
315 }
316
317 unsigned Parts = ArgsParts[i];
318
319 if (!UseStack && Parts <= RegsLeft) {
320 unsigned FirstVal = ValNo;
321 for (unsigned j = 0; j < Parts; j++) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000322 unsigned Reg = State.AllocateReg(RegList);
Job Noormane9a1d4c2013-10-15 08:19:39 +0000323 State.addLoc(CCValAssign::getReg(ValNo++, ArgVT, Reg, LocVT, LocInfo));
324 RegsLeft--;
325 }
326
327 // Reverse the order of the pieces to agree with the "big endian" format
328 // required in the calling convention ABI.
329 SmallVectorImpl<CCValAssign>::iterator B = ArgLocs.begin() + FirstVal;
330 std::reverse(B, B + Parts);
331 } else {
332 UseStack = true;
333 for (unsigned j = 0; j < Parts; j++)
334 CC_MSP430_AssignStack(ValNo++, ArgVT, LocVT, LocInfo, ArgFlags, State);
335 }
336 }
337}
338
339static void AnalyzeRetResult(CCState &State,
340 const SmallVectorImpl<ISD::InputArg> &Ins) {
341 State.AnalyzeCallResult(Ins, RetCC_MSP430);
342}
343
344static void AnalyzeRetResult(CCState &State,
345 const SmallVectorImpl<ISD::OutputArg> &Outs) {
346 State.AnalyzeReturn(Outs, RetCC_MSP430);
347}
348
349template<typename ArgT>
350static void AnalyzeReturnValues(CCState &State,
351 SmallVectorImpl<CCValAssign> &RVLocs,
352 const SmallVectorImpl<ArgT> &Args) {
353 AnalyzeRetResult(State, Args);
354
355 // Reverse splitted return values to get the "big endian" format required
356 // to agree with the calling convention ABI.
357 std::reverse(RVLocs.begin(), RVLocs.end());
358}
359
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000360SDValue MSP430TargetLowering::LowerFormalArguments(
361 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
362 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
363 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000364
365 switch (CallConv) {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000366 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000367 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000368 case CallingConv::C:
369 case CallingConv::Fast:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000370 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000371 case CallingConv::MSP430_INTR:
David Blaikie46a9f012012-01-20 21:51:11 +0000372 if (Ins.empty())
373 return Chain;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000374 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000375 }
376}
377
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000378SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000379MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000380 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000381 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000382 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000383 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
384 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
385 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000386 SDValue Chain = CLI.Chain;
387 SDValue Callee = CLI.Callee;
388 bool &isTailCall = CLI.IsTailCall;
389 CallingConv::ID CallConv = CLI.CallConv;
390 bool isVarArg = CLI.IsVarArg;
391
Evan Cheng67a69dd2010-01-27 00:07:07 +0000392 // MSP430 target does not yet support tail call optimization.
393 isTailCall = false;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000394
395 switch (CallConv) {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000396 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000397 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000398 case CallingConv::Fast:
399 case CallingConv::C:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000400 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000401 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000402 case CallingConv::MSP430_INTR:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000403 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000404 }
405}
406
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000407/// LowerCCCArguments - transform physical registers into virtual registers and
408/// generate load operations for arguments places on the stack.
409// FIXME: struct return stuff
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000410SDValue MSP430TargetLowering::LowerCCCArguments(
411 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
412 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
413 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000414 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000415 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000416 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000417 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000418
419 // Assign locations to all of the incoming arguments.
420 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000421 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
422 *DAG.getContext());
Job Noormane9a1d4c2013-10-15 08:19:39 +0000423 AnalyzeArguments(CCInfo, ArgLocs, Ins);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000424
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000425 // Create frame index for the start of the first vararg value
426 if (isVarArg) {
427 unsigned Offset = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000428 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, Offset, true));
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000429 }
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000430
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000431 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
432 CCValAssign &VA = ArgLocs[i];
433 if (VA.isRegLoc()) {
434 // Arguments passed in registers
Owen Anderson53aa7a92009-08-10 22:56:29 +0000435 EVT RegVT = VA.getLocVT();
Owen Anderson9f944592009-08-11 20:47:22 +0000436 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Andersonb2c80da2011-02-25 21:41:48 +0000437 default:
Torok Edwinfa040022009-07-08 19:04:27 +0000438 {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000439#ifndef NDEBUG
Chris Lattner317dbbc2009-08-23 07:05:07 +0000440 errs() << "LowerFormalArguments Unhandled argument type: "
Craig Topper6ff46262016-04-17 17:37:33 +0000441 << RegVT.getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000442#endif
Craig Toppere73658d2014-04-28 04:05:08 +0000443 llvm_unreachable(nullptr);
Torok Edwinfa040022009-07-08 19:04:27 +0000444 }
Owen Anderson9f944592009-08-11 20:47:22 +0000445 case MVT::i16:
Craig Topperc7242e02012-04-20 07:30:17 +0000446 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000447 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000448 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000449
450 // If this is an 8-bit value, it is really passed promoted to 16
451 // bits. Insert an assert[sz]ext to capture this, then truncate to the
452 // right size.
453 if (VA.getLocInfo() == CCValAssign::SExt)
454 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
455 DAG.getValueType(VA.getValVT()));
456 else if (VA.getLocInfo() == CCValAssign::ZExt)
457 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
458 DAG.getValueType(VA.getValVT()));
459
460 if (VA.getLocInfo() != CCValAssign::Full)
461 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
462
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000463 InVals.push_back(ArgValue);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000464 }
465 } else {
466 // Sanity check
467 assert(VA.isMemLoc());
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000468
Anton Korobeynikov34148722012-11-21 17:23:03 +0000469 SDValue InVal;
470 ISD::ArgFlagsTy Flags = Ins[i].Flags;
471
472 if (Flags.isByVal()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000473 int FI = MFI.CreateFixedObject(Flags.getByValSize(),
474 VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000475 InVal = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Anton Korobeynikov34148722012-11-21 17:23:03 +0000476 } else {
477 // Load the argument to a virtual register
478 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
479 if (ObjSize > 2) {
480 errs() << "LowerFormalArguments Unhandled argument type: "
481 << EVT(VA.getLocVT()).getEVTString()
482 << "\n";
483 }
484 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +0000485 int FI = MFI.CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
Anton Korobeynikov34148722012-11-21 17:23:03 +0000486
487 // Create the SelectionDAG nodes corresponding to a load
488 //from this parameter
489 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
Alex Lorenze40c8a22015-08-11 23:09:45 +0000490 InVal = DAG.getLoad(
491 VA.getLocVT(), dl, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +0000492 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Anton Korobeynikov34148722012-11-21 17:23:03 +0000493 }
494
495 InVals.push_back(InVal);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000496 }
497 }
498
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000499 return Chain;
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000500}
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000501
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000502SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000503MSP430TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
504 bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000505 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000506 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000507 const SDLoc &dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000508
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000509 // CCValAssign - represent the assignment of the return value to a location
510 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000511
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000512 // ISRs cannot return any value.
David Blaikie46a9f012012-01-20 21:51:11 +0000513 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
Chris Lattner2104b8d2010-04-07 22:58:41 +0000514 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000515
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000516 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000517 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
518 *DAG.getContext());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000519
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000520 // Analize return values.
Job Noormane9a1d4c2013-10-15 08:19:39 +0000521 AnalyzeReturnValues(CCInfo, RVLocs, Outs);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000522
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000523 SDValue Flag;
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000524 SmallVector<SDValue, 4> RetOps(1, Chain);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000525
526 // Copy the result values into the output registers.
527 for (unsigned i = 0; i != RVLocs.size(); ++i) {
528 CCValAssign &VA = RVLocs[i];
529 assert(VA.isRegLoc() && "Can only return in registers!");
530
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000531 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000532 OutVals[i], Flag);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000533
Anton Korobeynikovc10f98a2009-05-03 13:00:11 +0000534 // Guarantee that all emitted copies are stuck together,
535 // avoiding something bad.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000536 Flag = Chain.getValue(1);
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000537 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000538 }
539
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000540 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
541 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
542
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000543 RetOps[0] = Chain; // Update chain.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000544
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000545 // Add the flag if we have it.
546 if (Flag.getNode())
547 RetOps.push_back(Flag);
548
Craig Topper48d114b2014-04-26 18:35:24 +0000549 return DAG.getNode(Opc, dl, MVT::Other, RetOps);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000550}
551
Anton Korobeynikov56135102009-05-03 13:07:31 +0000552/// LowerCCCCallTo - functions arguments are copied from virtual regs to
553/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Job Noormana928e1d2013-07-15 14:25:26 +0000554// TODO: sret.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000555SDValue MSP430TargetLowering::LowerCCCCallTo(
556 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg,
557 bool isTailCall, const SmallVectorImpl<ISD::OutputArg> &Outs,
558 const SmallVectorImpl<SDValue> &OutVals,
559 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
560 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000561 // Analyze operands of the call, assigning locations to each operand.
562 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000563 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
564 *DAG.getContext());
Job Noormane9a1d4c2013-10-15 08:19:39 +0000565 AnalyzeArguments(CCInfo, ArgLocs, Outs);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000566
567 // Get a count of how many bytes are to be pushed on the stack.
568 unsigned NumBytes = CCInfo.getNextStackOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +0000569 auto PtrVT = getPointerTy(DAG.getDataLayout());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000570
Mehdi Amini44ede332015-07-09 02:09:04 +0000571 Chain = DAG.getCALLSEQ_START(Chain,
572 DAG.getConstant(NumBytes, dl, PtrVT, true), dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000573
574 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
575 SmallVector<SDValue, 12> MemOpChains;
576 SDValue StackPtr;
577
578 // Walk the register/memloc assignments, inserting copies/loads.
579 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
580 CCValAssign &VA = ArgLocs[i];
581
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000582 SDValue Arg = OutVals[i];
Anton Korobeynikov56135102009-05-03 13:07:31 +0000583
584 // Promote the value if needed.
585 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000586 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000587 case CCValAssign::Full: break;
588 case CCValAssign::SExt:
589 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
590 break;
591 case CCValAssign::ZExt:
592 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
593 break;
594 case CCValAssign::AExt:
595 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
596 break;
597 }
598
599 // Arguments that can be passed on register must be kept at RegsToPass
600 // vector
601 if (VA.isRegLoc()) {
602 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
603 } else {
604 assert(VA.isMemLoc());
605
Craig Topper062a2ba2014-04-25 05:30:21 +0000606 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +0000607 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SP, PtrVT);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000608
Mehdi Amini44ede332015-07-09 02:09:04 +0000609 SDValue PtrOff =
610 DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
611 DAG.getIntPtrConstant(VA.getLocMemOffset(), dl));
Anton Korobeynikov56135102009-05-03 13:07:31 +0000612
Anton Korobeynikov34148722012-11-21 17:23:03 +0000613 SDValue MemOp;
614 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000615
Anton Korobeynikov34148722012-11-21 17:23:03 +0000616 if (Flags.isByVal()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000617 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i16);
Anton Korobeynikov34148722012-11-21 17:23:03 +0000618 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
619 Flags.getByValAlign(),
620 /*isVolatile*/false,
621 /*AlwaysInline=*/true,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000622 /*isTailCall=*/false,
Anton Korobeynikov34148722012-11-21 17:23:03 +0000623 MachinePointerInfo(),
624 MachinePointerInfo());
625 } else {
Justin Lebar9c375812016-07-15 18:27:10 +0000626 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
Anton Korobeynikov34148722012-11-21 17:23:03 +0000627 }
628
629 MemOpChains.push_back(MemOp);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000630 }
631 }
632
633 // Transform all store nodes into one single node because all store nodes are
634 // independent of each other.
635 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000636 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000637
638 // Build a sequence of copy-to-reg nodes chained together with token chain and
639 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000640 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov56135102009-05-03 13:07:31 +0000641 SDValue InFlag;
642 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
643 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
644 RegsToPass[i].second, InFlag);
645 InFlag = Chain.getValue(1);
646 }
647
648 // If the callee is a GlobalAddress node (quite common, every direct call is)
649 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
650 // Likewise ExternalSymbol -> TargetExternalSymbol.
651 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patela3ca21b2010-07-06 22:08:15 +0000652 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000653 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson9f944592009-08-11 20:47:22 +0000654 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000655
656 // Returns a chain & a flag for retval copy to use.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000657 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000658 SmallVector<SDValue, 8> Ops;
659 Ops.push_back(Chain);
660 Ops.push_back(Callee);
661
662 // Add argument registers to the end of the list so that they are
663 // known live into the call.
664 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
665 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
666 RegsToPass[i].second.getValueType()));
667
668 if (InFlag.getNode())
669 Ops.push_back(InFlag);
670
Craig Topper48d114b2014-04-26 18:35:24 +0000671 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, Ops);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000672 InFlag = Chain.getValue(1);
673
674 // Create the CALLSEQ_END node.
Mehdi Amini44ede332015-07-09 02:09:04 +0000675 Chain = DAG.getCALLSEQ_END(Chain, DAG.getConstant(NumBytes, dl, PtrVT, true),
676 DAG.getConstant(0, dl, PtrVT, true), InFlag, dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000677 InFlag = Chain.getValue(1);
678
679 // Handle result values, copying them out of physregs into vregs that we
680 // return.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000681 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
682 DAG, InVals);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000683}
684
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000685/// LowerCallResult - Lower the result values of a call into the
686/// appropriate copies out of appropriate physical registers.
687///
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000688SDValue MSP430TargetLowering::LowerCallResult(
689 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
690 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
691 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000692
693 // Assign locations to each value returned by this call.
694 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000695 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
696 *DAG.getContext());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000697
Job Noormane9a1d4c2013-10-15 08:19:39 +0000698 AnalyzeReturnValues(CCInfo, RVLocs, Ins);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000699
700 // Copy all of the result registers out of their specified physreg.
701 for (unsigned i = 0; i != RVLocs.size(); ++i) {
702 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
703 RVLocs[i].getValVT(), InFlag).getValue(1);
704 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000705 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov56135102009-05-03 13:07:31 +0000706 }
707
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000708 return Chain;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000709}
710
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000711SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000712 SelectionDAG &DAG) const {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000713 unsigned Opc = Op.getOpcode();
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000714 SDNode* N = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000715 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000716 SDLoc dl(N);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000717
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000718 // Expand non-constant shifts to loops:
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000719 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000720 switch (Opc) {
Craig Toppere55c5562012-02-07 02:50:20 +0000721 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000722 case ISD::SHL:
723 return DAG.getNode(MSP430ISD::SHL, dl,
724 VT, N->getOperand(0), N->getOperand(1));
725 case ISD::SRA:
726 return DAG.getNode(MSP430ISD::SRA, dl,
727 VT, N->getOperand(0), N->getOperand(1));
728 case ISD::SRL:
729 return DAG.getNode(MSP430ISD::SRL, dl,
730 VT, N->getOperand(0), N->getOperand(1));
731 }
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000732
733 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
734
735 // Expand the stuff into sequence of shifts.
736 // FIXME: for some shift amounts this might be done better!
737 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
738 SDValue Victim = N->getOperand(0);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000739
740 if (Opc == ISD::SRL && ShiftAmount) {
741 // Emit a special goodness here:
742 // srl A, 1 => clrc; rrc A
Anton Korobeynikovf3a6bc82009-05-03 13:16:37 +0000743 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000744 ShiftAmount -= 1;
745 }
746
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000747 while (ShiftAmount--)
Anton Korobeynikov6b5523a2009-05-17 10:15:22 +0000748 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000749 dl, VT, Victim);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000750
751 return Victim;
752}
753
Dan Gohman21cea8a2010-04-17 15:26:15 +0000754SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
755 SelectionDAG &DAG) const {
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000756 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
757 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +0000758 auto PtrVT = getPointerTy(DAG.getDataLayout());
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000759
760 // Create the TargetGlobalAddress node, folding in the constant offset.
Mehdi Amini44ede332015-07-09 02:09:04 +0000761 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op), PtrVT, Offset);
762 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op), PtrVT, Result);
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000763}
764
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000765SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000766 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000767 SDLoc dl(Op);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000768 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Mehdi Amini44ede332015-07-09 02:09:04 +0000769 auto PtrVT = getPointerTy(DAG.getDataLayout());
770 SDValue Result = DAG.getTargetExternalSymbol(Sym, PtrVT);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000771
Mehdi Amini44ede332015-07-09 02:09:04 +0000772 return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000773}
774
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000775SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
776 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000777 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +0000778 auto PtrVT = getPointerTy(DAG.getDataLayout());
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000779 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Mehdi Amini44ede332015-07-09 02:09:04 +0000780 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000781
Mehdi Amini44ede332015-07-09 02:09:04 +0000782 return DAG.getNode(MSP430ISD::Wrapper, dl, PtrVT, Result);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000783}
784
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000785static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000786 ISD::CondCode CC, const SDLoc &dl, SelectionDAG &DAG) {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000787 // FIXME: Handle bittests someday
788 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
789
790 // FIXME: Handle jump negative someday
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000791 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000792 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000793 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov96272012009-05-03 13:12:06 +0000794 case ISD::SETEQ:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000795 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000796 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000797 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000798 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000799 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000800 break;
801 case ISD::SETNE:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000802 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000803 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000804 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000805 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000806 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000807 break;
808 case ISD::SETULE:
Justin Bognerb03fd122016-08-17 05:10:15 +0000809 std::swap(LHS, RHS);
810 LLVM_FALLTHROUGH;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000811 case ISD::SETUGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000812 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
813 // fold constant into instruction.
814 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
815 LHS = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000816 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000817 TCC = MSP430CC::COND_LO;
818 break;
819 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000820 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikov96272012009-05-03 13:12:06 +0000821 break;
822 case ISD::SETUGT:
Justin Bognerb03fd122016-08-17 05:10:15 +0000823 std::swap(LHS, RHS);
824 LLVM_FALLTHROUGH;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000825 case ISD::SETULT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000826 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
827 // fold constant into instruction.
828 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
829 LHS = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000830 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000831 TCC = MSP430CC::COND_HS;
832 break;
833 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000834 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikov96272012009-05-03 13:12:06 +0000835 break;
836 case ISD::SETLE:
Justin Bognerb03fd122016-08-17 05:10:15 +0000837 std::swap(LHS, RHS);
838 LLVM_FALLTHROUGH;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000839 case ISD::SETGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000840 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
841 // fold constant into instruction.
842 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
843 LHS = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000844 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000845 TCC = MSP430CC::COND_L;
846 break;
847 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000848 TCC = MSP430CC::COND_GE;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000849 break;
850 case ISD::SETGT:
Justin Bognerb03fd122016-08-17 05:10:15 +0000851 std::swap(LHS, RHS);
852 LLVM_FALLTHROUGH;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000853 case ISD::SETLT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000854 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
855 // fold constant into instruction.
856 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
857 LHS = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000858 RHS = DAG.getConstant(C->getSExtValue() + 1, dl, C->getValueType(0));
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000859 TCC = MSP430CC::COND_GE;
860 break;
861 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000862 TCC = MSP430CC::COND_L;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000863 break;
864 }
865
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000866 TargetCC = DAG.getConstant(TCC, dl, MVT::i8);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000867 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000868}
869
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000870
Dan Gohman21cea8a2010-04-17 15:26:15 +0000871SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000872 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000873 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
874 SDValue LHS = Op.getOperand(2);
875 SDValue RHS = Op.getOperand(3);
876 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000877 SDLoc dl (Op);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000878
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000879 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000880 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000881
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000882 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000883 Chain, Dest, TargetCC, Flag);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000884}
885
Dan Gohman21cea8a2010-04-17 15:26:15 +0000886SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000887 SDValue LHS = Op.getOperand(0);
888 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000889 SDLoc dl (Op);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000890
891 // If we are doing an AND and testing against zero, then the CMP
892 // will not be generated. The AND (or BIT) will generate the condition codes,
893 // but they are different from CMP.
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000894 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
895 // lowering & isel wouldn't diverge.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000896 bool andCC = false;
897 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
898 if (RHSC->isNullValue() && LHS.hasOneUse() &&
899 (LHS.getOpcode() == ISD::AND ||
900 (LHS.getOpcode() == ISD::TRUNCATE &&
901 LHS.getOperand(0).getOpcode() == ISD::AND))) {
902 andCC = true;
903 }
904 }
905 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
906 SDValue TargetCC;
907 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
908
909 // Get the condition codes directly from the status register, if its easy.
910 // Otherwise a branch will be generated. Note that the AND and BIT
911 // instructions generate different flags than CMP, the carry bit can be used
912 // for NE/EQ.
913 bool Invert = false;
914 bool Shift = false;
915 bool Convert = true;
916 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
917 default:
918 Convert = false;
919 break;
920 case MSP430CC::COND_HS:
Job Noormaneb19aea2014-09-10 06:58:14 +0000921 // Res = SR & 1, no processing is required
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000922 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000923 case MSP430CC::COND_LO:
Job Noormaneb19aea2014-09-10 06:58:14 +0000924 // Res = ~(SR & 1)
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000925 Invert = true;
926 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000927 case MSP430CC::COND_NE:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000928 if (andCC) {
Job Noormaneb19aea2014-09-10 06:58:14 +0000929 // C = ~Z, thus Res = SR & 1, no processing is required
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000930 } else {
Job Noormaneb19aea2014-09-10 06:58:14 +0000931 // Res = ~((SR >> 1) & 1)
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000932 Shift = true;
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000933 Invert = true;
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000934 }
935 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000936 case MSP430CC::COND_E:
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000937 Shift = true;
Job Noormaneb19aea2014-09-10 06:58:14 +0000938 // C = ~Z for AND instruction, thus we can put Res = ~(SR & 1), however,
939 // Res = (SR >> 1) & 1 is 1 word shorter.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000940 break;
941 }
942 EVT VT = Op.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000943 SDValue One = DAG.getConstant(1, dl, VT);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000944 if (Convert) {
Job Noormaneb19aea2014-09-10 06:58:14 +0000945 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SR,
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000946 MVT::i16, Flag);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000947 if (Shift)
948 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
949 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
950 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
951 if (Invert)
952 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
953 return SR;
954 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000955 SDValue Zero = DAG.getConstant(0, dl, VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000956 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Benjamin Kramerea68a942015-02-19 15:26:17 +0000957 SDValue Ops[] = {One, Zero, TargetCC, Flag};
Craig Topper48d114b2014-04-26 18:35:24 +0000958 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000959 }
960}
961
Dan Gohman21cea8a2010-04-17 15:26:15 +0000962SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
963 SelectionDAG &DAG) const {
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000964 SDValue LHS = Op.getOperand(0);
965 SDValue RHS = Op.getOperand(1);
966 SDValue TrueV = Op.getOperand(2);
967 SDValue FalseV = Op.getOperand(3);
968 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000969 SDLoc dl (Op);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000970
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000971 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000972 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000973
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000974 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Benjamin Kramerea68a942015-02-19 15:26:17 +0000975 SDValue Ops[] = {TrueV, FalseV, TargetCC, Flag};
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000976
Craig Topper48d114b2014-04-26 18:35:24 +0000977 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, Ops);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000978}
979
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000980SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000981 SelectionDAG &DAG) const {
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000982 SDValue Val = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000983 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000984 SDLoc dl(Op);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000985
Owen Anderson9f944592009-08-11 20:47:22 +0000986 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000987
988 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
989 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
990 DAG.getValueType(Val.getValueType()));
991}
992
Dan Gohman21cea8a2010-04-17 15:26:15 +0000993SDValue
994MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000995 MachineFunction &MF = DAG.getMachineFunction();
996 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
997 int ReturnAddrIndex = FuncInfo->getRAIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000998 auto PtrVT = getPointerTy(MF.getDataLayout());
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000999
1000 if (ReturnAddrIndex == 0) {
1001 // Set up a frame object for the return address.
Mehdi Amini44ede332015-07-09 02:09:04 +00001002 uint64_t SlotSize = MF.getDataLayout().getPointerSize();
Matthias Braun941a7052016-07-28 18:40:00 +00001003 ReturnAddrIndex = MF.getFrameInfo().CreateFixedObject(SlotSize, -SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00001004 true);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001005 FuncInfo->setRAIndex(ReturnAddrIndex);
1006 }
1007
Mehdi Amini44ede332015-07-09 02:09:04 +00001008 return DAG.getFrameIndex(ReturnAddrIndex, PtrVT);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001009}
1010
Dan Gohman21cea8a2010-04-17 15:26:15 +00001011SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
1012 SelectionDAG &DAG) const {
Matthias Braun941a7052016-07-28 18:40:00 +00001013 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
1014 MFI.setReturnAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00001015
Bill Wendling908bf812014-01-06 00:43:20 +00001016 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001017 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001018
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001019 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001020 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001021 auto PtrVT = getPointerTy(DAG.getDataLayout());
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001022
1023 if (Depth > 0) {
1024 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1025 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00001026 DAG.getConstant(DAG.getDataLayout().getPointerSize(), dl, MVT::i16);
1027 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
1028 DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00001029 MachinePointerInfo());
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001030 }
1031
1032 // Just load the return address.
1033 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00001034 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI,
Justin Lebar9c375812016-07-15 18:27:10 +00001035 MachinePointerInfo());
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001036}
1037
Dan Gohman21cea8a2010-04-17 15:26:15 +00001038SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
1039 SelectionDAG &DAG) const {
Matthias Braun941a7052016-07-28 18:40:00 +00001040 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
1041 MFI.setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00001042
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001043 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001044 SDLoc dl(Op); // FIXME probably not meaningful
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001045 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1046 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Job Noormaneb19aea2014-09-10 06:58:14 +00001047 MSP430::FP, VT);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001048 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00001049 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001050 MachinePointerInfo());
Anton Korobeynikovff4ab512009-12-07 02:28:10 +00001051 return FrameAddr;
1052}
1053
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001054SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
1055 SelectionDAG &DAG) const {
1056 MachineFunction &MF = DAG.getMachineFunction();
1057 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001058 auto PtrVT = getPointerTy(DAG.getDataLayout());
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001059
1060 // Frame index of first vararg argument
Mehdi Amini44ede332015-07-09 02:09:04 +00001061 SDValue FrameIndex =
1062 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001063 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1064
1065 // Create a store of the frame index to the location operand
Justin Lebar9c375812016-07-15 18:27:10 +00001066 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex, Op.getOperand(1),
1067 MachinePointerInfo(SV));
Anton Korobeynikov568afeb2012-11-21 17:28:27 +00001068}
1069
Anton Korobeynikov82bedb12013-07-01 19:44:44 +00001070SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op,
1071 SelectionDAG &DAG) const {
1072 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001073 auto PtrVT = getPointerTy(DAG.getDataLayout());
1074 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1075 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT), PtrVT, Result);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +00001076}
1077
Anton Korobeynikovd3c83192009-11-07 17:15:06 +00001078/// getPostIndexedAddressParts - returns true by value, base pointer and
1079/// offset pointer and addressing mode by reference if this node can be
1080/// combined with a load / store to form a post-indexed load / store.
1081bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1082 SDValue &Base,
1083 SDValue &Offset,
1084 ISD::MemIndexedMode &AM,
1085 SelectionDAG &DAG) const {
1086
1087 LoadSDNode *LD = cast<LoadSDNode>(N);
1088 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
1089 return false;
1090
1091 EVT VT = LD->getMemoryVT();
1092 if (VT != MVT::i8 && VT != MVT::i16)
1093 return false;
1094
1095 if (Op->getOpcode() != ISD::ADD)
1096 return false;
1097
1098 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1099 uint64_t RHSC = RHS->getZExtValue();
1100 if ((VT == MVT::i16 && RHSC != 2) ||
1101 (VT == MVT::i8 && RHSC != 1))
1102 return false;
1103
1104 Base = Op->getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001105 Offset = DAG.getConstant(RHSC, SDLoc(N), VT);
Anton Korobeynikovd3c83192009-11-07 17:15:06 +00001106 AM = ISD::POST_INC;
1107 return true;
1108 }
1109
1110 return false;
1111}
1112
1113
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001114const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001115 switch ((MSP430ISD::NodeType)Opcode) {
1116 case MSP430ISD::FIRST_NUMBER: break;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001117 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov24a63162009-12-07 02:28:41 +00001118 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikov15a515b2009-05-03 13:03:33 +00001119 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikov61763b52009-05-03 13:16:17 +00001120 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1121 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovec3f0b32009-05-03 13:07:54 +00001122 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikovcfc97052009-05-03 13:08:33 +00001123 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001124 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikov96272012009-05-03 13:12:06 +00001125 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Matthias Braund04893f2015-05-07 21:33:59 +00001126 case MSP430ISD::SETCC: return "MSP430ISD::SETCC";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001127 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001128 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1129 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Matthias Braund04893f2015-05-07 21:33:59 +00001130 case MSP430ISD::SRL: return "MSP430ISD::SRL";
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001131 }
Matthias Braund04893f2015-05-07 21:33:59 +00001132 return nullptr;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001133}
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001134
Chris Lattner229907c2011-07-18 04:54:35 +00001135bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1136 Type *Ty2) const {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001137 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001138 return false;
1139
1140 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1141}
1142
1143bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1144 if (!VT1.isInteger() || !VT2.isInteger())
1145 return false;
1146
1147 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1148}
1149
Chris Lattner229907c2011-07-18 04:54:35 +00001150bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001151 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001152 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001153}
1154
1155bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1156 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1157 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1158}
1159
Eli Bendersky39e7c6e2012-12-18 18:21:29 +00001160bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1161 return isZExtFree(Val.getValueType(), VT2);
1162}
1163
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001164//===----------------------------------------------------------------------===//
1165// Other Lowering Code
1166//===----------------------------------------------------------------------===//
1167
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001168MachineBasicBlock *
1169MSP430TargetLowering::EmitShiftInstr(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001170 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001171 MachineFunction *F = BB->getParent();
1172 MachineRegisterInfo &RI = F->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001173 DebugLoc dl = MI.getDebugLoc();
Eric Christopherfbd9fba2015-01-29 23:46:42 +00001174 const TargetInstrInfo &TII = *F->getSubtarget().getInstrInfo();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001175
1176 unsigned Opc;
1177 const TargetRegisterClass * RC;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001178 switch (MI.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00001179 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001180 case MSP430::Shl8:
1181 Opc = MSP430::SHL8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001182 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001183 break;
1184 case MSP430::Shl16:
1185 Opc = MSP430::SHL16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001186 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001187 break;
1188 case MSP430::Sra8:
1189 Opc = MSP430::SAR8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001190 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001191 break;
1192 case MSP430::Sra16:
1193 Opc = MSP430::SAR16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001194 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001195 break;
1196 case MSP430::Srl8:
1197 Opc = MSP430::SAR8r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001198 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001199 break;
1200 case MSP430::Srl16:
1201 Opc = MSP430::SAR16r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001202 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001203 break;
1204 }
1205
1206 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc4829de2015-10-20 01:18:39 +00001207 MachineFunction::iterator I = ++BB->getIterator();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001208
1209 // Create loop block
1210 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1211 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1212
1213 F->insert(I, LoopBB);
1214 F->insert(I, RemBB);
1215
1216 // Update machine-CFG edges by transferring all successors of the current
1217 // block to the block containing instructions after shift.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001218 RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00001219 BB->end());
1220 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001221
Vadzim Dambrouskicc33fc82017-02-28 08:27:43 +00001222 // Add edges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001223 BB->addSuccessor(LoopBB);
1224 BB->addSuccessor(RemBB);
1225 LoopBB->addSuccessor(RemBB);
1226 LoopBB->addSuccessor(LoopBB);
1227
Craig Topperc7242e02012-04-20 07:30:17 +00001228 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1229 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001230 unsigned ShiftReg = RI.createVirtualRegister(RC);
1231 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001232 unsigned ShiftAmtSrcReg = MI.getOperand(2).getReg();
1233 unsigned SrcReg = MI.getOperand(1).getReg();
1234 unsigned DstReg = MI.getOperand(0).getReg();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001235
1236 // BB:
1237 // cmp 0, N
1238 // je RemBB
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +00001239 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1240 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001241 BuildMI(BB, dl, TII.get(MSP430::JCC))
1242 .addMBB(RemBB)
1243 .addImm(MSP430CC::COND_E);
1244
1245 // LoopBB:
1246 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1247 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1248 // ShiftReg2 = shift ShiftReg
1249 // ShiftAmt2 = ShiftAmt - 1;
1250 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1251 .addReg(SrcReg).addMBB(BB)
1252 .addReg(ShiftReg2).addMBB(LoopBB);
1253 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1254 .addReg(ShiftAmtSrcReg).addMBB(BB)
1255 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1256 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1257 .addReg(ShiftReg);
1258 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1259 .addReg(ShiftAmtReg).addImm(1);
1260 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1261 .addMBB(LoopBB)
1262 .addImm(MSP430CC::COND_NE);
1263
1264 // RemBB:
1265 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman34396292010-07-06 20:24:04 +00001266 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001267 .addReg(SrcReg).addMBB(BB)
1268 .addReg(ShiftReg2).addMBB(LoopBB);
1269
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001270 MI.eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001271 return RemBB;
1272}
1273
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001274MachineBasicBlock *
1275MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001276 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001277 unsigned Opc = MI.getOpcode();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001278
1279 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1280 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1281 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohman25c16532010-05-01 00:01:06 +00001282 return EmitShiftInstr(MI, BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001283
Eric Christopherfbd9fba2015-01-29 23:46:42 +00001284 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001285 DebugLoc dl = MI.getDebugLoc();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001286
1287 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001288 "Unexpected instr type to insert");
1289
1290 // To "insert" a SELECT instruction, we actually have to insert the diamond
1291 // control-flow pattern. The incoming instruction knows the destination vreg
1292 // to set, the condition code register to branch on, the true/false values to
1293 // select between, and a branch opcode to use.
1294 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc4829de2015-10-20 01:18:39 +00001295 MachineFunction::iterator I = ++BB->getIterator();
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001296
1297 // thisMBB:
1298 // ...
1299 // TrueVal = ...
1300 // cmpTY ccX, r1, r2
1301 // jCC copy1MBB
1302 // fallthrough --> copy0MBB
1303 MachineBasicBlock *thisMBB = BB;
1304 MachineFunction *F = BB->getParent();
1305 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1306 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001307 F->insert(I, copy0MBB);
1308 F->insert(I, copy1MBB);
1309 // Update machine-CFG edges by transferring all successors of the current
1310 // block to the new block which will contain the Phi node for the select.
Dan Gohman34396292010-07-06 20:24:04 +00001311 copy1MBB->splice(copy1MBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001312 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00001313 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001314 // Next, add the true and fallthrough blocks as its successors.
1315 BB->addSuccessor(copy0MBB);
1316 BB->addSuccessor(copy1MBB);
1317
Dan Gohman34396292010-07-06 20:24:04 +00001318 BuildMI(BB, dl, TII.get(MSP430::JCC))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001319 .addMBB(copy1MBB)
1320 .addImm(MI.getOperand(3).getImm());
Dan Gohman34396292010-07-06 20:24:04 +00001321
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001322 // copy0MBB:
1323 // %FalseValue = ...
1324 // # fallthrough to copy1MBB
1325 BB = copy0MBB;
1326
1327 // Update machine-CFG edges
1328 BB->addSuccessor(copy1MBB);
1329
1330 // copy1MBB:
1331 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1332 // ...
1333 BB = copy1MBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001334 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI), MI.getOperand(0).getReg())
1335 .addReg(MI.getOperand(2).getReg())
1336 .addMBB(copy0MBB)
1337 .addReg(MI.getOperand(1).getReg())
1338 .addMBB(thisMBB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001339
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001340 MI.eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001341 return BB;
1342}