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