blob: a5c4e9ffc5644cad0d85ea32f6ce019d7902d79c [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"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000037#include "llvm/ADT/VectorExtras.h"
38using namespace llvm;
39
40MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000041 TargetLowering(tm, new TargetLoweringObjectFileELF()),
42 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000043
44 // Set up the register classes.
Owen Andersone50ed302009-08-10 22:56:29 +000045 addRegisterClass(EVT::i8, MSP430::GR8RegisterClass);
46 addRegisterClass(EVT::i16, MSP430::GR16RegisterClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000047
48 // Compute derived properties from the register classes
49 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000050
Anton Korobeynikov1476d972009-05-03 13:03:14 +000051 // Provide all sorts of operation actions
52
53 // Division is expensive
54 setIntDivIsCheap(false);
55
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000056 // Even if we have only 1 bit shift here, we can perform
57 // shifts of the whole bitwidth 1 bit per step.
Owen Andersone50ed302009-08-10 22:56:29 +000058 setShiftAmountType(EVT::i8);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000059
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000060 setStackPointerRegisterToSaveRestore(MSP430::SPW);
61 setBooleanContents(ZeroOrOneBooleanContent);
62 setSchedulingPreference(SchedulingForLatency);
63
Owen Andersone50ed302009-08-10 22:56:29 +000064 setLoadExtAction(ISD::EXTLOAD, EVT::i1, Promote);
65 setLoadExtAction(ISD::SEXTLOAD, EVT::i1, Promote);
66 setLoadExtAction(ISD::ZEXTLOAD, EVT::i1, Promote);
67 setLoadExtAction(ISD::SEXTLOAD, EVT::i8, Expand);
68 setLoadExtAction(ISD::SEXTLOAD, EVT::i16, Expand);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000069
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000070 // We don't have any truncstores
Owen Andersone50ed302009-08-10 22:56:29 +000071 setTruncStoreAction(EVT::i16, EVT::i8, Expand);
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000072
Owen Andersone50ed302009-08-10 22:56:29 +000073 setOperationAction(ISD::SRA, EVT::i8, Custom);
74 setOperationAction(ISD::SHL, EVT::i8, Custom);
75 setOperationAction(ISD::SRL, EVT::i8, Custom);
76 setOperationAction(ISD::SRA, EVT::i16, Custom);
77 setOperationAction(ISD::SHL, EVT::i16, Custom);
78 setOperationAction(ISD::SRL, EVT::i16, Custom);
79 setOperationAction(ISD::ROTL, EVT::i8, Expand);
80 setOperationAction(ISD::ROTR, EVT::i8, Expand);
81 setOperationAction(ISD::ROTL, EVT::i16, Expand);
82 setOperationAction(ISD::ROTR, EVT::i16, Expand);
83 setOperationAction(ISD::GlobalAddress, EVT::i16, Custom);
84 setOperationAction(ISD::ExternalSymbol, EVT::i16, Custom);
85 setOperationAction(ISD::BR_JT, EVT::Other, Expand);
86 setOperationAction(ISD::BRIND, EVT::Other, Expand);
87 setOperationAction(ISD::BR_CC, EVT::i8, Custom);
88 setOperationAction(ISD::BR_CC, EVT::i16, Custom);
89 setOperationAction(ISD::BRCOND, EVT::Other, Expand);
90 setOperationAction(ISD::SETCC, EVT::i8, Expand);
91 setOperationAction(ISD::SETCC, EVT::i16, Expand);
92 setOperationAction(ISD::SELECT, EVT::i8, Expand);
93 setOperationAction(ISD::SELECT, EVT::i16, Expand);
94 setOperationAction(ISD::SELECT_CC, EVT::i8, Custom);
95 setOperationAction(ISD::SELECT_CC, EVT::i16, Custom);
96 setOperationAction(ISD::SIGN_EXTEND, EVT::i16, Custom);
Anton Korobeynikov8725bd22009-05-03 13:14:25 +000097
Owen Andersone50ed302009-08-10 22:56:29 +000098 setOperationAction(ISD::CTTZ, EVT::i8, Expand);
99 setOperationAction(ISD::CTTZ, EVT::i16, Expand);
100 setOperationAction(ISD::CTLZ, EVT::i8, Expand);
101 setOperationAction(ISD::CTLZ, EVT::i16, Expand);
102 setOperationAction(ISD::CTPOP, EVT::i8, Expand);
103 setOperationAction(ISD::CTPOP, EVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000104
Owen Andersone50ed302009-08-10 22:56:29 +0000105 setOperationAction(ISD::SHL_PARTS, EVT::i8, Expand);
106 setOperationAction(ISD::SHL_PARTS, EVT::i16, Expand);
107 setOperationAction(ISD::SRL_PARTS, EVT::i8, Expand);
108 setOperationAction(ISD::SRL_PARTS, EVT::i16, Expand);
109 setOperationAction(ISD::SRA_PARTS, EVT::i8, Expand);
110 setOperationAction(ISD::SRA_PARTS, EVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000111
Owen Andersone50ed302009-08-10 22:56:29 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i1, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000113
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000114 // FIXME: Implement efficiently multiplication by a constant
Owen Andersone50ed302009-08-10 22:56:29 +0000115 setOperationAction(ISD::MUL, EVT::i16, Expand);
116 setOperationAction(ISD::MULHS, EVT::i16, Expand);
117 setOperationAction(ISD::MULHU, EVT::i16, Expand);
118 setOperationAction(ISD::SMUL_LOHI, EVT::i16, Expand);
119 setOperationAction(ISD::UMUL_LOHI, EVT::i16, Expand);
Anton Korobeynikovf2f54022009-05-03 13:18:33 +0000120
Owen Andersone50ed302009-08-10 22:56:29 +0000121 setOperationAction(ISD::UDIV, EVT::i16, Expand);
122 setOperationAction(ISD::UDIVREM, EVT::i16, Expand);
123 setOperationAction(ISD::UREM, EVT::i16, Expand);
124 setOperationAction(ISD::SDIV, EVT::i16, Expand);
125 setOperationAction(ISD::SDIVREM, EVT::i16, Expand);
126 setOperationAction(ISD::SREM, EVT::i16, Expand);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000127}
128
Anton Korobeynikovb8639f52009-05-03 13:03:50 +0000129SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000130 switch (Op.getOpcode()) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000131 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000132 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000133 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000134 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000135 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000136 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
137 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000138 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000139 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000140 llvm_unreachable("unimplemented operand");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000141 return SDValue();
142 }
143}
144
Bill Wendlingb4202b82009-07-01 18:50:55 +0000145/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000146unsigned MSP430TargetLowering::getFunctionAlignment(const Function *F) const {
147 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4;
148}
149
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000150//===----------------------------------------------------------------------===//
151// Calling Convention Implementation
152//===----------------------------------------------------------------------===//
153
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000154#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000155
Dan Gohman98ca4f22009-08-05 01:29:28 +0000156SDValue
157MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
158 unsigned CallConv,
159 bool isVarArg,
160 const SmallVectorImpl<ISD::InputArg>
161 &Ins,
162 DebugLoc dl,
163 SelectionDAG &DAG,
164 SmallVectorImpl<SDValue> &InVals) {
165
166 switch (CallConv) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000167 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000168 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000169 case CallingConv::C:
170 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000171 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000172 }
173}
174
Dan Gohman98ca4f22009-08-05 01:29:28 +0000175SDValue
176MSP430TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
177 unsigned CallConv, bool isVarArg,
178 bool isTailCall,
179 const SmallVectorImpl<ISD::OutputArg> &Outs,
180 const SmallVectorImpl<ISD::InputArg> &Ins,
181 DebugLoc dl, SelectionDAG &DAG,
182 SmallVectorImpl<SDValue> &InVals) {
183
184 switch (CallConv) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000185 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000186 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000187 case CallingConv::Fast:
188 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000189 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
190 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000191 }
192}
193
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000194/// LowerCCCArguments - transform physical registers into virtual registers and
195/// generate load operations for arguments places on the stack.
196// FIXME: struct return stuff
197// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000198SDValue
199MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
200 unsigned CallConv,
201 bool isVarArg,
202 const SmallVectorImpl<ISD::InputArg>
203 &Ins,
204 DebugLoc dl,
205 SelectionDAG &DAG,
206 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000207 MachineFunction &MF = DAG.getMachineFunction();
208 MachineFrameInfo *MFI = MF.getFrameInfo();
209 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000210
211 // Assign locations to all of the incoming arguments.
212 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000213 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
214 ArgLocs, *DAG.getContext());
215 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000216
217 assert(!isVarArg && "Varargs not supported yet");
218
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000219 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
220 CCValAssign &VA = ArgLocs[i];
221 if (VA.isRegLoc()) {
222 // Arguments passed in registers
Owen Andersone50ed302009-08-10 22:56:29 +0000223 EVT RegVT = VA.getLocVT();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000224 switch (RegVT.getSimpleVT()) {
Torok Edwin804e0fe2009-07-08 19:04:27 +0000225 default:
226 {
Torok Edwindac237e2009-07-08 20:53:28 +0000227#ifndef NDEBUG
Dan Gohman98ca4f22009-08-05 01:29:28 +0000228 cerr << "LowerFormalArguments Unhandled argument type: "
Torok Edwindac237e2009-07-08 20:53:28 +0000229 << RegVT.getSimpleVT() << "\n";
230#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000231 llvm_unreachable(0);
Torok Edwin804e0fe2009-07-08 19:04:27 +0000232 }
Owen Andersone50ed302009-08-10 22:56:29 +0000233 case EVT::i16:
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000234 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000235 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000236 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000237 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000238
239 // If this is an 8-bit value, it is really passed promoted to 16
240 // bits. Insert an assert[sz]ext to capture this, then truncate to the
241 // right size.
242 if (VA.getLocInfo() == CCValAssign::SExt)
243 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
244 DAG.getValueType(VA.getValVT()));
245 else if (VA.getLocInfo() == CCValAssign::ZExt)
246 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
247 DAG.getValueType(VA.getValVT()));
248
249 if (VA.getLocInfo() != CCValAssign::Full)
250 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
251
Dan Gohman98ca4f22009-08-05 01:29:28 +0000252 InVals.push_back(ArgValue);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000253 }
254 } else {
255 // Sanity check
256 assert(VA.isMemLoc());
257 // Load the argument to a virtual register
258 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
259 if (ObjSize > 2) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000260 cerr << "LowerFormalArguments Unhandled argument type: "
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000261 << VA.getLocVT().getSimpleVT()
262 << "\n";
263 }
264 // Create the frame index object for this incoming parameter...
265 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
266
267 // Create the SelectionDAG nodes corresponding to a load
268 //from this parameter
Owen Andersone50ed302009-08-10 22:56:29 +0000269 SDValue FIN = DAG.getFrameIndex(FI, EVT::i16);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000270 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
271 PseudoSourceValue::getFixedStack(FI), 0));
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000272 }
273 }
274
Dan Gohman98ca4f22009-08-05 01:29:28 +0000275 return Chain;
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000276}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000277
Dan Gohman98ca4f22009-08-05 01:29:28 +0000278SDValue
279MSP430TargetLowering::LowerReturn(SDValue Chain,
280 unsigned CallConv, bool isVarArg,
281 const SmallVectorImpl<ISD::OutputArg> &Outs,
282 DebugLoc dl, SelectionDAG &DAG) {
283
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000284 // CCValAssign - represent the assignment of the return value to a location
285 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000286
287 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000288 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
289 RVLocs, *DAG.getContext());
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000290
Dan Gohman98ca4f22009-08-05 01:29:28 +0000291 // Analize return values.
292 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000293
294 // If this is the first return lowered for this function, add the regs to the
295 // liveout set for the function.
296 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
297 for (unsigned i = 0; i != RVLocs.size(); ++i)
298 if (RVLocs[i].isRegLoc())
299 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
300 }
301
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000302 SDValue Flag;
303
304 // Copy the result values into the output registers.
305 for (unsigned i = 0; i != RVLocs.size(); ++i) {
306 CCValAssign &VA = RVLocs[i];
307 assert(VA.isRegLoc() && "Can only return in registers!");
308
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000309 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +0000310 Outs[i].Val, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000311
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000312 // Guarantee that all emitted copies are stuck together,
313 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000314 Flag = Chain.getValue(1);
315 }
316
317 if (Flag.getNode())
Owen Andersone50ed302009-08-10 22:56:29 +0000318 return DAG.getNode(MSP430ISD::RET_FLAG, dl, EVT::Other, Chain, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000319
320 // Return Void
Owen Andersone50ed302009-08-10 22:56:29 +0000321 return DAG.getNode(MSP430ISD::RET_FLAG, dl, EVT::Other, Chain);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000322}
323
Anton Korobeynikov44288852009-05-03 13:07:31 +0000324/// LowerCCCCallTo - functions arguments are copied from virtual regs to
325/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
326/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000327SDValue
328MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
329 unsigned CallConv, bool isVarArg,
330 bool isTailCall,
331 const SmallVectorImpl<ISD::OutputArg>
332 &Outs,
333 const SmallVectorImpl<ISD::InputArg> &Ins,
334 DebugLoc dl, SelectionDAG &DAG,
335 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000336 // Analyze operands of the call, assigning locations to each operand.
337 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000338 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
339 ArgLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000340
Dan Gohman98ca4f22009-08-05 01:29:28 +0000341 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000342
343 // Get a count of how many bytes are to be pushed on the stack.
344 unsigned NumBytes = CCInfo.getNextStackOffset();
345
346 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
347 getPointerTy(), true));
348
349 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
350 SmallVector<SDValue, 12> MemOpChains;
351 SDValue StackPtr;
352
353 // Walk the register/memloc assignments, inserting copies/loads.
354 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
355 CCValAssign &VA = ArgLocs[i];
356
Dan Gohman98ca4f22009-08-05 01:29:28 +0000357 SDValue Arg = Outs[i].Val;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000358
359 // Promote the value if needed.
360 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000361 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000362 case CCValAssign::Full: break;
363 case CCValAssign::SExt:
364 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
365 break;
366 case CCValAssign::ZExt:
367 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
368 break;
369 case CCValAssign::AExt:
370 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
371 break;
372 }
373
374 // Arguments that can be passed on register must be kept at RegsToPass
375 // vector
376 if (VA.isRegLoc()) {
377 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
378 } else {
379 assert(VA.isMemLoc());
380
381 if (StackPtr.getNode() == 0)
382 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
383
384 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
385 StackPtr,
386 DAG.getIntPtrConstant(VA.getLocMemOffset()));
387
388
389 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
390 PseudoSourceValue::getStack(),
391 VA.getLocMemOffset()));
392 }
393 }
394
395 // Transform all store nodes into one single node because all store nodes are
396 // independent of each other.
397 if (!MemOpChains.empty())
Owen Andersone50ed302009-08-10 22:56:29 +0000398 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Anton Korobeynikov44288852009-05-03 13:07:31 +0000399 &MemOpChains[0], MemOpChains.size());
400
401 // Build a sequence of copy-to-reg nodes chained together with token chain and
402 // flag operands which copy the outgoing args into registers. The InFlag in
403 // necessary since all emited instructions must be stuck together.
404 SDValue InFlag;
405 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
406 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
407 RegsToPass[i].second, InFlag);
408 InFlag = Chain.getValue(1);
409 }
410
411 // If the callee is a GlobalAddress node (quite common, every direct call is)
412 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
413 // Likewise ExternalSymbol -> TargetExternalSymbol.
414 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Andersone50ed302009-08-10 22:56:29 +0000415 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), EVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000416 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Andersone50ed302009-08-10 22:56:29 +0000417 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), EVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000418
419 // Returns a chain & a flag for retval copy to use.
Owen Andersone50ed302009-08-10 22:56:29 +0000420 SDVTList NodeTys = DAG.getVTList(EVT::Other, EVT::Flag);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000421 SmallVector<SDValue, 8> Ops;
422 Ops.push_back(Chain);
423 Ops.push_back(Callee);
424
425 // Add argument registers to the end of the list so that they are
426 // known live into the call.
427 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
428 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
429 RegsToPass[i].second.getValueType()));
430
431 if (InFlag.getNode())
432 Ops.push_back(InFlag);
433
434 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
435 InFlag = Chain.getValue(1);
436
437 // Create the CALLSEQ_END node.
438 Chain = DAG.getCALLSEQ_END(Chain,
439 DAG.getConstant(NumBytes, getPointerTy(), true),
440 DAG.getConstant(0, getPointerTy(), true),
441 InFlag);
442 InFlag = Chain.getValue(1);
443
444 // Handle result values, copying them out of physregs into vregs that we
445 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000446 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
447 DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000448}
449
Dan Gohman98ca4f22009-08-05 01:29:28 +0000450/// LowerCallResult - Lower the result values of a call into the
451/// appropriate copies out of appropriate physical registers.
452///
453SDValue
Anton Korobeynikov44288852009-05-03 13:07:31 +0000454MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000455 unsigned CallConv, bool isVarArg,
456 const SmallVectorImpl<ISD::InputArg> &Ins,
457 DebugLoc dl, SelectionDAG &DAG,
458 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000459
460 // Assign locations to each value returned by this call.
461 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000462 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000463 RVLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000464
Dan Gohman98ca4f22009-08-05 01:29:28 +0000465 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000466
467 // Copy all of the result registers out of their specified physreg.
468 for (unsigned i = 0; i != RVLocs.size(); ++i) {
469 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
470 RVLocs[i].getValVT(), InFlag).getValue(1);
471 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000472 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000473 }
474
Dan Gohman98ca4f22009-08-05 01:29:28 +0000475 return Chain;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000476}
477
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000478SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
479 SelectionDAG &DAG) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000480 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000481 SDNode* N = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000482 EVT VT = Op.getValueType();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000483 DebugLoc dl = N->getDebugLoc();
484
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000485 // We currently only lower shifts of constant argument.
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000486 if (!isa<ConstantSDNode>(N->getOperand(1)))
487 return SDValue();
488
489 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
490
491 // Expand the stuff into sequence of shifts.
492 // FIXME: for some shift amounts this might be done better!
493 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
494 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000495
496 if (Opc == ISD::SRL && ShiftAmount) {
497 // Emit a special goodness here:
498 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000499 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000500 ShiftAmount -= 1;
501 }
502
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000503 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000504 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000505 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000506
507 return Victim;
508}
509
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000510SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
511 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
512 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
513
514 // Create the TargetGlobalAddress node, folding in the constant offset.
515 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
516 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
517 getPointerTy(), Result);
518}
519
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000520SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
521 SelectionDAG &DAG) {
522 DebugLoc dl = Op.getDebugLoc();
523 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
524 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
525
526 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);;
527}
528
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000529static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, unsigned &TargetCC,
530 ISD::CondCode CC,
531 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000532 // FIXME: Handle bittests someday
533 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
534
535 // FIXME: Handle jump negative someday
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000536 TargetCC = MSP430::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000537 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000538 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000539 case ISD::SETEQ:
540 TargetCC = MSP430::COND_E; // aka COND_Z
541 break;
542 case ISD::SETNE:
543 TargetCC = MSP430::COND_NE; // aka COND_NZ
544 break;
545 case ISD::SETULE:
546 std::swap(LHS, RHS); // FALLTHROUGH
547 case ISD::SETUGE:
548 TargetCC = MSP430::COND_HS; // aka COND_C
549 break;
550 case ISD::SETUGT:
551 std::swap(LHS, RHS); // FALLTHROUGH
552 case ISD::SETULT:
553 TargetCC = MSP430::COND_LO; // aka COND_NC
554 break;
555 case ISD::SETLE:
556 std::swap(LHS, RHS); // FALLTHROUGH
557 case ISD::SETGE:
558 TargetCC = MSP430::COND_GE;
559 break;
560 case ISD::SETGT:
561 std::swap(LHS, RHS); // FALLTHROUGH
562 case ISD::SETLT:
563 TargetCC = MSP430::COND_L;
564 break;
565 }
566
Owen Andersone50ed302009-08-10 22:56:29 +0000567 return DAG.getNode(MSP430ISD::CMP, dl, EVT::Flag, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000568}
569
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000570
571SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000572 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000573 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
574 SDValue LHS = Op.getOperand(2);
575 SDValue RHS = Op.getOperand(3);
576 SDValue Dest = Op.getOperand(4);
577 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000578
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000579 unsigned TargetCC = MSP430::COND_INVALID;
580 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000581
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000582 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
583 Chain,
Owen Andersone50ed302009-08-10 22:56:29 +0000584 Dest, DAG.getConstant(TargetCC, EVT::i8),
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000585 Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000586}
587
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000588SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
589 SDValue LHS = Op.getOperand(0);
590 SDValue RHS = Op.getOperand(1);
591 SDValue TrueV = Op.getOperand(2);
592 SDValue FalseV = Op.getOperand(3);
593 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000594 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000595
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000596 unsigned TargetCC = MSP430::COND_INVALID;
597 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000598
Owen Andersone50ed302009-08-10 22:56:29 +0000599 SDVTList VTs = DAG.getVTList(Op.getValueType(), EVT::Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000600 SmallVector<SDValue, 4> Ops;
601 Ops.push_back(TrueV);
602 Ops.push_back(FalseV);
Owen Andersone50ed302009-08-10 22:56:29 +0000603 Ops.push_back(DAG.getConstant(TargetCC, EVT::i8));
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000604 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000605
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000606 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000607}
608
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000609SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
610 SelectionDAG &DAG) {
611 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000612 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000613 DebugLoc dl = Op.getDebugLoc();
614
Owen Andersone50ed302009-08-10 22:56:29 +0000615 assert(VT == EVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000616
617 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
618 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
619 DAG.getValueType(Val.getValueType()));
620}
621
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000622const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
623 switch (Opcode) {
624 default: return NULL;
625 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000626 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000627 case MSP430ISD::RLA: return "MSP430ISD::RLA";
628 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000629 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000630 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000631 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000632 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000633 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000634 }
635}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000636
637//===----------------------------------------------------------------------===//
638// Other Lowering Code
639//===----------------------------------------------------------------------===//
640
641MachineBasicBlock*
642MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
643 MachineBasicBlock *BB) const {
644 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
645 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda4d2f62009-05-08 18:51:21 +0000646 assert((MI->getOpcode() == MSP430::Select16 ||
647 MI->getOpcode() == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000648 "Unexpected instr type to insert");
649
650 // To "insert" a SELECT instruction, we actually have to insert the diamond
651 // control-flow pattern. The incoming instruction knows the destination vreg
652 // to set, the condition code register to branch on, the true/false values to
653 // select between, and a branch opcode to use.
654 const BasicBlock *LLVM_BB = BB->getBasicBlock();
655 MachineFunction::iterator I = BB;
656 ++I;
657
658 // thisMBB:
659 // ...
660 // TrueVal = ...
661 // cmpTY ccX, r1, r2
662 // jCC copy1MBB
663 // fallthrough --> copy0MBB
664 MachineBasicBlock *thisMBB = BB;
665 MachineFunction *F = BB->getParent();
666 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
667 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
668 BuildMI(BB, dl, TII.get(MSP430::JCC))
669 .addMBB(copy1MBB)
670 .addImm(MI->getOperand(3).getImm());
671 F->insert(I, copy0MBB);
672 F->insert(I, copy1MBB);
673 // Update machine-CFG edges by transferring all successors of the current
674 // block to the new block which will contain the Phi node for the select.
675 copy1MBB->transferSuccessors(BB);
676 // Next, add the true and fallthrough blocks as its successors.
677 BB->addSuccessor(copy0MBB);
678 BB->addSuccessor(copy1MBB);
679
680 // copy0MBB:
681 // %FalseValue = ...
682 // # fallthrough to copy1MBB
683 BB = copy0MBB;
684
685 // Update machine-CFG edges
686 BB->addSuccessor(copy1MBB);
687
688 // copy1MBB:
689 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
690 // ...
691 BB = copy1MBB;
692 BuildMI(BB, dl, TII.get(MSP430::PHI),
693 MI->getOperand(0).getReg())
694 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
695 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
696
697 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
698 return BB;
699}