blob: 47a977798da9a438acd3dac69c7133a809be187b [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 Korobeynikovf2c3e172009-05-03 12:57:15 +000035#include "llvm/Support/Debug.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000036#include "llvm/Support/ErrorHandling.h"
Chris Lattner4437ae22009-08-23 07:05:07 +000037#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000038#include "llvm/ADT/VectorExtras.h"
39using namespace llvm;
40
41MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000042 TargetLowering(tm, new TargetLoweringObjectFileELF()),
43 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000044
45 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000046 addRegisterClass(MVT::i8, MSP430::GR8RegisterClass);
47 addRegisterClass(MVT::i16, MSP430::GR16RegisterClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000048
49 // Compute derived properties from the register classes
50 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000051
Anton Korobeynikov1476d972009-05-03 13:03:14 +000052 // Provide all sorts of operation actions
53
54 // Division is expensive
55 setIntDivIsCheap(false);
56
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000057 // Even if we have only 1 bit shift here, we can perform
58 // shifts of the whole bitwidth 1 bit per step.
Owen Anderson825b72b2009-08-11 20:47:22 +000059 setShiftAmountType(MVT::i8);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000060
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000061 setStackPointerRegisterToSaveRestore(MSP430::SPW);
62 setBooleanContents(ZeroOrOneBooleanContent);
63 setSchedulingPreference(SchedulingForLatency);
64
Anton Korobeynikov6534f832009-11-07 17:15:06 +000065 // We have post-incremented loads / stores
66 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
67 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
68
69 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
70 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
71 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
72 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +000073 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000074
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000075 // We don't have any truncstores
Owen Anderson825b72b2009-08-11 20:47:22 +000076 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000077
Owen Anderson825b72b2009-08-11 20:47:22 +000078 setOperationAction(ISD::SRA, MVT::i8, Custom);
79 setOperationAction(ISD::SHL, MVT::i8, Custom);
80 setOperationAction(ISD::SRL, MVT::i8, Custom);
81 setOperationAction(ISD::SRA, MVT::i16, Custom);
82 setOperationAction(ISD::SHL, MVT::i16, Custom);
83 setOperationAction(ISD::SRL, MVT::i16, Custom);
84 setOperationAction(ISD::ROTL, MVT::i8, Expand);
85 setOperationAction(ISD::ROTR, MVT::i8, Expand);
86 setOperationAction(ISD::ROTL, MVT::i16, Expand);
87 setOperationAction(ISD::ROTR, MVT::i16, Expand);
88 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
89 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
90 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
91 setOperationAction(ISD::BRIND, MVT::Other, Expand);
92 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
93 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
94 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
95 setOperationAction(ISD::SETCC, MVT::i8, Expand);
96 setOperationAction(ISD::SETCC, MVT::i16, Expand);
97 setOperationAction(ISD::SELECT, MVT::i8, Expand);
98 setOperationAction(ISD::SELECT, MVT::i16, Expand);
99 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
100 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
101 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov379a0872009-08-25 17:00:23 +0000102 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
103 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000104
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
106 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
107 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
108 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
109 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
110 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000111
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
113 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
114 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
115 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
116 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
117 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000118
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000120
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000121 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000122 setOperationAction(ISD::MUL, MVT::i8, Expand);
123 setOperationAction(ISD::MULHS, MVT::i8, Expand);
124 setOperationAction(ISD::MULHU, MVT::i8, Expand);
125 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
126 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::MUL, MVT::i16, Expand);
128 setOperationAction(ISD::MULHS, MVT::i16, Expand);
129 setOperationAction(ISD::MULHU, MVT::i16, Expand);
130 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
131 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikovf2f54022009-05-03 13:18:33 +0000132
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000133 setOperationAction(ISD::UDIV, MVT::i8, Expand);
134 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
135 setOperationAction(ISD::UREM, MVT::i8, Expand);
136 setOperationAction(ISD::SDIV, MVT::i8, Expand);
137 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
138 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::UDIV, MVT::i16, Expand);
140 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
141 setOperationAction(ISD::UREM, MVT::i16, Expand);
142 setOperationAction(ISD::SDIV, MVT::i16, Expand);
143 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
144 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000145}
146
Anton Korobeynikovb8639f52009-05-03 13:03:50 +0000147SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000148 switch (Op.getOpcode()) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000149 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000150 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000151 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000152 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000153 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000154 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
155 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000156 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000157 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000158 llvm_unreachable("unimplemented operand");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000159 return SDValue();
160 }
161}
162
Bill Wendlingb4202b82009-07-01 18:50:55 +0000163/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000164unsigned MSP430TargetLowering::getFunctionAlignment(const Function *F) const {
165 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4;
166}
167
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000168//===----------------------------------------------------------------------===//
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000169// MSP430 Inline Assembly Support
170//===----------------------------------------------------------------------===//
171
172/// getConstraintType - Given a constraint letter, return the type of
173/// constraint it is for this target.
174TargetLowering::ConstraintType
175MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
176 if (Constraint.size() == 1) {
177 switch (Constraint[0]) {
178 case 'r':
179 return C_RegisterClass;
180 default:
181 break;
182 }
183 }
184 return TargetLowering::getConstraintType(Constraint);
185}
186
187std::pair<unsigned, const TargetRegisterClass*>
188MSP430TargetLowering::
189getRegForInlineAsmConstraint(const std::string &Constraint,
190 EVT VT) const {
191 if (Constraint.size() == 1) {
192 // GCC Constraint Letters
193 switch (Constraint[0]) {
194 default: break;
195 case 'r': // GENERAL_REGS
196 if (VT == MVT::i8)
197 return std::make_pair(0U, MSP430::GR8RegisterClass);
198
199 return std::make_pair(0U, MSP430::GR16RegisterClass);
200 }
201 }
202
203 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
204}
205
206//===----------------------------------------------------------------------===//
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000207// Calling Convention Implementation
208//===----------------------------------------------------------------------===//
209
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000210#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000211
Dan Gohman98ca4f22009-08-05 01:29:28 +0000212SDValue
213MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000214 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000215 bool isVarArg,
216 const SmallVectorImpl<ISD::InputArg>
217 &Ins,
218 DebugLoc dl,
219 SelectionDAG &DAG,
220 SmallVectorImpl<SDValue> &InVals) {
221
222 switch (CallConv) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000223 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000224 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000225 case CallingConv::C:
226 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000227 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000228 }
229}
230
Dan Gohman98ca4f22009-08-05 01:29:28 +0000231SDValue
232MSP430TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000233 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000234 bool isTailCall,
235 const SmallVectorImpl<ISD::OutputArg> &Outs,
236 const SmallVectorImpl<ISD::InputArg> &Ins,
237 DebugLoc dl, SelectionDAG &DAG,
238 SmallVectorImpl<SDValue> &InVals) {
239
240 switch (CallConv) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000241 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000242 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000243 case CallingConv::Fast:
244 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000245 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
246 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000247 }
248}
249
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000250/// LowerCCCArguments - transform physical registers into virtual registers and
251/// generate load operations for arguments places on the stack.
252// FIXME: struct return stuff
253// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000254SDValue
255MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000256 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000257 bool isVarArg,
258 const SmallVectorImpl<ISD::InputArg>
259 &Ins,
260 DebugLoc dl,
261 SelectionDAG &DAG,
262 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000263 MachineFunction &MF = DAG.getMachineFunction();
264 MachineFrameInfo *MFI = MF.getFrameInfo();
265 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000266
267 // Assign locations to all of the incoming arguments.
268 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000269 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
270 ArgLocs, *DAG.getContext());
271 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000272
273 assert(!isVarArg && "Varargs not supported yet");
274
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000275 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
276 CCValAssign &VA = ArgLocs[i];
277 if (VA.isRegLoc()) {
278 // Arguments passed in registers
Owen Andersone50ed302009-08-10 22:56:29 +0000279 EVT RegVT = VA.getLocVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 switch (RegVT.getSimpleVT().SimpleTy) {
Torok Edwin804e0fe2009-07-08 19:04:27 +0000281 default:
282 {
Torok Edwindac237e2009-07-08 20:53:28 +0000283#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000284 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000286#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000287 llvm_unreachable(0);
Torok Edwin804e0fe2009-07-08 19:04:27 +0000288 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 case MVT::i16:
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000290 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000291 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000292 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000293 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000294
295 // If this is an 8-bit value, it is really passed promoted to 16
296 // bits. Insert an assert[sz]ext to capture this, then truncate to the
297 // right size.
298 if (VA.getLocInfo() == CCValAssign::SExt)
299 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
300 DAG.getValueType(VA.getValVT()));
301 else if (VA.getLocInfo() == CCValAssign::ZExt)
302 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
303 DAG.getValueType(VA.getValVT()));
304
305 if (VA.getLocInfo() != CCValAssign::Full)
306 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
307
Dan Gohman98ca4f22009-08-05 01:29:28 +0000308 InVals.push_back(ArgValue);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000309 }
310 } else {
311 // Sanity check
312 assert(VA.isMemLoc());
313 // Load the argument to a virtual register
314 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
315 if (ObjSize > 2) {
Chris Lattner4437ae22009-08-23 07:05:07 +0000316 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 << VA.getLocVT().getSimpleVT().SimpleTy
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000318 << "\n";
319 }
320 // Create the frame index object for this incoming parameter...
321 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
322
323 // Create the SelectionDAG nodes corresponding to a load
324 //from this parameter
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000326 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +0000327 PseudoSourceValue::getFixedStack(FI), 0));
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000328 }
329 }
330
Dan Gohman98ca4f22009-08-05 01:29:28 +0000331 return Chain;
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000332}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000333
Dan Gohman98ca4f22009-08-05 01:29:28 +0000334SDValue
335MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000336 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000337 const SmallVectorImpl<ISD::OutputArg> &Outs,
338 DebugLoc dl, SelectionDAG &DAG) {
339
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000340 // CCValAssign - represent the assignment of the return value to a location
341 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000342
343 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000344 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
345 RVLocs, *DAG.getContext());
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000346
Dan Gohman98ca4f22009-08-05 01:29:28 +0000347 // Analize return values.
348 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000349
350 // If this is the first return lowered for this function, add the regs to the
351 // liveout set for the function.
352 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
353 for (unsigned i = 0; i != RVLocs.size(); ++i)
354 if (RVLocs[i].isRegLoc())
355 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
356 }
357
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000358 SDValue Flag;
359
360 // Copy the result values into the output registers.
361 for (unsigned i = 0; i != RVLocs.size(); ++i) {
362 CCValAssign &VA = RVLocs[i];
363 assert(VA.isRegLoc() && "Can only return in registers!");
364
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000365 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +0000366 Outs[i].Val, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000367
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000368 // Guarantee that all emitted copies are stuck together,
369 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000370 Flag = Chain.getValue(1);
371 }
372
373 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000375
376 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000378}
379
Anton Korobeynikov44288852009-05-03 13:07:31 +0000380/// LowerCCCCallTo - functions arguments are copied from virtual regs to
381/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
382/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000383SDValue
384MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000385 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000386 bool isTailCall,
387 const SmallVectorImpl<ISD::OutputArg>
388 &Outs,
389 const SmallVectorImpl<ISD::InputArg> &Ins,
390 DebugLoc dl, SelectionDAG &DAG,
391 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000392 // Analyze operands of the call, assigning locations to each operand.
393 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000394 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
395 ArgLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000396
Dan Gohman98ca4f22009-08-05 01:29:28 +0000397 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000398
399 // Get a count of how many bytes are to be pushed on the stack.
400 unsigned NumBytes = CCInfo.getNextStackOffset();
401
402 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
403 getPointerTy(), true));
404
405 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
406 SmallVector<SDValue, 12> MemOpChains;
407 SDValue StackPtr;
408
409 // Walk the register/memloc assignments, inserting copies/loads.
410 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
411 CCValAssign &VA = ArgLocs[i];
412
Dan Gohman98ca4f22009-08-05 01:29:28 +0000413 SDValue Arg = Outs[i].Val;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000414
415 // Promote the value if needed.
416 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000417 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000418 case CCValAssign::Full: break;
419 case CCValAssign::SExt:
420 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
421 break;
422 case CCValAssign::ZExt:
423 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
424 break;
425 case CCValAssign::AExt:
426 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
427 break;
428 }
429
430 // Arguments that can be passed on register must be kept at RegsToPass
431 // vector
432 if (VA.isRegLoc()) {
433 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
434 } else {
435 assert(VA.isMemLoc());
436
437 if (StackPtr.getNode() == 0)
438 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
439
440 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
441 StackPtr,
442 DAG.getIntPtrConstant(VA.getLocMemOffset()));
443
444
445 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
446 PseudoSourceValue::getStack(),
447 VA.getLocMemOffset()));
448 }
449 }
450
451 // Transform all store nodes into one single node because all store nodes are
452 // independent of each other.
453 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov44288852009-05-03 13:07:31 +0000455 &MemOpChains[0], MemOpChains.size());
456
457 // Build a sequence of copy-to-reg nodes chained together with token chain and
458 // flag operands which copy the outgoing args into registers. The InFlag in
459 // necessary since all emited instructions must be stuck together.
460 SDValue InFlag;
461 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
462 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
463 RegsToPass[i].second, InFlag);
464 InFlag = Chain.getValue(1);
465 }
466
467 // If the callee is a GlobalAddress node (quite common, every direct call is)
468 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
469 // Likewise ExternalSymbol -> TargetExternalSymbol.
470 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000472 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000474
475 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000477 SmallVector<SDValue, 8> Ops;
478 Ops.push_back(Chain);
479 Ops.push_back(Callee);
480
481 // Add argument registers to the end of the list so that they are
482 // known live into the call.
483 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
484 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
485 RegsToPass[i].second.getValueType()));
486
487 if (InFlag.getNode())
488 Ops.push_back(InFlag);
489
490 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
491 InFlag = Chain.getValue(1);
492
493 // Create the CALLSEQ_END node.
494 Chain = DAG.getCALLSEQ_END(Chain,
495 DAG.getConstant(NumBytes, getPointerTy(), true),
496 DAG.getConstant(0, getPointerTy(), true),
497 InFlag);
498 InFlag = Chain.getValue(1);
499
500 // Handle result values, copying them out of physregs into vregs that we
501 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000502 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
503 DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000504}
505
Dan Gohman98ca4f22009-08-05 01:29:28 +0000506/// LowerCallResult - Lower the result values of a call into the
507/// appropriate copies out of appropriate physical registers.
508///
509SDValue
Anton Korobeynikov44288852009-05-03 13:07:31 +0000510MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000511 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000512 const SmallVectorImpl<ISD::InputArg> &Ins,
513 DebugLoc dl, SelectionDAG &DAG,
514 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000515
516 // Assign locations to each value returned by this call.
517 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000518 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000519 RVLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000520
Dan Gohman98ca4f22009-08-05 01:29:28 +0000521 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000522
523 // Copy all of the result registers out of their specified physreg.
524 for (unsigned i = 0; i != RVLocs.size(); ++i) {
525 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
526 RVLocs[i].getValVT(), InFlag).getValue(1);
527 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000528 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000529 }
530
Dan Gohman98ca4f22009-08-05 01:29:28 +0000531 return Chain;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000532}
533
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000534SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
535 SelectionDAG &DAG) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000536 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000537 SDNode* N = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000538 EVT VT = Op.getValueType();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000539 DebugLoc dl = N->getDebugLoc();
540
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000541 // We currently only lower shifts of constant argument.
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000542 if (!isa<ConstantSDNode>(N->getOperand(1)))
543 return SDValue();
544
545 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
546
547 // Expand the stuff into sequence of shifts.
548 // FIXME: for some shift amounts this might be done better!
549 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
550 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000551
552 if (Opc == ISD::SRL && ShiftAmount) {
553 // Emit a special goodness here:
554 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000555 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000556 ShiftAmount -= 1;
557 }
558
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000559 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000560 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000561 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000562
563 return Victim;
564}
565
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000566SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
567 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
568 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
569
570 // Create the TargetGlobalAddress node, folding in the constant offset.
571 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
572 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
573 getPointerTy(), Result);
574}
575
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000576SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
577 SelectionDAG &DAG) {
578 DebugLoc dl = Op.getDebugLoc();
579 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
580 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
581
582 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);;
583}
584
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000585static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000586 ISD::CondCode CC,
587 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000588 // FIXME: Handle bittests someday
589 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
590
591 // FIXME: Handle jump negative someday
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000592 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000593 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000594 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000595 case ISD::SETEQ:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000596 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000597 break;
598 case ISD::SETNE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000599 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000600 break;
601 case ISD::SETULE:
602 std::swap(LHS, RHS); // FALLTHROUGH
603 case ISD::SETUGE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000604 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000605 break;
606 case ISD::SETUGT:
607 std::swap(LHS, RHS); // FALLTHROUGH
608 case ISD::SETULT:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000609 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000610 break;
611 case ISD::SETLE:
612 std::swap(LHS, RHS); // FALLTHROUGH
613 case ISD::SETGE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000614 TCC = MSP430CC::COND_GE;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000615 break;
616 case ISD::SETGT:
617 std::swap(LHS, RHS); // FALLTHROUGH
618 case ISD::SETLT:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000619 TCC = MSP430CC::COND_L;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000620 break;
621 }
622
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000623 TargetCC = DAG.getConstant(TCC, MVT::i8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Flag, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000625}
626
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000627
628SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000629 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000630 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
631 SDValue LHS = Op.getOperand(2);
632 SDValue RHS = Op.getOperand(3);
633 SDValue Dest = Op.getOperand(4);
634 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000635
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000636 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000637 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000638
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000639 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000640 Chain, Dest, TargetCC, Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000641}
642
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000643SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
644 SDValue LHS = Op.getOperand(0);
645 SDValue RHS = Op.getOperand(1);
646 SDValue TrueV = Op.getOperand(2);
647 SDValue FalseV = Op.getOperand(3);
648 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000649 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000650
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000651 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000652 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000653
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000655 SmallVector<SDValue, 4> Ops;
656 Ops.push_back(TrueV);
657 Ops.push_back(FalseV);
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000658 Ops.push_back(TargetCC);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000659 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000660
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000661 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000662}
663
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000664SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
665 SelectionDAG &DAG) {
666 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000667 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000668 DebugLoc dl = Op.getDebugLoc();
669
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000671
672 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
673 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
674 DAG.getValueType(Val.getValueType()));
675}
676
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000677/// getPostIndexedAddressParts - returns true by value, base pointer and
678/// offset pointer and addressing mode by reference if this node can be
679/// combined with a load / store to form a post-indexed load / store.
680bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
681 SDValue &Base,
682 SDValue &Offset,
683 ISD::MemIndexedMode &AM,
684 SelectionDAG &DAG) const {
685
686 LoadSDNode *LD = cast<LoadSDNode>(N);
687 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
688 return false;
689
690 EVT VT = LD->getMemoryVT();
691 if (VT != MVT::i8 && VT != MVT::i16)
692 return false;
693
694 if (Op->getOpcode() != ISD::ADD)
695 return false;
696
697 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
698 uint64_t RHSC = RHS->getZExtValue();
699 if ((VT == MVT::i16 && RHSC != 2) ||
700 (VT == MVT::i8 && RHSC != 1))
701 return false;
702
703 Base = Op->getOperand(0);
704 Offset = DAG.getConstant(RHSC, VT);
705 AM = ISD::POST_INC;
706 return true;
707 }
708
709 return false;
710}
711
712
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000713const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
714 switch (Opcode) {
715 default: return NULL;
716 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000717 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000718 case MSP430ISD::RLA: return "MSP430ISD::RLA";
719 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000720 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000721 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000722 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000723 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000724 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000725 }
726}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000727
728//===----------------------------------------------------------------------===//
729// Other Lowering Code
730//===----------------------------------------------------------------------===//
731
732MachineBasicBlock*
733MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000734 MachineBasicBlock *BB,
735 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000736 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
737 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda4d2f62009-05-08 18:51:21 +0000738 assert((MI->getOpcode() == MSP430::Select16 ||
739 MI->getOpcode() == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000740 "Unexpected instr type to insert");
741
742 // To "insert" a SELECT instruction, we actually have to insert the diamond
743 // control-flow pattern. The incoming instruction knows the destination vreg
744 // to set, the condition code register to branch on, the true/false values to
745 // select between, and a branch opcode to use.
746 const BasicBlock *LLVM_BB = BB->getBasicBlock();
747 MachineFunction::iterator I = BB;
748 ++I;
749
750 // thisMBB:
751 // ...
752 // TrueVal = ...
753 // cmpTY ccX, r1, r2
754 // jCC copy1MBB
755 // fallthrough --> copy0MBB
756 MachineBasicBlock *thisMBB = BB;
757 MachineFunction *F = BB->getParent();
758 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
759 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
760 BuildMI(BB, dl, TII.get(MSP430::JCC))
761 .addMBB(copy1MBB)
762 .addImm(MI->getOperand(3).getImm());
763 F->insert(I, copy0MBB);
764 F->insert(I, copy1MBB);
Evan Chengce319102009-09-19 09:51:03 +0000765 // Inform sdisel of the edge changes.
766 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
767 SE = BB->succ_end(); SI != SE; ++SI)
768 EM->insert(std::make_pair(*SI, copy1MBB));
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000769 // Update machine-CFG edges by transferring all successors of the current
770 // block to the new block which will contain the Phi node for the select.
771 copy1MBB->transferSuccessors(BB);
772 // Next, add the true and fallthrough blocks as its successors.
773 BB->addSuccessor(copy0MBB);
774 BB->addSuccessor(copy1MBB);
775
776 // copy0MBB:
777 // %FalseValue = ...
778 // # fallthrough to copy1MBB
779 BB = copy0MBB;
780
781 // Update machine-CFG edges
782 BB->addSuccessor(copy1MBB);
783
784 // copy1MBB:
785 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
786 // ...
787 BB = copy1MBB;
788 BuildMI(BB, dl, TII.get(MSP430::PHI),
789 MI->getOperand(0).getReg())
790 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
791 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
792
793 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
794 return BB;
795}