blob: 5c4ccc8f407b8661c9d4bae6accf6c689412a049 [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"
18#include "MSP430TargetMachine.h"
19#include "MSP430Subtarget.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/CallingConv.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/GlobalAlias.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000031#include "llvm/CodeGen/PseudoSourceValue.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/Target/TargetLoweringObjectFile.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 +000039#include "llvm/ADT/VectorExtras.h"
40using namespace llvm;
41
Anton Korobeynikovb2de1ea2009-12-07 02:27:08 +000042typedef enum {
43 NoHWMult,
44 HWMultIntr,
45 HWMultNoIntr
46} HWMultUseMode;
47
48static cl::opt<HWMultUseMode>
49HWMultMode("msp430-hwmult-mode",
50 cl::desc("Hardware multiplier use mode"),
51 cl::init(HWMultNoIntr),
52 cl::values(
53 clEnumValN(NoHWMult, "no",
54 "Do not use hardware multiplier"),
55 clEnumValN(HWMultIntr, "interrupts",
56 "Assume hardware multiplier can be used inside interrupts"),
57 clEnumValN(HWMultNoIntr, "use",
58 "Assume hardware multiplier cannot be used inside interrupts"),
59 clEnumValEnd));
60
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000061MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000062 TargetLowering(tm, new TargetLoweringObjectFileELF()),
63 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000064
65 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000066 addRegisterClass(MVT::i8, MSP430::GR8RegisterClass);
67 addRegisterClass(MVT::i16, MSP430::GR16RegisterClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000068
69 // Compute derived properties from the register classes
70 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000071
Anton Korobeynikov1476d972009-05-03 13:03:14 +000072 // Provide all sorts of operation actions
73
74 // Division is expensive
75 setIntDivIsCheap(false);
76
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000077 // Even if we have only 1 bit shift here, we can perform
78 // shifts of the whole bitwidth 1 bit per step.
Owen Anderson825b72b2009-08-11 20:47:22 +000079 setShiftAmountType(MVT::i8);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000080
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000081 setStackPointerRegisterToSaveRestore(MSP430::SPW);
82 setBooleanContents(ZeroOrOneBooleanContent);
83 setSchedulingPreference(SchedulingForLatency);
84
Anton Korobeynikov06ac0822009-11-07 17:15:25 +000085 // We have post-incremented loads / stores.
Anton Korobeynikov6534f832009-11-07 17:15:06 +000086 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
87 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
88
89 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
90 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
91 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
92 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000094
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000095 // We don't have any truncstores
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000097
Owen Anderson825b72b2009-08-11 20:47:22 +000098 setOperationAction(ISD::SRA, MVT::i8, Custom);
99 setOperationAction(ISD::SHL, MVT::i8, Custom);
100 setOperationAction(ISD::SRL, MVT::i8, Custom);
101 setOperationAction(ISD::SRA, MVT::i16, Custom);
102 setOperationAction(ISD::SHL, MVT::i16, Custom);
103 setOperationAction(ISD::SRL, MVT::i16, Custom);
104 setOperationAction(ISD::ROTL, MVT::i8, Expand);
105 setOperationAction(ISD::ROTR, MVT::i8, Expand);
106 setOperationAction(ISD::ROTL, MVT::i16, Expand);
107 setOperationAction(ISD::ROTR, MVT::i16, Expand);
108 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
109 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
110 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
111 setOperationAction(ISD::BRIND, MVT::Other, Expand);
112 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
113 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
114 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
115 setOperationAction(ISD::SETCC, MVT::i8, Expand);
116 setOperationAction(ISD::SETCC, MVT::i16, Expand);
117 setOperationAction(ISD::SELECT, MVT::i8, Expand);
118 setOperationAction(ISD::SELECT, MVT::i16, Expand);
119 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
120 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
121 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov379a0872009-08-25 17:00:23 +0000122 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
123 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000124
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
126 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
127 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
128 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
129 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
130 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000131
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
133 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
134 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
135 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
136 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
137 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000138
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000140
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000141 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000142 setOperationAction(ISD::MUL, MVT::i8, Expand);
143 setOperationAction(ISD::MULHS, MVT::i8, Expand);
144 setOperationAction(ISD::MULHU, MVT::i8, Expand);
145 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
146 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000147 setOperationAction(ISD::MUL, MVT::i16, Expand);
148 setOperationAction(ISD::MULHS, MVT::i16, Expand);
149 setOperationAction(ISD::MULHU, MVT::i16, Expand);
150 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
151 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikovf2f54022009-05-03 13:18:33 +0000152
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000153 setOperationAction(ISD::UDIV, MVT::i8, Expand);
154 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
155 setOperationAction(ISD::UREM, MVT::i8, Expand);
156 setOperationAction(ISD::SDIV, MVT::i8, Expand);
157 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
158 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::UDIV, MVT::i16, Expand);
160 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
161 setOperationAction(ISD::UREM, MVT::i16, Expand);
162 setOperationAction(ISD::SDIV, MVT::i16, Expand);
163 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
164 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikovb2de1ea2009-12-07 02:27:08 +0000165
166 // Libcalls names.
167 if (HWMultMode == HWMultIntr) {
168 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
169 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
170 } else if (HWMultMode == HWMultNoIntr) {
171 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
172 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
173 }
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000174}
175
Anton Korobeynikovb8639f52009-05-03 13:03:50 +0000176SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000177 switch (Op.getOpcode()) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000178 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000179 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000180 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000181 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000182 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000183 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
184 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000185 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000186 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000187 llvm_unreachable("unimplemented operand");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000188 return SDValue();
189 }
190}
191
Bill Wendlingb4202b82009-07-01 18:50:55 +0000192/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000193unsigned MSP430TargetLowering::getFunctionAlignment(const Function *F) const {
Anton Korobeynikov3741be32009-11-22 01:13:39 +0000194 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000195}
196
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000197//===----------------------------------------------------------------------===//
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000198// MSP430 Inline Assembly Support
199//===----------------------------------------------------------------------===//
200
201/// getConstraintType - Given a constraint letter, return the type of
202/// constraint it is for this target.
203TargetLowering::ConstraintType
204MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
205 if (Constraint.size() == 1) {
206 switch (Constraint[0]) {
207 case 'r':
208 return C_RegisterClass;
209 default:
210 break;
211 }
212 }
213 return TargetLowering::getConstraintType(Constraint);
214}
215
216std::pair<unsigned, const TargetRegisterClass*>
217MSP430TargetLowering::
218getRegForInlineAsmConstraint(const std::string &Constraint,
219 EVT VT) const {
220 if (Constraint.size() == 1) {
221 // GCC Constraint Letters
222 switch (Constraint[0]) {
223 default: break;
224 case 'r': // GENERAL_REGS
225 if (VT == MVT::i8)
226 return std::make_pair(0U, MSP430::GR8RegisterClass);
227
228 return std::make_pair(0U, MSP430::GR16RegisterClass);
229 }
230 }
231
232 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
233}
234
235//===----------------------------------------------------------------------===//
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000236// Calling Convention Implementation
237//===----------------------------------------------------------------------===//
238
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000239#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000240
Dan Gohman98ca4f22009-08-05 01:29:28 +0000241SDValue
242MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000243 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000244 bool isVarArg,
245 const SmallVectorImpl<ISD::InputArg>
246 &Ins,
247 DebugLoc dl,
248 SelectionDAG &DAG,
249 SmallVectorImpl<SDValue> &InVals) {
250
251 switch (CallConv) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000252 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000253 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000254 case CallingConv::C:
255 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000256 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000257 }
258}
259
Dan Gohman98ca4f22009-08-05 01:29:28 +0000260SDValue
261MSP430TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000262 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000263 bool isTailCall,
264 const SmallVectorImpl<ISD::OutputArg> &Outs,
265 const SmallVectorImpl<ISD::InputArg> &Ins,
266 DebugLoc dl, SelectionDAG &DAG,
267 SmallVectorImpl<SDValue> &InVals) {
268
269 switch (CallConv) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000270 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000271 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000272 case CallingConv::Fast:
273 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000274 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
275 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000276 }
277}
278
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000279/// LowerCCCArguments - transform physical registers into virtual registers and
280/// generate load operations for arguments places on the stack.
281// FIXME: struct return stuff
282// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000283SDValue
284MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000285 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000286 bool isVarArg,
287 const SmallVectorImpl<ISD::InputArg>
288 &Ins,
289 DebugLoc dl,
290 SelectionDAG &DAG,
291 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000292 MachineFunction &MF = DAG.getMachineFunction();
293 MachineFrameInfo *MFI = MF.getFrameInfo();
294 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000295
296 // Assign locations to all of the incoming arguments.
297 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000298 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
299 ArgLocs, *DAG.getContext());
300 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000301
302 assert(!isVarArg && "Varargs not supported yet");
303
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000304 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
305 CCValAssign &VA = ArgLocs[i];
306 if (VA.isRegLoc()) {
307 // Arguments passed in registers
Owen Andersone50ed302009-08-10 22:56:29 +0000308 EVT RegVT = VA.getLocVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 switch (RegVT.getSimpleVT().SimpleTy) {
Torok Edwin804e0fe2009-07-08 19:04:27 +0000310 default:
311 {
Torok Edwindac237e2009-07-08 20:53:28 +0000312#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000313 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000315#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000316 llvm_unreachable(0);
Torok Edwin804e0fe2009-07-08 19:04:27 +0000317 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 case MVT::i16:
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000319 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000320 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000321 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000322 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000323
324 // If this is an 8-bit value, it is really passed promoted to 16
325 // bits. Insert an assert[sz]ext to capture this, then truncate to the
326 // right size.
327 if (VA.getLocInfo() == CCValAssign::SExt)
328 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
329 DAG.getValueType(VA.getValVT()));
330 else if (VA.getLocInfo() == CCValAssign::ZExt)
331 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
332 DAG.getValueType(VA.getValVT()));
333
334 if (VA.getLocInfo() != CCValAssign::Full)
335 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
336
Dan Gohman98ca4f22009-08-05 01:29:28 +0000337 InVals.push_back(ArgValue);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000338 }
339 } else {
340 // Sanity check
341 assert(VA.isMemLoc());
342 // Load the argument to a virtual register
343 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
344 if (ObjSize > 2) {
Chris Lattner4437ae22009-08-23 07:05:07 +0000345 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 << VA.getLocVT().getSimpleVT().SimpleTy
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000347 << "\n";
348 }
349 // Create the frame index object for this incoming parameter...
David Greene3f2bf852009-11-12 20:49:22 +0000350 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true, false);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000351
352 // Create the SelectionDAG nodes corresponding to a load
353 //from this parameter
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000355 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +0000356 PseudoSourceValue::getFixedStack(FI), 0));
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000357 }
358 }
359
Dan Gohman98ca4f22009-08-05 01:29:28 +0000360 return Chain;
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000361}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000362
Dan Gohman98ca4f22009-08-05 01:29:28 +0000363SDValue
364MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000365 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000366 const SmallVectorImpl<ISD::OutputArg> &Outs,
367 DebugLoc dl, SelectionDAG &DAG) {
368
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000369 // CCValAssign - represent the assignment of the return value to a location
370 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000371
372 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000373 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
374 RVLocs, *DAG.getContext());
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000375
Dan Gohman98ca4f22009-08-05 01:29:28 +0000376 // Analize return values.
377 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000378
379 // If this is the first return lowered for this function, add the regs to the
380 // liveout set for the function.
381 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
382 for (unsigned i = 0; i != RVLocs.size(); ++i)
383 if (RVLocs[i].isRegLoc())
384 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
385 }
386
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000387 SDValue Flag;
388
389 // Copy the result values into the output registers.
390 for (unsigned i = 0; i != RVLocs.size(); ++i) {
391 CCValAssign &VA = RVLocs[i];
392 assert(VA.isRegLoc() && "Can only return in registers!");
393
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000394 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +0000395 Outs[i].Val, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000396
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000397 // Guarantee that all emitted copies are stuck together,
398 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000399 Flag = Chain.getValue(1);
400 }
401
402 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000404
405 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000407}
408
Anton Korobeynikov44288852009-05-03 13:07:31 +0000409/// LowerCCCCallTo - functions arguments are copied from virtual regs to
410/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
411/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000412SDValue
413MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000414 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000415 bool isTailCall,
416 const SmallVectorImpl<ISD::OutputArg>
417 &Outs,
418 const SmallVectorImpl<ISD::InputArg> &Ins,
419 DebugLoc dl, SelectionDAG &DAG,
420 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000421 // Analyze operands of the call, assigning locations to each operand.
422 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000423 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
424 ArgLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000425
Dan Gohman98ca4f22009-08-05 01:29:28 +0000426 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000427
428 // Get a count of how many bytes are to be pushed on the stack.
429 unsigned NumBytes = CCInfo.getNextStackOffset();
430
431 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
432 getPointerTy(), true));
433
434 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
435 SmallVector<SDValue, 12> MemOpChains;
436 SDValue StackPtr;
437
438 // Walk the register/memloc assignments, inserting copies/loads.
439 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
440 CCValAssign &VA = ArgLocs[i];
441
Dan Gohman98ca4f22009-08-05 01:29:28 +0000442 SDValue Arg = Outs[i].Val;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000443
444 // Promote the value if needed.
445 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000446 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000447 case CCValAssign::Full: break;
448 case CCValAssign::SExt:
449 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
450 break;
451 case CCValAssign::ZExt:
452 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
453 break;
454 case CCValAssign::AExt:
455 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
456 break;
457 }
458
459 // Arguments that can be passed on register must be kept at RegsToPass
460 // vector
461 if (VA.isRegLoc()) {
462 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
463 } else {
464 assert(VA.isMemLoc());
465
466 if (StackPtr.getNode() == 0)
467 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
468
469 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
470 StackPtr,
471 DAG.getIntPtrConstant(VA.getLocMemOffset()));
472
473
474 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
475 PseudoSourceValue::getStack(),
476 VA.getLocMemOffset()));
477 }
478 }
479
480 // Transform all store nodes into one single node because all store nodes are
481 // independent of each other.
482 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov44288852009-05-03 13:07:31 +0000484 &MemOpChains[0], MemOpChains.size());
485
486 // Build a sequence of copy-to-reg nodes chained together with token chain and
487 // flag operands which copy the outgoing args into registers. The InFlag in
488 // necessary since all emited instructions must be stuck together.
489 SDValue InFlag;
490 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
491 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
492 RegsToPass[i].second, InFlag);
493 InFlag = Chain.getValue(1);
494 }
495
496 // If the callee is a GlobalAddress node (quite common, every direct call is)
497 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
498 // Likewise ExternalSymbol -> TargetExternalSymbol.
499 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000501 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000503
504 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000506 SmallVector<SDValue, 8> Ops;
507 Ops.push_back(Chain);
508 Ops.push_back(Callee);
509
510 // Add argument registers to the end of the list so that they are
511 // known live into the call.
512 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
513 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
514 RegsToPass[i].second.getValueType()));
515
516 if (InFlag.getNode())
517 Ops.push_back(InFlag);
518
519 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
520 InFlag = Chain.getValue(1);
521
522 // Create the CALLSEQ_END node.
523 Chain = DAG.getCALLSEQ_END(Chain,
524 DAG.getConstant(NumBytes, getPointerTy(), true),
525 DAG.getConstant(0, getPointerTy(), true),
526 InFlag);
527 InFlag = Chain.getValue(1);
528
529 // Handle result values, copying them out of physregs into vregs that we
530 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000531 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
532 DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000533}
534
Dan Gohman98ca4f22009-08-05 01:29:28 +0000535/// LowerCallResult - Lower the result values of a call into the
536/// appropriate copies out of appropriate physical registers.
537///
538SDValue
Anton Korobeynikov44288852009-05-03 13:07:31 +0000539MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000540 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000541 const SmallVectorImpl<ISD::InputArg> &Ins,
542 DebugLoc dl, SelectionDAG &DAG,
543 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000544
545 // Assign locations to each value returned by this call.
546 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000547 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000548 RVLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000549
Dan Gohman98ca4f22009-08-05 01:29:28 +0000550 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000551
552 // Copy all of the result registers out of their specified physreg.
553 for (unsigned i = 0; i != RVLocs.size(); ++i) {
554 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
555 RVLocs[i].getValVT(), InFlag).getValue(1);
556 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000557 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000558 }
559
Dan Gohman98ca4f22009-08-05 01:29:28 +0000560 return Chain;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000561}
562
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000563SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
564 SelectionDAG &DAG) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000565 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000566 SDNode* N = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000567 EVT VT = Op.getValueType();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000568 DebugLoc dl = N->getDebugLoc();
569
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000570 // We currently only lower shifts of constant argument.
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000571 if (!isa<ConstantSDNode>(N->getOperand(1)))
572 return SDValue();
573
574 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
575
576 // Expand the stuff into sequence of shifts.
577 // FIXME: for some shift amounts this might be done better!
578 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
579 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000580
581 if (Opc == ISD::SRL && ShiftAmount) {
582 // Emit a special goodness here:
583 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000584 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000585 ShiftAmount -= 1;
586 }
587
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000588 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000589 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000590 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000591
592 return Victim;
593}
594
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000595SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
596 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
597 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
598
599 // Create the TargetGlobalAddress node, folding in the constant offset.
600 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
601 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
602 getPointerTy(), Result);
603}
604
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000605SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
606 SelectionDAG &DAG) {
607 DebugLoc dl = Op.getDebugLoc();
608 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
609 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
610
611 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);;
612}
613
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000614static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000615 ISD::CondCode CC,
616 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000617 // FIXME: Handle bittests someday
618 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
619
620 // FIXME: Handle jump negative someday
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000621 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000622 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000623 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000624 case ISD::SETEQ:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000625 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000626 // Minor optimization: if RHS is a constant, swap operands, then the
627 // constant can be folded into comparison.
628 if (RHS.getOpcode() == ISD::Constant)
629 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000630 break;
631 case ISD::SETNE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000632 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000633 // Minor optimization: if RHS is a constant, swap operands, then the
634 // constant can be folded into comparison.
635 if (RHS.getOpcode() == ISD::Constant)
636 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000637 break;
638 case ISD::SETULE:
639 std::swap(LHS, RHS); // FALLTHROUGH
640 case ISD::SETUGE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000641 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000642 break;
643 case ISD::SETUGT:
644 std::swap(LHS, RHS); // FALLTHROUGH
645 case ISD::SETULT:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000646 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000647 break;
648 case ISD::SETLE:
649 std::swap(LHS, RHS); // FALLTHROUGH
650 case ISD::SETGE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000651 TCC = MSP430CC::COND_GE;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000652 break;
653 case ISD::SETGT:
654 std::swap(LHS, RHS); // FALLTHROUGH
655 case ISD::SETLT:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000656 TCC = MSP430CC::COND_L;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000657 break;
658 }
659
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000660 TargetCC = DAG.getConstant(TCC, MVT::i8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Flag, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000662}
663
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000664
665SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000666 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000667 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
668 SDValue LHS = Op.getOperand(2);
669 SDValue RHS = Op.getOperand(3);
670 SDValue Dest = Op.getOperand(4);
671 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000672
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000673 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000674 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000675
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000676 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000677 Chain, Dest, TargetCC, Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000678}
679
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000680SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
681 SDValue LHS = Op.getOperand(0);
682 SDValue RHS = Op.getOperand(1);
683 SDValue TrueV = Op.getOperand(2);
684 SDValue FalseV = Op.getOperand(3);
685 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000686 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000687
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000688 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000689 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000690
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000692 SmallVector<SDValue, 4> Ops;
693 Ops.push_back(TrueV);
694 Ops.push_back(FalseV);
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000695 Ops.push_back(TargetCC);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000696 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000697
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000698 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000699}
700
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000701SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
702 SelectionDAG &DAG) {
703 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000704 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000705 DebugLoc dl = Op.getDebugLoc();
706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000708
709 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
710 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
711 DAG.getValueType(Val.getValueType()));
712}
713
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000714/// getPostIndexedAddressParts - returns true by value, base pointer and
715/// offset pointer and addressing mode by reference if this node can be
716/// combined with a load / store to form a post-indexed load / store.
717bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
718 SDValue &Base,
719 SDValue &Offset,
720 ISD::MemIndexedMode &AM,
721 SelectionDAG &DAG) const {
722
723 LoadSDNode *LD = cast<LoadSDNode>(N);
724 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
725 return false;
726
727 EVT VT = LD->getMemoryVT();
728 if (VT != MVT::i8 && VT != MVT::i16)
729 return false;
730
731 if (Op->getOpcode() != ISD::ADD)
732 return false;
733
734 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
735 uint64_t RHSC = RHS->getZExtValue();
736 if ((VT == MVT::i16 && RHSC != 2) ||
737 (VT == MVT::i8 && RHSC != 1))
738 return false;
739
740 Base = Op->getOperand(0);
741 Offset = DAG.getConstant(RHSC, VT);
742 AM = ISD::POST_INC;
743 return true;
744 }
745
746 return false;
747}
748
749
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000750const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
751 switch (Opcode) {
752 default: return NULL;
753 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000754 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000755 case MSP430ISD::RLA: return "MSP430ISD::RLA";
756 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000757 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000758 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000759 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000760 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000761 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000762 }
763}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000764
765//===----------------------------------------------------------------------===//
766// Other Lowering Code
767//===----------------------------------------------------------------------===//
768
769MachineBasicBlock*
770MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000771 MachineBasicBlock *BB,
772 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000773 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
774 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda4d2f62009-05-08 18:51:21 +0000775 assert((MI->getOpcode() == MSP430::Select16 ||
776 MI->getOpcode() == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000777 "Unexpected instr type to insert");
778
779 // To "insert" a SELECT instruction, we actually have to insert the diamond
780 // control-flow pattern. The incoming instruction knows the destination vreg
781 // to set, the condition code register to branch on, the true/false values to
782 // select between, and a branch opcode to use.
783 const BasicBlock *LLVM_BB = BB->getBasicBlock();
784 MachineFunction::iterator I = BB;
785 ++I;
786
787 // thisMBB:
788 // ...
789 // TrueVal = ...
790 // cmpTY ccX, r1, r2
791 // jCC copy1MBB
792 // fallthrough --> copy0MBB
793 MachineBasicBlock *thisMBB = BB;
794 MachineFunction *F = BB->getParent();
795 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
796 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
797 BuildMI(BB, dl, TII.get(MSP430::JCC))
798 .addMBB(copy1MBB)
799 .addImm(MI->getOperand(3).getImm());
800 F->insert(I, copy0MBB);
801 F->insert(I, copy1MBB);
Evan Chengce319102009-09-19 09:51:03 +0000802 // Inform sdisel of the edge changes.
803 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
804 SE = BB->succ_end(); SI != SE; ++SI)
805 EM->insert(std::make_pair(*SI, copy1MBB));
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000806 // Update machine-CFG edges by transferring all successors of the current
807 // block to the new block which will contain the Phi node for the select.
808 copy1MBB->transferSuccessors(BB);
809 // Next, add the true and fallthrough blocks as its successors.
810 BB->addSuccessor(copy0MBB);
811 BB->addSuccessor(copy1MBB);
812
813 // copy0MBB:
814 // %FalseValue = ...
815 // # fallthrough to copy1MBB
816 BB = copy0MBB;
817
818 // Update machine-CFG edges
819 BB->addSuccessor(copy1MBB);
820
821 // copy1MBB:
822 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
823 // ...
824 BB = copy1MBB;
825 BuildMI(BB, dl, TII.get(MSP430::PHI),
826 MI->getOperand(0).getReg())
827 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
828 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
829
830 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
831 return BB;
832}