blob: f8b7e149f0dbf717261d75613444b1669f859719 [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 "MSP430TargetMachine.h"
20#include "MSP430Subtarget.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
23#include "llvm/Intrinsics.h"
24#include "llvm/CallingConv.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/GlobalAlias.h"
27#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/SelectionDAGISel.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000034#include "llvm/CodeGen/ValueTypes.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
Anton Korobeynikov06ccca52009-12-07 02:28:10 +000064 TD = getTargetData();
65
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
167 // Libcalls names.
168 if (HWMultMode == HWMultIntr) {
169 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
170 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
171 } else if (HWMultMode == HWMultNoIntr) {
172 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
173 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
174 }
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000175
176 setMinFunctionAlignment(1);
177 setPrefFunctionAlignment(2);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000178}
179
Dan Gohmand858e902010-04-17 15:26:15 +0000180SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
181 SelectionDAG &DAG) const {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000182 switch (Op.getOpcode()) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000183 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000184 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000185 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000186 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000187 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000188 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000189 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000190 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
191 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000192 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000193 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
194 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000195 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000196 llvm_unreachable("unimplemented operand");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000197 }
198}
199
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000200//===----------------------------------------------------------------------===//
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000201// MSP430 Inline Assembly Support
202//===----------------------------------------------------------------------===//
203
204/// getConstraintType - Given a constraint letter, return the type of
205/// constraint it is for this target.
206TargetLowering::ConstraintType
207MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
208 if (Constraint.size() == 1) {
209 switch (Constraint[0]) {
210 case 'r':
211 return C_RegisterClass;
212 default:
213 break;
214 }
215 }
216 return TargetLowering::getConstraintType(Constraint);
217}
218
219std::pair<unsigned, const TargetRegisterClass*>
220MSP430TargetLowering::
221getRegForInlineAsmConstraint(const std::string &Constraint,
222 EVT VT) const {
223 if (Constraint.size() == 1) {
224 // GCC Constraint Letters
225 switch (Constraint[0]) {
226 default: break;
227 case 'r': // GENERAL_REGS
228 if (VT == MVT::i8)
Craig Topper420761a2012-04-20 07:30:17 +0000229 return std::make_pair(0U, &MSP430::GR8RegClass);
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000230
Craig Topper420761a2012-04-20 07:30:17 +0000231 return std::make_pair(0U, &MSP430::GR16RegClass);
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000232 }
233 }
234
235 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
236}
237
238//===----------------------------------------------------------------------===//
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000239// Calling Convention Implementation
240//===----------------------------------------------------------------------===//
241
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000242#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000243
Dan Gohman98ca4f22009-08-05 01:29:28 +0000244SDValue
245MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000246 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000247 bool isVarArg,
248 const SmallVectorImpl<ISD::InputArg>
249 &Ins,
250 DebugLoc dl,
251 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000252 SmallVectorImpl<SDValue> &InVals)
253 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000254
255 switch (CallConv) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000256 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000257 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000258 case CallingConv::C:
259 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000260 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000261 case CallingConv::MSP430_INTR:
David Blaikie4d6ccb52012-01-20 21:51:11 +0000262 if (Ins.empty())
263 return Chain;
Chris Lattner75361b62010-04-07 22:58:41 +0000264 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000265 }
266}
267
Dan Gohman98ca4f22009-08-05 01:29:28 +0000268SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000269MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +0000270 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000271 SelectionDAG &DAG = CLI.DAG;
272 DebugLoc &dl = CLI.DL;
273 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
274 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
275 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
276 SDValue Chain = CLI.Chain;
277 SDValue Callee = CLI.Callee;
278 bool &isTailCall = CLI.IsTailCall;
279 CallingConv::ID CallConv = CLI.CallConv;
280 bool isVarArg = CLI.IsVarArg;
281
Evan Cheng0c439eb2010-01-27 00:07:07 +0000282 // MSP430 target does not yet support tail call optimization.
283 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000284
285 switch (CallConv) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000286 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000287 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000288 case CallingConv::Fast:
289 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000290 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +0000291 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000292 case CallingConv::MSP430_INTR:
Chris Lattner75361b62010-04-07 22:58:41 +0000293 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000294 }
295}
296
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000297/// LowerCCCArguments - transform physical registers into virtual registers and
298/// generate load operations for arguments places on the stack.
299// FIXME: struct return stuff
300// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000301SDValue
302MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000303 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000304 bool isVarArg,
305 const SmallVectorImpl<ISD::InputArg>
306 &Ins,
307 DebugLoc dl,
308 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000309 SmallVectorImpl<SDValue> &InVals)
310 const {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000311 MachineFunction &MF = DAG.getMachineFunction();
312 MachineFrameInfo *MFI = MF.getFrameInfo();
313 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000314
315 // Assign locations to all of the incoming arguments.
316 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000317 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000318 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000319 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000320
321 assert(!isVarArg && "Varargs not supported yet");
322
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000323 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
324 CCValAssign &VA = ArgLocs[i];
325 if (VA.isRegLoc()) {
326 // Arguments passed in registers
Owen Andersone50ed302009-08-10 22:56:29 +0000327 EVT RegVT = VA.getLocVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Anderson95771af2011-02-25 21:41:48 +0000329 default:
Torok Edwin804e0fe2009-07-08 19:04:27 +0000330 {
Torok Edwindac237e2009-07-08 20:53:28 +0000331#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000332 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000334#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000335 llvm_unreachable(0);
Torok Edwin804e0fe2009-07-08 19:04:27 +0000336 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 case MVT::i16:
Craig Topper420761a2012-04-20 07:30:17 +0000338 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000339 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000340 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000341
342 // If this is an 8-bit value, it is really passed promoted to 16
343 // bits. Insert an assert[sz]ext to capture this, then truncate to the
344 // right size.
345 if (VA.getLocInfo() == CCValAssign::SExt)
346 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
347 DAG.getValueType(VA.getValVT()));
348 else if (VA.getLocInfo() == CCValAssign::ZExt)
349 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
350 DAG.getValueType(VA.getValVT()));
351
352 if (VA.getLocInfo() != CCValAssign::Full)
353 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
354
Dan Gohman98ca4f22009-08-05 01:29:28 +0000355 InVals.push_back(ArgValue);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000356 }
357 } else {
358 // Sanity check
359 assert(VA.isMemLoc());
360 // Load the argument to a virtual register
361 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
362 if (ObjSize > 2) {
Chris Lattner4437ae22009-08-23 07:05:07 +0000363 errs() << "LowerFormalArguments Unhandled argument type: "
Duncan Sands1440e8b2010-11-03 11:35:31 +0000364 << EVT(VA.getLocVT()).getEVTString()
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000365 << "\n";
366 }
367 // Create the frame index object for this incoming parameter...
Evan Chenged2ae132010-07-03 00:40:23 +0000368 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000369
370 // Create the SelectionDAG nodes corresponding to a load
371 //from this parameter
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000373 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000374 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000375 false, false, false, 0));
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000376 }
377 }
378
Dan Gohman98ca4f22009-08-05 01:29:28 +0000379 return Chain;
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000380}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000381
Dan Gohman98ca4f22009-08-05 01:29:28 +0000382SDValue
383MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000384 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000385 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000386 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +0000387 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000388
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000389 // CCValAssign - represent the assignment of the return value to a location
390 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000391
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000392 // ISRs cannot return any value.
David Blaikie4d6ccb52012-01-20 21:51:11 +0000393 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
Chris Lattner75361b62010-04-07 22:58:41 +0000394 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000395
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000396 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +0000397 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000398 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000399
Dan Gohman98ca4f22009-08-05 01:29:28 +0000400 // Analize return values.
401 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000402
403 // If this is the first return lowered for this function, add the regs to the
404 // liveout set for the function.
405 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
406 for (unsigned i = 0; i != RVLocs.size(); ++i)
407 if (RVLocs[i].isRegLoc())
408 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
409 }
410
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000411 SDValue Flag;
412
413 // Copy the result values into the output registers.
414 for (unsigned i = 0; i != RVLocs.size(); ++i) {
415 CCValAssign &VA = RVLocs[i];
416 assert(VA.isRegLoc() && "Can only return in registers!");
417
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000418 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000419 OutVals[i], Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000420
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000421 // Guarantee that all emitted copies are stuck together,
422 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000423 Flag = Chain.getValue(1);
424 }
425
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000426 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
427 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
428
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000429 if (Flag.getNode())
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000430 return DAG.getNode(Opc, dl, MVT::Other, Chain, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000431
432 // Return Void
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000433 return DAG.getNode(Opc, dl, MVT::Other, Chain);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000434}
435
Anton Korobeynikov44288852009-05-03 13:07:31 +0000436/// LowerCCCCallTo - functions arguments are copied from virtual regs to
437/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
438/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000439SDValue
440MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000441 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000442 bool isTailCall,
443 const SmallVectorImpl<ISD::OutputArg>
444 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000445 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000446 const SmallVectorImpl<ISD::InputArg> &Ins,
447 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000448 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000449 // Analyze operands of the call, assigning locations to each operand.
450 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000451 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000452 getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000453
Dan Gohman98ca4f22009-08-05 01:29:28 +0000454 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000455
456 // Get a count of how many bytes are to be pushed on the stack.
457 unsigned NumBytes = CCInfo.getNextStackOffset();
458
459 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
460 getPointerTy(), true));
461
462 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
463 SmallVector<SDValue, 12> MemOpChains;
464 SDValue StackPtr;
465
466 // Walk the register/memloc assignments, inserting copies/loads.
467 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
468 CCValAssign &VA = ArgLocs[i];
469
Dan Gohmanc9403652010-07-07 15:54:55 +0000470 SDValue Arg = OutVals[i];
Anton Korobeynikov44288852009-05-03 13:07:31 +0000471
472 // Promote the value if needed.
473 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000474 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000475 case CCValAssign::Full: break;
476 case CCValAssign::SExt:
477 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
478 break;
479 case CCValAssign::ZExt:
480 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
481 break;
482 case CCValAssign::AExt:
483 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
484 break;
485 }
486
487 // Arguments that can be passed on register must be kept at RegsToPass
488 // vector
489 if (VA.isRegLoc()) {
490 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
491 } else {
492 assert(VA.isMemLoc());
493
494 if (StackPtr.getNode() == 0)
495 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
496
497 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
498 StackPtr,
499 DAG.getIntPtrConstant(VA.getLocMemOffset()));
500
501
502 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000503 MachinePointerInfo(),false, false, 0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000504 }
505 }
506
507 // Transform all store nodes into one single node because all store nodes are
508 // independent of each other.
509 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov44288852009-05-03 13:07:31 +0000511 &MemOpChains[0], MemOpChains.size());
512
513 // Build a sequence of copy-to-reg nodes chained together with token chain and
514 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000515 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikov44288852009-05-03 13:07:31 +0000516 SDValue InFlag;
517 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
518 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
519 RegsToPass[i].second, InFlag);
520 InFlag = Chain.getValue(1);
521 }
522
523 // If the callee is a GlobalAddress node (quite common, every direct call is)
524 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
525 // Likewise ExternalSymbol -> TargetExternalSymbol.
526 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000527 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000528 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000530
531 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000532 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000533 SmallVector<SDValue, 8> Ops;
534 Ops.push_back(Chain);
535 Ops.push_back(Callee);
536
537 // Add argument registers to the end of the list so that they are
538 // known live into the call.
539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
540 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
541 RegsToPass[i].second.getValueType()));
542
543 if (InFlag.getNode())
544 Ops.push_back(InFlag);
545
546 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
547 InFlag = Chain.getValue(1);
548
549 // Create the CALLSEQ_END node.
550 Chain = DAG.getCALLSEQ_END(Chain,
551 DAG.getConstant(NumBytes, getPointerTy(), true),
552 DAG.getConstant(0, getPointerTy(), true),
553 InFlag);
554 InFlag = Chain.getValue(1);
555
556 // Handle result values, copying them out of physregs into vregs that we
557 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000558 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
559 DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000560}
561
Dan Gohman98ca4f22009-08-05 01:29:28 +0000562/// LowerCallResult - Lower the result values of a call into the
563/// appropriate copies out of appropriate physical registers.
564///
565SDValue
Anton Korobeynikov44288852009-05-03 13:07:31 +0000566MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000567 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000568 const SmallVectorImpl<ISD::InputArg> &Ins,
569 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000570 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000571
572 // Assign locations to each value returned by this call.
573 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000574 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000575 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000576
Dan Gohman98ca4f22009-08-05 01:29:28 +0000577 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000578
579 // Copy all of the result registers out of their specified physreg.
580 for (unsigned i = 0; i != RVLocs.size(); ++i) {
581 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
582 RVLocs[i].getValVT(), InFlag).getValue(1);
583 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000584 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000585 }
586
Dan Gohman98ca4f22009-08-05 01:29:28 +0000587 return Chain;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000588}
589
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000590SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000591 SelectionDAG &DAG) const {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000592 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000593 SDNode* N = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000594 EVT VT = Op.getValueType();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000595 DebugLoc dl = N->getDebugLoc();
596
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000597 // Expand non-constant shifts to loops:
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000598 if (!isa<ConstantSDNode>(N->getOperand(1)))
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000599 switch (Opc) {
Craig Topperbc219812012-02-07 02:50:20 +0000600 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000601 case ISD::SHL:
602 return DAG.getNode(MSP430ISD::SHL, dl,
603 VT, N->getOperand(0), N->getOperand(1));
604 case ISD::SRA:
605 return DAG.getNode(MSP430ISD::SRA, dl,
606 VT, N->getOperand(0), N->getOperand(1));
607 case ISD::SRL:
608 return DAG.getNode(MSP430ISD::SRL, dl,
609 VT, N->getOperand(0), N->getOperand(1));
610 }
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000611
612 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
613
614 // Expand the stuff into sequence of shifts.
615 // FIXME: for some shift amounts this might be done better!
616 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
617 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000618
619 if (Opc == ISD::SRL && ShiftAmount) {
620 // Emit a special goodness here:
621 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000622 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000623 ShiftAmount -= 1;
624 }
625
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000626 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000627 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000628 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000629
630 return Victim;
631}
632
Dan Gohmand858e902010-04-17 15:26:15 +0000633SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
634 SelectionDAG &DAG) const {
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000635 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
636 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
637
638 // Create the TargetGlobalAddress node, folding in the constant offset.
Devang Patel0d881da2010-07-06 22:08:15 +0000639 SDValue Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
640 getPointerTy(), Offset);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000641 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
642 getPointerTy(), Result);
643}
644
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000645SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000646 SelectionDAG &DAG) const {
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000647 DebugLoc dl = Op.getDebugLoc();
648 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
649 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
650
Chad Rosier90f20042012-02-22 17:25:00 +0000651 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000652}
653
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000654SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
655 SelectionDAG &DAG) const {
656 DebugLoc dl = Op.getDebugLoc();
657 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
658 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(), /*isTarget=*/true);
659
Chad Rosier90f20042012-02-22 17:25:00 +0000660 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000661}
662
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000663static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000664 ISD::CondCode CC,
665 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000666 // FIXME: Handle bittests someday
667 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
668
669 // FIXME: Handle jump negative someday
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000670 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000671 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000672 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000673 case ISD::SETEQ:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000674 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000675 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000676 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000677 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000678 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000679 break;
680 case ISD::SETNE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000681 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000682 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000683 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000684 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000685 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000686 break;
687 case ISD::SETULE:
688 std::swap(LHS, RHS); // FALLTHROUGH
689 case ISD::SETUGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000690 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
691 // fold constant into instruction.
692 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
693 LHS = RHS;
694 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
695 TCC = MSP430CC::COND_LO;
696 break;
697 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000698 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000699 break;
700 case ISD::SETUGT:
701 std::swap(LHS, RHS); // FALLTHROUGH
702 case ISD::SETULT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000703 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
704 // fold constant into instruction.
705 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
706 LHS = RHS;
707 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
708 TCC = MSP430CC::COND_HS;
709 break;
710 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000711 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000712 break;
713 case ISD::SETLE:
714 std::swap(LHS, RHS); // FALLTHROUGH
715 case ISD::SETGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000716 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
717 // fold constant into instruction.
718 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
719 LHS = RHS;
720 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
721 TCC = MSP430CC::COND_L;
722 break;
723 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000724 TCC = MSP430CC::COND_GE;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000725 break;
726 case ISD::SETGT:
727 std::swap(LHS, RHS); // FALLTHROUGH
728 case ISD::SETLT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000729 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
730 // fold constant into instruction.
731 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
732 LHS = RHS;
733 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
734 TCC = MSP430CC::COND_GE;
735 break;
736 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000737 TCC = MSP430CC::COND_L;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000738 break;
739 }
740
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000741 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000742 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000743}
744
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000745
Dan Gohmand858e902010-04-17 15:26:15 +0000746SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000747 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000748 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
749 SDValue LHS = Op.getOperand(2);
750 SDValue RHS = Op.getOperand(3);
751 SDValue Dest = Op.getOperand(4);
752 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000753
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000754 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000755 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000756
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000757 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000758 Chain, Dest, TargetCC, Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000759}
760
Dan Gohmand858e902010-04-17 15:26:15 +0000761SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000762 SDValue LHS = Op.getOperand(0);
763 SDValue RHS = Op.getOperand(1);
764 DebugLoc dl = Op.getDebugLoc();
765
766 // If we are doing an AND and testing against zero, then the CMP
767 // will not be generated. The AND (or BIT) will generate the condition codes,
768 // but they are different from CMP.
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000769 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
770 // lowering & isel wouldn't diverge.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000771 bool andCC = false;
772 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
773 if (RHSC->isNullValue() && LHS.hasOneUse() &&
774 (LHS.getOpcode() == ISD::AND ||
775 (LHS.getOpcode() == ISD::TRUNCATE &&
776 LHS.getOperand(0).getOpcode() == ISD::AND))) {
777 andCC = true;
778 }
779 }
780 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
781 SDValue TargetCC;
782 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
783
784 // Get the condition codes directly from the status register, if its easy.
785 // Otherwise a branch will be generated. Note that the AND and BIT
786 // instructions generate different flags than CMP, the carry bit can be used
787 // for NE/EQ.
788 bool Invert = false;
789 bool Shift = false;
790 bool Convert = true;
791 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
792 default:
793 Convert = false;
794 break;
795 case MSP430CC::COND_HS:
796 // Res = SRW & 1, no processing is required
797 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000798 case MSP430CC::COND_LO:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000799 // Res = ~(SRW & 1)
800 Invert = true;
801 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000802 case MSP430CC::COND_NE:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000803 if (andCC) {
804 // C = ~Z, thus Res = SRW & 1, no processing is required
805 } else {
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000806 // Res = ~((SRW >> 1) & 1)
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000807 Shift = true;
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000808 Invert = true;
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000809 }
810 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000811 case MSP430CC::COND_E:
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000812 Shift = true;
813 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
814 // Res = (SRW >> 1) & 1 is 1 word shorter.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000815 break;
816 }
817 EVT VT = Op.getValueType();
818 SDValue One = DAG.getConstant(1, VT);
819 if (Convert) {
820 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000821 MVT::i16, Flag);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000822 if (Shift)
823 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
824 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
825 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
826 if (Invert)
827 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
828 return SR;
829 } else {
830 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000831 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000832 SmallVector<SDValue, 4> Ops;
833 Ops.push_back(One);
834 Ops.push_back(Zero);
835 Ops.push_back(TargetCC);
836 Ops.push_back(Flag);
837 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
838 }
839}
840
Dan Gohmand858e902010-04-17 15:26:15 +0000841SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
842 SelectionDAG &DAG) const {
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000843 SDValue LHS = Op.getOperand(0);
844 SDValue RHS = Op.getOperand(1);
845 SDValue TrueV = Op.getOperand(2);
846 SDValue FalseV = Op.getOperand(3);
847 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000848 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000849
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000850 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000851 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000852
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000853 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000854 SmallVector<SDValue, 4> Ops;
855 Ops.push_back(TrueV);
856 Ops.push_back(FalseV);
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000857 Ops.push_back(TargetCC);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000858 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000859
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000860 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000861}
862
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000863SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000864 SelectionDAG &DAG) const {
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000865 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000866 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000867 DebugLoc dl = Op.getDebugLoc();
868
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000870
871 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
872 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
873 DAG.getValueType(Val.getValueType()));
874}
875
Dan Gohmand858e902010-04-17 15:26:15 +0000876SDValue
877MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000878 MachineFunction &MF = DAG.getMachineFunction();
879 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
880 int ReturnAddrIndex = FuncInfo->getRAIndex();
881
882 if (ReturnAddrIndex == 0) {
883 // Set up a frame object for the return address.
884 uint64_t SlotSize = TD->getPointerSize();
885 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +0000886 true);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000887 FuncInfo->setRAIndex(ReturnAddrIndex);
888 }
889
890 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
891}
892
Dan Gohmand858e902010-04-17 15:26:15 +0000893SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
894 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +0000895 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
896 MFI->setReturnAddressIsTaken(true);
897
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000898 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
899 DebugLoc dl = Op.getDebugLoc();
900
901 if (Depth > 0) {
902 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
903 SDValue Offset =
904 DAG.getConstant(TD->getPointerSize(), MVT::i16);
905 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
906 DAG.getNode(ISD::ADD, dl, getPointerTy(),
907 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000908 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000909 }
910
911 // Just load the return address.
912 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
913 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000914 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000915}
916
Dan Gohmand858e902010-04-17 15:26:15 +0000917SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
918 SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000919 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
920 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000921
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000922 EVT VT = Op.getValueType();
923 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
924 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
925 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
926 MSP430::FPW, VT);
927 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000928 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
929 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000930 false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000931 return FrameAddr;
932}
933
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000934/// getPostIndexedAddressParts - returns true by value, base pointer and
935/// offset pointer and addressing mode by reference if this node can be
936/// combined with a load / store to form a post-indexed load / store.
937bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
938 SDValue &Base,
939 SDValue &Offset,
940 ISD::MemIndexedMode &AM,
941 SelectionDAG &DAG) const {
942
943 LoadSDNode *LD = cast<LoadSDNode>(N);
944 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
945 return false;
946
947 EVT VT = LD->getMemoryVT();
948 if (VT != MVT::i8 && VT != MVT::i16)
949 return false;
950
951 if (Op->getOpcode() != ISD::ADD)
952 return false;
953
954 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
955 uint64_t RHSC = RHS->getZExtValue();
956 if ((VT == MVT::i16 && RHSC != 2) ||
957 (VT == MVT::i8 && RHSC != 1))
958 return false;
959
960 Base = Op->getOperand(0);
961 Offset = DAG.getConstant(RHSC, VT);
962 AM = ISD::POST_INC;
963 return true;
964 }
965
966 return false;
967}
968
969
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000970const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
971 switch (Opcode) {
972 default: return NULL;
973 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov6bfcba72009-12-07 02:28:41 +0000974 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000975 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000976 case MSP430ISD::RLA: return "MSP430ISD::RLA";
977 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000978 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000979 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000980 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000981 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000982 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000983 case MSP430ISD::SHL: return "MSP430ISD::SHL";
984 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000985 }
986}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000987
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000988bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
989 Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000990 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +0000991 return false;
992
993 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
994}
995
996bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
997 if (!VT1.isInteger() || !VT2.isInteger())
998 return false;
999
1000 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1001}
1002
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001003bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001004 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001005 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001006}
1007
1008bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1009 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1010 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1011}
1012
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001013//===----------------------------------------------------------------------===//
1014// Other Lowering Code
1015//===----------------------------------------------------------------------===//
1016
1017MachineBasicBlock*
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001018MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001019 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001020 MachineFunction *F = BB->getParent();
1021 MachineRegisterInfo &RI = F->getRegInfo();
1022 DebugLoc dl = MI->getDebugLoc();
1023 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1024
1025 unsigned Opc;
1026 const TargetRegisterClass * RC;
1027 switch (MI->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00001028 default: llvm_unreachable("Invalid shift opcode!");
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001029 case MSP430::Shl8:
1030 Opc = MSP430::SHL8r1;
Craig Topper420761a2012-04-20 07:30:17 +00001031 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001032 break;
1033 case MSP430::Shl16:
1034 Opc = MSP430::SHL16r1;
Craig Topper420761a2012-04-20 07:30:17 +00001035 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001036 break;
1037 case MSP430::Sra8:
1038 Opc = MSP430::SAR8r1;
Craig Topper420761a2012-04-20 07:30:17 +00001039 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001040 break;
1041 case MSP430::Sra16:
1042 Opc = MSP430::SAR16r1;
Craig Topper420761a2012-04-20 07:30:17 +00001043 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001044 break;
1045 case MSP430::Srl8:
1046 Opc = MSP430::SAR8r1c;
Craig Topper420761a2012-04-20 07:30:17 +00001047 RC = &MSP430::GR8RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001048 break;
1049 case MSP430::Srl16:
1050 Opc = MSP430::SAR16r1c;
Craig Topper420761a2012-04-20 07:30:17 +00001051 RC = &MSP430::GR16RegClass;
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001052 break;
1053 }
1054
1055 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1056 MachineFunction::iterator I = BB;
1057 ++I;
1058
1059 // Create loop block
1060 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1061 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1062
1063 F->insert(I, LoopBB);
1064 F->insert(I, RemBB);
1065
1066 // Update machine-CFG edges by transferring all successors of the current
1067 // block to the block containing instructions after shift.
Dan Gohman14152b42010-07-06 20:24:04 +00001068 RemBB->splice(RemBB->begin(), BB,
1069 llvm::next(MachineBasicBlock::iterator(MI)),
1070 BB->end());
1071 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001072
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001073 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1074 BB->addSuccessor(LoopBB);
1075 BB->addSuccessor(RemBB);
1076 LoopBB->addSuccessor(RemBB);
1077 LoopBB->addSuccessor(LoopBB);
1078
Craig Topper420761a2012-04-20 07:30:17 +00001079 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1080 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001081 unsigned ShiftReg = RI.createVirtualRegister(RC);
1082 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1083 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1084 unsigned SrcReg = MI->getOperand(1).getReg();
1085 unsigned DstReg = MI->getOperand(0).getReg();
1086
1087 // BB:
1088 // cmp 0, N
1089 // je RemBB
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +00001090 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1091 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001092 BuildMI(BB, dl, TII.get(MSP430::JCC))
1093 .addMBB(RemBB)
1094 .addImm(MSP430CC::COND_E);
1095
1096 // LoopBB:
1097 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1098 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1099 // ShiftReg2 = shift ShiftReg
1100 // ShiftAmt2 = ShiftAmt - 1;
1101 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1102 .addReg(SrcReg).addMBB(BB)
1103 .addReg(ShiftReg2).addMBB(LoopBB);
1104 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1105 .addReg(ShiftAmtSrcReg).addMBB(BB)
1106 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1107 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1108 .addReg(ShiftReg);
1109 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1110 .addReg(ShiftAmtReg).addImm(1);
1111 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1112 .addMBB(LoopBB)
1113 .addImm(MSP430CC::COND_NE);
1114
1115 // RemBB:
1116 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman14152b42010-07-06 20:24:04 +00001117 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001118 .addReg(SrcReg).addMBB(BB)
1119 .addReg(ShiftReg2).addMBB(LoopBB);
1120
Dan Gohman14152b42010-07-06 20:24:04 +00001121 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001122 return RemBB;
1123}
1124
1125MachineBasicBlock*
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001126MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001127 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001128 unsigned Opc = MI->getOpcode();
1129
1130 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1131 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1132 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001133 return EmitShiftInstr(MI, BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001134
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001135 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1136 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001137
1138 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001139 "Unexpected instr type to insert");
1140
1141 // To "insert" a SELECT instruction, we actually have to insert the diamond
1142 // control-flow pattern. The incoming instruction knows the destination vreg
1143 // to set, the condition code register to branch on, the true/false values to
1144 // select between, and a branch opcode to use.
1145 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1146 MachineFunction::iterator I = BB;
1147 ++I;
1148
1149 // thisMBB:
1150 // ...
1151 // TrueVal = ...
1152 // cmpTY ccX, r1, r2
1153 // jCC copy1MBB
1154 // fallthrough --> copy0MBB
1155 MachineBasicBlock *thisMBB = BB;
1156 MachineFunction *F = BB->getParent();
1157 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1158 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001159 F->insert(I, copy0MBB);
1160 F->insert(I, copy1MBB);
1161 // Update machine-CFG edges by transferring all successors of the current
1162 // block to the new block which will contain the Phi node for the select.
Dan Gohman14152b42010-07-06 20:24:04 +00001163 copy1MBB->splice(copy1MBB->begin(), BB,
1164 llvm::next(MachineBasicBlock::iterator(MI)),
1165 BB->end());
1166 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001167 // Next, add the true and fallthrough blocks as its successors.
1168 BB->addSuccessor(copy0MBB);
1169 BB->addSuccessor(copy1MBB);
1170
Dan Gohman14152b42010-07-06 20:24:04 +00001171 BuildMI(BB, dl, TII.get(MSP430::JCC))
1172 .addMBB(copy1MBB)
1173 .addImm(MI->getOperand(3).getImm());
1174
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001175 // copy0MBB:
1176 // %FalseValue = ...
1177 // # fallthrough to copy1MBB
1178 BB = copy0MBB;
1179
1180 // Update machine-CFG edges
1181 BB->addSuccessor(copy1MBB);
1182
1183 // copy1MBB:
1184 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1185 // ...
1186 BB = copy1MBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001187 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001188 MI->getOperand(0).getReg())
1189 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1190 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1191
Dan Gohman14152b42010-07-06 20:24:04 +00001192 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001193 return BB;
1194}