blob: a4818b2ceb99cf45e8ae7c799f21cfba04e0ae7f [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);
172
Anton Korobeynikov28d3c732009-12-07 02:27:08 +0000173 // Libcalls names.
174 if (HWMultMode == HWMultIntr) {
175 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
176 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
177 } else if (HWMultMode == HWMultNoIntr) {
178 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
179 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
180 }
Eli Friedman2518f832011-05-06 20:34:06 +0000181
182 setMinFunctionAlignment(1);
183 setPrefFunctionAlignment(2);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000184}
185
Dan Gohman21cea8a2010-04-17 15:26:15 +0000186SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
187 SelectionDAG &DAG) const {
Anton Korobeynikov10138002009-05-03 12:57:15 +0000188 switch (Op.getOpcode()) {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000189 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000190 case ISD::SRL:
Anton Korobeynikov56135102009-05-03 13:07:31 +0000191 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000192 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000193 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000194 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000195 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000196 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
197 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000198 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000199 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
200 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000201 case ISD::VASTART: return LowerVASTART(Op, DAG);
Anton Korobeynikov10138002009-05-03 12:57:15 +0000202 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000203 llvm_unreachable("unimplemented operand");
Anton Korobeynikov10138002009-05-03 12:57:15 +0000204 }
205}
206
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000207//===----------------------------------------------------------------------===//
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000208// MSP430 Inline Assembly Support
209//===----------------------------------------------------------------------===//
210
211/// getConstraintType - Given a constraint letter, return the type of
212/// constraint it is for this target.
213TargetLowering::ConstraintType
214MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
215 if (Constraint.size() == 1) {
216 switch (Constraint[0]) {
217 case 'r':
218 return C_RegisterClass;
219 default:
220 break;
221 }
222 }
223 return TargetLowering::getConstraintType(Constraint);
224}
225
226std::pair<unsigned, const TargetRegisterClass*>
227MSP430TargetLowering::
228getRegForInlineAsmConstraint(const std::string &Constraint,
229 EVT VT) const {
230 if (Constraint.size() == 1) {
231 // GCC Constraint Letters
232 switch (Constraint[0]) {
233 default: break;
234 case 'r': // GENERAL_REGS
235 if (VT == MVT::i8)
Craig Topperc7242e02012-04-20 07:30:17 +0000236 return std::make_pair(0U, &MSP430::GR8RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000237
Craig Topperc7242e02012-04-20 07:30:17 +0000238 return std::make_pair(0U, &MSP430::GR16RegClass);
Anton Korobeynikova0e01be2009-08-26 13:44:29 +0000239 }
240 }
241
242 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
243}
244
245//===----------------------------------------------------------------------===//
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000246// Calling Convention Implementation
247//===----------------------------------------------------------------------===//
248
Anton Korobeynikov10138002009-05-03 12:57:15 +0000249#include "MSP430GenCallingConv.inc"
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000250
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000251SDValue
252MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000253 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000254 bool isVarArg,
255 const SmallVectorImpl<ISD::InputArg>
256 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000257 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000258 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000259 SmallVectorImpl<SDValue> &InVals)
260 const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000261
262 switch (CallConv) {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000263 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000264 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000265 case CallingConv::C:
266 case CallingConv::Fast:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000267 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000268 case CallingConv::MSP430_INTR:
David Blaikie46a9f012012-01-20 21:51:11 +0000269 if (Ins.empty())
270 return Chain;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000271 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000272 }
273}
274
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000275SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000276MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000277 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000278 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000279 SDLoc &dl = CLI.DL;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000280 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
281 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
282 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
283 SDValue Chain = CLI.Chain;
284 SDValue Callee = CLI.Callee;
285 bool &isTailCall = CLI.IsTailCall;
286 CallingConv::ID CallConv = CLI.CallConv;
287 bool isVarArg = CLI.IsVarArg;
288
Evan Cheng67a69dd2010-01-27 00:07:07 +0000289 // MSP430 target does not yet support tail call optimization.
290 isTailCall = false;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000291
292 switch (CallConv) {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000293 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000294 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000295 case CallingConv::Fast:
296 case CallingConv::C:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000297 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000298 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000299 case CallingConv::MSP430_INTR:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000300 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000301 }
302}
303
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000304/// LowerCCCArguments - transform physical registers into virtual registers and
305/// generate load operations for arguments places on the stack.
306// FIXME: struct return stuff
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000307SDValue
308MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000309 CallingConv::ID CallConv,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000310 bool isVarArg,
311 const SmallVectorImpl<ISD::InputArg>
312 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000313 SDLoc dl,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000314 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000315 SmallVectorImpl<SDValue> &InVals)
316 const {
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000317 MachineFunction &MF = DAG.getMachineFunction();
318 MachineFrameInfo *MFI = MF.getFrameInfo();
319 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000320 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000321
322 // Assign locations to all of the incoming arguments.
323 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000324 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000325 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000326 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000327
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000328 // Create frame index for the start of the first vararg value
329 if (isVarArg) {
330 unsigned Offset = CCInfo.getNextStackOffset();
331 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, Offset, true));
332 }
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000333
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000334 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
335 CCValAssign &VA = ArgLocs[i];
336 if (VA.isRegLoc()) {
337 // Arguments passed in registers
Owen Anderson53aa7a92009-08-10 22:56:29 +0000338 EVT RegVT = VA.getLocVT();
Owen Anderson9f944592009-08-11 20:47:22 +0000339 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Andersonb2c80da2011-02-25 21:41:48 +0000340 default:
Torok Edwinfa040022009-07-08 19:04:27 +0000341 {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000342#ifndef NDEBUG
Chris Lattner317dbbc2009-08-23 07:05:07 +0000343 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson9f944592009-08-11 20:47:22 +0000344 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000345#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000346 llvm_unreachable(0);
Torok Edwinfa040022009-07-08 19:04:27 +0000347 }
Owen Anderson9f944592009-08-11 20:47:22 +0000348 case MVT::i16:
Craig Topperc7242e02012-04-20 07:30:17 +0000349 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000350 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000351 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000352
353 // If this is an 8-bit value, it is really passed promoted to 16
354 // bits. Insert an assert[sz]ext to capture this, then truncate to the
355 // right size.
356 if (VA.getLocInfo() == CCValAssign::SExt)
357 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
358 DAG.getValueType(VA.getValVT()));
359 else if (VA.getLocInfo() == CCValAssign::ZExt)
360 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
361 DAG.getValueType(VA.getValVT()));
362
363 if (VA.getLocInfo() != CCValAssign::Full)
364 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
365
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000366 InVals.push_back(ArgValue);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000367 }
368 } else {
369 // Sanity check
370 assert(VA.isMemLoc());
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000371
Anton Korobeynikov34148722012-11-21 17:23:03 +0000372 SDValue InVal;
373 ISD::ArgFlagsTy Flags = Ins[i].Flags;
374
375 if (Flags.isByVal()) {
376 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
377 VA.getLocMemOffset(), true);
378 InVal = DAG.getFrameIndex(FI, getPointerTy());
379 } else {
380 // Load the argument to a virtual register
381 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
382 if (ObjSize > 2) {
383 errs() << "LowerFormalArguments Unhandled argument type: "
384 << EVT(VA.getLocVT()).getEVTString()
385 << "\n";
386 }
387 // Create the frame index object for this incoming parameter...
388 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
389
390 // Create the SelectionDAG nodes corresponding to a load
391 //from this parameter
392 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
393 InVal = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
394 MachinePointerInfo::getFixedStack(FI),
395 false, false, false, 0);
396 }
397
398 InVals.push_back(InVal);
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000399 }
400 }
401
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000402 return Chain;
Anton Korobeynikov3849be62009-05-03 12:59:33 +0000403}
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000404
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000405SDValue
406MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000407 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000408 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000409 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000410 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000411
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000412 // CCValAssign - represent the assignment of the return value to a location
413 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000414
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000415 // ISRs cannot return any value.
David Blaikie46a9f012012-01-20 21:51:11 +0000416 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
Chris Lattner2104b8d2010-04-07 22:58:41 +0000417 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000418
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000419 // CCState - Info about the registers and stack slot.
Eric Christopher0713a9d2011-06-08 23:55:35 +0000420 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000421 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000422
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000423 // Analize return values.
424 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000425
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000426 SDValue Flag;
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000427 SmallVector<SDValue, 4> RetOps(1, Chain);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000428
429 // Copy the result values into the output registers.
430 for (unsigned i = 0; i != RVLocs.size(); ++i) {
431 CCValAssign &VA = RVLocs[i];
432 assert(VA.isRegLoc() && "Can only return in registers!");
433
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000434 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000435 OutVals[i], Flag);
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000436
Anton Korobeynikovc10f98a2009-05-03 13:00:11 +0000437 // Guarantee that all emitted copies are stuck together,
438 // avoiding something bad.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000439 Flag = Chain.getValue(1);
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000440 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000441 }
442
Anton Korobeynikovb4be8ce2009-12-07 02:27:53 +0000443 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
444 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
445
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000446 RetOps[0] = Chain; // Update chain.
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000447
Jakob Stoklund Olesenb52a3ec2013-02-05 18:12:06 +0000448 // Add the flag if we have it.
449 if (Flag.getNode())
450 RetOps.push_back(Flag);
451
452 return DAG.getNode(Opc, dl, MVT::Other, &RetOps[0], RetOps.size());
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +0000453}
454
Anton Korobeynikov56135102009-05-03 13:07:31 +0000455/// LowerCCCCallTo - functions arguments are copied from virtual regs to
456/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
457/// TODO: sret.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000458SDValue
459MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000460 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000461 bool isTailCall,
462 const SmallVectorImpl<ISD::OutputArg>
463 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000464 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000465 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000466 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000467 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000468 // Analyze operands of the call, assigning locations to each operand.
469 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000470 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000471 getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000472
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000473 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000474
475 // Get a count of how many bytes are to be pushed on the stack.
476 unsigned NumBytes = CCInfo.getNextStackOffset();
477
478 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
Andrew Trickad6d08a2013-05-29 22:03:55 +0000479 getPointerTy(), true),
480 dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000481
482 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
483 SmallVector<SDValue, 12> MemOpChains;
484 SDValue StackPtr;
485
486 // Walk the register/memloc assignments, inserting copies/loads.
487 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
488 CCValAssign &VA = ArgLocs[i];
489
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000490 SDValue Arg = OutVals[i];
Anton Korobeynikov56135102009-05-03 13:07:31 +0000491
492 // Promote the value if needed.
493 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000494 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov56135102009-05-03 13:07:31 +0000495 case CCValAssign::Full: break;
496 case CCValAssign::SExt:
497 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
498 break;
499 case CCValAssign::ZExt:
500 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
501 break;
502 case CCValAssign::AExt:
503 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
504 break;
505 }
506
507 // Arguments that can be passed on register must be kept at RegsToPass
508 // vector
509 if (VA.isRegLoc()) {
510 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
511 } else {
512 assert(VA.isMemLoc());
513
514 if (StackPtr.getNode() == 0)
515 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
516
517 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
518 StackPtr,
519 DAG.getIntPtrConstant(VA.getLocMemOffset()));
520
Anton Korobeynikov34148722012-11-21 17:23:03 +0000521 SDValue MemOp;
522 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000523
Anton Korobeynikov34148722012-11-21 17:23:03 +0000524 if (Flags.isByVal()) {
525 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16);
526 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
527 Flags.getByValAlign(),
528 /*isVolatile*/false,
529 /*AlwaysInline=*/true,
530 MachinePointerInfo(),
531 MachinePointerInfo());
532 } else {
533 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(),
534 false, false, 0);
535 }
536
537 MemOpChains.push_back(MemOp);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000538 }
539 }
540
541 // Transform all store nodes into one single node because all store nodes are
542 // independent of each other.
543 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +0000544 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov56135102009-05-03 13:07:31 +0000545 &MemOpChains[0], MemOpChains.size());
546
547 // Build a sequence of copy-to-reg nodes chained together with token chain and
548 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000549 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov56135102009-05-03 13:07:31 +0000550 SDValue InFlag;
551 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
552 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
553 RegsToPass[i].second, InFlag);
554 InFlag = Chain.getValue(1);
555 }
556
557 // If the callee is a GlobalAddress node (quite common, every direct call is)
558 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
559 // Likewise ExternalSymbol -> TargetExternalSymbol.
560 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patela3ca21b2010-07-06 22:08:15 +0000561 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000562 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson9f944592009-08-11 20:47:22 +0000563 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000564
565 // Returns a chain & a flag for retval copy to use.
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000566 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000567 SmallVector<SDValue, 8> Ops;
568 Ops.push_back(Chain);
569 Ops.push_back(Callee);
570
571 // Add argument registers to the end of the list so that they are
572 // known live into the call.
573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
574 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
575 RegsToPass[i].second.getValueType()));
576
577 if (InFlag.getNode())
578 Ops.push_back(InFlag);
579
580 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
581 InFlag = Chain.getValue(1);
582
583 // Create the CALLSEQ_END node.
584 Chain = DAG.getCALLSEQ_END(Chain,
585 DAG.getConstant(NumBytes, getPointerTy(), true),
586 DAG.getConstant(0, getPointerTy(), true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000587 InFlag, dl);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000588 InFlag = Chain.getValue(1);
589
590 // Handle result values, copying them out of physregs into vregs that we
591 // return.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000592 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
593 DAG, InVals);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000594}
595
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000596/// LowerCallResult - Lower the result values of a call into the
597/// appropriate copies out of appropriate physical registers.
598///
599SDValue
Anton Korobeynikov56135102009-05-03 13:07:31 +0000600MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +0000601 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000602 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000603 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000604 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov56135102009-05-03 13:07:31 +0000605
606 // Assign locations to each value returned by this call.
607 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000608 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000609 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov56135102009-05-03 13:07:31 +0000610
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000611 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov56135102009-05-03 13:07:31 +0000612
613 // Copy all of the result registers out of their specified physreg.
614 for (unsigned i = 0; i != RVLocs.size(); ++i) {
615 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
616 RVLocs[i].getValVT(), InFlag).getValue(1);
617 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000618 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov56135102009-05-03 13:07:31 +0000619 }
620
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000621 return Chain;
Anton Korobeynikov56135102009-05-03 13:07:31 +0000622}
623
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000624SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000625 SelectionDAG &DAG) const {
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000626 unsigned Opc = Op.getOpcode();
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000627 SDNode* N = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000628 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000629 SDLoc dl(N);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000630
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000631 // Expand non-constant shifts to loops:
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000632 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000633 switch (Opc) {
Craig Toppere55c5562012-02-07 02:50:20 +0000634 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +0000635 case ISD::SHL:
636 return DAG.getNode(MSP430ISD::SHL, dl,
637 VT, N->getOperand(0), N->getOperand(1));
638 case ISD::SRA:
639 return DAG.getNode(MSP430ISD::SRA, dl,
640 VT, N->getOperand(0), N->getOperand(1));
641 case ISD::SRL:
642 return DAG.getNode(MSP430ISD::SRL, dl,
643 VT, N->getOperand(0), N->getOperand(1));
644 }
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000645
646 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
647
648 // Expand the stuff into sequence of shifts.
649 // FIXME: for some shift amounts this might be done better!
650 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
651 SDValue Victim = N->getOperand(0);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000652
653 if (Opc == ISD::SRL && ShiftAmount) {
654 // Emit a special goodness here:
655 // srl A, 1 => clrc; rrc A
Anton Korobeynikovf3a6bc82009-05-03 13:16:37 +0000656 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikov61763b52009-05-03 13:16:17 +0000657 ShiftAmount -= 1;
658 }
659
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000660 while (ShiftAmount--)
Anton Korobeynikov6b5523a2009-05-17 10:15:22 +0000661 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikova3f7a832009-05-03 13:13:17 +0000662 dl, VT, Victim);
Anton Korobeynikov15a515b2009-05-03 13:03:33 +0000663
664 return Victim;
665}
666
Dan Gohman21cea8a2010-04-17 15:26:15 +0000667SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
668 SelectionDAG &DAG) const {
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000669 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
670 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
671
672 // Create the TargetGlobalAddress node, folding in the constant offset.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000673 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
Devang Patela3ca21b2010-07-06 22:08:15 +0000674 getPointerTy(), Offset);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000675 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op),
Anton Korobeynikovcfc97052009-05-03 13:08:33 +0000676 getPointerTy(), Result);
677}
678
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000679SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000680 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000681 SDLoc dl(Op);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000682 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
683 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
684
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000685 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovba0e81d2009-05-03 13:14:46 +0000686}
687
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000688SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
689 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000690 SDLoc dl(Op);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000691 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liaoabb87d42012-09-12 21:43:09 +0000692 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000693
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000694 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000695}
696
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000697static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000698 ISD::CondCode CC,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000699 SDLoc dl, SelectionDAG &DAG) {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000700 // FIXME: Handle bittests someday
701 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
702
703 // FIXME: Handle jump negative someday
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000704 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000705 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000706 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov96272012009-05-03 13:12:06 +0000707 case ISD::SETEQ:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000708 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000709 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000710 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000711 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000712 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000713 break;
714 case ISD::SETNE:
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000715 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000716 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000717 // constant can be folded into comparison.
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +0000718 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikovabdf86d2009-11-22 01:14:08 +0000719 std::swap(LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000720 break;
721 case ISD::SETULE:
722 std::swap(LHS, RHS); // FALLTHROUGH
723 case ISD::SETUGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000724 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
725 // fold constant into instruction.
726 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
727 LHS = RHS;
728 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
729 TCC = MSP430CC::COND_LO;
730 break;
731 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000732 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikov96272012009-05-03 13:12:06 +0000733 break;
734 case ISD::SETUGT:
735 std::swap(LHS, RHS); // FALLTHROUGH
736 case ISD::SETULT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000737 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
738 // fold constant into instruction.
739 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
740 LHS = RHS;
741 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
742 TCC = MSP430CC::COND_HS;
743 break;
744 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000745 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikov96272012009-05-03 13:12:06 +0000746 break;
747 case ISD::SETLE:
748 std::swap(LHS, RHS); // FALLTHROUGH
749 case ISD::SETGE:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000750 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
751 // fold constant into instruction.
752 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
753 LHS = RHS;
754 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
755 TCC = MSP430CC::COND_L;
756 break;
757 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000758 TCC = MSP430CC::COND_GE;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000759 break;
760 case ISD::SETGT:
761 std::swap(LHS, RHS); // FALLTHROUGH
762 case ISD::SETLT:
Anton Korobeynikov6826ce72010-01-15 21:18:02 +0000763 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
764 // fold constant into instruction.
765 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
766 LHS = RHS;
767 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
768 TCC = MSP430CC::COND_GE;
769 break;
770 }
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000771 TCC = MSP430CC::COND_L;
Anton Korobeynikov96272012009-05-03 13:12:06 +0000772 break;
773 }
774
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000775 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000776 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000777}
778
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000779
Dan Gohman21cea8a2010-04-17 15:26:15 +0000780SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov96272012009-05-03 13:12:06 +0000781 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000782 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
783 SDValue LHS = Op.getOperand(2);
784 SDValue RHS = Op.getOperand(3);
785 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000786 SDLoc dl (Op);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000787
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000788 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000789 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000790
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000791 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000792 Chain, Dest, TargetCC, Flag);
Anton Korobeynikov96272012009-05-03 13:12:06 +0000793}
794
Dan Gohman21cea8a2010-04-17 15:26:15 +0000795SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000796 SDValue LHS = Op.getOperand(0);
797 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000798 SDLoc dl (Op);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000799
800 // If we are doing an AND and testing against zero, then the CMP
801 // will not be generated. The AND (or BIT) will generate the condition codes,
802 // but they are different from CMP.
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000803 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
804 // lowering & isel wouldn't diverge.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000805 bool andCC = false;
806 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
807 if (RHSC->isNullValue() && LHS.hasOneUse() &&
808 (LHS.getOpcode() == ISD::AND ||
809 (LHS.getOpcode() == ISD::TRUNCATE &&
810 LHS.getOperand(0).getOpcode() == ISD::AND))) {
811 andCC = true;
812 }
813 }
814 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
815 SDValue TargetCC;
816 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
817
818 // Get the condition codes directly from the status register, if its easy.
819 // Otherwise a branch will be generated. Note that the AND and BIT
820 // instructions generate different flags than CMP, the carry bit can be used
821 // for NE/EQ.
822 bool Invert = false;
823 bool Shift = false;
824 bool Convert = true;
825 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
826 default:
827 Convert = false;
828 break;
829 case MSP430CC::COND_HS:
830 // Res = SRW & 1, no processing is required
831 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000832 case MSP430CC::COND_LO:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000833 // Res = ~(SRW & 1)
834 Invert = true;
835 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000836 case MSP430CC::COND_NE:
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000837 if (andCC) {
838 // C = ~Z, thus Res = SRW & 1, no processing is required
839 } else {
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000840 // Res = ~((SRW >> 1) & 1)
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000841 Shift = true;
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000842 Invert = true;
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000843 }
844 break;
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000845 case MSP430CC::COND_E:
Anton Korobeynikove96503f2010-02-21 12:28:58 +0000846 Shift = true;
847 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
848 // Res = (SRW >> 1) & 1 is 1 word shorter.
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000849 break;
850 }
851 EVT VT = Op.getValueType();
852 SDValue One = DAG.getConstant(1, VT);
853 if (Convert) {
854 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
Anton Korobeynikov93a7d022010-01-15 21:18:18 +0000855 MVT::i16, Flag);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000856 if (Shift)
857 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
858 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
859 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
860 if (Invert)
861 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
862 return SR;
863 } else {
864 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000865 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikove27e0282009-12-11 23:01:29 +0000866 SmallVector<SDValue, 4> Ops;
867 Ops.push_back(One);
868 Ops.push_back(Zero);
869 Ops.push_back(TargetCC);
870 Ops.push_back(Flag);
871 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
872 }
873}
874
Dan Gohman21cea8a2010-04-17 15:26:15 +0000875SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
876 SelectionDAG &DAG) const {
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000877 SDValue LHS = Op.getOperand(0);
878 SDValue RHS = Op.getOperand(1);
879 SDValue TrueV = Op.getOperand(2);
880 SDValue FalseV = Op.getOperand(3);
881 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000882 SDLoc dl (Op);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000883
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000884 SDValue TargetCC;
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000885 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000886
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000887 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000888 SmallVector<SDValue, 4> Ops;
889 Ops.push_back(TrueV);
890 Ops.push_back(FalseV);
Anton Korobeynikov2983dcb2009-10-21 19:16:49 +0000891 Ops.push_back(TargetCC);
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000892 Ops.push_back(Flag);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000893
Anton Korobeynikov47fcd722009-05-03 13:19:09 +0000894 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikovb6321e152009-05-03 13:12:23 +0000895}
896
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000897SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000898 SelectionDAG &DAG) const {
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000899 SDValue Val = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000900 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000901 SDLoc dl(Op);
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000902
Owen Anderson9f944592009-08-11 20:47:22 +0000903 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikov29747e92009-05-03 13:17:49 +0000904
905 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
906 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
907 DAG.getValueType(Val.getValueType()));
908}
909
Dan Gohman21cea8a2010-04-17 15:26:15 +0000910SDValue
911MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000912 MachineFunction &MF = DAG.getMachineFunction();
913 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
914 int ReturnAddrIndex = FuncInfo->getRAIndex();
915
916 if (ReturnAddrIndex == 0) {
917 // Set up a frame object for the return address.
Chandler Carruth5da3f052012-11-01 09:14:31 +0000918 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000919 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +0000920 true);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000921 FuncInfo->setRAIndex(ReturnAddrIndex);
922 }
923
924 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
925}
926
Dan Gohman21cea8a2010-04-17 15:26:15 +0000927SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
928 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +0000929 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
930 MFI->setReturnAddressIsTaken(true);
931
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000932 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000933 SDLoc dl(Op);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000934
935 if (Depth > 0) {
936 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
937 SDValue Offset =
Chandler Carruth5da3f052012-11-01 09:14:31 +0000938 DAG.getConstant(TD->getPointerSize(), MVT::i16);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000939 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
940 DAG.getNode(ISD::ADD, dl, getPointerTy(),
941 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000942 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000943 }
944
945 // Just load the return address.
946 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
947 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000948 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000949}
950
Dan Gohman21cea8a2010-04-17 15:26:15 +0000951SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
952 SelectionDAG &DAG) const {
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000953 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
954 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +0000955
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000956 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000957 SDLoc dl(Op); // FIXME probably not meaningful
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000958 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
959 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
960 MSP430::FPW, VT);
961 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +0000962 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
963 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000964 false, false, false, 0);
Anton Korobeynikovff4ab512009-12-07 02:28:10 +0000965 return FrameAddr;
966}
967
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000968SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
969 SelectionDAG &DAG) const {
970 MachineFunction &MF = DAG.getMachineFunction();
971 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
972
973 // Frame index of first vararg argument
974 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
975 getPointerTy());
976 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
977
978 // Create a store of the frame index to the location operand
Andrew Trickef9de2a2013-05-25 02:42:55 +0000979 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex,
Anton Korobeynikov568afeb2012-11-21 17:28:27 +0000980 Op.getOperand(1), MachinePointerInfo(SV),
981 false, false, 0);
982}
983
Anton Korobeynikovd3c83192009-11-07 17:15:06 +0000984/// getPostIndexedAddressParts - returns true by value, base pointer and
985/// offset pointer and addressing mode by reference if this node can be
986/// combined with a load / store to form a post-indexed load / store.
987bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
988 SDValue &Base,
989 SDValue &Offset,
990 ISD::MemIndexedMode &AM,
991 SelectionDAG &DAG) const {
992
993 LoadSDNode *LD = cast<LoadSDNode>(N);
994 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
995 return false;
996
997 EVT VT = LD->getMemoryVT();
998 if (VT != MVT::i8 && VT != MVT::i16)
999 return false;
1000
1001 if (Op->getOpcode() != ISD::ADD)
1002 return false;
1003
1004 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1005 uint64_t RHSC = RHS->getZExtValue();
1006 if ((VT == MVT::i16 && RHSC != 2) ||
1007 (VT == MVT::i8 && RHSC != 1))
1008 return false;
1009
1010 Base = Op->getOperand(0);
1011 Offset = DAG.getConstant(RHSC, VT);
1012 AM = ISD::POST_INC;
1013 return true;
1014 }
1015
1016 return false;
1017}
1018
1019
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001020const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
1021 switch (Opcode) {
1022 default: return NULL;
1023 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov24a63162009-12-07 02:28:41 +00001024 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikov15a515b2009-05-03 13:03:33 +00001025 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikov61763b52009-05-03 13:16:17 +00001026 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1027 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovec3f0b32009-05-03 13:07:54 +00001028 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikovcfc97052009-05-03 13:08:33 +00001029 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001030 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikov96272012009-05-03 13:12:06 +00001031 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov47fcd722009-05-03 13:19:09 +00001032 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001033 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1034 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikov7bfc3ea2009-05-03 12:59:50 +00001035 }
1036}
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001037
Chris Lattner229907c2011-07-18 04:54:35 +00001038bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1039 Type *Ty2) const {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001040 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001041 return false;
1042
1043 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1044}
1045
1046bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1047 if (!VT1.isInteger() || !VT2.isInteger())
1048 return false;
1049
1050 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1051}
1052
Chris Lattner229907c2011-07-18 04:54:35 +00001053bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001054 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001055 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikova6450df2010-01-15 21:19:43 +00001056}
1057
1058bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1059 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1060 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1061}
1062
Eli Bendersky39e7c6e2012-12-18 18:21:29 +00001063bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1064 return isZExtFree(Val.getValueType(), VT2);
1065}
1066
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001067//===----------------------------------------------------------------------===//
1068// Other Lowering Code
1069//===----------------------------------------------------------------------===//
1070
1071MachineBasicBlock*
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001072MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001073 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001074 MachineFunction *F = BB->getParent();
1075 MachineRegisterInfo &RI = F->getRegInfo();
1076 DebugLoc dl = MI->getDebugLoc();
1077 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1078
1079 unsigned Opc;
1080 const TargetRegisterClass * RC;
1081 switch (MI->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00001082 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001083 case MSP430::Shl8:
1084 Opc = MSP430::SHL8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001085 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001086 break;
1087 case MSP430::Shl16:
1088 Opc = MSP430::SHL16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001089 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001090 break;
1091 case MSP430::Sra8:
1092 Opc = MSP430::SAR8r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001093 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001094 break;
1095 case MSP430::Sra16:
1096 Opc = MSP430::SAR16r1;
Craig Topperc7242e02012-04-20 07:30:17 +00001097 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001098 break;
1099 case MSP430::Srl8:
1100 Opc = MSP430::SAR8r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001101 RC = &MSP430::GR8RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001102 break;
1103 case MSP430::Srl16:
1104 Opc = MSP430::SAR16r1c;
Craig Topperc7242e02012-04-20 07:30:17 +00001105 RC = &MSP430::GR16RegClass;
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001106 break;
1107 }
1108
1109 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1110 MachineFunction::iterator I = BB;
1111 ++I;
1112
1113 // Create loop block
1114 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1115 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1116
1117 F->insert(I, LoopBB);
1118 F->insert(I, RemBB);
1119
1120 // Update machine-CFG edges by transferring all successors of the current
1121 // block to the block containing instructions after shift.
Dan Gohman34396292010-07-06 20:24:04 +00001122 RemBB->splice(RemBB->begin(), BB,
1123 llvm::next(MachineBasicBlock::iterator(MI)),
1124 BB->end());
1125 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001126
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001127 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1128 BB->addSuccessor(LoopBB);
1129 BB->addSuccessor(RemBB);
1130 LoopBB->addSuccessor(RemBB);
1131 LoopBB->addSuccessor(LoopBB);
1132
Craig Topperc7242e02012-04-20 07:30:17 +00001133 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1134 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001135 unsigned ShiftReg = RI.createVirtualRegister(RC);
1136 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1137 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1138 unsigned SrcReg = MI->getOperand(1).getReg();
1139 unsigned DstReg = MI->getOperand(0).getReg();
1140
1141 // BB:
1142 // cmp 0, N
1143 // je RemBB
Anton Korobeynikovcefa7ad2010-01-15 01:29:49 +00001144 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1145 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001146 BuildMI(BB, dl, TII.get(MSP430::JCC))
1147 .addMBB(RemBB)
1148 .addImm(MSP430CC::COND_E);
1149
1150 // LoopBB:
1151 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1152 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1153 // ShiftReg2 = shift ShiftReg
1154 // ShiftAmt2 = ShiftAmt - 1;
1155 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1156 .addReg(SrcReg).addMBB(BB)
1157 .addReg(ShiftReg2).addMBB(LoopBB);
1158 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1159 .addReg(ShiftAmtSrcReg).addMBB(BB)
1160 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1161 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1162 .addReg(ShiftReg);
1163 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1164 .addReg(ShiftAmtReg).addImm(1);
1165 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1166 .addMBB(LoopBB)
1167 .addImm(MSP430CC::COND_NE);
1168
1169 // RemBB:
1170 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman34396292010-07-06 20:24:04 +00001171 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001172 .addReg(SrcReg).addMBB(BB)
1173 .addReg(ShiftReg2).addMBB(LoopBB);
1174
Dan Gohman34396292010-07-06 20:24:04 +00001175 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001176 return RemBB;
1177}
1178
1179MachineBasicBlock*
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001180MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001181 MachineBasicBlock *BB) const {
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001182 unsigned Opc = MI->getOpcode();
1183
1184 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1185 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1186 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohman25c16532010-05-01 00:01:06 +00001187 return EmitShiftInstr(MI, BB);
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001188
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001189 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1190 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovd8f32092009-12-12 18:55:37 +00001191
1192 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001193 "Unexpected instr type to insert");
1194
1195 // To "insert" a SELECT instruction, we actually have to insert the diamond
1196 // control-flow pattern. The incoming instruction knows the destination vreg
1197 // to set, the condition code register to branch on, the true/false values to
1198 // select between, and a branch opcode to use.
1199 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1200 MachineFunction::iterator I = BB;
1201 ++I;
1202
1203 // thisMBB:
1204 // ...
1205 // TrueVal = ...
1206 // cmpTY ccX, r1, r2
1207 // jCC copy1MBB
1208 // fallthrough --> copy0MBB
1209 MachineBasicBlock *thisMBB = BB;
1210 MachineFunction *F = BB->getParent();
1211 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1212 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001213 F->insert(I, copy0MBB);
1214 F->insert(I, copy1MBB);
1215 // Update machine-CFG edges by transferring all successors of the current
1216 // block to the new block which will contain the Phi node for the select.
Dan Gohman34396292010-07-06 20:24:04 +00001217 copy1MBB->splice(copy1MBB->begin(), BB,
1218 llvm::next(MachineBasicBlock::iterator(MI)),
1219 BB->end());
1220 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001221 // Next, add the true and fallthrough blocks as its successors.
1222 BB->addSuccessor(copy0MBB);
1223 BB->addSuccessor(copy1MBB);
1224
Dan Gohman34396292010-07-06 20:24:04 +00001225 BuildMI(BB, dl, TII.get(MSP430::JCC))
1226 .addMBB(copy1MBB)
1227 .addImm(MI->getOperand(3).getImm());
1228
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001229 // copy0MBB:
1230 // %FalseValue = ...
1231 // # fallthrough to copy1MBB
1232 BB = copy0MBB;
1233
1234 // Update machine-CFG edges
1235 BB->addSuccessor(copy1MBB);
1236
1237 // copy1MBB:
1238 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1239 // ...
1240 BB = copy1MBB;
Dan Gohman34396292010-07-06 20:24:04 +00001241 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001242 MI->getOperand(0).getReg())
1243 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1244 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1245
Dan Gohman34396292010-07-06 20:24:04 +00001246 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikovb6321e152009-05-03 13:12:23 +00001247 return BB;
1248}