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