blob: 09cdf3268553bd314b8baf844cea1b80e96d3d4f [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
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000426 SDValue Flag;
Jakob Stoklund Olesen294014e2013-02-05 18:12:06 +0000427 SmallVector<SDValue, 4> RetOps(1, Chain);
Anton Korobeynikovfd1b7c72009-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 Korobeynikovfd1b7c72009-05-03 12:59:50 +0000434 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000435 OutVals[i], Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000436
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000437 // Guarantee that all emitted copies are stuck together,
438 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000439 Flag = Chain.getValue(1);
Jakob Stoklund Olesen294014e2013-02-05 18:12:06 +0000440 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000441 }
442
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000443 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
444 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
445
Jakob Stoklund Olesen294014e2013-02-05 18:12:06 +0000446 RetOps[0] = Chain; // Update chain.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000447
Jakob Stoklund Olesen294014e2013-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 Korobeynikovfd1b7c72009-05-03 12:59:50 +0000453}
454
Anton Korobeynikov44288852009-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 Gohman98ca4f22009-08-05 01:29:28 +0000458SDValue
459MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000460 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000461 bool isTailCall,
462 const SmallVectorImpl<ISD::OutputArg>
463 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000464 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000465 const SmallVectorImpl<ISD::InputArg> &Ins,
466 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000467 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000468 // Analyze operands of the call, assigning locations to each operand.
469 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000470 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000471 getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000472
Dan Gohman98ca4f22009-08-05 01:29:28 +0000473 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov44288852009-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,
479 getPointerTy(), true));
480
481 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
482 SmallVector<SDValue, 12> MemOpChains;
483 SDValue StackPtr;
484
485 // Walk the register/memloc assignments, inserting copies/loads.
486 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
487 CCValAssign &VA = ArgLocs[i];
488
Dan Gohmanc9403652010-07-07 15:54:55 +0000489 SDValue Arg = OutVals[i];
Anton Korobeynikov44288852009-05-03 13:07:31 +0000490
491 // Promote the value if needed.
492 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000493 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000494 case CCValAssign::Full: break;
495 case CCValAssign::SExt:
496 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
497 break;
498 case CCValAssign::ZExt:
499 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
500 break;
501 case CCValAssign::AExt:
502 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
503 break;
504 }
505
506 // Arguments that can be passed on register must be kept at RegsToPass
507 // vector
508 if (VA.isRegLoc()) {
509 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
510 } else {
511 assert(VA.isMemLoc());
512
513 if (StackPtr.getNode() == 0)
514 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
515
516 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
517 StackPtr,
518 DAG.getIntPtrConstant(VA.getLocMemOffset()));
519
Anton Korobeynikov6cbeb4d2012-11-21 17:23:03 +0000520 SDValue MemOp;
521 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000522
Anton Korobeynikov6cbeb4d2012-11-21 17:23:03 +0000523 if (Flags.isByVal()) {
524 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16);
525 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
526 Flags.getByValAlign(),
527 /*isVolatile*/false,
528 /*AlwaysInline=*/true,
529 MachinePointerInfo(),
530 MachinePointerInfo());
531 } else {
532 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(),
533 false, false, 0);
534 }
535
536 MemOpChains.push_back(MemOp);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000537 }
538 }
539
540 // Transform all store nodes into one single node because all store nodes are
541 // independent of each other.
542 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov44288852009-05-03 13:07:31 +0000544 &MemOpChains[0], MemOpChains.size());
545
546 // Build a sequence of copy-to-reg nodes chained together with token chain and
547 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000548 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov44288852009-05-03 13:07:31 +0000549 SDValue InFlag;
550 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
551 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
552 RegsToPass[i].second, InFlag);
553 InFlag = Chain.getValue(1);
554 }
555
556 // If the callee is a GlobalAddress node (quite common, every direct call is)
557 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
558 // Likewise ExternalSymbol -> TargetExternalSymbol.
559 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000560 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000561 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000563
564 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000565 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000566 SmallVector<SDValue, 8> Ops;
567 Ops.push_back(Chain);
568 Ops.push_back(Callee);
569
570 // Add argument registers to the end of the list so that they are
571 // known live into the call.
572 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
573 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
574 RegsToPass[i].second.getValueType()));
575
576 if (InFlag.getNode())
577 Ops.push_back(InFlag);
578
579 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
580 InFlag = Chain.getValue(1);
581
582 // Create the CALLSEQ_END node.
583 Chain = DAG.getCALLSEQ_END(Chain,
584 DAG.getConstant(NumBytes, getPointerTy(), true),
585 DAG.getConstant(0, getPointerTy(), true),
586 InFlag);
587 InFlag = Chain.getValue(1);
588
589 // Handle result values, copying them out of physregs into vregs that we
590 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000591 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
592 DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000593}
594
Dan Gohman98ca4f22009-08-05 01:29:28 +0000595/// LowerCallResult - Lower the result values of a call into the
596/// appropriate copies out of appropriate physical registers.
597///
598SDValue
Anton Korobeynikov44288852009-05-03 13:07:31 +0000599MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000600 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000601 const SmallVectorImpl<ISD::InputArg> &Ins,
602 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000603 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000604
605 // Assign locations to each value returned by this call.
606 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000607 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000608 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000609
Dan Gohman98ca4f22009-08-05 01:29:28 +0000610 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000611
612 // Copy all of the result registers out of their specified physreg.
613 for (unsigned i = 0; i != RVLocs.size(); ++i) {
614 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
615 RVLocs[i].getValVT(), InFlag).getValue(1);
616 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000617 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000618 }
619
Dan Gohman98ca4f22009-08-05 01:29:28 +0000620 return Chain;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000621}
622
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000623SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000624 SelectionDAG &DAG) const {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000625 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000626 SDNode* N = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000627 EVT VT = Op.getValueType();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000628 DebugLoc dl = N->getDebugLoc();
629
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000630 // Expand non-constant shifts to loops:
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000631 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000632 switch (Opc) {
Craig Topperbc219812012-02-07 02:50:20 +0000633 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000634 case ISD::SHL:
635 return DAG.getNode(MSP430ISD::SHL, dl,
636 VT, N->getOperand(0), N->getOperand(1));
637 case ISD::SRA:
638 return DAG.getNode(MSP430ISD::SRA, dl,
639 VT, N->getOperand(0), N->getOperand(1));
640 case ISD::SRL:
641 return DAG.getNode(MSP430ISD::SRL, dl,
642 VT, N->getOperand(0), N->getOperand(1));
643 }
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000644
645 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
646
647 // Expand the stuff into sequence of shifts.
648 // FIXME: for some shift amounts this might be done better!
649 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
650 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000651
652 if (Opc == ISD::SRL && ShiftAmount) {
653 // Emit a special goodness here:
654 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000655 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000656 ShiftAmount -= 1;
657 }
658
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000659 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000660 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000661 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000662
663 return Victim;
664}
665
Dan Gohmand858e902010-04-17 15:26:15 +0000666SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
667 SelectionDAG &DAG) const {
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000668 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
669 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
670
671 // Create the TargetGlobalAddress node, folding in the constant offset.
Devang Patel0d881da2010-07-06 22:08:15 +0000672 SDValue Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
673 getPointerTy(), Offset);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000674 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
675 getPointerTy(), Result);
676}
677
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000678SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000679 SelectionDAG &DAG) const {
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000680 DebugLoc dl = Op.getDebugLoc();
681 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
682 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
683
Chad Rosier90f20042012-02-22 17:25:00 +0000684 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000685}
686
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000687SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
688 SelectionDAG &DAG) const {
689 DebugLoc dl = Op.getDebugLoc();
690 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000691 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000692
Chad Rosier90f20042012-02-22 17:25:00 +0000693 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000694}
695
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000696static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000697 ISD::CondCode CC,
698 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000699 // FIXME: Handle bittests someday
700 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
701
702 // FIXME: Handle jump negative someday
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000703 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000704 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000705 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000706 case ISD::SETEQ:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000707 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000708 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000709 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000710 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000711 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000712 break;
713 case ISD::SETNE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000714 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000715 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000716 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000717 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000718 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000719 break;
720 case ISD::SETULE:
721 std::swap(LHS, RHS); // FALLTHROUGH
722 case ISD::SETUGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000723 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
724 // fold constant into instruction.
725 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
726 LHS = RHS;
727 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
728 TCC = MSP430CC::COND_LO;
729 break;
730 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000731 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000732 break;
733 case ISD::SETUGT:
734 std::swap(LHS, RHS); // FALLTHROUGH
735 case ISD::SETULT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000736 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
737 // fold constant into instruction.
738 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
739 LHS = RHS;
740 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
741 TCC = MSP430CC::COND_HS;
742 break;
743 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000744 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000745 break;
746 case ISD::SETLE:
747 std::swap(LHS, RHS); // FALLTHROUGH
748 case ISD::SETGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000749 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
750 // fold constant into instruction.
751 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
752 LHS = RHS;
753 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
754 TCC = MSP430CC::COND_L;
755 break;
756 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000757 TCC = MSP430CC::COND_GE;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000758 break;
759 case ISD::SETGT:
760 std::swap(LHS, RHS); // FALLTHROUGH
761 case ISD::SETLT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000762 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
763 // fold constant into instruction.
764 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
765 LHS = RHS;
766 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
767 TCC = MSP430CC::COND_GE;
768 break;
769 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000770 TCC = MSP430CC::COND_L;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000771 break;
772 }
773
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000774 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000775 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000776}
777
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000778
Dan Gohmand858e902010-04-17 15:26:15 +0000779SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000780 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000781 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
782 SDValue LHS = Op.getOperand(2);
783 SDValue RHS = Op.getOperand(3);
784 SDValue Dest = Op.getOperand(4);
785 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000786
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000787 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000788 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000789
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000790 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000791 Chain, Dest, TargetCC, Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000792}
793
Dan Gohmand858e902010-04-17 15:26:15 +0000794SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000795 SDValue LHS = Op.getOperand(0);
796 SDValue RHS = Op.getOperand(1);
797 DebugLoc dl = Op.getDebugLoc();
798
799 // If we are doing an AND and testing against zero, then the CMP
800 // will not be generated. The AND (or BIT) will generate the condition codes,
801 // but they are different from CMP.
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000802 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
803 // lowering & isel wouldn't diverge.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000804 bool andCC = false;
805 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
806 if (RHSC->isNullValue() && LHS.hasOneUse() &&
807 (LHS.getOpcode() == ISD::AND ||
808 (LHS.getOpcode() == ISD::TRUNCATE &&
809 LHS.getOperand(0).getOpcode() == ISD::AND))) {
810 andCC = true;
811 }
812 }
813 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
814 SDValue TargetCC;
815 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
816
817 // Get the condition codes directly from the status register, if its easy.
818 // Otherwise a branch will be generated. Note that the AND and BIT
819 // instructions generate different flags than CMP, the carry bit can be used
820 // for NE/EQ.
821 bool Invert = false;
822 bool Shift = false;
823 bool Convert = true;
824 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
825 default:
826 Convert = false;
827 break;
828 case MSP430CC::COND_HS:
829 // Res = SRW & 1, no processing is required
830 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000831 case MSP430CC::COND_LO:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000832 // Res = ~(SRW & 1)
833 Invert = true;
834 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000835 case MSP430CC::COND_NE:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000836 if (andCC) {
837 // C = ~Z, thus Res = SRW & 1, no processing is required
838 } else {
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000839 // Res = ~((SRW >> 1) & 1)
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000840 Shift = true;
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000841 Invert = true;
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000842 }
843 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000844 case MSP430CC::COND_E:
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000845 Shift = true;
846 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
847 // Res = (SRW >> 1) & 1 is 1 word shorter.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000848 break;
849 }
850 EVT VT = Op.getValueType();
851 SDValue One = DAG.getConstant(1, VT);
852 if (Convert) {
853 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000854 MVT::i16, Flag);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000855 if (Shift)
856 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
857 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
858 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
859 if (Invert)
860 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
861 return SR;
862 } else {
863 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000864 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000865 SmallVector<SDValue, 4> Ops;
866 Ops.push_back(One);
867 Ops.push_back(Zero);
868 Ops.push_back(TargetCC);
869 Ops.push_back(Flag);
870 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
871 }
872}
873
Dan Gohmand858e902010-04-17 15:26:15 +0000874SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
875 SelectionDAG &DAG) const {
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000876 SDValue LHS = Op.getOperand(0);
877 SDValue RHS = Op.getOperand(1);
878 SDValue TrueV = Op.getOperand(2);
879 SDValue FalseV = Op.getOperand(3);
880 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000881 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000882
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000883 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000884 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000885
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000886 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000887 SmallVector<SDValue, 4> Ops;
888 Ops.push_back(TrueV);
889 Ops.push_back(FalseV);
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000890 Ops.push_back(TargetCC);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000891 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000892
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000893 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000894}
895
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000896SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000897 SelectionDAG &DAG) const {
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000898 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000899 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000900 DebugLoc dl = Op.getDebugLoc();
901
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000903
904 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
905 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
906 DAG.getValueType(Val.getValueType()));
907}
908
Dan Gohmand858e902010-04-17 15:26:15 +0000909SDValue
910MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000911 MachineFunction &MF = DAG.getMachineFunction();
912 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
913 int ReturnAddrIndex = FuncInfo->getRAIndex();
914
915 if (ReturnAddrIndex == 0) {
916 // Set up a frame object for the return address.
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000917 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000918 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +0000919 true);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000920 FuncInfo->setRAIndex(ReturnAddrIndex);
921 }
922
923 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
924}
925
Dan Gohmand858e902010-04-17 15:26:15 +0000926SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
927 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +0000928 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
929 MFI->setReturnAddressIsTaken(true);
930
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000931 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
932 DebugLoc dl = Op.getDebugLoc();
933
934 if (Depth > 0) {
935 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
936 SDValue Offset =
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000937 DAG.getConstant(TD->getPointerSize(), MVT::i16);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000938 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
939 DAG.getNode(ISD::ADD, dl, getPointerTy(),
940 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000941 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000942 }
943
944 // Just load the return address.
945 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
946 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000947 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000948}
949
Dan Gohmand858e902010-04-17 15:26:15 +0000950SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
951 SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000952 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
953 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000954
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000955 EVT VT = Op.getValueType();
956 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
957 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
958 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
959 MSP430::FPW, VT);
960 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000961 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
962 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000963 false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000964 return FrameAddr;
965}
966
Anton Korobeynikov0ae61242012-11-21 17:28:27 +0000967SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
968 SelectionDAG &DAG) const {
969 MachineFunction &MF = DAG.getMachineFunction();
970 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
971
972 // Frame index of first vararg argument
973 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
974 getPointerTy());
975 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
976
977 // Create a store of the frame index to the location operand
978 return DAG.getStore(Op.getOperand(0), Op.getDebugLoc(), FrameIndex,
979 Op.getOperand(1), MachinePointerInfo(SV),
980 false, false, 0);
981}
982
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000983/// getPostIndexedAddressParts - returns true by value, base pointer and
984/// offset pointer and addressing mode by reference if this node can be
985/// combined with a load / store to form a post-indexed load / store.
986bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
987 SDValue &Base,
988 SDValue &Offset,
989 ISD::MemIndexedMode &AM,
990 SelectionDAG &DAG) const {
991
992 LoadSDNode *LD = cast<LoadSDNode>(N);
993 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
994 return false;
995
996 EVT VT = LD->getMemoryVT();
997 if (VT != MVT::i8 && VT != MVT::i16)
998 return false;
999
1000 if (Op->getOpcode() != ISD::ADD)
1001 return false;
1002
1003 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1004 uint64_t RHSC = RHS->getZExtValue();
1005 if ((VT == MVT::i16 && RHSC != 2) ||
1006 (VT == MVT::i8 && RHSC != 1))
1007 return false;
1008
1009 Base = Op->getOperand(0);
1010 Offset = DAG.getConstant(RHSC, VT);
1011 AM = ISD::POST_INC;
1012 return true;
1013 }
1014
1015 return false;
1016}
1017
1018
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +00001019const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
1020 switch (Opcode) {
1021 default: return NULL;
1022 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov6bfcba72009-12-07 02:28:41 +00001023 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +00001024 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +00001025 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1026 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +00001027 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +00001028 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +00001029 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +00001030 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +00001031 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001032 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1033 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +00001034 }
1035}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001036
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001037bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1038 Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001039 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001040 return false;
1041
1042 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1043}
1044
1045bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1046 if (!VT1.isInteger() || !VT2.isInteger())
1047 return false;
1048
1049 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1050}
1051
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001052bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001053 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001054 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001055}
1056
1057bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1058 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1059 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1060}
1061
Eli Bendersky968b6672012-12-18 18:21:29 +00001062bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1063 return isZExtFree(Val.getValueType(), VT2);
1064}
1065
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001066//===----------------------------------------------------------------------===//
1067// Other Lowering Code
1068//===----------------------------------------------------------------------===//
1069
1070MachineBasicBlock*
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001071MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001072 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001073 MachineFunction *F = BB->getParent();
1074 MachineRegisterInfo &RI = F->getRegInfo();
1075 DebugLoc dl = MI->getDebugLoc();
1076 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1077
1078 unsigned Opc;
1079 const TargetRegisterClass * RC;
1080 switch (MI->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00001081 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001082 case MSP430::Shl8:
1083 Opc = MSP430::SHL8r1;
Craig Topper420761a2012-04-20 07:30:17 +00001084 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001085 break;
1086 case MSP430::Shl16:
1087 Opc = MSP430::SHL16r1;
Craig Topper420761a2012-04-20 07:30:17 +00001088 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001089 break;
1090 case MSP430::Sra8:
1091 Opc = MSP430::SAR8r1;
Craig Topper420761a2012-04-20 07:30:17 +00001092 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001093 break;
1094 case MSP430::Sra16:
1095 Opc = MSP430::SAR16r1;
Craig Topper420761a2012-04-20 07:30:17 +00001096 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001097 break;
1098 case MSP430::Srl8:
1099 Opc = MSP430::SAR8r1c;
Craig Topper420761a2012-04-20 07:30:17 +00001100 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001101 break;
1102 case MSP430::Srl16:
1103 Opc = MSP430::SAR16r1c;
Craig Topper420761a2012-04-20 07:30:17 +00001104 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001105 break;
1106 }
1107
1108 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1109 MachineFunction::iterator I = BB;
1110 ++I;
1111
1112 // Create loop block
1113 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1114 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1115
1116 F->insert(I, LoopBB);
1117 F->insert(I, RemBB);
1118
1119 // Update machine-CFG edges by transferring all successors of the current
1120 // block to the block containing instructions after shift.
Dan Gohman14152b42010-07-06 20:24:04 +00001121 RemBB->splice(RemBB->begin(), BB,
1122 llvm::next(MachineBasicBlock::iterator(MI)),
1123 BB->end());
1124 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001125
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001126 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1127 BB->addSuccessor(LoopBB);
1128 BB->addSuccessor(RemBB);
1129 LoopBB->addSuccessor(RemBB);
1130 LoopBB->addSuccessor(LoopBB);
1131
Craig Topper420761a2012-04-20 07:30:17 +00001132 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1133 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001134 unsigned ShiftReg = RI.createVirtualRegister(RC);
1135 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1136 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1137 unsigned SrcReg = MI->getOperand(1).getReg();
1138 unsigned DstReg = MI->getOperand(0).getReg();
1139
1140 // BB:
1141 // cmp 0, N
1142 // je RemBB
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +00001143 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1144 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001145 BuildMI(BB, dl, TII.get(MSP430::JCC))
1146 .addMBB(RemBB)
1147 .addImm(MSP430CC::COND_E);
1148
1149 // LoopBB:
1150 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1151 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1152 // ShiftReg2 = shift ShiftReg
1153 // ShiftAmt2 = ShiftAmt - 1;
1154 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1155 .addReg(SrcReg).addMBB(BB)
1156 .addReg(ShiftReg2).addMBB(LoopBB);
1157 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1158 .addReg(ShiftAmtSrcReg).addMBB(BB)
1159 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1160 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1161 .addReg(ShiftReg);
1162 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1163 .addReg(ShiftAmtReg).addImm(1);
1164 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1165 .addMBB(LoopBB)
1166 .addImm(MSP430CC::COND_NE);
1167
1168 // RemBB:
1169 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman14152b42010-07-06 20:24:04 +00001170 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001171 .addReg(SrcReg).addMBB(BB)
1172 .addReg(ShiftReg2).addMBB(LoopBB);
1173
Dan Gohman14152b42010-07-06 20:24:04 +00001174 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001175 return RemBB;
1176}
1177
1178MachineBasicBlock*
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001179MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001180 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001181 unsigned Opc = MI->getOpcode();
1182
1183 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1184 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1185 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001186 return EmitShiftInstr(MI, BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001187
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001188 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1189 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001190
1191 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001192 "Unexpected instr type to insert");
1193
1194 // To "insert" a SELECT instruction, we actually have to insert the diamond
1195 // control-flow pattern. The incoming instruction knows the destination vreg
1196 // to set, the condition code register to branch on, the true/false values to
1197 // select between, and a branch opcode to use.
1198 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1199 MachineFunction::iterator I = BB;
1200 ++I;
1201
1202 // thisMBB:
1203 // ...
1204 // TrueVal = ...
1205 // cmpTY ccX, r1, r2
1206 // jCC copy1MBB
1207 // fallthrough --> copy0MBB
1208 MachineBasicBlock *thisMBB = BB;
1209 MachineFunction *F = BB->getParent();
1210 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1211 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001212 F->insert(I, copy0MBB);
1213 F->insert(I, copy1MBB);
1214 // Update machine-CFG edges by transferring all successors of the current
1215 // block to the new block which will contain the Phi node for the select.
Dan Gohman14152b42010-07-06 20:24:04 +00001216 copy1MBB->splice(copy1MBB->begin(), BB,
1217 llvm::next(MachineBasicBlock::iterator(MI)),
1218 BB->end());
1219 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001220 // Next, add the true and fallthrough blocks as its successors.
1221 BB->addSuccessor(copy0MBB);
1222 BB->addSuccessor(copy1MBB);
1223
Dan Gohman14152b42010-07-06 20:24:04 +00001224 BuildMI(BB, dl, TII.get(MSP430::JCC))
1225 .addMBB(copy1MBB)
1226 .addImm(MI->getOperand(3).getImm());
1227
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001228 // copy0MBB:
1229 // %FalseValue = ...
1230 // # fallthrough to copy1MBB
1231 BB = copy0MBB;
1232
1233 // Update machine-CFG edges
1234 BB->addSuccessor(copy1MBB);
1235
1236 // copy1MBB:
1237 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1238 // ...
1239 BB = copy1MBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001240 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001241 MI->getOperand(0).getReg())
1242 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1243 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1244
Dan Gohman14152b42010-07-06 20:24:04 +00001245 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001246 return BB;
1247}