blob: acfc7c989dd0c57455211373fb21e7d2692442a1 [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 Korobeynikov8b528e52009-05-03 13:12:23 +000061 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000062 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 Korobeynikovbf8ef3f2009-05-03 13:16:37 +000070 setOperationAction(ISD::SRA, MVT::i8, Custom);
71 setOperationAction(ISD::SHL, MVT::i8, Custom);
72 setOperationAction(ISD::SRL, MVT::i8, Custom);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +000073 setOperationAction(ISD::SRA, MVT::i16, Custom);
Anton Korobeynikovea54c982009-05-03 13:13:17 +000074 setOperationAction(ISD::SHL, MVT::i16, Custom);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +000075 setOperationAction(ISD::SRL, MVT::i16, Custom);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +000076 setOperationAction(ISD::RET, MVT::Other, Custom);
77 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +000078 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Anton Korobeynikov0dbf2922009-05-03 13:15:40 +000079 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
80 setOperationAction(ISD::BRIND, MVT::Other, Expand);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +000081 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
82 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
83 setOperationAction(ISD::SETCC, MVT::i8, Custom);
84 setOperationAction(ISD::SETCC, MVT::i16, Custom);
85 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
86 setOperationAction(ISD::SELECT, MVT::i8, Custom);
87 setOperationAction(ISD::SELECT, MVT::i16, Custom);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +000088 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov8725bd22009-05-03 13:14:25 +000089
90 // FIXME: Implement efficiently multiplication by a constant
91 setOperationAction(ISD::MUL, MVT::i16, Expand);
92 setOperationAction(ISD::MULHS, MVT::i16, Expand);
93 setOperationAction(ISD::MULHU, MVT::i16, Expand);
94 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
95 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikovf2f54022009-05-03 13:18:33 +000096
97 setOperationAction(ISD::UDIV, MVT::i16, Expand);
98 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
99 setOperationAction(ISD::SDIV, MVT::i16, Expand);
100 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000101}
102
Anton Korobeynikovb8639f52009-05-03 13:03:50 +0000103SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000104 switch (Op.getOpcode()) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000105 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000106 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000107 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000108 case ISD::SRA: return LowerShifts(Op, DAG);
109 case ISD::RET: return LowerRET(Op, DAG);
110 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000111 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000112 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000113 case ISD::SETCC: return LowerSETCC(Op, DAG);
114 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000115 case ISD::SELECT: return LowerSELECT(Op, DAG);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000116 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000117 default:
118 assert(0 && "unimplemented operand");
119 return SDValue();
120 }
121}
122
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000123//===----------------------------------------------------------------------===//
124// Calling Convention Implementation
125//===----------------------------------------------------------------------===//
126
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000127#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000128
129SDValue MSP430TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
130 SelectionDAG &DAG) {
131 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
132 switch (CC) {
133 default:
134 assert(0 && "Unsupported calling convention");
135 case CallingConv::C:
136 case CallingConv::Fast:
137 return LowerCCCArguments(Op, DAG);
138 }
139}
140
Anton Korobeynikov44288852009-05-03 13:07:31 +0000141SDValue MSP430TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
142 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
143 unsigned CallingConv = TheCall->getCallingConv();
144 switch (CallingConv) {
145 default:
146 assert(0 && "Unsupported calling convention");
147 case CallingConv::Fast:
148 case CallingConv::C:
149 return LowerCCCCallTo(Op, DAG, CallingConv);
150 }
151}
152
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000153/// LowerCCCArguments - transform physical registers into virtual registers and
154/// generate load operations for arguments places on the stack.
155// FIXME: struct return stuff
156// FIXME: varargs
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000157SDValue MSP430TargetLowering::LowerCCCArguments(SDValue Op,
158 SelectionDAG &DAG) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000159 MachineFunction &MF = DAG.getMachineFunction();
160 MachineFrameInfo *MFI = MF.getFrameInfo();
161 MachineRegisterInfo &RegInfo = MF.getRegInfo();
162 SDValue Root = Op.getOperand(0);
163 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
164 unsigned CC = MF.getFunction()->getCallingConv();
165 DebugLoc dl = Op.getDebugLoc();
166
167 // Assign locations to all of the incoming arguments.
168 SmallVector<CCValAssign, 16> ArgLocs;
169 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
170 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MSP430);
171
172 assert(!isVarArg && "Varargs not supported yet");
173
174 SmallVector<SDValue, 16> ArgValues;
175 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
176 CCValAssign &VA = ArgLocs[i];
177 if (VA.isRegLoc()) {
178 // Arguments passed in registers
179 MVT RegVT = VA.getLocVT();
180 switch (RegVT.getSimpleVT()) {
181 default:
182 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
183 << RegVT.getSimpleVT()
184 << "\n";
185 abort();
186 case MVT::i16:
187 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000188 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000189 RegInfo.addLiveIn(VA.getLocReg(), VReg);
190 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
191
192 // If this is an 8-bit value, it is really passed promoted to 16
193 // bits. Insert an assert[sz]ext to capture this, then truncate to the
194 // right size.
195 if (VA.getLocInfo() == CCValAssign::SExt)
196 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
197 DAG.getValueType(VA.getValVT()));
198 else if (VA.getLocInfo() == CCValAssign::ZExt)
199 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
200 DAG.getValueType(VA.getValVT()));
201
202 if (VA.getLocInfo() != CCValAssign::Full)
203 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
204
205 ArgValues.push_back(ArgValue);
206 }
207 } else {
208 // Sanity check
209 assert(VA.isMemLoc());
210 // Load the argument to a virtual register
211 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
212 if (ObjSize > 2) {
213 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
214 << VA.getLocVT().getSimpleVT()
215 << "\n";
216 }
217 // Create the frame index object for this incoming parameter...
218 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
219
220 // Create the SelectionDAG nodes corresponding to a load
221 //from this parameter
222 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
223 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
224 PseudoSourceValue::getFixedStack(FI), 0));
225 }
226 }
227
228 ArgValues.push_back(Root);
229
230 // Return the new list of results.
231 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
232 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
233}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000234
235SDValue MSP430TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
236 // CCValAssign - represent the assignment of the return value to a location
237 SmallVector<CCValAssign, 16> RVLocs;
238 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
239 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
240 DebugLoc dl = Op.getDebugLoc();
241
242 // CCState - Info about the registers and stack slot.
243 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
244
245 // Analize return values of ISD::RET
246 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_MSP430);
247
248 // If this is the first return lowered for this function, add the regs to the
249 // liveout set for the function.
250 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
251 for (unsigned i = 0; i != RVLocs.size(); ++i)
252 if (RVLocs[i].isRegLoc())
253 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
254 }
255
256 // The chain is always operand #0
257 SDValue Chain = Op.getOperand(0);
258 SDValue Flag;
259
260 // Copy the result values into the output registers.
261 for (unsigned i = 0; i != RVLocs.size(); ++i) {
262 CCValAssign &VA = RVLocs[i];
263 assert(VA.isRegLoc() && "Can only return in registers!");
264
265 // ISD::RET => ret chain, (regnum1,val1), ...
266 // So i*2+1 index only the regnums
267 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
268 Op.getOperand(i*2+1), Flag);
269
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000270 // Guarantee that all emitted copies are stuck together,
271 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000272 Flag = Chain.getValue(1);
273 }
274
275 if (Flag.getNode())
276 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
277
278 // Return Void
279 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain);
280}
281
Anton Korobeynikov44288852009-05-03 13:07:31 +0000282/// LowerCCCCallTo - functions arguments are copied from virtual regs to
283/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
284/// TODO: sret.
285SDValue MSP430TargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
286 unsigned CC) {
287 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
288 SDValue Chain = TheCall->getChain();
289 SDValue Callee = TheCall->getCallee();
290 bool isVarArg = TheCall->isVarArg();
291 DebugLoc dl = Op.getDebugLoc();
292
293 // Analyze operands of the call, assigning locations to each operand.
294 SmallVector<CCValAssign, 16> ArgLocs;
295 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
296
297 CCInfo.AnalyzeCallOperands(TheCall, CC_MSP430);
298
299 // Get a count of how many bytes are to be pushed on the stack.
300 unsigned NumBytes = CCInfo.getNextStackOffset();
301
302 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
303 getPointerTy(), true));
304
305 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
306 SmallVector<SDValue, 12> MemOpChains;
307 SDValue StackPtr;
308
309 // Walk the register/memloc assignments, inserting copies/loads.
310 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
311 CCValAssign &VA = ArgLocs[i];
312
313 // Arguments start after the 5 first operands of ISD::CALL
314 SDValue Arg = TheCall->getArg(i);
315
316 // Promote the value if needed.
317 switch (VA.getLocInfo()) {
318 default: assert(0 && "Unknown loc info!");
319 case CCValAssign::Full: break;
320 case CCValAssign::SExt:
321 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
322 break;
323 case CCValAssign::ZExt:
324 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
325 break;
326 case CCValAssign::AExt:
327 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
328 break;
329 }
330
331 // Arguments that can be passed on register must be kept at RegsToPass
332 // vector
333 if (VA.isRegLoc()) {
334 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
335 } else {
336 assert(VA.isMemLoc());
337
338 if (StackPtr.getNode() == 0)
339 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
340
341 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
342 StackPtr,
343 DAG.getIntPtrConstant(VA.getLocMemOffset()));
344
345
346 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
347 PseudoSourceValue::getStack(),
348 VA.getLocMemOffset()));
349 }
350 }
351
352 // Transform all store nodes into one single node because all store nodes are
353 // independent of each other.
354 if (!MemOpChains.empty())
355 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
356 &MemOpChains[0], MemOpChains.size());
357
358 // Build a sequence of copy-to-reg nodes chained together with token chain and
359 // flag operands which copy the outgoing args into registers. The InFlag in
360 // necessary since all emited instructions must be stuck together.
361 SDValue InFlag;
362 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
363 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
364 RegsToPass[i].second, InFlag);
365 InFlag = Chain.getValue(1);
366 }
367
368 // If the callee is a GlobalAddress node (quite common, every direct call is)
369 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
370 // Likewise ExternalSymbol -> TargetExternalSymbol.
371 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
372 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i16);
373 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
374 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
375
376 // Returns a chain & a flag for retval copy to use.
377 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
378 SmallVector<SDValue, 8> Ops;
379 Ops.push_back(Chain);
380 Ops.push_back(Callee);
381
382 // Add argument registers to the end of the list so that they are
383 // known live into the call.
384 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
385 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
386 RegsToPass[i].second.getValueType()));
387
388 if (InFlag.getNode())
389 Ops.push_back(InFlag);
390
391 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
392 InFlag = Chain.getValue(1);
393
394 // Create the CALLSEQ_END node.
395 Chain = DAG.getCALLSEQ_END(Chain,
396 DAG.getConstant(NumBytes, getPointerTy(), true),
397 DAG.getConstant(0, getPointerTy(), true),
398 InFlag);
399 InFlag = Chain.getValue(1);
400
401 // Handle result values, copying them out of physregs into vregs that we
402 // return.
403 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
404 Op.getResNo());
405}
406
407/// LowerCallResult - Lower the result values of an ISD::CALL into the
408/// appropriate copies out of appropriate physical registers. This assumes that
409/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
410/// being lowered. Returns a SDNode with the same number of values as the
411/// ISD::CALL.
412SDNode*
413MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
414 CallSDNode *TheCall,
415 unsigned CallingConv,
416 SelectionDAG &DAG) {
417 bool isVarArg = TheCall->isVarArg();
418 DebugLoc dl = TheCall->getDebugLoc();
419
420 // Assign locations to each value returned by this call.
421 SmallVector<CCValAssign, 16> RVLocs;
422 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
423
424 CCInfo.AnalyzeCallResult(TheCall, RetCC_MSP430);
425 SmallVector<SDValue, 8> ResultVals;
426
427 // Copy all of the result registers out of their specified physreg.
428 for (unsigned i = 0; i != RVLocs.size(); ++i) {
429 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
430 RVLocs[i].getValVT(), InFlag).getValue(1);
431 InFlag = Chain.getValue(2);
432 ResultVals.push_back(Chain.getValue(0));
433 }
434
435 ResultVals.push_back(Chain);
436
437 // Merge everything together with a MERGE_VALUES node.
438 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
439 &ResultVals[0], ResultVals.size()).getNode();
440}
441
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000442SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
443 SelectionDAG &DAG) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000444 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000445 SDNode* N = Op.getNode();
446 MVT VT = Op.getValueType();
447 DebugLoc dl = N->getDebugLoc();
448
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000449 // We currently only lower shifts of constant argument.
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000450 if (!isa<ConstantSDNode>(N->getOperand(1)))
451 return SDValue();
452
453 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
454
455 // Expand the stuff into sequence of shifts.
456 // FIXME: for some shift amounts this might be done better!
457 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
458 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000459
460 if (Opc == ISD::SRL && ShiftAmount) {
461 // Emit a special goodness here:
462 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000463 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000464 ShiftAmount -= 1;
465 }
466
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000467 while (ShiftAmount--)
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000468 Victim = DAG.getNode((Opc == ISD::SRA ? MSP430ISD::RRA : MSP430ISD::RLA),
469 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000470
471 return Victim;
472}
473
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000474SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
475 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
476 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
477
478 // Create the TargetGlobalAddress node, folding in the constant offset.
479 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
480 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
481 getPointerTy(), Result);
482}
483
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000484SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
485 SelectionDAG &DAG) {
486 DebugLoc dl = Op.getDebugLoc();
487 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
488 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
489
490 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);;
491}
492
493
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000494MVT MSP430TargetLowering::getSetCCResultType(MVT VT) const {
495 return MVT::i8;
496}
497
498SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
499 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
500 SDValue LHS = Op.getOperand(0);
501 SDValue RHS = Op.getOperand(1);
502 DebugLoc dl = Op.getDebugLoc();
503 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
504
505 // FIXME: Handle bittests someday
506 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
507
508 // FIXME: Handle jump negative someday
509 unsigned TargetCC = 0;
510 switch (CC) {
511 default: assert(0 && "Invalid integer condition!");
512 case ISD::SETEQ:
513 TargetCC = MSP430::COND_E; // aka COND_Z
514 break;
515 case ISD::SETNE:
516 TargetCC = MSP430::COND_NE; // aka COND_NZ
517 break;
518 case ISD::SETULE:
519 std::swap(LHS, RHS); // FALLTHROUGH
520 case ISD::SETUGE:
521 TargetCC = MSP430::COND_HS; // aka COND_C
522 break;
523 case ISD::SETUGT:
524 std::swap(LHS, RHS); // FALLTHROUGH
525 case ISD::SETULT:
526 TargetCC = MSP430::COND_LO; // aka COND_NC
527 break;
528 case ISD::SETLE:
529 std::swap(LHS, RHS); // FALLTHROUGH
530 case ISD::SETGE:
531 TargetCC = MSP430::COND_GE;
532 break;
533 case ISD::SETGT:
534 std::swap(LHS, RHS); // FALLTHROUGH
535 case ISD::SETLT:
536 TargetCC = MSP430::COND_L;
537 break;
538 }
539
540 SDValue Cond = DAG.getNode(MSP430ISD::CMP, dl, MVT::i16, LHS, RHS);
541 return DAG.getNode(MSP430ISD::SETCC, dl, MVT::i8,
542 DAG.getConstant(TargetCC, MVT::i8), Cond);
543}
544
545SDValue MSP430TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
546 SDValue Chain = Op.getOperand(0);
547 SDValue Cond = Op.getOperand(1);
548 SDValue Dest = Op.getOperand(2);
549 DebugLoc dl = Op.getDebugLoc();
550 SDValue CC;
551
552 // Lower condition if not lowered yet
553 if (Cond.getOpcode() == ISD::SETCC)
554 Cond = LowerSETCC(Cond, DAG);
555
556 // If condition flag is set by a MSP430ISD::CMP, then use it as the condition
557 // setting operand in place of the MSP430ISD::SETCC.
558 if (Cond.getOpcode() == MSP430ISD::SETCC) {
559 CC = Cond.getOperand(0);
560 Cond = Cond.getOperand(1);
561 } else
562 assert(0 && "Unimplemented condition!");
563
564 return DAG.getNode(MSP430ISD::BRCOND, dl, Op.getValueType(),
565 Chain, Dest, CC, Cond);
566}
567
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000568SDValue MSP430TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
569 SDValue Cond = Op.getOperand(0);
570 SDValue TrueV = Op.getOperand(1);
571 SDValue FalseV = Op.getOperand(2);
572 DebugLoc dl = Op.getDebugLoc();
573 SDValue CC;
574
575 // Lower condition if not lowered yet
576 if (Cond.getOpcode() == ISD::SETCC)
577 Cond = LowerSETCC(Cond, DAG);
578
579 // If condition flag is set by a MSP430ISD::CMP, then use it as the condition
580 // setting operand in place of the MSP430ISD::SETCC.
581 if (Cond.getOpcode() == MSP430ISD::SETCC) {
582 CC = Cond.getOperand(0);
583 Cond = Cond.getOperand(1);
584 TrueV = Cond.getOperand(0);
585 FalseV = Cond.getOperand(1);
586 } else {
587 CC = DAG.getConstant(MSP430::COND_NE, MVT::i16);
588 Cond = DAG.getNode(MSP430ISD::CMP, dl, MVT::i16,
589 Cond, DAG.getConstant(0, MVT::i16));
590 }
591
592 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
593 SmallVector<SDValue, 4> Ops;
594 Ops.push_back(TrueV);
595 Ops.push_back(FalseV);
596 Ops.push_back(CC);
597 Ops.push_back(Cond);
598
599 return DAG.getNode(MSP430ISD::SELECT, dl, VTs, &Ops[0], Ops.size());
600}
601
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000602SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
603 SelectionDAG &DAG) {
604 SDValue Val = Op.getOperand(0);
605 MVT VT = Op.getValueType();
606 DebugLoc dl = Op.getDebugLoc();
607
608 assert(VT == MVT::i16 && "Only support i16 for now!");
609
610 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
611 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
612 DAG.getValueType(Val.getValueType()));
613}
614
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000615const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
616 switch (Opcode) {
617 default: return NULL;
618 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000619 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000620 case MSP430ISD::RLA: return "MSP430ISD::RLA";
621 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000622 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000623 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000624 case MSP430ISD::BRCOND: return "MSP430ISD::BRCOND";
625 case MSP430ISD::CMP: return "MSP430ISD::CMP";
626 case MSP430ISD::SETCC: return "MSP430ISD::SETCC";
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000627 case MSP430ISD::SELECT: return "MSP430ISD::SELECT";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000628 }
629}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000630
631//===----------------------------------------------------------------------===//
632// Other Lowering Code
633//===----------------------------------------------------------------------===//
634
635MachineBasicBlock*
636MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
637 MachineBasicBlock *BB) const {
638 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
639 DebugLoc dl = MI->getDebugLoc();
640 assert((MI->getOpcode() == MSP430::Select16) &&
641 "Unexpected instr type to insert");
642
643 // To "insert" a SELECT instruction, we actually have to insert the diamond
644 // control-flow pattern. The incoming instruction knows the destination vreg
645 // to set, the condition code register to branch on, the true/false values to
646 // select between, and a branch opcode to use.
647 const BasicBlock *LLVM_BB = BB->getBasicBlock();
648 MachineFunction::iterator I = BB;
649 ++I;
650
651 // thisMBB:
652 // ...
653 // TrueVal = ...
654 // cmpTY ccX, r1, r2
655 // jCC copy1MBB
656 // fallthrough --> copy0MBB
657 MachineBasicBlock *thisMBB = BB;
658 MachineFunction *F = BB->getParent();
659 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
660 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
661 BuildMI(BB, dl, TII.get(MSP430::JCC))
662 .addMBB(copy1MBB)
663 .addImm(MI->getOperand(3).getImm());
664 F->insert(I, copy0MBB);
665 F->insert(I, copy1MBB);
666 // Update machine-CFG edges by transferring all successors of the current
667 // block to the new block which will contain the Phi node for the select.
668 copy1MBB->transferSuccessors(BB);
669 // Next, add the true and fallthrough blocks as its successors.
670 BB->addSuccessor(copy0MBB);
671 BB->addSuccessor(copy1MBB);
672
673 // copy0MBB:
674 // %FalseValue = ...
675 // # fallthrough to copy1MBB
676 BB = copy0MBB;
677
678 // Update machine-CFG edges
679 BB->addSuccessor(copy1MBB);
680
681 // copy1MBB:
682 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
683 // ...
684 BB = copy1MBB;
685 BuildMI(BB, dl, TII.get(MSP430::PHI),
686 MI->getOperand(0).getReg())
687 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
688 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
689
690 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
691 return BB;
692}