blob: ab8be5a3fbbf09115a4a54f5b18cd126912b8b4f [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"
34#include "llvm/Support/Debug.h"
35#include "llvm/ADT/VectorExtras.h"
36using namespace llvm;
37
38MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
39 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
40
41 // Set up the register classes.
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000042 addRegisterClass(MVT::i8, MSP430::GR8RegisterClass);
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000043 addRegisterClass(MVT::i16, MSP430::GR16RegisterClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000044
45 // Compute derived properties from the register classes
46 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000047
Anton Korobeynikov1476d972009-05-03 13:03:14 +000048 // Provide all sorts of operation actions
49
50 // Division is expensive
51 setIntDivIsCheap(false);
52
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000053 // Even if we have only 1 bit shift here, we can perform
54 // shifts of the whole bitwidth 1 bit per step.
55 setShiftAmountType(MVT::i8);
56
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000057 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
58 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
59 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
60 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
61 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
62
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000063 // We don't have any truncstores
64 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
65
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000066 setOperationAction(ISD::SRA, MVT::i16, Custom);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000067 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000068}
69
Anton Korobeynikovb8639f52009-05-03 13:03:50 +000070SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000071 switch (Op.getOpcode()) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000072 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Anton Korobeynikov44288852009-05-03 13:07:31 +000073 case ISD::SRA: return LowerShifts(Op, DAG);
74 case ISD::RET: return LowerRET(Op, DAG);
75 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000076 default:
77 assert(0 && "unimplemented operand");
78 return SDValue();
79 }
80}
81
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000082//===----------------------------------------------------------------------===//
83// Calling Convention Implementation
84//===----------------------------------------------------------------------===//
85
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000086#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000087
88SDValue MSP430TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
89 SelectionDAG &DAG) {
90 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
91 switch (CC) {
92 default:
93 assert(0 && "Unsupported calling convention");
94 case CallingConv::C:
95 case CallingConv::Fast:
96 return LowerCCCArguments(Op, DAG);
97 }
98}
99
Anton Korobeynikov44288852009-05-03 13:07:31 +0000100SDValue MSP430TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
101 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
102 unsigned CallingConv = TheCall->getCallingConv();
103 switch (CallingConv) {
104 default:
105 assert(0 && "Unsupported calling convention");
106 case CallingConv::Fast:
107 case CallingConv::C:
108 return LowerCCCCallTo(Op, DAG, CallingConv);
109 }
110}
111
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000112/// LowerCCCArguments - transform physical registers into virtual registers and
113/// generate load operations for arguments places on the stack.
114// FIXME: struct return stuff
115// FIXME: varargs
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000116SDValue MSP430TargetLowering::LowerCCCArguments(SDValue Op,
117 SelectionDAG &DAG) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000118 MachineFunction &MF = DAG.getMachineFunction();
119 MachineFrameInfo *MFI = MF.getFrameInfo();
120 MachineRegisterInfo &RegInfo = MF.getRegInfo();
121 SDValue Root = Op.getOperand(0);
122 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
123 unsigned CC = MF.getFunction()->getCallingConv();
124 DebugLoc dl = Op.getDebugLoc();
125
126 // Assign locations to all of the incoming arguments.
127 SmallVector<CCValAssign, 16> ArgLocs;
128 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
129 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MSP430);
130
131 assert(!isVarArg && "Varargs not supported yet");
132
133 SmallVector<SDValue, 16> ArgValues;
134 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
135 CCValAssign &VA = ArgLocs[i];
136 if (VA.isRegLoc()) {
137 // Arguments passed in registers
138 MVT RegVT = VA.getLocVT();
139 switch (RegVT.getSimpleVT()) {
140 default:
141 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
142 << RegVT.getSimpleVT()
143 << "\n";
144 abort();
145 case MVT::i16:
146 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000147 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000148 RegInfo.addLiveIn(VA.getLocReg(), VReg);
149 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
150
151 // If this is an 8-bit value, it is really passed promoted to 16
152 // bits. Insert an assert[sz]ext to capture this, then truncate to the
153 // right size.
154 if (VA.getLocInfo() == CCValAssign::SExt)
155 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
156 DAG.getValueType(VA.getValVT()));
157 else if (VA.getLocInfo() == CCValAssign::ZExt)
158 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
159 DAG.getValueType(VA.getValVT()));
160
161 if (VA.getLocInfo() != CCValAssign::Full)
162 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
163
164 ArgValues.push_back(ArgValue);
165 }
166 } else {
167 // Sanity check
168 assert(VA.isMemLoc());
169 // Load the argument to a virtual register
170 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
171 if (ObjSize > 2) {
172 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
173 << VA.getLocVT().getSimpleVT()
174 << "\n";
175 }
176 // Create the frame index object for this incoming parameter...
177 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
178
179 // Create the SelectionDAG nodes corresponding to a load
180 //from this parameter
181 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
182 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
183 PseudoSourceValue::getFixedStack(FI), 0));
184 }
185 }
186
187 ArgValues.push_back(Root);
188
189 // Return the new list of results.
190 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
191 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
192}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000193
194SDValue MSP430TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
195 // CCValAssign - represent the assignment of the return value to a location
196 SmallVector<CCValAssign, 16> RVLocs;
197 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
198 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
199 DebugLoc dl = Op.getDebugLoc();
200
201 // CCState - Info about the registers and stack slot.
202 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
203
204 // Analize return values of ISD::RET
205 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_MSP430);
206
207 // If this is the first return lowered for this function, add the regs to the
208 // liveout set for the function.
209 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
210 for (unsigned i = 0; i != RVLocs.size(); ++i)
211 if (RVLocs[i].isRegLoc())
212 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
213 }
214
215 // The chain is always operand #0
216 SDValue Chain = Op.getOperand(0);
217 SDValue Flag;
218
219 // Copy the result values into the output registers.
220 for (unsigned i = 0; i != RVLocs.size(); ++i) {
221 CCValAssign &VA = RVLocs[i];
222 assert(VA.isRegLoc() && "Can only return in registers!");
223
224 // ISD::RET => ret chain, (regnum1,val1), ...
225 // So i*2+1 index only the regnums
226 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
227 Op.getOperand(i*2+1), Flag);
228
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000229 // Guarantee that all emitted copies are stuck together,
230 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000231 Flag = Chain.getValue(1);
232 }
233
234 if (Flag.getNode())
235 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
236
237 // Return Void
238 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain);
239}
240
Anton Korobeynikov44288852009-05-03 13:07:31 +0000241/// LowerCCCCallTo - functions arguments are copied from virtual regs to
242/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
243/// TODO: sret.
244SDValue MSP430TargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
245 unsigned CC) {
246 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
247 SDValue Chain = TheCall->getChain();
248 SDValue Callee = TheCall->getCallee();
249 bool isVarArg = TheCall->isVarArg();
250 DebugLoc dl = Op.getDebugLoc();
251
252 // Analyze operands of the call, assigning locations to each operand.
253 SmallVector<CCValAssign, 16> ArgLocs;
254 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
255
256 CCInfo.AnalyzeCallOperands(TheCall, CC_MSP430);
257
258 // Get a count of how many bytes are to be pushed on the stack.
259 unsigned NumBytes = CCInfo.getNextStackOffset();
260
261 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
262 getPointerTy(), true));
263
264 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
265 SmallVector<SDValue, 12> MemOpChains;
266 SDValue StackPtr;
267
268 // Walk the register/memloc assignments, inserting copies/loads.
269 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
270 CCValAssign &VA = ArgLocs[i];
271
272 // Arguments start after the 5 first operands of ISD::CALL
273 SDValue Arg = TheCall->getArg(i);
274
275 // Promote the value if needed.
276 switch (VA.getLocInfo()) {
277 default: assert(0 && "Unknown loc info!");
278 case CCValAssign::Full: break;
279 case CCValAssign::SExt:
280 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
281 break;
282 case CCValAssign::ZExt:
283 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
284 break;
285 case CCValAssign::AExt:
286 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
287 break;
288 }
289
290 // Arguments that can be passed on register must be kept at RegsToPass
291 // vector
292 if (VA.isRegLoc()) {
293 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
294 } else {
295 assert(VA.isMemLoc());
296
297 if (StackPtr.getNode() == 0)
298 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
299
300 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
301 StackPtr,
302 DAG.getIntPtrConstant(VA.getLocMemOffset()));
303
304
305 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
306 PseudoSourceValue::getStack(),
307 VA.getLocMemOffset()));
308 }
309 }
310
311 // Transform all store nodes into one single node because all store nodes are
312 // independent of each other.
313 if (!MemOpChains.empty())
314 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
315 &MemOpChains[0], MemOpChains.size());
316
317 // Build a sequence of copy-to-reg nodes chained together with token chain and
318 // flag operands which copy the outgoing args into registers. The InFlag in
319 // necessary since all emited instructions must be stuck together.
320 SDValue InFlag;
321 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
322 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
323 RegsToPass[i].second, InFlag);
324 InFlag = Chain.getValue(1);
325 }
326
327 // If the callee is a GlobalAddress node (quite common, every direct call is)
328 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
329 // Likewise ExternalSymbol -> TargetExternalSymbol.
330 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
331 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i16);
332 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
333 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
334
335 // Returns a chain & a flag for retval copy to use.
336 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
337 SmallVector<SDValue, 8> Ops;
338 Ops.push_back(Chain);
339 Ops.push_back(Callee);
340
341 // Add argument registers to the end of the list so that they are
342 // known live into the call.
343 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
344 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
345 RegsToPass[i].second.getValueType()));
346
347 if (InFlag.getNode())
348 Ops.push_back(InFlag);
349
350 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
351 InFlag = Chain.getValue(1);
352
353 // Create the CALLSEQ_END node.
354 Chain = DAG.getCALLSEQ_END(Chain,
355 DAG.getConstant(NumBytes, getPointerTy(), true),
356 DAG.getConstant(0, getPointerTy(), true),
357 InFlag);
358 InFlag = Chain.getValue(1);
359
360 // Handle result values, copying them out of physregs into vregs that we
361 // return.
362 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
363 Op.getResNo());
364}
365
366/// LowerCallResult - Lower the result values of an ISD::CALL into the
367/// appropriate copies out of appropriate physical registers. This assumes that
368/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
369/// being lowered. Returns a SDNode with the same number of values as the
370/// ISD::CALL.
371SDNode*
372MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
373 CallSDNode *TheCall,
374 unsigned CallingConv,
375 SelectionDAG &DAG) {
376 bool isVarArg = TheCall->isVarArg();
377 DebugLoc dl = TheCall->getDebugLoc();
378
379 // Assign locations to each value returned by this call.
380 SmallVector<CCValAssign, 16> RVLocs;
381 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
382
383 CCInfo.AnalyzeCallResult(TheCall, RetCC_MSP430);
384 SmallVector<SDValue, 8> ResultVals;
385
386 // Copy all of the result registers out of their specified physreg.
387 for (unsigned i = 0; i != RVLocs.size(); ++i) {
388 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
389 RVLocs[i].getValVT(), InFlag).getValue(1);
390 InFlag = Chain.getValue(2);
391 ResultVals.push_back(Chain.getValue(0));
392 }
393
394 ResultVals.push_back(Chain);
395
396 // Merge everything together with a MERGE_VALUES node.
397 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
398 &ResultVals[0], ResultVals.size()).getNode();
399}
400
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000401SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
402 SelectionDAG &DAG) {
403 assert(Op.getOpcode() == ISD::SRA && "Only SRA is currently supported.");
404 SDNode* N = Op.getNode();
405 MVT VT = Op.getValueType();
406 DebugLoc dl = N->getDebugLoc();
407
408 // We currently only lower SRA of constant argument.
409 if (!isa<ConstantSDNode>(N->getOperand(1)))
410 return SDValue();
411
412 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
413
414 // Expand the stuff into sequence of shifts.
415 // FIXME: for some shift amounts this might be done better!
416 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
417 SDValue Victim = N->getOperand(0);
418 while (ShiftAmount--)
419 Victim = DAG.getNode(MSP430ISD::RRA, dl, VT, Victim);
420
421 return Victim;
422}
423
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000424const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
425 switch (Opcode) {
426 default: return NULL;
427 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000428 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000429 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000430 }
431}