blob: 5c9413767e239b592d76f4ffd7c5d0103e9f64e5 [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 +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
Anton Korobeynikov06ccca52009-12-07 02:28:10 +000065 TD = getTargetData();
66
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000067 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000068 addRegisterClass(MVT::i8, MSP430::GR8RegisterClass);
69 addRegisterClass(MVT::i16, MSP430::GR16RegisterClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000070
71 // Compute derived properties from the register classes
72 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000073
Anton Korobeynikov1476d972009-05-03 13:03:14 +000074 // Provide all sorts of operation actions
75
76 // Division is expensive
77 setIntDivIsCheap(false);
78
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000079 setStackPointerRegisterToSaveRestore(MSP430::SPW);
80 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000081 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000082
Anton Korobeynikov06ac0822009-11-07 17:15:25 +000083 // We have post-incremented loads / stores.
Anton Korobeynikov6534f832009-11-07 17:15:06 +000084 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
85 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
86
87 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
88 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
89 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
90 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000092
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000093 // We don't have any truncstores
Owen Anderson825b72b2009-08-11 20:47:22 +000094 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000095
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setOperationAction(ISD::SRA, MVT::i8, Custom);
97 setOperationAction(ISD::SHL, MVT::i8, Custom);
98 setOperationAction(ISD::SRL, MVT::i8, Custom);
99 setOperationAction(ISD::SRA, MVT::i16, Custom);
100 setOperationAction(ISD::SHL, MVT::i16, Custom);
101 setOperationAction(ISD::SRL, MVT::i16, Custom);
102 setOperationAction(ISD::ROTL, MVT::i8, Expand);
103 setOperationAction(ISD::ROTR, MVT::i8, Expand);
104 setOperationAction(ISD::ROTL, MVT::i16, Expand);
105 setOperationAction(ISD::ROTR, MVT::i16, Expand);
106 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
107 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000108 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
111 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
112 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000113 setOperationAction(ISD::SETCC, MVT::i8, Custom);
114 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 setOperationAction(ISD::SELECT, MVT::i8, Expand);
116 setOperationAction(ISD::SELECT, MVT::i16, Expand);
117 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
118 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
119 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov379a0872009-08-25 17:00:23 +0000120 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
121 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000122
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
124 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
125 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
126 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
127 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
128 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000129
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
131 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
132 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
133 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
134 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
135 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000136
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000138
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000139 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000140 setOperationAction(ISD::MUL, MVT::i8, Expand);
141 setOperationAction(ISD::MULHS, MVT::i8, Expand);
142 setOperationAction(ISD::MULHU, MVT::i8, Expand);
143 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
144 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::MUL, MVT::i16, Expand);
146 setOperationAction(ISD::MULHS, MVT::i16, Expand);
147 setOperationAction(ISD::MULHU, MVT::i16, Expand);
148 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
149 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikovf2f54022009-05-03 13:18:33 +0000150
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000151 setOperationAction(ISD::UDIV, MVT::i8, Expand);
152 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
153 setOperationAction(ISD::UREM, MVT::i8, Expand);
154 setOperationAction(ISD::SDIV, MVT::i8, Expand);
155 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
156 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::UDIV, MVT::i16, Expand);
158 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
159 setOperationAction(ISD::UREM, MVT::i16, Expand);
160 setOperationAction(ISD::SDIV, MVT::i16, Expand);
161 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
162 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikovb2de1ea2009-12-07 02:27:08 +0000163
164 // Libcalls names.
165 if (HWMultMode == HWMultIntr) {
166 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
167 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
168 } else if (HWMultMode == HWMultNoIntr) {
169 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
170 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
171 }
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000172
173 setMinFunctionAlignment(1);
174 setPrefFunctionAlignment(2);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000175}
176
Dan Gohmand858e902010-04-17 15:26:15 +0000177SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
178 SelectionDAG &DAG) const {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000179 switch (Op.getOpcode()) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000180 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000181 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000182 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000183 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000184 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000185 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000186 case ISD::SETCC: return LowerSETCC(Op, DAG);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000187 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
188 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000189 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000190 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
191 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000192 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000193 llvm_unreachable("unimplemented operand");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000194 return SDValue();
195 }
196}
197
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000198//===----------------------------------------------------------------------===//
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000199// MSP430 Inline Assembly Support
200//===----------------------------------------------------------------------===//
201
202/// getConstraintType - Given a constraint letter, return the type of
203/// constraint it is for this target.
204TargetLowering::ConstraintType
205MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
206 if (Constraint.size() == 1) {
207 switch (Constraint[0]) {
208 case 'r':
209 return C_RegisterClass;
210 default:
211 break;
212 }
213 }
214 return TargetLowering::getConstraintType(Constraint);
215}
216
217std::pair<unsigned, const TargetRegisterClass*>
218MSP430TargetLowering::
219getRegForInlineAsmConstraint(const std::string &Constraint,
220 EVT VT) const {
221 if (Constraint.size() == 1) {
222 // GCC Constraint Letters
223 switch (Constraint[0]) {
224 default: break;
225 case 'r': // GENERAL_REGS
226 if (VT == MVT::i8)
227 return std::make_pair(0U, MSP430::GR8RegisterClass);
228
229 return std::make_pair(0U, MSP430::GR16RegisterClass);
230 }
231 }
232
233 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
234}
235
236//===----------------------------------------------------------------------===//
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000237// Calling Convention Implementation
238//===----------------------------------------------------------------------===//
239
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000240#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000241
Dan Gohman98ca4f22009-08-05 01:29:28 +0000242SDValue
243MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000244 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000245 bool isVarArg,
246 const SmallVectorImpl<ISD::InputArg>
247 &Ins,
248 DebugLoc dl,
249 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000250 SmallVectorImpl<SDValue> &InVals)
251 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000252
253 switch (CallConv) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000254 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000255 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000256 case CallingConv::C:
257 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000258 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000259 case CallingConv::MSP430_INTR:
260 if (Ins.empty())
261 return Chain;
262 else {
Chris Lattner75361b62010-04-07 22:58:41 +0000263 report_fatal_error("ISRs cannot have arguments");
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000264 return SDValue();
265 }
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000266 }
267}
268
Dan Gohman98ca4f22009-08-05 01:29:28 +0000269SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000270MSP430TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000271 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000272 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000273 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000274 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000275 const SmallVectorImpl<ISD::InputArg> &Ins,
276 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000277 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000278 // MSP430 target does not yet support tail call optimization.
279 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000280
281 switch (CallConv) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000282 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000283 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000284 case CallingConv::Fast:
285 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000286 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +0000287 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000288 case CallingConv::MSP430_INTR:
Chris Lattner75361b62010-04-07 22:58:41 +0000289 report_fatal_error("ISRs cannot be called directly");
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000290 return SDValue();
Anton Korobeynikov44288852009-05-03 13:07:31 +0000291 }
292}
293
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000294/// LowerCCCArguments - transform physical registers into virtual registers and
295/// generate load operations for arguments places on the stack.
296// FIXME: struct return stuff
297// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000298SDValue
299MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000300 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000301 bool isVarArg,
302 const SmallVectorImpl<ISD::InputArg>
303 &Ins,
304 DebugLoc dl,
305 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000306 SmallVectorImpl<SDValue> &InVals)
307 const {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000308 MachineFunction &MF = DAG.getMachineFunction();
309 MachineFrameInfo *MFI = MF.getFrameInfo();
310 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000311
312 // Assign locations to all of the incoming arguments.
313 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000314 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
315 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000316 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000317
318 assert(!isVarArg && "Varargs not supported yet");
319
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000320 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
321 CCValAssign &VA = ArgLocs[i];
322 if (VA.isRegLoc()) {
323 // Arguments passed in registers
Owen Andersone50ed302009-08-10 22:56:29 +0000324 EVT RegVT = VA.getLocVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 switch (RegVT.getSimpleVT().SimpleTy) {
Owen Anderson95771af2011-02-25 21:41:48 +0000326 default:
Torok Edwin804e0fe2009-07-08 19:04:27 +0000327 {
Torok Edwindac237e2009-07-08 20:53:28 +0000328#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000329 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000331#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000332 llvm_unreachable(0);
Torok Edwin804e0fe2009-07-08 19:04:27 +0000333 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 case MVT::i16:
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000335 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000336 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000337 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000338 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000339
340 // If this is an 8-bit value, it is really passed promoted to 16
341 // bits. Insert an assert[sz]ext to capture this, then truncate to the
342 // right size.
343 if (VA.getLocInfo() == CCValAssign::SExt)
344 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
345 DAG.getValueType(VA.getValVT()));
346 else if (VA.getLocInfo() == CCValAssign::ZExt)
347 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
348 DAG.getValueType(VA.getValVT()));
349
350 if (VA.getLocInfo() != CCValAssign::Full)
351 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
352
Dan Gohman98ca4f22009-08-05 01:29:28 +0000353 InVals.push_back(ArgValue);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000354 }
355 } else {
356 // Sanity check
357 assert(VA.isMemLoc());
358 // Load the argument to a virtual register
359 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
360 if (ObjSize > 2) {
Chris Lattner4437ae22009-08-23 07:05:07 +0000361 errs() << "LowerFormalArguments Unhandled argument type: "
Duncan Sands1440e8b2010-11-03 11:35:31 +0000362 << EVT(VA.getLocVT()).getEVTString()
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000363 << "\n";
364 }
365 // Create the frame index object for this incoming parameter...
Evan Chenged2ae132010-07-03 00:40:23 +0000366 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000367
368 // Create the SelectionDAG nodes corresponding to a load
369 //from this parameter
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000371 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000372 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000373 false, false, false, 0));
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000374 }
375 }
376
Dan Gohman98ca4f22009-08-05 01:29:28 +0000377 return Chain;
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000378}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000379
Dan Gohman98ca4f22009-08-05 01:29:28 +0000380SDValue
381MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000382 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000383 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000384 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +0000385 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000386
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000387 // CCValAssign - represent the assignment of the return value to a location
388 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000389
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000390 // ISRs cannot return any value.
391 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000392 report_fatal_error("ISRs cannot return any value");
Anton Korobeynikove662f7a2009-12-07 02:27:53 +0000393 return SDValue();
394 }
395
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(),
398 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(),
452 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(),
575 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) {
600 default:
601 assert(0 && "Invalid shift opcode!");
602 case ISD::SHL:
603 return DAG.getNode(MSP430ISD::SHL, dl,
604 VT, N->getOperand(0), N->getOperand(1));
605 case ISD::SRA:
606 return DAG.getNode(MSP430ISD::SRA, dl,
607 VT, N->getOperand(0), N->getOperand(1));
608 case ISD::SRL:
609 return DAG.getNode(MSP430ISD::SRL, dl,
610 VT, N->getOperand(0), N->getOperand(1));
611 }
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000612
613 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
614
615 // Expand the stuff into sequence of shifts.
616 // FIXME: for some shift amounts this might be done better!
617 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
618 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000619
620 if (Opc == ISD::SRL && ShiftAmount) {
621 // Emit a special goodness here:
622 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000623 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000624 ShiftAmount -= 1;
625 }
626
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000627 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000628 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000629 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000630
631 return Victim;
632}
633
Dan Gohmand858e902010-04-17 15:26:15 +0000634SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
635 SelectionDAG &DAG) const {
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000636 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
637 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
638
639 // Create the TargetGlobalAddress node, folding in the constant offset.
Devang Patel0d881da2010-07-06 22:08:15 +0000640 SDValue Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
641 getPointerTy(), Offset);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000642 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
643 getPointerTy(), Result);
644}
645
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000646SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000647 SelectionDAG &DAG) const {
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000648 DebugLoc dl = Op.getDebugLoc();
649 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
650 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
651
652 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);;
653}
654
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000655SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
656 SelectionDAG &DAG) const {
657 DebugLoc dl = Op.getDebugLoc();
658 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
659 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(), /*isTarget=*/true);
660
661 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);;
662}
663
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000664static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000665 ISD::CondCode CC,
666 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000667 // FIXME: Handle bittests someday
668 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
669
670 // FIXME: Handle jump negative someday
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000671 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000672 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000673 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000674 case ISD::SETEQ:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000675 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000676 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000677 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000678 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000679 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000680 break;
681 case ISD::SETNE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000682 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000683 // Minor optimization: if LHS is a constant, swap operands, then the
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000684 // constant can be folded into comparison.
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +0000685 if (LHS.getOpcode() == ISD::Constant)
Anton Korobeynikov1722f062009-11-22 01:14:08 +0000686 std::swap(LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000687 break;
688 case ISD::SETULE:
689 std::swap(LHS, RHS); // FALLTHROUGH
690 case ISD::SETUGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000691 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
692 // fold constant into instruction.
693 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
694 LHS = RHS;
695 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
696 TCC = MSP430CC::COND_LO;
697 break;
698 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000699 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000700 break;
701 case ISD::SETUGT:
702 std::swap(LHS, RHS); // FALLTHROUGH
703 case ISD::SETULT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000704 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
705 // fold constant into instruction.
706 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
707 LHS = RHS;
708 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
709 TCC = MSP430CC::COND_HS;
710 break;
711 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000712 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000713 break;
714 case ISD::SETLE:
715 std::swap(LHS, RHS); // FALLTHROUGH
716 case ISD::SETGE:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000717 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
718 // fold constant into instruction.
719 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
720 LHS = RHS;
721 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
722 TCC = MSP430CC::COND_L;
723 break;
724 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000725 TCC = MSP430CC::COND_GE;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000726 break;
727 case ISD::SETGT:
728 std::swap(LHS, RHS); // FALLTHROUGH
729 case ISD::SETLT:
Anton Korobeynikov0c1ba912010-01-15 21:18:02 +0000730 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
731 // fold constant into instruction.
732 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
733 LHS = RHS;
734 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
735 TCC = MSP430CC::COND_GE;
736 break;
737 }
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000738 TCC = MSP430CC::COND_L;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000739 break;
740 }
741
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000742 TargetCC = DAG.getConstant(TCC, MVT::i8);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000743 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000744}
745
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000746
Dan Gohmand858e902010-04-17 15:26:15 +0000747SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000748 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000749 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
750 SDValue LHS = Op.getOperand(2);
751 SDValue RHS = Op.getOperand(3);
752 SDValue Dest = Op.getOperand(4);
753 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000754
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000755 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000756 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000757
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000758 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000759 Chain, Dest, TargetCC, Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000760}
761
Dan Gohmand858e902010-04-17 15:26:15 +0000762SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000763 SDValue LHS = Op.getOperand(0);
764 SDValue RHS = Op.getOperand(1);
765 DebugLoc dl = Op.getDebugLoc();
766
767 // If we are doing an AND and testing against zero, then the CMP
768 // will not be generated. The AND (or BIT) will generate the condition codes,
769 // but they are different from CMP.
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000770 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
771 // lowering & isel wouldn't diverge.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000772 bool andCC = false;
773 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
774 if (RHSC->isNullValue() && LHS.hasOneUse() &&
775 (LHS.getOpcode() == ISD::AND ||
776 (LHS.getOpcode() == ISD::TRUNCATE &&
777 LHS.getOperand(0).getOpcode() == ISD::AND))) {
778 andCC = true;
779 }
780 }
781 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
782 SDValue TargetCC;
783 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
784
785 // Get the condition codes directly from the status register, if its easy.
786 // Otherwise a branch will be generated. Note that the AND and BIT
787 // instructions generate different flags than CMP, the carry bit can be used
788 // for NE/EQ.
789 bool Invert = false;
790 bool Shift = false;
791 bool Convert = true;
792 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
793 default:
794 Convert = false;
795 break;
796 case MSP430CC::COND_HS:
797 // Res = SRW & 1, no processing is required
798 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000799 case MSP430CC::COND_LO:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000800 // Res = ~(SRW & 1)
801 Invert = true;
802 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000803 case MSP430CC::COND_NE:
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000804 if (andCC) {
805 // C = ~Z, thus Res = SRW & 1, no processing is required
806 } else {
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000807 // Res = ~((SRW >> 1) & 1)
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000808 Shift = true;
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000809 Invert = true;
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000810 }
811 break;
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000812 case MSP430CC::COND_E:
Anton Korobeynikov455080f2010-02-21 12:28:58 +0000813 Shift = true;
814 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
815 // Res = (SRW >> 1) & 1 is 1 word shorter.
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000816 break;
817 }
818 EVT VT = Op.getValueType();
819 SDValue One = DAG.getConstant(1, VT);
820 if (Convert) {
821 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
Anton Korobeynikovcb50e0b2010-01-15 21:18:18 +0000822 MVT::i16, Flag);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000823 if (Shift)
824 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
825 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
826 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
827 if (Invert)
828 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
829 return SR;
830 } else {
831 SDValue Zero = DAG.getConstant(0, VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000832 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8d1ffbd2009-12-11 23:01:29 +0000833 SmallVector<SDValue, 4> Ops;
834 Ops.push_back(One);
835 Ops.push_back(Zero);
836 Ops.push_back(TargetCC);
837 Ops.push_back(Flag);
838 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
839 }
840}
841
Dan Gohmand858e902010-04-17 15:26:15 +0000842SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
843 SelectionDAG &DAG) const {
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000844 SDValue LHS = Op.getOperand(0);
845 SDValue RHS = Op.getOperand(1);
846 SDValue TrueV = Op.getOperand(2);
847 SDValue FalseV = Op.getOperand(3);
848 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000849 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000850
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000851 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000852 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000853
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000854 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000855 SmallVector<SDValue, 4> Ops;
856 Ops.push_back(TrueV);
857 Ops.push_back(FalseV);
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000858 Ops.push_back(TargetCC);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000859 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000860
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000861 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000862}
863
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000864SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000865 SelectionDAG &DAG) const {
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000866 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000867 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000868 DebugLoc dl = Op.getDebugLoc();
869
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000871
872 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
873 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
874 DAG.getValueType(Val.getValueType()));
875}
876
Dan Gohmand858e902010-04-17 15:26:15 +0000877SDValue
878MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000879 MachineFunction &MF = DAG.getMachineFunction();
880 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
881 int ReturnAddrIndex = FuncInfo->getRAIndex();
882
883 if (ReturnAddrIndex == 0) {
884 // Set up a frame object for the return address.
885 uint64_t SlotSize = TD->getPointerSize();
886 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +0000887 true);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000888 FuncInfo->setRAIndex(ReturnAddrIndex);
889 }
890
891 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
892}
893
Dan Gohmand858e902010-04-17 15:26:15 +0000894SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
895 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +0000896 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
897 MFI->setReturnAddressIsTaken(true);
898
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000899 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
900 DebugLoc dl = Op.getDebugLoc();
901
902 if (Depth > 0) {
903 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
904 SDValue Offset =
905 DAG.getConstant(TD->getPointerSize(), MVT::i16);
906 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
907 DAG.getNode(ISD::ADD, dl, getPointerTy(),
908 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000909 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000910 }
911
912 // Just load the return address.
913 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
914 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000915 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000916}
917
Dan Gohmand858e902010-04-17 15:26:15 +0000918SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
919 SelectionDAG &DAG) const {
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000920 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
921 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000922
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000923 EVT VT = Op.getValueType();
924 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
925 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
926 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
927 MSP430::FPW, VT);
928 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000929 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
930 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000931 false, false, false, 0);
Anton Korobeynikov06ccca52009-12-07 02:28:10 +0000932 return FrameAddr;
933}
934
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000935/// getPostIndexedAddressParts - returns true by value, base pointer and
936/// offset pointer and addressing mode by reference if this node can be
937/// combined with a load / store to form a post-indexed load / store.
938bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
939 SDValue &Base,
940 SDValue &Offset,
941 ISD::MemIndexedMode &AM,
942 SelectionDAG &DAG) const {
943
944 LoadSDNode *LD = cast<LoadSDNode>(N);
945 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
946 return false;
947
948 EVT VT = LD->getMemoryVT();
949 if (VT != MVT::i8 && VT != MVT::i16)
950 return false;
951
952 if (Op->getOpcode() != ISD::ADD)
953 return false;
954
955 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
956 uint64_t RHSC = RHS->getZExtValue();
957 if ((VT == MVT::i16 && RHSC != 2) ||
958 (VT == MVT::i8 && RHSC != 1))
959 return false;
960
961 Base = Op->getOperand(0);
962 Offset = DAG.getConstant(RHSC, VT);
963 AM = ISD::POST_INC;
964 return true;
965 }
966
967 return false;
968}
969
970
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000971const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
972 switch (Opcode) {
973 default: return NULL;
974 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikov6bfcba72009-12-07 02:28:41 +0000975 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000976 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000977 case MSP430ISD::RLA: return "MSP430ISD::RLA";
978 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000979 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000980 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000981 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000982 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000983 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikov2625de32009-12-12 18:55:37 +0000984 case MSP430ISD::SHL: return "MSP430ISD::SHL";
985 case MSP430ISD::SRA: return "MSP430ISD::SRA";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000986 }
987}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000988
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000989bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
990 Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000991 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +0000992 return false;
993
994 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
995}
996
997bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
998 if (!VT1.isInteger() || !VT2.isInteger())
999 return false;
1000
1001 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1002}
1003
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001004bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001005 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001006 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
Anton Korobeynikov9afb7c52010-01-15 21:19:43 +00001007}
1008
1009bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1010 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1011 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1012}
1013
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001014//===----------------------------------------------------------------------===//
1015// Other Lowering Code
1016//===----------------------------------------------------------------------===//
1017
1018MachineBasicBlock*
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001019MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001020 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001021 MachineFunction *F = BB->getParent();
1022 MachineRegisterInfo &RI = F->getRegInfo();
1023 DebugLoc dl = MI->getDebugLoc();
1024 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1025
1026 unsigned Opc;
1027 const TargetRegisterClass * RC;
1028 switch (MI->getOpcode()) {
1029 default:
1030 assert(0 && "Invalid shift opcode!");
1031 case MSP430::Shl8:
1032 Opc = MSP430::SHL8r1;
1033 RC = MSP430::GR8RegisterClass;
1034 break;
1035 case MSP430::Shl16:
1036 Opc = MSP430::SHL16r1;
1037 RC = MSP430::GR16RegisterClass;
1038 break;
1039 case MSP430::Sra8:
1040 Opc = MSP430::SAR8r1;
1041 RC = MSP430::GR8RegisterClass;
1042 break;
1043 case MSP430::Sra16:
1044 Opc = MSP430::SAR16r1;
1045 RC = MSP430::GR16RegisterClass;
1046 break;
1047 case MSP430::Srl8:
1048 Opc = MSP430::SAR8r1c;
1049 RC = MSP430::GR8RegisterClass;
1050 break;
1051 case MSP430::Srl16:
1052 Opc = MSP430::SAR16r1c;
1053 RC = MSP430::GR16RegisterClass;
1054 break;
1055 }
1056
1057 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1058 MachineFunction::iterator I = BB;
1059 ++I;
1060
1061 // Create loop block
1062 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1063 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1064
1065 F->insert(I, LoopBB);
1066 F->insert(I, RemBB);
1067
1068 // Update machine-CFG edges by transferring all successors of the current
1069 // block to the block containing instructions after shift.
Dan Gohman14152b42010-07-06 20:24:04 +00001070 RemBB->splice(RemBB->begin(), BB,
1071 llvm::next(MachineBasicBlock::iterator(MI)),
1072 BB->end());
1073 RemBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001074
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001075 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1076 BB->addSuccessor(LoopBB);
1077 BB->addSuccessor(RemBB);
1078 LoopBB->addSuccessor(RemBB);
1079 LoopBB->addSuccessor(LoopBB);
1080
1081 unsigned ShiftAmtReg = RI.createVirtualRegister(MSP430::GR8RegisterClass);
1082 unsigned ShiftAmtReg2 = RI.createVirtualRegister(MSP430::GR8RegisterClass);
1083 unsigned ShiftReg = RI.createVirtualRegister(RC);
1084 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1085 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1086 unsigned SrcReg = MI->getOperand(1).getReg();
1087 unsigned DstReg = MI->getOperand(0).getReg();
1088
1089 // BB:
1090 // cmp 0, N
1091 // je RemBB
Anton Korobeynikovf7ed9792010-01-15 01:29:49 +00001092 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1093 .addReg(ShiftAmtSrcReg).addImm(0);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001094 BuildMI(BB, dl, TII.get(MSP430::JCC))
1095 .addMBB(RemBB)
1096 .addImm(MSP430CC::COND_E);
1097
1098 // LoopBB:
1099 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1100 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1101 // ShiftReg2 = shift ShiftReg
1102 // ShiftAmt2 = ShiftAmt - 1;
1103 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1104 .addReg(SrcReg).addMBB(BB)
1105 .addReg(ShiftReg2).addMBB(LoopBB);
1106 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1107 .addReg(ShiftAmtSrcReg).addMBB(BB)
1108 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1109 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1110 .addReg(ShiftReg);
1111 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1112 .addReg(ShiftAmtReg).addImm(1);
1113 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1114 .addMBB(LoopBB)
1115 .addImm(MSP430CC::COND_NE);
1116
1117 // RemBB:
1118 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
Dan Gohman14152b42010-07-06 20:24:04 +00001119 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001120 .addReg(SrcReg).addMBB(BB)
1121 .addReg(ShiftReg2).addMBB(LoopBB);
1122
Dan Gohman14152b42010-07-06 20:24:04 +00001123 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001124 return RemBB;
1125}
1126
1127MachineBasicBlock*
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001128MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001129 MachineBasicBlock *BB) const {
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001130 unsigned Opc = MI->getOpcode();
1131
1132 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1133 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1134 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001135 return EmitShiftInstr(MI, BB);
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001136
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001137 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1138 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikov2625de32009-12-12 18:55:37 +00001139
1140 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001141 "Unexpected instr type to insert");
1142
1143 // To "insert" a SELECT instruction, we actually have to insert the diamond
1144 // control-flow pattern. The incoming instruction knows the destination vreg
1145 // to set, the condition code register to branch on, the true/false values to
1146 // select between, and a branch opcode to use.
1147 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1148 MachineFunction::iterator I = BB;
1149 ++I;
1150
1151 // thisMBB:
1152 // ...
1153 // TrueVal = ...
1154 // cmpTY ccX, r1, r2
1155 // jCC copy1MBB
1156 // fallthrough --> copy0MBB
1157 MachineBasicBlock *thisMBB = BB;
1158 MachineFunction *F = BB->getParent();
1159 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1160 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001161 F->insert(I, copy0MBB);
1162 F->insert(I, copy1MBB);
1163 // Update machine-CFG edges by transferring all successors of the current
1164 // block to the new block which will contain the Phi node for the select.
Dan Gohman14152b42010-07-06 20:24:04 +00001165 copy1MBB->splice(copy1MBB->begin(), BB,
1166 llvm::next(MachineBasicBlock::iterator(MI)),
1167 BB->end());
1168 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001169 // Next, add the true and fallthrough blocks as its successors.
1170 BB->addSuccessor(copy0MBB);
1171 BB->addSuccessor(copy1MBB);
1172
Dan Gohman14152b42010-07-06 20:24:04 +00001173 BuildMI(BB, dl, TII.get(MSP430::JCC))
1174 .addMBB(copy1MBB)
1175 .addImm(MI->getOperand(3).getImm());
1176
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001177 // copy0MBB:
1178 // %FalseValue = ...
1179 // # fallthrough to copy1MBB
1180 BB = copy0MBB;
1181
1182 // Update machine-CFG edges
1183 BB->addSuccessor(copy1MBB);
1184
1185 // copy1MBB:
1186 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1187 // ...
1188 BB = copy1MBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001189 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001190 MI->getOperand(0).getReg())
1191 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1192 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1193
Dan Gohman14152b42010-07-06 20:24:04 +00001194 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov8b528e52009-05-03 13:12:23 +00001195 return BB;
1196}