blob: 682eb2c7e69ae3adcfe3986c6a14e968ab139c16 [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"
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000035#include "llvm/Support/CommandLine.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000036#include "llvm/Support/Debug.h"
Torok Edwinfa040022009-07-08 19:04:27 +000037#include "llvm/Support/ErrorHandling.h"
Chris Lattner317dbbc2009-08-23 07:05:07 +000038#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000039using namespace llvm;
40
Anton Korobeynikov28d3c732009-12-07 02:27:08 +000041typedef enum {
42 NoHWMult,
43 HWMultIntr,
44 HWMultNoIntr
45} HWMultUseMode;
46
47static cl::opt<HWMultUseMode>
48HWMultMode("msp430-hwmult-mode",
49 cl::desc("Hardware multiplier use mode"),
50 cl::init(HWMultNoIntr),
51 cl::values(
52 clEnumValN(NoHWMult, "no",
53 "Do not use hardware multiplier"),
54 clEnumValN(HWMultIntr, "interrupts",
55 "Assume hardware multiplier can be used inside interrupts"),
56 clEnumValN(HWMultNoIntr, "use",
57 "Assume hardware multiplier cannot be used inside interrupts"),
58 clEnumValEnd));
59
Anton Korobeynikov10138002009-05-03 12:57:15 +000060MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
Chris Lattner5e693ed2009-07-28 03:13:23 +000061 TargetLowering(tm, new TargetLoweringObjectFileELF()),
Benjamin Kramer628a39f2012-06-06 18:25:08 +000062 Subtarget(*tm.getSubtargetImpl()) {
Anton Korobeynikov10138002009-05-03 12:57:15 +000063
Micah Villmowcdfe20b2012-10-08 16:38:25 +000064 TD = getDataLayout();
Anton Korobeynikovff4ab512009-12-07 02:28:10 +000065
Anton Korobeynikov10138002009-05-03 12:57:15 +000066 // Set up the register classes.
Craig Topperc7242e02012-04-20 07:30:17 +000067 addRegisterClass(MVT::i8, &MSP430::GR8RegClass);
68 addRegisterClass(MVT::i16, &MSP430::GR16RegClass);
Anton Korobeynikov10138002009-05-03 12:57:15 +000069
70 // Compute derived properties from the register classes
71 computeRegisterProperties();
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +000072
Anton Korobeynikov55a085b2009-05-03 13:03:14 +000073 // Provide all sorts of operation actions
74
75 // Division is expensive
76 setIntDivIsCheap(false);
77
Anton Korobeynikov7212c152009-05-03 13:11:35 +000078 setStackPointerRegisterToSaveRestore(MSP430::SPW);
79 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sandsf2641e12011-09-06 19:07:46 +000080 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Anton Korobeynikov7212c152009-05-03 13:11:35 +000081
Anton Korobeynikovcf84ab52009-11-07 17:15:25 +000082 // We have post-incremented loads / stores.
Anton Korobeynikovd3c83192009-11-07 17:15:06 +000083 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
84 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
85
86 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
87 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
88 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
89 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +000090 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Anton Korobeynikov31ecd232009-05-03 13:06:03 +000091
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000092 // We don't have any truncstores
Owen Anderson9f944592009-08-11 20:47:22 +000093 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikoved1c3df2009-05-03 13:06:26 +000094
Owen Anderson9f944592009-08-11 20:47:22 +000095 setOperationAction(ISD::SRA, MVT::i8, Custom);
96 setOperationAction(ISD::SHL, MVT::i8, Custom);
97 setOperationAction(ISD::SRL, MVT::i8, Custom);
98 setOperationAction(ISD::SRA, MVT::i16, Custom);
99 setOperationAction(ISD::SHL, MVT::i16, Custom);
100 setOperationAction(ISD::SRL, MVT::i16, Custom);
101 setOperationAction(ISD::ROTL, MVT::i8, Expand);
102 setOperationAction(ISD::ROTR, MVT::i8, Expand);
103 setOperationAction(ISD::ROTL, MVT::i16, Expand);
104 setOperationAction(ISD::ROTR, MVT::i16, Expand);
105 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
106 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000107 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000108 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000109 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
110 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
111 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000112 setOperationAction(ISD::SETCC, MVT::i8, Custom);
113 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000114 setOperationAction(ISD::SELECT, MVT::i8, Expand);
115 setOperationAction(ISD::SELECT, MVT::i16, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
117 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
118 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov271cdda2009-08-25 17:00:23 +0000119 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
120 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000121
Owen Anderson9f944592009-08-11 20:47:22 +0000122 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
123 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000124 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Expand);
125 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000126 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
127 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000128 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Expand);
129 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000130 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
131 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000132
Owen Anderson9f944592009-08-11 20:47:22 +0000133 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
134 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
135 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
136 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
137 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
138 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000139
Owen Anderson9f944592009-08-11 20:47:22 +0000140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedman6a60a66b2009-07-17 07:28:06 +0000141
Anton Korobeynikovde60d1c2009-05-03 13:14:25 +0000142 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000143 setOperationAction(ISD::MUL, MVT::i8, Expand);
144 setOperationAction(ISD::MULHS, MVT::i8, Expand);
145 setOperationAction(ISD::MULHU, MVT::i8, Expand);
146 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
147 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000148 setOperationAction(ISD::MUL, MVT::i16, Expand);
149 setOperationAction(ISD::MULHS, MVT::i16, Expand);
150 setOperationAction(ISD::MULHU, MVT::i16, Expand);
151 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
152 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikoveb2152f2009-05-03 13:18:33 +0000153
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000154 setOperationAction(ISD::UDIV, MVT::i8, Expand);
155 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
156 setOperationAction(ISD::UREM, MVT::i8, Expand);
157 setOperationAction(ISD::SDIV, MVT::i8, Expand);
158 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
159 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000160 setOperationAction(ISD::UDIV, MVT::i16, Expand);
161 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
162 setOperationAction(ISD::UREM, MVT::i16, Expand);
163 setOperationAction(ISD::SDIV, MVT::i16, Expand);
164 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
165 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000166
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000167 // varargs support
168 setOperationAction(ISD::VASTART, MVT::Other, Custom);
169 setOperationAction(ISD::VAARG, MVT::Other, Expand);
170 setOperationAction(ISD::VAEND, MVT::Other, Expand);
171 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000172 setOperationAction(ISD::JumpTable, MVT::i16, Custom);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000173
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000174 // Libcalls names.
175 if (HWMultMode == HWMultIntr) {
176 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
177 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
178 } else if (HWMultMode == HWMultNoIntr) {
179 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
180 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
181 }
Eli Friedman2518f832011-05-06 20:34:06 +0000182
183 setMinFunctionAlignment(1);
184 setPrefFunctionAlignment(2);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000185}
186
Dan Gohman21cea8a2010-04-17 15:26:15 +0000187SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
188 SelectionDAG &DAG) const {
Anton Korobeynikov10138002009-05-03 12:57:15 +0000189 switch (Op.getOpcode()) {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000190 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000191 case ISD::SRL:
Anton Korobeynikov56135102009-05-03 13:07:31 +0000192 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000193 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000194 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000195 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000196 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000197 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
198 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000199 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000200 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
201 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000202 case ISD::VASTART: return LowerVASTART(Op, DAG);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000203 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000204 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000205 llvm_unreachable("unimplemented operand");
Anton Korobeynikov10138002009-05-03 12:57:15 +0000206 }
207}
208
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000209//===----------------------------------------------------------------------===//
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000210// MSP430 Inline Assembly Support
211//===----------------------------------------------------------------------===//
212
213/// getConstraintType - Given a constraint letter, return the type of
214/// constraint it is for this target.
215TargetLowering::ConstraintType
216MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
217 if (Constraint.size() == 1) {
218 switch (Constraint[0]) {
219 case 'r':
220 return C_RegisterClass;
221 default:
222 break;
223 }
224 }
225 return TargetLowering::getConstraintType(Constraint);
226}
227
228std::pair<unsigned, const TargetRegisterClass*>
229MSP430TargetLowering::
230getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +0000231 MVT VT) const {
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000232 if (Constraint.size() == 1) {
233 // GCC Constraint Letters
234 switch (Constraint[0]) {
235 default: break;
236 case 'r': // GENERAL_REGS
237 if (VT == MVT::i8)
Craig Topperc7242e02012-04-20 07:30:17 +0000238 return std::make_pair(0U, &MSP430::GR8RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000239
Craig Topperc7242e02012-04-20 07:30:17 +0000240 return std::make_pair(0U, &MSP430::GR16RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000241 }
242 }
243
244 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
245}
246
247//===----------------------------------------------------------------------===//
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000248// Calling Convention Implementation
249//===----------------------------------------------------------------------===//
250
Anton Korobeynikov10138002009-05-03 12:57:15 +0000251#include "MSP430GenCallingConv.inc"
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000252
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000253SDValue
254MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000255 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000256 bool isVarArg,
257 const SmallVectorImpl<ISD::InputArg>
258 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000259 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000260 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000261 SmallVectorImpl<SDValue> &InVals)
262 const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000263
264 switch (CallConv) {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000265 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000266 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000267 case CallingConv::C:
268 case CallingConv::Fast:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000269 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000270 case CallingConv::MSP430_INTR:
David Blaikie46a9f012012-01-20 21:51:11 +0000271 if (Ins.empty())
272 return Chain;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000273 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000274 }
275}
276
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000277SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000278MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000279 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000280 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000281 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000282 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
283 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
284 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000285 SDValue Chain = CLI.Chain;
286 SDValue Callee = CLI.Callee;
287 bool &isTailCall = CLI.IsTailCall;
288 CallingConv::ID CallConv = CLI.CallConv;
289 bool isVarArg = CLI.IsVarArg;
290
Evan Cheng67a69dd2010-01-27 00:07:07 +0000291 // MSP430 target does not yet support tail call optimization.
292 isTailCall = false;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000293
294 switch (CallConv) {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000295 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000296 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000297 case CallingConv::Fast:
298 case CallingConv::C:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000299 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000300 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000301 case CallingConv::MSP430_INTR:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000302 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000303 }
304}
305
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000306/// LowerCCCArguments - transform physical registers into virtual registers and
307/// generate load operations for arguments places on the stack.
308// FIXME: struct return stuff
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000309SDValue
310MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000311 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000312 bool isVarArg,
313 const SmallVectorImpl<ISD::InputArg>
314 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000315 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000316 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000317 SmallVectorImpl<SDValue> &InVals)
318 const {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000319 MachineFunction &MF = DAG.getMachineFunction();
320 MachineFrameInfo *MFI = MF.getFrameInfo();
321 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000322 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000323
324 // Assign locations to all of the incoming arguments.
325 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000326 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000327 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000328 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000329
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000330 // Create frame index for the start of the first vararg value
331 if (isVarArg) {
332 unsigned Offset = CCInfo.getNextStackOffset();
333 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, Offset, true));
334 }
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000335
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000336 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
337 CCValAssign &VA = ArgLocs[i];
338 if (VA.isRegLoc()) {
339 // Arguments passed in registers
Owen Anderson53aa7a92009-08-10 22:56:29 +0000340 EVT RegVT = VA.getLocVT();
Owen Anderson9f944592009-08-11 20:47:22 +0000341 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Andersonb2c80da2011-02-25 21:41:48 +0000342 default:
Torok Edwinfa040022009-07-08 19:04:27 +0000343 {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000344#ifndef NDEBUG
Chris Lattner317dbbc2009-08-23 07:05:07 +0000345 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson9f944592009-08-11 20:47:22 +0000346 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000347#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000348 llvm_unreachable(0);
Torok Edwinfa040022009-07-08 19:04:27 +0000349 }
Owen Anderson9f944592009-08-11 20:47:22 +0000350 case MVT::i16:
Craig Topperc7242e02012-04-20 07:30:17 +0000351 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000352 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000353 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000354
355 // If this is an 8-bit value, it is really passed promoted to 16
356 // bits. Insert an assert[sz]ext to capture this, then truncate to the
357 // right size.
358 if (VA.getLocInfo() == CCValAssign::SExt)
359 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
360 DAG.getValueType(VA.getValVT()));
361 else if (VA.getLocInfo() == CCValAssign::ZExt)
362 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
363 DAG.getValueType(VA.getValVT()));
364
365 if (VA.getLocInfo() != CCValAssign::Full)
366 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
367
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000368 InVals.push_back(ArgValue);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000369 }
370 } else {
371 // Sanity check
372 assert(VA.isMemLoc());
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000373
Anton Korobeynikov34148722012-11-21 17:23:03 +0000374 SDValue InVal;
375 ISD::ArgFlagsTy Flags = Ins[i].Flags;
376
377 if (Flags.isByVal()) {
378 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
379 VA.getLocMemOffset(), true);
380 InVal = DAG.getFrameIndex(FI, getPointerTy());
381 } else {
382 // Load the argument to a virtual register
383 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
384 if (ObjSize > 2) {
385 errs() << "LowerFormalArguments Unhandled argument type: "
386 << EVT(VA.getLocVT()).getEVTString()
387 << "\n";
388 }
389 // Create the frame index object for this incoming parameter...
390 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
391
392 // Create the SelectionDAG nodes corresponding to a load
393 //from this parameter
394 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
395 InVal = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
396 MachinePointerInfo::getFixedStack(FI),
397 false, false, false, 0);
398 }
399
400 InVals.push_back(InVal);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000401 }
402 }
403
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000404 return Chain;
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000405}
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000406
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000407SDValue
408MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000409 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000410 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000411 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000412 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000413
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000414 // CCValAssign - represent the assignment of the return value to a location
415 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000416
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000417 // ISRs cannot return any value.
David Blaikie46a9f012012-01-20 21:51:11 +0000418 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
Chris Lattner2104b8d2010-04-07 22:58:41 +0000419 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000420
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000421 // CCState - Info about the registers and stack slot.
Eric Christopher0713a9d2011-06-08 23:55:35 +0000422 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000423 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000424
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000425 // Analize return values.
426 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000427
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000428 SDValue Flag;
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000429 SmallVector<SDValue, 4> RetOps(1, Chain);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000430
431 // Copy the result values into the output registers.
432 for (unsigned i = 0; i != RVLocs.size(); ++i) {
433 CCValAssign &VA = RVLocs[i];
434 assert(VA.isRegLoc() && "Can only return in registers!");
435
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000436 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000437 OutVals[i], Flag);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000438
Anton Korobeynikovc10f98a2009-05-03 13:00:11 +0000439 // Guarantee that all emitted copies are stuck together,
440 // avoiding something bad.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000441 Flag = Chain.getValue(1);
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000442 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000443 }
444
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000445 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
446 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
447
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000448 RetOps[0] = Chain; // Update chain.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000449
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000450 // Add the flag if we have it.
451 if (Flag.getNode())
452 RetOps.push_back(Flag);
453
454 return DAG.getNode(Opc, dl, MVT::Other, &RetOps[0], RetOps.size());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000455}
456
Anton Korobeynikov56135102009-05-03 13:07:31 +0000457/// LowerCCCCallTo - functions arguments are copied from virtual regs to
458/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
459/// TODO: sret.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000460SDValue
461MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000462 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000463 bool isTailCall,
464 const SmallVectorImpl<ISD::OutputArg>
465 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000466 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000467 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000468 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000469 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000470 // Analyze operands of the call, assigning locations to each operand.
471 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000472 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000473 getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000474
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000475 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000476
477 // Get a count of how many bytes are to be pushed on the stack.
478 unsigned NumBytes = CCInfo.getNextStackOffset();
479
480 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
Andrew Trickad6d08a2013-05-29 22:03:55 +0000481 getPointerTy(), true),
482 dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000483
484 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
485 SmallVector<SDValue, 12> MemOpChains;
486 SDValue StackPtr;
487
488 // Walk the register/memloc assignments, inserting copies/loads.
489 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
490 CCValAssign &VA = ArgLocs[i];
491
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000492 SDValue Arg = OutVals[i];
Anton Korobeynikov56135102009-05-03 13:07:31 +0000493
494 // Promote the value if needed.
495 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000496 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000497 case CCValAssign::Full: break;
498 case CCValAssign::SExt:
499 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
500 break;
501 case CCValAssign::ZExt:
502 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
503 break;
504 case CCValAssign::AExt:
505 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
506 break;
507 }
508
509 // Arguments that can be passed on register must be kept at RegsToPass
510 // vector
511 if (VA.isRegLoc()) {
512 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
513 } else {
514 assert(VA.isMemLoc());
515
516 if (StackPtr.getNode() == 0)
517 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
518
519 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
520 StackPtr,
521 DAG.getIntPtrConstant(VA.getLocMemOffset()));
522
Anton Korobeynikov34148722012-11-21 17:23:03 +0000523 SDValue MemOp;
524 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000525
Anton Korobeynikov34148722012-11-21 17:23:03 +0000526 if (Flags.isByVal()) {
527 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16);
528 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
529 Flags.getByValAlign(),
530 /*isVolatile*/false,
531 /*AlwaysInline=*/true,
532 MachinePointerInfo(),
533 MachinePointerInfo());
534 } else {
535 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(),
536 false, false, 0);
537 }
538
539 MemOpChains.push_back(MemOp);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000540 }
541 }
542
543 // Transform all store nodes into one single node because all store nodes are
544 // independent of each other.
545 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +0000546 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov56135102009-05-03 13:07:31 +0000547 &MemOpChains[0], MemOpChains.size());
548
549 // Build a sequence of copy-to-reg nodes chained together with token chain and
550 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000551 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov56135102009-05-03 13:07:31 +0000552 SDValue InFlag;
553 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
554 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
555 RegsToPass[i].second, InFlag);
556 InFlag = Chain.getValue(1);
557 }
558
559 // If the callee is a GlobalAddress node (quite common, every direct call is)
560 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
561 // Likewise ExternalSymbol -> TargetExternalSymbol.
562 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patela3ca21b2010-07-06 22:08:15 +0000563 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000564 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson9f944592009-08-11 20:47:22 +0000565 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000566
567 // Returns a chain & a flag for retval copy to use.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000568 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000569 SmallVector<SDValue, 8> Ops;
570 Ops.push_back(Chain);
571 Ops.push_back(Callee);
572
573 // Add argument registers to the end of the list so that they are
574 // known live into the call.
575 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
576 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
577 RegsToPass[i].second.getValueType()));
578
579 if (InFlag.getNode())
580 Ops.push_back(InFlag);
581
582 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
583 InFlag = Chain.getValue(1);
584
585 // Create the CALLSEQ_END node.
586 Chain = DAG.getCALLSEQ_END(Chain,
587 DAG.getConstant(NumBytes, getPointerTy(), true),
588 DAG.getConstant(0, getPointerTy(), true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000589 InFlag, dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000590 InFlag = Chain.getValue(1);
591
592 // Handle result values, copying them out of physregs into vregs that we
593 // return.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000594 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
595 DAG, InVals);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000596}
597
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000598/// LowerCallResult - Lower the result values of a call into the
599/// appropriate copies out of appropriate physical registers.
600///
601SDValue
Anton Korobeynikov56135102009-05-03 13:07:31 +0000602MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000603 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000604 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000605 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000606 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000607
608 // Assign locations to each value returned by this call.
609 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000610 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000611 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000612
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000613 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000614
615 // Copy all of the result registers out of their specified physreg.
616 for (unsigned i = 0; i != RVLocs.size(); ++i) {
617 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
618 RVLocs[i].getValVT(), InFlag).getValue(1);
619 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000620 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov56135102009-05-03 13:07:31 +0000621 }
622
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000623 return Chain;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000624}
625
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000626SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000627 SelectionDAG &DAG) const {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000628 unsigned Opc = Op.getOpcode();
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000629 SDNode* N = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000630 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000631 SDLoc dl(N);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000632
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000633 // Expand non-constant shifts to loops:
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000634 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000635 switch (Opc) {
Craig Toppere55c5562012-02-07 02:50:20 +0000636 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000637 case ISD::SHL:
638 return DAG.getNode(MSP430ISD::SHL, dl,
639 VT, N->getOperand(0), N->getOperand(1));
640 case ISD::SRA:
641 return DAG.getNode(MSP430ISD::SRA, dl,
642 VT, N->getOperand(0), N->getOperand(1));
643 case ISD::SRL:
644 return DAG.getNode(MSP430ISD::SRL, dl,
645 VT, N->getOperand(0), N->getOperand(1));
646 }
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000647
648 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
649
650 // Expand the stuff into sequence of shifts.
651 // FIXME: for some shift amounts this might be done better!
652 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
653 SDValue Victim = N->getOperand(0);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000654
655 if (Opc == ISD::SRL && ShiftAmount) {
656 // Emit a special goodness here:
657 // srl A, 1 => clrc; rrc A
Anton Korobeynikovf3a6bc82009-05-03 13:16:37 +0000658 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000659 ShiftAmount -= 1;
660 }
661
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000662 while (ShiftAmount--)
Anton Korobeynikov6b5523a2009-05-17 10:15:22 +0000663 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000664 dl, VT, Victim);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000665
666 return Victim;
667}
668
Dan Gohman21cea8a2010-04-17 15:26:15 +0000669SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
670 SelectionDAG &DAG) const {
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000671 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
672 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
673
674 // Create the TargetGlobalAddress node, folding in the constant offset.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000675 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
Devang Patela3ca21b2010-07-06 22:08:15 +0000676 getPointerTy(), Offset);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000677 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op),
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000678 getPointerTy(), Result);
679}
680
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000681SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000682 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000683 SDLoc dl(Op);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000684 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
685 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
686
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000687 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000688}
689
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000690SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
691 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000692 SDLoc dl(Op);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000693 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liaoabb87d42012-09-12 21:43:09 +0000694 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000695
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000696 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000697}
698
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000699static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000700 ISD::CondCode CC,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000701 SDLoc dl, SelectionDAG &DAG) {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000702 // FIXME: Handle bittests someday
703 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
704
705 // FIXME: Handle jump negative someday
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000706 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000707 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000708 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov96272012009-05-03 13:12:06 +0000709 case ISD::SETEQ:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000710 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000711 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000712 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000713 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000714 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000715 break;
716 case ISD::SETNE:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000717 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000718 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000719 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000720 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000721 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000722 break;
723 case ISD::SETULE:
724 std::swap(LHS, RHS); // FALLTHROUGH
725 case ISD::SETUGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000726 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
727 // fold constant into instruction.
728 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
729 LHS = RHS;
730 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
731 TCC = MSP430CC::COND_LO;
732 break;
733 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000734 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikov96272012009-05-03 13:12:06 +0000735 break;
736 case ISD::SETUGT:
737 std::swap(LHS, RHS); // FALLTHROUGH
738 case ISD::SETULT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000739 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
740 // fold constant into instruction.
741 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
742 LHS = RHS;
743 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
744 TCC = MSP430CC::COND_HS;
745 break;
746 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000747 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikov96272012009-05-03 13:12:06 +0000748 break;
749 case ISD::SETLE:
750 std::swap(LHS, RHS); // FALLTHROUGH
751 case ISD::SETGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000752 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
753 // fold constant into instruction.
754 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
755 LHS = RHS;
756 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
757 TCC = MSP430CC::COND_L;
758 break;
759 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000760 TCC = MSP430CC::COND_GE;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000761 break;
762 case ISD::SETGT:
763 std::swap(LHS, RHS); // FALLTHROUGH
764 case ISD::SETLT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000765 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
766 // fold constant into instruction.
767 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
768 LHS = RHS;
769 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
770 TCC = MSP430CC::COND_GE;
771 break;
772 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000773 TCC = MSP430CC::COND_L;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000774 break;
775 }
776
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000777 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000778 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000779}
780
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000781
Dan Gohman21cea8a2010-04-17 15:26:15 +0000782SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000783 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000784 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
785 SDValue LHS = Op.getOperand(2);
786 SDValue RHS = Op.getOperand(3);
787 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000788 SDLoc dl (Op);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000789
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000790 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000791 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000792
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000793 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000794 Chain, Dest, TargetCC, Flag);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000795}
796
Dan Gohman21cea8a2010-04-17 15:26:15 +0000797SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000798 SDValue LHS = Op.getOperand(0);
799 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000800 SDLoc dl (Op);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000801
802 // If we are doing an AND and testing against zero, then the CMP
803 // will not be generated. The AND (or BIT) will generate the condition codes,
804 // but they are different from CMP.
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000805 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
806 // lowering & isel wouldn't diverge.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000807 bool andCC = false;
808 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
809 if (RHSC->isNullValue() && LHS.hasOneUse() &&
810 (LHS.getOpcode() == ISD::AND ||
811 (LHS.getOpcode() == ISD::TRUNCATE &&
812 LHS.getOperand(0).getOpcode() == ISD::AND))) {
813 andCC = true;
814 }
815 }
816 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
817 SDValue TargetCC;
818 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
819
820 // Get the condition codes directly from the status register, if its easy.
821 // Otherwise a branch will be generated. Note that the AND and BIT
822 // instructions generate different flags than CMP, the carry bit can be used
823 // for NE/EQ.
824 bool Invert = false;
825 bool Shift = false;
826 bool Convert = true;
827 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
828 default:
829 Convert = false;
830 break;
831 case MSP430CC::COND_HS:
832 // Res = SRW & 1, no processing is required
833 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000834 case MSP430CC::COND_LO:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000835 // Res = ~(SRW & 1)
836 Invert = true;
837 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000838 case MSP430CC::COND_NE:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000839 if (andCC) {
840 // C = ~Z, thus Res = SRW & 1, no processing is required
841 } else {
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000842 // Res = ~((SRW >> 1) & 1)
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000843 Shift = true;
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000844 Invert = true;
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000845 }
846 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000847 case MSP430CC::COND_E:
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000848 Shift = true;
849 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
850 // Res = (SRW >> 1) & 1 is 1 word shorter.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000851 break;
852 }
853 EVT VT = Op.getValueType();
854 SDValue One = DAG.getConstant(1, VT);
855 if (Convert) {
856 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000857 MVT::i16, Flag);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000858 if (Shift)
859 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
860 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
861 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
862 if (Invert)
863 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
864 return SR;
865 } else {
866 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000867 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000868 SmallVector<SDValue, 4> Ops;
869 Ops.push_back(One);
870 Ops.push_back(Zero);
871 Ops.push_back(TargetCC);
872 Ops.push_back(Flag);
873 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
874 }
875}
876
Dan Gohman21cea8a2010-04-17 15:26:15 +0000877SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
878 SelectionDAG &DAG) const {
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000879 SDValue LHS = Op.getOperand(0);
880 SDValue RHS = Op.getOperand(1);
881 SDValue TrueV = Op.getOperand(2);
882 SDValue FalseV = Op.getOperand(3);
883 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000884 SDLoc dl (Op);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000885
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000886 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000887 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000888
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000889 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000890 SmallVector<SDValue, 4> Ops;
891 Ops.push_back(TrueV);
892 Ops.push_back(FalseV);
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000893 Ops.push_back(TargetCC);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000894 Ops.push_back(Flag);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000895
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000896 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000897}
898
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000899SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000900 SelectionDAG &DAG) const {
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000901 SDValue Val = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000902 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000903 SDLoc dl(Op);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000904
Owen Anderson9f944592009-08-11 20:47:22 +0000905 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000906
907 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
908 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
909 DAG.getValueType(Val.getValueType()));
910}
911
Dan Gohman21cea8a2010-04-17 15:26:15 +0000912SDValue
913MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000914 MachineFunction &MF = DAG.getMachineFunction();
915 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
916 int ReturnAddrIndex = FuncInfo->getRAIndex();
917
918 if (ReturnAddrIndex == 0) {
919 // Set up a frame object for the return address.
Chandler Carruth5da3f052012-11-01 09:14:31 +0000920 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000921 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +0000922 true);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000923 FuncInfo->setRAIndex(ReturnAddrIndex);
924 }
925
926 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
927}
928
Dan Gohman21cea8a2010-04-17 15:26:15 +0000929SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
930 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +0000931 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
932 MFI->setReturnAddressIsTaken(true);
933
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000934 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000935 SDLoc dl(Op);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000936
937 if (Depth > 0) {
938 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
939 SDValue Offset =
Chandler Carruth5da3f052012-11-01 09:14:31 +0000940 DAG.getConstant(TD->getPointerSize(), MVT::i16);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000941 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
942 DAG.getNode(ISD::ADD, dl, getPointerTy(),
943 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000944 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000945 }
946
947 // Just load the return address.
948 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
949 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000950 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000951}
952
Dan Gohman21cea8a2010-04-17 15:26:15 +0000953SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
954 SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000955 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
956 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +0000957
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000958 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000959 SDLoc dl(Op); // FIXME probably not meaningful
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000960 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
961 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
962 MSP430::FPW, VT);
963 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +0000964 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
965 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000966 false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000967 return FrameAddr;
968}
969
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000970SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
971 SelectionDAG &DAG) const {
972 MachineFunction &MF = DAG.getMachineFunction();
973 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
974
975 // Frame index of first vararg argument
976 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
977 getPointerTy());
978 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
979
980 // Create a store of the frame index to the location operand
Andrew Trickef9de2a2013-05-25 02:42:55 +0000981 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex,
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000982 Op.getOperand(1), MachinePointerInfo(SV),
983 false, false, 0);
984}
985
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000986SDValue MSP430TargetLowering::LowerJumpTable(SDValue Op,
987 SelectionDAG &DAG) const {
988 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
989 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikovfee796d2013-07-14 15:11:00 +0000990 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(JT),
991 getPointerTy(), Result);
Anton Korobeynikov82bedb12013-07-01 19:44:44 +0000992}
993
Anton Korobeynikovd3c83192009-11-07 17:15:06 +0000994/// getPostIndexedAddressParts - returns true by value, base pointer and
995/// offset pointer and addressing mode by reference if this node can be
996/// combined with a load / store to form a post-indexed load / store.
997bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
998 SDValue &Base,
999 SDValue &Offset,
1000 ISD::MemIndexedMode &AM,
1001 SelectionDAG &DAG) const {
1002
1003 LoadSDNode *LD = cast<LoadSDNode>(N);
1004 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
1005 return false;
1006
1007 EVT VT = LD->getMemoryVT();
1008 if (VT != MVT::i8 && VT != MVT::i16)
1009 return false;
1010
1011 if (Op->getOpcode() != ISD::ADD)
1012 return false;
1013
1014 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1015 uint64_t RHSC = RHS->getZExtValue();
1016 if ((VT == MVT::i16 && RHSC != 2) ||
1017 (VT == MVT::i8 && RHSC != 1))
1018 return false;
1019
1020 Base = Op->getOperand(0);
1021 Offset = DAG.getConstant(RHSC, VT);
1022 AM = ISD::POST_INC;
1023 return true;
1024 }
1025
1026 return false;
1027}
1028
1029
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001030const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
1031 switch (Opcode) {
1032 default: return NULL;
1033 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov24a63162009-12-07 02:28:41 +00001034 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikov15a515b2009-05-03 13:03:33 +00001035 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikov61763b52009-05-03 13:16:17 +00001036 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1037 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovec3f0b32009-05-03 13:07:54 +00001038 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikovcfc97052009-05-03 13:08:33 +00001039 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001040 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikov96272012009-05-03 13:12:06 +00001041 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001042 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001043 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1044 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001045 }
1046}
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001047
Chris Lattner229907c2011-07-18 04:54:35 +00001048bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1049 Type *Ty2) const {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001050 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001051 return false;
1052
1053 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1054}
1055
1056bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1057 if (!VT1.isInteger() || !VT2.isInteger())
1058 return false;
1059
1060 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1061}
1062
Chris Lattner229907c2011-07-18 04:54:35 +00001063bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001064 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001065 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001066}
1067
1068bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1069 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1070 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1071}
1072
Eli Bendersky39e7c6e2012-12-18 18:21:29 +00001073bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1074 return isZExtFree(Val.getValueType(), VT2);
1075}
1076
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001077//===----------------------------------------------------------------------===//
1078// Other Lowering Code
1079//===----------------------------------------------------------------------===//
1080
1081MachineBasicBlock*
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001082MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001083 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001084 MachineFunction *F = BB->getParent();
1085 MachineRegisterInfo &RI = F->getRegInfo();
1086 DebugLoc dl = MI->getDebugLoc();
1087 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1088
1089 unsigned Opc;
1090 const TargetRegisterClass * RC;
1091 switch (MI->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00001092 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001093 case MSP430::Shl8:
1094 Opc = MSP430::SHL8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001095 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001096 break;
1097 case MSP430::Shl16:
1098 Opc = MSP430::SHL16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001099 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001100 break;
1101 case MSP430::Sra8:
1102 Opc = MSP430::SAR8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001103 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001104 break;
1105 case MSP430::Sra16:
1106 Opc = MSP430::SAR16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001107 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001108 break;
1109 case MSP430::Srl8:
1110 Opc = MSP430::SAR8r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001111 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001112 break;
1113 case MSP430::Srl16:
1114 Opc = MSP430::SAR16r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001115 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001116 break;
1117 }
1118
1119 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1120 MachineFunction::iterator I = BB;
1121 ++I;
1122
1123 // Create loop block
1124 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1125 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1126
1127 F->insert(I, LoopBB);
1128 F->insert(I, RemBB);
1129
1130 // Update machine-CFG edges by transferring all successors of the current
1131 // block to the block containing instructions after shift.
Dan Gohman34396292010-07-06 20:24:04 +00001132 RemBB->splice(RemBB->begin(), BB,
1133 llvm::next(MachineBasicBlock::iterator(MI)),
1134 BB->end());
1135 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001136
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001137 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1138 BB->addSuccessor(LoopBB);
1139 BB->addSuccessor(RemBB);
1140 LoopBB->addSuccessor(RemBB);
1141 LoopBB->addSuccessor(LoopBB);
1142
Craig Topperc7242e02012-04-20 07:30:17 +00001143 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1144 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001145 unsigned ShiftReg = RI.createVirtualRegister(RC);
1146 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1147 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1148 unsigned SrcReg = MI->getOperand(1).getReg();
1149 unsigned DstReg = MI->getOperand(0).getReg();
1150
1151 // BB:
1152 // cmp 0, N
1153 // je RemBB
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +00001154 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1155 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001156 BuildMI(BB, dl, TII.get(MSP430::JCC))
1157 .addMBB(RemBB)
1158 .addImm(MSP430CC::COND_E);
1159
1160 // LoopBB:
1161 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1162 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1163 // ShiftReg2 = shift ShiftReg
1164 // ShiftAmt2 = ShiftAmt - 1;
1165 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1166 .addReg(SrcReg).addMBB(BB)
1167 .addReg(ShiftReg2).addMBB(LoopBB);
1168 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1169 .addReg(ShiftAmtSrcReg).addMBB(BB)
1170 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1171 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1172 .addReg(ShiftReg);
1173 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1174 .addReg(ShiftAmtReg).addImm(1);
1175 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1176 .addMBB(LoopBB)
1177 .addImm(MSP430CC::COND_NE);
1178
1179 // RemBB:
1180 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman34396292010-07-06 20:24:04 +00001181 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001182 .addReg(SrcReg).addMBB(BB)
1183 .addReg(ShiftReg2).addMBB(LoopBB);
1184
Dan Gohman34396292010-07-06 20:24:04 +00001185 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001186 return RemBB;
1187}
1188
1189MachineBasicBlock*
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001190MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001191 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001192 unsigned Opc = MI->getOpcode();
1193
1194 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1195 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1196 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohman25c16532010-05-01 00:01:06 +00001197 return EmitShiftInstr(MI, BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001198
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001199 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1200 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001201
1202 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001203 "Unexpected instr type to insert");
1204
1205 // To "insert" a SELECT instruction, we actually have to insert the diamond
1206 // control-flow pattern. The incoming instruction knows the destination vreg
1207 // to set, the condition code register to branch on, the true/false values to
1208 // select between, and a branch opcode to use.
1209 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1210 MachineFunction::iterator I = BB;
1211 ++I;
1212
1213 // thisMBB:
1214 // ...
1215 // TrueVal = ...
1216 // cmpTY ccX, r1, r2
1217 // jCC copy1MBB
1218 // fallthrough --> copy0MBB
1219 MachineBasicBlock *thisMBB = BB;
1220 MachineFunction *F = BB->getParent();
1221 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1222 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001223 F->insert(I, copy0MBB);
1224 F->insert(I, copy1MBB);
1225 // Update machine-CFG edges by transferring all successors of the current
1226 // block to the new block which will contain the Phi node for the select.
Dan Gohman34396292010-07-06 20:24:04 +00001227 copy1MBB->splice(copy1MBB->begin(), BB,
1228 llvm::next(MachineBasicBlock::iterator(MI)),
1229 BB->end());
1230 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001231 // Next, add the true and fallthrough blocks as its successors.
1232 BB->addSuccessor(copy0MBB);
1233 BB->addSuccessor(copy1MBB);
1234
Dan Gohman34396292010-07-06 20:24:04 +00001235 BuildMI(BB, dl, TII.get(MSP430::JCC))
1236 .addMBB(copy1MBB)
1237 .addImm(MI->getOperand(3).getImm());
1238
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001239 // copy0MBB:
1240 // %FalseValue = ...
1241 // # fallthrough to copy1MBB
1242 BB = copy0MBB;
1243
1244 // Update machine-CFG edges
1245 BB->addSuccessor(copy1MBB);
1246
1247 // copy1MBB:
1248 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1249 // ...
1250 BB = copy1MBB;
Dan Gohman34396292010-07-06 20:24:04 +00001251 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001252 MI->getOperand(0).getReg())
1253 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1254 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1255
Dan Gohman34396292010-07-06 20:24:04 +00001256 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001257 return BB;
1258}