blob: 5a156c15c7af7faebdc24d623293dd054baf200a [file] [log] [blame]
Anton Korobeynikovf2c3e172009-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 Korobeynikov06ccca52009-12-07 02:28:10 +000018#include "MSP430MachineFunctionInfo.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000019#include "MSP430Subtarget.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "MSP430TargetMachine.h"
Anton Korobeynikovf2c3e172009-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 Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000028#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-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 Korobeynikovb2de1ea2009-12-07 02:27:08 +000035#include "llvm/Support/CommandLine.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000036#include "llvm/Support/Debug.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000037#include "llvm/Support/ErrorHandling.h"
Chris Lattner4437ae22009-08-23 07:05:07 +000038#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000039using namespace llvm;
40
Anton Korobeynikovb2de1ea2009-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 Korobeynikovf2c3e172009-05-03 12:57:15 +000060MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000061 TargetLowering(tm, new TargetLoweringObjectFileELF()),
Benjamin Kramera7542d52012-06-06 18:25:08 +000062 Subtarget(*tm.getSubtargetImpl()) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000063
Micah Villmow3574eca2012-10-08 16:38:25 +000064 TD = getDataLayout();
Anton Korobeynikov06ccca52009-12-07 02:28:10 +000065
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000066 // Set up the register classes.
Craig Topper420761a2012-04-20 07:30:17 +000067 addRegisterClass(MVT::i8, &MSP430::GR8RegClass);
68 addRegisterClass(MVT::i16, &MSP430::GR16RegClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000069
70 // Compute derived properties from the register classes
71 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000072
Anton Korobeynikov1476d972009-05-03 13:03:14 +000073 // Provide all sorts of operation actions
74
75 // Division is expensive
76 setIntDivIsCheap(false);
77
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000078 setStackPointerRegisterToSaveRestore(MSP430::SPW);
79 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000080 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000081
Anton Korobeynikov06ac0822009-11-07 17:15:25 +000082 // We have post-incremented loads / stores.
Anton Korobeynikov6534f832009-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 Anderson825b72b2009-08-11 20:47:22 +000090 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000091
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000092 // We don't have any truncstores
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000094
Owen Anderson825b72b2009-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 Korobeynikov69d5b482010-05-01 12:04:32 +0000107 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson825b72b2009-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 Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000112 setOperationAction(ISD::SETCC, MVT::i8, Custom);
113 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Owen Anderson825b72b2009-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 Korobeynikov379a0872009-08-25 17:00:23 +0000119 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
120 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000121
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
123 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000124 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Expand);
125 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
127 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000128 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Expand);
129 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
131 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000132
Owen Anderson825b72b2009-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 Friedmane4ce8802009-07-17 07:28:06 +0000139
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000141
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000142 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikov8983da72009-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 Anderson825b72b2009-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 Korobeynikovf2f54022009-05-03 13:18:33 +0000153
Anton Korobeynikov8983da72009-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 Anderson825b72b2009-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 Korobeynikovb2de1ea2009-12-07 02:27:08 +0000166
Anton Korobeynikov0ae61242012-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 Korobeynikovb2de1ea2009-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 Friedmanfc5d3052011-05-06 20:34:06 +0000181
182 setMinFunctionAlignment(1);
183 setPrefFunctionAlignment(2);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000184}
185
Dan Gohmand858e902010-04-17 15:26:15 +0000186SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
187 SelectionDAG &DAG) const {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000188 switch (Op.getOpcode()) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000189 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000190 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000191 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000192 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000193 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000194 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000195 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov1bb8cd72009-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 Korobeynikovb78e2142009-05-03 13:17:49 +0000198 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000199 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
200 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov0ae61242012-11-21 17:28:27 +0000201 case ISD::VASTART: return LowerVASTART(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000202 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000203 llvm_unreachable("unimplemented operand");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000204 }
205}
206
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000207//===----------------------------------------------------------------------===//
Anton Korobeynikovcd761282009-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 Topper420761a2012-04-20 07:30:17 +0000236 return std::make_pair(0U, &MSP430::GR8RegClass);
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000237
Craig Topper420761a2012-04-20 07:30:17 +0000238 return std::make_pair(0U, &MSP430::GR16RegClass);
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000239 }
240 }
241
242 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
243}
244
245//===----------------------------------------------------------------------===//
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000246// Calling Convention Implementation
247//===----------------------------------------------------------------------===//
248
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000249#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000250
Dan Gohman98ca4f22009-08-05 01:29:28 +0000251SDValue
252MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000253 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000254 bool isVarArg,
255 const SmallVectorImpl<ISD::InputArg>
256 &Ins,
257 DebugLoc dl,
258 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000259 SmallVectorImpl<SDValue> &InVals)
260 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000261
262 switch (CallConv) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000263 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000264 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000265 case CallingConv::C:
266 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000267 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000268 case CallingConv::MSP430_INTR:
David Blaikie4d6ccb52012-01-20 21:51:11 +0000269 if (Ins.empty())
270 return Chain;
Chris Lattner75361b62010-04-07 22:58:41 +0000271 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000272 }
273}
274
Dan Gohman98ca4f22009-08-05 01:29:28 +0000275SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000276MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +0000277 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000278 SelectionDAG &DAG = CLI.DAG;
279 DebugLoc &dl = CLI.DL;
280 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 Cheng0c439eb2010-01-27 00:07:07 +0000289 // MSP430 target does not yet support tail call optimization.
290 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000291
292 switch (CallConv) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000293 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000294 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000295 case CallingConv::Fast:
296 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000297 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +0000298 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000299 case CallingConv::MSP430_INTR:
Chris Lattner75361b62010-04-07 22:58:41 +0000300 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000301 }
302}
303
Anton Korobeynikovc8fbb6a2009-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 Gohman98ca4f22009-08-05 01:29:28 +0000307SDValue
308MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000309 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000310 bool isVarArg,
311 const SmallVectorImpl<ISD::InputArg>
312 &Ins,
313 DebugLoc dl,
314 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000315 SmallVectorImpl<SDValue> &InVals)
316 const {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000317 MachineFunction &MF = DAG.getMachineFunction();
318 MachineFrameInfo *MFI = MF.getFrameInfo();
319 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov0ae61242012-11-21 17:28:27 +0000320 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000321
322 // Assign locations to all of the incoming arguments.
323 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000324 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000325 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000326 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000327
Anton Korobeynikov0ae61242012-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 Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000333
Anton Korobeynikovc8fbb6a2009-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 Andersone50ed302009-08-10 22:56:29 +0000338 EVT RegVT = VA.getLocVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Anderson95771af2011-02-25 21:41:48 +0000340 default:
Torok Edwin804e0fe2009-07-08 19:04:27 +0000341 {
Torok Edwindac237e2009-07-08 20:53:28 +0000342#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000343 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000345#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000346 llvm_unreachable(0);
Torok Edwin804e0fe2009-07-08 19:04:27 +0000347 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 case MVT::i16:
Craig Topper420761a2012-04-20 07:30:17 +0000349 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000350 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000351 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikovc8fbb6a2009-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 Gohman98ca4f22009-08-05 01:29:28 +0000366 InVals.push_back(ArgValue);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000367 }
368 } else {
369 // Sanity check
370 assert(VA.isMemLoc());
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000371
Anton Korobeynikov6cbeb4d2012-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 Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000399 }
400 }
401
Dan Gohman98ca4f22009-08-05 01:29:28 +0000402 return Chain;
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000403}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000404
Dan Gohman98ca4f22009-08-05 01:29:28 +0000405SDValue
406MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000407 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000408 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000409 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +0000410 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000411
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000412 // CCValAssign - represent the assignment of the return value to a location
413 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000414
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000415 // ISRs cannot return any value.
David Blaikie4d6ccb52012-01-20 21:51:11 +0000416 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
Chris Lattner75361b62010-04-07 22:58:41 +0000417 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000418
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000419 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +0000420 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000421 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000422
Dan Gohman98ca4f22009-08-05 01:29:28 +0000423 // Analize return values.
424 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000425
426 // If this is the first return lowered for this function, add the regs to the
427 // liveout set for the function.
428 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
429 for (unsigned i = 0; i != RVLocs.size(); ++i)
430 if (RVLocs[i].isRegLoc())
431 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
432 }
433
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000434 SDValue Flag;
435
436 // Copy the result values into the output registers.
437 for (unsigned i = 0; i != RVLocs.size(); ++i) {
438 CCValAssign &VA = RVLocs[i];
439 assert(VA.isRegLoc() && "Can only return in registers!");
440
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000441 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000442 OutVals[i], Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000443
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000444 // Guarantee that all emitted copies are stuck together,
445 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000446 Flag = Chain.getValue(1);
447 }
448
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000449 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
450 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
451
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000452 if (Flag.getNode())
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000453 return DAG.getNode(Opc, dl, MVT::Other, Chain, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000454
455 // Return Void
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000456 return DAG.getNode(Opc, dl, MVT::Other, Chain);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000457}
458
Anton Korobeynikov44288852009-05-03 13:07:31 +0000459/// LowerCCCCallTo - functions arguments are copied from virtual regs to
460/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
461/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000462SDValue
463MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000464 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000465 bool isTailCall,
466 const SmallVectorImpl<ISD::OutputArg>
467 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000468 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000469 const SmallVectorImpl<ISD::InputArg> &Ins,
470 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000471 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000472 // Analyze operands of the call, assigning locations to each operand.
473 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000474 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000475 getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000476
Dan Gohman98ca4f22009-08-05 01:29:28 +0000477 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000478
479 // Get a count of how many bytes are to be pushed on the stack.
480 unsigned NumBytes = CCInfo.getNextStackOffset();
481
482 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
483 getPointerTy(), true));
484
485 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
486 SmallVector<SDValue, 12> MemOpChains;
487 SDValue StackPtr;
488
489 // Walk the register/memloc assignments, inserting copies/loads.
490 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
491 CCValAssign &VA = ArgLocs[i];
492
Dan Gohmanc9403652010-07-07 15:54:55 +0000493 SDValue Arg = OutVals[i];
Anton Korobeynikov44288852009-05-03 13:07:31 +0000494
495 // Promote the value if needed.
496 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000497 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000498 case CCValAssign::Full: break;
499 case CCValAssign::SExt:
500 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
501 break;
502 case CCValAssign::ZExt:
503 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
504 break;
505 case CCValAssign::AExt:
506 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
507 break;
508 }
509
510 // Arguments that can be passed on register must be kept at RegsToPass
511 // vector
512 if (VA.isRegLoc()) {
513 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
514 } else {
515 assert(VA.isMemLoc());
516
517 if (StackPtr.getNode() == 0)
518 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
519
520 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
521 StackPtr,
522 DAG.getIntPtrConstant(VA.getLocMemOffset()));
523
Anton Korobeynikov6cbeb4d2012-11-21 17:23:03 +0000524 SDValue MemOp;
525 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000526
Anton Korobeynikov6cbeb4d2012-11-21 17:23:03 +0000527 if (Flags.isByVal()) {
528 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16);
529 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
530 Flags.getByValAlign(),
531 /*isVolatile*/false,
532 /*AlwaysInline=*/true,
533 MachinePointerInfo(),
534 MachinePointerInfo());
535 } else {
536 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(),
537 false, false, 0);
538 }
539
540 MemOpChains.push_back(MemOp);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000541 }
542 }
543
544 // Transform all store nodes into one single node because all store nodes are
545 // independent of each other.
546 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov44288852009-05-03 13:07:31 +0000548 &MemOpChains[0], MemOpChains.size());
549
550 // Build a sequence of copy-to-reg nodes chained together with token chain and
551 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000552 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov44288852009-05-03 13:07:31 +0000553 SDValue InFlag;
554 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
555 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
556 RegsToPass[i].second, InFlag);
557 InFlag = Chain.getValue(1);
558 }
559
560 // If the callee is a GlobalAddress node (quite common, every direct call is)
561 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
562 // Likewise ExternalSymbol -> TargetExternalSymbol.
563 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000564 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000565 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000567
568 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000569 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000570 SmallVector<SDValue, 8> Ops;
571 Ops.push_back(Chain);
572 Ops.push_back(Callee);
573
574 // Add argument registers to the end of the list so that they are
575 // known live into the call.
576 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
577 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
578 RegsToPass[i].second.getValueType()));
579
580 if (InFlag.getNode())
581 Ops.push_back(InFlag);
582
583 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
584 InFlag = Chain.getValue(1);
585
586 // Create the CALLSEQ_END node.
587 Chain = DAG.getCALLSEQ_END(Chain,
588 DAG.getConstant(NumBytes, getPointerTy(), true),
589 DAG.getConstant(0, getPointerTy(), true),
590 InFlag);
591 InFlag = Chain.getValue(1);
592
593 // Handle result values, copying them out of physregs into vregs that we
594 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000595 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
596 DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000597}
598
Dan Gohman98ca4f22009-08-05 01:29:28 +0000599/// LowerCallResult - Lower the result values of a call into the
600/// appropriate copies out of appropriate physical registers.
601///
602SDValue
Anton Korobeynikov44288852009-05-03 13:07:31 +0000603MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000604 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000605 const SmallVectorImpl<ISD::InputArg> &Ins,
606 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000607 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000608
609 // Assign locations to each value returned by this call.
610 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000611 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000612 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000613
Dan Gohman98ca4f22009-08-05 01:29:28 +0000614 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000615
616 // Copy all of the result registers out of their specified physreg.
617 for (unsigned i = 0; i != RVLocs.size(); ++i) {
618 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
619 RVLocs[i].getValVT(), InFlag).getValue(1);
620 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000621 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000622 }
623
Dan Gohman98ca4f22009-08-05 01:29:28 +0000624 return Chain;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000625}
626
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000627SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000628 SelectionDAG &DAG) const {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000629 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000630 SDNode* N = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000631 EVT VT = Op.getValueType();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000632 DebugLoc dl = N->getDebugLoc();
633
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000634 // Expand non-constant shifts to loops:
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000635 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000636 switch (Opc) {
Craig Topperbc219812012-02-07 02:50:20 +0000637 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000638 case ISD::SHL:
639 return DAG.getNode(MSP430ISD::SHL, dl,
640 VT, N->getOperand(0), N->getOperand(1));
641 case ISD::SRA:
642 return DAG.getNode(MSP430ISD::SRA, dl,
643 VT, N->getOperand(0), N->getOperand(1));
644 case ISD::SRL:
645 return DAG.getNode(MSP430ISD::SRL, dl,
646 VT, N->getOperand(0), N->getOperand(1));
647 }
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000648
649 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
650
651 // Expand the stuff into sequence of shifts.
652 // FIXME: for some shift amounts this might be done better!
653 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
654 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000655
656 if (Opc == ISD::SRL && ShiftAmount) {
657 // Emit a special goodness here:
658 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000659 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000660 ShiftAmount -= 1;
661 }
662
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000663 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000664 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000665 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000666
667 return Victim;
668}
669
Dan Gohmand858e902010-04-17 15:26:15 +0000670SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
671 SelectionDAG &DAG) const {
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000672 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
673 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
674
675 // Create the TargetGlobalAddress node, folding in the constant offset.
Devang Patel0d881da2010-07-06 22:08:15 +0000676 SDValue Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
677 getPointerTy(), Offset);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000678 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
679 getPointerTy(), Result);
680}
681
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000682SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000683 SelectionDAG &DAG) const {
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000684 DebugLoc dl = Op.getDebugLoc();
685 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
686 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
687
Chad Rosier90f20042012-02-22 17:25:00 +0000688 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000689}
690
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000691SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
692 SelectionDAG &DAG) const {
693 DebugLoc dl = Op.getDebugLoc();
694 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000695 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000696
Chad Rosier90f20042012-02-22 17:25:00 +0000697 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000698}
699
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000700static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000701 ISD::CondCode CC,
702 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000703 // FIXME: Handle bittests someday
704 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
705
706 // FIXME: Handle jump negative someday
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000707 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000708 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000709 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000710 case ISD::SETEQ:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000711 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000712 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000713 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000714 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000715 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000716 break;
717 case ISD::SETNE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000718 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000719 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000720 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000721 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000722 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000723 break;
724 case ISD::SETULE:
725 std::swap(LHS, RHS); // FALLTHROUGH
726 case ISD::SETUGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000727 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
728 // fold constant into instruction.
729 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
730 LHS = RHS;
731 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
732 TCC = MSP430CC::COND_LO;
733 break;
734 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000735 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000736 break;
737 case ISD::SETUGT:
738 std::swap(LHS, RHS); // FALLTHROUGH
739 case ISD::SETULT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000740 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
741 // fold constant into instruction.
742 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
743 LHS = RHS;
744 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
745 TCC = MSP430CC::COND_HS;
746 break;
747 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000748 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000749 break;
750 case ISD::SETLE:
751 std::swap(LHS, RHS); // FALLTHROUGH
752 case ISD::SETGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000753 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
754 // fold constant into instruction.
755 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
756 LHS = RHS;
757 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
758 TCC = MSP430CC::COND_L;
759 break;
760 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000761 TCC = MSP430CC::COND_GE;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000762 break;
763 case ISD::SETGT:
764 std::swap(LHS, RHS); // FALLTHROUGH
765 case ISD::SETLT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000766 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
767 // fold constant into instruction.
768 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
769 LHS = RHS;
770 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
771 TCC = MSP430CC::COND_GE;
772 break;
773 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000774 TCC = MSP430CC::COND_L;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000775 break;
776 }
777
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000778 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000779 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000780}
781
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000782
Dan Gohmand858e902010-04-17 15:26:15 +0000783SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000784 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000785 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
786 SDValue LHS = Op.getOperand(2);
787 SDValue RHS = Op.getOperand(3);
788 SDValue Dest = Op.getOperand(4);
789 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000790
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000791 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000792 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000793
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000794 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000795 Chain, Dest, TargetCC, Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000796}
797
Dan Gohmand858e902010-04-17 15:26:15 +0000798SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000799 SDValue LHS = Op.getOperand(0);
800 SDValue RHS = Op.getOperand(1);
801 DebugLoc dl = Op.getDebugLoc();
802
803 // If we are doing an AND and testing against zero, then the CMP
804 // will not be generated. The AND (or BIT) will generate the condition codes,
805 // but they are different from CMP.
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000806 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
807 // lowering & isel wouldn't diverge.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000808 bool andCC = false;
809 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
810 if (RHSC->isNullValue() && LHS.hasOneUse() &&
811 (LHS.getOpcode() == ISD::AND ||
812 (LHS.getOpcode() == ISD::TRUNCATE &&
813 LHS.getOperand(0).getOpcode() == ISD::AND))) {
814 andCC = true;
815 }
816 }
817 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
818 SDValue TargetCC;
819 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
820
821 // Get the condition codes directly from the status register, if its easy.
822 // Otherwise a branch will be generated. Note that the AND and BIT
823 // instructions generate different flags than CMP, the carry bit can be used
824 // for NE/EQ.
825 bool Invert = false;
826 bool Shift = false;
827 bool Convert = true;
828 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
829 default:
830 Convert = false;
831 break;
832 case MSP430CC::COND_HS:
833 // Res = SRW & 1, no processing is required
834 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000835 case MSP430CC::COND_LO:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000836 // Res = ~(SRW & 1)
837 Invert = true;
838 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000839 case MSP430CC::COND_NE:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000840 if (andCC) {
841 // C = ~Z, thus Res = SRW & 1, no processing is required
842 } else {
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000843 // Res = ~((SRW >> 1) & 1)
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000844 Shift = true;
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000845 Invert = true;
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000846 }
847 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000848 case MSP430CC::COND_E:
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000849 Shift = true;
850 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
851 // Res = (SRW >> 1) & 1 is 1 word shorter.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000852 break;
853 }
854 EVT VT = Op.getValueType();
855 SDValue One = DAG.getConstant(1, VT);
856 if (Convert) {
857 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000858 MVT::i16, Flag);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000859 if (Shift)
860 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
861 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
862 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
863 if (Invert)
864 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
865 return SR;
866 } else {
867 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000868 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000869 SmallVector<SDValue, 4> Ops;
870 Ops.push_back(One);
871 Ops.push_back(Zero);
872 Ops.push_back(TargetCC);
873 Ops.push_back(Flag);
874 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
875 }
876}
877
Dan Gohmand858e902010-04-17 15:26:15 +0000878SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
879 SelectionDAG &DAG) const {
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000880 SDValue LHS = Op.getOperand(0);
881 SDValue RHS = Op.getOperand(1);
882 SDValue TrueV = Op.getOperand(2);
883 SDValue FalseV = Op.getOperand(3);
884 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000885 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000886
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000887 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000888 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000889
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000890 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000891 SmallVector<SDValue, 4> Ops;
892 Ops.push_back(TrueV);
893 Ops.push_back(FalseV);
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000894 Ops.push_back(TargetCC);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000895 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000896
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000897 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000898}
899
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000900SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000901 SelectionDAG &DAG) const {
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000902 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000903 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000904 DebugLoc dl = Op.getDebugLoc();
905
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000907
908 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
909 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
910 DAG.getValueType(Val.getValueType()));
911}
912
Dan Gohmand858e902010-04-17 15:26:15 +0000913SDValue
914MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000915 MachineFunction &MF = DAG.getMachineFunction();
916 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
917 int ReturnAddrIndex = FuncInfo->getRAIndex();
918
919 if (ReturnAddrIndex == 0) {
920 // Set up a frame object for the return address.
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000921 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000922 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +0000923 true);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000924 FuncInfo->setRAIndex(ReturnAddrIndex);
925 }
926
927 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
928}
929
Dan Gohmand858e902010-04-17 15:26:15 +0000930SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
931 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +0000932 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
933 MFI->setReturnAddressIsTaken(true);
934
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000935 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
936 DebugLoc dl = Op.getDebugLoc();
937
938 if (Depth > 0) {
939 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
940 SDValue Offset =
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000941 DAG.getConstant(TD->getPointerSize(), MVT::i16);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000942 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
943 DAG.getNode(ISD::ADD, dl, getPointerTy(),
944 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000945 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000946 }
947
948 // Just load the return address.
949 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
950 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000951 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000952}
953
Dan Gohmand858e902010-04-17 15:26:15 +0000954SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
955 SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000956 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
957 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000958
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000959 EVT VT = Op.getValueType();
960 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
961 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
962 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
963 MSP430::FPW, VT);
964 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000965 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
966 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000967 false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000968 return FrameAddr;
969}
970
Anton Korobeynikov0ae61242012-11-21 17:28:27 +0000971SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
972 SelectionDAG &DAG) const {
973 MachineFunction &MF = DAG.getMachineFunction();
974 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
975
976 // Frame index of first vararg argument
977 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
978 getPointerTy());
979 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
980
981 // Create a store of the frame index to the location operand
982 return DAG.getStore(Op.getOperand(0), Op.getDebugLoc(), FrameIndex,
983 Op.getOperand(1), MachinePointerInfo(SV),
984 false, false, 0);
985}
986
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000987/// getPostIndexedAddressParts - returns true by value, base pointer and
988/// offset pointer and addressing mode by reference if this node can be
989/// combined with a load / store to form a post-indexed load / store.
990bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
991 SDValue &Base,
992 SDValue &Offset,
993 ISD::MemIndexedMode &AM,
994 SelectionDAG &DAG) const {
995
996 LoadSDNode *LD = cast<LoadSDNode>(N);
997 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
998 return false;
999
1000 EVT VT = LD->getMemoryVT();
1001 if (VT != MVT::i8 && VT != MVT::i16)
1002 return false;
1003
1004 if (Op->getOpcode() != ISD::ADD)
1005 return false;
1006
1007 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1008 uint64_t RHSC = RHS->getZExtValue();
1009 if ((VT == MVT::i16 && RHSC != 2) ||
1010 (VT == MVT::i8 && RHSC != 1))
1011 return false;
1012
1013 Base = Op->getOperand(0);
1014 Offset = DAG.getConstant(RHSC, VT);
1015 AM = ISD::POST_INC;
1016 return true;
1017 }
1018
1019 return false;
1020}
1021
1022
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +00001023const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
1024 switch (Opcode) {
1025 default: return NULL;
1026 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov6bfcba72009-12-07 02:28:41 +00001027 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +00001028 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +00001029 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1030 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +00001031 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +00001032 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +00001033 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +00001034 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +00001035 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001036 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1037 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +00001038 }
1039}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001040
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001041bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1042 Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001043 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001044 return false;
1045
1046 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1047}
1048
1049bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1050 if (!VT1.isInteger() || !VT2.isInteger())
1051 return false;
1052
1053 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1054}
1055
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001056bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001057 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001058 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001059}
1060
1061bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1062 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1063 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1064}
1065
Eli Bendersky968b6672012-12-18 18:21:29 +00001066bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1067 return isZExtFree(Val.getValueType(), VT2);
1068}
1069
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001070//===----------------------------------------------------------------------===//
1071// Other Lowering Code
1072//===----------------------------------------------------------------------===//
1073
1074MachineBasicBlock*
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001075MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001076 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001077 MachineFunction *F = BB->getParent();
1078 MachineRegisterInfo &RI = F->getRegInfo();
1079 DebugLoc dl = MI->getDebugLoc();
1080 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1081
1082 unsigned Opc;
1083 const TargetRegisterClass * RC;
1084 switch (MI->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00001085 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001086 case MSP430::Shl8:
1087 Opc = MSP430::SHL8r1;
Craig Topper420761a2012-04-20 07:30:17 +00001088 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001089 break;
1090 case MSP430::Shl16:
1091 Opc = MSP430::SHL16r1;
Craig Topper420761a2012-04-20 07:30:17 +00001092 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001093 break;
1094 case MSP430::Sra8:
1095 Opc = MSP430::SAR8r1;
Craig Topper420761a2012-04-20 07:30:17 +00001096 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001097 break;
1098 case MSP430::Sra16:
1099 Opc = MSP430::SAR16r1;
Craig Topper420761a2012-04-20 07:30:17 +00001100 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001101 break;
1102 case MSP430::Srl8:
1103 Opc = MSP430::SAR8r1c;
Craig Topper420761a2012-04-20 07:30:17 +00001104 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001105 break;
1106 case MSP430::Srl16:
1107 Opc = MSP430::SAR16r1c;
Craig Topper420761a2012-04-20 07:30:17 +00001108 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001109 break;
1110 }
1111
1112 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1113 MachineFunction::iterator I = BB;
1114 ++I;
1115
1116 // Create loop block
1117 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1118 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1119
1120 F->insert(I, LoopBB);
1121 F->insert(I, RemBB);
1122
1123 // Update machine-CFG edges by transferring all successors of the current
1124 // block to the block containing instructions after shift.
Dan Gohman14152b42010-07-06 20:24:04 +00001125 RemBB->splice(RemBB->begin(), BB,
1126 llvm::next(MachineBasicBlock::iterator(MI)),
1127 BB->end());
1128 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001129
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001130 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1131 BB->addSuccessor(LoopBB);
1132 BB->addSuccessor(RemBB);
1133 LoopBB->addSuccessor(RemBB);
1134 LoopBB->addSuccessor(LoopBB);
1135
Craig Topper420761a2012-04-20 07:30:17 +00001136 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1137 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001138 unsigned ShiftReg = RI.createVirtualRegister(RC);
1139 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1140 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1141 unsigned SrcReg = MI->getOperand(1).getReg();
1142 unsigned DstReg = MI->getOperand(0).getReg();
1143
1144 // BB:
1145 // cmp 0, N
1146 // je RemBB
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +00001147 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1148 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001149 BuildMI(BB, dl, TII.get(MSP430::JCC))
1150 .addMBB(RemBB)
1151 .addImm(MSP430CC::COND_E);
1152
1153 // LoopBB:
1154 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1155 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1156 // ShiftReg2 = shift ShiftReg
1157 // ShiftAmt2 = ShiftAmt - 1;
1158 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1159 .addReg(SrcReg).addMBB(BB)
1160 .addReg(ShiftReg2).addMBB(LoopBB);
1161 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1162 .addReg(ShiftAmtSrcReg).addMBB(BB)
1163 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1164 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1165 .addReg(ShiftReg);
1166 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1167 .addReg(ShiftAmtReg).addImm(1);
1168 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1169 .addMBB(LoopBB)
1170 .addImm(MSP430CC::COND_NE);
1171
1172 // RemBB:
1173 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman14152b42010-07-06 20:24:04 +00001174 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001175 .addReg(SrcReg).addMBB(BB)
1176 .addReg(ShiftReg2).addMBB(LoopBB);
1177
Dan Gohman14152b42010-07-06 20:24:04 +00001178 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001179 return RemBB;
1180}
1181
1182MachineBasicBlock*
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001183MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001184 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001185 unsigned Opc = MI->getOpcode();
1186
1187 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1188 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1189 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001190 return EmitShiftInstr(MI, BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001191
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001192 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1193 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001194
1195 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001196 "Unexpected instr type to insert");
1197
1198 // To "insert" a SELECT instruction, we actually have to insert the diamond
1199 // control-flow pattern. The incoming instruction knows the destination vreg
1200 // to set, the condition code register to branch on, the true/false values to
1201 // select between, and a branch opcode to use.
1202 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1203 MachineFunction::iterator I = BB;
1204 ++I;
1205
1206 // thisMBB:
1207 // ...
1208 // TrueVal = ...
1209 // cmpTY ccX, r1, r2
1210 // jCC copy1MBB
1211 // fallthrough --> copy0MBB
1212 MachineBasicBlock *thisMBB = BB;
1213 MachineFunction *F = BB->getParent();
1214 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1215 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001216 F->insert(I, copy0MBB);
1217 F->insert(I, copy1MBB);
1218 // Update machine-CFG edges by transferring all successors of the current
1219 // block to the new block which will contain the Phi node for the select.
Dan Gohman14152b42010-07-06 20:24:04 +00001220 copy1MBB->splice(copy1MBB->begin(), BB,
1221 llvm::next(MachineBasicBlock::iterator(MI)),
1222 BB->end());
1223 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001224 // Next, add the true and fallthrough blocks as its successors.
1225 BB->addSuccessor(copy0MBB);
1226 BB->addSuccessor(copy1MBB);
1227
Dan Gohman14152b42010-07-06 20:24:04 +00001228 BuildMI(BB, dl, TII.get(MSP430::JCC))
1229 .addMBB(copy1MBB)
1230 .addImm(MI->getOperand(3).getImm());
1231
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001232 // copy0MBB:
1233 // %FalseValue = ...
1234 // # fallthrough to copy1MBB
1235 BB = copy0MBB;
1236
1237 // Update machine-CFG edges
1238 BB->addSuccessor(copy1MBB);
1239
1240 // copy1MBB:
1241 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1242 // ...
1243 BB = copy1MBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001244 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001245 MI->getOperand(0).getReg())
1246 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1247 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1248
Dan Gohman14152b42010-07-06 20:24:04 +00001249 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001250 return BB;
1251}